cipher_sm4_hw_rv64i.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright 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. * RV64 ZVKSED support for AES modes ecb, cbc, ofb, cfb, ctr.
  11. * This file is included by cipher_sm4_hw.c
  12. */
  13. #define cipher_hw_rv64i_zvksed_sm4_cbc ossl_cipher_hw_generic_cbc
  14. #define cipher_hw_rv64i_zvksed_sm4_ecb ossl_cipher_hw_generic_ecb
  15. #define cipher_hw_rv64i_zvksed_sm4_ofb128 ossl_cipher_hw_generic_ofb128
  16. #define cipher_hw_rv64i_zvksed_sm4_cfb128 ossl_cipher_hw_generic_cfb128
  17. #define cipher_hw_rv64i_zvksed_sm4_ctr ossl_cipher_hw_generic_ctr
  18. static int cipher_hw_rv64i_zvksed_sm4_initkey(PROV_CIPHER_CTX *ctx,
  19. const unsigned char *key,
  20. size_t keylen)
  21. {
  22. PROV_SM4_CTX *sctx = (PROV_SM4_CTX *)ctx;
  23. SM4_KEY *ks = &sctx->ks.ks;
  24. ctx->ks = ks;
  25. if (ctx->enc
  26. || (ctx->mode != EVP_CIPH_ECB_MODE
  27. && ctx->mode != EVP_CIPH_CBC_MODE)) {
  28. rv64i_zvksed_sm4_set_encrypt_key(key, ks);
  29. ctx->block = (block128_f) rv64i_zvksed_sm4_encrypt;
  30. ctx->stream.cbc = NULL;
  31. } else {
  32. rv64i_zvksed_sm4_set_decrypt_key(key, ks);
  33. ctx->block = (block128_f) rv64i_zvksed_sm4_decrypt;
  34. ctx->stream.cbc = NULL;
  35. }
  36. return 1;
  37. }
  38. #define PROV_CIPHER_HW_declare(mode) \
  39. static const PROV_CIPHER_HW rv64i_zvksed_sm4_##mode = { \
  40. cipher_hw_rv64i_zvksed_sm4_initkey, \
  41. cipher_hw_rv64i_zvksed_sm4_##mode, \
  42. cipher_hw_sm4_copyctx \
  43. };
  44. #define PROV_CIPHER_HW_select(mode) \
  45. if (RISCV_HAS_ZVKB_AND_ZVKSED() && riscv_vlen() >= 128) \
  46. return &rv64i_zvksed_sm4_##mode;