aes.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. * refrenced from https://github.com/kokke/tiny-AES-c
  11. */
  12. #ifndef __ASIO2_AES_IMPL_HPP__
  13. #define __ASIO2_AES_IMPL_HPP__
  14. #include <cassert>
  15. #include <cstdint>
  16. #include <cstring>
  17. #include <string>
  18. #include <vector>
  19. #include <array>
  20. #include <sstream>
  21. namespace asio2
  22. {
  23. class aes
  24. {
  25. protected:
  26. // state - array holding the intermediate results during decryption.
  27. typedef uint8_t state_t[4][4];
  28. // Block length in bytes - AES is 128b block only
  29. static constexpr int AES_BLOCKLEN = 16;
  30. public:
  31. enum class mode_t
  32. {
  33. cbc,
  34. ecb,
  35. ctr,
  36. //ocf, // not supported
  37. //cfb, // not supported
  38. };
  39. public:
  40. /*
  41. * if key.size() <= 16, key will be resized to 16 and padded with '\0', the data block is 128 bit.
  42. * if key.size() > 16 && <= 24, key will be resized to 24 and padded with '\0', the data block is 192 bit.
  43. * if key.size() > 24, key will be resized to 32 and padded with '\0', the data block is 256 bit.
  44. */
  45. explicit aes(std::string key, mode_t mode = mode_t::ecb) : key_(std::move(key)), mode_(mode)
  46. {
  47. init();
  48. }
  49. ~aes()
  50. {
  51. }
  52. aes(const aes & other) : key_(other.key_)
  53. {
  54. init();
  55. }
  56. aes & operator=(const aes & other)
  57. {
  58. key_ = other.key_;
  59. init();
  60. return (*this);
  61. }
  62. aes(aes && other) : key_(std::move(other.key_))
  63. {
  64. init();
  65. }
  66. aes & operator=(aes && other)
  67. {
  68. key_ = std::move(other.key_);
  69. init();
  70. return (*this);
  71. }
  72. mode_t mode() { return mode_; }
  73. mode_t set_mode() { return mode_; }
  74. aes & mode(mode_t mode) { mode_ = mode; return (*this); }
  75. aes & get_mode(mode_t mode) { mode_ = mode; return (*this); }
  76. aes & iv(uint8_t iv[AES_BLOCKLEN]) { memcpy(&Iv_[0], iv, AES_BLOCKLEN); return (*this); }
  77. /*
  78. * note : if msg contains '\0',there may be a wrong result when decrypt
  79. */
  80. std::string encrypt(std::string msg)
  81. {
  82. if (msg.empty())
  83. return std::string{};
  84. if ((msg.size() % AES_BLOCKLEN) != 0)
  85. {
  86. msg.resize(msg.size() + AES_BLOCKLEN - (msg.size() % AES_BLOCKLEN));
  87. }
  88. switch (mode_)
  89. {
  90. case mode_t::cbc: return encrypt_with_cbc(std::move(msg));
  91. case mode_t::ecb: return encrypt_with_ecb(std::move(msg));
  92. case mode_t::ctr: return encrypt_with_ctr(std::move(msg));
  93. }
  94. return std::string{};
  95. }
  96. std::string decrypt(std::string msg)
  97. {
  98. if (msg.empty() || (msg.size() % AES_BLOCKLEN) != 0)
  99. return std::string{};
  100. std::string s{};
  101. switch (mode_)
  102. {
  103. case mode_t::cbc: s = decrypt_with_cbc(std::move(msg)); break;
  104. case mode_t::ecb: s = decrypt_with_ecb(std::move(msg)); break;
  105. case mode_t::ctr: s = decrypt_with_ctr(std::move(msg)); break;
  106. }
  107. while (!s.empty() && s.back() == '\0')
  108. s.erase(s.size() - 1);
  109. return s;
  110. }
  111. protected:
  112. std::string encrypt_with_cbc(std::string msg)
  113. {
  114. AES_init_ctx((const uint8_t*)key_.data());
  115. AES_CBC_encrypt_buffer((uint8_t*)msg.data(), uint32_t(msg.size()));
  116. return msg;
  117. }
  118. std::string decrypt_with_cbc(std::string msg)
  119. {
  120. AES_init_ctx((const uint8_t*)key_.data());
  121. AES_CBC_decrypt_buffer((uint8_t*)msg.data(), uint32_t(msg.size()));
  122. return msg;
  123. }
  124. std::string encrypt_with_ecb(std::string msg)
  125. {
  126. AES_init_ctx((const uint8_t*)key_.data());
  127. uint8_t * buf = (uint8_t*)msg.data();
  128. for (std::size_t i = 0; i < msg.size(); i += AES_BLOCKLEN)
  129. {
  130. AES_ECB_encrypt(buf);
  131. buf += AES_BLOCKLEN;
  132. }
  133. return msg;
  134. }
  135. std::string decrypt_with_ecb(std::string msg)
  136. {
  137. AES_init_ctx((const uint8_t*)key_.data());
  138. uint8_t * buf = (uint8_t*)msg.data();
  139. for (std::size_t i = 0; i < msg.size(); i += AES_BLOCKLEN)
  140. {
  141. AES_ECB_decrypt(buf);
  142. buf += AES_BLOCKLEN;
  143. }
  144. return msg;
  145. }
  146. std::string encrypt_with_ctr(std::string msg)
  147. {
  148. AES_init_ctx((const uint8_t*)key_.data());
  149. AES_CTR_xcrypt_buffer((uint8_t*)msg.data(), uint32_t(msg.size()));
  150. return msg;
  151. }
  152. std::string decrypt_with_ctr(std::string msg)
  153. {
  154. AES_init_ctx((const uint8_t*)key_.data());
  155. AES_CTR_xcrypt_buffer((uint8_t*)msg.data(), uint32_t(msg.size()));
  156. return msg;
  157. }
  158. protected:
  159. void init()
  160. {
  161. if (key_.size() <= std::size_t(16)) // 128/8
  162. {
  163. key_.resize(16);
  164. Nk = 4; // The number of 32 bit words in a key.
  165. Nr = 10; // The number of rounds in AES Cipher.
  166. RoundKey_.resize(176);
  167. }
  168. else if (key_.size() <= std::size_t(24)) // 192/8
  169. {
  170. key_.resize(24);
  171. Nk = 6;
  172. Nr = 12;
  173. RoundKey_.resize(208);
  174. }
  175. else// 256/8
  176. {
  177. key_.resize(32);
  178. Nk = 8;
  179. Nr = 14;
  180. RoundKey_.resize(240);
  181. }
  182. }
  183. void AES_init_ctx(const uint8_t* key)
  184. {
  185. KeyExpansion(&RoundKey_[0], key);
  186. }
  187. void AES_init_ctx_iv(const uint8_t* key, const uint8_t* iv)
  188. {
  189. KeyExpansion(&RoundKey_[0], key);
  190. memcpy(&Iv_[0], iv, AES_BLOCKLEN);
  191. }
  192. void AES_ctx_set_iv(const uint8_t* iv)
  193. {
  194. memcpy(&Iv_[0], iv, AES_BLOCKLEN);
  195. }
  196. void AES_ECB_encrypt(uint8_t* buf)
  197. {
  198. // The next function call encrypts the PlainText with the Key using AES algorithm.
  199. Cipher((state_t*)buf, &RoundKey_[0]);
  200. }
  201. void AES_ECB_decrypt(uint8_t* buf)
  202. {
  203. // The next function call decrypts the PlainText with the Key using AES algorithm.
  204. InvCipher((state_t*)buf, &RoundKey_[0]);
  205. }
  206. void AES_CBC_encrypt_buffer(uint8_t* buf, uint32_t length)
  207. {
  208. uint32_t i;
  209. uint8_t *iv = &Iv_[0];
  210. for (i = 0; i < length; i += AES_BLOCKLEN)
  211. {
  212. XorWithIv(buf, iv);
  213. Cipher((state_t*)buf, &RoundKey_[0]);
  214. iv = buf;
  215. buf += AES_BLOCKLEN;
  216. }
  217. /* store Iv in ctx for next call */
  218. memcpy(&Iv_[0], iv, AES_BLOCKLEN);
  219. }
  220. void AES_CBC_decrypt_buffer(uint8_t* buf, uint32_t length)
  221. {
  222. uint32_t i;
  223. uint8_t storeNextIv[AES_BLOCKLEN];
  224. for (i = 0; i < length; i += AES_BLOCKLEN)
  225. {
  226. memcpy(storeNextIv, buf, AES_BLOCKLEN);
  227. InvCipher((state_t*)buf, &RoundKey_[0]);
  228. XorWithIv(buf, &Iv_[0]);
  229. memcpy(&Iv_[0], storeNextIv, AES_BLOCKLEN);
  230. buf += AES_BLOCKLEN;
  231. }
  232. }
  233. void AES_CTR_xcrypt_buffer(uint8_t* buf, uint32_t length)
  234. {
  235. uint8_t buffer[AES_BLOCKLEN];
  236. unsigned i;
  237. int bi;
  238. for (i = 0, bi = AES_BLOCKLEN; i < length; ++i, ++bi)
  239. {
  240. if (bi == AES_BLOCKLEN) /* we need to regen xor compliment in buffer */
  241. {
  242. memcpy(buffer, &Iv_[0], AES_BLOCKLEN);
  243. Cipher((state_t*)buffer, &RoundKey_[0]);
  244. /* Increment Iv and handle overflow */
  245. for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi)
  246. {
  247. /* inc will overflow */
  248. if (Iv_[bi] == 255)
  249. {
  250. Iv_[bi] = uint8_t(0);
  251. continue;
  252. }
  253. Iv_[bi] = uint8_t(Iv_[bi] + uint8_t(1));
  254. break;
  255. }
  256. bi = 0;
  257. }
  258. buf[i] = (buf[i] ^ buffer[bi]);
  259. }
  260. }
  261. void XorWithIv(uint8_t* buf, const uint8_t* iv)
  262. {
  263. uint8_t i;
  264. for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size
  265. {
  266. buf[i] ^= iv[i];
  267. }
  268. }
  269. inline uint8_t Multiply(uint8_t x, uint8_t y)
  270. {
  271. return (uint8_t((((y & 1) * x) ^
  272. ((y>>1 & 1) * xtime(x)) ^
  273. ((y>>2 & 1) * xtime(xtime(x))) ^
  274. ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^
  275. ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))))); /* this last call to xtime() can be omitted */
  276. }
  277. // This function adds the round key to state.
  278. // The round key is added to the state by an XOR function.
  279. void AddRoundKey(uint8_t round, state_t* state, const uint8_t* RoundKey)
  280. {
  281. uint8_t i,j;
  282. for (i = 0; i < 4; ++i)
  283. {
  284. for (j = 0; j < 4; ++j)
  285. {
  286. (*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j];
  287. }
  288. }
  289. }
  290. // The SubBytes Function Substitutes the values in the
  291. // state matrix with values in an S-box.
  292. void SubBytes(state_t* state)
  293. {
  294. uint8_t i, j;
  295. for (i = 0; i < 4; ++i)
  296. {
  297. for (j = 0; j < 4; ++j)
  298. {
  299. (*state)[j][i] = getSBoxValue((*state)[j][i]);
  300. }
  301. }
  302. }
  303. // The ShiftRows() function shifts the rows in the state to the left.
  304. // Each row is shifted with different offset.
  305. // Offset = Row number. So the first row is not shifted.
  306. void ShiftRows(state_t* state)
  307. {
  308. uint8_t temp;
  309. // Rotate first row 1 columns to left
  310. temp = (*state)[0][1];
  311. (*state)[0][1] = (*state)[1][1];
  312. (*state)[1][1] = (*state)[2][1];
  313. (*state)[2][1] = (*state)[3][1];
  314. (*state)[3][1] = temp;
  315. // Rotate second row 2 columns to left
  316. temp = (*state)[0][2];
  317. (*state)[0][2] = (*state)[2][2];
  318. (*state)[2][2] = temp;
  319. temp = (*state)[1][2];
  320. (*state)[1][2] = (*state)[3][2];
  321. (*state)[3][2] = temp;
  322. // Rotate third row 3 columns to left
  323. temp = (*state)[0][3];
  324. (*state)[0][3] = (*state)[3][3];
  325. (*state)[3][3] = (*state)[2][3];
  326. (*state)[2][3] = (*state)[1][3];
  327. (*state)[1][3] = temp;
  328. }
  329. inline uint8_t xtime(uint8_t x)
  330. {
  331. return (uint8_t(((x<<1) ^ (((x>>7) & 1) * 0x1b))));
  332. }
  333. // MixColumns function mixes the columns of the state matrix
  334. void MixColumns(state_t* state)
  335. {
  336. uint8_t i;
  337. uint8_t Tmp, Tm, t;
  338. for (i = 0; i < 4; ++i)
  339. {
  340. t = (*state)[i][0];
  341. Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;
  342. Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ;
  343. Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ;
  344. Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ;
  345. Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ;
  346. }
  347. }
  348. // MixColumns function mixes the columns of the state matrix.
  349. // The method used to multiply may be difficult to understand for the inexperienced.
  350. // Please use the references to gain more information.
  351. void InvMixColumns(state_t* state)
  352. {
  353. int i;
  354. uint8_t a, b, c, d;
  355. for (i = 0; i < 4; ++i)
  356. {
  357. a = (*state)[i][0];
  358. b = (*state)[i][1];
  359. c = (*state)[i][2];
  360. d = (*state)[i][3];
  361. (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
  362. (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
  363. (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
  364. (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
  365. }
  366. }
  367. // The SubBytes Function Substitutes the values in the
  368. // state matrix with values in an S-box.
  369. void InvSubBytes(state_t* state)
  370. {
  371. uint8_t i, j;
  372. for (i = 0; i < 4; ++i)
  373. {
  374. for (j = 0; j < 4; ++j)
  375. {
  376. (*state)[j][i] = getSBoxInvert((*state)[j][i]);
  377. }
  378. }
  379. }
  380. void InvShiftRows(state_t* state)
  381. {
  382. uint8_t temp;
  383. // Rotate first row 1 columns to right
  384. temp = (*state)[3][1];
  385. (*state)[3][1] = (*state)[2][1];
  386. (*state)[2][1] = (*state)[1][1];
  387. (*state)[1][1] = (*state)[0][1];
  388. (*state)[0][1] = temp;
  389. // Rotate second row 2 columns to right
  390. temp = (*state)[0][2];
  391. (*state)[0][2] = (*state)[2][2];
  392. (*state)[2][2] = temp;
  393. temp = (*state)[1][2];
  394. (*state)[1][2] = (*state)[3][2];
  395. (*state)[3][2] = temp;
  396. // Rotate third row 3 columns to right
  397. temp = (*state)[0][3];
  398. (*state)[0][3] = (*state)[1][3];
  399. (*state)[1][3] = (*state)[2][3];
  400. (*state)[2][3] = (*state)[3][3];
  401. (*state)[3][3] = temp;
  402. }
  403. inline uint8_t getSBoxValue(uint8_t num)
  404. {
  405. return sbox[num];
  406. }
  407. inline uint8_t getSBoxInvert(uint8_t num)
  408. {
  409. return rsbox[num];
  410. }
  411. // This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states.
  412. void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key)
  413. {
  414. unsigned i, j, k;
  415. uint8_t tempa[4]; // Used for the column/row operations
  416. // The first round key is the key itself.
  417. for (i = 0; i < Nk; ++i)
  418. {
  419. RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
  420. RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
  421. RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
  422. RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
  423. }
  424. // All other round keys are found from the previous round keys.
  425. for (i = Nk; i < Nb * (Nr + 1); ++i)
  426. {
  427. {
  428. k = (i - 1) * 4;
  429. tempa[0] = RoundKey[k + 0];
  430. tempa[1] = RoundKey[k + 1];
  431. tempa[2] = RoundKey[k + 2];
  432. tempa[3] = RoundKey[k + 3];
  433. }
  434. if (i % Nk == 0)
  435. {
  436. // This function shifts the 4 bytes in a word to the left once.
  437. // [a0,a1,a2,a3] becomes [a1,a2,a3,a0]
  438. // Function RotWord()
  439. {
  440. const uint8_t u8tmp = tempa[0];
  441. tempa[0] = tempa[1];
  442. tempa[1] = tempa[2];
  443. tempa[2] = tempa[3];
  444. tempa[3] = u8tmp;
  445. }
  446. // SubWord() is a function that takes a four-byte input word and
  447. // applies the S-box to each of the four bytes to produce an output word.
  448. // Function Subword()
  449. {
  450. tempa[0] = getSBoxValue(tempa[0]);
  451. tempa[1] = getSBoxValue(tempa[1]);
  452. tempa[2] = getSBoxValue(tempa[2]);
  453. tempa[3] = getSBoxValue(tempa[3]);
  454. }
  455. tempa[0] = tempa[0] ^ Rcon[i / Nk];
  456. }
  457. if (Nk == 8) // AES256
  458. {
  459. if (i % Nk == 4)
  460. {
  461. // Function Subword()
  462. {
  463. tempa[0] = getSBoxValue(tempa[0]);
  464. tempa[1] = getSBoxValue(tempa[1]);
  465. tempa[2] = getSBoxValue(tempa[2]);
  466. tempa[3] = getSBoxValue(tempa[3]);
  467. }
  468. }
  469. }
  470. j = i * 4; k = (i - Nk) * 4;
  471. RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
  472. RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
  473. RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
  474. RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
  475. }
  476. }
  477. // Cipher is the main function that encrypts the PlainText.
  478. void Cipher(state_t* state, const uint8_t* RoundKey)
  479. {
  480. uint8_t round = 0;
  481. // Add the First round key to the state before starting the rounds.
  482. AddRoundKey(0, state, RoundKey);
  483. // There will be Nr rounds.
  484. // The first Nr-1 rounds are identical.
  485. // These Nr rounds are executed in the loop below.
  486. // Last one without MixColumns()
  487. for (round = 1; ; ++round)
  488. {
  489. SubBytes(state);
  490. ShiftRows(state);
  491. if (round == Nr) {
  492. break;
  493. }
  494. MixColumns(state);
  495. AddRoundKey(round, state, RoundKey);
  496. }
  497. // Add round key to last round
  498. AddRoundKey(uint8_t(Nr), state, RoundKey);
  499. }
  500. void InvCipher(state_t* state, const uint8_t* RoundKey)
  501. {
  502. uint8_t round = 0;
  503. // Add the First round key to the state before starting the rounds.
  504. AddRoundKey(uint8_t(Nr), state, RoundKey);
  505. // There will be Nr rounds.
  506. // The first Nr-1 rounds are identical.
  507. // These Nr rounds are executed in the loop below.
  508. // Last one without InvMixColumn()
  509. for (round = uint8_t(Nr - 1); ; --round)
  510. {
  511. InvShiftRows(state);
  512. InvSubBytes(state);
  513. AddRoundKey(round, state, RoundKey);
  514. if (round == 0) {
  515. break;
  516. }
  517. InvMixColumns(state);
  518. }
  519. }
  520. protected:
  521. std::string key_;
  522. mode_t mode_ = mode_t::ecb;
  523. // The number of columns comprising a state in AES. This is a constant in AES. Value=4
  524. unsigned int Nb = 4;
  525. unsigned int Nk = 4; // The number of 32 bit words in a key.
  526. unsigned int Nr = 10; // The number of rounds in AES Cipher.
  527. std::vector<uint8_t> RoundKey_{};
  528. std::array<uint8_t, AES_BLOCKLEN> Iv_{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
  529. // The lookup-tables are marked const so they can be placed in read-only storage instead of RAM
  530. // The numbers below can be computed dynamically trading ROM for RAM -
  531. // This can be useful in (embedded) bootloader applications, where ROM is often limited.
  532. const uint8_t sbox[256] =
  533. {
  534. //0 1 2 3 4 5 6 7 8 9 A B C D E F
  535. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  536. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  537. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  538. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  539. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  540. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  541. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  542. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  543. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  544. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  545. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  546. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  547. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  548. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  549. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  550. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  551. };
  552. const uint8_t rsbox[256] =
  553. {
  554. 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  555. 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  556. 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  557. 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  558. 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  559. 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  560. 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  561. 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  562. 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  563. 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  564. 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  565. 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  566. 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  567. 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  568. 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  569. 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  570. };
  571. // The round constant word array, Rcon[i], contains the values given by
  572. // x to the power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
  573. const uint8_t Rcon[11] =
  574. {
  575. 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
  576. };
  577. };
  578. }
  579. #endif // !__ASIO2_AES_IMPL_HPP__