@bsv/sdk 1.9.24 → 1.9.30

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.
Files changed (75) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/primitives/AESGCM.js +160 -76
  3. package/dist/cjs/src/primitives/AESGCM.js.map +1 -1
  4. package/dist/cjs/src/primitives/ECDSA.js +22 -23
  5. package/dist/cjs/src/primitives/ECDSA.js.map +1 -1
  6. package/dist/cjs/src/primitives/Point.js +102 -22
  7. package/dist/cjs/src/primitives/Point.js.map +1 -1
  8. package/dist/cjs/src/primitives/PrivateKey.js +2 -2
  9. package/dist/cjs/src/primitives/PrivateKey.js.map +1 -1
  10. package/dist/cjs/src/primitives/PublicKey.js +1 -1
  11. package/dist/cjs/src/primitives/PublicKey.js.map +1 -1
  12. package/dist/cjs/src/primitives/SymmetricKey.js +20 -19
  13. package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -1
  14. package/dist/cjs/src/primitives/hex.js +1 -3
  15. package/dist/cjs/src/primitives/hex.js.map +1 -1
  16. package/dist/cjs/src/primitives/utils.js +10 -0
  17. package/dist/cjs/src/primitives/utils.js.map +1 -1
  18. package/dist/cjs/src/totp/totp.js +3 -1
  19. package/dist/cjs/src/totp/totp.js.map +1 -1
  20. package/dist/cjs/src/wallet/ProtoWallet.js +4 -2
  21. package/dist/cjs/src/wallet/ProtoWallet.js.map +1 -1
  22. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  23. package/dist/esm/src/primitives/AESGCM.js +158 -75
  24. package/dist/esm/src/primitives/AESGCM.js.map +1 -1
  25. package/dist/esm/src/primitives/ECDSA.js +22 -23
  26. package/dist/esm/src/primitives/ECDSA.js.map +1 -1
  27. package/dist/esm/src/primitives/Point.js +102 -22
  28. package/dist/esm/src/primitives/Point.js.map +1 -1
  29. package/dist/esm/src/primitives/PrivateKey.js +2 -2
  30. package/dist/esm/src/primitives/PrivateKey.js.map +1 -1
  31. package/dist/esm/src/primitives/PublicKey.js +1 -1
  32. package/dist/esm/src/primitives/PublicKey.js.map +1 -1
  33. package/dist/esm/src/primitives/SymmetricKey.js +20 -19
  34. package/dist/esm/src/primitives/SymmetricKey.js.map +1 -1
  35. package/dist/esm/src/primitives/hex.js +1 -3
  36. package/dist/esm/src/primitives/hex.js.map +1 -1
  37. package/dist/esm/src/primitives/utils.js +9 -0
  38. package/dist/esm/src/primitives/utils.js.map +1 -1
  39. package/dist/esm/src/totp/totp.js +3 -1
  40. package/dist/esm/src/totp/totp.js.map +1 -1
  41. package/dist/esm/src/wallet/ProtoWallet.js +4 -2
  42. package/dist/esm/src/wallet/ProtoWallet.js.map +1 -1
  43. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  44. package/dist/types/src/primitives/AESGCM.d.ts +59 -9
  45. package/dist/types/src/primitives/AESGCM.d.ts.map +1 -1
  46. package/dist/types/src/primitives/ECDSA.d.ts.map +1 -1
  47. package/dist/types/src/primitives/Point.d.ts +2 -0
  48. package/dist/types/src/primitives/Point.d.ts.map +1 -1
  49. package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -1
  50. package/dist/types/src/primitives/hex.d.ts.map +1 -1
  51. package/dist/types/src/primitives/utils.d.ts +1 -0
  52. package/dist/types/src/primitives/utils.d.ts.map +1 -1
  53. package/dist/types/src/totp/totp.d.ts.map +1 -1
  54. package/dist/types/src/wallet/ProtoWallet.d.ts.map +1 -1
  55. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  56. package/dist/umd/bundle.js +3 -3
  57. package/dist/umd/bundle.js.map +1 -1
  58. package/docs/reference/primitives.md +206 -60
  59. package/package.json +1 -1
  60. package/src/primitives/AESGCM.ts +225 -103
  61. package/src/primitives/ECDSA.ts +25 -23
  62. package/src/primitives/Point.ts +142 -23
  63. package/src/primitives/PrivateKey.ts +2 -2
  64. package/src/primitives/PublicKey.ts +1 -1
  65. package/src/primitives/SymmetricKey.ts +28 -20
  66. package/src/primitives/__tests/AESGCM.test.ts +254 -354
  67. package/src/primitives/__tests/ECDSA.test.ts +39 -0
  68. package/src/primitives/__tests/Point.test.ts +112 -0
  69. package/src/primitives/__tests/utils.test.ts +24 -1
  70. package/src/primitives/hex.ts +1 -3
  71. package/src/primitives/utils.ts +10 -0
  72. package/src/totp/__tests/totp.test.ts +21 -0
  73. package/src/totp/totp.ts +9 -1
  74. package/src/wallet/ProtoWallet.ts +8 -3
  75. package/src/wallet/__tests/ProtoWallet.test.ts +55 -34
@@ -68,503 +68,335 @@ describe('AES', () => {
68
68
 
69
69
  describe('ghash', () => {
70
70
  it('should ghash', () => {
71
- expect(toArray('f38cbb1ad69223dcc3457ae5b6b0f885', 'hex')).toEqual(
72
- ghash(
73
- toArray(
74
- '000000000000000000000000000000000388dace60b6a392f328c2b971b2fe780000000000000000000000' +
75
- '0000000080',
76
- 'hex'
77
- ),
78
- toArray('66e94bd4ef8a2c3b884cfa59ca342b2e', 'hex')
71
+ const input = new Uint8Array(
72
+ toArray(
73
+ '000000000000000000000000000000000388dace60b6a392f328c2b971b2fe780000000000000000000000' +
74
+ '0000000080',
75
+ 'hex'
79
76
  )
80
77
  )
78
+ const h = new Uint8Array(
79
+ toArray('66e94bd4ef8a2c3b884cfa59ca342b2e', 'hex')
80
+ )
81
+ const out = ghash(input, h)
82
+
83
+ expect(toArray('f38cbb1ad69223dcc3457ae5b6b0f885', 'hex')).toEqual(
84
+ Array.from(out)
85
+ )
81
86
  })
82
87
  })
83
88
 
84
89
  describe('AESGCM', () => {
85
90
  it('should encrypt: Test Case 1', () => {
86
- const output = AESGCM(
87
- [],
88
- [],
89
- toArray('000000000000000000000000', 'hex'),
91
+ const plainText = new Uint8Array(0)
92
+ const iv = new Uint8Array(
93
+ toArray('000000000000000000000000', 'hex')
94
+ )
95
+ const key = new Uint8Array(
90
96
  toArray('00000000000000000000000000000000', 'hex')
91
97
  )
92
98
 
93
- expect([]).toEqual(output.result)
99
+ const output = AESGCM(plainText, iv, key)
100
+
101
+ expect([]).toEqual(Array.from(output.result))
94
102
  expect(toArray('58e2fccefa7e3061367f1d57a4e7455a', 'hex')).toEqual(
95
- output.authenticationTag
103
+ Array.from(output.authenticationTag)
96
104
  )
97
105
  })
98
106
 
99
107
  it('should encrypt: Test Case 2', () => {
100
- const output = AESGCM(
101
- toArray('00000000000000000000000000000000', 'hex'),
102
- [],
103
- toArray('000000000000000000000000', 'hex'),
108
+ const plainText = new Uint8Array(
109
+ toArray('00000000000000000000000000000000', 'hex')
110
+ )
111
+ const iv = new Uint8Array(
112
+ toArray('000000000000000000000000', 'hex')
113
+ )
114
+ const key = new Uint8Array(
104
115
  toArray('00000000000000000000000000000000', 'hex')
105
116
  )
106
117
 
118
+ const output = AESGCM(plainText, iv, key)
119
+
107
120
  expect(toArray('0388dace60b6a392f328c2b971b2fe78', 'hex')).toEqual(
108
- output.result
121
+ Array.from(output.result)
109
122
  )
110
123
  expect(toArray('ab6e47d42cec13bdf53a67b21257bddf', 'hex')).toEqual(
111
- output.authenticationTag
124
+ Array.from(output.authenticationTag)
112
125
  )
113
126
  })
114
127
 
115
128
  it('should encrypt: Test Case 3', () => {
116
- const output = AESGCM(
129
+ const plainText = new Uint8Array(
117
130
  toArray(
118
131
  'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
119
132
  '809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255',
120
133
  'hex'
121
- ),
122
- [],
123
- toArray('cafebabefacedbaddecaf888', 'hex'),
124
- toArray('feffe9928665731c6d6a8f9467308308', 'hex')
125
- )
126
-
127
- expect(
128
- toArray(
129
- '42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8' +
130
- 'f6a5aac84aa051ba30b396a0aac973d58e091473f5985',
131
- 'hex'
132
134
  )
133
- ).toEqual(output.result)
134
- expect(toArray('4d5c2af327cd64a62cf35abd2ba6fab4', 'hex')).toEqual(
135
- output.authenticationTag
136
- )
137
- })
138
-
139
- it('should encrypt: Test Case 4', () => {
140
- const output = AESGCM(
141
- toArray(
142
- 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
143
- '809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
144
- 'hex'
145
- ),
146
- toArray('feedfacedeadbeeffeedfacedeadbeefabaddad2', 'hex'),
147
- toArray('cafebabefacedbaddecaf888', 'hex'),
148
- toArray('feffe9928665731c6d6a8f9467308308', 'hex')
149
135
  )
150
- expect(
151
- toArray(
152
- '42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8' +
153
- 'f6a5aac84aa051ba30b396a0aac973d58e091',
154
- 'hex'
155
- )
156
- ).toEqual(output.result)
157
- expect(toArray('5bc94fbc3221a5db94fae95ae7121a47', 'hex')).toEqual(
158
- output.authenticationTag
136
+ const iv = new Uint8Array(
137
+ toArray('cafebabefacedbaddecaf888', 'hex')
159
138
  )
160
- })
161
-
162
- it('should encrypt: Test Case 5', () => {
163
- const output = AESGCM(
164
- toArray(
165
- 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
166
- '809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
167
- 'hex'
168
- ),
169
- toArray('feedfacedeadbeeffeedfacedeadbeefabaddad2', 'hex'),
170
- toArray('cafebabefacedbad', 'hex'),
139
+ const key = new Uint8Array(
171
140
  toArray('feffe9928665731c6d6a8f9467308308', 'hex')
172
141
  )
173
142
 
174
- expect(
175
- toArray(
176
- '61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b0' +
177
- '97544d4896b424989b5e1ebac0f07c23f4598',
178
- 'hex'
179
- )
180
- ).toEqual(output.result)
181
- expect(toArray('3612d2e79e3b0785561be14aaca2fccb', 'hex')).toEqual(
182
- output.authenticationTag
183
- )
184
- })
185
-
186
- it('should encrypt: Test Case 6', () => {
187
- const output = AESGCM(
188
- toArray(
189
- 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
190
- '809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
191
- 'hex'
192
- ),
193
- toArray('feedfacedeadbeeffeedfacedeadbeefabaddad2', 'hex'),
194
- toArray(
195
- '9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416' +
196
- 'aedbf5a0de6a57a637b39b',
197
- 'hex'
198
- ),
199
- toArray('feffe9928665731c6d6a8f9467308308', 'hex')
200
- )
143
+ const output = AESGCM(plainText, iv, key)
201
144
 
202
145
  expect(
203
146
  toArray(
204
- '8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccd' +
205
- 'cb281d48c7c6fd62875d2aca417034c34aee5',
147
+ '42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8' +
148
+ 'f6a5aac84aa051ba30b396a0aac973d58e091473f5985',
206
149
  'hex'
207
150
  )
208
- ).toEqual(output.result)
209
- expect(toArray('619cc5aefffe0bfa462af43c1699d050', 'hex')).toEqual(
210
- output.authenticationTag
151
+ ).toEqual(Array.from(output.result))
152
+ expect(toArray('4d5c2af327cd64a62cf35abd2ba6fab4', 'hex')).toEqual(
153
+ Array.from(output.authenticationTag)
211
154
  )
212
155
  })
213
156
 
214
157
  it('should encrypt: Test Case 7', () => {
215
- const output = AESGCM(
216
- [],
217
- [],
218
- toArray('000000000000000000000000', 'hex'),
158
+ const plainText = new Uint8Array(0)
159
+ const iv = new Uint8Array(
160
+ toArray('000000000000000000000000', 'hex')
161
+ )
162
+ const key = new Uint8Array(
219
163
  toArray('000000000000000000000000000000000000000000000000', 'hex')
220
164
  )
221
165
 
222
- expect([]).toEqual(output.result)
166
+ const output = AESGCM(plainText, iv, key)
167
+
168
+ expect([]).toEqual(Array.from(output.result))
223
169
  expect(toArray('cd33b28ac773f74ba00ed1f312572435', 'hex')).toEqual(
224
- output.authenticationTag
170
+ Array.from(output.authenticationTag)
225
171
  )
226
172
  })
227
173
 
228
174
  it('should encrypt: Test Case 8', () => {
229
- const output = AESGCM(
230
- toArray('00000000000000000000000000000000', 'hex'),
231
- [],
232
- toArray('000000000000000000000000', 'hex'),
175
+ const plainText = new Uint8Array(
176
+ toArray('00000000000000000000000000000000', 'hex')
177
+ )
178
+ const iv = new Uint8Array(
179
+ toArray('000000000000000000000000', 'hex')
180
+ )
181
+ const key = new Uint8Array(
233
182
  toArray('000000000000000000000000000000000000000000000000', 'hex')
234
183
  )
235
184
 
185
+ const output = AESGCM(plainText, iv, key)
186
+
236
187
  expect(toArray('98e7247c07f0fe411c267e4384b0f600', 'hex')).toEqual(
237
- output.result
188
+ Array.from(output.result)
238
189
  )
239
190
  expect(toArray('2ff58d80033927ab8ef4d4587514f0fb', 'hex')).toEqual(
240
- output.authenticationTag
191
+ Array.from(output.authenticationTag)
241
192
  )
242
193
  })
243
194
 
244
195
  it('should encrypt: Test Case 9', () => {
245
- const output = AESGCM(
196
+ const plainText = new Uint8Array(
246
197
  toArray(
247
198
  'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
248
199
  '809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255',
249
200
  'hex'
250
- ),
251
- [],
252
- toArray('cafebabefacedbaddecaf888', 'hex'),
253
- toArray('feffe9928665731c6d6a8f9467308308feffe9928665731c', 'hex')
254
- )
255
-
256
- expect(
257
- toArray(
258
- '3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac6' +
259
- '19d18c84a3f4718e2448b2fe324d9ccda2710acade256',
260
- 'hex'
261
201
  )
262
- ).toEqual(output.result)
263
- expect(toArray('9924a7c8587336bfb118024db8674a14', 'hex')).toEqual(
264
- output.authenticationTag
265
202
  )
266
- })
267
-
268
- it('should encrypt: Test Case 10', () => {
269
- const output = AESGCM(
270
- toArray(
271
- 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
272
- '809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
273
- 'hex'
274
- ),
275
- toArray('feedfacedeadbeeffeedfacedeadbeefabaddad2', 'hex'),
276
- toArray('cafebabefacedbaddecaf888', 'hex'),
277
- toArray('feffe9928665731c6d6a8f9467308308feffe9928665731c', 'hex')
203
+ const iv = new Uint8Array(
204
+ toArray('cafebabefacedbaddecaf888', 'hex')
278
205
  )
279
-
280
- expect(
281
- toArray(
282
- '3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac6' +
283
- '19d18c84a3f4718e2448b2fe324d9ccda2710',
284
- 'hex'
285
- )
286
- ).toEqual(output.result)
287
- expect(toArray('2519498e80f1478f37ba55bd6d27618c', 'hex')).toEqual(
288
- output.authenticationTag
289
- )
290
- })
291
-
292
- it('should encrypt: Test Case 11', () => {
293
- const output = AESGCM(
294
- toArray(
295
- 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
296
- '809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
297
- 'hex'
298
- ),
299
- toArray('feedfacedeadbeeffeedfacedeadbeefabaddad2', 'hex'),
300
- toArray('cafebabefacedbad', 'hex'),
206
+ const key = new Uint8Array(
301
207
  toArray('feffe9928665731c6d6a8f9467308308feffe9928665731c', 'hex')
302
208
  )
303
209
 
304
- expect(
305
- toArray(
306
- '0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66' +
307
- '541d4d4dad1c9e93a19a58e8b473fa0f062f7',
308
- 'hex'
309
- )
310
- ).toEqual(output.result)
311
- expect(toArray('65dcc57fcf623a24094fcca40d3533f8', 'hex')).toEqual(
312
- output.authenticationTag
313
- )
314
- })
315
-
316
- it('should encrypt: Test Case 12', () => {
317
- const output = AESGCM(
318
- toArray(
319
- 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
320
- '809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
321
- 'hex'
322
- ),
323
- toArray('feedfacedeadbeeffeedfacedeadbeefabaddad2', 'hex'),
324
- toArray(
325
- '9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b5254' +
326
- '16aedbf5a0de6a57a637b39b',
327
- 'hex'
328
- ),
329
- toArray('feffe9928665731c6d6a8f9467308308feffe9928665731c', 'hex')
330
- )
210
+ const output = AESGCM(plainText, iv, key)
331
211
 
332
212
  expect(
333
213
  toArray(
334
- 'd27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f' +
335
- '037589b292db3e67c036745fa22e7e9b7373b',
214
+ '3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac6' +
215
+ '19d18c84a3f4718e2448b2fe324d9ccda2710acade256',
336
216
  'hex'
337
217
  )
338
- ).toEqual(output.result)
339
- expect(toArray('dcf566ff291c25bbb8568fc3d376a6d9', 'hex')).toEqual(
340
- output.authenticationTag
218
+ ).toEqual(Array.from(output.result))
219
+ expect(toArray('9924a7c8587336bfb118024db8674a14', 'hex')).toEqual(
220
+ Array.from(output.authenticationTag)
341
221
  )
342
222
  })
343
223
 
344
224
  it('should encrypt: Test Case 13', () => {
345
- const output = AESGCM(
346
- [],
347
- [],
348
- toArray('000000000000000000000000', 'hex'),
225
+ const plainText = new Uint8Array(0)
226
+ const iv = new Uint8Array(
227
+ toArray('000000000000000000000000', 'hex')
228
+ )
229
+ const key = new Uint8Array(
349
230
  toArray(
350
231
  '0000000000000000000000000000000000000000000000000000000000000000',
351
232
  'hex'
352
233
  )
353
234
  )
354
235
 
355
- expect([]).toEqual(output.result)
236
+ const output = AESGCM(plainText, iv, key)
237
+
238
+ expect([]).toEqual(Array.from(output.result))
356
239
  expect(toArray('530f8afbc74536b9a963b4f1c4cb738b', 'hex')).toEqual(
357
- output.authenticationTag
240
+ Array.from(output.authenticationTag)
358
241
  )
359
242
  })
360
243
 
361
244
  it('should encrypt: Test Case 14', () => {
362
- const output = AESGCM(
363
- toArray('00000000000000000000000000000000', 'hex'),
364
- [],
365
- toArray('000000000000000000000000', 'hex'),
245
+ const plainText = new Uint8Array(
246
+ toArray('00000000000000000000000000000000', 'hex')
247
+ )
248
+ const iv = new Uint8Array(
249
+ toArray('000000000000000000000000', 'hex')
250
+ )
251
+ const key = new Uint8Array(
366
252
  toArray(
367
253
  '0000000000000000000000000000000000000000000000000000000000000000',
368
254
  'hex'
369
255
  )
370
256
  )
371
257
 
258
+ const output = AESGCM(plainText, iv, key)
259
+
372
260
  expect(toArray('cea7403d4d606b6e074ec5d3baf39d18', 'hex')).toEqual(
373
- output.result
261
+ Array.from(output.result)
374
262
  )
375
263
  expect(toArray('d0d1c8a799996bf0265b98b5d48ab919', 'hex')).toEqual(
376
- output.authenticationTag
264
+ Array.from(output.authenticationTag)
377
265
  )
378
266
  })
379
267
 
380
268
  it('should encrypt: Test Case 15', () => {
381
- const output = AESGCM(
269
+ const plainText = new Uint8Array(
382
270
  toArray(
383
271
  'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
384
272
  '809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255',
385
273
  'hex'
386
- ),
387
- [],
388
- toArray('cafebabefacedbaddecaf888', 'hex'),
389
- toArray(
390
- 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
391
- 'hex'
392
274
  )
393
275
  )
394
-
395
- expect(
396
- toArray(
397
- '522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b' +
398
- '08b1056828838c5f61e6393ba7a0abcc9f662898015ad',
399
- 'hex'
400
- )
401
- ).toEqual(output.result)
402
- expect(toArray('b094dac5d93471bdec1a502270e3cc6c', 'hex')).toEqual(
403
- output.authenticationTag
276
+ const iv = new Uint8Array(
277
+ toArray('cafebabefacedbaddecaf888', 'hex')
404
278
  )
405
- })
406
-
407
- it('should encrypt: Test Case 16', () => {
408
- const output = AESGCM(
409
- toArray(
410
- 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
411
- '809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
412
- 'hex'
413
- ),
414
- toArray('feedfacedeadbeeffeedfacedeadbeefabaddad2', 'hex'),
415
- toArray('cafebabefacedbaddecaf888', 'hex'),
279
+ const key = new Uint8Array(
416
280
  toArray(
417
281
  'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
418
282
  'hex'
419
283
  )
420
284
  )
421
285
 
422
- expect(
423
- toArray(
424
- '522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b' +
425
- '08b1056828838c5f61e6393ba7a0abcc9f662',
426
- 'hex'
427
- )
428
- ).toEqual(output.result)
429
- expect(toArray('76fc6ece0f4e1768cddf8853bb2d551b', 'hex')).toEqual(
430
- output.authenticationTag
431
- )
432
- })
433
-
434
- it('should encrypt: Test Case 17', () => {
435
- const output = AESGCM(
436
- toArray(
437
- 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
438
- '809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
439
- 'hex'
440
- ),
441
- toArray('feedfacedeadbeeffeedfacedeadbeefabaddad2', 'hex'),
442
- toArray('cafebabefacedbad', 'hex'),
443
- toArray(
444
- 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
445
- 'hex'
446
- )
447
- )
286
+ const output = AESGCM(plainText, iv, key)
448
287
 
449
288
  expect(
450
289
  toArray(
451
- 'c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954' +
452
- 'cc2363bc73f7862ac430e64abe499f47c9b1f',
453
- 'hex'
454
- )
455
- ).toEqual(output.result)
456
- expect(toArray('3a337dbf46a792c45e454913fe2ea8f2', 'hex')).toEqual(
457
- output.authenticationTag
458
- )
459
- })
460
-
461
- it('should encrypt: Test Case 18', () => {
462
- const output = AESGCM(
463
- toArray(
464
- 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956' +
465
- '809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
466
- 'hex'
467
- ),
468
- toArray('feedfacedeadbeeffeedfacedeadbeefabaddad2', 'hex'),
469
- toArray(
470
- '9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416' +
471
- 'aedbf5a0de6a57a637b39b',
472
- 'hex'
473
- ),
474
- toArray(
475
- 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
476
- 'hex'
477
- )
478
- )
479
-
480
- expect(
481
- toArray(
482
- '5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da' +
483
- '3ebf1c5d82cdea2418997200ef82e44ae7e3f',
290
+ '522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b' +
291
+ '08b1056828838c5f61e6393ba7a0abcc9f662898015ad',
484
292
  'hex'
485
293
  )
486
- ).toEqual(output.result)
487
- expect(toArray('a44a8266ee1c8eb0c8b5d4cf5ae9f19a', 'hex')).toEqual(
488
- output.authenticationTag
294
+ ).toEqual(Array.from(output.result))
295
+ expect(toArray('b094dac5d93471bdec1a502270e3cc6c', 'hex')).toEqual(
296
+ Array.from(output.authenticationTag)
489
297
  )
490
298
  })
491
299
  })
492
300
 
493
301
  describe('exclusiveOR', () => {
494
302
  it('should exclusiveOR', () => {
303
+ const out1 = exclusiveOR(
304
+ new Uint8Array([
305
+ 0xf0, 0xf8, 0x7f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306
+ 0x00, 0x00, 0x00, 0x00, 0x00
307
+ ]),
308
+ new Uint8Array([0x0f, 0x0f, 0x00, 0xf0])
309
+ )
310
+
495
311
  expect([
496
312
  0xff, 0xf7, 0x7f, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
497
313
  0x00, 0x00, 0x00, 0x00
498
- ]).toEqual(
499
- exclusiveOR(
500
- [
501
- 0xf0, 0xf8, 0x7f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
502
- 0x00, 0x00, 0x00, 0x00, 0x00
503
- ],
504
- [0x0f, 0x0f, 0x00, 0xf0]
505
- )
506
- )
314
+ ]).toEqual(Array.from(out1))
507
315
 
508
- expect([0xff, 0xf7, 0x7f, 0x0f]).toEqual(
509
- exclusiveOR([0xf0, 0xf8, 0x7f, 0xff], [0x0f, 0x0f, 0x00, 0xf0])
316
+ const out2 = exclusiveOR(
317
+ new Uint8Array([0xf0, 0xf8, 0x7f, 0xff]),
318
+ new Uint8Array([0x0f, 0x0f, 0x00, 0xf0])
510
319
  )
320
+
321
+ expect([0xff, 0xf7, 0x7f, 0x0f]).toEqual(Array.from(out2))
511
322
  })
512
323
  })
513
324
 
514
325
  describe('rightShift', () => {
515
326
  it('should rightShift', () => {
327
+ const input = new Uint8Array(
328
+ toArray('7b5b54657374566563746f725d53475d', 'hex')
329
+ )
330
+ const out = rightShift(input)
331
+
516
332
  expect(toArray('3dadaa32b9ba2b32b1ba37b92ea9a3ae', 'hex')).toEqual(
517
- rightShift(toArray('7b5b54657374566563746f725d53475d', 'hex'))
333
+ Array.from(out)
518
334
  )
519
335
  })
520
336
  })
521
337
 
522
338
  describe('multiply', () => {
523
339
  it('should multiply', () => {
340
+ const a = new Uint8Array(
341
+ toArray('952b2a56a5604ac0b32b6656a05b40b6', 'hex')
342
+ )
343
+ const b = new Uint8Array(
344
+ toArray('dfa6bf4ded81db03ffcaff95f830f061', 'hex')
345
+ )
346
+ const out = multiply(a, b)
347
+
524
348
  expect(toArray('da53eb0ad2c55bb64fc4802cc3feda60', 'hex')).toEqual(
525
- multiply(
526
- toArray('952b2a56a5604ac0b32b6656a05b40b6', 'hex'),
527
- toArray('dfa6bf4ded81db03ffcaff95f830f061', 'hex')
528
- )
349
+ Array.from(out)
529
350
  )
530
351
  })
531
352
 
532
353
  it('should commutatively multiply', () => {
533
- expect(
534
- multiply(
535
- toArray('48692853686179295b477565726f6e5d', 'hex'),
536
- toArray('7b5b54657374566563746f725d53475d', 'hex')
537
- )
538
- ).toEqual(
539
- multiply(
540
- toArray('7b5b54657374566563746f725d53475d', 'hex'),
541
- toArray('48692853686179295b477565726f6e5d', 'hex')
542
- )
354
+ const x = new Uint8Array(
355
+ toArray('48692853686179295b477565726f6e5d', 'hex')
543
356
  )
357
+ const y = new Uint8Array(
358
+ toArray('7b5b54657374566563746f725d53475d', 'hex')
359
+ )
360
+
361
+ const out1 = multiply(x, y)
362
+ const out2 = multiply(y, x)
363
+
364
+ expect(Array.from(out1)).toEqual(Array.from(out2))
544
365
  })
545
366
  })
546
367
 
547
368
  describe('incrementLeastSignificantThirtyTwoBits', () => {
548
369
  it('should incrementLeastSignificantThirtyTwoBits', () => {
370
+ const in1 = new Uint8Array(
371
+ toArray('00000000000000000000000000000000', 'hex')
372
+ )
373
+ const out1 = incrementLeastSignificantThirtyTwoBits(in1)
549
374
  expect(toArray('00000000000000000000000000000001', 'hex')).toEqual(
550
- incrementLeastSignificantThirtyTwoBits(
551
- toArray('00000000000000000000000000000000', 'hex')
552
- )
375
+ Array.from(out1)
553
376
  )
377
+
378
+ const in2 = new Uint8Array(
379
+ toArray('000000000000000000000000000000ff', 'hex')
380
+ )
381
+ const out2 = incrementLeastSignificantThirtyTwoBits(in2)
554
382
  expect(toArray('00000000000000000000000000000100', 'hex')).toEqual(
555
- incrementLeastSignificantThirtyTwoBits(
556
- toArray('000000000000000000000000000000ff', 'hex')
557
- )
383
+ Array.from(out2)
558
384
  )
385
+
386
+ const in3 = new Uint8Array(
387
+ toArray('00000000000000000000000000ffffff', 'hex')
388
+ )
389
+ const out3 = incrementLeastSignificantThirtyTwoBits(in3)
559
390
  expect(toArray('00000000000000000000000001000000', 'hex')).toEqual(
560
- incrementLeastSignificantThirtyTwoBits(
561
- toArray('00000000000000000000000000ffffff', 'hex')
562
- )
391
+ Array.from(out3)
563
392
  )
393
+
394
+ const in4 = new Uint8Array(
395
+ toArray('000000000000000000000000ffffffff', 'hex')
396
+ )
397
+ const out4 = incrementLeastSignificantThirtyTwoBits(in4)
564
398
  expect(toArray('00000000000000000000000000000000', 'hex')).toEqual(
565
- incrementLeastSignificantThirtyTwoBits(
566
- toArray('000000000000000000000000ffffffff', 'hex')
567
- )
399
+ Array.from(out4)
568
400
  )
569
401
  })
570
402
  })
@@ -574,7 +406,9 @@ describe('checkBit', () => {
574
406
  let i
575
407
  let j
576
408
  let k = 0
577
- let block = toArray('7b5b54657374566563746f725d53475d', 'hex')
409
+ let block = new Uint8Array(
410
+ toArray('7b5b54657374566563746f725d53475d', 'hex')
411
+ ) as any
578
412
  const expected = [
579
413
  0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0,
580
414
  1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1,
@@ -587,12 +421,12 @@ describe('checkBit', () => {
587
421
 
588
422
  for (i = 0; i < 16; i++) {
589
423
  for (j = 7; j !== -1; j--) {
590
- expect(expected[k++]).toEqual(checkBit(block, i, j))
424
+ expect(expected[k++]).toEqual(checkBit(Array.from(block), i, j))
591
425
  }
592
426
  }
593
427
 
594
428
  for (i = 0; i < 128; i++) {
595
- expect(expectedLSB[i]).toEqual(checkBit(block, 15, 0))
429
+ expect(expectedLSB[i]).toEqual(checkBit(Array.from(block), 15, 0))
596
430
  block = rightShift(block)
597
431
  }
598
432
  })
@@ -645,56 +479,122 @@ describe('getBytes', () => {
645
479
  })
646
480
 
647
481
  describe('AESGCM IV validation', () => {
648
- const key = new Array(16).fill(0x01)
649
- const aad: number[] = []
650
- const plaintext = [1, 2, 3, 4]
482
+ const key = new Uint8Array(new Array(16).fill(0x01))
483
+ const plaintext = new Uint8Array([1, 2, 3, 4])
651
484
 
652
485
  it('AESGCM throws when IV is empty', () => {
653
486
  expect(() => {
654
- AESGCM(plaintext, aad, [], key)
487
+ AESGCM(plaintext, new Uint8Array(), key)
655
488
  }).toThrow(new Error('Initialization vector must not be empty'))
656
489
  })
657
490
 
658
491
  it('AESGCMDecrypt throws when IV is empty', () => {
659
- const iv = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
660
- const { result: ciphertext, authenticationTag } = AESGCM(plaintext, aad, iv, key)
492
+ const iv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
493
+ const { result: ciphertext, authenticationTag } = AESGCM(
494
+ plaintext,
495
+ iv,
496
+ key
497
+ )
661
498
 
662
499
  // Now call decrypt but with an empty IV – this should be rejected
663
500
  expect(() => {
664
- AESGCMDecrypt(ciphertext, aad, [], authenticationTag, key)
501
+ AESGCMDecrypt(ciphertext, new Uint8Array(), authenticationTag, key)
665
502
  }).toThrow(new Error('Initialization vector must not be empty'))
666
503
  })
667
504
 
668
505
  it('AESGCM throws when key is empty', () => {
669
- const iv = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
506
+ const iv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
670
507
 
671
508
  expect(() => {
672
- AESGCM(plaintext, aad, iv, [])
509
+ AESGCM(plaintext, iv, new Uint8Array())
673
510
  }).toThrow(new Error('Key must not be empty'))
674
511
  })
675
512
 
676
513
  it('AESGCMDecrypt throws when key is empty', () => {
677
- const iv = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
678
- const { result: ciphertext, authenticationTag } = AESGCM(plaintext, aad, iv, key)
514
+ const iv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
515
+ const { result: ciphertext, authenticationTag } = AESGCM(
516
+ plaintext,
517
+ iv,
518
+ key
519
+ )
679
520
 
680
521
  expect(() => {
681
- AESGCMDecrypt(ciphertext, aad, iv, authenticationTag, [])
522
+ AESGCMDecrypt(
523
+ ciphertext,
524
+ iv,
525
+ authenticationTag,
526
+ new Uint8Array()
527
+ )
682
528
  }).toThrow(new Error('Key must not be empty'))
683
529
  })
684
530
 
685
531
  it('AESGCMDecrypt throws when cipher text is empty', () => {
686
- const iv = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
532
+ const iv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
687
533
 
688
534
  expect(() => {
689
- AESGCMDecrypt([], aad, iv, [], key)
535
+ AESGCMDecrypt(new Uint8Array(), iv, new Uint8Array(), key)
690
536
  }).toThrow(new Error('Cipher text must not be empty'))
691
537
  })
692
538
 
693
539
  it('AESGCM still work with a valid IV', () => {
694
- const iv = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
695
- const { result: ciphertext, authenticationTag } = AESGCM(plaintext, aad, iv, key)
696
- const decrypted = AESGCMDecrypt(ciphertext, aad, iv, authenticationTag, key)
540
+ const iv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
541
+ const { result: ciphertext, authenticationTag } = AESGCM(
542
+ plaintext,
543
+ iv,
544
+ key
545
+ )
546
+ const decrypted = AESGCMDecrypt(
547
+ ciphertext,
548
+ iv,
549
+ authenticationTag,
550
+ key
551
+ ) as Uint8Array
552
+
553
+ expect(Array.from(decrypted)).toEqual(Array.from(plaintext))
554
+ })
555
+ })
556
+
557
+ function expectUint8ArrayEqual (a: Uint8Array, b: Uint8Array) {
558
+ expect(a.length).toBe(b.length)
697
559
 
698
- expect(decrypted).toEqual(plaintext)
560
+ for (let i = 0; i < a.length; i++) {
561
+ if (a[i] !== b[i]) {
562
+ throw new Error(`mismatch at index ${i}: ${a[i]} !== ${b[i]}`)
563
+ }
564
+ }
565
+ }
566
+
567
+ describe('AESGCM large input (non-mocked)', () => {
568
+ // NOTE: This test is intentionally skipped by default because it allocates
569
+ // ~500MB+ and will be very slow / memory-heavy.
570
+ // Un-skip locally when you want to manually verify behavior for lengths
571
+ // larger than 2^32 bits.
572
+ it.skip('handles ciphertext longer than 2^32 bits', () => {
573
+ const key = new Uint8Array(new Array(16).fill(0x01))
574
+ const iv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
575
+
576
+ // 2^32 bits = 2^29 bytes. Go just beyond that boundary.
577
+ const bigSizeBytes = (1 << 29) + 16 // 2^29 + 16 bytes (> 2^32 bits)
578
+
579
+ // Use a typed array instead of a giant sparse JS array.
580
+ const plaintext = new Uint8Array(bigSizeBytes) // already zero-initialized
581
+
582
+ const { result: ciphertext, authenticationTag } = AESGCM(
583
+ plaintext,
584
+ iv,
585
+ key
586
+ )
587
+
588
+ const decrypted = AESGCMDecrypt(
589
+ ciphertext,
590
+ iv,
591
+ authenticationTag,
592
+ key
593
+ ) as Uint8Array | null
594
+
595
+ expect(decrypted).not.toBeNull()
596
+ const decryptedBytes = decrypted as Uint8Array
597
+ expect(decryptedBytes.length).toBe(bigSizeBytes)
598
+ expectUint8ArrayEqual(decryptedBytes, plaintext)
699
599
  })
700
600
  })