cipher_aes_hw_rv64i.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 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. /*-
  44. * RISC-V RV64 ZVKNED support for AES modes ecb, cbc, ofb, cfb, ctr.
  45. * This file is included by cipher_aes_hw.c
  46. */
  47. #define cipher_hw_rv64i_zvkned_cbc ossl_cipher_hw_generic_cbc
  48. #define cipher_hw_rv64i_zvkned_ecb ossl_cipher_hw_generic_ecb
  49. #define cipher_hw_rv64i_zvkned_ofb128 ossl_cipher_hw_generic_ofb128
  50. #define cipher_hw_rv64i_zvkned_cfb128 ossl_cipher_hw_generic_cfb128
  51. #define cipher_hw_rv64i_zvkned_cfb8 ossl_cipher_hw_generic_cfb8
  52. #define cipher_hw_rv64i_zvkned_cfb1 ossl_cipher_hw_generic_cfb1
  53. #define cipher_hw_rv64i_zvkned_ctr ossl_cipher_hw_generic_ctr
  54. static int cipher_hw_rv64i_zvkned_initkey(PROV_CIPHER_CTX *dat,
  55. const unsigned char *key,
  56. size_t keylen)
  57. {
  58. int ret;
  59. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  60. AES_KEY *ks = &adat->ks.ks;
  61. dat->ks = ks;
  62. /*
  63. * Zvkned only supports 128 and 256 bit keys for key schedule generation.
  64. * For AES-192 case, we could fallback to `AES_set_encrypt_key`.
  65. * All Zvkned-based implementations use the same `encrypt-key` scheduling
  66. * for both encryption and decryption.
  67. */
  68. if (keylen * 8 == 128 || keylen * 8 == 256) {
  69. ret = rv64i_zvkned_set_encrypt_key(key, keylen * 8, ks);
  70. } else {
  71. ret = AES_set_encrypt_key(key, keylen * 8, ks);
  72. }
  73. if (dat->mode == EVP_CIPH_CBC_MODE) {
  74. if (dat->enc) {
  75. dat->stream.cbc = (cbc128_f) rv64i_zvkned_cbc_encrypt;
  76. } else {
  77. dat->stream.cbc = (cbc128_f) rv64i_zvkned_cbc_decrypt;
  78. }
  79. } else if (dat->mode == EVP_CIPH_CTR_MODE) {
  80. if (RISCV_HAS_ZVKB()) {
  81. dat->stream.ctr = (ctr128_f) rv64i_zvkb_zvkned_ctr32_encrypt_blocks;
  82. }
  83. } else if (dat->mode == EVP_CIPH_ECB_MODE) {
  84. if (dat->enc) {
  85. dat->stream.ecb = (ecb128_f) rv64i_zvkned_ecb_encrypt;
  86. } else {
  87. dat->stream.ecb = (ecb128_f) rv64i_zvkned_ecb_decrypt;
  88. }
  89. }
  90. /* Zvkned supports aes-128/192/256 encryption and decryption. */
  91. if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE) &&
  92. !dat->enc) {
  93. dat->block = (block128_f) rv64i_zvkned_decrypt;
  94. } else {
  95. dat->block = (block128_f) rv64i_zvkned_encrypt;
  96. }
  97. if (ret < 0) {
  98. ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. #define PROV_CIPHER_HW_declare(mode) \
  104. static const PROV_CIPHER_HW rv64i_zknd_zkne_##mode = { \
  105. cipher_hw_rv64i_zknd_zkne_initkey, \
  106. cipher_hw_rv64i_zknd_zkne_##mode, \
  107. cipher_hw_aes_copyctx \
  108. }; \
  109. static const PROV_CIPHER_HW rv64i_zvkned_##mode = { \
  110. cipher_hw_rv64i_zvkned_initkey, \
  111. cipher_hw_rv64i_zvkned_##mode, \
  112. cipher_hw_aes_copyctx \
  113. };
  114. #define PROV_CIPHER_HW_select(mode) \
  115. if (RISCV_HAS_ZVKNED() && riscv_vlen() >= 128) \
  116. return &rv64i_zvkned_##mode; \
  117. else if (RISCV_HAS_ZKND_AND_ZKNE()) \
  118. return &rv64i_zknd_zkne_##mode;