-
Notifications
You must be signed in to change notification settings - Fork 108
Description
What's happening?
crypto.subtle.generateKey sometimes returns a key with empty bytes like:
Exported key:
[248, 21, 138, 140, 37, 183, 146, 130, 28, 75, 183, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
if I import the exported key it doesn't work when trying to decrypt:
[Error: error in DoCipher, status: 2]
The issue also happens if I create a key and import it:
(Run it in a loop and it will happen at some point)
const key = crypto.randomBytes(32);
const importedKey = await crypto.subtle.importKey('raw', random, 'AES-CBC', true, ['encrypt', 'decrypt']);
const exportedKey = await crypto.subtle.exportKey('raw', importedKey);
exportedKey !== key (where exportedKey has empty bytes)
Reproducible Code
const test = async () => {
while (true) {
try {
const bytes = crypto.getRandomValues(new Uint8Array(16)).slice().buffer;
const key = await crypto.subtle.generateKey({ name: 'AES-CBC', length: 256 }, true, ['encrypt', 'decrypt']);
const exportedKey = await crypto.subtle.exportKey('raw', key);
console.log('exportedKey', new Uint8Array(exportedKey));
const iv = crypto.getRandomValues(new Uint8Array(16)).slice().buffer;
const encryptedBytes = await crypto.subtle.encrypt({ name: 'AES-CBC', iv }, key, bytes);
const importedBytes = await crypto.subtle.importKey('raw', exportedKey, 'AES-CBC', true, ['encrypt', 'decrypt']);
const decryptedBytes = await crypto.subtle.decrypt({ name: 'AES-CBC', iv }, importedBytes, encryptedBytes);
console.log('decrypted');
} catch (error) {
console.error('error', error);
}
}
};
test();Relevant log output
(NOBRIDGE) LOG exportedKey [139, 104, 250, 170, 146, 5, 138, 209, 222, 67, 13, 12, 38, 235, 208, 183, 121, 249, 135, 188, 235, 187, 98, 126, 229, 170, 0, 0, 0, 0, 0, 0]
(NOBRIDGE) ERROR error [Error: error in DoCipher, status: 2]Device
iPhone 14 Pro (18.1)
QuickCrypto Version
0.7.12
Can you reproduce this issue in the QuickCrypto Example app?
I didn't try (
Additional information
- I am using Expo
- I have read the Troubleshooting Guide
- I agree to follow this project's Code of Conduct
- I searched for similar issues in this repository and found none.