2
0

cipher_aes_hw_aesni.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*-
  10. * AES-NI support for AES modes ecb, cbc, ofb, cfb, ctr.
  11. * This file is included by cipher_aes_hw.c
  12. */
  13. #define cipher_hw_aesni_ofb128 ossl_cipher_hw_generic_ofb128
  14. #define cipher_hw_aesni_cfb128 ossl_cipher_hw_generic_cfb128
  15. #define cipher_hw_aesni_cfb8 ossl_cipher_hw_generic_cfb8
  16. #define cipher_hw_aesni_cfb1 ossl_cipher_hw_generic_cfb1
  17. #define cipher_hw_aesni_ctr ossl_cipher_hw_generic_ctr
  18. static int cipher_hw_aesni_initkey(PROV_CIPHER_CTX *dat,
  19. const unsigned char *key, size_t keylen)
  20. {
  21. int ret;
  22. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  23. AES_KEY *ks = &adat->ks.ks;
  24. dat->ks = ks;
  25. if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
  26. && !dat->enc) {
  27. ret = aesni_set_decrypt_key(key, keylen * 8, ks);
  28. dat->block = (block128_f) aesni_decrypt;
  29. dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
  30. (cbc128_f) aesni_cbc_encrypt : NULL;
  31. } else {
  32. ret = aesni_set_encrypt_key(key, keylen * 8, ks);
  33. dat->block = (block128_f) aesni_encrypt;
  34. if (dat->mode == EVP_CIPH_CBC_MODE)
  35. dat->stream.cbc = (cbc128_f) aesni_cbc_encrypt;
  36. else if (dat->mode == EVP_CIPH_CTR_MODE)
  37. dat->stream.ctr = (ctr128_f) aesni_ctr32_encrypt_blocks;
  38. else
  39. dat->stream.cbc = NULL;
  40. }
  41. if (ret < 0) {
  42. ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
  43. return 0;
  44. }
  45. return 1;
  46. }
  47. static int cipher_hw_aesni_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
  48. const unsigned char *in, size_t len)
  49. {
  50. const AES_KEY *ks = ctx->ks;
  51. aesni_cbc_encrypt(in, out, len, ks, ctx->iv, ctx->enc);
  52. return 1;
  53. }
  54. static int cipher_hw_aesni_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
  55. const unsigned char *in, size_t len)
  56. {
  57. if (len < ctx->blocksize)
  58. return 1;
  59. aesni_ecb_encrypt(in, out, len, ctx->ks, ctx->enc);
  60. return 1;
  61. }
  62. #define PROV_CIPHER_HW_declare(mode) \
  63. static const PROV_CIPHER_HW aesni_##mode = { \
  64. cipher_hw_aesni_initkey, \
  65. cipher_hw_aesni_##mode, \
  66. cipher_hw_aes_copyctx \
  67. };
  68. #define PROV_CIPHER_HW_select(mode) \
  69. if (AESNI_CAPABLE) \
  70. return &aesni_##mode;