aes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include <string.h> // CBC mode, for memset
  2. #include <stdio.h>
  3. #include "benchmarks/aes/aes.h"
  4. // The number of columns comprising a state in AES. This is a constant in AES. Value=4
  5. #define Nb 4
  6. #define Nk 4 // The number of 32 bit words in a key.
  7. #define Nr 10 // The number of rounds in AES Cipher.
  8. #define getSBoxValue(num) (sbox[(num)])
  9. // state - array holding the intermediate results during decryption.
  10. typedef uint8_t state_t[4][4];
  11. static const uint8_t sbox[256] = {
  12. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  13. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  14. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  15. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  16. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  17. 0x09, 0x83, 0x2c, 0x1a, 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xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16};
  29. static const uint8_t rsbox[256] = {
  30. 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  31. 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  32. 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  33. 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  34. 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  35. 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  36. 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  37. 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  38. 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  39. 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  40. 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  41. 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  42. 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  43. 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  44. 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  45. 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d};
  46. static const uint8_t Rcon[11] = {
  47. 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36};
  48. static void XorWithIv(uint8_t *buf, const uint8_t *Iv)
  49. {
  50. uint8_t i;
  51. for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size
  52. {
  53. buf[i] ^= Iv[i];
  54. }
  55. }
  56. // This function adds the round key to state.
  57. // The round key is added to the state by an XOR function.
  58. static void AddRoundKey(uint8_t round, state_t *state, const uint8_t *RoundKey)
  59. {
  60. uint8_t i, j;
  61. for (i = 0; i < 4; ++i)
  62. {
  63. for (j = 0; j < 4; ++j)
  64. {
  65. (*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j];
  66. }
  67. }
  68. }
  69. // The SubBytes Function Substitutes the values in the
  70. // state matrix with values in an S-box.
  71. static void SubBytes(state_t *state)
  72. {
  73. uint8_t i, j;
  74. for (i = 0; i < 4; ++i)
  75. {
  76. for (j = 0; j < 4; ++j)
  77. {
  78. (*state)[j][i] = getSBoxValue((*state)[j][i]);
  79. }
  80. }
  81. }
  82. // The ShiftRows() function shifts the rows in the state to the left.
  83. // Each row is shifted with different offset.
  84. // Offset = Row number. So the first row is not shifted.
  85. static void ShiftRows(state_t *state)
  86. {
  87. uint8_t temp;
  88. // Rotate first row 1 columns to left
  89. temp = (*state)[0][1];
  90. (*state)[0][1] = (*state)[1][1];
  91. (*state)[1][1] = (*state)[2][1];
  92. (*state)[2][1] = (*state)[3][1];
  93. (*state)[3][1] = temp;
  94. // Rotate second row 2 columns to left
  95. temp = (*state)[0][2];
  96. (*state)[0][2] = (*state)[2][2];
  97. (*state)[2][2] = temp;
  98. temp = (*state)[1][2];
  99. (*state)[1][2] = (*state)[3][2];
  100. (*state)[3][2] = temp;
  101. // Rotate third row 3 columns to left
  102. temp = (*state)[0][3];
  103. (*state)[0][3] = (*state)[3][3];
  104. (*state)[3][3] = (*state)[2][3];
  105. (*state)[2][3] = (*state)[1][3];
  106. (*state)[1][3] = temp;
  107. }
  108. static uint8_t xtime(uint8_t x)
  109. {
  110. return ((x << 1) ^ (((x >> 7) & 1) * 0x1b));
  111. }
  112. // MixColumns function mixes the columns of the state matrix
  113. static void MixColumns(state_t *state)
  114. {
  115. uint8_t i;
  116. uint8_t Tmp, Tm, t;
  117. for (i = 0; i < 4; ++i)
  118. {
  119. t = (*state)[i][0];
  120. Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3];
  121. Tm = (*state)[i][0] ^ (*state)[i][1];
  122. Tm = xtime(Tm);
  123. (*state)[i][0] ^= Tm ^ Tmp;
  124. Tm = (*state)[i][1] ^ (*state)[i][2];
  125. Tm = xtime(Tm);
  126. (*state)[i][1] ^= Tm ^ Tmp;
  127. Tm = (*state)[i][2] ^ (*state)[i][3];
  128. Tm = xtime(Tm);
  129. (*state)[i][2] ^= Tm ^ Tmp;
  130. Tm = (*state)[i][3] ^ t;
  131. Tm = xtime(Tm);
  132. (*state)[i][3] ^= Tm ^ Tmp;
  133. }
  134. }
  135. static void KeyExpansion(uint8_t *RoundKey, const uint8_t *Key)
  136. {
  137. unsigned i, j, k;
  138. uint8_t tempa[4]; // Used for the column/row operations
  139. // The first round key is the key itself.
  140. for (i = 0; i < Nk; ++i)
  141. {
  142. RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
  143. RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
  144. RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
  145. RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
  146. }
  147. // All other round keys are found from the previous round keys.
  148. for (i = Nk; i < Nb * (Nr + 1); ++i)
  149. {
  150. {
  151. k = (i - 1) * 4;
  152. tempa[0] = RoundKey[k + 0];
  153. tempa[1] = RoundKey[k + 1];
  154. tempa[2] = RoundKey[k + 2];
  155. tempa[3] = RoundKey[k + 3];
  156. }
  157. if (i % Nk == 0)
  158. {
  159. // This function shifts the 4 bytes in a word to the left once.
  160. // [a0,a1,a2,a3] becomes [a1,a2,a3,a0]
  161. // Function RotWord()
  162. {
  163. const uint8_t u8tmp = tempa[0];
  164. tempa[0] = tempa[1];
  165. tempa[1] = tempa[2];
  166. tempa[2] = tempa[3];
  167. tempa[3] = u8tmp;
  168. }
  169. // SubWord() is a function that takes a four-byte input word and
  170. // applies the S-box to each of the four bytes to produce an output word.
  171. // Function Subword()
  172. {
  173. tempa[0] = getSBoxValue(tempa[0]);
  174. tempa[1] = getSBoxValue(tempa[1]);
  175. tempa[2] = getSBoxValue(tempa[2]);
  176. tempa[3] = getSBoxValue(tempa[3]);
  177. }
  178. tempa[0] = tempa[0] ^ Rcon[i / Nk];
  179. }
  180. j = i * 4;
  181. k = (i - Nk) * 4;
  182. RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
  183. RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
  184. RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
  185. RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
  186. }
  187. }
  188. // Cipher is the main function that encrypts the PlainText.
  189. static void Cipher(state_t *state, const uint8_t *RoundKey)
  190. {
  191. uint8_t round = 0;
  192. // Add the First round key to the state before starting the rounds.
  193. AddRoundKey(0, state, RoundKey);
  194. // There will be Nr rounds.
  195. // The first Nr-1 rounds are identical.
  196. // These Nr rounds are executed in the loop below.
  197. // Last one without MixColumns()
  198. for (round = 1;; ++round)
  199. {
  200. SubBytes(state);
  201. ShiftRows(state);
  202. if (round == Nr)
  203. {
  204. break;
  205. }
  206. MixColumns(state);
  207. AddRoundKey(round, state, RoundKey);
  208. }
  209. // Add round key to last round
  210. AddRoundKey(Nr, state, RoundKey);
  211. }
  212. void AES_init_ctx_iv(struct AES_ctx *ctx, const uint8_t *key, const uint8_t *iv)
  213. {
  214. KeyExpansion(ctx->RoundKey, key);
  215. memcpy(ctx->Iv, iv, AES_BLOCKLEN);
  216. }
  217. void AES_CBC_encrypt_buffer(struct AES_ctx *ctx, uint8_t *buf, size_t length)
  218. {
  219. size_t i;
  220. uint8_t *Iv = ctx->Iv;
  221. for (i = 0; i < length; i += AES_BLOCKLEN)
  222. {
  223. XorWithIv(buf, Iv);
  224. Cipher((state_t *)buf, ctx->RoundKey);
  225. Iv = buf;
  226. buf += AES_BLOCKLEN;
  227. }
  228. /* store Iv in ctx for next call */
  229. memcpy(ctx->Iv, Iv, AES_BLOCKLEN);
  230. }
  231. void vAes() {
  232. int IMC_REPEAT = 1000;
  233. for(int imc=0; imc < IMC_REPEAT; imc++) {
  234. uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
  235. uint8_t out[] = {0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  236. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
  237. 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
  238. 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7};
  239. uint8_t iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
  240. uint8_t in[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  241. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  242. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  243. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10};
  244. // struct AES_ctx ctx;
  245. uint8_t roundKey[AES_keyExpSize];
  246. uint8_t ctxIv[AES_BLOCKLEN];
  247. // AES_init_ctx_iv(&ctx, key, iv);
  248. // KeyExpansion(ctx.RoundKey, key);
  249. {
  250. uint8_t *RoundKey = roundKey;
  251. const uint8_t *Key = key;
  252. unsigned i, j, k;
  253. uint8_t tempa[4]; // Used for the column/row operations
  254. // The first round key is the key itself.
  255. for (i = 0; i < Nk; ++i)
  256. {
  257. RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
  258. RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
  259. RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
  260. RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
  261. }
  262. // All other round keys are found from the previous round keys.
  263. for (i = Nk; i < Nb * (Nr + 1); ++i)
  264. {
  265. {
  266. k = (i - 1) * 4;
  267. tempa[0] = RoundKey[k + 0];
  268. tempa[1] = RoundKey[k + 1];
  269. tempa[2] = RoundKey[k + 2];
  270. tempa[3] = RoundKey[k + 3];
  271. }
  272. if (i % Nk == 0)
  273. {
  274. // This function shifts the 4 bytes in a word to the left once.
  275. // [a0,a1,a2,a3] becomes [a1,a2,a3,a0]
  276. // Function RotWord()
  277. {
  278. const uint8_t u8tmp = tempa[0];
  279. tempa[0] = tempa[1];
  280. tempa[1] = tempa[2];
  281. tempa[2] = tempa[3];
  282. tempa[3] = u8tmp;
  283. }
  284. // SubWord() is a function that takes a four-byte input word and
  285. // applies the S-box to each of the four bytes to produce an output word.
  286. // Function Subword()
  287. {
  288. tempa[0] = getSBoxValue(tempa[0]);
  289. tempa[1] = getSBoxValue(tempa[1]);
  290. tempa[2] = getSBoxValue(tempa[2]);
  291. tempa[3] = getSBoxValue(tempa[3]);
  292. }
  293. tempa[0] = tempa[0] ^ Rcon[i / Nk];
  294. }
  295. j = i * 4;
  296. k = (i - Nk) * 4;
  297. RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
  298. RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
  299. RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
  300. RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
  301. }
  302. }
  303. memcpy(ctxIv, iv, AES_BLOCKLEN);
  304. // AES_CBC_encrypt_buffer(&ctx, in, 64);
  305. {
  306. size_t i;
  307. uint8_t *Iv = ctxIv;
  308. // uint8_t *buf = in;
  309. for (i = 0; i < 64; i += AES_BLOCKLEN)
  310. {
  311. // XorWithIv(buf, Iv);
  312. {
  313. uint8_t j;
  314. for (j = 0; j < AES_BLOCKLEN; ++j) // The block in AES is always 128bit no matter the key size
  315. {
  316. // buf[j] ^= Iv[j];
  317. in[i + j] ^= Iv[j];
  318. }
  319. }
  320. // Cipher((state_t *)buf, roundKey);
  321. Cipher((state_t *)(in + i), roundKey);
  322. // Iv = buf;
  323. Iv = in + i;
  324. // buf += AES_BLOCKLEN;
  325. }
  326. /* store Iv in ctx for next call */
  327. memcpy(ctxIv, Iv, AES_BLOCKLEN);
  328. }
  329. if(imc == IMC_REPEAT-1) {
  330. printf("CBC encrypt: ");
  331. if (0 == memcmp((char *)out, (char *)in, 64))
  332. {
  333. printf("SUCCESS!\r\n");
  334. }
  335. else
  336. {
  337. printf("FAILURE!\r\n");
  338. }
  339. }
  340. }
  341. return;
  342. }