aes.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "aes.h"
  2. #if defined(_MSC_VER)
  3. #include <intrin.h>
  4. #else
  5. #include <x86intrin.h>
  6. #endif
  7. extern const uint32_t accelc_aes_rcon[11];
  8. extern const uint8_t accelc_aes_SBox[256];
  9. extern const uint8_t accelc_aes_InverseSBox[256];
  10. extern const uint8_t accelc_aes_GF2p8_Mul_0x02[256];
  11. extern const uint8_t accelc_aes_GF2p8_Mul_0x03[256];
  12. extern const uint8_t accelc_aes_GF2p8_Mul_0x09[256];
  13. extern const uint8_t accelc_aes_GF2p8_Mul_0x0B[256];
  14. extern const uint8_t accelc_aes_GF2p8_Mul_0x0D[256];
  15. extern const uint8_t accelc_aes_GF2p8_Mul_0x0E[256];
  16. #define Swap(X, Y, Temp) \
  17. Temp = X; \
  18. X = Y; \
  19. Y = Temp;
  20. void accelc_AES128_encrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
  21. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
  22. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
  23. uint8_t ShiftTemp = 0;
  24. for (int i = 1; i < 10; ++i) {
  25. for (int j = 0; j < 16; ++j)
  26. srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
  27. //Shift rows starts;
  28. //Shift the second row;
  29. Swap(srcBytes[1], srcBytes[5], ShiftTemp)
  30. Swap(srcBytes[5], srcBytes[9], ShiftTemp)
  31. Swap(srcBytes[9], srcBytes[13], ShiftTemp)
  32. //Shift the third row;
  33. Swap(srcBytes[2], srcBytes[10], ShiftTemp)
  34. Swap(srcBytes[6], srcBytes[14], ShiftTemp)
  35. //Shift the fourth row;
  36. Swap(srcBytes[3], srcBytes[15], ShiftTemp)
  37. Swap(srcBytes[15], srcBytes[11], ShiftTemp)
  38. Swap(srcBytes[11], srcBytes[7], ShiftTemp)
  39. //Shift rows ends;
  40. for (int j = 0; j < 16; j += 4) {
  41. uint8_t temp[4];
  42. *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
  43. srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x02[temp[0]] ^ accelc_aes_GF2p8_Mul_0x03[temp[1]] ^ temp[2] ^ temp[3]);
  44. srcBytes[j + 1] = (uint8_t)(temp[0] ^ accelc_aes_GF2p8_Mul_0x02[temp[1]] ^ accelc_aes_GF2p8_Mul_0x03[temp[2]] ^ temp[3]);
  45. srcBytes[j + 2] = (uint8_t)(temp[0] ^ temp[1] ^ accelc_aes_GF2p8_Mul_0x02[temp[2]] ^ accelc_aes_GF2p8_Mul_0x03[temp[3]]);
  46. srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x03[temp[0]] ^ temp[1] ^ temp[2] ^ accelc_aes_GF2p8_Mul_0x02[temp[3]]);
  47. }
  48. ((uint64_t*)(srcBytes))[0] ^= srcKey->qword[i * 2];
  49. ((uint64_t*)(srcBytes))[1] ^= srcKey->qword[i * 2 + 1];
  50. }
  51. for (int j = 0; j < 16; ++j)
  52. srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
  53. //Shift rows starts;
  54. //Shift the second row;
  55. Swap(srcBytes[1], srcBytes[5], ShiftTemp)
  56. Swap(srcBytes[5], srcBytes[9], ShiftTemp)
  57. Swap(srcBytes[9], srcBytes[13], ShiftTemp)
  58. //Shift the third row;
  59. Swap(srcBytes[2], srcBytes[10], ShiftTemp)
  60. Swap(srcBytes[6], srcBytes[14], ShiftTemp)
  61. //Shift the fourth row;
  62. Swap(srcBytes[3], srcBytes[15], ShiftTemp)
  63. Swap(srcBytes[15], srcBytes[11], ShiftTemp)
  64. Swap(srcBytes[11], srcBytes[7], ShiftTemp)
  65. //Shift rows ends;
  66. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[20];
  67. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[21];
  68. }
  69. void accelc_AES128_decrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
  70. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[20];
  71. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[21];
  72. uint8_t ShiftTemp = 0;
  73. for (int i = 9; i > 0; --i) {
  74. //Inverse Shift rows starts;
  75. //Inverse shift the second row;
  76. Swap(srcBytes[13], srcBytes[9], ShiftTemp)
  77. Swap(srcBytes[9], srcBytes[5], ShiftTemp)
  78. Swap(srcBytes[5], srcBytes[1], ShiftTemp)
  79. //Inverse shift the third row;
  80. Swap(srcBytes[14], srcBytes[6], ShiftTemp)
  81. Swap(srcBytes[10], srcBytes[2], ShiftTemp)
  82. //Inverse shift the fourth row;
  83. Swap(srcBytes[3], srcBytes[7], ShiftTemp)
  84. Swap(srcBytes[7], srcBytes[11], ShiftTemp)
  85. Swap(srcBytes[11], srcBytes[15], ShiftTemp)
  86. for (int j = 0; j < 16; ++j)
  87. srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
  88. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
  89. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
  90. for (int j = 0; j < 16; j += 4) {
  91. uint8_t temp[4];
  92. *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
  93. srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0E[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[2]] ^ accelc_aes_GF2p8_Mul_0x09[temp[3]]);
  94. srcBytes[j + 1] = (uint8_t)(accelc_aes_GF2p8_Mul_0x09[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[3]]);
  95. srcBytes[j + 2] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0D[temp[0]] ^ accelc_aes_GF2p8_Mul_0x09[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[3]]);
  96. srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0B[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[1]] ^ accelc_aes_GF2p8_Mul_0x09[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[3]]);
  97. }
  98. }
  99. //Inverse Shift rows starts;
  100. //Inverse shift the second row;
  101. Swap(srcBytes[13], srcBytes[9], ShiftTemp)
  102. Swap(srcBytes[9], srcBytes[5], ShiftTemp)
  103. Swap(srcBytes[5], srcBytes[1], ShiftTemp)
  104. //Inverse shift the third row;
  105. Swap(srcBytes[14], srcBytes[6], ShiftTemp)
  106. Swap(srcBytes[10], srcBytes[2], ShiftTemp)
  107. //Inverse shift the fourth row;
  108. Swap(srcBytes[3], srcBytes[7], ShiftTemp)
  109. Swap(srcBytes[7], srcBytes[11], ShiftTemp)
  110. Swap(srcBytes[11], srcBytes[15], ShiftTemp)
  111. for (int j = 0; j < 16; ++j)
  112. srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
  113. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
  114. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
  115. }
  116. void accelc_AES128_set_key(const uint8_t srcUserKey[16], AES_KEY* dstKey) {
  117. dstKey->qword[0] = ((const uint64_t*)srcUserKey)[0];
  118. dstKey->qword[1] = ((const uint64_t*)srcUserKey)[1];
  119. for (int i = 4; i < 44; ++i) {
  120. uint32_t temp = dstKey->dword[i - 1];
  121. if (i % 4 == 0) {
  122. temp = _rotr(temp, 8);
  123. ((uint8_t*)&temp)[0] = accelc_aes_SBox[((uint8_t*)&temp)[0]];
  124. ((uint8_t*)&temp)[1] = accelc_aes_SBox[((uint8_t*)&temp)[1]];
  125. ((uint8_t*)&temp)[2] = accelc_aes_SBox[((uint8_t*)&temp)[2]];
  126. ((uint8_t*)&temp)[3] = accelc_aes_SBox[((uint8_t*)&temp)[3]];
  127. temp ^= accelc_aes_rcon[i / 4];
  128. }
  129. dstKey->dword[i] = dstKey->dword[i - 4] ^ temp;
  130. }
  131. }
  132. void accelc_AES192_encrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
  133. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
  134. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
  135. uint8_t ShiftTemp = 0;
  136. for (int i = 1; i < 12; ++i) {
  137. for (int j = 0; j < 16; ++j)
  138. srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
  139. //Shift rows starts;
  140. //Shift the second row;
  141. Swap(srcBytes[1], srcBytes[5], ShiftTemp)
  142. Swap(srcBytes[5], srcBytes[9], ShiftTemp)
  143. Swap(srcBytes[9], srcBytes[13], ShiftTemp)
  144. //Shift the third row;
  145. Swap(srcBytes[2], srcBytes[10], ShiftTemp)
  146. Swap(srcBytes[6], srcBytes[14], ShiftTemp)
  147. //Shift the fourth row;
  148. Swap(srcBytes[3], srcBytes[15], ShiftTemp)
  149. Swap(srcBytes[15], srcBytes[11], ShiftTemp)
  150. Swap(srcBytes[11], srcBytes[7], ShiftTemp)
  151. //Shift rows ends;
  152. for (int j = 0; j < 16; j += 4) {
  153. uint8_t temp[4];
  154. *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
  155. srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x02[temp[0]] ^ accelc_aes_GF2p8_Mul_0x03[temp[1]] ^ temp[2] ^ temp[3]);
  156. srcBytes[j + 1] = (uint8_t)(temp[0] ^ accelc_aes_GF2p8_Mul_0x02[temp[1]] ^ accelc_aes_GF2p8_Mul_0x03[temp[2]] ^ temp[3]);
  157. srcBytes[j + 2] = (uint8_t)(temp[0] ^ temp[1] ^ accelc_aes_GF2p8_Mul_0x02[temp[2]] ^ accelc_aes_GF2p8_Mul_0x03[temp[3]]);
  158. srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x03[temp[0]] ^ temp[1] ^ temp[2] ^ accelc_aes_GF2p8_Mul_0x02[temp[3]]);
  159. }
  160. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
  161. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
  162. }
  163. for (int j = 0; j < 16; ++j)
  164. srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
  165. //Shift rows starts;
  166. //Shift the second row;
  167. Swap(srcBytes[1], srcBytes[5], ShiftTemp) //Swap is a MACRO, no need to add ';'.
  168. Swap(srcBytes[5], srcBytes[9], ShiftTemp)
  169. Swap(srcBytes[9], srcBytes[13], ShiftTemp)
  170. //Shift the third row;
  171. Swap(srcBytes[2], srcBytes[10], ShiftTemp)
  172. Swap(srcBytes[6], srcBytes[14], ShiftTemp)
  173. //Shift the fourth row;
  174. Swap(srcBytes[3], srcBytes[15], ShiftTemp)
  175. Swap(srcBytes[15], srcBytes[11], ShiftTemp)
  176. Swap(srcBytes[11], srcBytes[7], ShiftTemp)
  177. //Shift rows ends;
  178. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[24];
  179. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[25];
  180. }
  181. void accelc_AES192_decrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
  182. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[24];
  183. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[25];
  184. uint8_t ShiftTemp = 0;
  185. for (int i = 11; i > 0; --i) {
  186. //Inverse Shift rows starts;
  187. //Inverse shift the second row;
  188. Swap(srcBytes[13], srcBytes[9], ShiftTemp)
  189. Swap(srcBytes[9], srcBytes[5], ShiftTemp)
  190. Swap(srcBytes[5], srcBytes[1], ShiftTemp)
  191. //Inverse shift the third row;
  192. Swap(srcBytes[14], srcBytes[6], ShiftTemp)
  193. Swap(srcBytes[10], srcBytes[2], ShiftTemp)
  194. //Inverse shift the fourth row;
  195. Swap(srcBytes[3], srcBytes[7], ShiftTemp)
  196. Swap(srcBytes[7], srcBytes[11], ShiftTemp)
  197. Swap(srcBytes[11], srcBytes[15], ShiftTemp)
  198. for (int j = 0; j < 16; ++j)
  199. srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
  200. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
  201. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
  202. for (int j = 0; j < 16; j += 4) {
  203. uint8_t temp[4];
  204. *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
  205. srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0E[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[2]] ^ accelc_aes_GF2p8_Mul_0x09[temp[3]]);
  206. srcBytes[j + 1] = (uint8_t)(accelc_aes_GF2p8_Mul_0x09[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[3]]);
  207. srcBytes[j + 2] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0D[temp[0]] ^ accelc_aes_GF2p8_Mul_0x09[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[3]]);
  208. srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0B[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[1]] ^ accelc_aes_GF2p8_Mul_0x09[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[3]]);
  209. }
  210. }
  211. //Inverse Shift rows starts;
  212. //Inverse shift the second row;
  213. Swap(srcBytes[13], srcBytes[9], ShiftTemp)
  214. Swap(srcBytes[9], srcBytes[5], ShiftTemp)
  215. Swap(srcBytes[5], srcBytes[1], ShiftTemp)
  216. //Inverse shift the third row;
  217. Swap(srcBytes[14], srcBytes[6], ShiftTemp)
  218. Swap(srcBytes[10], srcBytes[2], ShiftTemp)
  219. //Inverse shift the fourth row;
  220. Swap(srcBytes[3], srcBytes[7], ShiftTemp)
  221. Swap(srcBytes[7], srcBytes[11], ShiftTemp)
  222. Swap(srcBytes[11], srcBytes[15], ShiftTemp)
  223. for (uint8_t j = 0; j < 16; ++j)
  224. srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
  225. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
  226. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
  227. }
  228. void accelc_AES192_set_key(const uint8_t srcUserKey[24], AES_KEY* dstKey) {
  229. dstKey->qword[0] = ((const uint64_t*)srcUserKey)[0];
  230. dstKey->qword[1] = ((const uint64_t*)srcUserKey)[1];
  231. dstKey->qword[2] = ((const uint64_t*)srcUserKey)[2];
  232. for (int i = 6; i < 52; ++i) {
  233. uint32_t temp = dstKey->dword[i - 1];
  234. if (i % 6 == 0) {
  235. temp = _rotr(temp, 8);
  236. ((uint8_t*)&temp)[0] = accelc_aes_SBox[((uint8_t*)&temp)[0]];
  237. ((uint8_t*)&temp)[1] = accelc_aes_SBox[((uint8_t*)&temp)[1]];
  238. ((uint8_t*)&temp)[2] = accelc_aes_SBox[((uint8_t*)&temp)[2]];
  239. ((uint8_t*)&temp)[3] = accelc_aes_SBox[((uint8_t*)&temp)[3]];
  240. temp ^= accelc_aes_rcon[i / 6];
  241. }
  242. dstKey->dword[i] = dstKey->dword[i - 6] ^ temp;
  243. }
  244. }
  245. void accelc_AES256_encrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
  246. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
  247. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
  248. uint8_t ShiftTemp = 0;
  249. for (int i = 1; i < 14; ++i) {
  250. for (int j = 0; j < 16; ++j)
  251. srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
  252. //Shift rows starts;
  253. //Shift the second row;
  254. Swap(srcBytes[1], srcBytes[5], ShiftTemp)
  255. Swap(srcBytes[5], srcBytes[9], ShiftTemp)
  256. Swap(srcBytes[9], srcBytes[13], ShiftTemp)
  257. //Shift the third row;
  258. Swap(srcBytes[2], srcBytes[10], ShiftTemp)
  259. Swap(srcBytes[6], srcBytes[14], ShiftTemp)
  260. //Shift the fourth row;
  261. Swap(srcBytes[3], srcBytes[15], ShiftTemp)
  262. Swap(srcBytes[15], srcBytes[11], ShiftTemp)
  263. Swap(srcBytes[11], srcBytes[7], ShiftTemp)
  264. //Shift rows ends;
  265. for (int j = 0; j < 16; j += 4) {
  266. uint8_t temp[4];
  267. *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
  268. srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x02[temp[0]] ^ accelc_aes_GF2p8_Mul_0x03[temp[1]] ^ temp[2] ^ temp[3]);
  269. srcBytes[j + 1] = (uint8_t)(temp[0] ^ accelc_aes_GF2p8_Mul_0x02[temp[1]] ^ accelc_aes_GF2p8_Mul_0x03[temp[2]] ^ temp[3]);
  270. srcBytes[j + 2] = (uint8_t)(temp[0] ^ temp[1] ^ accelc_aes_GF2p8_Mul_0x02[temp[2]] ^ accelc_aes_GF2p8_Mul_0x03[temp[3]]);
  271. srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x03[temp[0]] ^ temp[1] ^ temp[2] ^ accelc_aes_GF2p8_Mul_0x02[temp[3]]);
  272. }
  273. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
  274. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
  275. }
  276. for (int j = 0; j < 16; ++j)
  277. srcBytes[j] = accelc_aes_SBox[srcBytes[j]];
  278. //Shift rows starts;
  279. //Shift the second row;
  280. Swap(srcBytes[1], srcBytes[5], ShiftTemp)
  281. Swap(srcBytes[5], srcBytes[9], ShiftTemp)
  282. Swap(srcBytes[9], srcBytes[13], ShiftTemp)
  283. //Shift the third row;
  284. Swap(srcBytes[2], srcBytes[10], ShiftTemp)
  285. Swap(srcBytes[6], srcBytes[14], ShiftTemp)
  286. //Shift the fourth row;
  287. Swap(srcBytes[3], srcBytes[15], ShiftTemp)
  288. Swap(srcBytes[15], srcBytes[11], ShiftTemp)
  289. Swap(srcBytes[11], srcBytes[7], ShiftTemp)
  290. //Shift rows ends;
  291. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[28];
  292. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[29];
  293. }
  294. void accelc_AES256_decrypt(uint8_t srcBytes[AES_BLOCK_SIZE], const AES_KEY* srcKey) {
  295. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[28];
  296. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[29];
  297. uint8_t ShiftTemp = 0;
  298. for (int i = 13; i > 0; --i) {
  299. //Inverse Shift rows starts;
  300. //Inverse shift the second row;
  301. Swap(srcBytes[13], srcBytes[9], ShiftTemp)
  302. Swap(srcBytes[9], srcBytes[5], ShiftTemp)
  303. Swap(srcBytes[5], srcBytes[1], ShiftTemp)
  304. //Inverse shift the third row;
  305. Swap(srcBytes[14], srcBytes[6], ShiftTemp)
  306. Swap(srcBytes[10], srcBytes[2], ShiftTemp)
  307. //Inverse shift the fourth row;
  308. Swap(srcBytes[3], srcBytes[7], ShiftTemp)
  309. Swap(srcBytes[7], srcBytes[11], ShiftTemp)
  310. Swap(srcBytes[11], srcBytes[15], ShiftTemp)
  311. for (int j = 0; j < 16; ++j)
  312. srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
  313. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[i * 2];
  314. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[i * 2 + 1];
  315. for (int j = 0; j < 16; j += 4) {
  316. uint8_t temp[4];
  317. *(uint32_t*)temp = ((uint32_t*)srcBytes)[j / 4];
  318. srcBytes[j] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0E[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[2]] ^ accelc_aes_GF2p8_Mul_0x09[temp[3]]);
  319. srcBytes[j + 1] = (uint8_t)(accelc_aes_GF2p8_Mul_0x09[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[3]]);
  320. srcBytes[j + 2] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0D[temp[0]] ^ accelc_aes_GF2p8_Mul_0x09[temp[1]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0B[temp[3]]);
  321. srcBytes[j + 3] = (uint8_t)(accelc_aes_GF2p8_Mul_0x0B[temp[0]] ^ accelc_aes_GF2p8_Mul_0x0D[temp[1]] ^ accelc_aes_GF2p8_Mul_0x09[temp[2]] ^ accelc_aes_GF2p8_Mul_0x0E[temp[3]]);
  322. }
  323. }
  324. //Inverse Shift rows starts;
  325. //Inverse shift the second row;
  326. Swap(srcBytes[13], srcBytes[9], ShiftTemp)
  327. Swap(srcBytes[9], srcBytes[5], ShiftTemp)
  328. Swap(srcBytes[5], srcBytes[1], ShiftTemp)
  329. //Inverse shift the third row;
  330. Swap(srcBytes[14], srcBytes[6], ShiftTemp)
  331. Swap(srcBytes[10], srcBytes[2], ShiftTemp)
  332. //Inverse shift the fourth row;
  333. Swap(srcBytes[3], srcBytes[7], ShiftTemp)
  334. Swap(srcBytes[7], srcBytes[11], ShiftTemp)
  335. Swap(srcBytes[11], srcBytes[15], ShiftTemp)
  336. for (int j = 0; j < 16; ++j)
  337. srcBytes[j] = accelc_aes_InverseSBox[srcBytes[j]];
  338. ((uint64_t*)srcBytes)[0] ^= srcKey->qword[0];
  339. ((uint64_t*)srcBytes)[1] ^= srcKey->qword[1];
  340. }
  341. void accelc_AES256_set_key(const uint8_t srcUserKey[32], AES_KEY* dstKey) {
  342. dstKey->qword[0] = ((const uint64_t*)srcUserKey)[0];
  343. dstKey->qword[1] = ((const uint64_t*)srcUserKey)[1];
  344. dstKey->qword[2] = ((const uint64_t*)srcUserKey)[2];
  345. dstKey->qword[3] = ((const uint64_t*)srcUserKey)[3];
  346. for (int i = 8; i < 60; ++i) {
  347. uint32_t temp = dstKey->dword[i - 1];
  348. if (i % 8 == 0) {
  349. temp = _rotr(temp, 8);
  350. ((uint8_t*)&temp)[0] = accelc_aes_SBox[((uint8_t*)&temp)[0]];
  351. ((uint8_t*)&temp)[1] = accelc_aes_SBox[((uint8_t*)&temp)[1]];
  352. ((uint8_t*)&temp)[2] = accelc_aes_SBox[((uint8_t*)&temp)[2]];
  353. ((uint8_t*)&temp)[3] = accelc_aes_SBox[((uint8_t*)&temp)[3]];
  354. temp ^= accelc_aes_rcon[i / 8];
  355. }
  356. if (i % 8 == 4) {
  357. ((uint8_t*)&temp)[0] = accelc_aes_SBox[((uint8_t*)&temp)[0]];
  358. ((uint8_t*)&temp)[1] = accelc_aes_SBox[((uint8_t*)&temp)[1]];
  359. ((uint8_t*)&temp)[2] = accelc_aes_SBox[((uint8_t*)&temp)[2]];
  360. ((uint8_t*)&temp)[3] = accelc_aes_SBox[((uint8_t*)&temp)[3]];
  361. }
  362. dstKey->dword[i] = dstKey->dword[i - 8] ^ temp;
  363. }
  364. }