rijndael-vaes.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* VAES/AVX2 AMD64 accelerated AES for Libgcrypt
  2. * Copyright (C) 2021 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "types.h" /* for byte and u32 typedefs */
  24. #include "g10lib.h"
  25. #include "cipher.h"
  26. #include "bufhelp.h"
  27. #include "rijndael-internal.h"
  28. #include "./cipher-internal.h"
  29. #ifdef USE_VAES
  30. # ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS
  31. # define ASM_FUNC_ABI __attribute__((sysv_abi))
  32. # else
  33. # define ASM_FUNC_ABI
  34. # endif
  35. extern void _gcry_aes_aesni_prepare_decryption (RIJNDAEL_context *ctx);
  36. extern void _gcry_vaes_avx2_cbc_dec_amd64 (const void *keysched,
  37. unsigned char *iv,
  38. void *outbuf_arg,
  39. const void *inbuf_arg,
  40. size_t nblocks,
  41. unsigned int nrounds) ASM_FUNC_ABI;
  42. extern void _gcry_vaes_avx2_cfb_dec_amd64 (const void *keysched,
  43. unsigned char *iv,
  44. void *outbuf_arg,
  45. const void *inbuf_arg,
  46. size_t nblocks,
  47. unsigned int nrounds) ASM_FUNC_ABI;
  48. extern void _gcry_vaes_avx2_ctr_enc_amd64 (const void *keysched,
  49. unsigned char *ctr,
  50. void *outbuf_arg,
  51. const void *inbuf_arg,
  52. size_t nblocks,
  53. unsigned int nrounds) ASM_FUNC_ABI;
  54. extern void _gcry_vaes_avx2_ctr32le_enc_amd64 (const void *keysched,
  55. unsigned char *ctr,
  56. void *outbuf_arg,
  57. const void *inbuf_arg,
  58. size_t nblocks,
  59. unsigned int nrounds)
  60. ASM_FUNC_ABI;
  61. extern size_t _gcry_vaes_avx2_ocb_crypt_amd64 (const void *keysched,
  62. unsigned int blkn,
  63. void *outbuf_arg,
  64. const void *inbuf_arg,
  65. size_t nblocks,
  66. unsigned int nrounds,
  67. unsigned char *offset,
  68. unsigned char *checksum,
  69. unsigned char *L_table,
  70. int enc_dec_auth) ASM_FUNC_ABI;
  71. extern void _gcry_vaes_avx2_xts_crypt_amd64 (const void *keysched,
  72. unsigned char *tweak,
  73. void *outbuf_arg,
  74. const void *inbuf_arg,
  75. size_t nblocks,
  76. unsigned int nrounds,
  77. int encrypt) ASM_FUNC_ABI;
  78. extern void _gcry_vaes_avx2_ecb_crypt_amd64 (const void *keysched,
  79. int encrypt,
  80. void *outbuf_arg,
  81. const void *inbuf_arg,
  82. size_t nblocks,
  83. unsigned int nrounds) ASM_FUNC_ABI;
  84. void
  85. _gcry_aes_vaes_ecb_crypt (void *context, void *outbuf,
  86. const void *inbuf, size_t nblocks,
  87. int encrypt)
  88. {
  89. RIJNDAEL_context *ctx = context;
  90. const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32;
  91. unsigned int nrounds = ctx->rounds;
  92. if (!encrypt && !ctx->decryption_prepared)
  93. {
  94. _gcry_aes_aesni_prepare_decryption (ctx);
  95. ctx->decryption_prepared = 1;
  96. }
  97. _gcry_vaes_avx2_ecb_crypt_amd64 (keysched, encrypt, outbuf, inbuf,
  98. nblocks, nrounds);
  99. }
  100. void
  101. _gcry_aes_vaes_cbc_dec (void *context, unsigned char *iv,
  102. void *outbuf, const void *inbuf,
  103. size_t nblocks)
  104. {
  105. RIJNDAEL_context *ctx = context;
  106. const void *keysched = ctx->keyschdec32;
  107. unsigned int nrounds = ctx->rounds;
  108. if (!ctx->decryption_prepared)
  109. {
  110. _gcry_aes_aesni_prepare_decryption (ctx);
  111. ctx->decryption_prepared = 1;
  112. }
  113. _gcry_vaes_avx2_cbc_dec_amd64 (keysched, iv, outbuf, inbuf, nblocks, nrounds);
  114. }
  115. void
  116. _gcry_aes_vaes_cfb_dec (void *context, unsigned char *iv,
  117. void *outbuf, const void *inbuf,
  118. size_t nblocks)
  119. {
  120. RIJNDAEL_context *ctx = context;
  121. const void *keysched = ctx->keyschenc32;
  122. unsigned int nrounds = ctx->rounds;
  123. _gcry_vaes_avx2_cfb_dec_amd64 (keysched, iv, outbuf, inbuf, nblocks, nrounds);
  124. }
  125. void
  126. _gcry_aes_vaes_ctr_enc (void *context, unsigned char *iv,
  127. void *outbuf, const void *inbuf,
  128. size_t nblocks)
  129. {
  130. RIJNDAEL_context *ctx = context;
  131. const void *keysched = ctx->keyschenc32;
  132. unsigned int nrounds = ctx->rounds;
  133. _gcry_vaes_avx2_ctr_enc_amd64 (keysched, iv, outbuf, inbuf, nblocks, nrounds);
  134. }
  135. void
  136. _gcry_aes_vaes_ctr32le_enc (void *context, unsigned char *iv,
  137. void *outbuf, const void *inbuf,
  138. size_t nblocks)
  139. {
  140. RIJNDAEL_context *ctx = context;
  141. const void *keysched = ctx->keyschenc32;
  142. unsigned int nrounds = ctx->rounds;
  143. _gcry_vaes_avx2_ctr32le_enc_amd64 (keysched, iv, outbuf, inbuf, nblocks,
  144. nrounds);
  145. }
  146. size_t
  147. _gcry_aes_vaes_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
  148. const void *inbuf_arg, size_t nblocks,
  149. int encrypt)
  150. {
  151. RIJNDAEL_context *ctx = (void *)&c->context.c;
  152. const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32;
  153. unsigned char *outbuf = outbuf_arg;
  154. const unsigned char *inbuf = inbuf_arg;
  155. unsigned int nrounds = ctx->rounds;
  156. u64 blkn = c->u_mode.ocb.data_nblocks;
  157. if (!encrypt && !ctx->decryption_prepared)
  158. {
  159. _gcry_aes_aesni_prepare_decryption (ctx);
  160. ctx->decryption_prepared = 1;
  161. }
  162. c->u_mode.ocb.data_nblocks = blkn + nblocks;
  163. return _gcry_vaes_avx2_ocb_crypt_amd64 (keysched, (unsigned int)blkn, outbuf,
  164. inbuf, nblocks, nrounds, c->u_iv.iv,
  165. c->u_ctr.ctr, c->u_mode.ocb.L[0],
  166. encrypt);
  167. }
  168. size_t
  169. _gcry_aes_vaes_ocb_auth (gcry_cipher_hd_t c, const void *inbuf_arg,
  170. size_t nblocks)
  171. {
  172. RIJNDAEL_context *ctx = (void *)&c->context.c;
  173. const void *keysched = ctx->keyschenc32;
  174. const unsigned char *inbuf = inbuf_arg;
  175. unsigned int nrounds = ctx->rounds;
  176. u64 blkn = c->u_mode.ocb.aad_nblocks;
  177. c->u_mode.ocb.aad_nblocks = blkn + nblocks;
  178. return _gcry_vaes_avx2_ocb_crypt_amd64 (keysched, (unsigned int)blkn, NULL,
  179. inbuf, nblocks, nrounds,
  180. c->u_mode.ocb.aad_offset,
  181. c->u_mode.ocb.aad_sum,
  182. c->u_mode.ocb.L[0], 2);
  183. }
  184. void
  185. _gcry_aes_vaes_xts_crypt (void *context, unsigned char *tweak,
  186. void *outbuf, const void *inbuf,
  187. size_t nblocks, int encrypt)
  188. {
  189. RIJNDAEL_context *ctx = context;
  190. const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32;
  191. unsigned int nrounds = ctx->rounds;
  192. if (!encrypt && !ctx->decryption_prepared)
  193. {
  194. _gcry_aes_aesni_prepare_decryption (ctx);
  195. ctx->decryption_prepared = 1;
  196. }
  197. _gcry_vaes_avx2_xts_crypt_amd64 (keysched, tweak, outbuf, inbuf, nblocks,
  198. nrounds, encrypt);
  199. }
  200. #endif /* USE_VAES */