cipher_aes_ocb_hw.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2019-2020 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. #else
  93. # define PROV_CIPHER_HW_declare()
  94. # define PROV_CIPHER_HW_select()
  95. # endif
  96. static const PROV_CIPHER_HW aes_generic_ocb = {
  97. cipher_hw_aes_ocb_generic_initkey,
  98. NULL
  99. };
  100. PROV_CIPHER_HW_declare()
  101. const PROV_CIPHER_HW *ossl_prov_cipher_hw_aes_ocb(size_t keybits)
  102. {
  103. PROV_CIPHER_HW_select()
  104. return &aes_generic_ocb;
  105. }