cipher_aes_hw_rv64i.inc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2022 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. * RISC-V 64 ZKND ZKNE support for AES modes ecb, cbc, ofb, cfb, ctr.
  11. * This file is included by cipher_aes_hw.c
  12. */
  13. #define cipher_hw_rv64i_zknd_zkne_cbc ossl_cipher_hw_generic_cbc
  14. #define cipher_hw_rv64i_zknd_zkne_ecb ossl_cipher_hw_generic_ecb
  15. #define cipher_hw_rv64i_zknd_zkne_ofb128 ossl_cipher_hw_generic_ofb128
  16. #define cipher_hw_rv64i_zknd_zkne_cfb128 ossl_cipher_hw_generic_cfb128
  17. #define cipher_hw_rv64i_zknd_zkne_cfb8 ossl_cipher_hw_generic_cfb8
  18. #define cipher_hw_rv64i_zknd_zkne_cfb1 ossl_cipher_hw_generic_cfb1
  19. #define cipher_hw_rv64i_zknd_zkne_ctr ossl_cipher_hw_generic_ctr
  20. static int cipher_hw_rv64i_zknd_zkne_initkey(PROV_CIPHER_CTX *dat,
  21. const unsigned char *key, size_t keylen)
  22. {
  23. int ret;
  24. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  25. AES_KEY *ks = &adat->ks.ks;
  26. dat->ks = ks;
  27. if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
  28. && !dat->enc) {
  29. ret = rv64i_zknd_set_decrypt_key(key, keylen * 8, ks);
  30. dat->block = (block128_f) rv64i_zknd_decrypt;
  31. dat->stream.cbc = NULL;
  32. } else {
  33. ret = rv64i_zkne_set_encrypt_key(key, keylen * 8, ks);
  34. dat->block = (block128_f) rv64i_zkne_encrypt;
  35. dat->stream.cbc = NULL;
  36. }
  37. if (ret < 0) {
  38. ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
  39. return 0;
  40. }
  41. return 1;
  42. }
  43. #define PROV_CIPHER_HW_declare(mode) \
  44. static const PROV_CIPHER_HW rv64i_zknd_zkne_##mode = { \
  45. cipher_hw_rv64i_zknd_zkne_initkey, \
  46. cipher_hw_rv64i_zknd_zkne_##mode, \
  47. cipher_hw_aes_copyctx \
  48. };
  49. #define PROV_CIPHER_HW_select(mode) \
  50. if (RISCV_HAS_ZKND_AND_ZKNE()) \
  51. return &rv64i_zknd_zkne_##mode;