ocb2.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /** @fileOverview OCB 2.0 implementation
  2. *
  3. * @author Emily Stark
  4. * @author Mike Hamburg
  5. * @author Dan Boneh
  6. */
  7. /** @namespace
  8. * Phil Rogaway's Offset CodeBook mode, version 2.0.
  9. * May be covered by US and international patents.
  10. *
  11. * @author Emily Stark
  12. * @author Mike Hamburg
  13. * @author Dan Boneh
  14. */
  15. sjcl.mode.ocb2 = {
  16. /** The name of the mode.
  17. * @constant
  18. */
  19. name: "ocb2",
  20. /** Encrypt in OCB mode, version 2.0.
  21. * @param {Object} prp The block cipher. It must have a block size of 16 bytes.
  22. * @param {bitArray} plaintext The plaintext data.
  23. * @param {bitArray} iv The initialization value.
  24. * @param {bitArray} [adata=[]] The authenticated data.
  25. * @param {Number} [tlen=64] the desired tag length, in bits.
  26. * @param [false] premac 1 if the authentication data is pre-macced with PMAC.
  27. * @return The encrypted data, an array of bytes.
  28. * @throws {sjcl.exception.invalid} if the IV isn't exactly 128 bits.
  29. */
  30. encrypt: function(prp, plaintext, iv, adata, tlen, premac) {
  31. if (sjcl.bitArray.bitLength(iv) !== 128) {
  32. throw new sjcl.exception.invalid("ocb iv must be 128 bits");
  33. }
  34. var i,
  35. times2 = sjcl.mode.ocb2._times2,
  36. w = sjcl.bitArray,
  37. xor = w._xor4,
  38. checksum = [0,0,0,0],
  39. delta = times2(prp.encrypt(iv)),
  40. bi, bl,
  41. output = [],
  42. pad;
  43. adata = adata || [];
  44. tlen = tlen || 64;
  45. for (i=0; i+4 < plaintext.length; i+=4) {
  46. /* Encrypt a non-final block */
  47. bi = plaintext.slice(i,i+4);
  48. checksum = xor(checksum, bi);
  49. output = output.concat(xor(delta,prp.encrypt(xor(delta, bi))));
  50. delta = times2(delta);
  51. }
  52. /* Chop out the final block */
  53. bi = plaintext.slice(i);
  54. bl = w.bitLength(bi);
  55. pad = prp.encrypt(xor(delta,[0,0,0,bl]));
  56. bi = w.clamp(xor(bi,pad), bl);
  57. /* Checksum the final block, and finalize the checksum */
  58. checksum = xor(checksum,xor(bi,pad));
  59. checksum = prp.encrypt(xor(checksum,xor(delta,times2(delta))));
  60. /* MAC the header */
  61. if (adata.length) {
  62. checksum = xor(checksum, premac ? adata : sjcl.mode.ocb2.pmac(prp, adata));
  63. }
  64. return output.concat(w.concat(bi, w.clamp(checksum, tlen)));
  65. },
  66. /** Decrypt in OCB mode.
  67. * @param {Object} prp The block cipher. It must have a block size of 16 bytes.
  68. * @param {bitArray} ciphertext The ciphertext data.
  69. * @param {bitArray} iv The initialization value.
  70. * @param {bitArray} [adata=[]] The authenticated data.
  71. * @param {Number} [tlen=64] the desired tag length, in bits.
  72. * @param {boolean} [premac=false] true if the authentication data is pre-macced with PMAC.
  73. * @return The decrypted data, an array of bytes.
  74. * @throws {sjcl.exception.invalid} if the IV isn't exactly 128 bits.
  75. * @throws {sjcl.exception.corrupt} if if the message is corrupt.
  76. */
  77. decrypt: function(prp, ciphertext, iv, adata, tlen, premac) {
  78. if (sjcl.bitArray.bitLength(iv) !== 128) {
  79. throw new sjcl.exception.invalid("ocb iv must be 128 bits");
  80. }
  81. tlen = tlen || 64;
  82. var i,
  83. times2 = sjcl.mode.ocb2._times2,
  84. w = sjcl.bitArray,
  85. xor = w._xor4,
  86. checksum = [0,0,0,0],
  87. delta = times2(prp.encrypt(iv)),
  88. bi, bl,
  89. len = sjcl.bitArray.bitLength(ciphertext) - tlen,
  90. output = [],
  91. pad;
  92. adata = adata || [];
  93. for (i=0; i+4 < len/32; i+=4) {
  94. /* Decrypt a non-final block */
  95. bi = xor(delta, prp.decrypt(xor(delta, ciphertext.slice(i,i+4))));
  96. checksum = xor(checksum, bi);
  97. output = output.concat(bi);
  98. delta = times2(delta);
  99. }
  100. /* Chop out and decrypt the final block */
  101. bl = len-i*32;
  102. pad = prp.encrypt(xor(delta,[0,0,0,bl]));
  103. bi = xor(pad, w.clamp(ciphertext.slice(i),bl));
  104. /* Checksum the final block, and finalize the checksum */
  105. checksum = xor(checksum, bi);
  106. checksum = prp.encrypt(xor(checksum, xor(delta, times2(delta))));
  107. /* MAC the header */
  108. if (adata.length) {
  109. checksum = xor(checksum, premac ? adata : sjcl.mode.ocb2.pmac(prp, adata));
  110. }
  111. if (!w.equal(w.clamp(checksum, tlen), w.bitSlice(ciphertext, len))) {
  112. throw new sjcl.exception.corrupt("ocb: tag doesn't match");
  113. }
  114. return output.concat(w.clamp(bi,bl));
  115. },
  116. /** PMAC authentication for OCB associated data.
  117. * @param {Object} prp The block cipher. It must have a block size of 16 bytes.
  118. * @param {bitArray} adata The authenticated data.
  119. */
  120. pmac: function(prp, adata) {
  121. var i,
  122. times2 = sjcl.mode.ocb2._times2,
  123. w = sjcl.bitArray,
  124. xor = w._xor4,
  125. checksum = [0,0,0,0],
  126. delta = prp.encrypt([0,0,0,0]),
  127. bi;
  128. delta = xor(delta,times2(times2(delta)));
  129. for (i=0; i+4<adata.length; i+=4) {
  130. delta = times2(delta);
  131. checksum = xor(checksum, prp.encrypt(xor(delta, adata.slice(i,i+4))));
  132. }
  133. bi = adata.slice(i);
  134. if (w.bitLength(bi) < 128) {
  135. delta = xor(delta,times2(delta));
  136. bi = w.concat(bi,[0x80000000|0]);
  137. }
  138. checksum = xor(checksum, bi);
  139. return prp.encrypt(xor(times2(xor(delta,times2(delta))), checksum));
  140. },
  141. /** Double a block of words, OCB style.
  142. * @private
  143. */
  144. _times2: function(x) {
  145. return [x[0]<<1 ^ x[1]>>>31,
  146. x[1]<<1 ^ x[2]>>>31,
  147. x[2]<<1 ^ x[3]>>>31,
  148. x[3]<<1 ^ (x[0]>>>31)*0x87];
  149. }
  150. };