sm-crypto-v2 1.4.0 → 1.5.1
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 +39 -28
- package/dist/index.d.ts +49 -62
- package/dist/index.js +425 -274
- package/dist/index.mjs +421 -270
- package/package.json +1 -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 +40 -63
- package/src/sm2/kx.ts +23 -16
- package/src/sm2/rng.ts +71 -0
- package/src/sm2/sm3.ts +187 -120
- package/src/sm2/utils.ts +1 -23
- package/src/sm3/index.ts +7 -4
- package/src/sm3/utils.ts +117 -0
- package/src/sm4/index.ts +10 -12
- package/.babelrc +0 -3
- package/.eslintrc.js +0 -97
- package/.github/workflows/test.yml +0 -17
- package/benchmark/index.js +0 -29
- package/benchmark/package.json +0 -19
- package/benchmark/pnpm-lock.yaml +0 -28
- package/pnpm-lock.yaml +0 -3942
package/dist/index.js
CHANGED
@@ -17,17 +17,17 @@ var __copyProps = (to, from, except, desc) => {
|
|
17
17
|
}
|
18
18
|
return to;
|
19
19
|
};
|
20
|
-
var __toESM = (
|
21
|
-
isNodeMode || !
|
22
|
-
|
20
|
+
var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
|
21
|
+
isNodeMode || !mod2 || !mod2.__esModule ? __defProp(target, "default", { value: mod2, enumerable: true }) : target,
|
22
|
+
mod2
|
23
23
|
));
|
24
|
-
var __toCommonJS = (
|
24
|
+
var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
|
25
25
|
|
26
26
|
// src/index.ts
|
27
27
|
var src_exports = {};
|
28
28
|
__export(src_exports, {
|
29
29
|
sm2: () => sm2_exports,
|
30
|
-
sm3: () =>
|
30
|
+
sm3: () => sm32,
|
31
31
|
sm4: () => sm4_exports
|
32
32
|
});
|
33
33
|
module.exports = __toCommonJS(src_exports);
|
@@ -35,12 +35,12 @@ module.exports = __toCommonJS(src_exports);
|
|
35
35
|
// src/sm2/index.ts
|
36
36
|
var sm2_exports = {};
|
37
37
|
__export(sm2_exports, {
|
38
|
+
EmptyArray: () => EmptyArray,
|
38
39
|
arrayToHex: () => arrayToHex,
|
39
40
|
arrayToUtf8: () => arrayToUtf8,
|
40
41
|
calculateSharedKey: () => calculateSharedKey,
|
41
42
|
comparePublicKeyHex: () => comparePublicKeyHex,
|
42
43
|
compressPublicKeyHex: () => compressPublicKeyHex,
|
43
|
-
concatArray: () => concatArray,
|
44
44
|
doDecrypt: () => doDecrypt,
|
45
45
|
doEncrypt: () => doEncrypt,
|
46
46
|
doSignature: () => doSignature,
|
@@ -49,9 +49,10 @@ __export(sm2_exports, {
|
|
49
49
|
getHash: () => getHash,
|
50
50
|
getPoint: () => getPoint,
|
51
51
|
getPublicKeyFromPrivateKey: () => getPublicKeyFromPrivateKey,
|
52
|
+
getZ: () => getZ,
|
52
53
|
hexToArray: () => hexToArray,
|
53
54
|
initRNGPool: () => initRNGPool,
|
54
|
-
leftPad: () =>
|
55
|
+
leftPad: () => leftPad,
|
55
56
|
utf8ToHex: () => utf8ToHex,
|
56
57
|
verifyPublicKey: () => verifyPublicKey
|
57
58
|
});
|
@@ -186,181 +187,7 @@ var utils2 = __toESM(require("@noble/curves/abstract/utils"));
|
|
186
187
|
var import_weierstrass = require("@noble/curves/abstract/weierstrass");
|
187
188
|
var import_modular = require("@noble/curves/abstract/modular");
|
188
189
|
|
189
|
-
// src/sm2/
|
190
|
-
var W = new Uint32Array(68);
|
191
|
-
var M = new Uint32Array(64);
|
192
|
-
function rotl(x, n) {
|
193
|
-
const s = n & 31;
|
194
|
-
return x << s | x >>> 32 - s;
|
195
|
-
}
|
196
|
-
function xor(x, y) {
|
197
|
-
const result = new Uint8Array(x.length);
|
198
|
-
for (let i = x.length - 1; i >= 0; i--)
|
199
|
-
result[i] = (x[i] ^ y[i]) & 255;
|
200
|
-
return result;
|
201
|
-
}
|
202
|
-
function P0(X) {
|
203
|
-
return X ^ rotl(X, 9) ^ rotl(X, 17);
|
204
|
-
}
|
205
|
-
function P1(X) {
|
206
|
-
return X ^ rotl(X, 15) ^ rotl(X, 23);
|
207
|
-
}
|
208
|
-
function sm3(array) {
|
209
|
-
let len = array.length * 8;
|
210
|
-
let k = len % 512;
|
211
|
-
k = k >= 448 ? 512 - k % 448 - 1 : 448 - k - 1;
|
212
|
-
const kArr = new Array((k - 7) / 8);
|
213
|
-
const lenArr = new Array(8);
|
214
|
-
for (let i = 0, len2 = kArr.length; i < len2; i++)
|
215
|
-
kArr[i] = 0;
|
216
|
-
for (let i = 0, len2 = lenArr.length; i < len2; i++)
|
217
|
-
lenArr[i] = 0;
|
218
|
-
let lenString = len.toString(2);
|
219
|
-
for (let i = 7; i >= 0; i--) {
|
220
|
-
if (lenString.length > 8) {
|
221
|
-
const start = lenString.length - 8;
|
222
|
-
lenArr[i] = parseInt(lenString.substring(start), 2);
|
223
|
-
lenString = lenString.substring(0, start);
|
224
|
-
} else if (lenString.length > 0) {
|
225
|
-
lenArr[i] = parseInt(lenString, 2);
|
226
|
-
lenString = "";
|
227
|
-
}
|
228
|
-
}
|
229
|
-
const m = Uint8Array.from([...array, 128, ...kArr, ...lenArr]);
|
230
|
-
const dataView = new DataView(m.buffer, 0);
|
231
|
-
const n = m.length / 64;
|
232
|
-
const V = new Uint32Array([1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214]);
|
233
|
-
for (let i = 0; i < n; i++) {
|
234
|
-
W.fill(0);
|
235
|
-
M.fill(0);
|
236
|
-
const start = 16 * i;
|
237
|
-
for (let j = 0; j < 16; j++) {
|
238
|
-
W[j] = dataView.getUint32((start + j) * 4, false);
|
239
|
-
}
|
240
|
-
for (let j = 16; j < 68; j++) {
|
241
|
-
W[j] = P1(W[j - 16] ^ W[j - 9] ^ rotl(W[j - 3], 15)) ^ rotl(W[j - 13], 7) ^ W[j - 6];
|
242
|
-
}
|
243
|
-
for (let j = 0; j < 64; j++) {
|
244
|
-
M[j] = W[j] ^ W[j + 4];
|
245
|
-
}
|
246
|
-
const T1 = 2043430169;
|
247
|
-
const T2 = 2055708042;
|
248
|
-
let A = V[0];
|
249
|
-
let B = V[1];
|
250
|
-
let C = V[2];
|
251
|
-
let D = V[3];
|
252
|
-
let E = V[4];
|
253
|
-
let F = V[5];
|
254
|
-
let G = V[6];
|
255
|
-
let H = V[7];
|
256
|
-
let SS1;
|
257
|
-
let SS2;
|
258
|
-
let TT1;
|
259
|
-
let TT2;
|
260
|
-
let T;
|
261
|
-
for (let j = 0; j < 64; j++) {
|
262
|
-
T = j >= 0 && j <= 15 ? T1 : T2;
|
263
|
-
SS1 = rotl(rotl(A, 12) + E + rotl(T, j), 7);
|
264
|
-
SS2 = SS1 ^ rotl(A, 12);
|
265
|
-
TT1 = (j >= 0 && j <= 15 ? A ^ B ^ C : A & B | A & C | B & C) + D + SS2 + M[j];
|
266
|
-
TT2 = (j >= 0 && j <= 15 ? E ^ F ^ G : E & F | ~E & G) + H + SS1 + W[j];
|
267
|
-
D = C;
|
268
|
-
C = rotl(B, 9);
|
269
|
-
B = A;
|
270
|
-
A = TT1;
|
271
|
-
H = G;
|
272
|
-
G = rotl(F, 19);
|
273
|
-
F = E;
|
274
|
-
E = P0(TT2);
|
275
|
-
}
|
276
|
-
V[0] ^= A;
|
277
|
-
V[1] ^= B;
|
278
|
-
V[2] ^= C;
|
279
|
-
V[3] ^= D;
|
280
|
-
V[4] ^= E;
|
281
|
-
V[5] ^= F;
|
282
|
-
V[6] ^= G;
|
283
|
-
V[7] ^= H;
|
284
|
-
}
|
285
|
-
const result = new Uint8Array(V.length * 4);
|
286
|
-
for (let i = 0, len2 = V.length; i < len2; i++) {
|
287
|
-
const word = V[i];
|
288
|
-
result[i * 4] = (word & 4278190080) >>> 24;
|
289
|
-
result[i * 4 + 1] = (word & 16711680) >>> 16;
|
290
|
-
result[i * 4 + 2] = (word & 65280) >>> 8;
|
291
|
-
result[i * 4 + 3] = word & 255;
|
292
|
-
}
|
293
|
-
return result;
|
294
|
-
}
|
295
|
-
var blockLen = 64;
|
296
|
-
var iPad = new Uint8Array(blockLen);
|
297
|
-
var oPad = new Uint8Array(blockLen);
|
298
|
-
for (let i = 0; i < blockLen; i++) {
|
299
|
-
iPad[i] = 54;
|
300
|
-
oPad[i] = 92;
|
301
|
-
}
|
302
|
-
function hmac(input, key) {
|
303
|
-
if (key.length > blockLen)
|
304
|
-
key = sm3(key);
|
305
|
-
while (key.length < blockLen) {
|
306
|
-
const padKey = new Uint8Array(blockLen);
|
307
|
-
padKey.set(key);
|
308
|
-
key = padKey;
|
309
|
-
}
|
310
|
-
const iPadKey = xor(key, iPad);
|
311
|
-
const oPadKey = xor(key, oPad);
|
312
|
-
const hash = sm3(concatArray(iPadKey, input));
|
313
|
-
return sm3(concatArray(oPadKey, hash));
|
314
|
-
}
|
315
|
-
|
316
|
-
// src/sm3/index.ts
|
317
|
-
var sm3_exports = {};
|
318
|
-
__export(sm3_exports, {
|
319
|
-
sm3: () => sm32,
|
320
|
-
utf8ToArray: () => utf8ToArray
|
321
|
-
});
|
322
|
-
function utf8ToArray(str) {
|
323
|
-
const arr = [];
|
324
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
325
|
-
const point = str.codePointAt(i);
|
326
|
-
if (point <= 127) {
|
327
|
-
arr.push(point);
|
328
|
-
} else if (point <= 2047) {
|
329
|
-
arr.push(192 | point >>> 6);
|
330
|
-
arr.push(128 | point & 63);
|
331
|
-
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
332
|
-
arr.push(224 | point >>> 12);
|
333
|
-
arr.push(128 | point >>> 6 & 63);
|
334
|
-
arr.push(128 | point & 63);
|
335
|
-
} else if (point >= 65536 && point <= 1114111) {
|
336
|
-
i++;
|
337
|
-
arr.push(240 | point >>> 18 & 28);
|
338
|
-
arr.push(128 | point >>> 12 & 63);
|
339
|
-
arr.push(128 | point >>> 6 & 63);
|
340
|
-
arr.push(128 | point & 63);
|
341
|
-
} else {
|
342
|
-
arr.push(point);
|
343
|
-
throw new Error("input is not supported");
|
344
|
-
}
|
345
|
-
}
|
346
|
-
return new Uint8Array(arr);
|
347
|
-
}
|
348
|
-
function sm32(input, options) {
|
349
|
-
input = typeof input === "string" ? utf8ToArray(input) : input;
|
350
|
-
if (options) {
|
351
|
-
const mode = options.mode || "hmac";
|
352
|
-
if (mode !== "hmac")
|
353
|
-
throw new Error("invalid mode");
|
354
|
-
let key = options.key;
|
355
|
-
if (!key)
|
356
|
-
throw new Error("invalid key");
|
357
|
-
key = typeof key === "string" ? hexToArray(key) : key;
|
358
|
-
return arrayToHex(Array.from(hmac(input, key)));
|
359
|
-
}
|
360
|
-
return arrayToHex(Array.from(sm3(input)));
|
361
|
-
}
|
362
|
-
|
363
|
-
// src/sm2/ec.ts
|
190
|
+
// src/sm2/rng.ts
|
364
191
|
var DEFAULT_PRNG_POOL_SIZE = 16384;
|
365
192
|
var prngPool = new Uint8Array(0);
|
366
193
|
var _syncCrypto;
|
@@ -415,13 +242,304 @@ function randomBytes(length = 0) {
|
|
415
242
|
return result;
|
416
243
|
}
|
417
244
|
}
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
245
|
+
|
246
|
+
// src/sm3/utils.ts
|
247
|
+
var u8a = (a) => a instanceof Uint8Array;
|
248
|
+
var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
249
|
+
var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
250
|
+
if (!isLE)
|
251
|
+
throw new Error("Non little-endian hardware is not supported");
|
252
|
+
var hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, "0"));
|
253
|
+
function bytesToHex(bytes) {
|
254
|
+
if (!u8a(bytes))
|
255
|
+
throw new Error("Uint8Array expected");
|
256
|
+
let hex = "";
|
257
|
+
for (let i = 0; i < bytes.length; i++) {
|
258
|
+
hex += hexes[bytes[i]];
|
259
|
+
}
|
260
|
+
return hex;
|
261
|
+
}
|
262
|
+
function utf8ToBytes(str) {
|
263
|
+
if (typeof str !== "string")
|
264
|
+
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
265
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
266
|
+
}
|
267
|
+
function toBytes(data) {
|
268
|
+
if (typeof data === "string")
|
269
|
+
data = utf8ToBytes(data);
|
270
|
+
if (!u8a(data))
|
271
|
+
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
272
|
+
return data;
|
273
|
+
}
|
274
|
+
var Hash = class {
|
275
|
+
clone() {
|
276
|
+
return this._cloneInto();
|
277
|
+
}
|
278
|
+
};
|
279
|
+
function wrapConstructor(hashCons) {
|
280
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
281
|
+
const tmp2 = hashCons();
|
282
|
+
hashC.outputLen = tmp2.outputLen;
|
283
|
+
hashC.blockLen = tmp2.blockLen;
|
284
|
+
hashC.create = () => hashCons();
|
423
285
|
return hashC;
|
424
286
|
}
|
287
|
+
|
288
|
+
// src/sm2/sm3.ts
|
289
|
+
var BoolA = (A, B, C) => A & B | A & C | B & C;
|
290
|
+
var BoolB = (A, B, C) => A ^ B ^ C;
|
291
|
+
var BoolC = (A, B, C) => A & B | ~A & C;
|
292
|
+
function setBigUint64(view, byteOffset, value, isLE2) {
|
293
|
+
if (typeof view.setBigUint64 === "function")
|
294
|
+
return view.setBigUint64(byteOffset, value, isLE2);
|
295
|
+
const _32n = BigInt(32);
|
296
|
+
const _u32_max = BigInt(4294967295);
|
297
|
+
const wh = Number(value >> _32n & _u32_max);
|
298
|
+
const wl = Number(value & _u32_max);
|
299
|
+
const h = isLE2 ? 4 : 0;
|
300
|
+
const l = isLE2 ? 0 : 4;
|
301
|
+
view.setUint32(byteOffset + h, wh, isLE2);
|
302
|
+
view.setUint32(byteOffset + l, wl, isLE2);
|
303
|
+
}
|
304
|
+
function rotl(x2, n) {
|
305
|
+
const s = n & 31;
|
306
|
+
return x2 << s | x2 >>> 32 - s;
|
307
|
+
}
|
308
|
+
function P0(X) {
|
309
|
+
return X ^ rotl(X, 9) ^ rotl(X, 17);
|
310
|
+
}
|
311
|
+
function P1(X) {
|
312
|
+
return X ^ rotl(X, 15) ^ rotl(X, 23);
|
313
|
+
}
|
314
|
+
var SHA2 = class extends Hash {
|
315
|
+
constructor(blockLen, outputLen, padOffset, isLE2) {
|
316
|
+
super();
|
317
|
+
this.blockLen = blockLen;
|
318
|
+
this.outputLen = outputLen;
|
319
|
+
this.padOffset = padOffset;
|
320
|
+
this.isLE = isLE2;
|
321
|
+
this.buffer = new Uint8Array(blockLen);
|
322
|
+
this.view = createView(this.buffer);
|
323
|
+
}
|
324
|
+
buffer;
|
325
|
+
view;
|
326
|
+
finished = false;
|
327
|
+
length = 0;
|
328
|
+
pos = 0;
|
329
|
+
destroyed = false;
|
330
|
+
update(data) {
|
331
|
+
const { view, buffer, blockLen } = this;
|
332
|
+
data = toBytes(data);
|
333
|
+
const len = data.length;
|
334
|
+
for (let pos = 0; pos < len; ) {
|
335
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
336
|
+
if (take === blockLen) {
|
337
|
+
const dataView = createView(data);
|
338
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
339
|
+
this.process(dataView, pos);
|
340
|
+
continue;
|
341
|
+
}
|
342
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
343
|
+
this.pos += take;
|
344
|
+
pos += take;
|
345
|
+
if (this.pos === blockLen) {
|
346
|
+
this.process(view, 0);
|
347
|
+
this.pos = 0;
|
348
|
+
}
|
349
|
+
}
|
350
|
+
this.length += data.length;
|
351
|
+
this.roundClean();
|
352
|
+
return this;
|
353
|
+
}
|
354
|
+
digestInto(out) {
|
355
|
+
this.finished = true;
|
356
|
+
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
357
|
+
let { pos } = this;
|
358
|
+
buffer[pos++] = 128;
|
359
|
+
this.buffer.subarray(pos).fill(0);
|
360
|
+
if (this.padOffset > blockLen - pos) {
|
361
|
+
this.process(view, 0);
|
362
|
+
pos = 0;
|
363
|
+
}
|
364
|
+
for (let i = pos; i < blockLen; i++)
|
365
|
+
buffer[i] = 0;
|
366
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
|
367
|
+
this.process(view, 0);
|
368
|
+
const oview = createView(out);
|
369
|
+
const len = this.outputLen;
|
370
|
+
if (len % 4)
|
371
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
372
|
+
const outLen = len / 4;
|
373
|
+
const state = this.get();
|
374
|
+
if (outLen > state.length)
|
375
|
+
throw new Error("_sha2: outputLen bigger than state");
|
376
|
+
for (let i = 0; i < outLen; i++)
|
377
|
+
oview.setUint32(4 * i, state[i], isLE2);
|
378
|
+
}
|
379
|
+
digest() {
|
380
|
+
const { buffer, outputLen } = this;
|
381
|
+
this.digestInto(buffer);
|
382
|
+
const res = buffer.slice(0, outputLen);
|
383
|
+
this.destroy();
|
384
|
+
return res;
|
385
|
+
}
|
386
|
+
_cloneInto(to) {
|
387
|
+
to ||= new this.constructor();
|
388
|
+
to.set(...this.get());
|
389
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
390
|
+
to.length = length;
|
391
|
+
to.pos = pos;
|
392
|
+
to.finished = finished;
|
393
|
+
to.destroyed = destroyed;
|
394
|
+
if (length % blockLen)
|
395
|
+
to.buffer.set(buffer);
|
396
|
+
return to;
|
397
|
+
}
|
398
|
+
};
|
399
|
+
var IV = new Uint32Array([1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214]);
|
400
|
+
var SM3_W = new Uint32Array(68);
|
401
|
+
var SM3_M = new Uint32Array(64);
|
402
|
+
var T1 = 2043430169;
|
403
|
+
var T2 = 2055708042;
|
404
|
+
var SM3 = class extends SHA2 {
|
405
|
+
A = IV[0] | 0;
|
406
|
+
B = IV[1] | 0;
|
407
|
+
C = IV[2] | 0;
|
408
|
+
D = IV[3] | 0;
|
409
|
+
E = IV[4] | 0;
|
410
|
+
F = IV[5] | 0;
|
411
|
+
G = IV[6] | 0;
|
412
|
+
H = IV[7] | 0;
|
413
|
+
constructor() {
|
414
|
+
super(64, 32, 8, false);
|
415
|
+
}
|
416
|
+
get() {
|
417
|
+
const { A, B, C, D, E, F, G, H } = this;
|
418
|
+
return [A, B, C, D, E, F, G, H];
|
419
|
+
}
|
420
|
+
set(A, B, C, D, E, F, G, H) {
|
421
|
+
this.A = A | 0;
|
422
|
+
this.B = B | 0;
|
423
|
+
this.C = C | 0;
|
424
|
+
this.D = D | 0;
|
425
|
+
this.E = E | 0;
|
426
|
+
this.F = F | 0;
|
427
|
+
this.G = G | 0;
|
428
|
+
this.H = H | 0;
|
429
|
+
}
|
430
|
+
process(view, offset) {
|
431
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
432
|
+
SM3_W[i] = view.getUint32(offset, false);
|
433
|
+
for (let i = 16; i < 68; i++) {
|
434
|
+
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];
|
435
|
+
}
|
436
|
+
for (let i = 0; i < 64; i++) {
|
437
|
+
SM3_M[i] = SM3_W[i] ^ SM3_W[i + 4];
|
438
|
+
}
|
439
|
+
let { A, B, C, D, E, F, G, H } = this;
|
440
|
+
for (let j = 0; j < 64; j++) {
|
441
|
+
let small = j >= 0 && j <= 15;
|
442
|
+
let T = small ? T1 : T2;
|
443
|
+
let SS1 = rotl(rotl(A, 12) + E + rotl(T, j), 7);
|
444
|
+
let SS2 = SS1 ^ rotl(A, 12);
|
445
|
+
let TT1 = (small ? BoolB(A, B, C) : BoolA(A, B, C)) + D + SS2 + SM3_M[j] | 0;
|
446
|
+
let TT2 = (small ? BoolB(E, F, G) : BoolC(E, F, G)) + H + SS1 + SM3_W[j] | 0;
|
447
|
+
D = C;
|
448
|
+
C = rotl(B, 9);
|
449
|
+
B = A;
|
450
|
+
A = TT1;
|
451
|
+
H = G;
|
452
|
+
G = rotl(F, 19);
|
453
|
+
F = E;
|
454
|
+
E = P0(TT2);
|
455
|
+
}
|
456
|
+
A = A ^ this.A | 0;
|
457
|
+
B = B ^ this.B | 0;
|
458
|
+
C = C ^ this.C | 0;
|
459
|
+
D = D ^ this.D | 0;
|
460
|
+
E = E ^ this.E | 0;
|
461
|
+
F = F ^ this.F | 0;
|
462
|
+
G = G ^ this.G | 0;
|
463
|
+
H = H ^ this.H | 0;
|
464
|
+
this.set(A, B, C, D, E, F, G, H);
|
465
|
+
}
|
466
|
+
roundClean() {
|
467
|
+
SM3_W.fill(0);
|
468
|
+
}
|
469
|
+
destroy() {
|
470
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
471
|
+
this.buffer.fill(0);
|
472
|
+
}
|
473
|
+
};
|
474
|
+
var sm3 = wrapConstructor(() => new SM3());
|
475
|
+
|
476
|
+
// src/sm2/hmac.ts
|
477
|
+
var HMAC = class extends Hash {
|
478
|
+
oHash;
|
479
|
+
iHash;
|
480
|
+
blockLen;
|
481
|
+
outputLen;
|
482
|
+
finished = false;
|
483
|
+
destroyed = false;
|
484
|
+
constructor(hash, _key) {
|
485
|
+
super();
|
486
|
+
const key = toBytes(_key);
|
487
|
+
this.iHash = hash.create();
|
488
|
+
if (typeof this.iHash.update !== "function")
|
489
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
490
|
+
this.blockLen = this.iHash.blockLen;
|
491
|
+
this.outputLen = this.iHash.outputLen;
|
492
|
+
const blockLen = this.blockLen;
|
493
|
+
const pad = new Uint8Array(blockLen);
|
494
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
495
|
+
for (let i = 0; i < pad.length; i++)
|
496
|
+
pad[i] ^= 54;
|
497
|
+
this.iHash.update(pad);
|
498
|
+
this.oHash = hash.create();
|
499
|
+
for (let i = 0; i < pad.length; i++)
|
500
|
+
pad[i] ^= 54 ^ 92;
|
501
|
+
this.oHash.update(pad);
|
502
|
+
pad.fill(0);
|
503
|
+
}
|
504
|
+
update(buf) {
|
505
|
+
this.iHash.update(buf);
|
506
|
+
return this;
|
507
|
+
}
|
508
|
+
digestInto(out) {
|
509
|
+
this.finished = true;
|
510
|
+
this.iHash.digestInto(out);
|
511
|
+
this.oHash.update(out);
|
512
|
+
this.oHash.digestInto(out);
|
513
|
+
this.destroy();
|
514
|
+
}
|
515
|
+
digest() {
|
516
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
517
|
+
this.digestInto(out);
|
518
|
+
return out;
|
519
|
+
}
|
520
|
+
_cloneInto(to) {
|
521
|
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
522
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
523
|
+
to = to;
|
524
|
+
to.finished = finished;
|
525
|
+
to.destroyed = destroyed;
|
526
|
+
to.blockLen = blockLen;
|
527
|
+
to.outputLen = outputLen;
|
528
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
529
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
530
|
+
return to;
|
531
|
+
}
|
532
|
+
destroy() {
|
533
|
+
this.destroyed = true;
|
534
|
+
this.oHash.destroy();
|
535
|
+
this.iHash.destroy();
|
536
|
+
}
|
537
|
+
};
|
538
|
+
var hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
539
|
+
hmac.create = (hash, key) => new HMAC(hash, key);
|
540
|
+
|
541
|
+
// src/sm2/ec.ts
|
542
|
+
var import_utils3 = require("@noble/curves/abstract/utils");
|
425
543
|
var sm2Fp = (0, import_modular.Field)(BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991999"));
|
426
544
|
var sm2Curve = (0, import_weierstrass.weierstrass)({
|
427
545
|
a: BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991996"),
|
@@ -431,18 +549,19 @@ var sm2Curve = (0, import_weierstrass.weierstrass)({
|
|
431
549
|
n: BigInt("115792089210356248756420345214020892766061623724957744567843809356293439045923"),
|
432
550
|
Gx: BigInt("22963146547237050559479531362550074578802567295341616970375194840604139615431"),
|
433
551
|
Gy: BigInt("85132369209828568825618990617112496413088388631904505083283536607588877201568"),
|
434
|
-
hash:
|
435
|
-
hmac: (key, ...msgs) => hmac(
|
552
|
+
hash: sm3,
|
553
|
+
hmac: (key, ...msgs) => hmac(sm3, key, (0, import_utils3.concatBytes)(...msgs)),
|
436
554
|
randomBytes
|
437
555
|
});
|
556
|
+
var field = (0, import_modular.Field)(BigInt(sm2Curve.CURVE.n));
|
438
557
|
|
439
558
|
// src/sm2/utils.ts
|
440
559
|
var import_modular2 = require("@noble/curves/abstract/modular");
|
441
560
|
function generateKeyPairHex(str) {
|
442
561
|
const privateKey = str ? utils2.numberToBytesBE((0, import_modular2.mod)(BigInt(str), ONE) + ONE, 32) : sm2Curve.utils.randomPrivateKey();
|
443
562
|
const publicKey = sm2Curve.getPublicKey(privateKey, false);
|
444
|
-
const privPad =
|
445
|
-
const pubPad =
|
563
|
+
const privPad = leftPad(utils2.bytesToHex(privateKey), 64);
|
564
|
+
const pubPad = leftPad(utils2.bytesToHex(publicKey), 64);
|
446
565
|
return { privateKey: privPad, publicKey: pubPad };
|
447
566
|
}
|
448
567
|
function compressPublicKeyHex(s) {
|
@@ -471,7 +590,7 @@ function utf8ToHex(input) {
|
|
471
590
|
}
|
472
591
|
return hexChars.join("");
|
473
592
|
}
|
474
|
-
function
|
593
|
+
function leftPad(input, num) {
|
475
594
|
if (input.length >= num)
|
476
595
|
return input;
|
477
596
|
return new Array(num - input.length + 1).join("0") + input;
|
@@ -503,7 +622,7 @@ function arrayToUtf8(arr) {
|
|
503
622
|
function hexToArray(hexStr) {
|
504
623
|
let hexStrLength = hexStr.length;
|
505
624
|
if (hexStrLength % 2 !== 0) {
|
506
|
-
hexStr =
|
625
|
+
hexStr = leftPad(hexStr, hexStrLength + 1);
|
507
626
|
}
|
508
627
|
hexStrLength = hexStr.length;
|
509
628
|
const wordLength = hexStrLength / 2;
|
@@ -517,9 +636,9 @@ function verifyPublicKey(publicKey) {
|
|
517
636
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
518
637
|
if (!point)
|
519
638
|
return false;
|
520
|
-
const
|
639
|
+
const x2 = point.x;
|
521
640
|
const y = point.y;
|
522
|
-
return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.
|
641
|
+
return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.addN(sm2Fp.mulN(x2, sm2Fp.sqrN(x2)), sm2Fp.mulN(x2, sm2Curve.CURVE.a)), sm2Curve.CURVE.b);
|
523
642
|
}
|
524
643
|
function comparePublicKeyHex(publicKey1, publicKey2) {
|
525
644
|
const point1 = sm2Curve.ProjectivePoint.fromHex(publicKey1);
|
@@ -530,36 +649,26 @@ function comparePublicKeyHex(publicKey1, publicKey2) {
|
|
530
649
|
return false;
|
531
650
|
return point1.equals(point2);
|
532
651
|
}
|
533
|
-
function concatArray(...arrays) {
|
534
|
-
let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
|
535
|
-
if (!arrays.length)
|
536
|
-
return new Uint8Array();
|
537
|
-
let result = new Uint8Array(totalLength);
|
538
|
-
let length = 0;
|
539
|
-
for (let array of arrays) {
|
540
|
-
result.set(array, length);
|
541
|
-
length += array.length;
|
542
|
-
}
|
543
|
-
return result;
|
544
|
-
}
|
545
652
|
|
546
653
|
// src/sm2/index.ts
|
547
|
-
var mod2 = __toESM(require("@noble/curves/abstract/modular"));
|
548
654
|
var utils4 = __toESM(require("@noble/curves/abstract/utils"));
|
549
655
|
|
550
656
|
// src/sm2/kx.ts
|
551
657
|
var utils3 = __toESM(require("@noble/curves/abstract/utils"));
|
552
|
-
var import_modular3 = require("@noble/curves/abstract/modular");
|
553
|
-
var field = (0, import_modular3.Field)(BigInt(sm2Curve.CURVE.n));
|
554
658
|
var wPow2 = utils3.hexToNumber("80000000000000000000000000000000");
|
555
659
|
var wPow2Sub1 = utils3.hexToNumber("7fffffffffffffffffffffffffffffff");
|
556
660
|
function hkdf(z, keylen) {
|
557
|
-
let t = new Uint8Array();
|
558
661
|
let msg = new Uint8Array(keylen);
|
559
662
|
let ct = 1;
|
560
663
|
let offset = 0;
|
664
|
+
let t = EmptyArray;
|
665
|
+
const ctShift = new Uint8Array(4);
|
561
666
|
const nextT = () => {
|
562
|
-
|
667
|
+
ctShift[0] = ct >> 24 & 255;
|
668
|
+
ctShift[1] = ct >> 16 & 255;
|
669
|
+
ctShift[2] = ct >> 8 & 255;
|
670
|
+
ctShift[3] = ct & 255;
|
671
|
+
t = sm3(utils3.concatBytes(z, ctShift));
|
563
672
|
ct++;
|
564
673
|
offset = 0;
|
565
674
|
};
|
@@ -571,28 +680,32 @@ function hkdf(z, keylen) {
|
|
571
680
|
}
|
572
681
|
return msg;
|
573
682
|
}
|
574
|
-
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, idA = "1234567812345678", idB = "1234567812345678"
|
683
|
+
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, sharedKeyLength, isRecipient = false, idA = "1234567812345678", idB = "1234567812345678") {
|
575
684
|
const RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey);
|
576
685
|
const RB = sm2Curve.ProjectivePoint.fromHex(ephemeralPublicKeyB);
|
577
686
|
const PB = sm2Curve.ProjectivePoint.fromHex(publicKeyB);
|
578
|
-
|
579
|
-
|
687
|
+
let ZA = getZ(keypairA.publicKey, idA);
|
688
|
+
let ZB = getZ(publicKeyB, idB);
|
689
|
+
if (isRecipient) {
|
690
|
+
[ZA, ZB] = [ZB, ZA];
|
691
|
+
}
|
580
692
|
const rA = utils3.hexToNumber(ephemeralKeypairA.privateKey);
|
581
693
|
const dA = utils3.hexToNumber(keypairA.privateKey);
|
582
694
|
const x1 = RA.x;
|
583
|
-
const x1_ =
|
584
|
-
const tA = field.add(dA, field.
|
695
|
+
const x1_ = wPow2 + (x1 & wPow2Sub1);
|
696
|
+
const tA = field.add(dA, field.mulN(x1_, rA));
|
585
697
|
const x2 = RB.x;
|
586
698
|
const x2_ = field.add(wPow2, x2 & wPow2Sub1);
|
587
699
|
const U = RB.multiply(x2_).add(PB).multiply(tA);
|
588
|
-
const xU = hexToArray(
|
589
|
-
const yU = hexToArray(
|
590
|
-
const KA = hkdf(
|
700
|
+
const xU = hexToArray(leftPad(utils3.numberToHexUnpadded(U.x), 64));
|
701
|
+
const yU = hexToArray(leftPad(utils3.numberToHexUnpadded(U.y), 64));
|
702
|
+
const KA = hkdf(utils3.concatBytes(xU, yU, ZA, ZB), sharedKeyLength);
|
591
703
|
return KA;
|
592
704
|
}
|
593
705
|
|
594
706
|
// src/sm2/index.ts
|
595
707
|
var C1C2C3 = 0;
|
708
|
+
var EmptyArray = new Uint8Array();
|
596
709
|
function doEncrypt(msg, publicKey, cipherMode = 1) {
|
597
710
|
const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
|
598
711
|
const publicKeyPoint = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
@@ -602,26 +715,33 @@ function doEncrypt(msg, publicKey, cipherMode = 1) {
|
|
602
715
|
if (c1.length > 128)
|
603
716
|
c1 = c1.substring(c1.length - 128);
|
604
717
|
const p = publicKeyPoint.multiply(k);
|
605
|
-
const x2 = hexToArray(
|
606
|
-
const y2 = hexToArray(
|
607
|
-
const c3 =
|
718
|
+
const x2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.x), 64));
|
719
|
+
const y2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.y), 64));
|
720
|
+
const c3 = bytesToHex(sm3(utils4.concatBytes(x2, msgArr, y2)));
|
721
|
+
xorCipherStream(x2, y2, msgArr);
|
722
|
+
const c2 = bytesToHex(msgArr);
|
723
|
+
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
724
|
+
}
|
725
|
+
function xorCipherStream(x2, y2, msg) {
|
608
726
|
let ct = 1;
|
609
727
|
let offset = 0;
|
610
|
-
let t =
|
611
|
-
const
|
728
|
+
let t = EmptyArray;
|
729
|
+
const ctShift = new Uint8Array(4);
|
612
730
|
const nextT = () => {
|
613
|
-
|
731
|
+
ctShift[0] = ct >> 24 & 255;
|
732
|
+
ctShift[1] = ct >> 16 & 255;
|
733
|
+
ctShift[2] = ct >> 8 & 255;
|
734
|
+
ctShift[3] = ct & 255;
|
735
|
+
t = sm3(utils4.concatBytes(x2, y2, ctShift));
|
614
736
|
ct++;
|
615
737
|
offset = 0;
|
616
738
|
};
|
617
739
|
nextT();
|
618
|
-
for (let i = 0, len =
|
740
|
+
for (let i = 0, len = msg.length; i < len; i++) {
|
619
741
|
if (offset === t.length)
|
620
742
|
nextT();
|
621
|
-
|
743
|
+
msg[i] ^= t[offset++] & 255;
|
622
744
|
}
|
623
|
-
const c2 = arrayToHex(Array.from(msgArr));
|
624
|
-
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
625
745
|
}
|
626
746
|
function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
627
747
|
output = "string"
|
@@ -636,24 +756,10 @@ function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
|
636
756
|
const msg = hexToArray(c2);
|
637
757
|
const c1 = sm2Curve.ProjectivePoint.fromHex("04" + encryptData.substring(0, 128));
|
638
758
|
const p = c1.multiply(privateKeyInteger);
|
639
|
-
const x2 = hexToArray(
|
640
|
-
const y2 = hexToArray(
|
641
|
-
|
642
|
-
|
643
|
-
let t = new Uint8Array();
|
644
|
-
const z = concatArray(x2, y2);
|
645
|
-
const nextT = () => {
|
646
|
-
t = sm3(Uint8Array.from([...z, ct >> 24 & 255, ct >> 16 & 255, ct >> 8 & 255, ct & 255]));
|
647
|
-
ct++;
|
648
|
-
offset = 0;
|
649
|
-
};
|
650
|
-
nextT();
|
651
|
-
for (let i = 0, len = msg.length; i < len; i++) {
|
652
|
-
if (offset === t.length)
|
653
|
-
nextT();
|
654
|
-
msg[i] ^= t[offset++] & 255;
|
655
|
-
}
|
656
|
-
const checkC3 = arrayToHex(Array.from(sm3(concatArray(x2, msg, y2))));
|
759
|
+
const x2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.x), 64));
|
760
|
+
const y2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.y), 64));
|
761
|
+
xorCipherStream(x2, y2, msg);
|
762
|
+
const checkC3 = arrayToHex(Array.from(sm3(utils4.concatBytes(x2, msg, y2))));
|
657
763
|
if (checkC3 === c3.toLowerCase()) {
|
658
764
|
return output === "array" ? msg : arrayToUtf8(msg);
|
659
765
|
} else {
|
@@ -687,13 +793,13 @@ function doSignature(msg, privateKey, options = {}) {
|
|
687
793
|
point = getPoint();
|
688
794
|
}
|
689
795
|
k = point.k;
|
690
|
-
r =
|
796
|
+
r = field.add(e, point.x1);
|
691
797
|
} while (r === ZERO || r + k === sm2Curve.CURVE.n);
|
692
|
-
s =
|
798
|
+
s = field.mul(field.inv(field.addN(dA, ONE)), field.subN(k, field.mulN(r, dA)));
|
693
799
|
} while (s === ZERO);
|
694
800
|
if (der)
|
695
801
|
return encodeDer(r, s);
|
696
|
-
return
|
802
|
+
return leftPad(utils4.numberToHexUnpadded(r), 64) + leftPad(utils4.numberToHexUnpadded(s), 64);
|
697
803
|
}
|
698
804
|
function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
699
805
|
let hashHex;
|
@@ -719,19 +825,19 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
719
825
|
}
|
720
826
|
const PA = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
721
827
|
const e = utils4.hexToNumber(hashHex);
|
722
|
-
const t =
|
828
|
+
const t = field.add(r, s);
|
723
829
|
if (t === ZERO)
|
724
830
|
return false;
|
725
831
|
const x1y1 = sm2Curve.ProjectivePoint.BASE.multiply(s).add(PA.multiply(t));
|
726
|
-
const R =
|
832
|
+
const R = field.add(e, x1y1.x);
|
727
833
|
return r === R;
|
728
834
|
}
|
729
|
-
function
|
835
|
+
function getZ(publicKey, userId = "1234567812345678") {
|
730
836
|
userId = utf8ToHex(userId);
|
731
|
-
const a =
|
732
|
-
const b =
|
733
|
-
const gx =
|
734
|
-
const gy =
|
837
|
+
const a = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.a), 64);
|
838
|
+
const b = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.b), 64);
|
839
|
+
const gx = leftPad(utils4.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.x), 64);
|
840
|
+
const gy = leftPad(utils4.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.y), 64);
|
735
841
|
let px;
|
736
842
|
let py;
|
737
843
|
if (publicKey.length === 128) {
|
@@ -739,17 +845,21 @@ function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
|
739
845
|
py = publicKey.substring(64, 128);
|
740
846
|
} else {
|
741
847
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
742
|
-
px =
|
743
|
-
py =
|
848
|
+
px = leftPad(utils4.numberToHexUnpadded(point.x), 64);
|
849
|
+
py = leftPad(utils4.numberToHexUnpadded(point.y), 64);
|
744
850
|
}
|
745
851
|
const data = hexToArray(userId + a + b + gx + gy + px + py);
|
746
852
|
const entl = userId.length * 4;
|
747
|
-
const z = sm3(
|
748
|
-
return
|
853
|
+
const z = sm3(utils4.concatBytes(new Uint8Array([entl >> 8 & 255, entl & 255]), data));
|
854
|
+
return z;
|
855
|
+
}
|
856
|
+
function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
857
|
+
const z = getZ(publicKey, userId);
|
858
|
+
return bytesToHex(sm3(utils4.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
|
749
859
|
}
|
750
860
|
function getPublicKeyFromPrivateKey(privateKey) {
|
751
861
|
const pubKey = sm2Curve.getPublicKey(privateKey, false);
|
752
|
-
const pubPad =
|
862
|
+
const pubPad = leftPad(utils4.bytesToHex(pubKey), 64);
|
753
863
|
return pubPad;
|
754
864
|
}
|
755
865
|
function getPoint() {
|
@@ -763,6 +873,48 @@ function getPoint() {
|
|
763
873
|
};
|
764
874
|
}
|
765
875
|
|
876
|
+
// src/sm3/index.ts
|
877
|
+
function utf8ToArray(str) {
|
878
|
+
const arr = [];
|
879
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
880
|
+
const point = str.codePointAt(i);
|
881
|
+
if (point <= 127) {
|
882
|
+
arr.push(point);
|
883
|
+
} else if (point <= 2047) {
|
884
|
+
arr.push(192 | point >>> 6);
|
885
|
+
arr.push(128 | point & 63);
|
886
|
+
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
887
|
+
arr.push(224 | point >>> 12);
|
888
|
+
arr.push(128 | point >>> 6 & 63);
|
889
|
+
arr.push(128 | point & 63);
|
890
|
+
} else if (point >= 65536 && point <= 1114111) {
|
891
|
+
i++;
|
892
|
+
arr.push(240 | point >>> 18 & 28);
|
893
|
+
arr.push(128 | point >>> 12 & 63);
|
894
|
+
arr.push(128 | point >>> 6 & 63);
|
895
|
+
arr.push(128 | point & 63);
|
896
|
+
} else {
|
897
|
+
arr.push(point);
|
898
|
+
throw new Error("input is not supported");
|
899
|
+
}
|
900
|
+
}
|
901
|
+
return new Uint8Array(arr);
|
902
|
+
}
|
903
|
+
function sm32(input, options) {
|
904
|
+
input = typeof input === "string" ? utf8ToArray(input) : input;
|
905
|
+
if (options) {
|
906
|
+
const mode = options.mode || "hmac";
|
907
|
+
if (mode !== "hmac")
|
908
|
+
throw new Error("invalid mode");
|
909
|
+
let key = options.key;
|
910
|
+
if (!key)
|
911
|
+
throw new Error("invalid key");
|
912
|
+
key = typeof key === "string" ? hexToArray(key) : key;
|
913
|
+
return bytesToHex(hmac(sm3, key, input));
|
914
|
+
}
|
915
|
+
return bytesToHex(sm3(input));
|
916
|
+
}
|
917
|
+
|
766
918
|
// src/sm4/index.ts
|
767
919
|
var sm4_exports = {};
|
768
920
|
__export(sm4_exports, {
|
@@ -1074,9 +1226,9 @@ function l1(b) {
|
|
1074
1226
|
function l2(b) {
|
1075
1227
|
return b ^ rotl(b, 13) ^ rotl(b, 23);
|
1076
1228
|
}
|
1229
|
+
var x = new Uint32Array(4);
|
1230
|
+
var tmp = new Uint32Array(4);
|
1077
1231
|
function sms4Crypt(input, output, roundKey) {
|
1078
|
-
const x = new Uint32Array(4);
|
1079
|
-
const tmp = new Uint32Array(4);
|
1080
1232
|
for (let i = 0; i < 4; i++) {
|
1081
1233
|
tmp[0] = input[4 * i] & 255;
|
1082
1234
|
tmp[1] = input[4 * i + 1] & 255;
|
@@ -1102,8 +1254,6 @@ function sms4Crypt(input, output, roundKey) {
|
|
1102
1254
|
}
|
1103
1255
|
}
|
1104
1256
|
function sms4KeyExt(key, roundKey, cryptFlag) {
|
1105
|
-
const x = new Uint32Array(4);
|
1106
|
-
const tmp = new Uint32Array(4);
|
1107
1257
|
for (let i = 0; i < 4; i++) {
|
1108
1258
|
tmp[0] = key[0 + 4 * i] & 255;
|
1109
1259
|
tmp[1] = key[1 + 4 * i] & 255;
|
@@ -1133,6 +1283,7 @@ function sms4KeyExt(key, roundKey, cryptFlag) {
|
|
1133
1283
|
}
|
1134
1284
|
}
|
1135
1285
|
}
|
1286
|
+
var blockOutput = new Uint8Array(16);
|
1136
1287
|
function sm4(inArray, key, cryptFlag, options = {}) {
|
1137
1288
|
let {
|
1138
1289
|
padding = "pkcs#7",
|
@@ -1176,8 +1327,7 @@ function sm4(inArray, key, cryptFlag, options = {}) {
|
|
1176
1327
|
let restLen = inArray.length;
|
1177
1328
|
let point = 0;
|
1178
1329
|
while (restLen >= BLOCK) {
|
1179
|
-
const input = inArray.
|
1180
|
-
const output2 = new Uint8Array(16);
|
1330
|
+
const input = inArray.subarray(point, point + 16);
|
1181
1331
|
if (mode === "cbc") {
|
1182
1332
|
for (let i = 0; i < BLOCK; i++) {
|
1183
1333
|
if (cryptFlag !== DECRYPT) {
|
@@ -1185,18 +1335,18 @@ function sm4(inArray, key, cryptFlag, options = {}) {
|
|
1185
1335
|
}
|
1186
1336
|
}
|
1187
1337
|
}
|
1188
|
-
sms4Crypt(input,
|
1338
|
+
sms4Crypt(input, blockOutput, roundKey);
|
1189
1339
|
for (let i = 0; i < BLOCK; i++) {
|
1190
1340
|
if (mode === "cbc") {
|
1191
1341
|
if (cryptFlag === DECRYPT) {
|
1192
|
-
|
1342
|
+
blockOutput[i] ^= lastVector[i];
|
1193
1343
|
}
|
1194
1344
|
}
|
1195
|
-
outArray[point + i] =
|
1345
|
+
outArray[point + i] = blockOutput[i];
|
1196
1346
|
}
|
1197
1347
|
if (mode === "cbc") {
|
1198
1348
|
if (cryptFlag !== DECRYPT) {
|
1199
|
-
lastVector =
|
1349
|
+
lastVector = blockOutput;
|
1200
1350
|
} else {
|
1201
1351
|
lastVector = input;
|
1202
1352
|
}
|
@@ -1215,7 +1365,7 @@ function sm4(inArray, key, cryptFlag, options = {}) {
|
|
1215
1365
|
}
|
1216
1366
|
if (output !== "array") {
|
1217
1367
|
if (cryptFlag !== DECRYPT) {
|
1218
|
-
return
|
1368
|
+
return bytesToHex(outArray);
|
1219
1369
|
} else {
|
1220
1370
|
return arrayToUtf8(outArray);
|
1221
1371
|
}
|
@@ -1235,3 +1385,4 @@ function decrypt(inArray, key, options = {}) {
|
|
1235
1385
|
sm3,
|
1236
1386
|
sm4
|
1237
1387
|
});
|
1388
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|