aes.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. CryptoJS v3.1.2
  3. code.google.com/p/crypto-js
  4. (c) 2009-2013 by Jeff Mott. All rights reserved.
  5. code.google.com/p/crypto-js/wiki/License
  6. */
  7. (function () {
  8. // Shortcuts
  9. var C = CryptoJS;
  10. var C_lib = C.lib;
  11. var BlockCipher = C_lib.BlockCipher;
  12. var C_algo = C.algo;
  13. // Lookup tables
  14. var SBOX = [];
  15. var INV_SBOX = [];
  16. var SUB_MIX_0 = [];
  17. var SUB_MIX_1 = [];
  18. var SUB_MIX_2 = [];
  19. var SUB_MIX_3 = [];
  20. var INV_SUB_MIX_0 = [];
  21. var INV_SUB_MIX_1 = [];
  22. var INV_SUB_MIX_2 = [];
  23. var INV_SUB_MIX_3 = [];
  24. // Compute lookup tables
  25. (function () {
  26. // Compute double table
  27. var d = [];
  28. for (var i = 0; i < 256; i++) {
  29. if (i < 128) {
  30. d[i] = i << 1;
  31. } else {
  32. d[i] = (i << 1) ^ 0x11b;
  33. }
  34. }
  35. // Walk GF(2^8)
  36. var x = 0;
  37. var xi = 0;
  38. for (var i = 0; i < 256; i++) {
  39. // Compute sbox
  40. var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
  41. sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
  42. SBOX[x] = sx;
  43. INV_SBOX[sx] = x;
  44. // Compute multiplication
  45. var x2 = d[x];
  46. var x4 = d[x2];
  47. var x8 = d[x4];
  48. // Compute sub bytes, mix columns tables
  49. var t = (d[sx] * 0x101) ^ (sx * 0x1010100);
  50. SUB_MIX_0[x] = (t << 24) | (t >>> 8);
  51. SUB_MIX_1[x] = (t << 16) | (t >>> 16);
  52. SUB_MIX_2[x] = (t << 8) | (t >>> 24);
  53. SUB_MIX_3[x] = t;
  54. // Compute inv sub bytes, inv mix columns tables
  55. var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
  56. INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8);
  57. INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16);
  58. INV_SUB_MIX_2[sx] = (t << 8) | (t >>> 24);
  59. INV_SUB_MIX_3[sx] = t;
  60. // Compute next counter
  61. if (!x) {
  62. x = xi = 1;
  63. } else {
  64. x = x2 ^ d[d[d[x8 ^ x2]]];
  65. xi ^= d[d[xi]];
  66. }
  67. }
  68. }());
  69. // Precomputed Rcon lookup
  70. var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
  71. /**
  72. * AES block cipher algorithm.
  73. */
  74. var AES = C_algo.AES = BlockCipher.extend({
  75. _doReset: function () {
  76. // Shortcuts
  77. var key = this._key;
  78. var keyWords = key.words;
  79. var keySize = key.sigBytes / 4;
  80. // Compute number of rounds
  81. var nRounds = this._nRounds = keySize + 6
  82. // Compute number of key schedule rows
  83. var ksRows = (nRounds + 1) * 4;
  84. // Compute key schedule
  85. var keySchedule = this._keySchedule = [];
  86. for (var ksRow = 0; ksRow < ksRows; ksRow++) {
  87. if (ksRow < keySize) {
  88. keySchedule[ksRow] = keyWords[ksRow];
  89. } else {
  90. var t = keySchedule[ksRow - 1];
  91. if (!(ksRow % keySize)) {
  92. // Rot word
  93. t = (t << 8) | (t >>> 24);
  94. // Sub word
  95. t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
  96. // Mix Rcon
  97. t ^= RCON[(ksRow / keySize) | 0] << 24;
  98. } else if (keySize > 6 && ksRow % keySize == 4) {
  99. // Sub word
  100. t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
  101. }
  102. keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t;
  103. }
  104. }
  105. // Compute inv key schedule
  106. var invKeySchedule = this._invKeySchedule = [];
  107. for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) {
  108. var ksRow = ksRows - invKsRow;
  109. if (invKsRow % 4) {
  110. var t = keySchedule[ksRow];
  111. } else {
  112. var t = keySchedule[ksRow - 4];
  113. }
  114. if (invKsRow < 4 || ksRow <= 4) {
  115. invKeySchedule[invKsRow] = t;
  116. } else {
  117. invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^
  118. INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]];
  119. }
  120. }
  121. },
  122. encryptBlock: function (M, offset) {
  123. this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX);
  124. },
  125. decryptBlock: function (M, offset) {
  126. // Swap 2nd and 4th rows
  127. var t = M[offset + 1];
  128. M[offset + 1] = M[offset + 3];
  129. M[offset + 3] = t;
  130. this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX);
  131. // Inv swap 2nd and 4th rows
  132. var t = M[offset + 1];
  133. M[offset + 1] = M[offset + 3];
  134. M[offset + 3] = t;
  135. },
  136. _doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) {
  137. // Shortcut
  138. var nRounds = this._nRounds;
  139. // Get input, add round key
  140. var s0 = M[offset] ^ keySchedule[0];
  141. var s1 = M[offset + 1] ^ keySchedule[1];
  142. var s2 = M[offset + 2] ^ keySchedule[2];
  143. var s3 = M[offset + 3] ^ keySchedule[3];
  144. // Key schedule row counter
  145. var ksRow = 4;
  146. // Rounds
  147. for (var round = 1; round < nRounds; round++) {
  148. // Shift rows, sub bytes, mix columns, add round key
  149. var t0 = SUB_MIX_0[s0 >>> 24] ^ SUB_MIX_1[(s1 >>> 16) & 0xff] ^ SUB_MIX_2[(s2 >>> 8) & 0xff] ^ SUB_MIX_3[s3 & 0xff] ^ keySchedule[ksRow++];
  150. var t1 = SUB_MIX_0[s1 >>> 24] ^ SUB_MIX_1[(s2 >>> 16) & 0xff] ^ SUB_MIX_2[(s3 >>> 8) & 0xff] ^ SUB_MIX_3[s0 & 0xff] ^ keySchedule[ksRow++];
  151. var t2 = SUB_MIX_0[s2 >>> 24] ^ SUB_MIX_1[(s3 >>> 16) & 0xff] ^ SUB_MIX_2[(s0 >>> 8) & 0xff] ^ SUB_MIX_3[s1 & 0xff] ^ keySchedule[ksRow++];
  152. var t3 = SUB_MIX_0[s3 >>> 24] ^ SUB_MIX_1[(s0 >>> 16) & 0xff] ^ SUB_MIX_2[(s1 >>> 8) & 0xff] ^ SUB_MIX_3[s2 & 0xff] ^ keySchedule[ksRow++];
  153. // Update state
  154. s0 = t0;
  155. s1 = t1;
  156. s2 = t2;
  157. s3 = t3;
  158. }
  159. // Shift rows, sub bytes, add round key
  160. var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
  161. var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
  162. var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
  163. var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
  164. // Set output
  165. M[offset] = t0;
  166. M[offset + 1] = t1;
  167. M[offset + 2] = t2;
  168. M[offset + 3] = t3;
  169. },
  170. keySize: 256/32
  171. });
  172. /**
  173. * Shortcut functions to the cipher's object interface.
  174. *
  175. * @example
  176. *
  177. * var ciphertext = CryptoJS.AES.encrypt(message, key, cfg);
  178. * var plaintext = CryptoJS.AES.decrypt(ciphertext, key, cfg);
  179. */
  180. C.AES = BlockCipher._createHelper(AES);
  181. }());