aes.js 6.9 KB

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