cipher_aes_gcm_hw_rv64i.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 support for AES GCM.
  11. * This file is included by cipher_aes_gcm_hw.c
  12. */
  13. /*-
  14. * RISC-V 64 ZKND and ZKNE support for AES GCM.
  15. */
  16. static int rv64i_zknd_zkne_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
  17. size_t keylen)
  18. {
  19. PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
  20. AES_KEY *ks = &actx->ks.ks;
  21. GCM_HW_SET_KEY_CTR_FN(ks, rv64i_zkne_set_encrypt_key, rv64i_zkne_encrypt,
  22. NULL);
  23. return 1;
  24. }
  25. static const PROV_GCM_HW rv64i_zknd_zkne_gcm = {
  26. rv64i_zknd_zkne_gcm_initkey,
  27. ossl_gcm_setiv,
  28. ossl_gcm_aad_update,
  29. generic_aes_gcm_cipher_update,
  30. ossl_gcm_cipher_final,
  31. ossl_gcm_one_shot
  32. };
  33. /*-
  34. * RISC-V RV64 ZVKNED support for AES GCM.
  35. */
  36. static int rv64i_zvkned_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
  37. size_t keylen)
  38. {
  39. PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
  40. AES_KEY *ks = &actx->ks.ks;
  41. /*
  42. * Zvkned only supports 128 and 256 bit keys for key schedule generation.
  43. * For AES-192 case, we could fallback to `AES_set_encrypt_key`.
  44. */
  45. if (keylen * 8 == 128 || keylen * 8 == 256) {
  46. GCM_HW_SET_KEY_CTR_FN(ks, rv64i_zvkned_set_encrypt_key,
  47. rv64i_zvkned_encrypt, NULL);
  48. } else {
  49. GCM_HW_SET_KEY_CTR_FN(ks, AES_set_encrypt_key,
  50. rv64i_zvkned_encrypt, NULL);
  51. }
  52. return 1;
  53. }
  54. static const PROV_GCM_HW rv64i_zvkned_gcm = {
  55. rv64i_zvkned_gcm_initkey,
  56. ossl_gcm_setiv,
  57. ossl_gcm_aad_update,
  58. generic_aes_gcm_cipher_update,
  59. ossl_gcm_cipher_final,
  60. ossl_gcm_one_shot
  61. };
  62. /*-
  63. * RISC-V RV64 ZVKB, ZVKG and ZVKNED support for AES GCM.
  64. */
  65. static int rv64i_zvkb_zvkg_zvkned_gcm_initkey(PROV_GCM_CTX *ctx,
  66. const unsigned char *key,
  67. size_t keylen) {
  68. PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
  69. AES_KEY *ks = &actx->ks.ks;
  70. /*
  71. * Zvkned only supports 128 and 256 bit keys for key schedule generation.
  72. * For AES-192 case, we could fallback to `AES_set_encrypt_key`.
  73. */
  74. if (keylen * 8 == 128 || keylen * 8 == 256) {
  75. GCM_HW_SET_KEY_CTR_FN(ks, rv64i_zvkned_set_encrypt_key,
  76. rv64i_zvkned_encrypt,
  77. rv64i_zvkb_zvkned_ctr32_encrypt_blocks);
  78. } else {
  79. GCM_HW_SET_KEY_CTR_FN(ks, AES_set_encrypt_key,
  80. rv64i_zvkned_encrypt,
  81. rv64i_zvkb_zvkned_ctr32_encrypt_blocks);
  82. }
  83. return 1;
  84. }
  85. static const PROV_GCM_HW rv64i_zvkb_zvkg_zvkned_gcm = {
  86. rv64i_zvkb_zvkg_zvkned_gcm_initkey,
  87. ossl_gcm_setiv,
  88. ossl_gcm_aad_update,
  89. generic_aes_gcm_cipher_update,
  90. ossl_gcm_cipher_final,
  91. ossl_gcm_one_shot
  92. };
  93. const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits) {
  94. if (RISCV_HAS_ZVKNED()) {
  95. if (RISCV_HAS_ZVKB() && RISCV_HAS_ZVKG() && riscv_vlen() >= 128) {
  96. return &rv64i_zvkb_zvkg_zvkned_gcm;
  97. }
  98. return &rv64i_zvkned_gcm;
  99. }
  100. if (RISCV_HAS_ZKND_AND_ZKNE()) {
  101. return &rv64i_zknd_zkne_gcm;
  102. }
  103. return &aes_gcm;
  104. }