aes.cc 31 KB

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