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.mjs
CHANGED
@@ -7,12 +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,
|
12
13
|
calculateSharedKey: () => calculateSharedKey,
|
13
14
|
comparePublicKeyHex: () => comparePublicKeyHex,
|
14
15
|
compressPublicKeyHex: () => compressPublicKeyHex,
|
15
|
-
concatArray: () => concatArray,
|
16
16
|
doDecrypt: () => doDecrypt,
|
17
17
|
doEncrypt: () => doEncrypt,
|
18
18
|
doSignature: () => doSignature,
|
@@ -21,9 +21,10 @@ __export(sm2_exports, {
|
|
21
21
|
getHash: () => getHash,
|
22
22
|
getPoint: () => getPoint,
|
23
23
|
getPublicKeyFromPrivateKey: () => getPublicKeyFromPrivateKey,
|
24
|
+
getZ: () => getZ,
|
24
25
|
hexToArray: () => hexToArray,
|
25
26
|
initRNGPool: () => initRNGPool,
|
26
|
-
leftPad: () =>
|
27
|
+
leftPad: () => leftPad,
|
27
28
|
utf8ToHex: () => utf8ToHex,
|
28
29
|
verifyPublicKey: () => verifyPublicKey
|
29
30
|
});
|
@@ -158,181 +159,7 @@ import * as utils2 from "@noble/curves/abstract/utils";
|
|
158
159
|
import { weierstrass } from "@noble/curves/abstract/weierstrass";
|
159
160
|
import { Field } from "@noble/curves/abstract/modular";
|
160
161
|
|
161
|
-
// src/sm2/
|
162
|
-
var W = new Uint32Array(68);
|
163
|
-
var M = new Uint32Array(64);
|
164
|
-
function rotl(x, n) {
|
165
|
-
const s = n & 31;
|
166
|
-
return x << s | x >>> 32 - s;
|
167
|
-
}
|
168
|
-
function xor(x, y) {
|
169
|
-
const result = new Uint8Array(x.length);
|
170
|
-
for (let i = x.length - 1; i >= 0; i--)
|
171
|
-
result[i] = (x[i] ^ y[i]) & 255;
|
172
|
-
return result;
|
173
|
-
}
|
174
|
-
function P0(X) {
|
175
|
-
return X ^ rotl(X, 9) ^ rotl(X, 17);
|
176
|
-
}
|
177
|
-
function P1(X) {
|
178
|
-
return X ^ rotl(X, 15) ^ rotl(X, 23);
|
179
|
-
}
|
180
|
-
function sm3(array) {
|
181
|
-
let len = array.length * 8;
|
182
|
-
let k = len % 512;
|
183
|
-
k = k >= 448 ? 512 - k % 448 - 1 : 448 - k - 1;
|
184
|
-
const kArr = new Array((k - 7) / 8);
|
185
|
-
const lenArr = new Array(8);
|
186
|
-
for (let i = 0, len2 = kArr.length; i < len2; i++)
|
187
|
-
kArr[i] = 0;
|
188
|
-
for (let i = 0, len2 = lenArr.length; i < len2; i++)
|
189
|
-
lenArr[i] = 0;
|
190
|
-
let lenString = len.toString(2);
|
191
|
-
for (let i = 7; i >= 0; i--) {
|
192
|
-
if (lenString.length > 8) {
|
193
|
-
const start = lenString.length - 8;
|
194
|
-
lenArr[i] = parseInt(lenString.substring(start), 2);
|
195
|
-
lenString = lenString.substring(0, start);
|
196
|
-
} else if (lenString.length > 0) {
|
197
|
-
lenArr[i] = parseInt(lenString, 2);
|
198
|
-
lenString = "";
|
199
|
-
}
|
200
|
-
}
|
201
|
-
const m = Uint8Array.from([...array, 128, ...kArr, ...lenArr]);
|
202
|
-
const dataView = new DataView(m.buffer, 0);
|
203
|
-
const n = m.length / 64;
|
204
|
-
const V = new Uint32Array([1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214]);
|
205
|
-
for (let i = 0; i < n; i++) {
|
206
|
-
W.fill(0);
|
207
|
-
M.fill(0);
|
208
|
-
const start = 16 * i;
|
209
|
-
for (let j = 0; j < 16; j++) {
|
210
|
-
W[j] = dataView.getUint32((start + j) * 4, false);
|
211
|
-
}
|
212
|
-
for (let j = 16; j < 68; j++) {
|
213
|
-
W[j] = P1(W[j - 16] ^ W[j - 9] ^ rotl(W[j - 3], 15)) ^ rotl(W[j - 13], 7) ^ W[j - 6];
|
214
|
-
}
|
215
|
-
for (let j = 0; j < 64; j++) {
|
216
|
-
M[j] = W[j] ^ W[j + 4];
|
217
|
-
}
|
218
|
-
const T1 = 2043430169;
|
219
|
-
const T2 = 2055708042;
|
220
|
-
let A = V[0];
|
221
|
-
let B = V[1];
|
222
|
-
let C = V[2];
|
223
|
-
let D = V[3];
|
224
|
-
let E = V[4];
|
225
|
-
let F = V[5];
|
226
|
-
let G = V[6];
|
227
|
-
let H = V[7];
|
228
|
-
let SS1;
|
229
|
-
let SS2;
|
230
|
-
let TT1;
|
231
|
-
let TT2;
|
232
|
-
let T;
|
233
|
-
for (let j = 0; j < 64; j++) {
|
234
|
-
T = j >= 0 && j <= 15 ? T1 : T2;
|
235
|
-
SS1 = rotl(rotl(A, 12) + E + rotl(T, j), 7);
|
236
|
-
SS2 = SS1 ^ rotl(A, 12);
|
237
|
-
TT1 = (j >= 0 && j <= 15 ? A ^ B ^ C : A & B | A & C | B & C) + D + SS2 + M[j];
|
238
|
-
TT2 = (j >= 0 && j <= 15 ? E ^ F ^ G : E & F | ~E & G) + H + SS1 + W[j];
|
239
|
-
D = C;
|
240
|
-
C = rotl(B, 9);
|
241
|
-
B = A;
|
242
|
-
A = TT1;
|
243
|
-
H = G;
|
244
|
-
G = rotl(F, 19);
|
245
|
-
F = E;
|
246
|
-
E = P0(TT2);
|
247
|
-
}
|
248
|
-
V[0] ^= A;
|
249
|
-
V[1] ^= B;
|
250
|
-
V[2] ^= C;
|
251
|
-
V[3] ^= D;
|
252
|
-
V[4] ^= E;
|
253
|
-
V[5] ^= F;
|
254
|
-
V[6] ^= G;
|
255
|
-
V[7] ^= H;
|
256
|
-
}
|
257
|
-
const result = new Uint8Array(V.length * 4);
|
258
|
-
for (let i = 0, len2 = V.length; i < len2; i++) {
|
259
|
-
const word = V[i];
|
260
|
-
result[i * 4] = (word & 4278190080) >>> 24;
|
261
|
-
result[i * 4 + 1] = (word & 16711680) >>> 16;
|
262
|
-
result[i * 4 + 2] = (word & 65280) >>> 8;
|
263
|
-
result[i * 4 + 3] = word & 255;
|
264
|
-
}
|
265
|
-
return result;
|
266
|
-
}
|
267
|
-
var blockLen = 64;
|
268
|
-
var iPad = new Uint8Array(blockLen);
|
269
|
-
var oPad = new Uint8Array(blockLen);
|
270
|
-
for (let i = 0; i < blockLen; i++) {
|
271
|
-
iPad[i] = 54;
|
272
|
-
oPad[i] = 92;
|
273
|
-
}
|
274
|
-
function hmac(input, key) {
|
275
|
-
if (key.length > blockLen)
|
276
|
-
key = sm3(key);
|
277
|
-
while (key.length < blockLen) {
|
278
|
-
const padKey = new Uint8Array(blockLen);
|
279
|
-
padKey.set(key);
|
280
|
-
key = padKey;
|
281
|
-
}
|
282
|
-
const iPadKey = xor(key, iPad);
|
283
|
-
const oPadKey = xor(key, oPad);
|
284
|
-
const hash = sm3(concatArray(iPadKey, input));
|
285
|
-
return sm3(concatArray(oPadKey, hash));
|
286
|
-
}
|
287
|
-
|
288
|
-
// src/sm3/index.ts
|
289
|
-
var sm3_exports = {};
|
290
|
-
__export(sm3_exports, {
|
291
|
-
sm3: () => sm32,
|
292
|
-
utf8ToArray: () => utf8ToArray
|
293
|
-
});
|
294
|
-
function utf8ToArray(str) {
|
295
|
-
const arr = [];
|
296
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
297
|
-
const point = str.codePointAt(i);
|
298
|
-
if (point <= 127) {
|
299
|
-
arr.push(point);
|
300
|
-
} else if (point <= 2047) {
|
301
|
-
arr.push(192 | point >>> 6);
|
302
|
-
arr.push(128 | point & 63);
|
303
|
-
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
304
|
-
arr.push(224 | point >>> 12);
|
305
|
-
arr.push(128 | point >>> 6 & 63);
|
306
|
-
arr.push(128 | point & 63);
|
307
|
-
} else if (point >= 65536 && point <= 1114111) {
|
308
|
-
i++;
|
309
|
-
arr.push(240 | point >>> 18 & 28);
|
310
|
-
arr.push(128 | point >>> 12 & 63);
|
311
|
-
arr.push(128 | point >>> 6 & 63);
|
312
|
-
arr.push(128 | point & 63);
|
313
|
-
} else {
|
314
|
-
arr.push(point);
|
315
|
-
throw new Error("input is not supported");
|
316
|
-
}
|
317
|
-
}
|
318
|
-
return new Uint8Array(arr);
|
319
|
-
}
|
320
|
-
function sm32(input, options) {
|
321
|
-
input = typeof input === "string" ? utf8ToArray(input) : input;
|
322
|
-
if (options) {
|
323
|
-
const mode = options.mode || "hmac";
|
324
|
-
if (mode !== "hmac")
|
325
|
-
throw new Error("invalid mode");
|
326
|
-
let key = options.key;
|
327
|
-
if (!key)
|
328
|
-
throw new Error("invalid key");
|
329
|
-
key = typeof key === "string" ? hexToArray(key) : key;
|
330
|
-
return arrayToHex(Array.from(hmac(input, key)));
|
331
|
-
}
|
332
|
-
return arrayToHex(Array.from(sm3(input)));
|
333
|
-
}
|
334
|
-
|
335
|
-
// src/sm2/ec.ts
|
162
|
+
// src/sm2/rng.ts
|
336
163
|
var DEFAULT_PRNG_POOL_SIZE = 16384;
|
337
164
|
var prngPool = new Uint8Array(0);
|
338
165
|
var _syncCrypto;
|
@@ -387,13 +214,304 @@ function randomBytes(length = 0) {
|
|
387
214
|
return result;
|
388
215
|
}
|
389
216
|
}
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
217
|
+
|
218
|
+
// src/sm3/utils.ts
|
219
|
+
var u8a = (a) => a instanceof Uint8Array;
|
220
|
+
var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
221
|
+
var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
222
|
+
if (!isLE)
|
223
|
+
throw new Error("Non little-endian hardware is not supported");
|
224
|
+
var hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, "0"));
|
225
|
+
function bytesToHex(bytes) {
|
226
|
+
if (!u8a(bytes))
|
227
|
+
throw new Error("Uint8Array expected");
|
228
|
+
let hex = "";
|
229
|
+
for (let i = 0; i < bytes.length; i++) {
|
230
|
+
hex += hexes[bytes[i]];
|
231
|
+
}
|
232
|
+
return hex;
|
233
|
+
}
|
234
|
+
function utf8ToBytes(str) {
|
235
|
+
if (typeof str !== "string")
|
236
|
+
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
237
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
238
|
+
}
|
239
|
+
function toBytes(data) {
|
240
|
+
if (typeof data === "string")
|
241
|
+
data = utf8ToBytes(data);
|
242
|
+
if (!u8a(data))
|
243
|
+
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
244
|
+
return data;
|
245
|
+
}
|
246
|
+
var Hash = class {
|
247
|
+
clone() {
|
248
|
+
return this._cloneInto();
|
249
|
+
}
|
250
|
+
};
|
251
|
+
function wrapConstructor(hashCons) {
|
252
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
253
|
+
const tmp2 = hashCons();
|
254
|
+
hashC.outputLen = tmp2.outputLen;
|
255
|
+
hashC.blockLen = tmp2.blockLen;
|
256
|
+
hashC.create = () => hashCons();
|
395
257
|
return hashC;
|
396
258
|
}
|
259
|
+
|
260
|
+
// src/sm2/sm3.ts
|
261
|
+
var BoolA = (A, B, C) => A & B | A & C | B & C;
|
262
|
+
var BoolB = (A, B, C) => A ^ B ^ C;
|
263
|
+
var BoolC = (A, B, C) => A & B | ~A & C;
|
264
|
+
function setBigUint64(view, byteOffset, value, isLE2) {
|
265
|
+
if (typeof view.setBigUint64 === "function")
|
266
|
+
return view.setBigUint64(byteOffset, value, isLE2);
|
267
|
+
const _32n = BigInt(32);
|
268
|
+
const _u32_max = BigInt(4294967295);
|
269
|
+
const wh = Number(value >> _32n & _u32_max);
|
270
|
+
const wl = Number(value & _u32_max);
|
271
|
+
const h = isLE2 ? 4 : 0;
|
272
|
+
const l = isLE2 ? 0 : 4;
|
273
|
+
view.setUint32(byteOffset + h, wh, isLE2);
|
274
|
+
view.setUint32(byteOffset + l, wl, isLE2);
|
275
|
+
}
|
276
|
+
function rotl(x2, n) {
|
277
|
+
const s = n & 31;
|
278
|
+
return x2 << s | x2 >>> 32 - s;
|
279
|
+
}
|
280
|
+
function P0(X) {
|
281
|
+
return X ^ rotl(X, 9) ^ rotl(X, 17);
|
282
|
+
}
|
283
|
+
function P1(X) {
|
284
|
+
return X ^ rotl(X, 15) ^ rotl(X, 23);
|
285
|
+
}
|
286
|
+
var SHA2 = class extends Hash {
|
287
|
+
constructor(blockLen, outputLen, padOffset, isLE2) {
|
288
|
+
super();
|
289
|
+
this.blockLen = blockLen;
|
290
|
+
this.outputLen = outputLen;
|
291
|
+
this.padOffset = padOffset;
|
292
|
+
this.isLE = isLE2;
|
293
|
+
this.buffer = new Uint8Array(blockLen);
|
294
|
+
this.view = createView(this.buffer);
|
295
|
+
}
|
296
|
+
buffer;
|
297
|
+
view;
|
298
|
+
finished = false;
|
299
|
+
length = 0;
|
300
|
+
pos = 0;
|
301
|
+
destroyed = false;
|
302
|
+
update(data) {
|
303
|
+
const { view, buffer, blockLen } = this;
|
304
|
+
data = toBytes(data);
|
305
|
+
const len = data.length;
|
306
|
+
for (let pos = 0; pos < len; ) {
|
307
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
308
|
+
if (take === blockLen) {
|
309
|
+
const dataView = createView(data);
|
310
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
311
|
+
this.process(dataView, pos);
|
312
|
+
continue;
|
313
|
+
}
|
314
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
315
|
+
this.pos += take;
|
316
|
+
pos += take;
|
317
|
+
if (this.pos === blockLen) {
|
318
|
+
this.process(view, 0);
|
319
|
+
this.pos = 0;
|
320
|
+
}
|
321
|
+
}
|
322
|
+
this.length += data.length;
|
323
|
+
this.roundClean();
|
324
|
+
return this;
|
325
|
+
}
|
326
|
+
digestInto(out) {
|
327
|
+
this.finished = true;
|
328
|
+
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
329
|
+
let { pos } = this;
|
330
|
+
buffer[pos++] = 128;
|
331
|
+
this.buffer.subarray(pos).fill(0);
|
332
|
+
if (this.padOffset > blockLen - pos) {
|
333
|
+
this.process(view, 0);
|
334
|
+
pos = 0;
|
335
|
+
}
|
336
|
+
for (let i = pos; i < blockLen; i++)
|
337
|
+
buffer[i] = 0;
|
338
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
|
339
|
+
this.process(view, 0);
|
340
|
+
const oview = createView(out);
|
341
|
+
const len = this.outputLen;
|
342
|
+
if (len % 4)
|
343
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
344
|
+
const outLen = len / 4;
|
345
|
+
const state = this.get();
|
346
|
+
if (outLen > state.length)
|
347
|
+
throw new Error("_sha2: outputLen bigger than state");
|
348
|
+
for (let i = 0; i < outLen; i++)
|
349
|
+
oview.setUint32(4 * i, state[i], isLE2);
|
350
|
+
}
|
351
|
+
digest() {
|
352
|
+
const { buffer, outputLen } = this;
|
353
|
+
this.digestInto(buffer);
|
354
|
+
const res = buffer.slice(0, outputLen);
|
355
|
+
this.destroy();
|
356
|
+
return res;
|
357
|
+
}
|
358
|
+
_cloneInto(to) {
|
359
|
+
to ||= new this.constructor();
|
360
|
+
to.set(...this.get());
|
361
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
362
|
+
to.length = length;
|
363
|
+
to.pos = pos;
|
364
|
+
to.finished = finished;
|
365
|
+
to.destroyed = destroyed;
|
366
|
+
if (length % blockLen)
|
367
|
+
to.buffer.set(buffer);
|
368
|
+
return to;
|
369
|
+
}
|
370
|
+
};
|
371
|
+
var IV = new Uint32Array([1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214]);
|
372
|
+
var SM3_W = new Uint32Array(68);
|
373
|
+
var SM3_M = new Uint32Array(64);
|
374
|
+
var T1 = 2043430169;
|
375
|
+
var T2 = 2055708042;
|
376
|
+
var SM3 = class extends SHA2 {
|
377
|
+
A = IV[0] | 0;
|
378
|
+
B = IV[1] | 0;
|
379
|
+
C = IV[2] | 0;
|
380
|
+
D = IV[3] | 0;
|
381
|
+
E = IV[4] | 0;
|
382
|
+
F = IV[5] | 0;
|
383
|
+
G = IV[6] | 0;
|
384
|
+
H = IV[7] | 0;
|
385
|
+
constructor() {
|
386
|
+
super(64, 32, 8, false);
|
387
|
+
}
|
388
|
+
get() {
|
389
|
+
const { A, B, C, D, E, F, G, H } = this;
|
390
|
+
return [A, B, C, D, E, F, G, H];
|
391
|
+
}
|
392
|
+
set(A, B, C, D, E, F, G, H) {
|
393
|
+
this.A = A | 0;
|
394
|
+
this.B = B | 0;
|
395
|
+
this.C = C | 0;
|
396
|
+
this.D = D | 0;
|
397
|
+
this.E = E | 0;
|
398
|
+
this.F = F | 0;
|
399
|
+
this.G = G | 0;
|
400
|
+
this.H = H | 0;
|
401
|
+
}
|
402
|
+
process(view, offset) {
|
403
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
404
|
+
SM3_W[i] = view.getUint32(offset, false);
|
405
|
+
for (let i = 16; i < 68; i++) {
|
406
|
+
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];
|
407
|
+
}
|
408
|
+
for (let i = 0; i < 64; i++) {
|
409
|
+
SM3_M[i] = SM3_W[i] ^ SM3_W[i + 4];
|
410
|
+
}
|
411
|
+
let { A, B, C, D, E, F, G, H } = this;
|
412
|
+
for (let j = 0; j < 64; j++) {
|
413
|
+
let small = j >= 0 && j <= 15;
|
414
|
+
let T = small ? T1 : T2;
|
415
|
+
let SS1 = rotl(rotl(A, 12) + E + rotl(T, j), 7);
|
416
|
+
let SS2 = SS1 ^ rotl(A, 12);
|
417
|
+
let TT1 = (small ? BoolB(A, B, C) : BoolA(A, B, C)) + D + SS2 + SM3_M[j] | 0;
|
418
|
+
let TT2 = (small ? BoolB(E, F, G) : BoolC(E, F, G)) + H + SS1 + SM3_W[j] | 0;
|
419
|
+
D = C;
|
420
|
+
C = rotl(B, 9);
|
421
|
+
B = A;
|
422
|
+
A = TT1;
|
423
|
+
H = G;
|
424
|
+
G = rotl(F, 19);
|
425
|
+
F = E;
|
426
|
+
E = P0(TT2);
|
427
|
+
}
|
428
|
+
A = A ^ this.A | 0;
|
429
|
+
B = B ^ this.B | 0;
|
430
|
+
C = C ^ this.C | 0;
|
431
|
+
D = D ^ this.D | 0;
|
432
|
+
E = E ^ this.E | 0;
|
433
|
+
F = F ^ this.F | 0;
|
434
|
+
G = G ^ this.G | 0;
|
435
|
+
H = H ^ this.H | 0;
|
436
|
+
this.set(A, B, C, D, E, F, G, H);
|
437
|
+
}
|
438
|
+
roundClean() {
|
439
|
+
SM3_W.fill(0);
|
440
|
+
}
|
441
|
+
destroy() {
|
442
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
443
|
+
this.buffer.fill(0);
|
444
|
+
}
|
445
|
+
};
|
446
|
+
var sm3 = wrapConstructor(() => new SM3());
|
447
|
+
|
448
|
+
// src/sm2/hmac.ts
|
449
|
+
var HMAC = class extends Hash {
|
450
|
+
oHash;
|
451
|
+
iHash;
|
452
|
+
blockLen;
|
453
|
+
outputLen;
|
454
|
+
finished = false;
|
455
|
+
destroyed = false;
|
456
|
+
constructor(hash, _key) {
|
457
|
+
super();
|
458
|
+
const key = toBytes(_key);
|
459
|
+
this.iHash = hash.create();
|
460
|
+
if (typeof this.iHash.update !== "function")
|
461
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
462
|
+
this.blockLen = this.iHash.blockLen;
|
463
|
+
this.outputLen = this.iHash.outputLen;
|
464
|
+
const blockLen = this.blockLen;
|
465
|
+
const pad = new Uint8Array(blockLen);
|
466
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
467
|
+
for (let i = 0; i < pad.length; i++)
|
468
|
+
pad[i] ^= 54;
|
469
|
+
this.iHash.update(pad);
|
470
|
+
this.oHash = hash.create();
|
471
|
+
for (let i = 0; i < pad.length; i++)
|
472
|
+
pad[i] ^= 54 ^ 92;
|
473
|
+
this.oHash.update(pad);
|
474
|
+
pad.fill(0);
|
475
|
+
}
|
476
|
+
update(buf) {
|
477
|
+
this.iHash.update(buf);
|
478
|
+
return this;
|
479
|
+
}
|
480
|
+
digestInto(out) {
|
481
|
+
this.finished = true;
|
482
|
+
this.iHash.digestInto(out);
|
483
|
+
this.oHash.update(out);
|
484
|
+
this.oHash.digestInto(out);
|
485
|
+
this.destroy();
|
486
|
+
}
|
487
|
+
digest() {
|
488
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
489
|
+
this.digestInto(out);
|
490
|
+
return out;
|
491
|
+
}
|
492
|
+
_cloneInto(to) {
|
493
|
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
494
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
495
|
+
to = to;
|
496
|
+
to.finished = finished;
|
497
|
+
to.destroyed = destroyed;
|
498
|
+
to.blockLen = blockLen;
|
499
|
+
to.outputLen = outputLen;
|
500
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
501
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
502
|
+
return to;
|
503
|
+
}
|
504
|
+
destroy() {
|
505
|
+
this.destroyed = true;
|
506
|
+
this.oHash.destroy();
|
507
|
+
this.iHash.destroy();
|
508
|
+
}
|
509
|
+
};
|
510
|
+
var hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
511
|
+
hmac.create = (hash, key) => new HMAC(hash, key);
|
512
|
+
|
513
|
+
// src/sm2/ec.ts
|
514
|
+
import { concatBytes } from "@noble/curves/abstract/utils";
|
397
515
|
var sm2Fp = Field(BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991999"));
|
398
516
|
var sm2Curve = weierstrass({
|
399
517
|
a: BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991996"),
|
@@ -403,18 +521,19 @@ var sm2Curve = weierstrass({
|
|
403
521
|
n: BigInt("115792089210356248756420345214020892766061623724957744567843809356293439045923"),
|
404
522
|
Gx: BigInt("22963146547237050559479531362550074578802567295341616970375194840604139615431"),
|
405
523
|
Gy: BigInt("85132369209828568825618990617112496413088388631904505083283536607588877201568"),
|
406
|
-
hash:
|
407
|
-
hmac: (key, ...msgs) => hmac(
|
524
|
+
hash: sm3,
|
525
|
+
hmac: (key, ...msgs) => hmac(sm3, key, concatBytes(...msgs)),
|
408
526
|
randomBytes
|
409
527
|
});
|
528
|
+
var field = Field(BigInt(sm2Curve.CURVE.n));
|
410
529
|
|
411
530
|
// src/sm2/utils.ts
|
412
531
|
import { mod } from "@noble/curves/abstract/modular";
|
413
532
|
function generateKeyPairHex(str) {
|
414
533
|
const privateKey = str ? utils2.numberToBytesBE(mod(BigInt(str), ONE) + ONE, 32) : sm2Curve.utils.randomPrivateKey();
|
415
534
|
const publicKey = sm2Curve.getPublicKey(privateKey, false);
|
416
|
-
const privPad =
|
417
|
-
const pubPad =
|
535
|
+
const privPad = leftPad(utils2.bytesToHex(privateKey), 64);
|
536
|
+
const pubPad = leftPad(utils2.bytesToHex(publicKey), 64);
|
418
537
|
return { privateKey: privPad, publicKey: pubPad };
|
419
538
|
}
|
420
539
|
function compressPublicKeyHex(s) {
|
@@ -443,7 +562,7 @@ function utf8ToHex(input) {
|
|
443
562
|
}
|
444
563
|
return hexChars.join("");
|
445
564
|
}
|
446
|
-
function
|
565
|
+
function leftPad(input, num) {
|
447
566
|
if (input.length >= num)
|
448
567
|
return input;
|
449
568
|
return new Array(num - input.length + 1).join("0") + input;
|
@@ -475,7 +594,7 @@ function arrayToUtf8(arr) {
|
|
475
594
|
function hexToArray(hexStr) {
|
476
595
|
let hexStrLength = hexStr.length;
|
477
596
|
if (hexStrLength % 2 !== 0) {
|
478
|
-
hexStr =
|
597
|
+
hexStr = leftPad(hexStr, hexStrLength + 1);
|
479
598
|
}
|
480
599
|
hexStrLength = hexStr.length;
|
481
600
|
const wordLength = hexStrLength / 2;
|
@@ -489,9 +608,9 @@ function verifyPublicKey(publicKey) {
|
|
489
608
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
490
609
|
if (!point)
|
491
610
|
return false;
|
492
|
-
const
|
611
|
+
const x2 = point.x;
|
493
612
|
const y = point.y;
|
494
|
-
return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.
|
613
|
+
return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.addN(sm2Fp.mulN(x2, sm2Fp.sqrN(x2)), sm2Fp.mulN(x2, sm2Curve.CURVE.a)), sm2Curve.CURVE.b);
|
495
614
|
}
|
496
615
|
function comparePublicKeyHex(publicKey1, publicKey2) {
|
497
616
|
const point1 = sm2Curve.ProjectivePoint.fromHex(publicKey1);
|
@@ -502,36 +621,26 @@ function comparePublicKeyHex(publicKey1, publicKey2) {
|
|
502
621
|
return false;
|
503
622
|
return point1.equals(point2);
|
504
623
|
}
|
505
|
-
function concatArray(...arrays) {
|
506
|
-
let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
|
507
|
-
if (!arrays.length)
|
508
|
-
return new Uint8Array();
|
509
|
-
let result = new Uint8Array(totalLength);
|
510
|
-
let length = 0;
|
511
|
-
for (let array of arrays) {
|
512
|
-
result.set(array, length);
|
513
|
-
length += array.length;
|
514
|
-
}
|
515
|
-
return result;
|
516
|
-
}
|
517
624
|
|
518
625
|
// src/sm2/index.ts
|
519
|
-
import * as mod2 from "@noble/curves/abstract/modular";
|
520
626
|
import * as utils4 from "@noble/curves/abstract/utils";
|
521
627
|
|
522
628
|
// src/sm2/kx.ts
|
523
629
|
import * as utils3 from "@noble/curves/abstract/utils";
|
524
|
-
import { Field as Field2 } from "@noble/curves/abstract/modular";
|
525
|
-
var field = Field2(BigInt(sm2Curve.CURVE.n));
|
526
630
|
var wPow2 = utils3.hexToNumber("80000000000000000000000000000000");
|
527
631
|
var wPow2Sub1 = utils3.hexToNumber("7fffffffffffffffffffffffffffffff");
|
528
632
|
function hkdf(z, keylen) {
|
529
|
-
let t = new Uint8Array();
|
530
633
|
let msg = new Uint8Array(keylen);
|
531
634
|
let ct = 1;
|
532
635
|
let offset = 0;
|
636
|
+
let t = EmptyArray;
|
637
|
+
const ctShift = new Uint8Array(4);
|
533
638
|
const nextT = () => {
|
534
|
-
|
639
|
+
ctShift[0] = ct >> 24 & 255;
|
640
|
+
ctShift[1] = ct >> 16 & 255;
|
641
|
+
ctShift[2] = ct >> 8 & 255;
|
642
|
+
ctShift[3] = ct & 255;
|
643
|
+
t = sm3(utils3.concatBytes(z, ctShift));
|
535
644
|
ct++;
|
536
645
|
offset = 0;
|
537
646
|
};
|
@@ -543,28 +652,32 @@ function hkdf(z, keylen) {
|
|
543
652
|
}
|
544
653
|
return msg;
|
545
654
|
}
|
546
|
-
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, idA = "1234567812345678", idB = "1234567812345678"
|
655
|
+
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, sharedKeyLength, isRecipient = false, idA = "1234567812345678", idB = "1234567812345678") {
|
547
656
|
const RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey);
|
548
657
|
const RB = sm2Curve.ProjectivePoint.fromHex(ephemeralPublicKeyB);
|
549
658
|
const PB = sm2Curve.ProjectivePoint.fromHex(publicKeyB);
|
550
|
-
|
551
|
-
|
659
|
+
let ZA = getZ(keypairA.publicKey, idA);
|
660
|
+
let ZB = getZ(publicKeyB, idB);
|
661
|
+
if (isRecipient) {
|
662
|
+
[ZA, ZB] = [ZB, ZA];
|
663
|
+
}
|
552
664
|
const rA = utils3.hexToNumber(ephemeralKeypairA.privateKey);
|
553
665
|
const dA = utils3.hexToNumber(keypairA.privateKey);
|
554
666
|
const x1 = RA.x;
|
555
|
-
const x1_ =
|
556
|
-
const tA = field.add(dA, field.
|
667
|
+
const x1_ = wPow2 + (x1 & wPow2Sub1);
|
668
|
+
const tA = field.add(dA, field.mulN(x1_, rA));
|
557
669
|
const x2 = RB.x;
|
558
670
|
const x2_ = field.add(wPow2, x2 & wPow2Sub1);
|
559
671
|
const U = RB.multiply(x2_).add(PB).multiply(tA);
|
560
|
-
const xU = hexToArray(
|
561
|
-
const yU = hexToArray(
|
562
|
-
const KA = hkdf(
|
672
|
+
const xU = hexToArray(leftPad(utils3.numberToHexUnpadded(U.x), 64));
|
673
|
+
const yU = hexToArray(leftPad(utils3.numberToHexUnpadded(U.y), 64));
|
674
|
+
const KA = hkdf(utils3.concatBytes(xU, yU, ZA, ZB), sharedKeyLength);
|
563
675
|
return KA;
|
564
676
|
}
|
565
677
|
|
566
678
|
// src/sm2/index.ts
|
567
679
|
var C1C2C3 = 0;
|
680
|
+
var EmptyArray = new Uint8Array();
|
568
681
|
function doEncrypt(msg, publicKey, cipherMode = 1) {
|
569
682
|
const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
|
570
683
|
const publicKeyPoint = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
@@ -574,26 +687,33 @@ function doEncrypt(msg, publicKey, cipherMode = 1) {
|
|
574
687
|
if (c1.length > 128)
|
575
688
|
c1 = c1.substring(c1.length - 128);
|
576
689
|
const p = publicKeyPoint.multiply(k);
|
577
|
-
const x2 = hexToArray(
|
578
|
-
const y2 = hexToArray(
|
579
|
-
const c3 =
|
690
|
+
const x2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.x), 64));
|
691
|
+
const y2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.y), 64));
|
692
|
+
const c3 = bytesToHex(sm3(utils4.concatBytes(x2, msgArr, y2)));
|
693
|
+
xorCipherStream(x2, y2, msgArr);
|
694
|
+
const c2 = bytesToHex(msgArr);
|
695
|
+
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
696
|
+
}
|
697
|
+
function xorCipherStream(x2, y2, msg) {
|
580
698
|
let ct = 1;
|
581
699
|
let offset = 0;
|
582
|
-
let t =
|
583
|
-
const
|
700
|
+
let t = EmptyArray;
|
701
|
+
const ctShift = new Uint8Array(4);
|
584
702
|
const nextT = () => {
|
585
|
-
|
703
|
+
ctShift[0] = ct >> 24 & 255;
|
704
|
+
ctShift[1] = ct >> 16 & 255;
|
705
|
+
ctShift[2] = ct >> 8 & 255;
|
706
|
+
ctShift[3] = ct & 255;
|
707
|
+
t = sm3(utils4.concatBytes(x2, y2, ctShift));
|
586
708
|
ct++;
|
587
709
|
offset = 0;
|
588
710
|
};
|
589
711
|
nextT();
|
590
|
-
for (let i = 0, len =
|
712
|
+
for (let i = 0, len = msg.length; i < len; i++) {
|
591
713
|
if (offset === t.length)
|
592
714
|
nextT();
|
593
|
-
|
715
|
+
msg[i] ^= t[offset++] & 255;
|
594
716
|
}
|
595
|
-
const c2 = arrayToHex(Array.from(msgArr));
|
596
|
-
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
597
717
|
}
|
598
718
|
function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
599
719
|
output = "string"
|
@@ -608,24 +728,10 @@ function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
|
608
728
|
const msg = hexToArray(c2);
|
609
729
|
const c1 = sm2Curve.ProjectivePoint.fromHex("04" + encryptData.substring(0, 128));
|
610
730
|
const p = c1.multiply(privateKeyInteger);
|
611
|
-
const x2 = hexToArray(
|
612
|
-
const y2 = hexToArray(
|
613
|
-
|
614
|
-
|
615
|
-
let t = new Uint8Array();
|
616
|
-
const z = concatArray(x2, y2);
|
617
|
-
const nextT = () => {
|
618
|
-
t = sm3(Uint8Array.from([...z, ct >> 24 & 255, ct >> 16 & 255, ct >> 8 & 255, ct & 255]));
|
619
|
-
ct++;
|
620
|
-
offset = 0;
|
621
|
-
};
|
622
|
-
nextT();
|
623
|
-
for (let i = 0, len = msg.length; i < len; i++) {
|
624
|
-
if (offset === t.length)
|
625
|
-
nextT();
|
626
|
-
msg[i] ^= t[offset++] & 255;
|
627
|
-
}
|
628
|
-
const checkC3 = arrayToHex(Array.from(sm3(concatArray(x2, msg, y2))));
|
731
|
+
const x2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.x), 64));
|
732
|
+
const y2 = hexToArray(leftPad(utils4.numberToHexUnpadded(p.y), 64));
|
733
|
+
xorCipherStream(x2, y2, msg);
|
734
|
+
const checkC3 = arrayToHex(Array.from(sm3(utils4.concatBytes(x2, msg, y2))));
|
629
735
|
if (checkC3 === c3.toLowerCase()) {
|
630
736
|
return output === "array" ? msg : arrayToUtf8(msg);
|
631
737
|
} else {
|
@@ -659,13 +765,13 @@ function doSignature(msg, privateKey, options = {}) {
|
|
659
765
|
point = getPoint();
|
660
766
|
}
|
661
767
|
k = point.k;
|
662
|
-
r =
|
768
|
+
r = field.add(e, point.x1);
|
663
769
|
} while (r === ZERO || r + k === sm2Curve.CURVE.n);
|
664
|
-
s =
|
770
|
+
s = field.mul(field.inv(field.addN(dA, ONE)), field.subN(k, field.mulN(r, dA)));
|
665
771
|
} while (s === ZERO);
|
666
772
|
if (der)
|
667
773
|
return encodeDer(r, s);
|
668
|
-
return
|
774
|
+
return leftPad(utils4.numberToHexUnpadded(r), 64) + leftPad(utils4.numberToHexUnpadded(s), 64);
|
669
775
|
}
|
670
776
|
function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
671
777
|
let hashHex;
|
@@ -691,19 +797,19 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
691
797
|
}
|
692
798
|
const PA = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
693
799
|
const e = utils4.hexToNumber(hashHex);
|
694
|
-
const t =
|
800
|
+
const t = field.add(r, s);
|
695
801
|
if (t === ZERO)
|
696
802
|
return false;
|
697
803
|
const x1y1 = sm2Curve.ProjectivePoint.BASE.multiply(s).add(PA.multiply(t));
|
698
|
-
const R =
|
804
|
+
const R = field.add(e, x1y1.x);
|
699
805
|
return r === R;
|
700
806
|
}
|
701
|
-
function
|
807
|
+
function getZ(publicKey, userId = "1234567812345678") {
|
702
808
|
userId = utf8ToHex(userId);
|
703
|
-
const a =
|
704
|
-
const b =
|
705
|
-
const gx =
|
706
|
-
const gy =
|
809
|
+
const a = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.a), 64);
|
810
|
+
const b = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.b), 64);
|
811
|
+
const gx = leftPad(utils4.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.x), 64);
|
812
|
+
const gy = leftPad(utils4.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.y), 64);
|
707
813
|
let px;
|
708
814
|
let py;
|
709
815
|
if (publicKey.length === 128) {
|
@@ -711,17 +817,21 @@ function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
|
711
817
|
py = publicKey.substring(64, 128);
|
712
818
|
} else {
|
713
819
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
714
|
-
px =
|
715
|
-
py =
|
820
|
+
px = leftPad(utils4.numberToHexUnpadded(point.x), 64);
|
821
|
+
py = leftPad(utils4.numberToHexUnpadded(point.y), 64);
|
716
822
|
}
|
717
823
|
const data = hexToArray(userId + a + b + gx + gy + px + py);
|
718
824
|
const entl = userId.length * 4;
|
719
|
-
const z = sm3(
|
720
|
-
return
|
825
|
+
const z = sm3(utils4.concatBytes(new Uint8Array([entl >> 8 & 255, entl & 255]), data));
|
826
|
+
return z;
|
827
|
+
}
|
828
|
+
function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
829
|
+
const z = getZ(publicKey, userId);
|
830
|
+
return bytesToHex(sm3(utils4.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
|
721
831
|
}
|
722
832
|
function getPublicKeyFromPrivateKey(privateKey) {
|
723
833
|
const pubKey = sm2Curve.getPublicKey(privateKey, false);
|
724
|
-
const pubPad =
|
834
|
+
const pubPad = leftPad(utils4.bytesToHex(pubKey), 64);
|
725
835
|
return pubPad;
|
726
836
|
}
|
727
837
|
function getPoint() {
|
@@ -735,6 +845,48 @@ function getPoint() {
|
|
735
845
|
};
|
736
846
|
}
|
737
847
|
|
848
|
+
// src/sm3/index.ts
|
849
|
+
function utf8ToArray(str) {
|
850
|
+
const arr = [];
|
851
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
852
|
+
const point = str.codePointAt(i);
|
853
|
+
if (point <= 127) {
|
854
|
+
arr.push(point);
|
855
|
+
} else if (point <= 2047) {
|
856
|
+
arr.push(192 | point >>> 6);
|
857
|
+
arr.push(128 | point & 63);
|
858
|
+
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
859
|
+
arr.push(224 | point >>> 12);
|
860
|
+
arr.push(128 | point >>> 6 & 63);
|
861
|
+
arr.push(128 | point & 63);
|
862
|
+
} else if (point >= 65536 && point <= 1114111) {
|
863
|
+
i++;
|
864
|
+
arr.push(240 | point >>> 18 & 28);
|
865
|
+
arr.push(128 | point >>> 12 & 63);
|
866
|
+
arr.push(128 | point >>> 6 & 63);
|
867
|
+
arr.push(128 | point & 63);
|
868
|
+
} else {
|
869
|
+
arr.push(point);
|
870
|
+
throw new Error("input is not supported");
|
871
|
+
}
|
872
|
+
}
|
873
|
+
return new Uint8Array(arr);
|
874
|
+
}
|
875
|
+
function sm32(input, options) {
|
876
|
+
input = typeof input === "string" ? utf8ToArray(input) : input;
|
877
|
+
if (options) {
|
878
|
+
const mode = options.mode || "hmac";
|
879
|
+
if (mode !== "hmac")
|
880
|
+
throw new Error("invalid mode");
|
881
|
+
let key = options.key;
|
882
|
+
if (!key)
|
883
|
+
throw new Error("invalid key");
|
884
|
+
key = typeof key === "string" ? hexToArray(key) : key;
|
885
|
+
return bytesToHex(hmac(sm3, key, input));
|
886
|
+
}
|
887
|
+
return bytesToHex(sm3(input));
|
888
|
+
}
|
889
|
+
|
738
890
|
// src/sm4/index.ts
|
739
891
|
var sm4_exports = {};
|
740
892
|
__export(sm4_exports, {
|
@@ -1046,9 +1198,9 @@ function l1(b) {
|
|
1046
1198
|
function l2(b) {
|
1047
1199
|
return b ^ rotl(b, 13) ^ rotl(b, 23);
|
1048
1200
|
}
|
1201
|
+
var x = new Uint32Array(4);
|
1202
|
+
var tmp = new Uint32Array(4);
|
1049
1203
|
function sms4Crypt(input, output, roundKey) {
|
1050
|
-
const x = new Uint32Array(4);
|
1051
|
-
const tmp = new Uint32Array(4);
|
1052
1204
|
for (let i = 0; i < 4; i++) {
|
1053
1205
|
tmp[0] = input[4 * i] & 255;
|
1054
1206
|
tmp[1] = input[4 * i + 1] & 255;
|
@@ -1074,8 +1226,6 @@ function sms4Crypt(input, output, roundKey) {
|
|
1074
1226
|
}
|
1075
1227
|
}
|
1076
1228
|
function sms4KeyExt(key, roundKey, cryptFlag) {
|
1077
|
-
const x = new Uint32Array(4);
|
1078
|
-
const tmp = new Uint32Array(4);
|
1079
1229
|
for (let i = 0; i < 4; i++) {
|
1080
1230
|
tmp[0] = key[0 + 4 * i] & 255;
|
1081
1231
|
tmp[1] = key[1 + 4 * i] & 255;
|
@@ -1105,6 +1255,7 @@ function sms4KeyExt(key, roundKey, cryptFlag) {
|
|
1105
1255
|
}
|
1106
1256
|
}
|
1107
1257
|
}
|
1258
|
+
var blockOutput = new Uint8Array(16);
|
1108
1259
|
function sm4(inArray, key, cryptFlag, options = {}) {
|
1109
1260
|
let {
|
1110
1261
|
padding = "pkcs#7",
|
@@ -1148,8 +1299,7 @@ function sm4(inArray, key, cryptFlag, options = {}) {
|
|
1148
1299
|
let restLen = inArray.length;
|
1149
1300
|
let point = 0;
|
1150
1301
|
while (restLen >= BLOCK) {
|
1151
|
-
const input = inArray.
|
1152
|
-
const output2 = new Uint8Array(16);
|
1302
|
+
const input = inArray.subarray(point, point + 16);
|
1153
1303
|
if (mode === "cbc") {
|
1154
1304
|
for (let i = 0; i < BLOCK; i++) {
|
1155
1305
|
if (cryptFlag !== DECRYPT) {
|
@@ -1157,18 +1307,18 @@ function sm4(inArray, key, cryptFlag, options = {}) {
|
|
1157
1307
|
}
|
1158
1308
|
}
|
1159
1309
|
}
|
1160
|
-
sms4Crypt(input,
|
1310
|
+
sms4Crypt(input, blockOutput, roundKey);
|
1161
1311
|
for (let i = 0; i < BLOCK; i++) {
|
1162
1312
|
if (mode === "cbc") {
|
1163
1313
|
if (cryptFlag === DECRYPT) {
|
1164
|
-
|
1314
|
+
blockOutput[i] ^= lastVector[i];
|
1165
1315
|
}
|
1166
1316
|
}
|
1167
|
-
outArray[point + i] =
|
1317
|
+
outArray[point + i] = blockOutput[i];
|
1168
1318
|
}
|
1169
1319
|
if (mode === "cbc") {
|
1170
1320
|
if (cryptFlag !== DECRYPT) {
|
1171
|
-
lastVector =
|
1321
|
+
lastVector = blockOutput;
|
1172
1322
|
} else {
|
1173
1323
|
lastVector = input;
|
1174
1324
|
}
|
@@ -1187,7 +1337,7 @@ function sm4(inArray, key, cryptFlag, options = {}) {
|
|
1187
1337
|
}
|
1188
1338
|
if (output !== "array") {
|
1189
1339
|
if (cryptFlag !== DECRYPT) {
|
1190
|
-
return
|
1340
|
+
return bytesToHex(outArray);
|
1191
1341
|
} else {
|
1192
1342
|
return arrayToUtf8(outArray);
|
1193
1343
|
}
|
@@ -1203,6 +1353,7 @@ function decrypt(inArray, key, options = {}) {
|
|
1203
1353
|
}
|
1204
1354
|
export {
|
1205
1355
|
sm2_exports as sm2,
|
1206
|
-
|
1356
|
+
sm32 as sm3,
|
1207
1357
|
sm4_exports as sm4
|
1208
1358
|
};
|
1359
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|