aes.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. #line __LINE__ "aes.c"
  2. /**************************************************************************
  3. Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
  4. LICENSE TERMS
  5. The redistribution and use of this software (with or without changes)
  6. is allowed without the payment of fees or royalties provided that:
  7. 1. source code distributions include the above copyright notice, this
  8. list of conditions and the following disclaimer;
  9. 2. binary distributions include the above copyright notice, this list
  10. of conditions and the following disclaimer in their documentation;
  11. 3. the name of the copyright holder is not used to endorse products
  12. built using this software without specific written permission.
  13. DISCLAIMER
  14. This software is provided 'as is' with no explicit or implied warranties
  15. in respect of its properties, including, but not limited to, correctness
  16. and/or fitness for purpose.
  17. ---------------------------------------------------------------------------
  18. Issue 09/09/2006
  19. This is an AES implementation that uses only 8-bit byte operations on the
  20. cipher state (there are options to use 32-bit types if available).
  21. The combination of mix columns and byte substitution used here is based on
  22. that developed by Karl Malbrain. His contribution is acknowledged.
  23. *****************************************************************************/
  24. #include "aes.h"
  25. #if 1
  26. # define HAVE_MEMCPY
  27. # include <string.h>
  28. # if defined( _MSC_VER )
  29. # include <intrin.h>
  30. # pragma intrinsic( memcpy )
  31. # endif
  32. #endif
  33. #include <stdlib.h>
  34. /* define if you have fast 32-bit types on your system */
  35. #if 1
  36. # define HAVE_UINT_32T
  37. #endif
  38. /* define if you don't want any tables */
  39. #if 1
  40. # define USE_TABLES
  41. #endif
  42. /* On Intel Core 2 duo VERSION_1 is faster */
  43. /* alternative versions (test for performance on your system) */
  44. #if 1
  45. # define VERSION_1
  46. #endif
  47. #include "aes.h"
  48. #if defined( HAVE_UINT_32T )
  49. typedef unsigned uint_32t; // Edited by Semtech - David Roe 1 Dec 13
  50. #endif
  51. /* functions for finite field multiplication in the AES Galois field */
  52. #define WPOLY 0x011b
  53. #define BPOLY 0x1b
  54. #define DPOLY 0x008d
  55. #define f1(x) (x)
  56. #define f2(x) ((x << 1) ^ (((x >> 7) & 1) * WPOLY))
  57. #define f4(x) ((x << 2) ^ (((x >> 6) & 1) * WPOLY) ^ (((x >> 6) & 2) * WPOLY))
  58. #define f8(x) ((x << 3) ^ (((x >> 5) & 1) * WPOLY) ^ (((x >> 5) & 2) * WPOLY) \
  59. ^ (((x >> 5) & 4) * WPOLY))
  60. #define d2(x) (((x) >> 1) ^ ((x) & 1 ? DPOLY : 0))
  61. #define f3(x) (f2(x) ^ x)
  62. #define f9(x) (f8(x) ^ x)
  63. #define fb(x) (f8(x) ^ f2(x) ^ x)
  64. #define fd(x) (f8(x) ^ f4(x) ^ x)
  65. #define fe(x) (f8(x) ^ f4(x) ^ f2(x))
  66. #if defined( USE_TABLES )
  67. #define sb_data(w) { /* S Box data values */ \
  68. w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\
  69. w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\
  70. w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\
  71. w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\
  72. w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\
  73. w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\
  74. w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\
  75. w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\
  76. w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\
  77. w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\
  78. w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\
  79. w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\
  80. w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\
  81. w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\
  82. w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\
  83. w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\
  84. w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\
  85. w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\
  86. w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\
  87. w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\
  88. w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\
  89. w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\
  90. w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\
  91. w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\
  92. w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\
  93. w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\
  94. w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\
  95. w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\
  96. w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\
  97. w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\
  98. w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\
  99. w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) }
  100. #define isb_data(w) { /* inverse S Box data values */ \
  101. w(0x52), w(0x09), w(0x6a), w(0xd5), w(0x30), w(0x36), w(0xa5), w(0x38),\
  102. w(0xbf), w(0x40), w(0xa3), w(0x9e), w(0x81), w(0xf3), w(0xd7), w(0xfb),\
  103. w(0x7c), w(0xe3), w(0x39), w(0x82), w(0x9b), w(0x2f), w(0xff), w(0x87),\
  104. w(0x34), w(0x8e), w(0x43), w(0x44), w(0xc4), w(0xde), w(0xe9), w(0xcb),\
  105. w(0x54), w(0x7b), w(0x94), w(0x32), w(0xa6), w(0xc2), w(0x23), w(0x3d),\
  106. w(0xee), w(0x4c), w(0x95), w(0x0b), w(0x42), w(0xfa), w(0xc3), w(0x4e),\
  107. w(0x08), w(0x2e), w(0xa1), w(0x66), w(0x28), w(0xd9), w(0x24), w(0xb2),\
  108. w(0x76), w(0x5b), w(0xa2), w(0x49), w(0x6d), w(0x8b), w(0xd1), w(0x25),\
  109. w(0x72), w(0xf8), w(0xf6), w(0x64), w(0x86), w(0x68), w(0x98), w(0x16),\
  110. w(0xd4), w(0xa4), w(0x5c), w(0xcc), w(0x5d), w(0x65), w(0xb6), w(0x92),\
  111. w(0x6c), w(0x70), w(0x48), w(0x50), w(0xfd), w(0xed), w(0xb9), w(0xda),\
  112. w(0x5e), w(0x15), w(0x46), w(0x57), w(0xa7), w(0x8d), w(0x9d), w(0x84),\
  113. w(0x90), w(0xd8), w(0xab), w(0x00), w(0x8c), w(0xbc), w(0xd3), w(0x0a),\
  114. w(0xf7), w(0xe4), w(0x58), w(0x05), w(0xb8), w(0xb3), w(0x45), w(0x06),\
  115. w(0xd0), w(0x2c), w(0x1e), w(0x8f), w(0xca), w(0x3f), w(0x0f), w(0x02),\
  116. w(0xc1), w(0xaf), w(0xbd), w(0x03), w(0x01), w(0x13), w(0x8a), w(0x6b),\
  117. w(0x3a), w(0x91), w(0x11), w(0x41), w(0x4f), w(0x67), w(0xdc), w(0xea),\
  118. w(0x97), w(0xf2), w(0xcf), w(0xce), w(0xf0), w(0xb4), w(0xe6), w(0x73),\
  119. w(0x96), w(0xac), w(0x74), w(0x22), w(0xe7), w(0xad), w(0x35), w(0x85),\
  120. w(0xe2), w(0xf9), w(0x37), w(0xe8), w(0x1c), w(0x75), w(0xdf), w(0x6e),\
  121. w(0x47), w(0xf1), w(0x1a), w(0x71), w(0x1d), w(0x29), w(0xc5), w(0x89),\
  122. w(0x6f), w(0xb7), w(0x62), w(0x0e), w(0xaa), w(0x18), w(0xbe), w(0x1b),\
  123. w(0xfc), w(0x56), w(0x3e), w(0x4b), w(0xc6), w(0xd2), w(0x79), w(0x20),\
  124. w(0x9a), w(0xdb), w(0xc0), w(0xfe), w(0x78), w(0xcd), w(0x5a), w(0xf4),\
  125. w(0x1f), w(0xdd), w(0xa8), w(0x33), w(0x88), w(0x07), w(0xc7), w(0x31),\
  126. w(0xb1), w(0x12), w(0x10), w(0x59), w(0x27), w(0x80), w(0xec), w(0x5f),\
  127. w(0x60), w(0x51), w(0x7f), w(0xa9), w(0x19), w(0xb5), w(0x4a), w(0x0d),\
  128. w(0x2d), w(0xe5), w(0x7a), w(0x9f), w(0x93), w(0xc9), w(0x9c), w(0xef),\
  129. w(0xa0), w(0xe0), w(0x3b), w(0x4d), w(0xae), w(0x2a), w(0xf5), w(0xb0),\
  130. w(0xc8), w(0xeb), w(0xbb), w(0x3c), w(0x83), w(0x53), w(0x99), w(0x61),\
  131. w(0x17), w(0x2b), w(0x04), w(0x7e), w(0xba), w(0x77), w(0xd6), w(0x26),\
  132. w(0xe1), w(0x69), w(0x14), w(0x63), w(0x55), w(0x21), w(0x0c), w(0x7d) }
  133. #define mm_data(w) { /* basic data for forming finite field tables */ \
  134. w(0x00), w(0x01), w(0x02), w(0x03), w(0x04), w(0x05), w(0x06), w(0x07),\
  135. w(0x08), w(0x09), w(0x0a), w(0x0b), w(0x0c), w(0x0d), w(0x0e), w(0x0f),\
  136. w(0x10), w(0x11), w(0x12), w(0x13), w(0x14), w(0x15), w(0x16), w(0x17),\
  137. w(0x18), w(0x19), w(0x1a), w(0x1b), w(0x1c), w(0x1d), w(0x1e), w(0x1f),\
  138. w(0x20), w(0x21), w(0x22), w(0x23), w(0x24), w(0x25), w(0x26), w(0x27),\
  139. w(0x28), w(0x29), w(0x2a), w(0x2b), w(0x2c), w(0x2d), w(0x2e), w(0x2f),\
  140. w(0x30), w(0x31), w(0x32), w(0x33), w(0x34), w(0x35), w(0x36), w(0x37),\
  141. w(0x38), w(0x39), w(0x3a), w(0x3b), w(0x3c), w(0x3d), w(0x3e), w(0x3f),\
  142. w(0x40), w(0x41), w(0x42), w(0x43), w(0x44), w(0x45), w(0x46), w(0x47),\
  143. w(0x48), w(0x49), w(0x4a), w(0x4b), w(0x4c), w(0x4d), w(0x4e), w(0x4f),\
  144. w(0x50), w(0x51), w(0x52), w(0x53), w(0x54), w(0x55), w(0x56), w(0x57),\
  145. w(0x58), w(0x59), w(0x5a), w(0x5b), w(0x5c), w(0x5d), w(0x5e), w(0x5f),\
  146. w(0x60), w(0x61), w(0x62), w(0x63), w(0x64), w(0x65), w(0x66), w(0x67),\
  147. w(0x68), w(0x69), w(0x6a), w(0x6b), w(0x6c), w(0x6d), w(0x6e), w(0x6f),\
  148. w(0x70), w(0x71), w(0x72), w(0x73), w(0x74), w(0x75), w(0x76), w(0x77),\
  149. w(0x78), w(0x79), w(0x7a), w(0x7b), w(0x7c), w(0x7d), w(0x7e), w(0x7f),\
  150. w(0x80), w(0x81), w(0x82), w(0x83), w(0x84), w(0x85), w(0x86), w(0x87),\
  151. w(0x88), w(0x89), w(0x8a), w(0x8b), w(0x8c), w(0x8d), w(0x8e), w(0x8f),\
  152. w(0x90), w(0x91), w(0x92), w(0x93), w(0x94), w(0x95), w(0x96), w(0x97),\
  153. w(0x98), w(0x99), w(0x9a), w(0x9b), w(0x9c), w(0x9d), w(0x9e), w(0x9f),\
  154. w(0xa0), w(0xa1), w(0xa2), w(0xa3), w(0xa4), w(0xa5), w(0xa6), w(0xa7),\
  155. w(0xa8), w(0xa9), w(0xaa), w(0xab), w(0xac), w(0xad), w(0xae), w(0xaf),\
  156. w(0xb0), w(0xb1), w(0xb2), w(0xb3), w(0xb4), w(0xb5), w(0xb6), w(0xb7),\
  157. w(0xb8), w(0xb9), w(0xba), w(0xbb), w(0xbc), w(0xbd), w(0xbe), w(0xbf),\
  158. w(0xc0), w(0xc1), w(0xc2), w(0xc3), w(0xc4), w(0xc5), w(0xc6), w(0xc7),\
  159. w(0xc8), w(0xc9), w(0xca), w(0xcb), w(0xcc), w(0xcd), w(0xce), w(0xcf),\
  160. w(0xd0), w(0xd1), w(0xd2), w(0xd3), w(0xd4), w(0xd5), w(0xd6), w(0xd7),\
  161. w(0xd8), w(0xd9), w(0xda), w(0xdb), w(0xdc), w(0xdd), w(0xde), w(0xdf),\
  162. w(0xe0), w(0xe1), w(0xe2), w(0xe3), w(0xe4), w(0xe5), w(0xe6), w(0xe7),\
  163. w(0xe8), w(0xe9), w(0xea), w(0xeb), w(0xec), w(0xed), w(0xee), w(0xef),\
  164. w(0xf0), w(0xf1), w(0xf2), w(0xf3), w(0xf4), w(0xf5), w(0xf6), w(0xf7),\
  165. w(0xf8), w(0xf9), w(0xfa), w(0xfb), w(0xfc), w(0xfd), w(0xfe), w(0xff) }
  166. static const uint_8t sbox[256] = sb_data(f1);
  167. static const uint_8t isbox[256] = isb_data(f1);
  168. static const uint_8t gfm2_sbox[256] = sb_data(f2);
  169. static const uint_8t gfm3_sbox[256] = sb_data(f3);
  170. static const uint_8t gfmul_9[256] = mm_data(f9);
  171. static const uint_8t gfmul_b[256] = mm_data(fb);
  172. static const uint_8t gfmul_d[256] = mm_data(fd);
  173. static const uint_8t gfmul_e[256] = mm_data(fe);
  174. #define s_box(x) sbox[(x)]
  175. #define is_box(x) isbox[(x)]
  176. #define gfm2_sb(x) gfm2_sbox[(x)]
  177. #define gfm3_sb(x) gfm3_sbox[(x)]
  178. #define gfm_9(x) gfmul_9[(x)]
  179. #define gfm_b(x) gfmul_b[(x)]
  180. #define gfm_d(x) gfmul_d[(x)]
  181. #define gfm_e(x) gfmul_e[(x)]
  182. #else
  183. /* this is the high bit of x right shifted by 1 */
  184. /* position. Since the starting polynomial has */
  185. /* 9 bits (0x11b), this right shift keeps the */
  186. /* values of all top bits within a byte */
  187. static uint_8t hibit(const uint_8t x)
  188. { uint_8t r = (uint_8t)((x >> 1) | (x >> 2));
  189. r |= (r >> 2);
  190. r |= (r >> 4);
  191. return (r + 1) >> 1;
  192. }
  193. /* return the inverse of the finite field element x */
  194. static uint_8t gf_inv(const uint_8t x)
  195. { uint_8t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
  196. if(x < 2)
  197. return x;
  198. for( ; ; )
  199. {
  200. if(n1)
  201. while(n2 >= n1) /* divide polynomial p2 by p1 */
  202. {
  203. n2 /= n1; /* shift smaller polynomial left */
  204. p2 ^= (p1 * n2) & 0xff; /* and remove from larger one */
  205. v2 ^= (v1 * n2); /* shift accumulated value and */
  206. n2 = hibit(p2); /* add into result */
  207. }
  208. else
  209. return v1;
  210. if(n2) /* repeat with values swapped */
  211. while(n1 >= n2)
  212. {
  213. n1 /= n2;
  214. p1 ^= p2 * n1;
  215. v1 ^= v2 * n1;
  216. n1 = hibit(p1);
  217. }
  218. else
  219. return v2;
  220. }
  221. }
  222. /* The forward and inverse affine transformations used in the S-box */
  223. uint_8t fwd_affine(const uint_8t x)
  224. {
  225. #if defined( HAVE_UINT_32T )
  226. uint_32t w = x;
  227. w ^= (w << 1) ^ (w << 2) ^ (w << 3) ^ (w << 4);
  228. return 0x63 ^ ((w ^ (w >> 8)) & 0xff);
  229. #else
  230. return 0x63 ^ x ^ (x << 1) ^ (x << 2) ^ (x << 3) ^ (x << 4)
  231. ^ (x >> 7) ^ (x >> 6) ^ (x >> 5) ^ (x >> 4);
  232. #endif
  233. }
  234. uint_8t inv_affine(const uint_8t x)
  235. {
  236. #if defined( HAVE_UINT_32T )
  237. uint_32t w = x;
  238. w = (w << 1) ^ (w << 3) ^ (w << 6);
  239. return 0x05 ^ ((w ^ (w >> 8)) & 0xff);
  240. #else
  241. return 0x05 ^ (x << 1) ^ (x << 3) ^ (x << 6)
  242. ^ (x >> 7) ^ (x >> 5) ^ (x >> 2);
  243. #endif
  244. }
  245. #define s_box(x) fwd_affine(gf_inv(x))
  246. #define is_box(x) gf_inv(inv_affine(x))
  247. #define gfm2_sb(x) f2(s_box(x))
  248. #define gfm3_sb(x) f3(s_box(x))
  249. #define gfm_9(x) f9(x)
  250. #define gfm_b(x) fb(x)
  251. #define gfm_d(x) fd(x)
  252. #define gfm_e(x) fe(x)
  253. #endif
  254. #if defined( HAVE_MEMCPY )
  255. # define block_copy_nn(d, s, l) memcpy(d, s, l)
  256. # define block_copy(d, s) memcpy(d, s, N_BLOCK)
  257. #else
  258. # define block_copy_nn(d, s, l) copy_block_nn(d, s, l)
  259. # define block_copy(d, s) copy_block(d, s)
  260. static void copy_block( void *d, const void *s )
  261. {
  262. #if defined( HAVE_UINT_32T )
  263. ((uint_32t*)d)[ 0] = ((uint_32t*)s)[ 0];
  264. ((uint_32t*)d)[ 1] = ((uint_32t*)s)[ 1];
  265. ((uint_32t*)d)[ 2] = ((uint_32t*)s)[ 2];
  266. ((uint_32t*)d)[ 3] = ((uint_32t*)s)[ 3];
  267. #else
  268. ((uint_8t*)d)[ 0] = ((uint_8t*)s)[ 0];
  269. ((uint_8t*)d)[ 1] = ((uint_8t*)s)[ 1];
  270. ((uint_8t*)d)[ 2] = ((uint_8t*)s)[ 2];
  271. ((uint_8t*)d)[ 3] = ((uint_8t*)s)[ 3];
  272. ((uint_8t*)d)[ 4] = ((uint_8t*)s)[ 4];
  273. ((uint_8t*)d)[ 5] = ((uint_8t*)s)[ 5];
  274. ((uint_8t*)d)[ 6] = ((uint_8t*)s)[ 6];
  275. ((uint_8t*)d)[ 7] = ((uint_8t*)s)[ 7];
  276. ((uint_8t*)d)[ 8] = ((uint_8t*)s)[ 8];
  277. ((uint_8t*)d)[ 9] = ((uint_8t*)s)[ 9];
  278. ((uint_8t*)d)[10] = ((uint_8t*)s)[10];
  279. ((uint_8t*)d)[11] = ((uint_8t*)s)[11];
  280. ((uint_8t*)d)[12] = ((uint_8t*)s)[12];
  281. ((uint_8t*)d)[13] = ((uint_8t*)s)[13];
  282. ((uint_8t*)d)[14] = ((uint_8t*)s)[14];
  283. ((uint_8t*)d)[15] = ((uint_8t*)s)[15];
  284. #endif
  285. }
  286. static void copy_block_nn( uint_8t * d, const uint_8t *s, uint_8t nn )
  287. {
  288. while( nn-- )
  289. //*((uint_8t*)d)++ = *((uint_8t*)s)++;
  290. *d++ = *s++;
  291. }
  292. #endif /* HAVE_MEMCPY */
  293. static void xor_block( void *d, const void *s )
  294. {
  295. #if defined( HAVE_UINT_32T )
  296. ((uint_32t*)d)[ 0] ^= ((uint_32t*)s)[ 0];
  297. ((uint_32t*)d)[ 1] ^= ((uint_32t*)s)[ 1];
  298. ((uint_32t*)d)[ 2] ^= ((uint_32t*)s)[ 2];
  299. ((uint_32t*)d)[ 3] ^= ((uint_32t*)s)[ 3];
  300. #else
  301. ((uint_8t*)d)[ 0] ^= ((uint_8t*)s)[ 0];
  302. ((uint_8t*)d)[ 1] ^= ((uint_8t*)s)[ 1];
  303. ((uint_8t*)d)[ 2] ^= ((uint_8t*)s)[ 2];
  304. ((uint_8t*)d)[ 3] ^= ((uint_8t*)s)[ 3];
  305. ((uint_8t*)d)[ 4] ^= ((uint_8t*)s)[ 4];
  306. ((uint_8t*)d)[ 5] ^= ((uint_8t*)s)[ 5];
  307. ((uint_8t*)d)[ 6] ^= ((uint_8t*)s)[ 6];
  308. ((uint_8t*)d)[ 7] ^= ((uint_8t*)s)[ 7];
  309. ((uint_8t*)d)[ 8] ^= ((uint_8t*)s)[ 8];
  310. ((uint_8t*)d)[ 9] ^= ((uint_8t*)s)[ 9];
  311. ((uint_8t*)d)[10] ^= ((uint_8t*)s)[10];
  312. ((uint_8t*)d)[11] ^= ((uint_8t*)s)[11];
  313. ((uint_8t*)d)[12] ^= ((uint_8t*)s)[12];
  314. ((uint_8t*)d)[13] ^= ((uint_8t*)s)[13];
  315. ((uint_8t*)d)[14] ^= ((uint_8t*)s)[14];
  316. ((uint_8t*)d)[15] ^= ((uint_8t*)s)[15];
  317. #endif
  318. }
  319. static void copy_and_key( void *d, const void *s, const void *k )
  320. {
  321. #if defined( HAVE_UINT_32T )
  322. ((uint_32t*)d)[ 0] = ((uint_32t*)s)[ 0] ^ ((uint_32t*)k)[ 0];
  323. ((uint_32t*)d)[ 1] = ((uint_32t*)s)[ 1] ^ ((uint_32t*)k)[ 1];
  324. ((uint_32t*)d)[ 2] = ((uint_32t*)s)[ 2] ^ ((uint_32t*)k)[ 2];
  325. ((uint_32t*)d)[ 3] = ((uint_32t*)s)[ 3] ^ ((uint_32t*)k)[ 3];
  326. #elif 1
  327. ((uint_8t*)d)[ 0] = ((uint_8t*)s)[ 0] ^ ((uint_8t*)k)[ 0];
  328. ((uint_8t*)d)[ 1] = ((uint_8t*)s)[ 1] ^ ((uint_8t*)k)[ 1];
  329. ((uint_8t*)d)[ 2] = ((uint_8t*)s)[ 2] ^ ((uint_8t*)k)[ 2];
  330. ((uint_8t*)d)[ 3] = ((uint_8t*)s)[ 3] ^ ((uint_8t*)k)[ 3];
  331. ((uint_8t*)d)[ 4] = ((uint_8t*)s)[ 4] ^ ((uint_8t*)k)[ 4];
  332. ((uint_8t*)d)[ 5] = ((uint_8t*)s)[ 5] ^ ((uint_8t*)k)[ 5];
  333. ((uint_8t*)d)[ 6] = ((uint_8t*)s)[ 6] ^ ((uint_8t*)k)[ 6];
  334. ((uint_8t*)d)[ 7] = ((uint_8t*)s)[ 7] ^ ((uint_8t*)k)[ 7];
  335. ((uint_8t*)d)[ 8] = ((uint_8t*)s)[ 8] ^ ((uint_8t*)k)[ 8];
  336. ((uint_8t*)d)[ 9] = ((uint_8t*)s)[ 9] ^ ((uint_8t*)k)[ 9];
  337. ((uint_8t*)d)[10] = ((uint_8t*)s)[10] ^ ((uint_8t*)k)[10];
  338. ((uint_8t*)d)[11] = ((uint_8t*)s)[11] ^ ((uint_8t*)k)[11];
  339. ((uint_8t*)d)[12] = ((uint_8t*)s)[12] ^ ((uint_8t*)k)[12];
  340. ((uint_8t*)d)[13] = ((uint_8t*)s)[13] ^ ((uint_8t*)k)[13];
  341. ((uint_8t*)d)[14] = ((uint_8t*)s)[14] ^ ((uint_8t*)k)[14];
  342. ((uint_8t*)d)[15] = ((uint_8t*)s)[15] ^ ((uint_8t*)k)[15];
  343. #else
  344. block_copy(d, s);
  345. xor_block(d, k);
  346. #endif
  347. }
  348. static void add_round_key( uint_8t d[N_BLOCK], const uint_8t k[N_BLOCK] )
  349. {
  350. xor_block(d, k);
  351. }
  352. static void shift_sub_rows( uint_8t st[N_BLOCK] )
  353. { uint_8t tt;
  354. st[ 0] = s_box(st[ 0]); st[ 4] = s_box(st[ 4]);
  355. st[ 8] = s_box(st[ 8]); st[12] = s_box(st[12]);
  356. tt = st[1]; st[ 1] = s_box(st[ 5]); st[ 5] = s_box(st[ 9]);
  357. st[ 9] = s_box(st[13]); st[13] = s_box( tt );
  358. tt = st[2]; st[ 2] = s_box(st[10]); st[10] = s_box( tt );
  359. tt = st[6]; st[ 6] = s_box(st[14]); st[14] = s_box( tt );
  360. tt = st[15]; st[15] = s_box(st[11]); st[11] = s_box(st[ 7]);
  361. st[ 7] = s_box(st[ 3]); st[ 3] = s_box( tt );
  362. }
  363. static void inv_shift_sub_rows( uint_8t st[N_BLOCK] )
  364. { uint_8t tt;
  365. st[ 0] = is_box(st[ 0]); st[ 4] = is_box(st[ 4]);
  366. st[ 8] = is_box(st[ 8]); st[12] = is_box(st[12]);
  367. tt = st[13]; st[13] = is_box(st[9]); st[ 9] = is_box(st[5]);
  368. st[ 5] = is_box(st[1]); st[ 1] = is_box( tt );
  369. tt = st[2]; st[ 2] = is_box(st[10]); st[10] = is_box( tt );
  370. tt = st[6]; st[ 6] = is_box(st[14]); st[14] = is_box( tt );
  371. tt = st[3]; st[ 3] = is_box(st[ 7]); st[ 7] = is_box(st[11]);
  372. st[11] = is_box(st[15]); st[15] = is_box( tt );
  373. }
  374. #if defined( VERSION_1 )
  375. static void mix_sub_columns( uint_8t dt[N_BLOCK] )
  376. { uint_8t st[N_BLOCK];
  377. block_copy(st, dt);
  378. #else
  379. static void mix_sub_columns( uint_8t dt[N_BLOCK], uint_8t st[N_BLOCK] )
  380. {
  381. #endif
  382. dt[ 0] = gfm2_sb(st[0]) ^ gfm3_sb(st[5]) ^ s_box(st[10]) ^ s_box(st[15]);
  383. dt[ 1] = s_box(st[0]) ^ gfm2_sb(st[5]) ^ gfm3_sb(st[10]) ^ s_box(st[15]);
  384. dt[ 2] = s_box(st[0]) ^ s_box(st[5]) ^ gfm2_sb(st[10]) ^ gfm3_sb(st[15]);
  385. dt[ 3] = gfm3_sb(st[0]) ^ s_box(st[5]) ^ s_box(st[10]) ^ gfm2_sb(st[15]);
  386. dt[ 4] = gfm2_sb(st[4]) ^ gfm3_sb(st[9]) ^ s_box(st[14]) ^ s_box(st[3]);
  387. dt[ 5] = s_box(st[4]) ^ gfm2_sb(st[9]) ^ gfm3_sb(st[14]) ^ s_box(st[3]);
  388. dt[ 6] = s_box(st[4]) ^ s_box(st[9]) ^ gfm2_sb(st[14]) ^ gfm3_sb(st[3]);
  389. dt[ 7] = gfm3_sb(st[4]) ^ s_box(st[9]) ^ s_box(st[14]) ^ gfm2_sb(st[3]);
  390. dt[ 8] = gfm2_sb(st[8]) ^ gfm3_sb(st[13]) ^ s_box(st[2]) ^ s_box(st[7]);
  391. dt[ 9] = s_box(st[8]) ^ gfm2_sb(st[13]) ^ gfm3_sb(st[2]) ^ s_box(st[7]);
  392. dt[10] = s_box(st[8]) ^ s_box(st[13]) ^ gfm2_sb(st[2]) ^ gfm3_sb(st[7]);
  393. dt[11] = gfm3_sb(st[8]) ^ s_box(st[13]) ^ s_box(st[2]) ^ gfm2_sb(st[7]);
  394. dt[12] = gfm2_sb(st[12]) ^ gfm3_sb(st[1]) ^ s_box(st[6]) ^ s_box(st[11]);
  395. dt[13] = s_box(st[12]) ^ gfm2_sb(st[1]) ^ gfm3_sb(st[6]) ^ s_box(st[11]);
  396. dt[14] = s_box(st[12]) ^ s_box(st[1]) ^ gfm2_sb(st[6]) ^ gfm3_sb(st[11]);
  397. dt[15] = gfm3_sb(st[12]) ^ s_box(st[1]) ^ s_box(st[6]) ^ gfm2_sb(st[11]);
  398. }
  399. #if defined( VERSION_1 )
  400. static void inv_mix_sub_columns( uint_8t dt[N_BLOCK] )
  401. { uint_8t st[N_BLOCK];
  402. block_copy(st, dt);
  403. #else
  404. static void inv_mix_sub_columns( uint_8t dt[N_BLOCK], uint_8t st[N_BLOCK] )
  405. {
  406. #endif
  407. dt[ 0] = is_box(gfm_e(st[ 0]) ^ gfm_b(st[ 1]) ^ gfm_d(st[ 2]) ^ gfm_9(st[ 3]));
  408. dt[ 5] = is_box(gfm_9(st[ 0]) ^ gfm_e(st[ 1]) ^ gfm_b(st[ 2]) ^ gfm_d(st[ 3]));
  409. dt[10] = is_box(gfm_d(st[ 0]) ^ gfm_9(st[ 1]) ^ gfm_e(st[ 2]) ^ gfm_b(st[ 3]));
  410. dt[15] = is_box(gfm_b(st[ 0]) ^ gfm_d(st[ 1]) ^ gfm_9(st[ 2]) ^ gfm_e(st[ 3]));
  411. dt[ 4] = is_box(gfm_e(st[ 4]) ^ gfm_b(st[ 5]) ^ gfm_d(st[ 6]) ^ gfm_9(st[ 7]));
  412. dt[ 9] = is_box(gfm_9(st[ 4]) ^ gfm_e(st[ 5]) ^ gfm_b(st[ 6]) ^ gfm_d(st[ 7]));
  413. dt[14] = is_box(gfm_d(st[ 4]) ^ gfm_9(st[ 5]) ^ gfm_e(st[ 6]) ^ gfm_b(st[ 7]));
  414. dt[ 3] = is_box(gfm_b(st[ 4]) ^ gfm_d(st[ 5]) ^ gfm_9(st[ 6]) ^ gfm_e(st[ 7]));
  415. dt[ 8] = is_box(gfm_e(st[ 8]) ^ gfm_b(st[ 9]) ^ gfm_d(st[10]) ^ gfm_9(st[11]));
  416. dt[13] = is_box(gfm_9(st[ 8]) ^ gfm_e(st[ 9]) ^ gfm_b(st[10]) ^ gfm_d(st[11]));
  417. dt[ 2] = is_box(gfm_d(st[ 8]) ^ gfm_9(st[ 9]) ^ gfm_e(st[10]) ^ gfm_b(st[11]));
  418. dt[ 7] = is_box(gfm_b(st[ 8]) ^ gfm_d(st[ 9]) ^ gfm_9(st[10]) ^ gfm_e(st[11]));
  419. dt[12] = is_box(gfm_e(st[12]) ^ gfm_b(st[13]) ^ gfm_d(st[14]) ^ gfm_9(st[15]));
  420. dt[ 1] = is_box(gfm_9(st[12]) ^ gfm_e(st[13]) ^ gfm_b(st[14]) ^ gfm_d(st[15]));
  421. dt[ 6] = is_box(gfm_d(st[12]) ^ gfm_9(st[13]) ^ gfm_e(st[14]) ^ gfm_b(st[15]));
  422. dt[11] = is_box(gfm_b(st[12]) ^ gfm_d(st[13]) ^ gfm_9(st[14]) ^ gfm_e(st[15]));
  423. }
  424. #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
  425. /* Set the cipher key for the pre-keyed version */
  426. return_type aes_set_key( const unsigned char key[], length_type keylen, aes_context ctx[1] )
  427. {
  428. uint_8t cc, rc, hi;
  429. switch( keylen )
  430. {
  431. case 16:
  432. case 24:
  433. case 32:
  434. break;
  435. default:
  436. ctx->rnd = 0;
  437. return -1;
  438. }
  439. block_copy_nn(ctx->ksch, key, keylen);
  440. hi = (keylen + 28) << 2;
  441. ctx->rnd = (hi >> 4) - 1;
  442. for( cc = keylen, rc = 1; cc < hi; cc += 4 )
  443. { uint_8t tt, t0, t1, t2, t3;
  444. t0 = ctx->ksch[cc - 4];
  445. t1 = ctx->ksch[cc - 3];
  446. t2 = ctx->ksch[cc - 2];
  447. t3 = ctx->ksch[cc - 1];
  448. if( cc % keylen == 0 )
  449. {
  450. tt = t0;
  451. t0 = s_box(t1) ^ rc;
  452. t1 = s_box(t2);
  453. t2 = s_box(t3);
  454. t3 = s_box(tt);
  455. rc = f2(rc);
  456. }
  457. else if( keylen > 24 && cc % keylen == 16 )
  458. {
  459. t0 = s_box(t0);
  460. t1 = s_box(t1);
  461. t2 = s_box(t2);
  462. t3 = s_box(t3);
  463. }
  464. tt = cc - keylen;
  465. ctx->ksch[cc + 0] = ctx->ksch[tt + 0] ^ t0;
  466. ctx->ksch[cc + 1] = ctx->ksch[tt + 1] ^ t1;
  467. ctx->ksch[cc + 2] = ctx->ksch[tt + 2] ^ t2;
  468. ctx->ksch[cc + 3] = ctx->ksch[tt + 3] ^ t3;
  469. }
  470. return 0;
  471. }
  472. #endif
  473. #if defined( AES_ENC_PREKEYED )
  474. /* Encrypt a single block of 16 bytes */
  475. return_type aes_encrypt( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1] )
  476. {
  477. if( ctx->rnd )
  478. {
  479. uint_8t s1[N_BLOCK], r;
  480. copy_and_key( s1, in, ctx->ksch );
  481. for( r = 1 ; r < ctx->rnd ; ++r )
  482. #if defined( VERSION_1 )
  483. {
  484. mix_sub_columns( s1 );
  485. add_round_key( s1, ctx->ksch + r * N_BLOCK);
  486. }
  487. #else
  488. { uint_8t s2[N_BLOCK];
  489. mix_sub_columns( s2, s1 );
  490. copy_and_key( s1, s2, ctx->ksch + r * N_BLOCK);
  491. }
  492. #endif
  493. shift_sub_rows( s1 );
  494. copy_and_key( out, s1, ctx->ksch + r * N_BLOCK );
  495. }
  496. else
  497. return -1;
  498. return 0;
  499. }
  500. /* CBC encrypt a number of blocks (input and return an IV) */
  501. return_type aes_cbc_encrypt( const unsigned char *in, unsigned char *out,
  502. int n_block, unsigned char iv[N_BLOCK], const aes_context ctx[1] )
  503. {
  504. while(n_block--)
  505. {
  506. xor_block(iv, in);
  507. if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
  508. return EXIT_FAILURE;
  509. //memcpy(out, iv, N_BLOCK);
  510. block_copy(out, iv);
  511. in += N_BLOCK;
  512. out += N_BLOCK;
  513. }
  514. return EXIT_SUCCESS;
  515. }
  516. #endif
  517. #if defined( AES_DEC_PREKEYED )
  518. /* Decrypt a single block of 16 bytes */
  519. return_type aes_decrypt( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1] )
  520. {
  521. if( ctx->rnd )
  522. {
  523. uint_8t s1[N_BLOCK], r;
  524. copy_and_key( s1, in, ctx->ksch + ctx->rnd * N_BLOCK );
  525. inv_shift_sub_rows( s1 );
  526. for( r = ctx->rnd ; --r ; )
  527. #if defined( VERSION_1 )
  528. {
  529. add_round_key( s1, ctx->ksch + r * N_BLOCK );
  530. inv_mix_sub_columns( s1 );
  531. }
  532. #else
  533. { uint_8t s2[N_BLOCK];
  534. copy_and_key( s2, s1, ctx->ksch + r * N_BLOCK );
  535. inv_mix_sub_columns( s1, s2 );
  536. }
  537. #endif
  538. copy_and_key( out, s1, ctx->ksch );
  539. }
  540. else
  541. return -1;
  542. return 0;
  543. }
  544. /* CBC decrypt a number of blocks (input and return an IV) */
  545. return_type aes_cbc_decrypt( const unsigned char *in, unsigned char *out,
  546. int n_block, unsigned char iv[N_BLOCK], const aes_context ctx[1] )
  547. {
  548. while(n_block--)
  549. { uint_8t tmp[N_BLOCK];
  550. //memcpy(tmp, in, N_BLOCK);
  551. block_copy(tmp, in);
  552. if(aes_decrypt(in, out, ctx) != EXIT_SUCCESS)
  553. return EXIT_FAILURE;
  554. xor_block(out, iv);
  555. //memcpy(iv, tmp, N_BLOCK);
  556. block_copy(iv, tmp);
  557. in += N_BLOCK;
  558. out += N_BLOCK;
  559. }
  560. return EXIT_SUCCESS;
  561. }
  562. #endif
  563. #if defined( AES_ENC_128_OTFK )
  564. /* The 'on the fly' encryption key update for for 128 bit keys */
  565. static void update_encrypt_key_128( uint_8t k[N_BLOCK], uint_8t *rc )
  566. { uint_8t cc;
  567. k[0] ^= s_box(k[13]) ^ *rc;
  568. k[1] ^= s_box(k[14]);
  569. k[2] ^= s_box(k[15]);
  570. k[3] ^= s_box(k[12]);
  571. *rc = f2( *rc );
  572. for(cc = 4; cc < 16; cc += 4 )
  573. {
  574. k[cc + 0] ^= k[cc - 4];
  575. k[cc + 1] ^= k[cc - 3];
  576. k[cc + 2] ^= k[cc - 2];
  577. k[cc + 3] ^= k[cc - 1];
  578. }
  579. }
  580. /* Encrypt a single block of 16 bytes with 'on the fly' 128 bit keying */
  581. void aes_encrypt_128( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
  582. const unsigned char key[N_BLOCK], unsigned char o_key[N_BLOCK] )
  583. { uint_8t s1[N_BLOCK], r, rc = 1;
  584. if(o_key != key)
  585. block_copy( o_key, key );
  586. copy_and_key( s1, in, o_key );
  587. for( r = 1 ; r < 10 ; ++r )
  588. #if defined( VERSION_1 )
  589. {
  590. mix_sub_columns( s1 );
  591. update_encrypt_key_128( o_key, &rc );
  592. add_round_key( s1, o_key );
  593. }
  594. #else
  595. { uint_8t s2[N_BLOCK];
  596. mix_sub_columns( s2, s1 );
  597. update_encrypt_key_128( o_key, &rc );
  598. copy_and_key( s1, s2, o_key );
  599. }
  600. #endif
  601. shift_sub_rows( s1 );
  602. update_encrypt_key_128( o_key, &rc );
  603. copy_and_key( out, s1, o_key );
  604. }
  605. #endif
  606. #if defined( AES_DEC_128_OTFK )
  607. /* The 'on the fly' decryption key update for for 128 bit keys */
  608. static void update_decrypt_key_128( uint_8t k[N_BLOCK], uint_8t *rc )
  609. { uint_8t cc;
  610. for( cc = 12; cc > 0; cc -= 4 )
  611. {
  612. k[cc + 0] ^= k[cc - 4];
  613. k[cc + 1] ^= k[cc - 3];
  614. k[cc + 2] ^= k[cc - 2];
  615. k[cc + 3] ^= k[cc - 1];
  616. }
  617. *rc = d2(*rc);
  618. k[0] ^= s_box(k[13]) ^ *rc;
  619. k[1] ^= s_box(k[14]);
  620. k[2] ^= s_box(k[15]);
  621. k[3] ^= s_box(k[12]);
  622. }
  623. /* Decrypt a single block of 16 bytes with 'on the fly' 128 bit keying */
  624. void aes_decrypt_128( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
  625. const unsigned char key[N_BLOCK], unsigned char o_key[N_BLOCK] )
  626. {
  627. uint_8t s1[N_BLOCK], r, rc = 0x6c;
  628. if(o_key != key)
  629. block_copy( o_key, key );
  630. copy_and_key( s1, in, o_key );
  631. inv_shift_sub_rows( s1 );
  632. for( r = 10 ; --r ; )
  633. #if defined( VERSION_1 )
  634. {
  635. update_decrypt_key_128( o_key, &rc );
  636. add_round_key( s1, o_key );
  637. inv_mix_sub_columns( s1 );
  638. }
  639. #else
  640. { uint_8t s2[N_BLOCK];
  641. update_decrypt_key_128( o_key, &rc );
  642. copy_and_key( s2, s1, o_key );
  643. inv_mix_sub_columns( s1, s2 );
  644. }
  645. #endif
  646. update_decrypt_key_128( o_key, &rc );
  647. copy_and_key( out, s1, o_key );
  648. }
  649. #endif
  650. #if defined( AES_ENC_256_OTFK )
  651. /* The 'on the fly' encryption key update for for 256 bit keys */
  652. static void update_encrypt_key_256( uint_8t k[2 * N_BLOCK], uint_8t *rc )
  653. { uint_8t cc;
  654. k[0] ^= s_box(k[29]) ^ *rc;
  655. k[1] ^= s_box(k[30]);
  656. k[2] ^= s_box(k[31]);
  657. k[3] ^= s_box(k[28]);
  658. *rc = f2( *rc );
  659. for(cc = 4; cc < 16; cc += 4)
  660. {
  661. k[cc + 0] ^= k[cc - 4];
  662. k[cc + 1] ^= k[cc - 3];
  663. k[cc + 2] ^= k[cc - 2];
  664. k[cc + 3] ^= k[cc - 1];
  665. }
  666. k[16] ^= s_box(k[12]);
  667. k[17] ^= s_box(k[13]);
  668. k[18] ^= s_box(k[14]);
  669. k[19] ^= s_box(k[15]);
  670. for( cc = 20; cc < 32; cc += 4 )
  671. {
  672. k[cc + 0] ^= k[cc - 4];
  673. k[cc + 1] ^= k[cc - 3];
  674. k[cc + 2] ^= k[cc - 2];
  675. k[cc + 3] ^= k[cc - 1];
  676. }
  677. }
  678. /* Encrypt a single block of 16 bytes with 'on the fly' 256 bit keying */
  679. void aes_encrypt_256( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
  680. const unsigned char key[2 * N_BLOCK], unsigned char o_key[2 * N_BLOCK] )
  681. {
  682. uint_8t s1[N_BLOCK], r, rc = 1;
  683. if(o_key != key)
  684. {
  685. block_copy( o_key, key );
  686. block_copy( o_key + 16, key + 16 );
  687. }
  688. copy_and_key( s1, in, o_key );
  689. for( r = 1 ; r < 14 ; ++r )
  690. #if defined( VERSION_1 )
  691. {
  692. mix_sub_columns(s1);
  693. if( r & 1 )
  694. add_round_key( s1, o_key + 16 );
  695. else
  696. {
  697. update_encrypt_key_256( o_key, &rc );
  698. add_round_key( s1, o_key );
  699. }
  700. }
  701. #else
  702. { uint_8t s2[N_BLOCK];
  703. mix_sub_columns( s2, s1 );
  704. if( r & 1 )
  705. copy_and_key( s1, s2, o_key + 16 );
  706. else
  707. {
  708. update_encrypt_key_256( o_key, &rc );
  709. copy_and_key( s1, s2, o_key );
  710. }
  711. }
  712. #endif
  713. shift_sub_rows( s1 );
  714. update_encrypt_key_256( o_key, &rc );
  715. copy_and_key( out, s1, o_key );
  716. }
  717. #endif
  718. #if defined( AES_DEC_256_OTFK )
  719. /* The 'on the fly' encryption key update for for 256 bit keys */
  720. static void update_decrypt_key_256( uint_8t k[2 * N_BLOCK], uint_8t *rc )
  721. { uint_8t cc;
  722. for(cc = 28; cc > 16; cc -= 4)
  723. {
  724. k[cc + 0] ^= k[cc - 4];
  725. k[cc + 1] ^= k[cc - 3];
  726. k[cc + 2] ^= k[cc - 2];
  727. k[cc + 3] ^= k[cc - 1];
  728. }
  729. k[16] ^= s_box(k[12]);
  730. k[17] ^= s_box(k[13]);
  731. k[18] ^= s_box(k[14]);
  732. k[19] ^= s_box(k[15]);
  733. for(cc = 12; cc > 0; cc -= 4)
  734. {
  735. k[cc + 0] ^= k[cc - 4];
  736. k[cc + 1] ^= k[cc - 3];
  737. k[cc + 2] ^= k[cc - 2];
  738. k[cc + 3] ^= k[cc - 1];
  739. }
  740. *rc = d2(*rc);
  741. k[0] ^= s_box(k[29]) ^ *rc;
  742. k[1] ^= s_box(k[30]);
  743. k[2] ^= s_box(k[31]);
  744. k[3] ^= s_box(k[28]);
  745. }
  746. /* Decrypt a single block of 16 bytes with 'on the fly'
  747. 256 bit keying
  748. */
  749. void aes_decrypt_256( const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK],
  750. const unsigned char key[2 * N_BLOCK], unsigned char o_key[2 * N_BLOCK] )
  751. {
  752. uint_8t s1[N_BLOCK], r, rc = 0x80;
  753. if(o_key != key)
  754. {
  755. block_copy( o_key, key );
  756. block_copy( o_key + 16, key + 16 );
  757. }
  758. copy_and_key( s1, in, o_key );
  759. inv_shift_sub_rows( s1 );
  760. for( r = 14 ; --r ; )
  761. #if defined( VERSION_1 )
  762. {
  763. if( ( r & 1 ) )
  764. {
  765. update_decrypt_key_256( o_key, &rc );
  766. add_round_key( s1, o_key + 16 );
  767. }
  768. else
  769. add_round_key( s1, o_key );
  770. inv_mix_sub_columns( s1 );
  771. }
  772. #else
  773. { uint_8t s2[N_BLOCK];
  774. if( ( r & 1 ) )
  775. {
  776. update_decrypt_key_256( o_key, &rc );
  777. copy_and_key( s2, s1, o_key + 16 );
  778. }
  779. else
  780. copy_and_key( s2, s1, o_key );
  781. inv_mix_sub_columns( s1, s2 );
  782. }
  783. #endif
  784. copy_and_key( out, s1, o_key );
  785. }
  786. #endif