AES.js 9.8 KB

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