cipher_aes_ocb_hw.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2019-2022 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(RV64I_ZKND_ZKNE_CAPABLE)
  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. # define PROV_CIPHER_HW_declare() \
  103. static const PROV_CIPHER_HW aes_rv64i_zknd_zkne_ocb = { \
  104. cipher_hw_aes_ocb_rv64i_zknd_zkne_initkey, \
  105. NULL \
  106. };
  107. # define PROV_CIPHER_HW_select() \
  108. if (RV64I_ZKND_ZKNE_CAPABLE) \
  109. return &aes_rv64i_zknd_zkne_ocb;
  110. #elif defined(RV32I_ZBKB_ZKND_ZKNE_CAPABLE) && defined(RV32I_ZKND_ZKNE_CAPABLE)
  111. static int cipher_hw_aes_ocb_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx,
  112. const unsigned char *key,
  113. size_t keylen)
  114. {
  115. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  116. OCB_SET_KEY_FN(rv32i_zkne_set_encrypt_key, rv32i_zknd_zkne_set_decrypt_key,
  117. rv32i_zkne_encrypt, rv32i_zknd_decrypt, NULL, NULL);
  118. return 1;
  119. }
  120. static int cipher_hw_aes_ocb_rv32i_zbkb_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx,
  121. const unsigned char *key,
  122. size_t keylen)
  123. {
  124. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  125. OCB_SET_KEY_FN(rv32i_zbkb_zkne_set_encrypt_key, rv32i_zbkb_zknd_zkne_set_decrypt_key,
  126. rv32i_zkne_encrypt, rv32i_zknd_decrypt, NULL, NULL);
  127. return 1;
  128. }
  129. # define PROV_CIPHER_HW_declare() \
  130. static const PROV_CIPHER_HW aes_rv32i_zknd_zkne_ocb = { \
  131. cipher_hw_aes_ocb_rv32i_zknd_zkne_initkey, \
  132. NULL \
  133. }; \
  134. static const PROV_CIPHER_HW aes_rv32i_zbkb_zknd_zkne_ocb = { \
  135. cipher_hw_aes_ocb_rv32i_zbkb_zknd_zkne_initkey, \
  136. NULL \
  137. };
  138. # define PROV_CIPHER_HW_select() \
  139. if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE) \
  140. return &aes_rv32i_zbkb_zknd_zkne_ocb; \
  141. if (RV32I_ZKND_ZKNE_CAPABLE) \
  142. return &aes_rv32i_zknd_zkne_ocb;
  143. #else
  144. # define PROV_CIPHER_HW_declare()
  145. # define PROV_CIPHER_HW_select()
  146. # endif
  147. static const PROV_CIPHER_HW aes_generic_ocb = {
  148. cipher_hw_aes_ocb_generic_initkey,
  149. NULL
  150. };
  151. PROV_CIPHER_HW_declare()
  152. const PROV_CIPHER_HW *ossl_prov_cipher_hw_aes_ocb(size_t keybits)
  153. {
  154. PROV_CIPHER_HW_select()
  155. return &aes_generic_ocb;
  156. }