ccm_test.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. new sjcl.test.TestCase("CCM mode tests", function (cb) {
  2. if (!sjcl.cipher.aes || !sjcl.mode.ccm) {
  3. this.unimplemented();
  4. cb && cb();
  5. return;
  6. }
  7. var i, kat = sjcl.test.vector.ccm, tv, iv, ct, aes, len, tlen, thiz=this, w=sjcl.bitArray, pt, h=sjcl.codec.hex, ad;
  8. browserUtil.cpsIterate(function (j, cbb) {
  9. for (i=100*j; i<kat.length && i<100*(j+1); i++) {
  10. tv = kat[i];
  11. len = 32 * tv.key.length;
  12. aes = new sjcl.cipher.aes(h.toBits(tv.key));
  13. // Convert from strings
  14. iv = h.toBits(tv.iv);
  15. ad = h.toBits(tv.adata);
  16. pt = h.toBits(tv.pt);
  17. ct = h.toBits(tv.ct + tv.tag);
  18. tlen = tv.tag.length * 4;
  19. thiz.require(w.equal(sjcl.mode.ccm.encrypt(aes, pt, iv, ad, tlen), ct), "aes-"+len+"-ccm-encrypt #"+i);
  20. try {
  21. thiz.require(w.equal(sjcl.mode.ccm.decrypt(aes, ct, iv, ad, tlen), pt), "aes-"+len+"-ccm-decrypt #"+i);
  22. } catch (e) {
  23. thiz.fail("aes-ccm-decrypt #"+i+" (exn "+e+")");
  24. }
  25. }
  26. cbb();
  27. }, 0, kat.length / 100, true, cb);
  28. });