Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AES.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #include "AES.h"
  2. /*
  3. ---------------------------------------------------------------------------
  4. Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
  5. LICENSE TERMS
  6. The redistribution and use of this software (with or without changes)
  7. is allowed without the payment of fees or royalties provided that:
  8. 1. source code distributions include the above copyright notice, this
  9. list of conditions and the following disclaimer;
  10. 2. binary distributions include the above copyright notice, this list
  11. of conditions and the following disclaimer in their documentation;
  12. 3. the name of the copyright holder is not used to endorse products
  13. built using this software without specific written permission.
  14. DISCLAIMER
  15. This software is provided 'as is' with no explicit or implied warranties
  16. in respect of its properties, including, but not limited to, correctness
  17. and/or fitness for purpose.
  18. ---------------------------------------------------------------------------
  19. Issue 09/09/2006
  20. This is an AES implementation that uses only 8-bit byte operations on the
  21. cipher state (there are options to use 32-bit types if available).
  22. The combination of mix columns and byte substitution used here is based on
  23. that developed by Karl Malbrain. His contribution is acknowledged.
  24. */
  25. /* This version derived by Mark Tillotson 2012-01-23, tidied up, slimmed down
  26. and tailored to 8-bit microcontroller abilities and Arduino datatypes.
  27. The s-box and inverse s-box were retained as tables (0.5kB PROGMEM) but all
  28. the other transformations are coded to save table space. Many efficiency
  29. improvments to the routines mix_sub_columns() and inv_mix_sub_columns()
  30. (mainly common sub-expression elimination).
  31. Only the routines with precalculated subkey schedule are retained (together
  32. with set_key() - this does however mean each AES object takes 240 bytes of
  33. RAM, alas)
  34. The CBC routines side-effect the iv argument (so that successive calls work
  35. together correctly).
  36. All the encryption and decryption routines work with plain == cipher for
  37. in-place encryption, note.
  38. */
  39. /* functions for finite field multiplication in the AES Galois field */
  40. /* code was modified by george spanos <spaniakos@gmail.com>
  41. * 16/12/14
  42. */
  43. // GF(2^8) stuff
  44. #define WPOLY 0x011B
  45. #define DPOLY 0x008D
  46. const static byte s_fwd [0x100] PROGMEM = {
  47. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  48. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  49. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  50. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  51. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  52. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  53. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  54. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  55. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  56. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  57. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  58. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  59. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  60. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  61. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  62. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
  63. } ;
  64. const static byte s_inv [0x100] PROGMEM = {
  65. 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  66. 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  67. 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  68. 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  69. 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  70. 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  71. 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  72. 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  73. 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  74. 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  75. 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  76. 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  77. 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  78. 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  79. 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  80. 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
  81. } ;
  82. // times 2 in the GF(2^8)
  83. #define f2(x) (((x) & 0x80) ? (x << 1) ^ WPOLY : x << 1)
  84. #define d2(x) (((x) >> 1) ^ ((x) & 1 ? DPOLY : 0))
  85. static byte s_box (byte x)
  86. {
  87. // return fwd_affine (pgm_read_byte (&inv [x])) ;
  88. return pgm_read_byte (& s_fwd [x]) ;
  89. }
  90. // Inverse Sbox
  91. static byte is_box (byte x)
  92. {
  93. // return pgm_read_byte (&inv [inv_affine (x)]) ;
  94. return pgm_read_byte (& s_inv [x]) ;
  95. }
  96. static void xor_block (byte * d, byte * s)
  97. {
  98. for (byte i = 0 ; i < N_BLOCK ; i += 4) {
  99. *d++ ^= *s++ ; // some unrolling
  100. *d++ ^= *s++ ;
  101. *d++ ^= *s++ ;
  102. *d++ ^= *s++ ;
  103. }
  104. }
  105. static void copy_and_key (byte * d, byte * s, byte * k)
  106. {
  107. for (byte i = 0 ; i < N_BLOCK ; i += 4) {
  108. *d++ = *s++ ^ *k++ ; // some unrolling
  109. *d++ = *s++ ^ *k++ ;
  110. *d++ = *s++ ^ *k++ ;
  111. *d++ = *s++ ^ *k++ ;
  112. }
  113. }
  114. // #define add_round_key(d, k) xor_block (d, k)
  115. /* SUB ROW PHASE */
  116. static void shift_sub_rows (byte st [N_BLOCK])
  117. {
  118. st [0] = s_box (st [0]) ;
  119. st [4] = s_box (st [4]) ;
  120. st [8] = s_box (st [8]) ;
  121. st [12] = s_box (st [12]) ;
  122. byte tt = st [1] ;
  123. st [1] = s_box (st [5]) ;
  124. st [5] = s_box (st [9]) ;
  125. st [9] = s_box (st [13]) ;
  126. st [13] = s_box (tt) ;
  127. tt = st[2] ;
  128. st [2] = s_box (st [10]) ;
  129. st [10] = s_box (tt) ;
  130. tt = st[6] ;
  131. st [6] = s_box (st [14]) ;
  132. st [14] = s_box (tt) ;
  133. tt = st[15] ;
  134. st [15] = s_box (st [11]) ;
  135. st [11] = s_box (st [7]) ;
  136. st [7] = s_box (st [3]) ;
  137. st [3] = s_box (tt) ;
  138. }
  139. static void inv_shift_sub_rows (byte st[N_BLOCK])
  140. {
  141. st [0] = is_box (st[0]) ;
  142. st [4] = is_box (st [4]);
  143. st [8] = is_box (st[8]) ;
  144. st [12] = is_box (st [12]);
  145. byte tt = st[13] ;
  146. st [13] = is_box (st [9]) ;
  147. st [9] = is_box (st [5]) ;
  148. st [5] = is_box (st [1]) ;
  149. st [1] = is_box (tt) ;
  150. tt = st [2] ;
  151. st [2] = is_box (st [10]) ;
  152. st [10] = is_box (tt) ;
  153. tt = st [6] ;
  154. st [6] = is_box (st [14]) ;
  155. st [14] = is_box (tt) ;
  156. tt = st [3] ;
  157. st [3] = is_box (st [7]) ;
  158. st [7] = is_box (st [11]) ;
  159. st [11] = is_box (st [15]) ;
  160. st [15] = is_box (tt) ;
  161. }
  162. /* SUB COLUMNS PHASE */
  163. static void mix_sub_columns (byte dt[N_BLOCK], byte st[N_BLOCK])
  164. {
  165. byte j = 5 ;
  166. byte k = 10 ;
  167. byte l = 15 ;
  168. for (byte i = 0 ; i < N_BLOCK ; i += N_COL) {
  169. byte a = st [i] ;
  170. byte b = st [j] ;
  171. j = (j+N_COL) & 15 ;
  172. byte c = st [k] ;
  173. k = (k+N_COL) & 15 ;
  174. byte d = st [l] ;
  175. l = (l+N_COL) & 15 ;
  176. byte a1 = s_box (a), b1 = s_box (b), c1 = s_box (c), d1 = s_box (d) ;
  177. byte a2 = f2(a1), b2 = f2(b1), c2 = f2(c1), d2 = f2(d1) ;
  178. dt[i] = a2 ^ b2^b1 ^ c1 ^ d1 ;
  179. dt[i+1] = a1 ^ b2 ^ c2^c1 ^ d1 ;
  180. dt[i+2] = a1 ^ b1 ^ c2 ^ d2^d1 ;
  181. dt[i+3] = a2^a1 ^ b1 ^ c1 ^ d2 ;
  182. }
  183. }
  184. static void inv_mix_sub_columns (byte dt[N_BLOCK], byte st[N_BLOCK])
  185. {
  186. for (byte i = 0 ; i < N_BLOCK ; i += N_COL) {
  187. byte a1 = st [i] ;
  188. byte b1 = st [i+1] ;
  189. byte c1 = st [i+2] ;
  190. byte d1 = st [i+3] ;
  191. byte a2 = f2(a1), b2 = f2(b1), c2 = f2(c1), d2 = f2(d1) ;
  192. byte a4 = f2(a2), b4 = f2(b2), c4 = f2(c2), d4 = f2(d2) ;
  193. byte a8 = f2(a4), b8 = f2(b4), c8 = f2(c4), d8 = f2(d4) ;
  194. byte a9 = a8 ^ a1,b9 = b8 ^ b1,c9 = c8 ^ c1,d9 = d8 ^ d1 ;
  195. byte ac = a8 ^ a4,bc = b8 ^ b4,cc = c8 ^ c4,dc = d8 ^ d4 ;
  196. dt[i] = is_box (ac^a2 ^ b9^b2 ^ cc^c1 ^ d9) ;
  197. dt[(i+5)&15] = is_box (a9 ^ bc^b2 ^ c9^c2 ^ dc^d1) ;
  198. dt[(i+10)&15] = is_box (ac^a1 ^ b9 ^ cc^c2 ^ d9^d2) ;
  199. dt[(i+15)&15] = is_box (a9^a2 ^ bc^b1 ^ c9 ^ dc^d2) ;
  200. }
  201. }
  202. /******************************************************************************/
  203. AES::AES()
  204. {
  205. byte ar_iv[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 };
  206. IVC = 0x01;
  207. round = 0;
  208. pad = 0;
  209. size = 0;
  210. memset(key_sched, 0, KEY_SCHEDULE_BYTES);
  211. memcpy(iv,ar_iv,8);
  212. memcpy(iv+8,ar_iv,8);
  213. arr_pad[0] = 0x01;
  214. arr_pad[1] = 0x02;
  215. arr_pad[2] = 0x03;
  216. arr_pad[3] = 0x04;
  217. arr_pad[4] = 0x05;
  218. arr_pad[5] = 0x06;
  219. arr_pad[6] = 0x07;
  220. arr_pad[7] = 0x08;
  221. arr_pad[8] = 0x09;
  222. arr_pad[9] = 0x0a;
  223. arr_pad[10] = 0x0b;
  224. arr_pad[11] = 0x0c;
  225. arr_pad[12] = 0x0d;
  226. arr_pad[13] = 0x0e;
  227. arr_pad[14] = 0x0f;
  228. }
  229. /******************************************************************************/
  230. byte AES::set_key (byte key [], int keylen)
  231. {
  232. byte hi ;
  233. switch (keylen) {
  234. case 16:
  235. case 128:
  236. keylen = 16; // 10 rounds
  237. round = 10 ;
  238. break;
  239. case 24:
  240. case 192:
  241. keylen = 24; // 12 rounds
  242. round = 12 ;
  243. break;
  244. case 32:
  245. case 256:
  246. keylen = 32; // 14 rounds
  247. round = 14 ;
  248. break;
  249. default:
  250. round = 0;
  251. return AES_FAILURE;
  252. }
  253. hi = (round + 1) << 4 ;
  254. copy_n_bytes (key_sched, key, keylen) ;
  255. byte t[4] ;
  256. byte next = keylen ;
  257. for (byte cc = keylen, rc = 1 ; cc < hi ; cc += N_COL) {
  258. for (byte i = 0 ; i < N_COL ; i++) {
  259. t[i] = key_sched [cc-4+i] ;
  260. }
  261. if (cc == next) {
  262. next += keylen ;
  263. byte ttt = t[0] ;
  264. t[0] = s_box (t[1]) ^ rc ;
  265. t[1] = s_box (t[2]) ;
  266. t[2] = s_box (t[3]) ;
  267. t[3] = s_box (ttt) ;
  268. rc = f2 (rc) ;
  269. } else if (keylen == 32 && (cc & 31) == 16) {
  270. for (byte i = 0 ; i < 4 ; i++) {
  271. t[i] = s_box (t[i]) ;
  272. }
  273. }
  274. byte tt = cc - keylen ;
  275. for (byte i = 0 ; i < N_COL ; i++) {
  276. key_sched [cc + i] = key_sched [tt + i] ^ t[i] ;
  277. }
  278. }
  279. return AES_SUCCESS ;
  280. }
  281. /******************************************************************************/
  282. void AES::clean ()
  283. {
  284. for (byte i = 0 ; i < KEY_SCHEDULE_BYTES ; i++) {
  285. key_sched [i] = 0 ;
  286. }
  287. round = 0 ;
  288. }
  289. /******************************************************************************/
  290. void AES::copy_n_bytes (byte * d, byte * s, byte nn)
  291. {
  292. while (nn >= 4) {
  293. *d++ = *s++ ; // some unrolling
  294. *d++ = *s++ ;
  295. *d++ = *s++ ;
  296. *d++ = *s++ ;
  297. nn -= 4 ;
  298. }
  299. while (nn--) {
  300. *d++ = *s++ ;
  301. }
  302. }
  303. /******************************************************************************/
  304. byte AES::encrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK])
  305. {
  306. if (round) {
  307. byte s1 [N_BLOCK], r ;
  308. copy_and_key (s1, plain, (byte*) (key_sched)) ;
  309. for (r = 1 ; r < round ; r++) {
  310. byte s2 [N_BLOCK] ;
  311. mix_sub_columns (s2, s1) ;
  312. copy_and_key (s1, s2, (byte*) (key_sched + r * N_BLOCK)) ;
  313. }
  314. shift_sub_rows (s1) ;
  315. copy_and_key (cipher, s1, (byte*) (key_sched + r * N_BLOCK)) ;
  316. } else {
  317. return AES_FAILURE ;
  318. }
  319. return AES_SUCCESS ;
  320. }
  321. /******************************************************************************/
  322. byte AES::cbc_encrypt (byte * plain, byte * cipher, int n_block, byte iv [N_BLOCK])
  323. {
  324. while (n_block--) {
  325. xor_block (iv, plain) ;
  326. if (encrypt (iv, iv) != AES_SUCCESS) {
  327. return AES_FAILURE ;
  328. }
  329. copy_n_bytes (cipher, iv, N_BLOCK) ;
  330. plain += N_BLOCK ;
  331. cipher += N_BLOCK ;
  332. }
  333. return AES_SUCCESS ;
  334. }
  335. /******************************************************************************/
  336. byte AES::cbc_encrypt (byte * plain, byte * cipher, int n_block)
  337. {
  338. while (n_block--) {
  339. xor_block (iv, plain) ;
  340. if (encrypt (iv, iv) != AES_SUCCESS) {
  341. return AES_FAILURE ;
  342. }
  343. copy_n_bytes (cipher, iv, N_BLOCK) ;
  344. plain += N_BLOCK ;
  345. cipher += N_BLOCK ;
  346. }
  347. return AES_SUCCESS ;
  348. }
  349. /******************************************************************************/
  350. byte AES::decrypt (byte cipher [N_BLOCK], byte plain [N_BLOCK])
  351. {
  352. if (round) {
  353. byte s1 [N_BLOCK] ;
  354. copy_and_key (s1, cipher, (byte*) (key_sched + round * N_BLOCK)) ;
  355. inv_shift_sub_rows (s1) ;
  356. for (byte r = round ; --r ; ) {
  357. byte s2 [N_BLOCK] ;
  358. copy_and_key (s2, s1, (byte*) (key_sched + r * N_BLOCK)) ;
  359. inv_mix_sub_columns (s1, s2) ;
  360. }
  361. copy_and_key (plain, s1, (byte*) (key_sched)) ;
  362. } else {
  363. return AES_FAILURE ;
  364. }
  365. return AES_SUCCESS ;
  366. }
  367. /******************************************************************************/
  368. byte AES::cbc_decrypt (byte * cipher, byte * plain, int n_block, byte iv [N_BLOCK])
  369. {
  370. while (n_block--) {
  371. byte tmp [N_BLOCK] ;
  372. copy_n_bytes (tmp, cipher, N_BLOCK) ;
  373. if (decrypt (cipher, plain) != AES_SUCCESS) {
  374. return AES_FAILURE ;
  375. }
  376. xor_block (plain, iv) ;
  377. copy_n_bytes (iv, tmp, N_BLOCK) ;
  378. plain += N_BLOCK ;
  379. cipher += N_BLOCK;
  380. }
  381. return AES_SUCCESS ;
  382. }
  383. /******************************************************************************/
  384. byte AES::cbc_decrypt (byte * cipher, byte * plain, int n_block)
  385. {
  386. while (n_block--) {
  387. byte tmp [N_BLOCK] ;
  388. copy_n_bytes (tmp, cipher, N_BLOCK) ;
  389. if (decrypt (cipher, plain) != AES_SUCCESS) {
  390. return AES_FAILURE ;
  391. }
  392. xor_block (plain, iv) ;
  393. copy_n_bytes (iv, tmp, N_BLOCK) ;
  394. plain += N_BLOCK ;
  395. cipher += N_BLOCK;
  396. }
  397. return AES_SUCCESS ;
  398. }
  399. /*****************************************************************************/
  400. void AES::set_IV(unsigned long long int IVCl)
  401. {
  402. memcpy(iv,&IVCl,8);
  403. memcpy(iv+8,&IVCl,8);
  404. IVC = IVCl;
  405. }
  406. /******************************************************************************/
  407. void AES::iv_inc()
  408. {
  409. IVC += 1;
  410. memcpy(iv,&IVC,8);
  411. memcpy(iv+8,&IVC,8);
  412. }
  413. /******************************************************************************/
  414. int AES::get_size()
  415. {
  416. return size;
  417. }
  418. /******************************************************************************/
  419. void AES::set_size(int sizel)
  420. {
  421. size = sizel;
  422. }
  423. /******************************************************************************/
  424. void AES::get_IV(byte *out)
  425. {
  426. memcpy(out,&IVC,8);
  427. memcpy(out+8,&IVC,8);
  428. }
  429. /******************************************************************************/
  430. void AES::calc_size_n_pad(int p_size)
  431. {
  432. int s_of_p = p_size - 1;
  433. if ( s_of_p % N_BLOCK == 0) {
  434. size = s_of_p;
  435. } else {
  436. size = s_of_p + (N_BLOCK-(s_of_p % N_BLOCK));
  437. }
  438. pad = size - s_of_p;
  439. }
  440. /******************************************************************************/
  441. void AES::padPlaintext(void* in,byte* out)
  442. {
  443. memcpy(out,in,size);
  444. for (int i = size-pad; i < size; i++) {
  445. ;
  446. out[i] = arr_pad[pad - 1];
  447. }
  448. }
  449. /******************************************************************************/
  450. bool AES::CheckPad(byte* in,int lsize)
  451. {
  452. if (in[lsize-1] <= 0x0f) {
  453. int lpad = (int)in[lsize-1];
  454. for (int i = lsize - 1; i >= lsize-lpad; i--) {
  455. if (arr_pad[lpad - 1] != in[i]) {
  456. return false;
  457. }
  458. }
  459. } else {
  460. return true;
  461. }
  462. return true;
  463. }
  464. /******************************************************************************/
  465. void AES::printArray(byte output[],bool p_pad)
  466. {
  467. uint8_t i,j;
  468. uint8_t loops = size/N_BLOCK;
  469. uint8_t outp = N_BLOCK;
  470. for (j = 0; j < loops; j += 1) {
  471. if (p_pad && (j == (loops - 1)) ) {
  472. outp = N_BLOCK - pad;
  473. }
  474. for (i = 0; i < outp; i++) {
  475. printf_P(PSTR("%c"),output[j*N_BLOCK + i]);
  476. }
  477. }
  478. printf_P(PSTR("\n"));
  479. }
  480. /******************************************************************************/
  481. void AES::printArray(byte output[],int sizel)
  482. {
  483. for (int i = 0; i < sizel; i++) {
  484. printf_P(PSTR("%x"),output[i]);
  485. }
  486. printf_P(PSTR("\n"));
  487. }
  488. /******************************************************************************/
  489. void AES::do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits,
  490. byte ivl [N_BLOCK])
  491. {
  492. calc_size_n_pad(size_p);
  493. byte plain_p[get_size()];
  494. padPlaintext(plain,plain_p);
  495. int blocks = get_size() / N_BLOCK;
  496. set_key (key, bits) ;
  497. cbc_encrypt (plain_p, cipher, blocks, ivl);
  498. }
  499. /******************************************************************************/
  500. void AES::do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits)
  501. {
  502. calc_size_n_pad(size_p);
  503. byte plain_p[get_size()];
  504. padPlaintext(plain,plain_p);
  505. int blocks = get_size() / N_BLOCK;
  506. set_key (key, bits) ;
  507. cbc_encrypt (plain_p, cipher, blocks);
  508. }
  509. /******************************************************************************/
  510. void AES::do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits,
  511. byte ivl [N_BLOCK])
  512. {
  513. set_size(size_c);
  514. int blocks = size_c / N_BLOCK;
  515. set_key (key, bits);
  516. cbc_decrypt (cipher,plain, blocks, ivl);
  517. }
  518. /******************************************************************************/
  519. void AES::do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits)
  520. {
  521. set_size(size_c);
  522. int blocks = size_c / N_BLOCK;
  523. set_key (key, bits);
  524. cbc_decrypt (cipher,plain, blocks);
  525. }
  526. /******************************************************************************/
  527. #if defined(AES_LINUX)
  528. double AES::millis()
  529. {
  530. gettimeofday(&tv, NULL);
  531. return (tv.tv_sec + 0.000001 * tv.tv_usec);
  532. }
  533. #endif