aes.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* crypto/aes/aes.h -*- mode:C; c-file-style: "eay" -*- */
  2. /* ====================================================================
  3. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. */
  51. #ifndef DPFPP_AES_H
  52. #define DPFPP_AES_H
  53. #include <x86intrin.h>
  54. #define EXPAND_ASSIST(v1,v2,v3,v4,shuff_const,aes_const) \
  55. v2 = _mm_aeskeygenassist_si128(v4,aes_const); \
  56. v3 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(v3), \
  57. _mm_castsi128_ps(v1), 16)); \
  58. v1 = _mm_xor_si128(v1,v3); \
  59. v3 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(v3), \
  60. _mm_castsi128_ps(v1), 140)); \
  61. v1 = _mm_xor_si128(v1,v3); \
  62. v2 = _mm_shuffle_epi32(v2,shuff_const); \
  63. v1 = _mm_xor_si128(v1,v2)
  64. struct AES_KEY
  65. {
  66. AES_KEY(const __m128i userkey = _mm_set_epi64x(597349, 121379))
  67. : rounds(10)
  68. {
  69. __m128i x0, x1, x2;
  70. rd_key[0] = x0 = userkey;
  71. x2 = _mm_setzero_si128();
  72. EXPAND_ASSIST(x0, x1, x2, x0, 255, 1);
  73. rd_key[1] = x0;
  74. EXPAND_ASSIST(x0, x1, x2, x0, 255, 2);
  75. rd_key[2] = x0;
  76. EXPAND_ASSIST(x0, x1, x2, x0, 255, 4);
  77. rd_key[3] = x0;
  78. EXPAND_ASSIST(x0, x1, x2, x0, 255, 8);
  79. rd_key[4] = x0;
  80. EXPAND_ASSIST(x0, x1, x2, x0, 255, 16);
  81. rd_key[5] = x0;
  82. EXPAND_ASSIST(x0, x1, x2, x0, 255, 32);
  83. rd_key[6] = x0;
  84. EXPAND_ASSIST(x0, x1, x2, x0, 255, 64);
  85. rd_key[7] = x0;
  86. EXPAND_ASSIST(x0, x1, x2, x0, 255, 128);
  87. rd_key[8] = x0;
  88. EXPAND_ASSIST(x0, x1, x2, x0, 255, 27);
  89. rd_key[9] = x0;
  90. EXPAND_ASSIST(x0, x1, x2, x0, 255, 54);
  91. rd_key[10] = x0;
  92. }
  93. __m128i rd_key[11];
  94. unsigned int rounds;
  95. } ;
  96. static const AES_KEY default_aes_key;
  97. static inline void
  98. AES_ecb_encrypt_blks(__m128i * __restrict__ blks, unsigned int nblks, const AES_KEY * __restrict__ key)
  99. {
  100. for (unsigned int i = 0; i < nblks; ++i)
  101. blks[i] = _mm_xor_si128(blks[i], key->rd_key[0]);
  102. for (unsigned int j = 1; j < key->rounds; ++j)
  103. for (unsigned int i = 0; i < nblks; ++i)
  104. blks[i] = _mm_aesenc_si128(blks[i], key->rd_key[j]);
  105. for (unsigned int i = 0; i < nblks; ++i)
  106. blks[i] = _mm_aesenclast_si128(blks[i], key->rd_key[key->rounds]);
  107. }
  108. static inline void
  109. AES_set_decrypt_key_fast(AES_KEY * __restrict__ dkey, const AES_KEY * __restrict__ ekey)
  110. {
  111. int j = 0;
  112. int i = ekey->rounds;
  113. #if (OCB_KEY_LEN == 0)
  114. dkey->rounds = i;
  115. #endif
  116. dkey->rd_key[i--] = ekey->rd_key[j++];
  117. while (i)
  118. dkey->rd_key[i--] = _mm_aesimc_si128(ekey->rd_key[j++]);
  119. dkey->rd_key[i] = ekey->rd_key[j];
  120. }
  121. /*
  122. static inline void
  123. AES_set_decrypt_key(__m128i userkey, AES_KEY * __restrict__ key)
  124. {
  125. AES_KEY temp_key;
  126. AES_set_encrypt_key(userkey, &temp_key);
  127. AES_set_decrypt_key_fast(key, &temp_key);
  128. }
  129. */
  130. static inline void
  131. AES_ecb_decrypt_blks(__m128i * __restrict__ blks, unsigned nblks, const AES_KEY * __restrict__ key)
  132. {
  133. unsigned i, j, rnds = key->rounds;
  134. for (i = 0; i < nblks; ++i)
  135. blks[i] = _mm_xor_si128(blks[i], key->rd_key[0]);
  136. for (j = 1; j < rnds; ++j)
  137. for (i = 0; i < nblks; ++i)
  138. blks[i] = _mm_aesdec_si128(blks[i], key->rd_key[j]);
  139. for (i = 0; i < nblks; ++i)
  140. blks[i] = _mm_aesdeclast_si128(blks[i], key->rd_key[j]);
  141. }
  142. #endif