2
0

cipher_aes_hw_rv32i.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2022-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. * RISC-V 32 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_rv32i_zknd_zkne_cbc ossl_cipher_hw_generic_cbc
  14. #define cipher_hw_rv32i_zknd_zkne_ecb ossl_cipher_hw_generic_ecb
  15. #define cipher_hw_rv32i_zknd_zkne_ofb128 ossl_cipher_hw_generic_ofb128
  16. #define cipher_hw_rv32i_zknd_zkne_cfb128 ossl_cipher_hw_generic_cfb128
  17. #define cipher_hw_rv32i_zknd_zkne_cfb8 ossl_cipher_hw_generic_cfb8
  18. #define cipher_hw_rv32i_zknd_zkne_cfb1 ossl_cipher_hw_generic_cfb1
  19. #define cipher_hw_rv32i_zknd_zkne_ctr ossl_cipher_hw_generic_ctr
  20. #define cipher_hw_rv32i_zbkb_zknd_zkne_cbc ossl_cipher_hw_generic_cbc
  21. #define cipher_hw_rv32i_zbkb_zknd_zkne_ecb ossl_cipher_hw_generic_ecb
  22. #define cipher_hw_rv32i_zbkb_zknd_zkne_ofb128 ossl_cipher_hw_generic_ofb128
  23. #define cipher_hw_rv32i_zbkb_zknd_zkne_cfb128 ossl_cipher_hw_generic_cfb128
  24. #define cipher_hw_rv32i_zbkb_zknd_zkne_cfb8 ossl_cipher_hw_generic_cfb8
  25. #define cipher_hw_rv32i_zbkb_zknd_zkne_cfb1 ossl_cipher_hw_generic_cfb1
  26. #define cipher_hw_rv32i_zbkb_zknd_zkne_ctr ossl_cipher_hw_generic_ctr
  27. static int cipher_hw_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *dat,
  28. const unsigned char *key, size_t keylen)
  29. {
  30. int ret;
  31. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  32. AES_KEY *ks = &adat->ks.ks;
  33. dat->ks = ks;
  34. if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
  35. && !dat->enc) {
  36. ret = rv32i_zknd_zkne_set_decrypt_key(key, keylen * 8, ks);
  37. dat->block = (block128_f) rv32i_zknd_decrypt;
  38. dat->stream.cbc = NULL;
  39. } else {
  40. ret = rv32i_zkne_set_encrypt_key(key, keylen * 8, ks);
  41. dat->block = (block128_f) rv32i_zkne_encrypt;
  42. dat->stream.cbc = NULL;
  43. }
  44. if (ret < 0) {
  45. ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
  46. return 0;
  47. }
  48. return 1;
  49. }
  50. static int cipher_hw_rv32i_zbkb_zknd_zkne_initkey(PROV_CIPHER_CTX *dat,
  51. const unsigned char *key, size_t keylen)
  52. {
  53. int ret;
  54. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  55. AES_KEY *ks = &adat->ks.ks;
  56. dat->ks = ks;
  57. if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
  58. && !dat->enc) {
  59. ret = rv32i_zbkb_zknd_zkne_set_decrypt_key(key, keylen * 8, ks);
  60. dat->block = (block128_f) rv32i_zknd_decrypt;
  61. dat->stream.cbc = NULL;
  62. } else {
  63. ret = rv32i_zbkb_zkne_set_encrypt_key(key, keylen * 8, ks);
  64. dat->block = (block128_f) rv32i_zkne_encrypt;
  65. dat->stream.cbc = NULL;
  66. }
  67. if (ret < 0) {
  68. ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
  69. return 0;
  70. }
  71. return 1;
  72. }
  73. #define PROV_CIPHER_HW_declare(mode) \
  74. static const PROV_CIPHER_HW rv32i_zknd_zkne_##mode = { \
  75. cipher_hw_rv32i_zknd_zkne_initkey, \
  76. cipher_hw_rv32i_zknd_zkne_##mode, \
  77. cipher_hw_aes_copyctx \
  78. }; \
  79. static const PROV_CIPHER_HW rv32i_zbkb_zknd_zkne_##mode = { \
  80. cipher_hw_rv32i_zbkb_zknd_zkne_initkey, \
  81. cipher_hw_rv32i_zbkb_zknd_zkne_##mode, \
  82. cipher_hw_aes_copyctx \
  83. };
  84. #define PROV_CIPHER_HW_select(mode) \
  85. if (RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE()) \
  86. return &rv32i_zbkb_zknd_zkne_##mode; \
  87. if (RISCV_HAS_ZKND_AND_ZKNE()) \
  88. return &rv32i_zknd_zkne_##mode;