cipher_sm4_ccm_hw.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2021 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. * Generic support for SM4 CCM.
  11. */
  12. #include "cipher_sm4_ccm.h"
  13. #include "crypto/sm4_platform.h"
  14. #define SM4_HW_CCM_SET_KEY_FN(fn_set_enc_key, fn_blk, fn_ccm_enc, fn_ccm_dec) \
  15. fn_set_enc_key(key, &actx->ks.ks); \
  16. CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ks.ks, \
  17. (block128_f)fn_blk); \
  18. ctx->str = ctx->enc ? (ccm128_f)fn_ccm_enc : (ccm128_f)fn_ccm_dec; \
  19. ctx->key_set = 1;
  20. static int ccm_sm4_initkey(PROV_CCM_CTX *ctx,
  21. const unsigned char *key, size_t keylen)
  22. {
  23. PROV_SM4_CCM_CTX *actx = (PROV_SM4_CCM_CTX *)ctx;
  24. #ifdef HWSM4_CAPABLE
  25. if (HWSM4_CAPABLE) {
  26. SM4_HW_CCM_SET_KEY_FN(HWSM4_set_encrypt_key, HWSM4_encrypt, NULL, NULL);
  27. } else
  28. #endif /* HWSM4_CAPABLE */
  29. #ifdef VPSM4_EX_CAPABLE
  30. if (VPSM4_EX_CAPABLE) {
  31. SM4_HW_CCM_SET_KEY_FN(vpsm4_ex_set_encrypt_key, vpsm4_ex_encrypt, NULL,
  32. NULL);
  33. } else
  34. #endif /* VPSM4_EX_CAPABLE */
  35. #ifdef VPSM4_CAPABLE
  36. if (VPSM4_CAPABLE) {
  37. SM4_HW_CCM_SET_KEY_FN(vpsm4_set_encrypt_key, vpsm4_encrypt, NULL, NULL);
  38. } else
  39. #endif /* VPSM4_CAPABLE */
  40. {
  41. SM4_HW_CCM_SET_KEY_FN(ossl_sm4_set_key, ossl_sm4_encrypt, NULL, NULL);
  42. }
  43. return 1;
  44. }
  45. static const PROV_CCM_HW ccm_sm4 = {
  46. ccm_sm4_initkey,
  47. ossl_ccm_generic_setiv,
  48. ossl_ccm_generic_setaad,
  49. ossl_ccm_generic_auth_encrypt,
  50. ossl_ccm_generic_auth_decrypt,
  51. ossl_ccm_generic_gettag
  52. };
  53. const PROV_CCM_HW *ossl_prov_sm4_hw_ccm(size_t keybits)
  54. {
  55. return &ccm_sm4;
  56. }