cipher_aes_ccm_hw.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2019-2023 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. /* AES CCM mode */
  10. /*
  11. * This file uses the low level AES functions (which are deprecated for
  12. * non-internal use) in order to implement provider AES ciphers.
  13. */
  14. #include "internal/deprecated.h"
  15. #include "cipher_aes_ccm.h"
  16. #define AES_HW_CCM_SET_KEY_FN(fn_set_enc_key, fn_blk, fn_ccm_enc, fn_ccm_dec) \
  17. fn_set_enc_key(key, keylen * 8, &actx->ccm.ks.ks); \
  18. CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ccm.ks.ks, \
  19. (block128_f)fn_blk); \
  20. ctx->str = ctx->enc ? (ccm128_f)fn_ccm_enc : (ccm128_f)fn_ccm_dec; \
  21. ctx->key_set = 1;
  22. static int ccm_generic_aes_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
  23. size_t keylen)
  24. {
  25. PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
  26. #ifdef HWAES_CAPABLE
  27. if (HWAES_CAPABLE) {
  28. AES_HW_CCM_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_encrypt, NULL, NULL);
  29. } else
  30. #endif /* HWAES_CAPABLE */
  31. #ifdef VPAES_CAPABLE
  32. if (VPAES_CAPABLE) {
  33. AES_HW_CCM_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_encrypt, NULL, NULL);
  34. } else
  35. #endif
  36. {
  37. AES_HW_CCM_SET_KEY_FN(AES_set_encrypt_key, AES_encrypt, NULL, NULL)
  38. }
  39. return 1;
  40. }
  41. static const PROV_CCM_HW aes_ccm = {
  42. ccm_generic_aes_initkey,
  43. ossl_ccm_generic_setiv,
  44. ossl_ccm_generic_setaad,
  45. ossl_ccm_generic_auth_encrypt,
  46. ossl_ccm_generic_auth_decrypt,
  47. ossl_ccm_generic_gettag
  48. };
  49. #if defined(S390X_aes_128_CAPABLE)
  50. # include "cipher_aes_ccm_hw_s390x.inc"
  51. #elif defined(AESNI_CAPABLE)
  52. # include "cipher_aes_ccm_hw_aesni.inc"
  53. #elif defined(SPARC_AES_CAPABLE)
  54. # include "cipher_aes_ccm_hw_t4.inc"
  55. #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
  56. # include "cipher_aes_ccm_hw_rv64i.inc"
  57. #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 32
  58. # include "cipher_aes_ccm_hw_rv32i.inc"
  59. #else
  60. const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits)
  61. {
  62. return &aes_ccm;
  63. }
  64. #endif