ocb2_test.js 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. new sjcl.test.TestCase("OCB 2.0 mode tests", function (cb) {
  2. if (!sjcl.cipher.aes || !sjcl.mode.ocb2) {
  3. this.unimplemented();
  4. cb && cb();
  5. return;
  6. }
  7. var i, kat = sjcl.test.vector.ocb2, tv, iv, ct, aes, len, tlen, thiz=this, w=sjcl.bitArray, pt, iv, h = sjcl.codec.hex, ad, tlen;
  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. // sort of a hack because of the format of the vectors file
  14. pt = h.toBits(tv.pt);
  15. ct = h.toBits(tv.ct+tv.tag);
  16. iv = h.toBits(tv.iv);
  17. ad = h.toBits(tv.adata);
  18. tlen = tv.tag.length * 4;
  19. thiz.require(w.equal(sjcl.mode.ocb2.encrypt(aes, pt, iv, ad, tlen), ct), "aes-"+len+"-ocb2-encrypt #"+i);
  20. try {
  21. thiz.require(w.equal(sjcl.mode.ocb2.decrypt(aes, ct, iv, ad, tlen), pt), "aes-"+len+"-ocb2-decrypt #"+i);
  22. } catch (e) {
  23. thiz.fail("aes-ocb-decrypt #"+i+" (exn " + e + ")");
  24. }
  25. }
  26. cbb();
  27. }, 0, kat.length / 100, true, cb);
  28. });