aes.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /******************************************************************************
  2. *
  3. * THIS SOURCE CODE IS HEREBY PLACED INTO THE PUBLIC DOMAIN FOR THE GOOD OF ALL
  4. *
  5. * This is a simple and straightforward implementation of the AES Rijndael
  6. * 128-bit block cipher designed by Vincent Rijmen and Joan Daemen. The focus
  7. * of this work was correctness & accuracy. It is written in 'C' without any
  8. * particular focus upon optimization or speed. It should be endian (memory
  9. * byte order) neutral since the few places that care are handled explicitly.
  10. *
  11. * This implementation of Rijndael was created by Steven M. Gibson of GRC.com.
  12. *
  13. * It is intended for general purpose use, but was written in support of GRC's
  14. * reference implementation of the SQRL (Secure Quick Reliable Login) client.
  15. *
  16. * See: http://csrc.nist.gov/archive/aes/rijndael/wsdindex.html
  17. *
  18. * NO COPYRIGHT IS CLAIMED IN THIS WORK, HOWEVER, NEITHER IS ANY WARRANTY MADE
  19. * REGARDING ITS FITNESS FOR ANY PARTICULAR PURPOSE. USE IT AT YOUR OWN RISK.
  20. *
  21. *******************************************************************************/
  22. #include "aes.h"
  23. static int aes_tables_inited = 0; // run-once flag for performing key
  24. // expasion table generation (see below)
  25. /*
  26. * The following static local tables must be filled-in before the first use of
  27. * the GCM or AES ciphers. They are used for the AES key expansion/scheduling
  28. * and once built are read-only and thread safe. The "gcm_initialize" function
  29. * must be called once during system initialization to populate these arrays
  30. * for subsequent use by the AES key scheduler. If they have not been built
  31. * before attempted use, an error will be returned to the caller.
  32. *
  33. * NOTE: GCM Encryption/Decryption does NOT REQUIRE AES decryption. Since
  34. * GCM uses AES in counter-mode, where the AES cipher output is XORed with
  35. * the GCM input, we ONLY NEED AES encryption. Thus, to save space AES
  36. * decryption is typically disabled by setting AES_DECRYPTION to 0 in aes.h.
  37. */
  38. // We always need our forward tables
  39. static uchar FSb[256]; // Forward substitution box (FSb)
  40. static uint32_t FT0[256]; // Forward key schedule assembly tables
  41. static uint32_t FT1[256];
  42. static uint32_t FT2[256];
  43. static uint32_t FT3[256];
  44. #if AES_DECRYPTION // We ONLY need reverse for decryption
  45. static uchar RSb[256]; // Reverse substitution box (RSb)
  46. static uint32_t RT0[256]; // Reverse key schedule assembly tables
  47. static uint32_t RT1[256];
  48. static uint32_t RT2[256];
  49. static uint32_t RT3[256];
  50. #endif /* AES_DECRYPTION */
  51. static uint32_t RCON[10]; // AES round constants
  52. /*
  53. * Platform Endianness Neutralizing Load and Store Macro definitions
  54. * AES wants platform-neutral Little Endian (LE) byte ordering
  55. */
  56. #define GET_UINT32_LE(n, b, i) \
  57. { \
  58. (n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | \
  59. ((uint32_t)(b)[(i) + 2] << 16) | ((uint32_t)(b)[(i) + 3] << 24); \
  60. }
  61. #define PUT_UINT32_LE(n, b, i) \
  62. { \
  63. (b)[(i)] = (uchar)((n)); \
  64. (b)[(i) + 1] = (uchar)((n) >> 8); \
  65. (b)[(i) + 2] = (uchar)((n) >> 16); \
  66. (b)[(i) + 3] = (uchar)((n) >> 24); \
  67. }
  68. /*
  69. * AES forward and reverse encryption round processing macros
  70. */
  71. #define AES_FROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3) \
  72. { \
  73. X0 = *RK++ ^ FT0[(Y0) & 0xFF] ^ FT1[(Y1 >> 8) & 0xFF] ^ FT2[(Y2 >> 16) & 0xFF] ^ \
  74. FT3[(Y3 >> 24) & 0xFF]; \
  75. \
  76. X1 = *RK++ ^ FT0[(Y1) & 0xFF] ^ FT1[(Y2 >> 8) & 0xFF] ^ FT2[(Y3 >> 16) & 0xFF] ^ \
  77. FT3[(Y0 >> 24) & 0xFF]; \
  78. \
  79. X2 = *RK++ ^ FT0[(Y2) & 0xFF] ^ FT1[(Y3 >> 8) & 0xFF] ^ FT2[(Y0 >> 16) & 0xFF] ^ \
  80. FT3[(Y1 >> 24) & 0xFF]; \
  81. \
  82. X3 = *RK++ ^ FT0[(Y3) & 0xFF] ^ FT1[(Y0 >> 8) & 0xFF] ^ FT2[(Y1 >> 16) & 0xFF] ^ \
  83. FT3[(Y2 >> 24) & 0xFF]; \
  84. }
  85. #define AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3) \
  86. { \
  87. X0 = *RK++ ^ RT0[(Y0) & 0xFF] ^ RT1[(Y3 >> 8) & 0xFF] ^ RT2[(Y2 >> 16) & 0xFF] ^ \
  88. RT3[(Y1 >> 24) & 0xFF]; \
  89. \
  90. X1 = *RK++ ^ RT0[(Y1) & 0xFF] ^ RT1[(Y0 >> 8) & 0xFF] ^ RT2[(Y3 >> 16) & 0xFF] ^ \
  91. RT3[(Y2 >> 24) & 0xFF]; \
  92. \
  93. X2 = *RK++ ^ RT0[(Y2) & 0xFF] ^ RT1[(Y1 >> 8) & 0xFF] ^ RT2[(Y0 >> 16) & 0xFF] ^ \
  94. RT3[(Y3 >> 24) & 0xFF]; \
  95. \
  96. X3 = *RK++ ^ RT0[(Y3) & 0xFF] ^ RT1[(Y2 >> 8) & 0xFF] ^ RT2[(Y1 >> 16) & 0xFF] ^ \
  97. RT3[(Y0 >> 24) & 0xFF]; \
  98. }
  99. /*
  100. * These macros improve the readability of the key
  101. * generation initialization code by collapsing
  102. * repetitive common operations into logical pieces.
  103. */
  104. #define ROTL8(x) ((x << 8) & 0xFFFFFFFF) | (x >> 24)
  105. #define XTIME(x) ((x << 1) ^ ((x & 0x80) ? 0x1B : 0x00))
  106. #define MUL(x, y) ((x && y) ? pow[(log[x] + log[y]) % 255] : 0)
  107. #define MIX(x, y) \
  108. { \
  109. y = ((y << 1) | (y >> 7)) & 0xFF; \
  110. x ^= y; \
  111. }
  112. #define CPY128 \
  113. { \
  114. *RK++ = *SK++; \
  115. *RK++ = *SK++; \
  116. *RK++ = *SK++; \
  117. *RK++ = *SK++; \
  118. }
  119. /******************************************************************************
  120. *
  121. * AES_INIT_KEYGEN_TABLES
  122. *
  123. * Fills the AES key expansion tables allocated above with their static
  124. * data. This is not "per key" data, but static system-wide read-only
  125. * table data. THIS FUNCTION IS NOT THREAD SAFE. It must be called once
  126. * at system initialization to setup the tables for all subsequent use.
  127. *
  128. ******************************************************************************/
  129. void aes_init_keygen_tables(void) {
  130. int i, x, y, z; // general purpose iteration and computation locals
  131. int pow[256];
  132. int log[256];
  133. if(aes_tables_inited) return;
  134. // fill the 'pow' and 'log' tables over GF(2^8)
  135. for(i = 0, x = 1; i < 256; i++) {
  136. pow[i] = x;
  137. log[x] = i;
  138. x = (x ^ XTIME(x)) & 0xFF;
  139. }
  140. // compute the round constants
  141. for(i = 0, x = 1; i < 10; i++) {
  142. RCON[i] = (uint32_t)x;
  143. x = XTIME(x) & 0xFF;
  144. }
  145. // fill the forward and reverse substitution boxes
  146. FSb[0x00] = 0x63;
  147. #if AES_DECRYPTION // whether AES decryption is supported
  148. RSb[0x63] = 0x00;
  149. #endif /* AES_DECRYPTION */
  150. for(i = 1; i < 256; i++) {
  151. x = y = pow[255 - log[i]];
  152. MIX(x, y);
  153. MIX(x, y);
  154. MIX(x, y);
  155. MIX(x, y);
  156. FSb[i] = (uchar)(x ^= 0x63);
  157. #if AES_DECRYPTION // whether AES decryption is supported
  158. RSb[x] = (uchar)i;
  159. #endif /* AES_DECRYPTION */
  160. }
  161. // generate the forward and reverse key expansion tables
  162. for(i = 0; i < 256; i++) {
  163. x = FSb[i];
  164. y = XTIME(x) & 0xFF;
  165. z = (y ^ x) & 0xFF;
  166. FT0[i] = ((uint32_t)y) ^ ((uint32_t)x << 8) ^ ((uint32_t)x << 16) ^ ((uint32_t)z << 24);
  167. FT1[i] = ROTL8(FT0[i]);
  168. FT2[i] = ROTL8(FT1[i]);
  169. FT3[i] = ROTL8(FT2[i]);
  170. #if AES_DECRYPTION // whether AES decryption is supported
  171. x = RSb[i];
  172. RT0[i] = ((uint32_t)MUL(0x0E, x)) ^ ((uint32_t)MUL(0x09, x) << 8) ^
  173. ((uint32_t)MUL(0x0D, x) << 16) ^ ((uint32_t)MUL(0x0B, x) << 24);
  174. RT1[i] = ROTL8(RT0[i]);
  175. RT2[i] = ROTL8(RT1[i]);
  176. RT3[i] = ROTL8(RT2[i]);
  177. #endif /* AES_DECRYPTION */
  178. }
  179. aes_tables_inited = 1; // flag that the tables have been generated
  180. } // to permit subsequent use of the AES cipher
  181. /******************************************************************************
  182. *
  183. * AES_SET_ENCRYPTION_KEY
  184. *
  185. * This is called by 'aes_setkey' when we're establishing a key for
  186. * subsequent encryption. We give it a pointer to the encryption
  187. * context, a pointer to the key, and the key's length in bytes.
  188. * Valid lengths are: 16, 24 or 32 bytes (128, 192, 256 bits).
  189. *
  190. ******************************************************************************/
  191. int aes_set_encryption_key(aes_context* ctx, const uchar* key, uint keysize) {
  192. uint i; // general purpose iteration local
  193. uint32_t* RK = ctx->rk; // initialize our RoundKey buffer pointer
  194. for(i = 0; i < (keysize >> 2); i++) {
  195. GET_UINT32_LE(RK[i], key, i << 2);
  196. }
  197. switch(ctx->rounds) {
  198. case 10:
  199. for(i = 0; i < 10; i++, RK += 4) {
  200. RK[4] = RK[0] ^ RCON[i] ^ ((uint32_t)FSb[(RK[3] >> 8) & 0xFF]) ^
  201. ((uint32_t)FSb[(RK[3] >> 16) & 0xFF] << 8) ^
  202. ((uint32_t)FSb[(RK[3] >> 24) & 0xFF] << 16) ^
  203. ((uint32_t)FSb[(RK[3]) & 0xFF] << 24);
  204. RK[5] = RK[1] ^ RK[4];
  205. RK[6] = RK[2] ^ RK[5];
  206. RK[7] = RK[3] ^ RK[6];
  207. }
  208. break;
  209. case 12:
  210. for(i = 0; i < 8; i++, RK += 6) {
  211. RK[6] = RK[0] ^ RCON[i] ^ ((uint32_t)FSb[(RK[5] >> 8) & 0xFF]) ^
  212. ((uint32_t)FSb[(RK[5] >> 16) & 0xFF] << 8) ^
  213. ((uint32_t)FSb[(RK[5] >> 24) & 0xFF] << 16) ^
  214. ((uint32_t)FSb[(RK[5]) & 0xFF] << 24);
  215. RK[7] = RK[1] ^ RK[6];
  216. RK[8] = RK[2] ^ RK[7];
  217. RK[9] = RK[3] ^ RK[8];
  218. RK[10] = RK[4] ^ RK[9];
  219. RK[11] = RK[5] ^ RK[10];
  220. }
  221. break;
  222. case 14:
  223. for(i = 0; i < 7; i++, RK += 8) {
  224. RK[8] = RK[0] ^ RCON[i] ^ ((uint32_t)FSb[(RK[7] >> 8) & 0xFF]) ^
  225. ((uint32_t)FSb[(RK[7] >> 16) & 0xFF] << 8) ^
  226. ((uint32_t)FSb[(RK[7] >> 24) & 0xFF] << 16) ^
  227. ((uint32_t)FSb[(RK[7]) & 0xFF] << 24);
  228. RK[9] = RK[1] ^ RK[8];
  229. RK[10] = RK[2] ^ RK[9];
  230. RK[11] = RK[3] ^ RK[10];
  231. RK[12] = RK[4] ^ ((uint32_t)FSb[(RK[11]) & 0xFF]) ^
  232. ((uint32_t)FSb[(RK[11] >> 8) & 0xFF] << 8) ^
  233. ((uint32_t)FSb[(RK[11] >> 16) & 0xFF] << 16) ^
  234. ((uint32_t)FSb[(RK[11] >> 24) & 0xFF] << 24);
  235. RK[13] = RK[5] ^ RK[12];
  236. RK[14] = RK[6] ^ RK[13];
  237. RK[15] = RK[7] ^ RK[14];
  238. }
  239. break;
  240. default:
  241. return -1;
  242. }
  243. return (0);
  244. }
  245. #if AES_DECRYPTION // whether AES decryption is supported
  246. /******************************************************************************
  247. *
  248. * AES_SET_DECRYPTION_KEY
  249. *
  250. * This is called by 'aes_setkey' when we're establishing a
  251. * key for subsequent decryption. We give it a pointer to
  252. * the encryption context, a pointer to the key, and the key's
  253. * length in bits. Valid lengths are: 128, 192, or 256 bits.
  254. *
  255. ******************************************************************************/
  256. int aes_set_decryption_key(aes_context* ctx, const uchar* key, uint keysize) {
  257. int i, j;
  258. aes_context cty; // a calling aes context for set_encryption_key
  259. uint32_t* RK = ctx->rk; // initialize our RoundKey buffer pointer
  260. uint32_t* SK;
  261. int ret;
  262. cty.rounds = ctx->rounds; // initialize our local aes context
  263. cty.rk = cty.buf; // round count and key buf pointer
  264. if((ret = aes_set_encryption_key(&cty, key, keysize)) != 0) return (ret);
  265. SK = cty.rk + cty.rounds * 4;
  266. CPY128 // copy a 128-bit block from *SK to *RK
  267. for(i = ctx->rounds - 1, SK -= 8; i > 0; i--, SK -= 8) {
  268. for(j = 0; j < 4; j++, SK++) {
  269. *RK++ = RT0[FSb[(*SK) & 0xFF]] ^ RT1[FSb[(*SK >> 8) & 0xFF]] ^
  270. RT2[FSb[(*SK >> 16) & 0xFF]] ^ RT3[FSb[(*SK >> 24) & 0xFF]];
  271. }
  272. }
  273. CPY128 // copy a 128-bit block from *SK to *RK
  274. memset(&cty, 0, sizeof(aes_context)); // clear local aes context
  275. return (0);
  276. }
  277. #endif /* AES_DECRYPTION */
  278. /******************************************************************************
  279. *
  280. * AES_SETKEY
  281. *
  282. * Invoked to establish the key schedule for subsequent encryption/decryption
  283. *
  284. ******************************************************************************/
  285. int aes_setkey(
  286. aes_context* ctx, // AES context provided by our caller
  287. int mode, // ENCRYPT or DECRYPT flag
  288. const uchar* key, // pointer to the key
  289. uint keysize) // key length in bytes
  290. {
  291. // since table initialization is not thread safe, we could either add
  292. // system-specific mutexes and init the AES key generation tables on
  293. // demand, or ask the developer to simply call "gcm_initialize" once during
  294. // application startup before threading begins. That's what we choose.
  295. if(!aes_tables_inited) return (-1); // fail the call when not inited.
  296. ctx->mode = mode; // capture the key type we're creating
  297. ctx->rk = ctx->buf; // initialize our round key pointer
  298. switch(keysize) // set the rounds count based upon the keysize
  299. {
  300. case 16:
  301. ctx->rounds = 10;
  302. break; // 16-byte, 128-bit key
  303. case 24:
  304. ctx->rounds = 12;
  305. break; // 24-byte, 192-bit key
  306. case 32:
  307. ctx->rounds = 14;
  308. break; // 32-byte, 256-bit key
  309. default:
  310. return (-1);
  311. }
  312. #if AES_DECRYPTION
  313. if(mode == DECRYPT) // expand our key for encryption or decryption
  314. return (aes_set_decryption_key(ctx, key, keysize));
  315. else /* ENCRYPT */
  316. #endif /* AES_DECRYPTION */
  317. return (aes_set_encryption_key(ctx, key, keysize));
  318. }
  319. /******************************************************************************
  320. *
  321. * AES_CIPHER
  322. *
  323. * Perform AES encryption and decryption.
  324. * The AES context will have been setup with the encryption mode
  325. * and all keying information appropriate for the task.
  326. *
  327. ******************************************************************************/
  328. int aes_cipher(aes_context* ctx, const uchar input[16], uchar output[16]) {
  329. int i;
  330. uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; // general purpose locals
  331. RK = ctx->rk;
  332. GET_UINT32_LE(X0, input, 0);
  333. X0 ^= *RK++; // load our 128-bit
  334. GET_UINT32_LE(X1, input, 4);
  335. X1 ^= *RK++; // input buffer in a storage
  336. GET_UINT32_LE(X2, input, 8);
  337. X2 ^= *RK++; // memory endian-neutral way
  338. GET_UINT32_LE(X3, input, 12);
  339. X3 ^= *RK++;
  340. #if AES_DECRYPTION // whether AES decryption is supported
  341. if(ctx->mode == DECRYPT) {
  342. for(i = (ctx->rounds >> 1) - 1; i > 0; i--) {
  343. AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
  344. AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3);
  345. }
  346. AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
  347. X0 = *RK++ ^ ((uint32_t)RSb[(Y0) & 0xFF]) ^ ((uint32_t)RSb[(Y3 >> 8) & 0xFF] << 8) ^
  348. ((uint32_t)RSb[(Y2 >> 16) & 0xFF] << 16) ^ ((uint32_t)RSb[(Y1 >> 24) & 0xFF] << 24);
  349. X1 = *RK++ ^ ((uint32_t)RSb[(Y1) & 0xFF]) ^ ((uint32_t)RSb[(Y0 >> 8) & 0xFF] << 8) ^
  350. ((uint32_t)RSb[(Y3 >> 16) & 0xFF] << 16) ^ ((uint32_t)RSb[(Y2 >> 24) & 0xFF] << 24);
  351. X2 = *RK++ ^ ((uint32_t)RSb[(Y2) & 0xFF]) ^ ((uint32_t)RSb[(Y1 >> 8) & 0xFF] << 8) ^
  352. ((uint32_t)RSb[(Y0 >> 16) & 0xFF] << 16) ^ ((uint32_t)RSb[(Y3 >> 24) & 0xFF] << 24);
  353. X3 = *RK++ ^ ((uint32_t)RSb[(Y3) & 0xFF]) ^ ((uint32_t)RSb[(Y2 >> 8) & 0xFF] << 8) ^
  354. ((uint32_t)RSb[(Y1 >> 16) & 0xFF] << 16) ^ ((uint32_t)RSb[(Y0 >> 24) & 0xFF] << 24);
  355. } else /* ENCRYPT */
  356. {
  357. #endif /* AES_DECRYPTION */
  358. for(i = (ctx->rounds >> 1) - 1; i > 0; i--) {
  359. AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
  360. AES_FROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3);
  361. }
  362. AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
  363. X0 = *RK++ ^ ((uint32_t)FSb[(Y0) & 0xFF]) ^ ((uint32_t)FSb[(Y1 >> 8) & 0xFF] << 8) ^
  364. ((uint32_t)FSb[(Y2 >> 16) & 0xFF] << 16) ^ ((uint32_t)FSb[(Y3 >> 24) & 0xFF] << 24);
  365. X1 = *RK++ ^ ((uint32_t)FSb[(Y1) & 0xFF]) ^ ((uint32_t)FSb[(Y2 >> 8) & 0xFF] << 8) ^
  366. ((uint32_t)FSb[(Y3 >> 16) & 0xFF] << 16) ^ ((uint32_t)FSb[(Y0 >> 24) & 0xFF] << 24);
  367. X2 = *RK++ ^ ((uint32_t)FSb[(Y2) & 0xFF]) ^ ((uint32_t)FSb[(Y3 >> 8) & 0xFF] << 8) ^
  368. ((uint32_t)FSb[(Y0 >> 16) & 0xFF] << 16) ^ ((uint32_t)FSb[(Y1 >> 24) & 0xFF] << 24);
  369. X3 = *RK++ ^ ((uint32_t)FSb[(Y3) & 0xFF]) ^ ((uint32_t)FSb[(Y0 >> 8) & 0xFF] << 8) ^
  370. ((uint32_t)FSb[(Y1 >> 16) & 0xFF] << 16) ^ ((uint32_t)FSb[(Y2 >> 24) & 0xFF] << 24);
  371. #if AES_DECRYPTION // whether AES decryption is supported
  372. }
  373. #endif /* AES_DECRYPTION */
  374. PUT_UINT32_LE(X0, output, 0);
  375. PUT_UINT32_LE(X1, output, 4);
  376. PUT_UINT32_LE(X2, output, 8);
  377. PUT_UINT32_LE(X3, output, 12);
  378. return (0);
  379. }
  380. /* end of aes.c */