bitwiseshiftleft / sjcl

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

"ccm: tag doesn't match" when trying to unserialize ECC keys #342

Closed marncz closed 7 years ago

marncz commented 7 years ago

I am getting this error when trying to pass freshly generated ECC key pair to the sjcl.encrypt function:

var pair = sjcl.ecc.elGamal.generateKeys(256);

var ct = sjcl.encrypt(pair.pub, "Hello World!");
var pt = sjcl.decrypt(pair.sec, ct);

Passing strings works fine but not bitArrays, can anyone help me?

Nilos commented 7 years ago

Your code example works for me:

>> node
> var sjcl = require("./sjcl")
undefined
> var pair = sjcl.ecc.elGamal.generateKeys(256);
undefined
> var ct = sjcl.encrypt(pair.pub, "Hello World!");
undefined
> var pt = sjcl.decrypt(pair.sec, ct);
undefined
> ct
'{"iv":"ZmsqcKeq5bRHyGncl8LACQ==","v":1,"iter":10000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","kemtag":"mE1IyzbkLxxoy6xTmxZ1oiKRd07pU3bxLepfp5bWovCNmG0OokXdjvlOQAtNfbdcrTzt/2Ay72JxTwXqydKf4g==","ct":"iyl4vJZByesWGyUh+E3xRhjI32M="}'
> pt
'Hello World!'
>
marncz commented 7 years ago

@Nilos Got past this error but now I am getting tag doesn't match error. Asymmetric works great when using freshly generated pair and using pair.pub and pair.sec to encrypt and decrypt, issue shows up when trying to de-serialize keys using hex codec:

var publicKeyObject = new sjcl.ecc.elGamal.publicKey(
 sjcl.ecc.curves.c256, 
 sjcl.codec.hex.toBits("17a64e2ed16049a00a4d48b5fb4f0498a268030e122540d26a56f4c0d4cc7f492b33dd640b282434c5a749db64a5607bc20b31c8450eb61009e5ee47fb4b6375")
);
var encrypted = sjcl.encrypt(publicKeyObject, "Hello World!");
console.log(encrypted);

var privateKeyObject = new sjcl.ecc.elGamal.secretKey(
 sjcl.ecc.curves.c256,      
 sjcl.ecc.curves.c256.field.fromBits(sjcl.codec.hex.toBits("060a5cdd0a9209281507b57a00f7c32a33fc97afbce3028dd0bbb38b17f8ac8f"))
);

var pt = sjcl.decrypt(privateKeyObject, encrypted);
console.log(pt);