cipher_aes_ocb_hw.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright 2019-2024 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. * This file uses the low level AES functions (which are deprecated for
  11. * non-internal use) in order to implement provider AES ciphers.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "cipher_aes_ocb.h"
  15. #define OCB_SET_KEY_FN(fn_set_enc_key, fn_set_dec_key, \
  16. fn_block_enc, fn_block_dec, \
  17. fn_stream_enc, fn_stream_dec) \
  18. CRYPTO_ocb128_cleanup(&ctx->ocb); \
  19. fn_set_enc_key(key, keylen * 8, &ctx->ksenc.ks); \
  20. fn_set_dec_key(key, keylen * 8, &ctx->ksdec.ks); \
  21. if (!CRYPTO_ocb128_init(&ctx->ocb, &ctx->ksenc.ks, &ctx->ksdec.ks, \
  22. (block128_f)fn_block_enc, (block128_f)fn_block_dec, \
  23. ctx->base.enc ? (ocb128_f)fn_stream_enc : \
  24. (ocb128_f)fn_stream_dec)) \
  25. return 0; \
  26. ctx->key_set = 1
  27. static int cipher_hw_aes_ocb_generic_initkey(PROV_CIPHER_CTX *vctx,
  28. const unsigned char *key,
  29. size_t keylen)
  30. {
  31. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  32. /*
  33. * We set both the encrypt and decrypt key here because decrypt
  34. * needs both. (i.e- AAD uses encrypt).
  35. */
  36. # ifdef HWAES_CAPABLE
  37. if (HWAES_CAPABLE) {
  38. OCB_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_set_decrypt_key,
  39. HWAES_encrypt, HWAES_decrypt,
  40. HWAES_ocb_encrypt, HWAES_ocb_decrypt);
  41. } else
  42. # endif
  43. # ifdef VPAES_CAPABLE
  44. if (VPAES_CAPABLE) {
  45. OCB_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_set_decrypt_key,
  46. vpaes_encrypt, vpaes_decrypt, NULL, NULL);
  47. } else
  48. # endif
  49. {
  50. OCB_SET_KEY_FN(AES_set_encrypt_key, AES_set_decrypt_key,
  51. AES_encrypt, AES_decrypt, NULL, NULL);
  52. }
  53. return 1;
  54. }
  55. # if defined(AESNI_CAPABLE)
  56. static int cipher_hw_aes_ocb_aesni_initkey(PROV_CIPHER_CTX *vctx,
  57. const unsigned char *key,
  58. size_t keylen)
  59. {
  60. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  61. OCB_SET_KEY_FN(aesni_set_encrypt_key, aesni_set_decrypt_key,
  62. aesni_encrypt, aesni_decrypt,
  63. aesni_ocb_encrypt, aesni_ocb_decrypt);
  64. return 1;
  65. }
  66. # define PROV_CIPHER_HW_declare() \
  67. static const PROV_CIPHER_HW aesni_ocb = { \
  68. cipher_hw_aes_ocb_aesni_initkey, \
  69. NULL \
  70. };
  71. # define PROV_CIPHER_HW_select() \
  72. if (AESNI_CAPABLE) \
  73. return &aesni_ocb;
  74. #elif defined(SPARC_AES_CAPABLE)
  75. static int cipher_hw_aes_ocb_t4_initkey(PROV_CIPHER_CTX *vctx,
  76. const unsigned char *key,
  77. size_t keylen)
  78. {
  79. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  80. OCB_SET_KEY_FN(aes_t4_set_encrypt_key, aes_t4_set_decrypt_key,
  81. aes_t4_encrypt, aes_t4_decrypt, NULL, NULL);
  82. return 1;
  83. }
  84. # define PROV_CIPHER_HW_declare() \
  85. static const PROV_CIPHER_HW aes_t4_ocb = { \
  86. cipher_hw_aes_ocb_t4_initkey, \
  87. NULL \
  88. };
  89. # define PROV_CIPHER_HW_select() \
  90. if (SPARC_AES_CAPABLE) \
  91. return &aes_t4_ocb;
  92. #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
  93. static int cipher_hw_aes_ocb_rv64i_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx,
  94. const unsigned char *key,
  95. size_t keylen)
  96. {
  97. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  98. OCB_SET_KEY_FN(rv64i_zkne_set_encrypt_key, rv64i_zknd_set_decrypt_key,
  99. rv64i_zkne_encrypt, rv64i_zknd_decrypt, NULL, NULL);
  100. return 1;
  101. }
  102. static int cipher_hw_aes_ocb_rv64i_zvkned_initkey(PROV_CIPHER_CTX *vctx,
  103. const unsigned char *key,
  104. size_t keylen)
  105. {
  106. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  107. /* Zvkned only supports 128 and 256 bit keys. */
  108. if (keylen * 8 == 128 || keylen * 8 == 256) {
  109. OCB_SET_KEY_FN(rv64i_zvkned_set_encrypt_key,
  110. rv64i_zvkned_set_decrypt_key,
  111. rv64i_zvkned_encrypt, rv64i_zvkned_decrypt,
  112. NULL, NULL);
  113. } else {
  114. OCB_SET_KEY_FN(AES_set_encrypt_key, AES_set_encrypt_key,
  115. rv64i_zvkned_encrypt, rv64i_zvkned_decrypt,
  116. NULL, NULL);
  117. }
  118. return 1;
  119. }
  120. # define PROV_CIPHER_HW_declare() \
  121. static const PROV_CIPHER_HW aes_rv64i_zknd_zkne_ocb = { \
  122. cipher_hw_aes_ocb_rv64i_zknd_zkne_initkey, \
  123. NULL \
  124. }; \
  125. static const PROV_CIPHER_HW aes_rv64i_zvkned_ocb = { \
  126. cipher_hw_aes_ocb_rv64i_zvkned_initkey, \
  127. NULL \
  128. };
  129. # define PROV_CIPHER_HW_select() \
  130. if (RISCV_HAS_ZVKNED() && riscv_vlen() >= 128) \
  131. return &aes_rv64i_zvkned_ocb; \
  132. else if (RISCV_HAS_ZKND_AND_ZKNE()) \
  133. return &aes_rv64i_zknd_zkne_ocb;
  134. #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 32
  135. static int cipher_hw_aes_ocb_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx,
  136. const unsigned char *key,
  137. size_t keylen)
  138. {
  139. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  140. OCB_SET_KEY_FN(rv32i_zkne_set_encrypt_key, rv32i_zknd_zkne_set_decrypt_key,
  141. rv32i_zkne_encrypt, rv32i_zknd_decrypt, NULL, NULL);
  142. return 1;
  143. }
  144. static int cipher_hw_aes_ocb_rv32i_zbkb_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx,
  145. const unsigned char *key,
  146. size_t keylen)
  147. {
  148. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  149. OCB_SET_KEY_FN(rv32i_zbkb_zkne_set_encrypt_key, rv32i_zbkb_zknd_zkne_set_decrypt_key,
  150. rv32i_zkne_encrypt, rv32i_zknd_decrypt, NULL, NULL);
  151. return 1;
  152. }
  153. # define PROV_CIPHER_HW_declare() \
  154. static const PROV_CIPHER_HW aes_rv32i_zknd_zkne_ocb = { \
  155. cipher_hw_aes_ocb_rv32i_zknd_zkne_initkey, \
  156. NULL \
  157. }; \
  158. static const PROV_CIPHER_HW aes_rv32i_zbkb_zknd_zkne_ocb = { \
  159. cipher_hw_aes_ocb_rv32i_zbkb_zknd_zkne_initkey, \
  160. NULL \
  161. };
  162. # define PROV_CIPHER_HW_select() \
  163. if (RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE()) \
  164. return &aes_rv32i_zbkb_zknd_zkne_ocb; \
  165. if (RISCV_HAS_ZKND_AND_ZKNE()) \
  166. return &aes_rv32i_zknd_zkne_ocb;
  167. #else
  168. # define PROV_CIPHER_HW_declare()
  169. # define PROV_CIPHER_HW_select()
  170. # endif
  171. static const PROV_CIPHER_HW aes_generic_ocb = {
  172. cipher_hw_aes_ocb_generic_initkey,
  173. NULL
  174. };
  175. PROV_CIPHER_HW_declare()
  176. const PROV_CIPHER_HW *ossl_prov_cipher_hw_aes_ocb(size_t keybits)
  177. {
  178. PROV_CIPHER_HW_select()
  179. return &aes_generic_ocb;
  180. }