|
3 | 3 | const { |
4 | 4 | PromiseWithResolvers, |
5 | 5 | SafeSet, |
| 6 | + TypedArrayPrototypeGetBuffer, |
| 7 | + TypedArrayPrototypeSet, |
6 | 8 | Uint8Array, |
7 | 9 | } = primordials; |
8 | 10 |
|
@@ -90,35 +92,30 @@ function mlKemExportKey(key, format) { |
90 | 92 | switch (format) { |
91 | 93 | case kWebCryptoKeyFormatRaw: { |
92 | 94 | if (key[kKeyType] === 'private') { |
93 | | - return key[kKeyObject][kHandle].rawSeed().buffer; |
| 95 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawSeed()); |
94 | 96 | } |
95 | 97 |
|
96 | | - return key[kKeyObject][kHandle].rawPublicKey().buffer; |
| 98 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].rawPublicKey()); |
97 | 99 | } |
98 | 100 | case kWebCryptoKeyFormatSPKI: { |
99 | | - return key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI).buffer; |
| 101 | + return TypedArrayPrototypeGetBuffer(key[kKeyObject][kHandle].export(kKeyFormatDER, kWebCryptoKeyFormatSPKI)); |
100 | 102 | } |
101 | 103 | case kWebCryptoKeyFormatPKCS8: { |
102 | 104 | const seed = key[kKeyObject][kHandle].rawSeed(); |
103 | 105 | const buffer = new Uint8Array(86); |
104 | | - buffer.set([ |
105 | | - 0x30, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0B, 0x06, |
| 106 | + const orc = { |
| 107 | + '__proto__': null, |
| 108 | + 'ML-KEM-512': 0x01, |
| 109 | + 'ML-KEM-768': 0x02, |
| 110 | + 'ML-KEM-1024': 0x03, |
| 111 | + }[key[kAlgorithm].name]; |
| 112 | + TypedArrayPrototypeSet(buffer, [ |
| 113 | + 0x30, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06, |
106 | 114 | 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, |
107 | | - 0x04, 0x00, 0x04, 0x42, 0x80, 0x40, |
| 115 | + 0x04, orc, 0x04, 0x42, 0x80, 0x40, |
108 | 116 | ], 0); |
109 | | - switch (key[kAlgorithm].name) { |
110 | | - case 'ML-KEM-512': |
111 | | - buffer.set([0x01], 17); |
112 | | - break; |
113 | | - case 'ML-KEM-768': |
114 | | - buffer.set([0x02], 17); |
115 | | - break; |
116 | | - case 'ML-KEM-1024': |
117 | | - buffer.set([0x03], 17); |
118 | | - break; |
119 | | - } |
120 | | - buffer.set(seed, 22); |
121 | | - return buffer.buffer; |
| 117 | + TypedArrayPrototypeSet(buffer, seed, 22); |
| 118 | + return TypedArrayPrototypeGetBuffer(buffer); |
122 | 119 | } |
123 | 120 | default: |
124 | 121 | return undefined; |
@@ -241,7 +238,11 @@ function mlKemEncapsulate(encapsulationKey) { |
241 | 238 | { name: 'OperationError', cause: error })); |
242 | 239 | } else { |
243 | 240 | const { 0: sharedKey, 1: ciphertext } = result; |
244 | | - resolve({ sharedKey: sharedKey.buffer, ciphertext: ciphertext.buffer }); |
| 241 | + |
| 242 | + resolve({ |
| 243 | + sharedKey: TypedArrayPrototypeGetBuffer(sharedKey), |
| 244 | + ciphertext: TypedArrayPrototypeGetBuffer(ciphertext), |
| 245 | + }); |
245 | 246 | } |
246 | 247 | }; |
247 | 248 | job.run(); |
@@ -270,7 +271,7 @@ function mlKemDecapsulate(decapsulationKey, ciphertext) { |
270 | 271 | 'The operation failed for an operation-specific reason', |
271 | 272 | { name: 'OperationError', cause: error })); |
272 | 273 | } else { |
273 | | - resolve(result.buffer); |
| 274 | + resolve(TypedArrayPrototypeGetBuffer(result)); |
274 | 275 | } |
275 | 276 | }; |
276 | 277 | job.run(); |
|
0 commit comments