cipher_sm4_gcm_hw.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2021-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. /*-
  10. * Generic support for SM4 GCM.
  11. */
  12. #include "cipher_sm4_gcm.h"
  13. #include "crypto/sm4_platform.h"
  14. # define SM4_GCM_HW_SET_KEY_CTR_FN(ks, fn_set_enc_key, fn_block, fn_ctr) \
  15. fn_set_enc_key(key, ks); \
  16. CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f)fn_block); \
  17. ctx->ctr = (ctr128_f)fn_ctr; \
  18. ctx->key_set = 1;
  19. static int sm4_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
  20. size_t keylen)
  21. {
  22. PROV_SM4_GCM_CTX *actx = (PROV_SM4_GCM_CTX *)ctx;
  23. SM4_KEY *ks = &actx->ks.ks;
  24. # ifdef HWSM4_CAPABLE
  25. if (HWSM4_CAPABLE) {
  26. # ifdef HWSM4_ctr32_encrypt_blocks
  27. SM4_GCM_HW_SET_KEY_CTR_FN(ks, HWSM4_set_encrypt_key, HWSM4_encrypt,
  28. HWSM4_ctr32_encrypt_blocks);
  29. # else /* HWSM4_ctr32_encrypt_blocks */
  30. SM4_GCM_HW_SET_KEY_CTR_FN(ks, HWSM4_set_encrypt_key, HWSM4_encrypt, NULL);
  31. # endif
  32. } else
  33. # endif /* HWSM4_CAPABLE */
  34. #ifdef VPSM4_EX_CAPABLE
  35. if (VPSM4_EX_CAPABLE) {
  36. SM4_GCM_HW_SET_KEY_CTR_FN(ks, vpsm4_ex_set_encrypt_key, vpsm4_ex_encrypt,
  37. vpsm4_ex_ctr32_encrypt_blocks);
  38. } else
  39. #endif /* VPSM4_EX_CAPABLE */
  40. # ifdef VPSM4_CAPABLE
  41. if (VPSM4_CAPABLE) {
  42. SM4_GCM_HW_SET_KEY_CTR_FN(ks, vpsm4_set_encrypt_key, vpsm4_encrypt,
  43. vpsm4_ctr32_encrypt_blocks);
  44. } else
  45. # endif /* VPSM4_CAPABLE */
  46. {
  47. SM4_GCM_HW_SET_KEY_CTR_FN(ks, ossl_sm4_set_key, ossl_sm4_encrypt, NULL);
  48. }
  49. return 1;
  50. }
  51. static int hw_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
  52. size_t len, unsigned char *out)
  53. {
  54. if (ctx->enc) {
  55. if (ctx->ctr != NULL) {
  56. if (CRYPTO_gcm128_encrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr))
  57. return 0;
  58. } else {
  59. if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len))
  60. return 0;
  61. }
  62. } else {
  63. if (ctx->ctr != NULL) {
  64. if (CRYPTO_gcm128_decrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr))
  65. return 0;
  66. } else {
  67. if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len))
  68. return 0;
  69. }
  70. }
  71. return 1;
  72. }
  73. static const PROV_GCM_HW sm4_gcm = {
  74. sm4_gcm_initkey,
  75. ossl_gcm_setiv,
  76. ossl_gcm_aad_update,
  77. hw_gcm_cipher_update,
  78. ossl_gcm_cipher_final,
  79. ossl_gcm_one_shot
  80. };
  81. #if defined(__riscv) && __riscv_xlen == 64
  82. # include "cipher_sm4_gcm_hw_rv64i.inc"
  83. #else
  84. const PROV_GCM_HW *ossl_prov_sm4_hw_gcm(size_t keybits)
  85. {
  86. return &sm4_gcm;
  87. }
  88. #endif