aesacc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * This file is adapted from PolarSSL 1.3.19 (GPL)
  3. */
  4. #include "aesni.h"
  5. #include "aesarm.h"
  6. #include <stdint.h>
  7. #include <string.h>
  8. #if defined(AES256) && (AES256 == 1)
  9. #define AES_KEYSIZE 256
  10. #ifdef HAVE_AMD64
  11. #define aeshw_setkey_enc aesni_setkey_enc_256
  12. #endif
  13. #elif defined(AES192) && (AES192 == 1)
  14. #define AES_KEYSIZE 192
  15. #ifdef HAVE_AMD64
  16. #define aeshw_setkey_enc aesni_setkey_enc_192
  17. #endif
  18. #else
  19. #define AES_KEYSIZE 128
  20. #ifdef HAVE_AMD64
  21. #define aeshw_setkey_enc aesni_setkey_enc_128
  22. #endif
  23. #endif
  24. #define AES_NR ((AES_KEYSIZE >> 5) + 6)
  25. #define AES_RKSIZE 272
  26. #ifdef HAVE_AMD64
  27. #define HAVE_HARDAES 1
  28. #define aeshw_supported aesni_supported
  29. #define aeshw_crypt_ecb aesni_crypt_ecb
  30. #define aeshw_inverse_key(a,b) aesni_inverse_key(a,b,AES_NR)
  31. #endif /* HAVE_AMD64 */
  32. #ifdef HAVE_ARM64
  33. #define HAVE_HARDAES 1
  34. #define aeshw_supported aesarm_supported
  35. #define aeshw_crypt_ecb aesarm_crypt_ecb
  36. #include "aesarm_table.h"
  37. #ifndef GET_UINT32_LE
  38. #define GET_UINT32_LE(n,b,i) \
  39. { \
  40. (n) = ( (uint32_t) (b)[(i) ] ) \
  41. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  42. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  43. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  44. }
  45. #endif
  46. static void aeshw_setkey_enc(uint8_t *rk, const uint8_t *key)
  47. {
  48. unsigned int i;
  49. uint32_t *RK;
  50. RK = (uint32_t *) rk;
  51. for( i = 0; i < ( AES_KEYSIZE >> 5 ); i++ )
  52. {
  53. GET_UINT32_LE( RK[i], key, i << 2 );
  54. }
  55. switch( AES_NR )
  56. {
  57. case 10:
  58. for( i = 0; i < 10; i++, RK += 4 )
  59. {
  60. RK[4] = RK[0] ^ RCON[i] ^
  61. ( (uint32_t) FSb[ ( RK[3] >> 8 ) & 0xFF ] ) ^
  62. ( (uint32_t) FSb[ ( RK[3] >> 16 ) & 0xFF ] << 8 ) ^
  63. ( (uint32_t) FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^
  64. ( (uint32_t) FSb[ ( RK[3] ) & 0xFF ] << 24 );
  65. RK[5] = RK[1] ^ RK[4];
  66. RK[6] = RK[2] ^ RK[5];
  67. RK[7] = RK[3] ^ RK[6];
  68. }
  69. break;
  70. case 12:
  71. for( i = 0; i < 8; i++, RK += 6 )
  72. {
  73. RK[6] = RK[0] ^ RCON[i] ^
  74. ( (uint32_t) FSb[ ( RK[5] >> 8 ) & 0xFF ] ) ^
  75. ( (uint32_t) FSb[ ( RK[5] >> 16 ) & 0xFF ] << 8 ) ^
  76. ( (uint32_t) FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^
  77. ( (uint32_t) FSb[ ( RK[5] ) & 0xFF ] << 24 );
  78. RK[7] = RK[1] ^ RK[6];
  79. RK[8] = RK[2] ^ RK[7];
  80. RK[9] = RK[3] ^ RK[8];
  81. RK[10] = RK[4] ^ RK[9];
  82. RK[11] = RK[5] ^ RK[10];
  83. }
  84. break;
  85. case 14:
  86. for( i = 0; i < 7; i++, RK += 8 )
  87. {
  88. RK[8] = RK[0] ^ RCON[i] ^
  89. ( (uint32_t) FSb[ ( RK[7] >> 8 ) & 0xFF ] ) ^
  90. ( (uint32_t) FSb[ ( RK[7] >> 16 ) & 0xFF ] << 8 ) ^
  91. ( (uint32_t) FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^
  92. ( (uint32_t) FSb[ ( RK[7] ) & 0xFF ] << 24 );
  93. RK[9] = RK[1] ^ RK[8];
  94. RK[10] = RK[2] ^ RK[9];
  95. RK[11] = RK[3] ^ RK[10];
  96. RK[12] = RK[4] ^
  97. ( (uint32_t) FSb[ ( RK[11] ) & 0xFF ] ) ^
  98. ( (uint32_t) FSb[ ( RK[11] >> 8 ) & 0xFF ] << 8 ) ^
  99. ( (uint32_t) FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^
  100. ( (uint32_t) FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 );
  101. RK[13] = RK[5] ^ RK[12];
  102. RK[14] = RK[6] ^ RK[13];
  103. RK[15] = RK[7] ^ RK[14];
  104. }
  105. break;
  106. }
  107. }
  108. static void aeshw_inverse_key(uint8_t *invkey, const uint8_t *fwdkey)
  109. {
  110. int i, j;
  111. uint32_t *RK;
  112. uint32_t *SK;
  113. RK = (uint32_t *) invkey;
  114. SK = ((uint32_t *) fwdkey) + AES_NR * 4;
  115. *RK++ = *SK++;
  116. *RK++ = *SK++;
  117. *RK++ = *SK++;
  118. *RK++ = *SK++;
  119. for( i = AES_NR - 1, SK -= 8; i > 0; i--, SK -= 8 )
  120. {
  121. for( j = 0; j < 4; j++, SK++ )
  122. {
  123. *RK++ = RT0[ FSb[ ( *SK ) & 0xFF ] ] ^
  124. RT1[ FSb[ ( *SK >> 8 ) & 0xFF ] ] ^
  125. RT2[ FSb[ ( *SK >> 16 ) & 0xFF ] ] ^
  126. RT3[ FSb[ ( *SK >> 24 ) & 0xFF ] ];
  127. }
  128. }
  129. *RK++ = *SK++;
  130. *RK++ = *SK++;
  131. *RK++ = *SK++;
  132. *RK++ = *SK++;
  133. }
  134. #endif /* HAVE_ARM64 */
  135. #ifdef HAVE_HARDAES
  136. static void aeshw_setkey_dec(uint8_t *rk, const uint8_t *key)
  137. {
  138. uint8_t rk_tmp[AES_RKSIZE];
  139. aeshw_setkey_enc(rk_tmp, key);
  140. aeshw_inverse_key(rk, rk_tmp);
  141. }
  142. static void aeshw_encrypt_ecb( int nr,
  143. unsigned char *rk,
  144. const unsigned char input[16],
  145. unsigned char output[16] )
  146. {
  147. aeshw_crypt_ecb(nr, rk, AES_ENCRYPT, input, output);
  148. }
  149. static void aeshw_decrypt_ecb( int nr,
  150. unsigned char *rk,
  151. const unsigned char input[16],
  152. unsigned char output[16] )
  153. {
  154. aeshw_crypt_ecb(nr, rk, AES_DECRYPT, input, output);
  155. }
  156. #endif /* HAVE_HARDAES */
  157. /* OpenSSL assembly functions */
  158. #define AES_MAXNR 14
  159. typedef struct {
  160. uint32_t rd_key[4 * (AES_MAXNR + 1)];
  161. uint32_t rounds;
  162. } AES_KEY;
  163. #if defined(__amd64__) || defined(__x86_64__) || \
  164. defined(__aarch64__)
  165. #define AES_set_encrypt_key vpaes_set_encrypt_key
  166. #define AES_set_decrypt_key vpaes_set_decrypt_key
  167. #define AES_encrypt vpaes_encrypt
  168. #define AES_decrypt vpaes_decrypt
  169. #endif /* VPAES for 64-bit Intel and ARM */
  170. #ifdef __cplusplus
  171. extern "C" {
  172. #endif
  173. int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
  174. AES_KEY *key);
  175. int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
  176. AES_KEY *key);
  177. void AES_encrypt(const unsigned char *in, unsigned char *out,
  178. const AES_KEY *key);
  179. void AES_decrypt(const unsigned char *in, unsigned char *out,
  180. const AES_KEY *key);
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. static void aes_encrypt_ecb( int nr,
  185. unsigned char *rk,
  186. const unsigned char input[16],
  187. unsigned char output[16] )
  188. {
  189. AES_encrypt(input, output, (AES_KEY *) rk);
  190. }
  191. static void aes_decrypt_ecb( int nr,
  192. unsigned char *rk,
  193. const unsigned char input[16],
  194. unsigned char output[16] )
  195. {
  196. AES_decrypt(input, output, (AES_KEY *) rk);
  197. }
  198. static void aes_setkey_enc(uint8_t *rk, const uint8_t *key)
  199. {
  200. AES_set_encrypt_key(key, AES_KEYSIZE, (AES_KEY *) rk);
  201. }
  202. static void aes_setkey_dec(uint8_t *rk, const uint8_t *key)
  203. {
  204. AES_set_decrypt_key(key, AES_KEYSIZE, (AES_KEY *) rk);
  205. }
  206. static void (*encrypt_ecb) ( int nr,
  207. unsigned char *rk,
  208. const unsigned char input[16],
  209. unsigned char output[16] )
  210. = aes_encrypt_ecb;
  211. static void (*decrypt_ecb) ( int nr,
  212. unsigned char *rk,
  213. const unsigned char input[16],
  214. unsigned char output[16] )
  215. = aes_decrypt_ecb;
  216. static void (*setkey_enc) (uint8_t *rk, const uint8_t *key)
  217. = aes_setkey_enc;
  218. static void (*setkey_dec) (uint8_t *rk, const uint8_t *key)
  219. = aes_setkey_dec;
  220. /*
  221. * AESNI-CBC buffer encryption/decryption
  222. */
  223. static void encrypt_cbc( uint8_t* rk,
  224. uint32_t length,
  225. uint8_t iv[16],
  226. const uint8_t *input,
  227. uint8_t *output )
  228. {
  229. int i;
  230. uint8_t temp[16];
  231. while( length > 0 )
  232. {
  233. for( i = 0; i < 16; i++ )
  234. output[i] = (uint8_t)( input[i] ^ iv[i] );
  235. encrypt_ecb( AES_NR, rk, output, output );
  236. memcpy( iv, output, 16 );
  237. input += 16;
  238. output += 16;
  239. length -= 16;
  240. }
  241. }
  242. static void decrypt_cbc( uint8_t* rk,
  243. uint32_t length,
  244. uint8_t iv[16],
  245. const uint8_t *input,
  246. uint8_t *output )
  247. {
  248. int i;
  249. uint8_t temp[16];
  250. while( length > 0 )
  251. {
  252. memcpy( temp, input, 16 );
  253. decrypt_ecb( AES_NR, rk, input, output );
  254. for( i = 0; i < 16; i++ )
  255. output[i] = (uint8_t)( output[i] ^ iv[i] );
  256. memcpy( iv, temp, 16 );
  257. input += 16;
  258. output += 16;
  259. length -= 16;
  260. }
  261. }
  262. static void aeshw_init(void)
  263. {
  264. #ifdef HAVE_HARDAES
  265. static int done = 0;
  266. if (!done) {
  267. if (aeshw_supported()) {
  268. encrypt_ecb = aeshw_encrypt_ecb;
  269. decrypt_ecb = aeshw_decrypt_ecb;
  270. setkey_enc = aeshw_setkey_enc;
  271. setkey_dec = aeshw_setkey_dec;
  272. }
  273. done = 1;
  274. }
  275. #endif
  276. }
  277. int AES_support_hwaccel(void)
  278. {
  279. #ifdef HAVE_HARDAES
  280. return aeshw_supported();
  281. #else
  282. return 0;
  283. #endif
  284. }
  285. void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  286. {
  287. uint8_t iv_tmp[16];
  288. static uint8_t rk[AES_RKSIZE];
  289. if (iv == NULL)
  290. {
  291. return;
  292. }
  293. aeshw_init();
  294. memcpy(iv_tmp, iv, 16);
  295. if(key!= NULL)
  296. setkey_enc(rk, key);
  297. encrypt_cbc(rk, length, iv_tmp, input, output);
  298. }
  299. void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
  300. {
  301. uint8_t iv_tmp[16];
  302. static uint8_t rk[AES_RKSIZE];
  303. if (iv == NULL)
  304. {
  305. return;
  306. }
  307. aeshw_init();
  308. memcpy(iv_tmp, iv, 16);
  309. if(key!= NULL)
  310. {
  311. setkey_dec(rk, key);
  312. }
  313. decrypt_cbc(rk, length, iv_tmp, input, output);
  314. }
  315. void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t* output, const uint32_t length)
  316. {
  317. uint8_t rk[AES_RKSIZE];
  318. if (key == NULL)
  319. {
  320. return;
  321. }
  322. aeshw_init();
  323. setkey_enc(rk, key);
  324. encrypt_ecb(AES_NR, rk, input, output);
  325. }
  326. void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
  327. {
  328. uint8_t rk[AES_RKSIZE];
  329. if (key == NULL)
  330. {
  331. return;
  332. }
  333. aeshw_init();
  334. setkey_dec(rk, key);
  335. decrypt_ecb(AES_NR, rk, input, output);
  336. }