aes.js 9.0 KB

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