Skip to content

Commit

Permalink
Merge pull request #1819 from uhoreg/fix_aes_iv
Browse files Browse the repository at this point in the history
Only clear bit 63 when we create the IV
  • Loading branch information
uhoreg authored Jul 29, 2021
2 parents 9c05077 + 600438d commit 529fe93
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/crypto/aes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ async function encryptNode(data: string, key: Uint8Array, name: string, ivStr?:
iv = decodeBase64(ivStr);
} else {
iv = crypto.randomBytes(16);
}

// clear bit 63 of the IV to stop us hitting the 64-bit counter boundary
// (which would mean we wouldn't be able to decrypt on Android). The loss
// of a single bit of iv is a price we have to pay.
iv[8] &= 0x7f;
// clear bit 63 of the IV to stop us hitting the 64-bit counter boundary
// (which would mean we wouldn't be able to decrypt on Android). The loss
// of a single bit of iv is a price we have to pay.
iv[8] &= 0x7f;
}

const [aesKey, hmacKey] = deriveKeysNode(key, name);

Expand Down Expand Up @@ -137,12 +137,12 @@ async function encryptBrowser(data: string, key: Uint8Array, name: string, ivStr
} else {
iv = new Uint8Array(16);
window.crypto.getRandomValues(iv);
}

// clear bit 63 of the IV to stop us hitting the 64-bit counter boundary
// (which would mean we wouldn't be able to decrypt on Android). The loss
// of a single bit of iv is a price we have to pay.
iv[8] &= 0x7f;
// clear bit 63 of the IV to stop us hitting the 64-bit counter boundary
// (which would mean we wouldn't be able to decrypt on Android). The loss
// of a single bit of iv is a price we have to pay.
iv[8] &= 0x7f;
}

const [aesKey, hmacKey] = await deriveKeysBrowser(key, name);
const encodedData = new TextEncoder().encode(data);
Expand Down

0 comments on commit 529fe93

Please sign in to comment.