2
0

cipher_aes_hw.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2001-2019 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. #include "cipher_aes.h"
  10. #include "internal/providercommonerr.h"
  11. static int cipher_hw_aes_initkey(PROV_CIPHER_CTX *dat,
  12. const unsigned char *key, size_t keylen)
  13. {
  14. int ret;
  15. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  16. AES_KEY *ks = &adat->ks.ks;
  17. dat->ks = ks;
  18. if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
  19. && !dat->enc) {
  20. #ifdef HWAES_CAPABLE
  21. if (HWAES_CAPABLE) {
  22. ret = HWAES_set_decrypt_key(key, keylen * 8, ks);
  23. dat->block = (block128_f)HWAES_decrypt;
  24. dat->stream.cbc = NULL;
  25. # ifdef HWAES_cbc_encrypt
  26. if (dat->mode == EVP_CIPH_CBC_MODE)
  27. dat->stream.cbc = (cbc128_f)HWAES_cbc_encrypt;
  28. # endif
  29. } else
  30. #endif
  31. #ifdef BSAES_CAPABLE
  32. if (BSAES_CAPABLE && dat->mode == EVP_CIPH_CBC_MODE) {
  33. ret = AES_set_decrypt_key(key, keylen * 8, ks);
  34. dat->block = (block128_f)AES_decrypt;
  35. dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
  36. } else
  37. #endif
  38. #ifdef VPAES_CAPABLE
  39. if (VPAES_CAPABLE) {
  40. ret = vpaes_set_decrypt_key(key, keylen * 8, ks);
  41. dat->block = (block128_f)vpaes_decrypt;
  42. dat->stream.cbc = (dat->mode == EVP_CIPH_CBC_MODE)
  43. ?(cbc128_f)vpaes_cbc_encrypt : NULL;
  44. } else
  45. #endif
  46. {
  47. ret = AES_set_decrypt_key(key, keylen * 8, ks);
  48. dat->block = (block128_f)AES_decrypt;
  49. dat->stream.cbc = (dat->mode == EVP_CIPH_CBC_MODE)
  50. ? (cbc128_f)AES_cbc_encrypt : NULL;
  51. }
  52. } else
  53. #ifdef HWAES_CAPABLE
  54. if (HWAES_CAPABLE) {
  55. ret = HWAES_set_encrypt_key(key, keylen * 8, ks);
  56. dat->block = (block128_f)HWAES_encrypt;
  57. dat->stream.cbc = NULL;
  58. # ifdef HWAES_cbc_encrypt
  59. if (dat->mode == EVP_CIPH_CBC_MODE)
  60. dat->stream.cbc = (cbc128_f)HWAES_cbc_encrypt;
  61. else
  62. # endif
  63. # ifdef HWAES_ctr32_encrypt_blocks
  64. if (dat->mode == EVP_CIPH_CTR_MODE)
  65. dat->stream.ctr = (ctr128_f)HWAES_ctr32_encrypt_blocks;
  66. else
  67. # endif
  68. (void)0; /* terminate potentially open 'else' */
  69. } else
  70. #endif
  71. #ifdef BSAES_CAPABLE
  72. if (BSAES_CAPABLE && dat->mode == EVP_CIPH_CTR_MODE) {
  73. ret = AES_set_encrypt_key(key, keylen * 8, ks);
  74. dat->block = (block128_f)AES_encrypt;
  75. dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
  76. } else
  77. #endif
  78. #ifdef VPAES_CAPABLE
  79. if (VPAES_CAPABLE) {
  80. ret = vpaes_set_encrypt_key(key, keylen * 8, ks);
  81. dat->block = (block128_f)vpaes_encrypt;
  82. dat->stream.cbc = (dat->mode == EVP_CIPH_CBC_MODE)
  83. ? (cbc128_f)vpaes_cbc_encrypt : NULL;
  84. } else
  85. #endif
  86. {
  87. ret = AES_set_encrypt_key(key, keylen * 8, ks);
  88. dat->block = (block128_f)AES_encrypt;
  89. dat->stream.cbc = (dat->mode == EVP_CIPH_CBC_MODE)
  90. ? (cbc128_f)AES_cbc_encrypt : NULL;
  91. #ifdef AES_CTR_ASM
  92. if (dat->mode == EVP_CIPH_CTR_MODE)
  93. dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt;
  94. #endif
  95. }
  96. if (ret < 0) {
  97. ERR_raise(ERR_LIB_PROV, PROV_R_AES_KEY_SETUP_FAILED);
  98. return 0;
  99. }
  100. return 1;
  101. }
  102. #define PROV_CIPHER_HW_aes_mode(mode) \
  103. static const PROV_CIPHER_HW aes_##mode = { \
  104. cipher_hw_aes_initkey, \
  105. cipher_hw_generic_##mode \
  106. }; \
  107. PROV_CIPHER_HW_declare(mode) \
  108. const PROV_CIPHER_HW *PROV_CIPHER_HW_aes_##mode(size_t keybits) \
  109. { \
  110. PROV_CIPHER_HW_select(mode) \
  111. return &aes_##mode; \
  112. }
  113. #if defined(AESNI_CAPABLE)
  114. # include "cipher_aes_hw_aesni.inc"
  115. #elif defined(SPARC_AES_CAPABLE)
  116. # include "cipher_aes_hw_t4.inc"
  117. #elif defined(S390X_aes_128_CAPABLE)
  118. # include "cipher_aes_hw_s390x.inc"
  119. #else
  120. /* The generic case */
  121. # define PROV_CIPHER_HW_declare(mode)
  122. # define PROV_CIPHER_HW_select(mode)
  123. #endif
  124. PROV_CIPHER_HW_aes_mode(cbc)
  125. PROV_CIPHER_HW_aes_mode(ecb)
  126. PROV_CIPHER_HW_aes_mode(ofb128)
  127. PROV_CIPHER_HW_aes_mode(cfb128)
  128. PROV_CIPHER_HW_aes_mode(cfb1)
  129. PROV_CIPHER_HW_aes_mode(cfb8)
  130. PROV_CIPHER_HW_aes_mode(ctr)