aes.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. (n) = ( (uint32_t) (b)[(i) ] ) \
  58. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  59. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  60. | ( (uint32_t) (b)[(i) + 3] << 24 ); }
  61. #define PUT_UINT32_LE(n,b,i) { \
  62. (b)[(i) ] = (uchar) ( (n) ); \
  63. (b)[(i) + 1] = (uchar) ( (n) >> 8 ); \
  64. (b)[(i) + 2] = (uchar) ( (n) >> 16 ); \
  65. (b)[(i) + 3] = (uchar) ( (n) >> 24 ); }
  66. /*
  67. * AES forward and reverse encryption round processing macros
  68. */
  69. #define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
  70. { \
  71. X0 = *RK++ ^ FT0[ ( Y0 ) & 0xFF ] ^ \
  72. FT1[ ( Y1 >> 8 ) & 0xFF ] ^ \
  73. FT2[ ( Y2 >> 16 ) & 0xFF ] ^ \
  74. FT3[ ( Y3 >> 24 ) & 0xFF ]; \
  75. \
  76. X1 = *RK++ ^ FT0[ ( Y1 ) & 0xFF ] ^ \
  77. FT1[ ( Y2 >> 8 ) & 0xFF ] ^ \
  78. FT2[ ( Y3 >> 16 ) & 0xFF ] ^ \
  79. FT3[ ( Y0 >> 24 ) & 0xFF ]; \
  80. \
  81. X2 = *RK++ ^ FT0[ ( Y2 ) & 0xFF ] ^ \
  82. FT1[ ( Y3 >> 8 ) & 0xFF ] ^ \
  83. FT2[ ( Y0 >> 16 ) & 0xFF ] ^ \
  84. FT3[ ( Y1 >> 24 ) & 0xFF ]; \
  85. \
  86. X3 = *RK++ ^ FT0[ ( Y3 ) & 0xFF ] ^ \
  87. FT1[ ( Y0 >> 8 ) & 0xFF ] ^ \
  88. FT2[ ( Y1 >> 16 ) & 0xFF ] ^ \
  89. FT3[ ( Y2 >> 24 ) & 0xFF ]; \
  90. }
  91. #define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
  92. { \
  93. X0 = *RK++ ^ RT0[ ( Y0 ) & 0xFF ] ^ \
  94. RT1[ ( Y3 >> 8 ) & 0xFF ] ^ \
  95. RT2[ ( Y2 >> 16 ) & 0xFF ] ^ \
  96. RT3[ ( Y1 >> 24 ) & 0xFF ]; \
  97. \
  98. X1 = *RK++ ^ RT0[ ( Y1 ) & 0xFF ] ^ \
  99. RT1[ ( Y0 >> 8 ) & 0xFF ] ^ \
  100. RT2[ ( Y3 >> 16 ) & 0xFF ] ^ \
  101. RT3[ ( Y2 >> 24 ) & 0xFF ]; \
  102. \
  103. X2 = *RK++ ^ RT0[ ( Y2 ) & 0xFF ] ^ \
  104. RT1[ ( Y1 >> 8 ) & 0xFF ] ^ \
  105. RT2[ ( Y0 >> 16 ) & 0xFF ] ^ \
  106. RT3[ ( Y3 >> 24 ) & 0xFF ]; \
  107. \
  108. X3 = *RK++ ^ RT0[ ( Y3 ) & 0xFF ] ^ \
  109. RT1[ ( Y2 >> 8 ) & 0xFF ] ^ \
  110. RT2[ ( Y1 >> 16 ) & 0xFF ] ^ \
  111. RT3[ ( Y0 >> 24 ) & 0xFF ]; \
  112. }
  113. /*
  114. * These macros improve the readability of the key
  115. * generation initialization code by collapsing
  116. * repetitive common operations into logical pieces.
  117. */
  118. #define ROTL8(x) ( ( x << 8 ) & 0xFFFFFFFF ) | ( x >> 24 )
  119. #define XTIME(x) ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) )
  120. #define MUL(x,y) ( ( x && y ) ? pow[(log[x]+log[y]) % 255] : 0 )
  121. #define MIX(x,y) { y = ( (y << 1) | (y >> 7) ) & 0xFF; x ^= y; }
  122. #define CPY128 { *RK++ = *SK++; *RK++ = *SK++; \
  123. *RK++ = *SK++; *RK++ = *SK++; }
  124. /******************************************************************************
  125. *
  126. * AES_INIT_KEYGEN_TABLES
  127. *
  128. * Fills the AES key expansion tables allocated above with their static
  129. * data. This is not "per key" data, but static system-wide read-only
  130. * table data. THIS FUNCTION IS NOT THREAD SAFE. It must be called once
  131. * at system initialization to setup the tables for all subsequent use.
  132. *
  133. ******************************************************************************/
  134. void aes_init_keygen_tables( void )
  135. {
  136. int i, x, y, z; // general purpose iteration and computation locals
  137. int pow[256];
  138. int log[256];
  139. if (aes_tables_inited) return;
  140. // fill the 'pow' and 'log' tables over GF(2^8)
  141. for( i = 0, x = 1; i < 256; i++ ) {
  142. pow[i] = x;
  143. log[x] = i;
  144. x = ( x ^ XTIME( x ) ) & 0xFF;
  145. }
  146. // compute the round constants
  147. for( i = 0, x = 1; i < 10; i++ ) {
  148. RCON[i] = (uint32_t) x;
  149. x = XTIME( x ) & 0xFF;
  150. }
  151. // fill the forward and reverse substitution boxes
  152. FSb[0x00] = 0x63;
  153. #if AES_DECRYPTION // whether AES decryption is supported
  154. RSb[0x63] = 0x00;
  155. #endif /* AES_DECRYPTION */
  156. for( i = 1; i < 256; i++ ) {
  157. x = y = pow[255 - log[i]];
  158. MIX(x,y);
  159. MIX(x,y);
  160. MIX(x,y);
  161. MIX(x,y);
  162. FSb[i] = (uchar) ( x ^= 0x63 );
  163. #if AES_DECRYPTION // whether AES decryption is supported
  164. RSb[x] = (uchar) i;
  165. #endif /* AES_DECRYPTION */
  166. }
  167. // generate the forward and reverse key expansion tables
  168. for( i = 0; i < 256; i++ ) {
  169. x = FSb[i];
  170. y = XTIME( x ) & 0xFF;
  171. z = ( y ^ x ) & 0xFF;
  172. FT0[i] = ( (uint32_t) y ) ^ ( (uint32_t) x << 8 ) ^
  173. ( (uint32_t) x << 16 ) ^ ( (uint32_t) z << 24 );
  174. FT1[i] = ROTL8( FT0[i] );
  175. FT2[i] = ROTL8( FT1[i] );
  176. FT3[i] = ROTL8( FT2[i] );
  177. #if AES_DECRYPTION // whether AES decryption is supported
  178. x = RSb[i];
  179. RT0[i] = ( (uint32_t) MUL( 0x0E, x ) ) ^
  180. ( (uint32_t) MUL( 0x09, x ) << 8 ) ^
  181. ( (uint32_t) MUL( 0x0D, x ) << 16 ) ^
  182. ( (uint32_t) MUL( 0x0B, x ) << 24 );
  183. RT1[i] = ROTL8( RT0[i] );
  184. RT2[i] = ROTL8( RT1[i] );
  185. RT3[i] = ROTL8( RT2[i] );
  186. #endif /* AES_DECRYPTION */
  187. }
  188. aes_tables_inited = 1; // flag that the tables have been generated
  189. } // to permit subsequent use of the AES cipher
  190. /******************************************************************************
  191. *
  192. * AES_SET_ENCRYPTION_KEY
  193. *
  194. * This is called by 'aes_setkey' when we're establishing a key for
  195. * subsequent encryption. We give it a pointer to the encryption
  196. * context, a pointer to the key, and the key's length in bytes.
  197. * Valid lengths are: 16, 24 or 32 bytes (128, 192, 256 bits).
  198. *
  199. ******************************************************************************/
  200. int aes_set_encryption_key( aes_context *ctx,
  201. const uchar *key,
  202. uint keysize )
  203. {
  204. uint i; // general purpose iteration local
  205. uint32_t *RK = ctx->rk; // initialize our RoundKey buffer pointer
  206. for( i = 0; i < (keysize >> 2); i++ ) {
  207. GET_UINT32_LE( RK[i], key, i << 2 );
  208. }
  209. switch( ctx->rounds )
  210. {
  211. case 10:
  212. for( i = 0; i < 10; i++, RK += 4 ) {
  213. RK[4] = RK[0] ^ RCON[i] ^
  214. ( (uint32_t) FSb[ ( RK[3] >> 8 ) & 0xFF ] ) ^
  215. ( (uint32_t) FSb[ ( RK[3] >> 16 ) & 0xFF ] << 8 ) ^
  216. ( (uint32_t) FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^
  217. ( (uint32_t) FSb[ ( RK[3] ) & 0xFF ] << 24 );
  218. RK[5] = RK[1] ^ RK[4];
  219. RK[6] = RK[2] ^ RK[5];
  220. RK[7] = RK[3] ^ RK[6];
  221. }
  222. break;
  223. case 12:
  224. for( i = 0; i < 8; i++, RK += 6 ) {
  225. RK[6] = RK[0] ^ RCON[i] ^
  226. ( (uint32_t) FSb[ ( RK[5] >> 8 ) & 0xFF ] ) ^
  227. ( (uint32_t) FSb[ ( RK[5] >> 16 ) & 0xFF ] << 8 ) ^
  228. ( (uint32_t) FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^
  229. ( (uint32_t) FSb[ ( RK[5] ) & 0xFF ] << 24 );
  230. RK[7] = RK[1] ^ RK[6];
  231. RK[8] = RK[2] ^ RK[7];
  232. RK[9] = RK[3] ^ RK[8];
  233. RK[10] = RK[4] ^ RK[9];
  234. RK[11] = RK[5] ^ RK[10];
  235. }
  236. break;
  237. case 14:
  238. for( i = 0; i < 7; i++, RK += 8 ) {
  239. RK[8] = RK[0] ^ RCON[i] ^
  240. ( (uint32_t) FSb[ ( RK[7] >> 8 ) & 0xFF ] ) ^
  241. ( (uint32_t) FSb[ ( RK[7] >> 16 ) & 0xFF ] << 8 ) ^
  242. ( (uint32_t) FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^
  243. ( (uint32_t) FSb[ ( RK[7] ) & 0xFF ] << 24 );
  244. RK[9] = RK[1] ^ RK[8];
  245. RK[10] = RK[2] ^ RK[9];
  246. RK[11] = RK[3] ^ RK[10];
  247. RK[12] = RK[4] ^
  248. ( (uint32_t) FSb[ ( RK[11] ) & 0xFF ] ) ^
  249. ( (uint32_t) FSb[ ( RK[11] >> 8 ) & 0xFF ] << 8 ) ^
  250. ( (uint32_t) FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^
  251. ( (uint32_t) FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 );
  252. RK[13] = RK[5] ^ RK[12];
  253. RK[14] = RK[6] ^ RK[13];
  254. RK[15] = RK[7] ^ RK[14];
  255. }
  256. break;
  257. default:
  258. return -1;
  259. }
  260. return( 0 );
  261. }
  262. #if AES_DECRYPTION // whether AES decryption is supported
  263. /******************************************************************************
  264. *
  265. * AES_SET_DECRYPTION_KEY
  266. *
  267. * This is called by 'aes_setkey' when we're establishing a
  268. * key for subsequent decryption. We give it a pointer to
  269. * the encryption context, a pointer to the key, and the key's
  270. * length in bits. Valid lengths are: 128, 192, or 256 bits.
  271. *
  272. ******************************************************************************/
  273. int aes_set_decryption_key( aes_context *ctx,
  274. const uchar *key,
  275. uint keysize )
  276. {
  277. int i, j;
  278. aes_context cty; // a calling aes context for set_encryption_key
  279. uint32_t *RK = ctx->rk; // initialize our RoundKey buffer pointer
  280. uint32_t *SK;
  281. int ret;
  282. cty.rounds = ctx->rounds; // initialize our local aes context
  283. cty.rk = cty.buf; // round count and key buf pointer
  284. if (( ret = aes_set_encryption_key( &cty, key, keysize )) != 0 )
  285. return( ret );
  286. SK = cty.rk + cty.rounds * 4;
  287. CPY128 // copy a 128-bit block from *SK to *RK
  288. for( i = ctx->rounds - 1, SK -= 8; i > 0; i--, SK -= 8 ) {
  289. for( j = 0; j < 4; j++, SK++ ) {
  290. *RK++ = RT0[ FSb[ ( *SK ) & 0xFF ] ] ^
  291. RT1[ FSb[ ( *SK >> 8 ) & 0xFF ] ] ^
  292. RT2[ FSb[ ( *SK >> 16 ) & 0xFF ] ] ^
  293. RT3[ FSb[ ( *SK >> 24 ) & 0xFF ] ];
  294. }
  295. }
  296. CPY128 // copy a 128-bit block from *SK to *RK
  297. memset( &cty, 0, sizeof( aes_context ) ); // clear local aes context
  298. return( 0 );
  299. }
  300. #endif /* AES_DECRYPTION */
  301. /******************************************************************************
  302. *
  303. * AES_SETKEY
  304. *
  305. * Invoked to establish the key schedule for subsequent encryption/decryption
  306. *
  307. ******************************************************************************/
  308. int aes_setkey( aes_context *ctx, // AES context provided by our caller
  309. int mode, // ENCRYPT or DECRYPT flag
  310. const uchar *key, // pointer to the key
  311. uint keysize ) // key length in bytes
  312. {
  313. // since table initialization is not thread safe, we could either add
  314. // system-specific mutexes and init the AES key generation tables on
  315. // demand, or ask the developer to simply call "gcm_initialize" once during
  316. // application startup before threading begins. That's what we choose.
  317. if( !aes_tables_inited ) return ( -1 ); // fail the call when not inited.
  318. ctx->mode = mode; // capture the key type we're creating
  319. ctx->rk = ctx->buf; // initialize our round key pointer
  320. switch( keysize ) // set the rounds count based upon the keysize
  321. {
  322. case 16: ctx->rounds = 10; break; // 16-byte, 128-bit key
  323. case 24: ctx->rounds = 12; break; // 24-byte, 192-bit key
  324. case 32: ctx->rounds = 14; break; // 32-byte, 256-bit key
  325. default: return(-1);
  326. }
  327. #if AES_DECRYPTION
  328. if( mode == DECRYPT ) // expand our key for encryption or decryption
  329. return( aes_set_decryption_key( ctx, key, keysize ) );
  330. else /* ENCRYPT */
  331. #endif /* AES_DECRYPTION */
  332. return( aes_set_encryption_key( ctx, key, keysize ) );
  333. }
  334. /******************************************************************************
  335. *
  336. * AES_CIPHER
  337. *
  338. * Perform AES encryption and decryption.
  339. * The AES context will have been setup with the encryption mode
  340. * and all keying information appropriate for the task.
  341. *
  342. ******************************************************************************/
  343. int aes_cipher( aes_context *ctx,
  344. const uchar input[16],
  345. uchar output[16] )
  346. {
  347. int i;
  348. uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; // general purpose locals
  349. RK = ctx->rk;
  350. GET_UINT32_LE( X0, input, 0 ); X0 ^= *RK++; // load our 128-bit
  351. GET_UINT32_LE( X1, input, 4 ); X1 ^= *RK++; // input buffer in a storage
  352. GET_UINT32_LE( X2, input, 8 ); X2 ^= *RK++; // memory endian-neutral way
  353. GET_UINT32_LE( X3, input, 12 ); X3 ^= *RK++;
  354. #if AES_DECRYPTION // whether AES decryption is supported
  355. if( ctx->mode == DECRYPT )
  356. {
  357. for( i = (ctx->rounds >> 1) - 1; i > 0; i-- )
  358. {
  359. AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
  360. AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
  361. }
  362. AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
  363. X0 = *RK++ ^ \
  364. ( (uint32_t) RSb[ ( Y0 ) & 0xFF ] ) ^
  365. ( (uint32_t) RSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
  366. ( (uint32_t) RSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
  367. ( (uint32_t) RSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
  368. X1 = *RK++ ^ \
  369. ( (uint32_t) RSb[ ( Y1 ) & 0xFF ] ) ^
  370. ( (uint32_t) RSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^
  371. ( (uint32_t) RSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
  372. ( (uint32_t) RSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
  373. X2 = *RK++ ^ \
  374. ( (uint32_t) RSb[ ( Y2 ) & 0xFF ] ) ^
  375. ( (uint32_t) RSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
  376. ( (uint32_t) RSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
  377. ( (uint32_t) RSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
  378. X3 = *RK++ ^ \
  379. ( (uint32_t) RSb[ ( Y3 ) & 0xFF ] ) ^
  380. ( (uint32_t) RSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
  381. ( (uint32_t) RSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
  382. ( (uint32_t) RSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
  383. }
  384. else /* ENCRYPT */
  385. {
  386. #endif /* AES_DECRYPTION */
  387. for( i = (ctx->rounds >> 1) - 1; i > 0; i-- )
  388. {
  389. AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
  390. AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
  391. }
  392. AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
  393. X0 = *RK++ ^ \
  394. ( (uint32_t) FSb[ ( Y0 ) & 0xFF ] ) ^
  395. ( (uint32_t) FSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
  396. ( (uint32_t) FSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
  397. ( (uint32_t) FSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
  398. X1 = *RK++ ^ \
  399. ( (uint32_t) FSb[ ( Y1 ) & 0xFF ] ) ^
  400. ( (uint32_t) FSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
  401. ( (uint32_t) FSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
  402. ( (uint32_t) FSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
  403. X2 = *RK++ ^ \
  404. ( (uint32_t) FSb[ ( Y2 ) & 0xFF ] ) ^
  405. ( (uint32_t) FSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
  406. ( (uint32_t) FSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
  407. ( (uint32_t) FSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
  408. X3 = *RK++ ^ \
  409. ( (uint32_t) FSb[ ( Y3 ) & 0xFF ] ) ^
  410. ( (uint32_t) FSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^
  411. ( (uint32_t) FSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
  412. ( (uint32_t) FSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
  413. #if AES_DECRYPTION // whether AES decryption is supported
  414. }
  415. #endif /* AES_DECRYPTION */
  416. PUT_UINT32_LE( X0, output, 0 );
  417. PUT_UINT32_LE( X1, output, 4 );
  418. PUT_UINT32_LE( X2, output, 8 );
  419. PUT_UINT32_LE( X3, output, 12 );
  420. return( 0 );
  421. }
  422. /* end of aes.c */