cipher_sm4_hw.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2019-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. #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_CAPABLE
  43. if (VPSM4_CAPABLE) {
  44. vpsm4_set_encrypt_key(key, ks);
  45. ctx->block = (block128_f)vpsm4_encrypt;
  46. ctx->stream.cbc = NULL;
  47. if (ctx->mode == EVP_CIPH_CBC_MODE)
  48. ctx->stream.cbc = (cbc128_f)vpsm4_cbc_encrypt;
  49. else if (ctx->mode == EVP_CIPH_ECB_MODE)
  50. ctx->stream.ecb = (ecb128_f)vpsm4_ecb_encrypt;
  51. else if (ctx->mode == EVP_CIPH_CTR_MODE)
  52. ctx->stream.ctr = (ctr128_f)vpsm4_ctr32_encrypt_blocks;
  53. } else
  54. #endif
  55. {
  56. ossl_sm4_set_key(key, ks);
  57. ctx->block = (block128_f)ossl_sm4_encrypt;
  58. }
  59. } else {
  60. #ifdef HWSM4_CAPABLE
  61. if (HWSM4_CAPABLE) {
  62. HWSM4_set_decrypt_key(key, ks);
  63. ctx->block = (block128_f)HWSM4_decrypt;
  64. ctx->stream.cbc = NULL;
  65. #ifdef HWSM4_cbc_encrypt
  66. if (ctx->mode == EVP_CIPH_CBC_MODE)
  67. ctx->stream.cbc = (cbc128_f)HWSM4_cbc_encrypt;
  68. #endif
  69. #ifdef HWSM4_ecb_encrypt
  70. if (ctx->mode == EVP_CIPH_ECB_MODE)
  71. ctx->stream.ecb = (ecb128_f)HWSM4_ecb_encrypt;
  72. #endif
  73. } else
  74. #endif
  75. #ifdef VPSM4_CAPABLE
  76. if (VPSM4_CAPABLE) {
  77. vpsm4_set_decrypt_key(key, ks);
  78. ctx->block = (block128_f)vpsm4_decrypt;
  79. ctx->stream.cbc = NULL;
  80. if (ctx->mode == EVP_CIPH_CBC_MODE)
  81. ctx->stream.cbc = (cbc128_f)vpsm4_cbc_encrypt;
  82. else if (ctx->mode == EVP_CIPH_ECB_MODE)
  83. ctx->stream.ecb = (ecb128_f)vpsm4_ecb_encrypt;
  84. } else
  85. #endif
  86. {
  87. ossl_sm4_set_key(key, ks);
  88. ctx->block = (block128_f)ossl_sm4_decrypt;
  89. }
  90. }
  91. return 1;
  92. }
  93. IMPLEMENT_CIPHER_HW_COPYCTX(cipher_hw_sm4_copyctx, PROV_SM4_CTX)
  94. # define PROV_CIPHER_HW_sm4_mode(mode) \
  95. static const PROV_CIPHER_HW sm4_##mode = { \
  96. cipher_hw_sm4_initkey, \
  97. ossl_cipher_hw_generic_##mode, \
  98. cipher_hw_sm4_copyctx \
  99. }; \
  100. const PROV_CIPHER_HW *ossl_prov_cipher_hw_sm4_##mode(size_t keybits) \
  101. { \
  102. return &sm4_##mode; \
  103. }
  104. PROV_CIPHER_HW_sm4_mode(cbc)
  105. PROV_CIPHER_HW_sm4_mode(ecb)
  106. PROV_CIPHER_HW_sm4_mode(ofb128)
  107. PROV_CIPHER_HW_sm4_mode(cfb128)
  108. PROV_CIPHER_HW_sm4_mode(ctr)