sm-crypto-v2 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +50 -28
- package/benchmark/index.js +14 -14
- package/benchmark/pnpm-lock.yaml +5 -1
- package/dist/index.d.ts +52 -61
- package/dist/index.js +458 -268
- package/dist/index.mjs +454 -264
- package/package.json +1 -1
- package/pnpm-lock.yaml +5 -1
- package/src/index.ts +2 -2
- package/src/sm2/ec.ts +8 -85
- package/src/sm2/hmac.ts +76 -0
- package/src/sm2/index.ts +32 -59
- package/src/sm2/kx.ts +80 -0
- package/src/sm2/rng.ts +71 -0
- package/src/sm2/sm3.ts +187 -120
- package/src/sm2/utils.ts +7 -24
- package/src/sm3/index.ts +7 -4
- package/src/sm3/utils.ts +117 -0
- package/src/sm4/index.ts +10 -12
package/dist/index.mjs
CHANGED
@@ -7,11 +7,12 @@ var __export = (target, all) => {
|
|
7
7
|
// src/sm2/index.ts
|
8
8
|
var sm2_exports = {};
|
9
9
|
__export(sm2_exports, {
|
10
|
+
EmptyArray: () => EmptyArray,
|
10
11
|
arrayToHex: () => arrayToHex,
|
11
12
|
arrayToUtf8: () => arrayToUtf8,
|
13
|
+
calculateSharedKey: () => calculateSharedKey,
|
12
14
|
comparePublicKeyHex: () => comparePublicKeyHex,
|
13
15
|
compressPublicKeyHex: () => compressPublicKeyHex,
|
14
|
-
concatArray: () => concatArray,
|
15
16
|
doDecrypt: () => doDecrypt,
|
16
17
|
doEncrypt: () => doEncrypt,
|
17
18
|
doSignature: () => doSignature,
|
@@ -22,7 +23,7 @@ __export(sm2_exports, {
|
|
22
23
|
getPublicKeyFromPrivateKey: () => getPublicKeyFromPrivateKey,
|
23
24
|
hexToArray: () => hexToArray,
|
24
25
|
initRNGPool: () => initRNGPool,
|
25
|
-
leftPad: () =>
|
26
|
+
leftPad: () => leftPad,
|
26
27
|
utf8ToHex: () => utf8ToHex,
|
27
28
|
verifyPublicKey: () => verifyPublicKey
|
28
29
|
});
|
@@ -157,181 +158,7 @@ import * as utils2 from "@noble/curves/abstract/utils";
|
|
157
158
|
import { weierstrass } from "@noble/curves/abstract/weierstrass";
|
158
159
|
import { Field } from "@noble/curves/abstract/modular";
|
159
160
|
|
160
|
-
// src/sm2/
|
161
|
-
var W = new Uint32Array(68);
|
162
|
-
var M = new Uint32Array(64);
|
163
|
-
function rotl(x, n) {
|
164
|
-
const s = n & 31;
|
165
|
-
return x << s | x >>> 32 - s;
|
166
|
-
}
|
167
|
-
function xor(x, y) {
|
168
|
-
const result = new Uint8Array(x.length);
|
169
|
-
for (let i = x.length - 1; i >= 0; i--)
|
170
|
-
result[i] = (x[i] ^ y[i]) & 255;
|
171
|
-
return result;
|
172
|
-
}
|
173
|
-
function P0(X) {
|
174
|
-
return X ^ rotl(X, 9) ^ rotl(X, 17);
|
175
|
-
}
|
176
|
-
function P1(X) {
|
177
|
-
return X ^ rotl(X, 15) ^ rotl(X, 23);
|
178
|
-
}
|
179
|
-
function sm3(array) {
|
180
|
-
let len = array.length * 8;
|
181
|
-
let k = len % 512;
|
182
|
-
k = k >= 448 ? 512 - k % 448 - 1 : 448 - k - 1;
|
183
|
-
const kArr = new Array((k - 7) / 8);
|
184
|
-
const lenArr = new Array(8);
|
185
|
-
for (let i = 0, len2 = kArr.length; i < len2; i++)
|
186
|
-
kArr[i] = 0;
|
187
|
-
for (let i = 0, len2 = lenArr.length; i < len2; i++)
|
188
|
-
lenArr[i] = 0;
|
189
|
-
let lenString = len.toString(2);
|
190
|
-
for (let i = 7; i >= 0; i--) {
|
191
|
-
if (lenString.length > 8) {
|
192
|
-
const start = lenString.length - 8;
|
193
|
-
lenArr[i] = parseInt(lenString.substring(start), 2);
|
194
|
-
lenString = lenString.substring(0, start);
|
195
|
-
} else if (lenString.length > 0) {
|
196
|
-
lenArr[i] = parseInt(lenString, 2);
|
197
|
-
lenString = "";
|
198
|
-
}
|
199
|
-
}
|
200
|
-
const m = Uint8Array.from([...array, 128, ...kArr, ...lenArr]);
|
201
|
-
const dataView = new DataView(m.buffer, 0);
|
202
|
-
const n = m.length / 64;
|
203
|
-
const V = new Uint32Array([1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214]);
|
204
|
-
for (let i = 0; i < n; i++) {
|
205
|
-
W.fill(0);
|
206
|
-
M.fill(0);
|
207
|
-
const start = 16 * i;
|
208
|
-
for (let j = 0; j < 16; j++) {
|
209
|
-
W[j] = dataView.getUint32((start + j) * 4, false);
|
210
|
-
}
|
211
|
-
for (let j = 16; j < 68; j++) {
|
212
|
-
W[j] = P1(W[j - 16] ^ W[j - 9] ^ rotl(W[j - 3], 15)) ^ rotl(W[j - 13], 7) ^ W[j - 6];
|
213
|
-
}
|
214
|
-
for (let j = 0; j < 64; j++) {
|
215
|
-
M[j] = W[j] ^ W[j + 4];
|
216
|
-
}
|
217
|
-
const T1 = 2043430169;
|
218
|
-
const T2 = 2055708042;
|
219
|
-
let A = V[0];
|
220
|
-
let B = V[1];
|
221
|
-
let C = V[2];
|
222
|
-
let D = V[3];
|
223
|
-
let E = V[4];
|
224
|
-
let F = V[5];
|
225
|
-
let G = V[6];
|
226
|
-
let H = V[7];
|
227
|
-
let SS1;
|
228
|
-
let SS2;
|
229
|
-
let TT1;
|
230
|
-
let TT2;
|
231
|
-
let T;
|
232
|
-
for (let j = 0; j < 64; j++) {
|
233
|
-
T = j >= 0 && j <= 15 ? T1 : T2;
|
234
|
-
SS1 = rotl(rotl(A, 12) + E + rotl(T, j), 7);
|
235
|
-
SS2 = SS1 ^ rotl(A, 12);
|
236
|
-
TT1 = (j >= 0 && j <= 15 ? A ^ B ^ C : A & B | A & C | B & C) + D + SS2 + M[j];
|
237
|
-
TT2 = (j >= 0 && j <= 15 ? E ^ F ^ G : E & F | ~E & G) + H + SS1 + W[j];
|
238
|
-
D = C;
|
239
|
-
C = rotl(B, 9);
|
240
|
-
B = A;
|
241
|
-
A = TT1;
|
242
|
-
H = G;
|
243
|
-
G = rotl(F, 19);
|
244
|
-
F = E;
|
245
|
-
E = P0(TT2);
|
246
|
-
}
|
247
|
-
V[0] ^= A;
|
248
|
-
V[1] ^= B;
|
249
|
-
V[2] ^= C;
|
250
|
-
V[3] ^= D;
|
251
|
-
V[4] ^= E;
|
252
|
-
V[5] ^= F;
|
253
|
-
V[6] ^= G;
|
254
|
-
V[7] ^= H;
|
255
|
-
}
|
256
|
-
const result = new Uint8Array(V.length * 4);
|
257
|
-
for (let i = 0, len2 = V.length; i < len2; i++) {
|
258
|
-
const word = V[i];
|
259
|
-
result[i * 4] = (word & 4278190080) >>> 24;
|
260
|
-
result[i * 4 + 1] = (word & 16711680) >>> 16;
|
261
|
-
result[i * 4 + 2] = (word & 65280) >>> 8;
|
262
|
-
result[i * 4 + 3] = word & 255;
|
263
|
-
}
|
264
|
-
return result;
|
265
|
-
}
|
266
|
-
var blockLen = 64;
|
267
|
-
var iPad = new Uint8Array(blockLen);
|
268
|
-
var oPad = new Uint8Array(blockLen);
|
269
|
-
for (let i = 0; i < blockLen; i++) {
|
270
|
-
iPad[i] = 54;
|
271
|
-
oPad[i] = 92;
|
272
|
-
}
|
273
|
-
function hmac(input, key) {
|
274
|
-
if (key.length > blockLen)
|
275
|
-
key = sm3(key);
|
276
|
-
while (key.length < blockLen) {
|
277
|
-
const padKey = new Uint8Array(blockLen);
|
278
|
-
padKey.set(key);
|
279
|
-
key = padKey;
|
280
|
-
}
|
281
|
-
const iPadKey = xor(key, iPad);
|
282
|
-
const oPadKey = xor(key, oPad);
|
283
|
-
const hash = sm3(concatArray(iPadKey, input));
|
284
|
-
return sm3(concatArray(oPadKey, hash));
|
285
|
-
}
|
286
|
-
|
287
|
-
// src/sm3/index.ts
|
288
|
-
var sm3_exports = {};
|
289
|
-
__export(sm3_exports, {
|
290
|
-
sm3: () => sm32,
|
291
|
-
utf8ToArray: () => utf8ToArray
|
292
|
-
});
|
293
|
-
function utf8ToArray(str) {
|
294
|
-
const arr = [];
|
295
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
296
|
-
const point = str.codePointAt(i);
|
297
|
-
if (point <= 127) {
|
298
|
-
arr.push(point);
|
299
|
-
} else if (point <= 2047) {
|
300
|
-
arr.push(192 | point >>> 6);
|
301
|
-
arr.push(128 | point & 63);
|
302
|
-
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
303
|
-
arr.push(224 | point >>> 12);
|
304
|
-
arr.push(128 | point >>> 6 & 63);
|
305
|
-
arr.push(128 | point & 63);
|
306
|
-
} else if (point >= 65536 && point <= 1114111) {
|
307
|
-
i++;
|
308
|
-
arr.push(240 | point >>> 18 & 28);
|
309
|
-
arr.push(128 | point >>> 12 & 63);
|
310
|
-
arr.push(128 | point >>> 6 & 63);
|
311
|
-
arr.push(128 | point & 63);
|
312
|
-
} else {
|
313
|
-
arr.push(point);
|
314
|
-
throw new Error("input is not supported");
|
315
|
-
}
|
316
|
-
}
|
317
|
-
return new Uint8Array(arr);
|
318
|
-
}
|
319
|
-
function sm32(input, options) {
|
320
|
-
input = typeof input === "string" ? utf8ToArray(input) : input;
|
321
|
-
if (options) {
|
322
|
-
const mode = options.mode || "hmac";
|
323
|
-
if (mode !== "hmac")
|
324
|
-
throw new Error("invalid mode");
|
325
|
-
let key = options.key;
|
326
|
-
if (!key)
|
327
|
-
throw new Error("invalid key");
|
328
|
-
key = typeof key === "string" ? hexToArray(key) : key;
|
329
|
-
return arrayToHex(Array.from(hmac(input, key)));
|
330
|
-
}
|
331
|
-
return arrayToHex(Array.from(sm3(input)));
|
332
|
-
}
|
333
|
-
|
334
|
-
// src/sm2/ec.ts
|
161
|
+
// src/sm2/rng.ts
|
335
162
|
var DEFAULT_PRNG_POOL_SIZE = 16384;
|
336
163
|
var prngPool = new Uint8Array(0);
|
337
164
|
var _syncCrypto;
|
@@ -386,13 +213,304 @@ function randomBytes(length = 0) {
|
|
386
213
|
return result;
|
387
214
|
}
|
388
215
|
}
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
216
|
+
|
217
|
+
// src/sm3/utils.ts
|
218
|
+
var u8a = (a) => a instanceof Uint8Array;
|
219
|
+
var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
220
|
+
var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
221
|
+
if (!isLE)
|
222
|
+
throw new Error("Non little-endian hardware is not supported");
|
223
|
+
var hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, "0"));
|
224
|
+
function bytesToHex(bytes) {
|
225
|
+
if (!u8a(bytes))
|
226
|
+
throw new Error("Uint8Array expected");
|
227
|
+
let hex = "";
|
228
|
+
for (let i = 0; i < bytes.length; i++) {
|
229
|
+
hex += hexes[bytes[i]];
|
230
|
+
}
|
231
|
+
return hex;
|
232
|
+
}
|
233
|
+
function utf8ToBytes(str) {
|
234
|
+
if (typeof str !== "string")
|
235
|
+
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
236
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
237
|
+
}
|
238
|
+
function toBytes(data) {
|
239
|
+
if (typeof data === "string")
|
240
|
+
data = utf8ToBytes(data);
|
241
|
+
if (!u8a(data))
|
242
|
+
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
243
|
+
return data;
|
244
|
+
}
|
245
|
+
var Hash = class {
|
246
|
+
clone() {
|
247
|
+
return this._cloneInto();
|
248
|
+
}
|
249
|
+
};
|
250
|
+
function wrapConstructor(hashCons) {
|
251
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
252
|
+
const tmp2 = hashCons();
|
253
|
+
hashC.outputLen = tmp2.outputLen;
|
254
|
+
hashC.blockLen = tmp2.blockLen;
|
255
|
+
hashC.create = () => hashCons();
|
394
256
|
return hashC;
|
395
257
|
}
|
258
|
+
|
259
|
+
// src/sm2/sm3.ts
|
260
|
+
var BoolA = (A, B, C) => A & B | A & C | B & C;
|
261
|
+
var BoolB = (A, B, C) => A ^ B ^ C;
|
262
|
+
var BoolC = (A, B, C) => A & B | ~A & C;
|
263
|
+
function setBigUint64(view, byteOffset, value, isLE2) {
|
264
|
+
if (typeof view.setBigUint64 === "function")
|
265
|
+
return view.setBigUint64(byteOffset, value, isLE2);
|
266
|
+
const _32n = BigInt(32);
|
267
|
+
const _u32_max = BigInt(4294967295);
|
268
|
+
const wh = Number(value >> _32n & _u32_max);
|
269
|
+
const wl = Number(value & _u32_max);
|
270
|
+
const h = isLE2 ? 4 : 0;
|
271
|
+
const l = isLE2 ? 0 : 4;
|
272
|
+
view.setUint32(byteOffset + h, wh, isLE2);
|
273
|
+
view.setUint32(byteOffset + l, wl, isLE2);
|
274
|
+
}
|
275
|
+
function rotl(x2, n) {
|
276
|
+
const s = n & 31;
|
277
|
+
return x2 << s | x2 >>> 32 - s;
|
278
|
+
}
|
279
|
+
function P0(X) {
|
280
|
+
return X ^ rotl(X, 9) ^ rotl(X, 17);
|
281
|
+
}
|
282
|
+
function P1(X) {
|
283
|
+
return X ^ rotl(X, 15) ^ rotl(X, 23);
|
284
|
+
}
|
285
|
+
var SHA2 = class extends Hash {
|
286
|
+
constructor(blockLen, outputLen, padOffset, isLE2) {
|
287
|
+
super();
|
288
|
+
this.blockLen = blockLen;
|
289
|
+
this.outputLen = outputLen;
|
290
|
+
this.padOffset = padOffset;
|
291
|
+
this.isLE = isLE2;
|
292
|
+
this.buffer = new Uint8Array(blockLen);
|
293
|
+
this.view = createView(this.buffer);
|
294
|
+
}
|
295
|
+
buffer;
|
296
|
+
view;
|
297
|
+
finished = false;
|
298
|
+
length = 0;
|
299
|
+
pos = 0;
|
300
|
+
destroyed = false;
|
301
|
+
update(data) {
|
302
|
+
const { view, buffer, blockLen } = this;
|
303
|
+
data = toBytes(data);
|
304
|
+
const len = data.length;
|
305
|
+
for (let pos = 0; pos < len; ) {
|
306
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
307
|
+
if (take === blockLen) {
|
308
|
+
const dataView = createView(data);
|
309
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
310
|
+
this.process(dataView, pos);
|
311
|
+
continue;
|
312
|
+
}
|
313
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
314
|
+
this.pos += take;
|
315
|
+
pos += take;
|
316
|
+
if (this.pos === blockLen) {
|
317
|
+
this.process(view, 0);
|
318
|
+
this.pos = 0;
|
319
|
+
}
|
320
|
+
}
|
321
|
+
this.length += data.length;
|
322
|
+
this.roundClean();
|
323
|
+
return this;
|
324
|
+
}
|
325
|
+
digestInto(out) {
|
326
|
+
this.finished = true;
|
327
|
+
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
328
|
+
let { pos } = this;
|
329
|
+
buffer[pos++] = 128;
|
330
|
+
this.buffer.subarray(pos).fill(0);
|
331
|
+
if (this.padOffset > blockLen - pos) {
|
332
|
+
this.process(view, 0);
|
333
|
+
pos = 0;
|
334
|
+
}
|
335
|
+
for (let i = pos; i < blockLen; i++)
|
336
|
+
buffer[i] = 0;
|
337
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
|
338
|
+
this.process(view, 0);
|
339
|
+
const oview = createView(out);
|
340
|
+
const len = this.outputLen;
|
341
|
+
if (len % 4)
|
342
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
343
|
+
const outLen = len / 4;
|
344
|
+
const state = this.get();
|
345
|
+
if (outLen > state.length)
|
346
|
+
throw new Error("_sha2: outputLen bigger than state");
|
347
|
+
for (let i = 0; i < outLen; i++)
|
348
|
+
oview.setUint32(4 * i, state[i], isLE2);
|
349
|
+
}
|
350
|
+
digest() {
|
351
|
+
const { buffer, outputLen } = this;
|
352
|
+
this.digestInto(buffer);
|
353
|
+
const res = buffer.slice(0, outputLen);
|
354
|
+
this.destroy();
|
355
|
+
return res;
|
356
|
+
}
|
357
|
+
_cloneInto(to) {
|
358
|
+
to ||= new this.constructor();
|
359
|
+
to.set(...this.get());
|
360
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
361
|
+
to.length = length;
|
362
|
+
to.pos = pos;
|
363
|
+
to.finished = finished;
|
364
|
+
to.destroyed = destroyed;
|
365
|
+
if (length % blockLen)
|
366
|
+
to.buffer.set(buffer);
|
367
|
+
return to;
|
368
|
+
}
|
369
|
+
};
|
370
|
+
var IV = new Uint32Array([1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214]);
|
371
|
+
var SM3_W = new Uint32Array(68);
|
372
|
+
var SM3_M = new Uint32Array(64);
|
373
|
+
var T1 = 2043430169;
|
374
|
+
var T2 = 2055708042;
|
375
|
+
var SM3 = class extends SHA2 {
|
376
|
+
A = IV[0] | 0;
|
377
|
+
B = IV[1] | 0;
|
378
|
+
C = IV[2] | 0;
|
379
|
+
D = IV[3] | 0;
|
380
|
+
E = IV[4] | 0;
|
381
|
+
F = IV[5] | 0;
|
382
|
+
G = IV[6] | 0;
|
383
|
+
H = IV[7] | 0;
|
384
|
+
constructor() {
|
385
|
+
super(64, 32, 8, false);
|
386
|
+
}
|
387
|
+
get() {
|
388
|
+
const { A, B, C, D, E, F, G, H } = this;
|
389
|
+
return [A, B, C, D, E, F, G, H];
|
390
|
+
}
|
391
|
+
set(A, B, C, D, E, F, G, H) {
|
392
|
+
this.A = A | 0;
|
393
|
+
this.B = B | 0;
|
394
|
+
this.C = C | 0;
|
395
|
+
this.D = D | 0;
|
396
|
+
this.E = E | 0;
|
397
|
+
this.F = F | 0;
|
398
|
+
this.G = G | 0;
|
399
|
+
this.H = H | 0;
|
400
|
+
}
|
401
|
+
process(view, offset) {
|
402
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
403
|
+
SM3_W[i] = view.getUint32(offset, false);
|
404
|
+
for (let i = 16; i < 68; i++) {
|
405
|
+
SM3_W[i] = P1(SM3_W[i - 16] ^ SM3_W[i - 9] ^ rotl(SM3_W[i - 3], 15)) ^ rotl(SM3_W[i - 13], 7) ^ SM3_W[i - 6];
|
406
|
+
}
|
407
|
+
for (let i = 0; i < 64; i++) {
|
408
|
+
SM3_M[i] = SM3_W[i] ^ SM3_W[i + 4];
|
409
|
+
}
|
410
|
+
let { A, B, C, D, E, F, G, H } = this;
|
411
|
+
for (let j = 0; j < 64; j++) {
|
412
|
+
let small = j >= 0 && j <= 15;
|
413
|
+
let T = small ? T1 : T2;
|
414
|
+
let SS1 = rotl(rotl(A, 12) + E + rotl(T, j), 7);
|
415
|
+
let SS2 = SS1 ^ rotl(A, 12);
|
416
|
+
let TT1 = (small ? BoolB(A, B, C) : BoolA(A, B, C)) + D + SS2 + SM3_M[j] | 0;
|
417
|
+
let TT2 = (small ? BoolB(E, F, G) : BoolC(E, F, G)) + H + SS1 + SM3_W[j] | 0;
|
418
|
+
D = C;
|
419
|
+
C = rotl(B, 9);
|
420
|
+
B = A;
|
421
|
+
A = TT1;
|
422
|
+
H = G;
|
423
|
+
G = rotl(F, 19);
|
424
|
+
F = E;
|
425
|
+
E = P0(TT2);
|
426
|
+
}
|
427
|
+
A = A ^ this.A | 0;
|
428
|
+
B = B ^ this.B | 0;
|
429
|
+
C = C ^ this.C | 0;
|
430
|
+
D = D ^ this.D | 0;
|
431
|
+
E = E ^ this.E | 0;
|
432
|
+
F = F ^ this.F | 0;
|
433
|
+
G = G ^ this.G | 0;
|
434
|
+
H = H ^ this.H | 0;
|
435
|
+
this.set(A, B, C, D, E, F, G, H);
|
436
|
+
}
|
437
|
+
roundClean() {
|
438
|
+
SM3_W.fill(0);
|
439
|
+
}
|
440
|
+
destroy() {
|
441
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
442
|
+
this.buffer.fill(0);
|
443
|
+
}
|
444
|
+
};
|
445
|
+
var sm3 = wrapConstructor(() => new SM3());
|
446
|
+
|
447
|
+
// src/sm2/hmac.ts
|
448
|
+
var HMAC = class extends Hash {
|
449
|
+
oHash;
|
450
|
+
iHash;
|
451
|
+
blockLen;
|
452
|
+
outputLen;
|
453
|
+
finished = false;
|
454
|
+
destroyed = false;
|
455
|
+
constructor(hash, _key) {
|
456
|
+
super();
|
457
|
+
const key = toBytes(_key);
|
458
|
+
this.iHash = hash.create();
|
459
|
+
if (typeof this.iHash.update !== "function")
|
460
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
461
|
+
this.blockLen = this.iHash.blockLen;
|
462
|
+
this.outputLen = this.iHash.outputLen;
|
463
|
+
const blockLen = this.blockLen;
|
464
|
+
const pad = new Uint8Array(blockLen);
|
465
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
466
|
+
for (let i = 0; i < pad.length; i++)
|
467
|
+
pad[i] ^= 54;
|
468
|
+
this.iHash.update(pad);
|
469
|
+
this.oHash = hash.create();
|
470
|
+
for (let i = 0; i < pad.length; i++)
|
471
|
+
pad[i] ^= 54 ^ 92;
|
472
|
+
this.oHash.update(pad);
|
473
|
+
pad.fill(0);
|
474
|
+
}
|
475
|
+
update(buf) {
|
476
|
+
this.iHash.update(buf);
|
477
|
+
return this;
|
478
|
+
}
|
479
|
+
digestInto(out) {
|
480
|
+
this.finished = true;
|
481
|
+
this.iHash.digestInto(out);
|
482
|
+
this.oHash.update(out);
|
483
|
+
this.oHash.digestInto(out);
|
484
|
+
this.destroy();
|
485
|
+
}
|
486
|
+
digest() {
|
487
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
488
|
+
this.digestInto(out);
|
489
|
+
return out;
|
490
|
+
}
|
491
|
+
_cloneInto(to) {
|
492
|
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
493
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
494
|
+
to = to;
|
495
|
+
to.finished = finished;
|
496
|
+
to.destroyed = destroyed;
|
497
|
+
to.blockLen = blockLen;
|
498
|
+
to.outputLen = outputLen;
|
499
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
500
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
501
|
+
return to;
|
502
|
+
}
|
503
|
+
destroy() {
|
504
|
+
this.destroyed = true;
|
505
|
+
this.oHash.destroy();
|
506
|
+
this.iHash.destroy();
|
507
|
+
}
|
508
|
+
};
|
509
|
+
var hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
510
|
+
hmac.create = (hash, key) => new HMAC(hash, key);
|
511
|
+
|
512
|
+
// src/sm2/ec.ts
|
513
|
+
import { concatBytes } from "@noble/curves/abstract/utils";
|
396
514
|
var sm2Fp = Field(BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991999"));
|
397
515
|
var sm2Curve = weierstrass({
|
398
516
|
a: BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991996"),
|
@@ -402,18 +520,19 @@ var sm2Curve = weierstrass({
|
|
402
520
|
n: BigInt("115792089210356248756420345214020892766061623724957744567843809356293439045923"),
|
403
521
|
Gx: BigInt("22963146547237050559479531362550074578802567295341616970375194840604139615431"),
|
404
522
|
Gy: BigInt("85132369209828568825618990617112496413088388631904505083283536607588877201568"),
|
405
|
-
hash:
|
406
|
-
hmac: (key, ...msgs) => hmac(
|
523
|
+
hash: sm3,
|
524
|
+
hmac: (key, ...msgs) => hmac(sm3, key, concatBytes(...msgs)),
|
407
525
|
randomBytes
|
408
526
|
});
|
527
|
+
var field = Field(BigInt(sm2Curve.CURVE.n));
|
409
528
|
|
410
529
|
// src/sm2/utils.ts
|
411
530
|
import { mod } from "@noble/curves/abstract/modular";
|
412
531
|
function generateKeyPairHex(str) {
|
413
532
|
const privateKey = str ? utils2.numberToBytesBE(mod(BigInt(str), ONE) + ONE, 32) : sm2Curve.utils.randomPrivateKey();
|
414
533
|
const publicKey = sm2Curve.getPublicKey(privateKey, false);
|
415
|
-
const privPad =
|
416
|
-
const pubPad =
|
534
|
+
const privPad = leftPad(utils2.bytesToHex(privateKey), 64);
|
535
|
+
const pubPad = leftPad(utils2.bytesToHex(publicKey), 64);
|
417
536
|
return { privateKey: privPad, publicKey: pubPad };
|
418
537
|
}
|
419
538
|
function compressPublicKeyHex(s) {
|
@@ -442,7 +561,7 @@ function utf8ToHex(input) {
|
|
442
561
|
}
|
443
562
|
return hexChars.join("");
|
444
563
|
}
|
445
|
-
function
|
564
|
+
function leftPad(input, num) {
|
446
565
|
if (input.length >= num)
|
447
566
|
return input;
|
448
567
|
return new Array(num - input.length + 1).join("0") + input;
|
@@ -474,7 +593,7 @@ function arrayToUtf8(arr) {
|
|
474
593
|
function hexToArray(hexStr) {
|
475
594
|
let hexStrLength = hexStr.length;
|
476
595
|
if (hexStrLength % 2 !== 0) {
|
477
|
-
hexStr =
|
596
|
+
hexStr = leftPad(hexStr, hexStrLength + 1);
|
478
597
|
}
|
479
598
|
hexStrLength = hexStr.length;
|
480
599
|
const wordLength = hexStrLength / 2;
|
@@ -488,9 +607,9 @@ function verifyPublicKey(publicKey) {
|
|
488
607
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
489
608
|
if (!point)
|
490
609
|
return false;
|
491
|
-
const
|
610
|
+
const x2 = point.x;
|
492
611
|
const y = point.y;
|
493
|
-
return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.
|
612
|
+
return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.addN(sm2Fp.mulN(x2, sm2Fp.sqrN(x2)), sm2Fp.mulN(x2, sm2Curve.CURVE.a)), sm2Curve.CURVE.b);
|
494
613
|
}
|
495
614
|
function comparePublicKeyHex(publicKey1, publicKey2) {
|
496
615
|
const point1 = sm2Curve.ProjectivePoint.fromHex(publicKey1);
|
@@ -501,57 +620,101 @@ function comparePublicKeyHex(publicKey1, publicKey2) {
|
|
501
620
|
return false;
|
502
621
|
return point1.equals(point2);
|
503
622
|
}
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
623
|
+
|
624
|
+
// src/sm2/index.ts
|
625
|
+
import * as utils4 from "@noble/curves/abstract/utils";
|
626
|
+
|
627
|
+
// src/sm2/kx.ts
|
628
|
+
import * as utils3 from "@noble/curves/abstract/utils";
|
629
|
+
var wPow2 = utils3.hexToNumber("80000000000000000000000000000000");
|
630
|
+
var wPow2Sub1 = utils3.hexToNumber("7fffffffffffffffffffffffffffffff");
|
631
|
+
function hkdf(z, keylen) {
|
632
|
+
let msg = new Uint8Array(keylen);
|
633
|
+
let ct = 1;
|
634
|
+
let offset = 0;
|
635
|
+
let t = EmptyArray;
|
636
|
+
const ctShift = new Uint8Array(4);
|
637
|
+
const nextT = () => {
|
638
|
+
ctShift[0] = ct >> 24 & 255;
|
639
|
+
ctShift[1] = ct >> 16 & 255;
|
640
|
+
ctShift[2] = ct >> 8 & 255;
|
641
|
+
ctShift[3] = ct & 255;
|
642
|
+
t = sm3(utils3.concatBytes(z, ctShift));
|
643
|
+
ct++;
|
644
|
+
offset = 0;
|
645
|
+
};
|
646
|
+
nextT();
|
647
|
+
for (let i = 0, len = msg.length; i < len; i++) {
|
648
|
+
if (offset === t.length)
|
649
|
+
nextT();
|
650
|
+
msg[i] = t[offset++] & 255;
|
513
651
|
}
|
514
|
-
return
|
652
|
+
return msg;
|
653
|
+
}
|
654
|
+
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, idA = "1234567812345678", idB = "1234567812345678", sharedKeyLength) {
|
655
|
+
const RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey);
|
656
|
+
const RB = sm2Curve.ProjectivePoint.fromHex(ephemeralPublicKeyB);
|
657
|
+
const PB = sm2Curve.ProjectivePoint.fromHex(publicKeyB);
|
658
|
+
const ZA = hexToArray(idA);
|
659
|
+
const ZB = hexToArray(idB);
|
660
|
+
const rA = utils3.hexToNumber(ephemeralKeypairA.privateKey);
|
661
|
+
const dA = utils3.hexToNumber(keypairA.privateKey);
|
662
|
+
const x1 = RA.x;
|
663
|
+
const x1_ = field.add(wPow2, x1 & wPow2Sub1);
|
664
|
+
const tA = field.add(dA, field.mulN(x1_, rA));
|
665
|
+
const x2 = RB.x;
|
666
|
+
const x2_ = field.add(wPow2, x2 & wPow2Sub1);
|
667
|
+
const U = RB.multiply(x2_).add(PB).multiply(tA);
|
668
|
+
const xU = hexToArray(leftPad(utils3.numberToHexUnpadded(U.x), 64));
|
669
|
+
const yU = hexToArray(leftPad(utils3.numberToHexUnpadded(U.y), 64));
|
670
|
+
const KA = hkdf(utils3.concatBytes(xU, yU, ZA, ZB), sharedKeyLength);
|
671
|
+
return KA;
|
515
672
|
}
|
516
673
|
|
517
674
|
// src/sm2/index.ts
|
518
|
-
import * as mod2 from "@noble/curves/abstract/modular";
|
519
|
-
import * as utils3 from "@noble/curves/abstract/utils";
|
520
675
|
var C1C2C3 = 0;
|
676
|
+
var EmptyArray = new Uint8Array();
|
521
677
|
function doEncrypt(msg, publicKey, cipherMode = 1) {
|
522
678
|
const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
|
523
679
|
const publicKeyPoint = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
524
680
|
const keypair = generateKeyPairHex();
|
525
|
-
const k =
|
681
|
+
const k = utils4.hexToNumber(keypair.privateKey);
|
526
682
|
let c1 = keypair.publicKey;
|
527
683
|
if (c1.length > 128)
|
528
684
|
c1 = c1.substring(c1.length - 128);
|
529
685
|
const p = publicKeyPoint.multiply(k);
|
530
|
-
const x2 = hexToArray(
|
531
|
-
const y2 = hexToArray(
|
532
|
-
const c3 =
|
686
|
+
const x2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.x), 64));
|
687
|
+
const y2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.y), 64));
|
688
|
+
const c3 = bytesToHex(sm3(utils4.concatBytes(x2, msgArr, y2)));
|
689
|
+
xorCipherStream(x2, y2, msgArr);
|
690
|
+
const c2 = bytesToHex(msgArr);
|
691
|
+
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
692
|
+
}
|
693
|
+
function xorCipherStream(x2, y2, msg) {
|
533
694
|
let ct = 1;
|
534
695
|
let offset = 0;
|
535
|
-
let t =
|
536
|
-
const
|
696
|
+
let t = EmptyArray;
|
697
|
+
const ctShift = new Uint8Array(4);
|
537
698
|
const nextT = () => {
|
538
|
-
|
699
|
+
ctShift[0] = ct >> 24 & 255;
|
700
|
+
ctShift[1] = ct >> 16 & 255;
|
701
|
+
ctShift[2] = ct >> 8 & 255;
|
702
|
+
ctShift[3] = ct & 255;
|
703
|
+
t = sm3(utils4.concatBytes(x2, y2, ctShift));
|
539
704
|
ct++;
|
540
705
|
offset = 0;
|
541
706
|
};
|
542
707
|
nextT();
|
543
|
-
for (let i = 0, len =
|
708
|
+
for (let i = 0, len = msg.length; i < len; i++) {
|
544
709
|
if (offset === t.length)
|
545
710
|
nextT();
|
546
|
-
|
711
|
+
msg[i] ^= t[offset++] & 255;
|
547
712
|
}
|
548
|
-
const c2 = arrayToHex(Array.from(msgArr));
|
549
|
-
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
550
713
|
}
|
551
714
|
function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
552
715
|
output = "string"
|
553
716
|
} = {}) {
|
554
|
-
const privateKeyInteger =
|
717
|
+
const privateKeyInteger = utils4.hexToNumber(privateKey);
|
555
718
|
let c3 = encryptData.substring(128, 128 + 64);
|
556
719
|
let c2 = encryptData.substring(128 + 64);
|
557
720
|
if (cipherMode === C1C2C3) {
|
@@ -561,24 +724,10 @@ function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
|
561
724
|
const msg = hexToArray(c2);
|
562
725
|
const c1 = sm2Curve.ProjectivePoint.fromHex("04" + encryptData.substring(0, 128));
|
563
726
|
const p = c1.multiply(privateKeyInteger);
|
564
|
-
const x2 = hexToArray(
|
565
|
-
const y2 = hexToArray(
|
566
|
-
|
567
|
-
|
568
|
-
let t = new Uint8Array();
|
569
|
-
const z = concatArray(x2, y2);
|
570
|
-
const nextT = () => {
|
571
|
-
t = sm3(Uint8Array.from([...z, ct >> 24 & 255, ct >> 16 & 255, ct >> 8 & 255, ct & 255]));
|
572
|
-
ct++;
|
573
|
-
offset = 0;
|
574
|
-
};
|
575
|
-
nextT();
|
576
|
-
for (let i = 0, len = msg.length; i < len; i++) {
|
577
|
-
if (offset === t.length)
|
578
|
-
nextT();
|
579
|
-
msg[i] ^= t[offset++] & 255;
|
580
|
-
}
|
581
|
-
const checkC3 = arrayToHex(Array.from(sm3(concatArray(x2, msg, y2))));
|
727
|
+
const x2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.x), 64));
|
728
|
+
const y2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.y), 64));
|
729
|
+
xorCipherStream(x2, y2, msg);
|
730
|
+
const checkC3 = arrayToHex(Array.from(sm3(utils4.concatBytes(x2, msg, y2))));
|
582
731
|
if (checkC3 === c3.toLowerCase()) {
|
583
732
|
return output === "array" ? msg : arrayToUtf8(msg);
|
584
733
|
} else {
|
@@ -598,8 +747,8 @@ function doSignature(msg, privateKey, options = {}) {
|
|
598
747
|
publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
|
599
748
|
hashHex = getHash(hashHex, publicKey, userId);
|
600
749
|
}
|
601
|
-
const dA =
|
602
|
-
const e =
|
750
|
+
const dA = utils4.hexToNumber(privateKey);
|
751
|
+
const e = utils4.hexToNumber(hashHex);
|
603
752
|
let k = null;
|
604
753
|
let r = null;
|
605
754
|
let s = null;
|
@@ -612,13 +761,13 @@ function doSignature(msg, privateKey, options = {}) {
|
|
612
761
|
point = getPoint();
|
613
762
|
}
|
614
763
|
k = point.k;
|
615
|
-
r =
|
764
|
+
r = field.add(e, point.x1);
|
616
765
|
} while (r === ZERO || r + k === sm2Curve.CURVE.n);
|
617
|
-
s =
|
766
|
+
s = field.mul(field.inv(field.addN(dA, ONE)), field.subN(k, field.mulN(r, dA)));
|
618
767
|
} while (s === ZERO);
|
619
768
|
if (der)
|
620
769
|
return encodeDer(r, s);
|
621
|
-
return
|
770
|
+
return leftPad(utils4.numberToHexUnpadded(r), 64) + leftPad(utils4.numberToHexUnpadded(s), 64);
|
622
771
|
}
|
623
772
|
function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
624
773
|
let hashHex;
|
@@ -639,24 +788,24 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
639
788
|
r = decodeDerObj.r;
|
640
789
|
s = decodeDerObj.s;
|
641
790
|
} else {
|
642
|
-
r =
|
643
|
-
s =
|
791
|
+
r = utils4.hexToNumber(signHex.substring(0, 64));
|
792
|
+
s = utils4.hexToNumber(signHex.substring(64));
|
644
793
|
}
|
645
794
|
const PA = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
646
|
-
const e =
|
647
|
-
const t =
|
795
|
+
const e = utils4.hexToNumber(hashHex);
|
796
|
+
const t = field.add(r, s);
|
648
797
|
if (t === ZERO)
|
649
798
|
return false;
|
650
799
|
const x1y1 = sm2Curve.ProjectivePoint.BASE.multiply(s).add(PA.multiply(t));
|
651
|
-
const R =
|
800
|
+
const R = field.add(e, x1y1.x);
|
652
801
|
return r === R;
|
653
802
|
}
|
654
803
|
function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
655
804
|
userId = utf8ToHex(userId);
|
656
|
-
const a =
|
657
|
-
const b =
|
658
|
-
const gx =
|
659
|
-
const gy =
|
805
|
+
const a = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.a), 64);
|
806
|
+
const b = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.b), 64);
|
807
|
+
const gx = leftPad(utils4.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.x), 64);
|
808
|
+
const gy = leftPad(utils4.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.y), 64);
|
660
809
|
let px;
|
661
810
|
let py;
|
662
811
|
if (publicKey.length === 128) {
|
@@ -664,23 +813,23 @@ function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
|
664
813
|
py = publicKey.substring(64, 128);
|
665
814
|
} else {
|
666
815
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
667
|
-
px =
|
668
|
-
py =
|
816
|
+
px = leftPad(utils4.numberToHexUnpadded(point.x), 64);
|
817
|
+
py = leftPad(utils4.numberToHexUnpadded(point.y), 64);
|
669
818
|
}
|
670
819
|
const data = hexToArray(userId + a + b + gx + gy + px + py);
|
671
820
|
const entl = userId.length * 4;
|
672
|
-
const z = sm3(
|
673
|
-
return
|
821
|
+
const z = sm3(utils4.concatBytes(new Uint8Array([entl >> 8 & 255, entl & 255]), data));
|
822
|
+
return bytesToHex(sm3(utils4.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
|
674
823
|
}
|
675
824
|
function getPublicKeyFromPrivateKey(privateKey) {
|
676
825
|
const pubKey = sm2Curve.getPublicKey(privateKey, false);
|
677
|
-
const pubPad =
|
826
|
+
const pubPad = leftPad(utils4.bytesToHex(pubKey), 64);
|
678
827
|
return pubPad;
|
679
828
|
}
|
680
829
|
function getPoint() {
|
681
830
|
const keypair = generateKeyPairHex();
|
682
831
|
const PA = sm2Curve.ProjectivePoint.fromHex(keypair.publicKey);
|
683
|
-
const k =
|
832
|
+
const k = utils4.hexToNumber(keypair.privateKey);
|
684
833
|
return {
|
685
834
|
...keypair,
|
686
835
|
k,
|
@@ -688,6 +837,48 @@ function getPoint() {
|
|
688
837
|
};
|
689
838
|
}
|
690
839
|
|
840
|
+
// src/sm3/index.ts
|
841
|
+
function utf8ToArray(str) {
|
842
|
+
const arr = [];
|
843
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
844
|
+
const point = str.codePointAt(i);
|
845
|
+
if (point <= 127) {
|
846
|
+
arr.push(point);
|
847
|
+
} else if (point <= 2047) {
|
848
|
+
arr.push(192 | point >>> 6);
|
849
|
+
arr.push(128 | point & 63);
|
850
|
+
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
851
|
+
arr.push(224 | point >>> 12);
|
852
|
+
arr.push(128 | point >>> 6 & 63);
|
853
|
+
arr.push(128 | point & 63);
|
854
|
+
} else if (point >= 65536 && point <= 1114111) {
|
855
|
+
i++;
|
856
|
+
arr.push(240 | point >>> 18 & 28);
|
857
|
+
arr.push(128 | point >>> 12 & 63);
|
858
|
+
arr.push(128 | point >>> 6 & 63);
|
859
|
+
arr.push(128 | point & 63);
|
860
|
+
} else {
|
861
|
+
arr.push(point);
|
862
|
+
throw new Error("input is not supported");
|
863
|
+
}
|
864
|
+
}
|
865
|
+
return new Uint8Array(arr);
|
866
|
+
}
|
867
|
+
function sm32(input, options) {
|
868
|
+
input = typeof input === "string" ? utf8ToArray(input) : input;
|
869
|
+
if (options) {
|
870
|
+
const mode = options.mode || "hmac";
|
871
|
+
if (mode !== "hmac")
|
872
|
+
throw new Error("invalid mode");
|
873
|
+
let key = options.key;
|
874
|
+
if (!key)
|
875
|
+
throw new Error("invalid key");
|
876
|
+
key = typeof key === "string" ? hexToArray(key) : key;
|
877
|
+
return bytesToHex(hmac(sm3, key, input));
|
878
|
+
}
|
879
|
+
return bytesToHex(sm3(input));
|
880
|
+
}
|
881
|
+
|
691
882
|
// src/sm4/index.ts
|
692
883
|
var sm4_exports = {};
|
693
884
|
__export(sm4_exports, {
|
@@ -999,9 +1190,9 @@ function l1(b) {
|
|
999
1190
|
function l2(b) {
|
1000
1191
|
return b ^ rotl(b, 13) ^ rotl(b, 23);
|
1001
1192
|
}
|
1193
|
+
var x = new Uint32Array(4);
|
1194
|
+
var tmp = new Uint32Array(4);
|
1002
1195
|
function sms4Crypt(input, output, roundKey) {
|
1003
|
-
const x = new Uint32Array(4);
|
1004
|
-
const tmp = new Uint32Array(4);
|
1005
1196
|
for (let i = 0; i < 4; i++) {
|
1006
1197
|
tmp[0] = input[4 * i] & 255;
|
1007
1198
|
tmp[1] = input[4 * i + 1] & 255;
|
@@ -1027,8 +1218,6 @@ function sms4Crypt(input, output, roundKey) {
|
|
1027
1218
|
}
|
1028
1219
|
}
|
1029
1220
|
function sms4KeyExt(key, roundKey, cryptFlag) {
|
1030
|
-
const x = new Uint32Array(4);
|
1031
|
-
const tmp = new Uint32Array(4);
|
1032
1221
|
for (let i = 0; i < 4; i++) {
|
1033
1222
|
tmp[0] = key[0 + 4 * i] & 255;
|
1034
1223
|
tmp[1] = key[1 + 4 * i] & 255;
|
@@ -1058,6 +1247,7 @@ function sms4KeyExt(key, roundKey, cryptFlag) {
|
|
1058
1247
|
}
|
1059
1248
|
}
|
1060
1249
|
}
|
1250
|
+
var blockOutput = new Uint8Array(16);
|
1061
1251
|
function sm4(inArray, key, cryptFlag, options = {}) {
|
1062
1252
|
let {
|
1063
1253
|
padding = "pkcs#7",
|
@@ -1101,8 +1291,7 @@ function sm4(inArray, key, cryptFlag, options = {}) {
|
|
1101
1291
|
let restLen = inArray.length;
|
1102
1292
|
let point = 0;
|
1103
1293
|
while (restLen >= BLOCK) {
|
1104
|
-
const input = inArray.
|
1105
|
-
const output2 = new Uint8Array(16);
|
1294
|
+
const input = inArray.subarray(point, point + 16);
|
1106
1295
|
if (mode === "cbc") {
|
1107
1296
|
for (let i = 0; i < BLOCK; i++) {
|
1108
1297
|
if (cryptFlag !== DECRYPT) {
|
@@ -1110,18 +1299,18 @@ function sm4(inArray, key, cryptFlag, options = {}) {
|
|
1110
1299
|
}
|
1111
1300
|
}
|
1112
1301
|
}
|
1113
|
-
sms4Crypt(input,
|
1302
|
+
sms4Crypt(input, blockOutput, roundKey);
|
1114
1303
|
for (let i = 0; i < BLOCK; i++) {
|
1115
1304
|
if (mode === "cbc") {
|
1116
1305
|
if (cryptFlag === DECRYPT) {
|
1117
|
-
|
1306
|
+
blockOutput[i] ^= lastVector[i];
|
1118
1307
|
}
|
1119
1308
|
}
|
1120
|
-
outArray[point + i] =
|
1309
|
+
outArray[point + i] = blockOutput[i];
|
1121
1310
|
}
|
1122
1311
|
if (mode === "cbc") {
|
1123
1312
|
if (cryptFlag !== DECRYPT) {
|
1124
|
-
lastVector =
|
1313
|
+
lastVector = blockOutput;
|
1125
1314
|
} else {
|
1126
1315
|
lastVector = input;
|
1127
1316
|
}
|
@@ -1140,7 +1329,7 @@ function sm4(inArray, key, cryptFlag, options = {}) {
|
|
1140
1329
|
}
|
1141
1330
|
if (output !== "array") {
|
1142
1331
|
if (cryptFlag !== DECRYPT) {
|
1143
|
-
return
|
1332
|
+
return bytesToHex(outArray);
|
1144
1333
|
} else {
|
1145
1334
|
return arrayToUtf8(outArray);
|
1146
1335
|
}
|
@@ -1156,6 +1345,7 @@ function decrypt(inArray, key, options = {}) {
|
|
1156
1345
|
}
|
1157
1346
|
export {
|
1158
1347
|
sm2_exports as sm2,
|
1159
|
-
|
1348
|
+
sm32 as sm3,
|
1160
1349
|
sm4_exports as sm4
|
1161
1350
|
};
|
1351
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|