TedKrovetzAesNiWrapperC.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. *
  4. * Copyright(c) 2013 Ted Krovetz.
  5. * This file was taken from the SCAPI project, where it was taken from the file ocb.c written by Ted Krovetz.
  6. * Some changes and additions may have been made and only part of the file written by Ted Krovetz has been copied
  7. * only for the use of this project.
  8. *
  9. * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. *
  11. */
  12. // Copyright(c) 2013 Ted Krovetz.
  13. #ifndef TED_FILE
  14. #define TED_FILE
  15. #include "../constants.h"
  16. #ifdef USE_PIPELINED_AES_NI
  17. #include <wmmintrin.h>
  18. #include "Config.h"
  19. #include <iostream>
  20. #include <stdlib.h>
  21. using namespace std;
  22. typedef struct { block rd_key[15]; int rounds; } AES_KEY;
  23. #define ROUNDS(ctx) ((ctx)->rounds)
  24. //output is written to v1, v2 and v3 are temporary variables, v4 is the previous key, shuff_const and aes_const are round/aes specific constants
  25. #define EXPAND_ASSIST(v1,v2,v3,v4,shuff_const,aes_const) \
  26. v2 = _mm_aeskeygenassist_si128(v4, aes_const); \
  27. v3 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(v3), \
  28. _mm_castsi128_ps(v1), 16)); \
  29. v1 = _mm_xor_si128(v1,v3); \
  30. v3 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(v3), \
  31. _mm_castsi128_ps(v1), 140)); \
  32. v1 = _mm_xor_si128(v1,v3); \
  33. v2 = _mm_shuffle_epi32(v2,shuff_const); \
  34. v1 = _mm_xor_si128(v1,v2)
  35. #define EXPAND192_STEP(idx,aes_const) \
  36. EXPAND_ASSIST(x0,x1,x2,x3,85,aes_const); \
  37. x3 = _mm_xor_si128(x3,_mm_slli_si128 (x3, 4)); \
  38. x3 = _mm_xor_si128(x3,_mm_shuffle_epi32(x0, 255)); \
  39. kp[idx] = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(tmp), \
  40. _mm_castsi128_ps(x0), 68)); \
  41. kp[idx+1] = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(x0), \
  42. _mm_castsi128_ps(x3), 78)); \
  43. EXPAND_ASSIST(x0,x1,x2,x3,85,(aes_const*2)); \
  44. x3 = _mm_xor_si128(x3,_mm_slli_si128 (x3, 4)); \
  45. x3 = _mm_xor_si128(x3,_mm_shuffle_epi32(x0, 255)); \
  46. kp[idx+2] = x0; tmp = x3
  47. void AES_128_Key_Expansion(const unsigned char *userkey, AES_KEY* aesKey);
  48. void AES_192_Key_Expansion(const unsigned char *userkey, AES_KEY* aesKey);
  49. void AES_256_Key_Expansion(const unsigned char *userkey, AES_KEY* aesKey);
  50. void AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *aesKey);
  51. void AES_encryptC(block *in, block *out, AES_KEY *aesKey);
  52. void AES_ecb_encrypt(block *blk, AES_KEY *aesKey);
  53. void AES_ecb_encrypt_blks(block *blks, unsigned nblks, AES_KEY *aesKey);
  54. void AES_ecb_encrypt_blks_4(block *blk, AES_KEY *aesKey);
  55. void AES_ecb_encrypt_blks_4_in_out(block *in, block *out, AES_KEY *aesKey);
  56. void AES_ecb_encrypt_blks_4_in_out_ind_keys(block *in, block *out, AES_KEY **aesKey, block** sched);
  57. void AES_ecb_encrypt_blks_4_in_out_par_ks(block *in, block *out, const unsigned char* userkey);
  58. void AES256_ecb_encrypt_blks_4_in_out_par_ks(block *in, block *out, const unsigned char* userkey);
  59. void AES_ecb_encrypt_blks_2_in_out(block *in, block *out, AES_KEY *aesKey);
  60. void AES_ecb_encrypt_chunk_in_out(block *in, block *out, unsigned nblks, AES_KEY *aesKey);
  61. #endif
  62. #endif