aes.js 8.4 KB

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