aes.c 15 KB

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