bitwiseshiftleft / sjcl

Stanford Javascript Crypto Library
http://bitwiseshiftleft.github.com/sjcl/
Other
7.18k stars 986 forks source link

Simple decryption test going wrong with CCM #328

Closed ghost closed 7 years ago

ghost commented 7 years ago

This simple code:

var aesCipher = new sjcl.cipher.aes(sjcl.hash.sha256.hash([1,2,3,4]));
var encryptedECKey = sjcl.codec.base64.fromBits(sjcl.mode.ccm.encrypt(aesCipher, sjcl.codec.base32hex.toBits("AAAA"), Array.from([1,2,3,4])));
console.log(sjcl.mode.ccm.decrypt(aesCipher, sjcl.codec.base64.toBits(encryptedECKey), Array.from([1,2,3,4])));

Returns the error: "ccm: tag doesn't match", despite having the exact same input for all fields

ghost commented 7 years ago

For anyone else who comes across this, I've temporarily solved this problem by switching to GCM over CCM

Nilos commented 7 years ago

@Dacnomanie I formatted your code for better readability.

ghost commented 7 years ago

@Nilos Thanks, how do I do that next time?

Nilos commented 7 years ago

See https://guides.github.com/features/mastering-markdown/#examples just select the code example. Basically add three backticks (`) before and after your code example.

Nilos commented 7 years ago

Should have seen this earlier but after some digging I now know what happens :)

The problem is in your usage of aes/ccm mode. CCM Mode requires at least one full block but you are not giving it one, which from my point of view is the reason it fails.

If you change the code to:

var aesCipher = new sjcl.cipher.aes(sjcl.hash.sha256.hash([1,2,3,4]));
var encryptedECKey = sjcl.codec.base64.fromBits(sjcl.mode.ccm.encrypt(aesCipher, sjcl.codec.base32hex.toBits("AAAAAAAA"), Array.from([1,2,3,4])));
console.log(sjcl.mode.ccm.decrypt(aesCipher, sjcl.codec.base64.toBits(encryptedECKey), Array.from([1,2,3,4])));

it works (I just increased the value from AAAA to AAAAAAAA)