sm-crypto-v2 1.9.3 → 1.10.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 CHANGED
@@ -2,6 +2,21 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.10.1](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.10.0...v1.10.1) (2025-04-27)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * miniprogram build issue ([5e8ece4](https://github.com/Cubelrti/sm-crypto-v2/commit/5e8ece41dab69fc449675f9d46dfd25163538a78))
11
+ * **sm2:** typing issue ([f368a72](https://github.com/Cubelrti/sm-crypto-v2/commit/f368a72cc354d0ec9601dda8cbd2590eb9e6c144))
12
+
13
+ ## [1.10.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.9.3...v1.10.0) (2025-04-14)
14
+
15
+
16
+ ### Features
17
+
18
+ * **sm4:** support gcm mode for `sm4` ([2c6a0df](https://github.com/Cubelrti/sm-crypto-v2/commit/2c6a0df8d591d802fd4816c6b1824e20e0b23e31))
19
+
5
20
  ### [1.9.3](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.9.2...v1.9.3) (2024-10-10)
6
21
 
7
22
 
package/README.md CHANGED
@@ -19,6 +19,7 @@ For WebAssembly-supported platform, see [sm-crypto-wasm](https://github.com/Cube
19
19
  - 🎲 自动选择最优的安全随机数实现,避免使用 `Math.random()` 和 `Date.now()` 进行模拟
20
20
  - 📚 同时导出 ES Module 和 CommonJS 两种格式,可按需使用
21
21
  - 🔑 提供 SM2 密钥交换 API
22
+ - 🔒 提供 SM4 GCM 模式加密解密能力
22
23
  - 🎒 未压缩大小 34kb,压缩后 17kb
23
24
 
24
25
  ## 安装
@@ -171,6 +172,14 @@ let encryptData = sm4.encrypt(msg, key) // 加密,默认输出 16 进制字符
171
172
  let encryptData = sm4.encrypt(msg, key, {padding: 'none'}) // 加密,不使用 padding
172
173
  let encryptData = sm4.encrypt(msg, key, {padding: 'none', output: 'array'}) // 加密,不使用 padding,输出为字节数组
173
174
  let encryptData = sm4.encrypt(msg, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 加密,cbc 模式
175
+ let encryptData = sm4.encrypt(msg, key, {
176
+ mode: 'gcm', // gcm 模式,必填 iv, 可选 aad
177
+ iv,
178
+ associatedData,
179
+ output: 'string',
180
+ outputTag: true, // 输出 aead tag,如果为 false 则不输出
181
+ }) // 输出格式 { output: T; tag?: T; } T 为 string/Uint8Array
182
+
174
183
  ```
175
184
 
176
185
  ### 解密
@@ -184,6 +193,13 @@ let decryptData = sm4.decrypt(encryptData, key) // 解密,默认输出 utf8
184
193
  let decryptData = sm4.decrypt(encryptData, key, {padding: 'none'}) // 解密,不使用 padding
185
194
  let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'}) // 解密,不使用 padding,输出为字节数组
186
195
  let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式
196
+ let decryptData = sm4.decrypt(encryptData, key, {
197
+ mode: 'gcm', // gcm 模式必填 iv tag, 可选 aad
198
+ iv,
199
+ associatedData,
200
+ tag: expectedAuthTag,
201
+ output: 'array'
202
+ }) // 输出格式 string/Uint8Array
187
203
  ```
188
204
 
189
205
  ### 密钥交换
package/dist/index.d.mts CHANGED
@@ -60,7 +60,7 @@ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?:
60
60
  asn1?: boolean;
61
61
  }): Uint8Array;
62
62
  declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
63
- output: 'string';
63
+ output?: 'string';
64
64
  asn1?: boolean;
65
65
  }): string;
66
66
  interface SignaturePoint {
@@ -146,30 +146,52 @@ declare function sm3(input: string | Uint8Array, options?: {
146
146
 
147
147
  interface SM4Options {
148
148
  padding?: 'pkcs#7' | 'pkcs#5' | 'none' | null;
149
- mode?: 'cbc' | 'ecb';
149
+ mode?: 'cbc' | 'ecb' | 'gcm';
150
150
  iv?: Uint8Array | string;
151
151
  output?: 'string' | 'array';
152
+ associatedData?: Uint8Array | string;
153
+ outputTag?: boolean;
154
+ tag?: Uint8Array | string;
152
155
  }
153
- declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array;
156
+ declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array | {
157
+ output: Uint8Array;
158
+ tag?: Uint8Array | undefined;
159
+ } | {
160
+ output: string;
161
+ tag: string | undefined;
162
+ };
163
+ interface GCMResult<T = Uint8Array | string> {
164
+ output: T;
165
+ tag?: T;
166
+ }
167
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options: {
168
+ mode: 'gcm';
169
+ output: 'array';
170
+ } & SM4Options): GCMResult<Uint8Array>;
171
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options: {
172
+ mode: 'gcm';
173
+ output?: 'string';
174
+ } & SM4Options): GCMResult<string>;
154
175
  declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
155
176
  output: 'array';
156
177
  } & SM4Options): Uint8Array;
157
178
  declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
158
- output: 'string';
179
+ output?: 'string';
159
180
  } & SM4Options): string;
160
181
  declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
161
182
  output: 'array';
162
183
  } & SM4Options): Uint8Array;
163
184
  declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
164
- output: 'string';
185
+ output?: 'string';
165
186
  } & SM4Options): string;
166
187
 
188
+ type index_GCMResult<T = Uint8Array | string> = GCMResult<T>;
167
189
  type index_SM4Options = SM4Options;
168
190
  declare const index_decrypt: typeof decrypt;
169
191
  declare const index_encrypt: typeof encrypt;
170
192
  declare const index_sm4: typeof sm4;
171
193
  declare namespace index {
172
- export { type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
194
+ export { type index_GCMResult as GCMResult, type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
173
195
  }
174
196
 
175
197
  export { index$1 as sm2, sm3, index as sm4 };
package/dist/index.d.ts CHANGED
@@ -60,7 +60,7 @@ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?:
60
60
  asn1?: boolean;
61
61
  }): Uint8Array;
62
62
  declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
63
- output: 'string';
63
+ output?: 'string';
64
64
  asn1?: boolean;
65
65
  }): string;
66
66
  interface SignaturePoint {
@@ -146,30 +146,52 @@ declare function sm3(input: string | Uint8Array, options?: {
146
146
 
147
147
  interface SM4Options {
148
148
  padding?: 'pkcs#7' | 'pkcs#5' | 'none' | null;
149
- mode?: 'cbc' | 'ecb';
149
+ mode?: 'cbc' | 'ecb' | 'gcm';
150
150
  iv?: Uint8Array | string;
151
151
  output?: 'string' | 'array';
152
+ associatedData?: Uint8Array | string;
153
+ outputTag?: boolean;
154
+ tag?: Uint8Array | string;
152
155
  }
153
- declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array;
156
+ declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array | {
157
+ output: Uint8Array;
158
+ tag?: Uint8Array | undefined;
159
+ } | {
160
+ output: string;
161
+ tag: string | undefined;
162
+ };
163
+ interface GCMResult<T = Uint8Array | string> {
164
+ output: T;
165
+ tag?: T;
166
+ }
167
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options: {
168
+ mode: 'gcm';
169
+ output: 'array';
170
+ } & SM4Options): GCMResult<Uint8Array>;
171
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options: {
172
+ mode: 'gcm';
173
+ output?: 'string';
174
+ } & SM4Options): GCMResult<string>;
154
175
  declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
155
176
  output: 'array';
156
177
  } & SM4Options): Uint8Array;
157
178
  declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
158
- output: 'string';
179
+ output?: 'string';
159
180
  } & SM4Options): string;
160
181
  declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
161
182
  output: 'array';
162
183
  } & SM4Options): Uint8Array;
163
184
  declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
164
- output: 'string';
185
+ output?: 'string';
165
186
  } & SM4Options): string;
166
187
 
188
+ type index_GCMResult<T = Uint8Array | string> = GCMResult<T>;
167
189
  type index_SM4Options = SM4Options;
168
190
  declare const index_decrypt: typeof decrypt;
169
191
  declare const index_encrypt: typeof encrypt;
170
192
  declare const index_sm4: typeof sm4;
171
193
  declare namespace index {
172
- export { type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
194
+ export { type index_GCMResult as GCMResult, type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
173
195
  }
174
196
 
175
197
  export { index$1 as sm2, sm3, index as sm4 };
package/dist/index.js CHANGED
@@ -800,10 +800,8 @@ function xorCipherStream(x2, y2, msg) {
800
800
  msg[i] ^= t[offset++] & 255;
801
801
  }
802
802
  }
803
- function doDecrypt(encryptData, privateKey, cipherMode = 1, {
804
- output = "string",
805
- asn1 = false
806
- } = {}) {
803
+ function doDecrypt(encryptData, privateKey, cipherMode = 1, options) {
804
+ const { output = "string", asn1 = false } = options || {};
807
805
  const privateKeyInteger = utils4.hexToNumber(privateKey);
808
806
  let c1;
809
807
  let c2;
@@ -998,6 +996,8 @@ __export(sm4_exports, {
998
996
  encrypt: () => encrypt,
999
997
  sm4: () => sm4
1000
998
  });
999
+ var import_polyval = require("@noble/ciphers/_polyval");
1000
+ var import_utils11 = require("@noble/ciphers/utils");
1001
1001
  var DECRYPT = 0;
1002
1002
  var ROUND = 32;
1003
1003
  var BLOCK = 16;
@@ -1385,14 +1385,148 @@ function sms4KeyExt(key, roundKey, cryptFlag) {
1385
1385
  }
1386
1386
  }
1387
1387
  }
1388
+ function incrementCounter(counter) {
1389
+ for (let i = counter.length - 1; i >= 0; i--) {
1390
+ counter[i]++;
1391
+ if (counter[i] !== 0)
1392
+ break;
1393
+ }
1394
+ }
1395
+ function sm4Gcm(inArray, key, ivArray, aadArray, cryptFlag, tagArray) {
1396
+ const tagLength = 16;
1397
+ function deriveKeys() {
1398
+ const roundKey2 = new Uint32Array(ROUND);
1399
+ sms4KeyExt(key, roundKey2, 1);
1400
+ const authKey = new Uint8Array(16).fill(0);
1401
+ const h2 = new Uint8Array(16);
1402
+ sms4Crypt(authKey, h2, roundKey2);
1403
+ let j02;
1404
+ if (ivArray.length === 12) {
1405
+ j02 = new Uint8Array(16);
1406
+ j02.set(ivArray, 0);
1407
+ j02[15] = 1;
1408
+ } else {
1409
+ const g = import_polyval.ghash.create(h2);
1410
+ g.update(ivArray);
1411
+ const lenIv = new Uint8Array(16);
1412
+ const view = (0, import_utils11.createView)(lenIv);
1413
+ (0, import_utils11.setBigUint64)(view, 8, BigInt(ivArray.length * 8), false);
1414
+ g.update(lenIv);
1415
+ j02 = g.digest();
1416
+ }
1417
+ const counter2 = new Uint8Array(j02);
1418
+ incrementCounter(counter2);
1419
+ const tagMask2 = new Uint8Array(16);
1420
+ sms4Crypt(j02, tagMask2, roundKey2);
1421
+ return { roundKey: roundKey2, h: h2, j0: j02, counter: counter2, tagMask: tagMask2 };
1422
+ }
1423
+ function computeTag(h2, data) {
1424
+ const aadLength = aadArray.length;
1425
+ const dataLength = data.length;
1426
+ const g = import_polyval.ghash.create(h2);
1427
+ if (aadLength > 0) {
1428
+ g.update(aadArray);
1429
+ }
1430
+ g.update(data);
1431
+ const lenBlock = new Uint8Array(16);
1432
+ const view = (0, import_utils11.createView)(lenBlock);
1433
+ (0, import_utils11.setBigUint64)(view, 0, BigInt(aadLength * 8), false);
1434
+ (0, import_utils11.setBigUint64)(view, 8, BigInt(dataLength * 8), false);
1435
+ g.update(lenBlock);
1436
+ return g.digest();
1437
+ }
1438
+ const { roundKey, h, j0, counter, tagMask } = deriveKeys();
1439
+ if (cryptFlag === DECRYPT && tagArray) {
1440
+ const calculatedTag = computeTag(h, inArray);
1441
+ for (let i = 0; i < 16; i++) {
1442
+ calculatedTag[i] ^= tagMask[i];
1443
+ }
1444
+ let tagMatch = 0;
1445
+ for (let i = 0; i < 16; i++) {
1446
+ tagMatch |= calculatedTag[i] ^ tagArray[i];
1447
+ }
1448
+ if (tagMatch !== 0) {
1449
+ throw new Error("authentication tag mismatch");
1450
+ }
1451
+ }
1452
+ const outArray = new Uint8Array(inArray.length);
1453
+ let point = 0;
1454
+ let restLen = inArray.length;
1455
+ while (restLen >= BLOCK) {
1456
+ const blockOut = new Uint8Array(BLOCK);
1457
+ sms4Crypt(counter, blockOut, roundKey);
1458
+ for (let i = 0; i < BLOCK && i < restLen; i++) {
1459
+ outArray[point + i] = inArray[point + i] ^ blockOut[i];
1460
+ }
1461
+ incrementCounter(counter);
1462
+ point += BLOCK;
1463
+ restLen -= BLOCK;
1464
+ }
1465
+ if (restLen > 0) {
1466
+ const blockOut = new Uint8Array(BLOCK);
1467
+ sms4Crypt(counter, blockOut, roundKey);
1468
+ for (let i = 0; i < restLen; i++) {
1469
+ outArray[point + i] = inArray[point + i] ^ blockOut[i];
1470
+ }
1471
+ }
1472
+ if (cryptFlag !== DECRYPT) {
1473
+ const calculatedTag = computeTag(h, outArray);
1474
+ for (let i = 0; i < 16; i++) {
1475
+ calculatedTag[i] ^= tagMask[i];
1476
+ }
1477
+ return { output: outArray, tag: calculatedTag };
1478
+ }
1479
+ return { output: outArray };
1480
+ }
1388
1481
  var blockOutput = new Uint8Array(16);
1389
1482
  function sm4(inArray, key, cryptFlag, options = {}) {
1390
1483
  let {
1391
1484
  padding = "pkcs#7",
1392
1485
  mode,
1393
1486
  iv = new Uint8Array(16),
1394
- output
1487
+ output,
1488
+ associatedData,
1489
+ outputTag,
1490
+ tag
1395
1491
  } = options;
1492
+ if (mode === "gcm") {
1493
+ const keyArray = typeof key === "string" ? hexToArray(key) : Uint8Array.from(key);
1494
+ const ivArray = typeof iv === "string" ? hexToArray(iv) : Uint8Array.from(iv);
1495
+ const aadArray = associatedData ? typeof associatedData === "string" ? hexToArray(associatedData) : Uint8Array.from(associatedData) : new Uint8Array(0);
1496
+ let inputArray;
1497
+ if (typeof inArray === "string") {
1498
+ if (cryptFlag !== DECRYPT) {
1499
+ inputArray = utf8ToArray(inArray);
1500
+ } else {
1501
+ inputArray = hexToArray(inArray);
1502
+ }
1503
+ } else {
1504
+ inputArray = Uint8Array.from(inArray);
1505
+ }
1506
+ const tagArray = tag ? typeof tag === "string" ? hexToArray(tag) : Uint8Array.from(tag) : void 0;
1507
+ const result = sm4Gcm(inputArray, keyArray, ivArray, aadArray, cryptFlag, tagArray);
1508
+ if (output === "array") {
1509
+ if (outputTag && cryptFlag !== DECRYPT) {
1510
+ return result;
1511
+ }
1512
+ return result.output;
1513
+ } else {
1514
+ if (outputTag && cryptFlag !== DECRYPT) {
1515
+ return {
1516
+ output: bytesToHex(result.output),
1517
+ tag: result.tag ? bytesToHex(result.tag) : void 0
1518
+ };
1519
+ }
1520
+ if (cryptFlag !== DECRYPT) {
1521
+ return {
1522
+ output: bytesToHex(result.output),
1523
+ tag: result.tag ? bytesToHex(result.tag) : void 0
1524
+ };
1525
+ } else {
1526
+ return arrayToUtf8(result.output);
1527
+ }
1528
+ }
1529
+ }
1396
1530
  if (mode === "cbc") {
1397
1531
  if (typeof iv === "string")
1398
1532
  iv = hexToArray(iv);
package/dist/index.mjs CHANGED
@@ -768,10 +768,8 @@ function xorCipherStream(x2, y2, msg) {
768
768
  msg[i] ^= t[offset++] & 255;
769
769
  }
770
770
  }
771
- function doDecrypt(encryptData, privateKey, cipherMode = 1, {
772
- output = "string",
773
- asn1 = false
774
- } = {}) {
771
+ function doDecrypt(encryptData, privateKey, cipherMode = 1, options) {
772
+ const { output = "string", asn1 = false } = options || {};
775
773
  const privateKeyInteger = utils4.hexToNumber(privateKey);
776
774
  let c1;
777
775
  let c2;
@@ -966,6 +964,8 @@ __export(sm4_exports, {
966
964
  encrypt: () => encrypt,
967
965
  sm4: () => sm4
968
966
  });
967
+ import { ghash } from "@noble/ciphers/_polyval";
968
+ import { createView as createView2, setBigUint64 as setBigUint642 } from "@noble/ciphers/utils";
969
969
  var DECRYPT = 0;
970
970
  var ROUND = 32;
971
971
  var BLOCK = 16;
@@ -1353,14 +1353,148 @@ function sms4KeyExt(key, roundKey, cryptFlag) {
1353
1353
  }
1354
1354
  }
1355
1355
  }
1356
+ function incrementCounter(counter) {
1357
+ for (let i = counter.length - 1; i >= 0; i--) {
1358
+ counter[i]++;
1359
+ if (counter[i] !== 0)
1360
+ break;
1361
+ }
1362
+ }
1363
+ function sm4Gcm(inArray, key, ivArray, aadArray, cryptFlag, tagArray) {
1364
+ const tagLength = 16;
1365
+ function deriveKeys() {
1366
+ const roundKey2 = new Uint32Array(ROUND);
1367
+ sms4KeyExt(key, roundKey2, 1);
1368
+ const authKey = new Uint8Array(16).fill(0);
1369
+ const h2 = new Uint8Array(16);
1370
+ sms4Crypt(authKey, h2, roundKey2);
1371
+ let j02;
1372
+ if (ivArray.length === 12) {
1373
+ j02 = new Uint8Array(16);
1374
+ j02.set(ivArray, 0);
1375
+ j02[15] = 1;
1376
+ } else {
1377
+ const g = ghash.create(h2);
1378
+ g.update(ivArray);
1379
+ const lenIv = new Uint8Array(16);
1380
+ const view = createView2(lenIv);
1381
+ setBigUint642(view, 8, BigInt(ivArray.length * 8), false);
1382
+ g.update(lenIv);
1383
+ j02 = g.digest();
1384
+ }
1385
+ const counter2 = new Uint8Array(j02);
1386
+ incrementCounter(counter2);
1387
+ const tagMask2 = new Uint8Array(16);
1388
+ sms4Crypt(j02, tagMask2, roundKey2);
1389
+ return { roundKey: roundKey2, h: h2, j0: j02, counter: counter2, tagMask: tagMask2 };
1390
+ }
1391
+ function computeTag(h2, data) {
1392
+ const aadLength = aadArray.length;
1393
+ const dataLength = data.length;
1394
+ const g = ghash.create(h2);
1395
+ if (aadLength > 0) {
1396
+ g.update(aadArray);
1397
+ }
1398
+ g.update(data);
1399
+ const lenBlock = new Uint8Array(16);
1400
+ const view = createView2(lenBlock);
1401
+ setBigUint642(view, 0, BigInt(aadLength * 8), false);
1402
+ setBigUint642(view, 8, BigInt(dataLength * 8), false);
1403
+ g.update(lenBlock);
1404
+ return g.digest();
1405
+ }
1406
+ const { roundKey, h, j0, counter, tagMask } = deriveKeys();
1407
+ if (cryptFlag === DECRYPT && tagArray) {
1408
+ const calculatedTag = computeTag(h, inArray);
1409
+ for (let i = 0; i < 16; i++) {
1410
+ calculatedTag[i] ^= tagMask[i];
1411
+ }
1412
+ let tagMatch = 0;
1413
+ for (let i = 0; i < 16; i++) {
1414
+ tagMatch |= calculatedTag[i] ^ tagArray[i];
1415
+ }
1416
+ if (tagMatch !== 0) {
1417
+ throw new Error("authentication tag mismatch");
1418
+ }
1419
+ }
1420
+ const outArray = new Uint8Array(inArray.length);
1421
+ let point = 0;
1422
+ let restLen = inArray.length;
1423
+ while (restLen >= BLOCK) {
1424
+ const blockOut = new Uint8Array(BLOCK);
1425
+ sms4Crypt(counter, blockOut, roundKey);
1426
+ for (let i = 0; i < BLOCK && i < restLen; i++) {
1427
+ outArray[point + i] = inArray[point + i] ^ blockOut[i];
1428
+ }
1429
+ incrementCounter(counter);
1430
+ point += BLOCK;
1431
+ restLen -= BLOCK;
1432
+ }
1433
+ if (restLen > 0) {
1434
+ const blockOut = new Uint8Array(BLOCK);
1435
+ sms4Crypt(counter, blockOut, roundKey);
1436
+ for (let i = 0; i < restLen; i++) {
1437
+ outArray[point + i] = inArray[point + i] ^ blockOut[i];
1438
+ }
1439
+ }
1440
+ if (cryptFlag !== DECRYPT) {
1441
+ const calculatedTag = computeTag(h, outArray);
1442
+ for (let i = 0; i < 16; i++) {
1443
+ calculatedTag[i] ^= tagMask[i];
1444
+ }
1445
+ return { output: outArray, tag: calculatedTag };
1446
+ }
1447
+ return { output: outArray };
1448
+ }
1356
1449
  var blockOutput = new Uint8Array(16);
1357
1450
  function sm4(inArray, key, cryptFlag, options = {}) {
1358
1451
  let {
1359
1452
  padding = "pkcs#7",
1360
1453
  mode,
1361
1454
  iv = new Uint8Array(16),
1362
- output
1455
+ output,
1456
+ associatedData,
1457
+ outputTag,
1458
+ tag
1363
1459
  } = options;
1460
+ if (mode === "gcm") {
1461
+ const keyArray = typeof key === "string" ? hexToArray(key) : Uint8Array.from(key);
1462
+ const ivArray = typeof iv === "string" ? hexToArray(iv) : Uint8Array.from(iv);
1463
+ const aadArray = associatedData ? typeof associatedData === "string" ? hexToArray(associatedData) : Uint8Array.from(associatedData) : new Uint8Array(0);
1464
+ let inputArray;
1465
+ if (typeof inArray === "string") {
1466
+ if (cryptFlag !== DECRYPT) {
1467
+ inputArray = utf8ToArray(inArray);
1468
+ } else {
1469
+ inputArray = hexToArray(inArray);
1470
+ }
1471
+ } else {
1472
+ inputArray = Uint8Array.from(inArray);
1473
+ }
1474
+ const tagArray = tag ? typeof tag === "string" ? hexToArray(tag) : Uint8Array.from(tag) : void 0;
1475
+ const result = sm4Gcm(inputArray, keyArray, ivArray, aadArray, cryptFlag, tagArray);
1476
+ if (output === "array") {
1477
+ if (outputTag && cryptFlag !== DECRYPT) {
1478
+ return result;
1479
+ }
1480
+ return result.output;
1481
+ } else {
1482
+ if (outputTag && cryptFlag !== DECRYPT) {
1483
+ return {
1484
+ output: bytesToHex(result.output),
1485
+ tag: result.tag ? bytesToHex(result.tag) : void 0
1486
+ };
1487
+ }
1488
+ if (cryptFlag !== DECRYPT) {
1489
+ return {
1490
+ output: bytesToHex(result.output),
1491
+ tag: result.tag ? bytesToHex(result.tag) : void 0
1492
+ };
1493
+ } else {
1494
+ return arrayToUtf8(result.output);
1495
+ }
1496
+ }
1497
+ }
1364
1498
  if (mode === "cbc") {
1365
1499
  if (typeof iv === "string")
1366
1500
  iv = hexToArray(iv);
@@ -60,7 +60,7 @@ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?:
60
60
  asn1?: boolean;
61
61
  }): Uint8Array;
62
62
  declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
63
- output: 'string';
63
+ output?: 'string';
64
64
  asn1?: boolean;
65
65
  }): string;
66
66
  interface SignaturePoint {
@@ -146,30 +146,52 @@ declare function sm3(input: string | Uint8Array, options?: {
146
146
 
147
147
  interface SM4Options {
148
148
  padding?: 'pkcs#7' | 'pkcs#5' | 'none' | null;
149
- mode?: 'cbc' | 'ecb';
149
+ mode?: 'cbc' | 'ecb' | 'gcm';
150
150
  iv?: Uint8Array | string;
151
151
  output?: 'string' | 'array';
152
+ associatedData?: Uint8Array | string;
153
+ outputTag?: boolean;
154
+ tag?: Uint8Array | string;
152
155
  }
153
- declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array;
156
+ declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array | {
157
+ output: Uint8Array;
158
+ tag?: Uint8Array | undefined;
159
+ } | {
160
+ output: string;
161
+ tag: string | undefined;
162
+ };
163
+ interface GCMResult<T = Uint8Array | string> {
164
+ output: T;
165
+ tag?: T;
166
+ }
167
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options: {
168
+ mode: 'gcm';
169
+ output: 'array';
170
+ } & SM4Options): GCMResult<Uint8Array>;
171
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options: {
172
+ mode: 'gcm';
173
+ output?: 'string';
174
+ } & SM4Options): GCMResult<string>;
154
175
  declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
155
176
  output: 'array';
156
177
  } & SM4Options): Uint8Array;
157
178
  declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
158
- output: 'string';
179
+ output?: 'string';
159
180
  } & SM4Options): string;
160
181
  declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
161
182
  output: 'array';
162
183
  } & SM4Options): Uint8Array;
163
184
  declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
164
- output: 'string';
185
+ output?: 'string';
165
186
  } & SM4Options): string;
166
187
 
188
+ type index_GCMResult<T = Uint8Array | string> = GCMResult<T>;
167
189
  type index_SM4Options = SM4Options;
168
190
  declare const index_decrypt: typeof decrypt;
169
191
  declare const index_encrypt: typeof encrypt;
170
192
  declare const index_sm4: typeof sm4;
171
193
  declare namespace index {
172
- export { type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
194
+ export { type index_GCMResult as GCMResult, type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
173
195
  }
174
196
 
175
197
  export { index$1 as sm2, sm3, index as sm4 };
@@ -1227,7 +1227,7 @@ function nLength(n, nBitLength) {
1227
1227
  };
1228
1228
  }
1229
1229
  function Field(ORDER, bitLen2) {
1230
- var isLE2 = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false, redef = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {};
1230
+ var isLE3 = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false, redef = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {};
1231
1231
  if (ORDER <= _0n2) throw new Error("Expected Fp ORDER > 0, got ".concat(ORDER));
1232
1232
  var _nLength = nLength(ORDER, bitLen2), BITS = _nLength.nBitLength, BYTES = _nLength.nByteLength;
1233
1233
  if (BYTES > 2048) throw new Error("Field lengths over 2048 bytes are not supported");
@@ -1304,22 +1304,22 @@ function Field(ORDER, bitLen2) {
1304
1304
  return c ? b : a;
1305
1305
  },
1306
1306
  toBytes: function(num) {
1307
- return isLE2 ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES);
1307
+ return isLE3 ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES);
1308
1308
  },
1309
1309
  fromBytes: function(bytes) {
1310
1310
  if (bytes.length !== BYTES) throw new Error("Fp.fromBytes: expected ".concat(BYTES, ", got ").concat(bytes.length));
1311
- return isLE2 ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
1311
+ return isLE3 ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
1312
1312
  }
1313
1313
  });
1314
1314
  return Object.freeze(f);
1315
1315
  }
1316
1316
  function hashToPrivateScalar(hash, groupOrder) {
1317
- var isLE2 = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
1317
+ var isLE3 = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
1318
1318
  hash = ensureBytes("privateHash", hash);
1319
1319
  var hashLen = hash.length;
1320
1320
  var minLen = nLength(groupOrder).nByteLength + 8;
1321
1321
  if (minLen < 24 || hashLen < minLen || hashLen > 1024) throw new Error("hashToPrivateScalar: expected ".concat(minLen, "-1024 bytes of input, got ").concat(hashLen));
1322
- var num = isLE2 ? bytesToNumberLE(hash) : bytesToNumberBE(hash);
1322
+ var num = isLE3 ? bytesToNumberLE(hash) : bytesToNumberBE(hash);
1323
1323
  return mod(num, groupOrder - _1n2) + _1n2;
1324
1324
  }
1325
1325
  // node_modules/.pnpm/@noble+curves@1.1.0/node_modules/@noble/curves/esm/abstract/curve.js
@@ -1564,7 +1564,7 @@ function weierstrassPoints(opts) {
1564
1564
  };
1565
1565
  var CURVE = validatePointOpts(opts);
1566
1566
  var Fp = CURVE.Fp;
1567
- var toBytes2 = CURVE.toBytes || function(c, point, isCompressed) {
1567
+ var toBytes3 = CURVE.toBytes || function(c, point, isCompressed) {
1568
1568
  var a = point.toAffine();
1569
1569
  return concatBytes(Uint8Array.from([
1570
1570
  4
@@ -1910,7 +1910,7 @@ function weierstrassPoints(opts) {
1910
1910
  value: function toRawBytes() {
1911
1911
  var isCompressed = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : true;
1912
1912
  this.assertValidity();
1913
- return toBytes2(Point, this, isCompressed);
1913
+ return toBytes3(Point, this, isCompressed);
1914
1914
  }
1915
1915
  },
1916
1916
  {
@@ -2542,16 +2542,16 @@ var BoolB = function(A, B, C) {
2542
2542
  var BoolC = function(A, B, C) {
2543
2543
  return A & B | ~A & C;
2544
2544
  };
2545
- function setBigUint64(view, byteOffset, value, isLE2) {
2546
- if (typeof view.setBigUint64 === "function") return view.setBigUint64(byteOffset, value, isLE2);
2545
+ function setBigUint64(view, byteOffset, value, isLE3) {
2546
+ if (typeof view.setBigUint64 === "function") return view.setBigUint64(byteOffset, value, isLE3);
2547
2547
  var _32n = BigInt(32);
2548
2548
  var _u32_max = BigInt(4294967295);
2549
2549
  var wh = Number(value >> _32n & _u32_max);
2550
2550
  var wl = Number(value & _u32_max);
2551
- var h = isLE2 ? 4 : 0;
2552
- var l = isLE2 ? 0 : 4;
2553
- view.setUint32(byteOffset + h, wh, isLE2);
2554
- view.setUint32(byteOffset + l, wl, isLE2);
2551
+ var h = isLE3 ? 4 : 0;
2552
+ var l = isLE3 ? 0 : 4;
2553
+ view.setUint32(byteOffset + h, wh, isLE3);
2554
+ view.setUint32(byteOffset + l, wl, isLE3);
2555
2555
  }
2556
2556
  function rotl(x2, n) {
2557
2557
  var s = n & 31;
@@ -2566,14 +2566,14 @@ function P1(X) {
2566
2566
  var SHA2 = /*#__PURE__*/ function(Hash) {
2567
2567
  _inherits(SHA2, Hash);
2568
2568
  var _super = _create_super(SHA2);
2569
- function SHA2(blockLen, outputLen, padOffset, isLE2) {
2569
+ function SHA2(blockLen, outputLen, padOffset, isLE3) {
2570
2570
  _class_call_check(this, SHA2);
2571
2571
  var _this;
2572
2572
  _this = _super.call(this);
2573
2573
  _this.blockLen = blockLen;
2574
2574
  _this.outputLen = outputLen;
2575
2575
  _this.padOffset = padOffset;
2576
- _this.isLE = isLE2;
2576
+ _this.isLE = isLE3;
2577
2577
  // For partial updates less than block size
2578
2578
  __publicField(_assert_this_initialized(_this), "buffer");
2579
2579
  __publicField(_assert_this_initialized(_this), "view");
@@ -2616,7 +2616,7 @@ var SHA2 = /*#__PURE__*/ function(Hash) {
2616
2616
  key: "digestInto",
2617
2617
  value: function digestInto(out) {
2618
2618
  this.finished = true;
2619
- var _this = this, buffer = _this.buffer, view = _this.view, blockLen = _this.blockLen, isLE2 = _this.isLE;
2619
+ var _this = this, buffer = _this.buffer, view = _this.view, blockLen = _this.blockLen, isLE3 = _this.isLE;
2620
2620
  var pos = this.pos;
2621
2621
  buffer[pos++] = 128;
2622
2622
  this.buffer.subarray(pos).fill(0);
@@ -2625,7 +2625,7 @@ var SHA2 = /*#__PURE__*/ function(Hash) {
2625
2625
  pos = 0;
2626
2626
  }
2627
2627
  for(var i = pos; i < blockLen; i++)buffer[i] = 0;
2628
- setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
2628
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE3);
2629
2629
  this.process(view, 0);
2630
2630
  var oview = createView(out);
2631
2631
  var len = this.outputLen;
@@ -2633,7 +2633,7 @@ var SHA2 = /*#__PURE__*/ function(Hash) {
2633
2633
  var outLen = len / 4;
2634
2634
  var state = this.get();
2635
2635
  if (outLen > state.length) throw new Error("_sha2: outputLen bigger than state");
2636
- for(var i1 = 0; i1 < outLen; i1++)oview.setUint32(4 * i1, state[i1], isLE2);
2636
+ for(var i1 = 0; i1 < outLen; i1++)oview.setUint32(4 * i1, state[i1], isLE3);
2637
2637
  }
2638
2638
  },
2639
2639
  {
@@ -3073,7 +3073,8 @@ function xorCipherStream(x2, y2, msg) {
3073
3073
  }
3074
3074
  }
3075
3075
  function doDecrypt(encryptData, privateKey) {
3076
- var cipherMode = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1, _ref = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {}, _ref_output = _ref.output, output = _ref_output === void 0 ? "string" : _ref_output, _ref_asn1 = _ref.asn1, asn1 = _ref_asn1 === void 0 ? false : _ref_asn1;
3076
+ var cipherMode = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1, options = arguments.length > 3 ? arguments[3] : void 0;
3077
+ var _ref = options || {}, _ref_output = _ref.output, output = _ref_output === void 0 ? "string" : _ref_output, _ref_asn1 = _ref.asn1, asn1 = _ref_asn1 === void 0 ? false : _ref_asn1;
3077
3078
  var privateKeyInteger = hexToNumber(privateKey);
3078
3079
  var c1;
3079
3080
  var c2;
@@ -3271,6 +3272,337 @@ __export(sm4_exports, {
3271
3272
  return sm4;
3272
3273
  }
3273
3274
  });
3275
+ // node_modules/.pnpm/@noble+ciphers@1.2.1/node_modules/@noble/ciphers/esm/_assert.js
3276
+ function isBytes(a) {
3277
+ return _instanceof(a, Uint8Array) || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
3278
+ }
3279
+ function abytes(b) {
3280
+ for(var _len = arguments.length, lengths = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){
3281
+ lengths[_key - 1] = arguments[_key];
3282
+ }
3283
+ if (!isBytes(b)) throw new Error("Uint8Array expected");
3284
+ if (lengths.length > 0 && !lengths.includes(b.length)) throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
3285
+ }
3286
+ function aexists(instance) {
3287
+ var checkFinished = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true;
3288
+ if (instance.destroyed) throw new Error("Hash instance has been destroyed");
3289
+ if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
3290
+ }
3291
+ function aoutput(out, instance) {
3292
+ abytes(out);
3293
+ var min = instance.outputLen;
3294
+ if (out.length < min) {
3295
+ throw new Error("digestInto() expects output buffer of length at least " + min);
3296
+ }
3297
+ }
3298
+ // node_modules/.pnpm/@noble+ciphers@1.2.1/node_modules/@noble/ciphers/esm/utils.js
3299
+ var u32 = function(arr) {
3300
+ return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
3301
+ };
3302
+ var createView2 = function(arr) {
3303
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
3304
+ };
3305
+ var isLE2 = new Uint8Array(new Uint32Array([
3306
+ 287454020
3307
+ ]).buffer)[0] === 68;
3308
+ if (!isLE2) throw new Error("Non little-endian hardware is not supported");
3309
+ function utf8ToBytes3(str) {
3310
+ if (typeof str !== "string") throw new Error("string expected");
3311
+ return new Uint8Array(new TextEncoder().encode(str));
3312
+ }
3313
+ function toBytes2(data) {
3314
+ if (typeof data === "string") data = utf8ToBytes3(data);
3315
+ else if (isBytes(data)) data = copyBytes(data);
3316
+ else throw new Error("Uint8Array expected, got " + (typeof data === "undefined" ? "undefined" : _type_of(data)));
3317
+ return data;
3318
+ }
3319
+ function setBigUint642(view, byteOffset, value, isLE3) {
3320
+ if (typeof view.setBigUint64 === "function") return view.setBigUint64(byteOffset, value, isLE3);
3321
+ var _32n = BigInt(32);
3322
+ var _u32_max = BigInt(4294967295);
3323
+ var wh = Number(value >> _32n & _u32_max);
3324
+ var wl = Number(value & _u32_max);
3325
+ var h = isLE3 ? 4 : 0;
3326
+ var l = isLE3 ? 0 : 4;
3327
+ view.setUint32(byteOffset + h, wh, isLE3);
3328
+ view.setUint32(byteOffset + l, wl, isLE3);
3329
+ }
3330
+ function copyBytes(bytes) {
3331
+ return Uint8Array.from(bytes);
3332
+ }
3333
+ function clean() {
3334
+ for(var _len = arguments.length, arrays = new Array(_len), _key = 0; _key < _len; _key++){
3335
+ arrays[_key] = arguments[_key];
3336
+ }
3337
+ for(var i = 0; i < arrays.length; i++){
3338
+ arrays[i].fill(0);
3339
+ }
3340
+ }
3341
+ // node_modules/.pnpm/@noble+ciphers@1.2.1/node_modules/@noble/ciphers/esm/_polyval.js
3342
+ var BLOCK_SIZE = 16;
3343
+ var ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
3344
+ var ZEROS32 = u32(ZEROS16);
3345
+ var POLY = 225;
3346
+ var mul2 = function(s0, s1, s2, s3) {
3347
+ var hiBit = s3 & 1;
3348
+ return {
3349
+ s3: s2 << 31 | s3 >>> 1,
3350
+ s2: s1 << 31 | s2 >>> 1,
3351
+ s1: s0 << 31 | s1 >>> 1,
3352
+ s0: s0 >>> 1 ^ POLY << 24 & -(hiBit & 1)
3353
+ };
3354
+ };
3355
+ var swapLE = function(n) {
3356
+ return (n >>> 0 & 255) << 24 | (n >>> 8 & 255) << 16 | (n >>> 16 & 255) << 8 | n >>> 24 & 255 | 0;
3357
+ };
3358
+ function _toGHASHKey(k) {
3359
+ k.reverse();
3360
+ var hiBit = k[15] & 1;
3361
+ var carry = 0;
3362
+ for(var i = 0; i < k.length; i++){
3363
+ var t = k[i];
3364
+ k[i] = t >>> 1 | carry;
3365
+ carry = (t & 1) << 7;
3366
+ }
3367
+ k[0] ^= -hiBit & 225;
3368
+ return k;
3369
+ }
3370
+ var estimateWindow = function(bytes) {
3371
+ if (bytes > 64 * 1024) return 8;
3372
+ if (bytes > 1024) return 4;
3373
+ return 2;
3374
+ };
3375
+ var GHASH = /*#__PURE__*/ function() {
3376
+ function GHASH(key, expectedLength) {
3377
+ _class_call_check(this, GHASH);
3378
+ this.blockLen = BLOCK_SIZE;
3379
+ this.outputLen = BLOCK_SIZE;
3380
+ this.s0 = 0;
3381
+ this.s1 = 0;
3382
+ this.s2 = 0;
3383
+ this.s3 = 0;
3384
+ this.finished = false;
3385
+ key = toBytes2(key);
3386
+ abytes(key, 16);
3387
+ var kView = createView2(key);
3388
+ var k0 = kView.getUint32(0, false);
3389
+ var k1 = kView.getUint32(4, false);
3390
+ var k2 = kView.getUint32(8, false);
3391
+ var k3 = kView.getUint32(12, false);
3392
+ var doubles = [];
3393
+ for(var i = 0; i < 128; i++){
3394
+ doubles.push({
3395
+ s0: swapLE(k0),
3396
+ s1: swapLE(k1),
3397
+ s2: swapLE(k2),
3398
+ s3: swapLE(k3)
3399
+ });
3400
+ var ref;
3401
+ ref = mul2(k0, k1, k2, k3), k0 = ref.s0, k1 = ref.s1, k2 = ref.s2, k3 = ref.s3, ref;
3402
+ }
3403
+ var W = estimateWindow(expectedLength || 1024);
3404
+ if (![
3405
+ 1,
3406
+ 2,
3407
+ 4,
3408
+ 8
3409
+ ].includes(W)) throw new Error("ghash: invalid window size, expected 2, 4 or 8");
3410
+ this.W = W;
3411
+ var bits = 128;
3412
+ var windows = bits / W;
3413
+ var windowSize = this.windowSize = Math.pow(2, W);
3414
+ var items = [];
3415
+ for(var w = 0; w < windows; w++){
3416
+ for(var byte = 0; byte < windowSize; byte++){
3417
+ var s0 = 0, s1 = 0, s2 = 0, s3 = 0;
3418
+ for(var j = 0; j < W; j++){
3419
+ var bit = byte >>> W - j - 1 & 1;
3420
+ if (!bit) continue;
3421
+ var _doubles_ = doubles[W * w + j], d0 = _doubles_.s0, d1 = _doubles_.s1, d2 = _doubles_.s2, d3 = _doubles_.s3;
3422
+ s0 ^= d0, s1 ^= d1, s2 ^= d2, s3 ^= d3;
3423
+ }
3424
+ items.push({
3425
+ s0: s0,
3426
+ s1: s1,
3427
+ s2: s2,
3428
+ s3: s3
3429
+ });
3430
+ }
3431
+ }
3432
+ this.t = items;
3433
+ }
3434
+ _create_class(GHASH, [
3435
+ {
3436
+ key: "_updateBlock",
3437
+ value: function _updateBlock(s0, s1, s2, s3) {
3438
+ s0 ^= this.s0, s1 ^= this.s1, s2 ^= this.s2, s3 ^= this.s3;
3439
+ var _this = this, W = _this.W, t = _this.t, windowSize = _this.windowSize;
3440
+ var o0 = 0, o1 = 0, o2 = 0, o3 = 0;
3441
+ var mask = (1 << W) - 1;
3442
+ var w = 0;
3443
+ for(var _i = 0, _iter = [
3444
+ s0,
3445
+ s1,
3446
+ s2,
3447
+ s3
3448
+ ]; _i < _iter.length; _i++){
3449
+ var num = _iter[_i];
3450
+ for(var bytePos = 0; bytePos < 4; bytePos++){
3451
+ var byte = num >>> 8 * bytePos & 255;
3452
+ for(var bitPos = 8 / W - 1; bitPos >= 0; bitPos--){
3453
+ var bit = byte >>> W * bitPos & mask;
3454
+ var _t_ = t[w * windowSize + bit], e0 = _t_.s0, e1 = _t_.s1, e2 = _t_.s2, e3 = _t_.s3;
3455
+ o0 ^= e0, o1 ^= e1, o2 ^= e2, o3 ^= e3;
3456
+ w += 1;
3457
+ }
3458
+ }
3459
+ }
3460
+ this.s0 = o0;
3461
+ this.s1 = o1;
3462
+ this.s2 = o2;
3463
+ this.s3 = o3;
3464
+ }
3465
+ },
3466
+ {
3467
+ key: "update",
3468
+ value: function update(data) {
3469
+ data = toBytes2(data);
3470
+ aexists(this);
3471
+ var b32 = u32(data);
3472
+ var blocks = Math.floor(data.length / BLOCK_SIZE);
3473
+ var left = data.length % BLOCK_SIZE;
3474
+ for(var i = 0; i < blocks; i++){
3475
+ this._updateBlock(b32[i * 4 + 0], b32[i * 4 + 1], b32[i * 4 + 2], b32[i * 4 + 3]);
3476
+ }
3477
+ if (left) {
3478
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
3479
+ this._updateBlock(ZEROS32[0], ZEROS32[1], ZEROS32[2], ZEROS32[3]);
3480
+ clean(ZEROS32);
3481
+ }
3482
+ return this;
3483
+ }
3484
+ },
3485
+ {
3486
+ key: "destroy",
3487
+ value: function destroy() {
3488
+ var t = this.t;
3489
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3490
+ try {
3491
+ for(var _iterator = t[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
3492
+ var elm = _step.value;
3493
+ elm.s0 = 0, elm.s1 = 0, elm.s2 = 0, elm.s3 = 0;
3494
+ }
3495
+ } catch (err) {
3496
+ _didIteratorError = true;
3497
+ _iteratorError = err;
3498
+ } finally{
3499
+ try {
3500
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
3501
+ _iterator.return();
3502
+ }
3503
+ } finally{
3504
+ if (_didIteratorError) {
3505
+ throw _iteratorError;
3506
+ }
3507
+ }
3508
+ }
3509
+ }
3510
+ },
3511
+ {
3512
+ key: "digestInto",
3513
+ value: function digestInto(out) {
3514
+ aexists(this);
3515
+ aoutput(out, this);
3516
+ this.finished = true;
3517
+ var _this = this, s0 = _this.s0, s1 = _this.s1, s2 = _this.s2, s3 = _this.s3;
3518
+ var o32 = u32(out);
3519
+ o32[0] = s0;
3520
+ o32[1] = s1;
3521
+ o32[2] = s2;
3522
+ o32[3] = s3;
3523
+ return out;
3524
+ }
3525
+ },
3526
+ {
3527
+ key: "digest",
3528
+ value: function digest() {
3529
+ var res = new Uint8Array(BLOCK_SIZE);
3530
+ this.digestInto(res);
3531
+ this.destroy();
3532
+ return res;
3533
+ }
3534
+ }
3535
+ ]);
3536
+ return GHASH;
3537
+ }();
3538
+ var Polyval = /*#__PURE__*/ function(GHASH) {
3539
+ _inherits(Polyval, GHASH);
3540
+ var _super = _create_super(Polyval);
3541
+ function Polyval(key, expectedLength) {
3542
+ _class_call_check(this, Polyval);
3543
+ key = toBytes2(key);
3544
+ var ghKey = _toGHASHKey(copyBytes(key));
3545
+ var _this = _super.call(this, ghKey, expectedLength);
3546
+ clean(ghKey);
3547
+ return _this;
3548
+ }
3549
+ _create_class(Polyval, [
3550
+ {
3551
+ key: "update",
3552
+ value: function update(data) {
3553
+ data = toBytes2(data);
3554
+ aexists(this);
3555
+ var b32 = u32(data);
3556
+ var left = data.length % BLOCK_SIZE;
3557
+ var blocks = Math.floor(data.length / BLOCK_SIZE);
3558
+ for(var i = 0; i < blocks; i++){
3559
+ this._updateBlock(swapLE(b32[i * 4 + 3]), swapLE(b32[i * 4 + 2]), swapLE(b32[i * 4 + 1]), swapLE(b32[i * 4 + 0]));
3560
+ }
3561
+ if (left) {
3562
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
3563
+ this._updateBlock(swapLE(ZEROS32[3]), swapLE(ZEROS32[2]), swapLE(ZEROS32[1]), swapLE(ZEROS32[0]));
3564
+ clean(ZEROS32);
3565
+ }
3566
+ return this;
3567
+ }
3568
+ },
3569
+ {
3570
+ key: "digestInto",
3571
+ value: function digestInto(out) {
3572
+ aexists(this);
3573
+ aoutput(out, this);
3574
+ this.finished = true;
3575
+ var _this = this, s0 = _this.s0, s1 = _this.s1, s2 = _this.s2, s3 = _this.s3;
3576
+ var o32 = u32(out);
3577
+ o32[0] = s0;
3578
+ o32[1] = s1;
3579
+ o32[2] = s2;
3580
+ o32[3] = s3;
3581
+ return out.reverse();
3582
+ }
3583
+ }
3584
+ ]);
3585
+ return Polyval;
3586
+ }(GHASH);
3587
+ function wrapConstructorWithKey(hashCons) {
3588
+ var hashC = function(msg, key) {
3589
+ return hashCons(key, msg.length).update(toBytes2(msg)).digest();
3590
+ };
3591
+ var tmp2 = hashCons(new Uint8Array(16), 0);
3592
+ hashC.outputLen = tmp2.outputLen;
3593
+ hashC.blockLen = tmp2.blockLen;
3594
+ hashC.create = function(key, expectedLength) {
3595
+ return hashCons(key, expectedLength);
3596
+ };
3597
+ return hashC;
3598
+ }
3599
+ var ghash = wrapConstructorWithKey(function(key, expectedLength) {
3600
+ return new GHASH(key, expectedLength);
3601
+ });
3602
+ var polyval = wrapConstructorWithKey(function(key, expectedLength) {
3603
+ return new Polyval(key, expectedLength);
3604
+ });
3605
+ // src/sm4/index.ts
3274
3606
  var DECRYPT = 0;
3275
3607
  var ROUND = 32;
3276
3608
  var BLOCK = 16;
@@ -3662,10 +3994,151 @@ function sms4KeyExt(key, roundKey, cryptFlag) {
3662
3994
  }
3663
3995
  }
3664
3996
  }
3997
+ function incrementCounter(counter) {
3998
+ for(var i = counter.length - 1; i >= 0; i--){
3999
+ counter[i]++;
4000
+ if (counter[i] !== 0) break;
4001
+ }
4002
+ }
4003
+ function sm4Gcm(inArray, key, ivArray, aadArray, cryptFlag, tagArray) {
4004
+ var deriveKeys = function deriveKeys() {
4005
+ var roundKey2 = new Uint32Array(ROUND);
4006
+ sms4KeyExt(key, roundKey2, 1);
4007
+ var authKey = new Uint8Array(16).fill(0);
4008
+ var h2 = new Uint8Array(16);
4009
+ sms4Crypt(authKey, h2, roundKey2);
4010
+ var j02;
4011
+ if (ivArray.length === 12) {
4012
+ j02 = new Uint8Array(16);
4013
+ j02.set(ivArray, 0);
4014
+ j02[15] = 1;
4015
+ } else {
4016
+ var g = ghash.create(h2);
4017
+ g.update(ivArray);
4018
+ var lenIv = new Uint8Array(16);
4019
+ var view = createView2(lenIv);
4020
+ setBigUint642(view, 8, BigInt(ivArray.length * 8), false);
4021
+ g.update(lenIv);
4022
+ j02 = g.digest();
4023
+ }
4024
+ var counter2 = new Uint8Array(j02);
4025
+ incrementCounter(counter2);
4026
+ var tagMask2 = new Uint8Array(16);
4027
+ sms4Crypt(j02, tagMask2, roundKey2);
4028
+ return {
4029
+ roundKey: roundKey2,
4030
+ h: h2,
4031
+ j0: j02,
4032
+ counter: counter2,
4033
+ tagMask: tagMask2
4034
+ };
4035
+ };
4036
+ var computeTag = function computeTag(h2, data) {
4037
+ var aadLength = aadArray.length;
4038
+ var dataLength = data.length;
4039
+ var g = ghash.create(h2);
4040
+ if (aadLength > 0) {
4041
+ g.update(aadArray);
4042
+ }
4043
+ g.update(data);
4044
+ var lenBlock = new Uint8Array(16);
4045
+ var view = createView2(lenBlock);
4046
+ setBigUint642(view, 0, BigInt(aadLength * 8), false);
4047
+ setBigUint642(view, 8, BigInt(dataLength * 8), false);
4048
+ g.update(lenBlock);
4049
+ return g.digest();
4050
+ };
4051
+ var tagLength = 16;
4052
+ var _deriveKeys = deriveKeys(), roundKey = _deriveKeys.roundKey, h = _deriveKeys.h, j0 = _deriveKeys.j0, counter = _deriveKeys.counter, tagMask = _deriveKeys.tagMask;
4053
+ if (cryptFlag === DECRYPT && tagArray) {
4054
+ var calculatedTag = computeTag(h, inArray);
4055
+ for(var i = 0; i < 16; i++){
4056
+ calculatedTag[i] ^= tagMask[i];
4057
+ }
4058
+ var tagMatch = 0;
4059
+ for(var i1 = 0; i1 < 16; i1++){
4060
+ tagMatch |= calculatedTag[i1] ^ tagArray[i1];
4061
+ }
4062
+ if (tagMatch !== 0) {
4063
+ throw new Error("authentication tag mismatch");
4064
+ }
4065
+ }
4066
+ var outArray = new Uint8Array(inArray.length);
4067
+ var point = 0;
4068
+ var restLen = inArray.length;
4069
+ while(restLen >= BLOCK){
4070
+ var blockOut = new Uint8Array(BLOCK);
4071
+ sms4Crypt(counter, blockOut, roundKey);
4072
+ for(var i2 = 0; i2 < BLOCK && i2 < restLen; i2++){
4073
+ outArray[point + i2] = inArray[point + i2] ^ blockOut[i2];
4074
+ }
4075
+ incrementCounter(counter);
4076
+ point += BLOCK;
4077
+ restLen -= BLOCK;
4078
+ }
4079
+ if (restLen > 0) {
4080
+ var blockOut1 = new Uint8Array(BLOCK);
4081
+ sms4Crypt(counter, blockOut1, roundKey);
4082
+ for(var i3 = 0; i3 < restLen; i3++){
4083
+ outArray[point + i3] = inArray[point + i3] ^ blockOut1[i3];
4084
+ }
4085
+ }
4086
+ if (cryptFlag !== DECRYPT) {
4087
+ var calculatedTag1 = computeTag(h, outArray);
4088
+ for(var i4 = 0; i4 < 16; i4++){
4089
+ calculatedTag1[i4] ^= tagMask[i4];
4090
+ }
4091
+ return {
4092
+ output: outArray,
4093
+ tag: calculatedTag1
4094
+ };
4095
+ }
4096
+ return {
4097
+ output: outArray
4098
+ };
4099
+ }
3665
4100
  var blockOutput = new Uint8Array(16);
3666
4101
  function sm4(inArray, key, cryptFlag) {
3667
4102
  var options = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {};
3668
- var _options_padding = options.padding, padding = _options_padding === void 0 ? "pkcs#7" : _options_padding, mode = options.mode, _options_iv = options.iv, iv = _options_iv === void 0 ? new Uint8Array(16) : _options_iv, output = options.output;
4103
+ var _options_padding = options.padding, padding = _options_padding === void 0 ? "pkcs#7" : _options_padding, mode = options.mode, _options_iv = options.iv, iv = _options_iv === void 0 ? new Uint8Array(16) : _options_iv, output = options.output, associatedData = options.associatedData, outputTag = options.outputTag, tag = options.tag;
4104
+ if (mode === "gcm") {
4105
+ var keyArray = typeof key === "string" ? hexToArray(key) : Uint8Array.from(key);
4106
+ var ivArray = typeof iv === "string" ? hexToArray(iv) : Uint8Array.from(iv);
4107
+ var aadArray = associatedData ? typeof associatedData === "string" ? hexToArray(associatedData) : Uint8Array.from(associatedData) : new Uint8Array(0);
4108
+ var inputArray;
4109
+ if (typeof inArray === "string") {
4110
+ if (cryptFlag !== DECRYPT) {
4111
+ inputArray = utf8ToArray(inArray);
4112
+ } else {
4113
+ inputArray = hexToArray(inArray);
4114
+ }
4115
+ } else {
4116
+ inputArray = Uint8Array.from(inArray);
4117
+ }
4118
+ var tagArray = tag ? typeof tag === "string" ? hexToArray(tag) : Uint8Array.from(tag) : void 0;
4119
+ var result = sm4Gcm(inputArray, keyArray, ivArray, aadArray, cryptFlag, tagArray);
4120
+ if (output === "array") {
4121
+ if (outputTag && cryptFlag !== DECRYPT) {
4122
+ return result;
4123
+ }
4124
+ return result.output;
4125
+ } else {
4126
+ if (outputTag && cryptFlag !== DECRYPT) {
4127
+ return {
4128
+ output: bytesToHex2(result.output),
4129
+ tag: result.tag ? bytesToHex2(result.tag) : void 0
4130
+ };
4131
+ }
4132
+ if (cryptFlag !== DECRYPT) {
4133
+ return {
4134
+ output: bytesToHex2(result.output),
4135
+ tag: result.tag ? bytesToHex2(result.tag) : void 0
4136
+ };
4137
+ } else {
4138
+ return arrayToUtf8(result.output);
4139
+ }
4140
+ }
4141
+ }
3669
4142
  if (mode === "cbc") {
3670
4143
  if (typeof iv === "string") iv = hexToArray(iv);
3671
4144
  if (iv.length !== 128 / 8) {
@@ -3770,4 +4243,7 @@ function decrypt(inArray, key) {
3770
4243
 
3771
4244
  @noble/curves/esm/abstract/weierstrass.js:
3772
4245
  (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
4246
+
4247
+ @noble/ciphers/esm/utils.js:
4248
+ (*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
3773
4249
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sm-crypto-v2",
3
- "version": "1.9.3",
3
+ "version": "1.10.1",
4
4
  "description": "sm-crypto-v2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -33,8 +33,12 @@
33
33
  "author": "june_01",
34
34
  "license": "MIT",
35
35
  "dependencies": {
36
+ "@noble/ciphers": "^1.2.1",
36
37
  "@noble/curves": "^1.1.0"
37
38
  },
39
+ "publishConfig": {
40
+ "registry": "https://registry.npmjs.org/"
41
+ },
38
42
  "devDependencies": {
39
43
  "@swc-node/register": "^1.6.6",
40
44
  "@swc/core": "^1.3.62",
@@ -10,7 +10,7 @@ export default defineConfig({
10
10
  minify: false,
11
11
  format: ['cjs'],
12
12
  target: 'es5',
13
- noExternal: ['@noble/curves'],
13
+ noExternal: ['@noble/curves', '@noble/ciphers'],
14
14
  tsconfig: 'tsconfig.json',
15
15
  esbuildOptions(options) {
16
16
  options.define.__BUILD_TS__ = Date.now().toString();