@syncular/crypto 0.32.0 → 0.33.0
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/dist/index.js +11 -2
- package/package.json +1 -1
- package/src/index.ts +14 -2
package/dist/index.js
CHANGED
|
@@ -138,6 +138,15 @@ export async function generateKeyPair(backend = webCryptoX25519) {
|
|
|
138
138
|
* vectors inject fixed ones for determinism).
|
|
139
139
|
*/
|
|
140
140
|
export async function wrapKeyWith(k, recipientPublic, ephemeralPrivate, nonce, backend = webCryptoX25519) {
|
|
141
|
+
if (k.byteLength !== 32) {
|
|
142
|
+
throw new Error('wrapKeyWith requires a 32-byte key');
|
|
143
|
+
}
|
|
144
|
+
if (recipientPublic.byteLength !== 32 || ephemeralPrivate.byteLength !== 32) {
|
|
145
|
+
throw new Error('wrapKeyWith requires 32-byte X25519 keys');
|
|
146
|
+
}
|
|
147
|
+
if (nonce.byteLength !== NONCE_LENGTH) {
|
|
148
|
+
throw new Error(`wrapKeyWith requires a ${NONCE_LENGTH}-byte nonce`);
|
|
149
|
+
}
|
|
141
150
|
const ephemeralPublic = await backend.publicFromPrivate(ephemeralPrivate);
|
|
142
151
|
const shared = await backend.sharedSecret(ephemeralPrivate, recipientPublic);
|
|
143
152
|
const wrapKey = await deriveWrapKey(shared);
|
|
@@ -167,9 +176,9 @@ export async function unwrapKey(envelope, recipientPrivate, backend = webCryptoX
|
|
|
167
176
|
const ephemeralPublic = envelope.subarray(1, 33);
|
|
168
177
|
const nonce = envelope.subarray(33, 33 + NONCE_LENGTH);
|
|
169
178
|
const wrapped = envelope.subarray(33 + NONCE_LENGTH);
|
|
170
|
-
const shared = await backend.sharedSecret(recipientPrivate, ephemeralPublic);
|
|
171
|
-
const wrapKey = await deriveWrapKey(shared);
|
|
172
179
|
try {
|
|
180
|
+
const shared = await backend.sharedSecret(recipientPrivate, ephemeralPublic);
|
|
181
|
+
const wrapKey = await deriveWrapKey(shared);
|
|
173
182
|
return new Uint8Array(await crypto.subtle.decrypt({ name: 'AES-GCM', iv: toBuffer(nonce), tagLength: 128 }, wrapKey, toBuffer(wrapped)));
|
|
174
183
|
}
|
|
175
184
|
catch {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -201,6 +201,15 @@ export async function wrapKeyWith(
|
|
|
201
201
|
nonce: Uint8Array,
|
|
202
202
|
backend: X25519Backend = webCryptoX25519,
|
|
203
203
|
): Promise<Uint8Array> {
|
|
204
|
+
if (k.byteLength !== 32) {
|
|
205
|
+
throw new Error('wrapKeyWith requires a 32-byte key');
|
|
206
|
+
}
|
|
207
|
+
if (recipientPublic.byteLength !== 32 || ephemeralPrivate.byteLength !== 32) {
|
|
208
|
+
throw new Error('wrapKeyWith requires 32-byte X25519 keys');
|
|
209
|
+
}
|
|
210
|
+
if (nonce.byteLength !== NONCE_LENGTH) {
|
|
211
|
+
throw new Error(`wrapKeyWith requires a ${NONCE_LENGTH}-byte nonce`);
|
|
212
|
+
}
|
|
204
213
|
const ephemeralPublic = await backend.publicFromPrivate(ephemeralPrivate);
|
|
205
214
|
const shared = await backend.sharedSecret(ephemeralPrivate, recipientPublic);
|
|
206
215
|
const wrapKey = await deriveWrapKey(shared);
|
|
@@ -248,9 +257,12 @@ export async function unwrapKey(
|
|
|
248
257
|
const ephemeralPublic = envelope.subarray(1, 33);
|
|
249
258
|
const nonce = envelope.subarray(33, 33 + NONCE_LENGTH);
|
|
250
259
|
const wrapped = envelope.subarray(33 + NONCE_LENGTH);
|
|
251
|
-
const shared = await backend.sharedSecret(recipientPrivate, ephemeralPublic);
|
|
252
|
-
const wrapKey = await deriveWrapKey(shared);
|
|
253
260
|
try {
|
|
261
|
+
const shared = await backend.sharedSecret(
|
|
262
|
+
recipientPrivate,
|
|
263
|
+
ephemeralPublic,
|
|
264
|
+
);
|
|
265
|
+
const wrapKey = await deriveWrapKey(shared);
|
|
254
266
|
return new Uint8Array(
|
|
255
267
|
await crypto.subtle.decrypt(
|
|
256
268
|
{ name: 'AES-GCM', iv: toBuffer(nonce), tagLength: 128 },
|