aes.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*!
  2. * Crypto-JS v1.1.0
  3. * http://code.google.com/p/crypto-js/
  4. * Copyright (c) 2009, Jeff Mott. All rights reserved.
  5. * http://code.google.com/p/crypto-js/wiki/License
  6. */
  7. (function(){
  8. // Shortcut
  9. var util = Crypto.util;
  10. // Precomputed SBOX
  11. var SBOX = [ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
  12. 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  13. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
  14. 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  15. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
  16. 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  17. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
  18. 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  19. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
  20. 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  21. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
  22. 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  23. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
  24. 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  25. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
  26. 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  27. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
  28. 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  29. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
  30. 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  31. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
  32. 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  33. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
  34. 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  35. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
  36. 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  37. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
  38. 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  39. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
  40. 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  41. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
  42. 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 ];
  43. // Compute inverse SBOX lookup table
  44. for (var INVSBOX = [], i = 0; i < 256; i++) INVSBOX[SBOX[i]] = i;
  45. // Compute mulitplication in GF(2^8) lookup tables
  46. var MULT2 = [],
  47. MULT3 = [],
  48. MULT9 = [],
  49. MULTB = [],
  50. MULTD = [],
  51. MULTE = [];
  52. function xtime(a, b) {
  53. for (var result = 0, i = 0; i < 8; i++) {
  54. if (b & 1) result ^= a;
  55. var hiBitSet = a & 0x80;
  56. a = (a << 1) & 0xFF;
  57. if (hiBitSet) a ^= 0x1b;
  58. b >>>= 1;
  59. }
  60. return result;
  61. }
  62. for (var i = 0; i < 256; i++) {
  63. MULT2[i] = xtime(i,2);
  64. MULT3[i] = xtime(i,3);
  65. MULT9[i] = xtime(i,9);
  66. MULTB[i] = xtime(i,0xB);
  67. MULTD[i] = xtime(i,0xD);
  68. MULTE[i] = xtime(i,0xE);
  69. }
  70. // Precomputed RCon lookup
  71. var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
  72. // Inner state
  73. var state = [[], [], [], []],
  74. keylength,
  75. nrounds,
  76. keyschedule;
  77. var AES = Crypto.AES = {
  78. /**
  79. * Public API
  80. */
  81. encrypt: function (message, password, mode) {
  82. var
  83. // Convert to bytes
  84. m = util.stringToBytes(message),
  85. // Generate random IV
  86. iv = util.randomBytes(AES._blocksize * 4),
  87. // Generate key
  88. k = Crypto.PBKDF2(password, util.bytesToString(iv), 32, { asBytes: true });
  89. // Determine mode
  90. mode = mode || Crypto.mode.OFB;
  91. // Encrypt
  92. AES._init(k);
  93. mode.encrypt(AES, m, iv);
  94. // Return ciphertext
  95. return util.bytesToBase64(iv.concat(m));
  96. },
  97. decrypt: function (ciphertext, password, mode) {
  98. var
  99. // Convert to bytes
  100. c = util.base64ToBytes(ciphertext),
  101. // Separate IV and message
  102. iv = c.splice(0, AES._blocksize * 4),
  103. // Generate key
  104. k = Crypto.PBKDF2(password, util.bytesToString(iv), 32, { asBytes: true });
  105. // Determine mode
  106. mode = mode || Crypto.mode.OFB;
  107. // Decrypt
  108. AES._init(k);
  109. mode.decrypt(AES, c, iv);
  110. // Return plaintext
  111. return util.bytesToString(c);
  112. },
  113. /**
  114. * Package private methods and properties
  115. */
  116. _blocksize: 4,
  117. _encryptblock: function (m, offset) {
  118. // Set input
  119. for (var row = 0; row < AES._blocksize; row++) {
  120. for (var col = 0; col < 4; col++)
  121. state[row][col] = m[offset + col * 4 + row];
  122. }
  123. // Add round key
  124. for (var row = 0; row < 4; row++) {
  125. for (var col = 0; col < 4; col++)
  126. state[row][col] ^= keyschedule[col][row];
  127. }
  128. for (var round = 1; round < nrounds; round++) {
  129. // Sub bytes
  130. for (var row = 0; row < 4; row++) {
  131. for (var col = 0; col < 4; col++)
  132. state[row][col] = SBOX[state[row][col]];
  133. }
  134. // Shift rows
  135. state[1].push(state[1].shift());
  136. state[2].push(state[2].shift());
  137. state[2].push(state[2].shift());
  138. state[3].unshift(state[3].pop());
  139. // Mix columns
  140. for (var col = 0; col < 4; col++) {
  141. var s0 = state[0][col],
  142. s1 = state[1][col],
  143. s2 = state[2][col],
  144. s3 = state[3][col];
  145. state[0][col] = MULT2[s0] ^ MULT3[s1] ^ s2 ^ s3;
  146. state[1][col] = s0 ^ MULT2[s1] ^ MULT3[s2] ^ s3;
  147. state[2][col] = s0 ^ s1 ^ MULT2[s2] ^ MULT3[s3];
  148. state[3][col] = MULT3[s0] ^ s1 ^ s2 ^ MULT2[s3];
  149. }
  150. // Add round key
  151. for (var row = 0; row < 4; row++) {
  152. for (var col = 0; col < 4; col++)
  153. state[row][col] ^= keyschedule[round * 4 + col][row];
  154. }
  155. }
  156. // Sub bytes
  157. for (var row = 0; row < 4; row++) {
  158. for (var col = 0; col < 4; col++)
  159. state[row][col] = SBOX[state[row][col]];
  160. }
  161. // Shift rows
  162. state[1].push(state[1].shift());
  163. state[2].push(state[2].shift());
  164. state[2].push(state[2].shift());
  165. state[3].unshift(state[3].pop());
  166. // Add round key
  167. for (var row = 0; row < 4; row++) {
  168. for (var col = 0; col < 4; col++)
  169. state[row][col] ^= keyschedule[nrounds * 4 + col][row];
  170. }
  171. // Set output
  172. for (var row = 0; row < AES._blocksize; row++) {
  173. for (var col = 0; col < 4; col++)
  174. m[offset + col * 4 + row] = state[row][col];
  175. }
  176. },
  177. _decryptblock: function (c, offset) {
  178. // Set input
  179. for (var row = 0; row < AES._blocksize; row++) {
  180. for (var col = 0; col < 4; col++)
  181. state[row][col] = c[offset + col * 4 + row];
  182. }
  183. // Add round key
  184. for (var row = 0; row < 4; row++) {
  185. for (var col = 0; col < 4; col++)
  186. state[row][col] ^= keyschedule[nrounds * 4 + col][row];
  187. }
  188. for (var round = 1; round < nrounds; round++) {
  189. // Inv shift rows
  190. state[1].unshift(state[1].pop());
  191. state[2].push(state[2].shift());
  192. state[2].push(state[2].shift());
  193. state[3].push(state[3].shift());
  194. // Inv sub bytes
  195. for (var row = 0; row < 4; row++) {
  196. for (var col = 0; col < 4; col++)
  197. state[row][col] = INVSBOX[state[row][col]];
  198. }
  199. // Add round key
  200. for (var row = 0; row < 4; row++) {
  201. for (var col = 0; col < 4; col++)
  202. state[row][col] ^= keyschedule[(nrounds - round) * 4 + col][row];
  203. }
  204. // Inv mix columns
  205. for (var col = 0; col < 4; col++) {
  206. var s0 = state[0][col],
  207. s1 = state[1][col],
  208. s2 = state[2][col],
  209. s3 = state[3][col];
  210. state[0][col] = MULTE[s0] ^ MULTB[s1] ^ MULTD[s2] ^ MULT9[s3];
  211. state[1][col] = MULT9[s0] ^ MULTE[s1] ^ MULTB[s2] ^ MULTD[s3];
  212. state[2][col] = MULTD[s0] ^ MULT9[s1] ^ MULTE[s2] ^ MULTB[s3];
  213. state[3][col] = MULTB[s0] ^ MULTD[s1] ^ MULT9[s2] ^ MULTE[s3];
  214. }
  215. }
  216. // Inv shift rows
  217. state[1].unshift(state[1].pop());
  218. state[2].push(state[2].shift());
  219. state[2].push(state[2].shift());
  220. state[3].push(state[3].shift());
  221. // Inv sub bytes
  222. for (var row = 0; row < 4; row++) {
  223. for (var col = 0; col < 4; col++)
  224. state[row][col] = INVSBOX[state[row][col]];
  225. }
  226. // Add round key
  227. for (var row = 0; row < 4; row++) {
  228. for (var col = 0; col < 4; col++)
  229. state[row][col] ^= keyschedule[col][row];
  230. }
  231. // Set output
  232. for (var row = 0; row < AES._blocksize; row++) {
  233. for (var col = 0; col < 4; col++)
  234. c[offset + col * 4 + row] = state[row][col];
  235. }
  236. },
  237. /**
  238. * Private methods
  239. */
  240. _init: function (k) {
  241. keylength = k.length / 4;
  242. nrounds = keylength + 6;
  243. AES._keyexpansion(k);
  244. },
  245. // Generate a key schedule
  246. _keyexpansion: function (k) {
  247. keyschedule = [];
  248. for (var row = 0; row < keylength; row++) {
  249. keyschedule[row] = [
  250. k[row * 4],
  251. k[row * 4 + 1],
  252. k[row * 4 + 2],
  253. k[row * 4 + 3]
  254. ];
  255. }
  256. for (var row = keylength; row < AES._blocksize * (nrounds + 1); row++) {
  257. var temp = [
  258. keyschedule[row - 1][0],
  259. keyschedule[row - 1][1],
  260. keyschedule[row - 1][2],
  261. keyschedule[row - 1][3]
  262. ];
  263. if (row % keylength == 0) {
  264. // Rot word
  265. temp.push(temp.shift());
  266. // Sub word
  267. temp[0] = SBOX[temp[0]];
  268. temp[1] = SBOX[temp[1]];
  269. temp[2] = SBOX[temp[2]];
  270. temp[3] = SBOX[temp[3]];
  271. temp[0] ^= RCON[row / keylength];
  272. } else if (keylength > 6 && row % keylength == 4) {
  273. // Sub word
  274. temp[0] = SBOX[temp[0]];
  275. temp[1] = SBOX[temp[1]];
  276. temp[2] = SBOX[temp[2]];
  277. temp[3] = SBOX[temp[3]];
  278. }
  279. keyschedule[row] = [
  280. keyschedule[row - keylength][0] ^ temp[0],
  281. keyschedule[row - keylength][1] ^ temp[1],
  282. keyschedule[row - keylength][2] ^ temp[2],
  283. keyschedule[row - keylength][3] ^ temp[3]
  284. ];
  285. }
  286. }
  287. };
  288. })();