2
0

cipher_sm4_hw.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2019-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. #include "cipher_sm4.h"
  10. static int cipher_hw_sm4_initkey(PROV_CIPHER_CTX *ctx,
  11. const unsigned char *key, size_t keylen)
  12. {
  13. PROV_SM4_CTX *sctx = (PROV_SM4_CTX *)ctx;
  14. SM4_KEY *ks = &sctx->ks.ks;
  15. ctx->ks = ks;
  16. if (ctx->enc
  17. || (ctx->mode != EVP_CIPH_ECB_MODE
  18. && ctx->mode != EVP_CIPH_CBC_MODE)) {
  19. #ifdef HWSM4_CAPABLE
  20. if (HWSM4_CAPABLE) {
  21. HWSM4_set_encrypt_key(key, ks);
  22. ctx->block = (block128_f)HWSM4_encrypt;
  23. ctx->stream.cbc = NULL;
  24. #ifdef HWSM4_cbc_encrypt
  25. if (ctx->mode == EVP_CIPH_CBC_MODE)
  26. ctx->stream.cbc = (cbc128_f)HWSM4_cbc_encrypt;
  27. else
  28. #endif
  29. #ifdef HWSM4_ecb_encrypt
  30. if (ctx->mode == EVP_CIPH_ECB_MODE)
  31. ctx->stream.ecb = (ecb128_f)HWSM4_ecb_encrypt;
  32. else
  33. #endif
  34. #ifdef HWSM4_ctr32_encrypt_blocks
  35. if (ctx->mode == EVP_CIPH_CTR_MODE)
  36. ctx->stream.ctr = (ctr128_f)HWSM4_ctr32_encrypt_blocks;
  37. else
  38. #endif
  39. (void)0; /* terminate potentially open 'else' */
  40. } else
  41. #endif
  42. #ifdef VPSM4_EX_CAPABLE
  43. if (VPSM4_EX_CAPABLE) {
  44. vpsm4_ex_set_encrypt_key(key, ks);
  45. ctx->block = (block128_f)vpsm4_ex_encrypt;
  46. ctx->stream.cbc = NULL;
  47. if (ctx->mode == EVP_CIPH_CBC_MODE)
  48. ctx->stream.cbc = (cbc128_f)vpsm4_ex_cbc_encrypt;
  49. else if (ctx->mode == EVP_CIPH_ECB_MODE)
  50. ctx->stream.ecb = (ecb128_f)vpsm4_ex_ecb_encrypt;
  51. else if (ctx->mode == EVP_CIPH_CTR_MODE)
  52. ctx->stream.ctr = (ctr128_f)vpsm4_ex_ctr32_encrypt_blocks;
  53. } else
  54. #endif
  55. #ifdef VPSM4_CAPABLE
  56. if (VPSM4_CAPABLE) {
  57. vpsm4_set_encrypt_key(key, ks);
  58. ctx->block = (block128_f)vpsm4_encrypt;
  59. ctx->stream.cbc = NULL;
  60. if (ctx->mode == EVP_CIPH_CBC_MODE)
  61. ctx->stream.cbc = (cbc128_f)vpsm4_cbc_encrypt;
  62. else if (ctx->mode == EVP_CIPH_ECB_MODE)
  63. ctx->stream.ecb = (ecb128_f)vpsm4_ecb_encrypt;
  64. else if (ctx->mode == EVP_CIPH_CTR_MODE)
  65. ctx->stream.ctr = (ctr128_f)vpsm4_ctr32_encrypt_blocks;
  66. } else
  67. #endif
  68. {
  69. ossl_sm4_set_key(key, ks);
  70. ctx->block = (block128_f)ossl_sm4_encrypt;
  71. }
  72. } else {
  73. #ifdef HWSM4_CAPABLE
  74. if (HWSM4_CAPABLE) {
  75. HWSM4_set_decrypt_key(key, ks);
  76. ctx->block = (block128_f)HWSM4_decrypt;
  77. ctx->stream.cbc = NULL;
  78. #ifdef HWSM4_cbc_encrypt
  79. if (ctx->mode == EVP_CIPH_CBC_MODE)
  80. ctx->stream.cbc = (cbc128_f)HWSM4_cbc_encrypt;
  81. #endif
  82. #ifdef HWSM4_ecb_encrypt
  83. if (ctx->mode == EVP_CIPH_ECB_MODE)
  84. ctx->stream.ecb = (ecb128_f)HWSM4_ecb_encrypt;
  85. #endif
  86. } else
  87. #endif
  88. #ifdef VPSM4_EX_CAPABLE
  89. if (VPSM4_EX_CAPABLE) {
  90. vpsm4_ex_set_decrypt_key(key, ks);
  91. ctx->block = (block128_f)vpsm4_ex_decrypt;
  92. ctx->stream.cbc = NULL;
  93. if (ctx->mode == EVP_CIPH_CBC_MODE)
  94. ctx->stream.cbc = (cbc128_f)vpsm4_ex_cbc_encrypt;
  95. else if (ctx->mode == EVP_CIPH_ECB_MODE)
  96. ctx->stream.ecb = (ecb128_f)vpsm4_ex_ecb_encrypt;
  97. } else
  98. #endif
  99. #ifdef VPSM4_CAPABLE
  100. if (VPSM4_CAPABLE) {
  101. vpsm4_set_decrypt_key(key, ks);
  102. ctx->block = (block128_f)vpsm4_decrypt;
  103. ctx->stream.cbc = NULL;
  104. if (ctx->mode == EVP_CIPH_CBC_MODE)
  105. ctx->stream.cbc = (cbc128_f)vpsm4_cbc_encrypt;
  106. else if (ctx->mode == EVP_CIPH_ECB_MODE)
  107. ctx->stream.ecb = (ecb128_f)vpsm4_ecb_encrypt;
  108. } else
  109. #endif
  110. {
  111. ossl_sm4_set_key(key, ks);
  112. ctx->block = (block128_f)ossl_sm4_decrypt;
  113. }
  114. }
  115. return 1;
  116. }
  117. IMPLEMENT_CIPHER_HW_COPYCTX(cipher_hw_sm4_copyctx, PROV_SM4_CTX)
  118. # define PROV_CIPHER_HW_sm4_mode(mode) \
  119. static const PROV_CIPHER_HW sm4_##mode = { \
  120. cipher_hw_sm4_initkey, \
  121. ossl_cipher_hw_generic_##mode, \
  122. cipher_hw_sm4_copyctx \
  123. }; \
  124. PROV_CIPHER_HW_declare(mode) \
  125. const PROV_CIPHER_HW *ossl_prov_cipher_hw_sm4_##mode(size_t keybits) \
  126. { \
  127. PROV_CIPHER_HW_select(mode) \
  128. return &sm4_##mode; \
  129. }
  130. #if defined(__riscv) && __riscv_xlen == 64
  131. # include "cipher_sm4_hw_rv64i.inc"
  132. #else
  133. /* The generic case */
  134. # define PROV_CIPHER_HW_declare(mode)
  135. # define PROV_CIPHER_HW_select(mode)
  136. #endif
  137. PROV_CIPHER_HW_sm4_mode(cbc)
  138. PROV_CIPHER_HW_sm4_mode(ecb)
  139. PROV_CIPHER_HW_sm4_mode(ofb128)
  140. PROV_CIPHER_HW_sm4_mode(cfb128)
  141. PROV_CIPHER_HW_sm4_mode(ctr)