Open
Description
Description
There seem to be an issue in the AES-CTR encryption implementation where the counter does not increment, resulting in keystream reuse. This vulnerability can allow an attacker to break the encryption under certain conditions.
Relevant Code
Here is the problematic code snippet where AES-CTR encryption is performed:
export async function encodeDescription(description, finalKeyHex) {
try {
// Convert hex key to WordArray
const keyWords = CryptoJS.enc.Hex.parse(finalKeyHex);
// Generate a 12-byte IV
const ivBytes = new Uint8Array(12);
crypto.getRandomValues(ivBytes); // Note: crypto is assumed to be defined elsewhere
const ivWordArray = CryptoJS.lib.WordArray.create(ivBytes);
// Convert plaintext to WordArray
const plaintextWords = CryptoJS.enc.Utf8.parse(description);
// Encrypt using AES-CTR with no padding
const encrypted = CryptoJS.AES.encrypt(plaintextWords, keyWords, {
iv: ivWordArray,
mode: CryptoJS.mode.CTR,
padding: CryptoJS.pad.NoPadding,
});
// Extract ciphertext as WordArray and convert to Uint8Array
const ciphertextBytes = wordArrayToUint8Array(encrypted.ciphertext);
// Combine IV and ciphertext
const combined = new Uint8Array(ivBytes.length + ciphertextBytes.length);
combined.set(ivBytes, 0);
combined.set(ciphertextBytes, ivBytes.length);
// Convert combined to base64
const combinedString = String.fromCharCode(...combined);
const base64Encoded = CryptoJS.enc.Base64.stringify(
CryptoJS.enc.Utf8.parse(combinedString),
);
return base64Encoded;
} catch (err) {
console.log('ERROR encodeDescription', err);
return '';
}
}
Metadata
Metadata
Assignees
Labels
No labels