Skip to content

AES CTR Does not increment the nonce when iv is less than 16 bytes #508

Open
@NBFinanceTech

Description

@NBFinanceTech

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions