e_sm4.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2017 Ribose Inc. All Rights Reserved.
  4. * Ported from Ribose contributions from Botan.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include "internal/deprecated.h"
  12. #include "internal/cryptlib.h"
  13. #ifndef OPENSSL_NO_SM4
  14. # include <openssl/evp.h>
  15. # include <openssl/modes.h>
  16. # include "crypto/sm4.h"
  17. # include "crypto/evp.h"
  18. # include "crypto/sm4_platform.h"
  19. # include "evp_local.h"
  20. typedef struct {
  21. union {
  22. OSSL_UNION_ALIGN;
  23. SM4_KEY ks;
  24. } ks;
  25. block128_f block;
  26. union {
  27. ecb128_f ecb;
  28. cbc128_f cbc;
  29. ctr128_f ctr;
  30. } stream;
  31. } EVP_SM4_KEY;
  32. # define BLOCK_CIPHER_generic(nid,blocksize,ivlen,nmode,mode,MODE,flags) \
  33. static const EVP_CIPHER sm4_##mode = { \
  34. nid##_##nmode,blocksize,128/8,ivlen, \
  35. flags|EVP_CIPH_##MODE##_MODE, \
  36. EVP_ORIG_GLOBAL, \
  37. sm4_init_key, \
  38. sm4_##mode##_cipher, \
  39. NULL, \
  40. sizeof(EVP_SM4_KEY), \
  41. NULL,NULL,NULL,NULL }; \
  42. const EVP_CIPHER *EVP_sm4_##mode(void) \
  43. { return &sm4_##mode; }
  44. #define DEFINE_BLOCK_CIPHERS(nid,flags) \
  45. BLOCK_CIPHER_generic(nid,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  46. BLOCK_CIPHER_generic(nid,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  47. BLOCK_CIPHER_generic(nid,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  48. BLOCK_CIPHER_generic(nid,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  49. BLOCK_CIPHER_generic(nid,1,16,ctr,ctr,CTR,flags)
  50. static int sm4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  51. const unsigned char *iv, int enc)
  52. {
  53. int mode;
  54. EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY,ctx);
  55. mode = EVP_CIPHER_CTX_get_mode(ctx);
  56. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  57. && !enc) {
  58. #ifdef HWSM4_CAPABLE
  59. if (HWSM4_CAPABLE) {
  60. HWSM4_set_decrypt_key(key, &dat->ks.ks);
  61. dat->block = (block128_f) HWSM4_decrypt;
  62. dat->stream.cbc = NULL;
  63. # ifdef HWSM4_cbc_encrypt
  64. if (mode == EVP_CIPH_CBC_MODE)
  65. dat->stream.cbc = (cbc128_f) HWSM4_cbc_encrypt;
  66. # endif
  67. # ifdef HWSM4_ecb_encrypt
  68. if (mode == EVP_CIPH_ECB_MODE)
  69. dat->stream.ecb = (ecb128_f) HWSM4_ecb_encrypt;
  70. # endif
  71. } else
  72. #endif
  73. #ifdef VPSM4_CAPABLE
  74. if (VPSM4_CAPABLE) {
  75. vpsm4_set_decrypt_key(key, &dat->ks.ks);
  76. dat->block = (block128_f) vpsm4_decrypt;
  77. dat->stream.cbc = NULL;
  78. if (mode == EVP_CIPH_CBC_MODE)
  79. dat->stream.cbc = (cbc128_f) vpsm4_cbc_encrypt;
  80. else if (mode == EVP_CIPH_ECB_MODE)
  81. dat->stream.ecb = (ecb128_f) vpsm4_ecb_encrypt;
  82. } else
  83. #endif
  84. {
  85. dat->block = (block128_f) ossl_sm4_decrypt;
  86. ossl_sm4_set_key(key, EVP_CIPHER_CTX_get_cipher_data(ctx));
  87. }
  88. } else
  89. #ifdef HWSM4_CAPABLE
  90. if (HWSM4_CAPABLE) {
  91. HWSM4_set_encrypt_key(key, &dat->ks.ks);
  92. dat->block = (block128_f) HWSM4_encrypt;
  93. dat->stream.cbc = NULL;
  94. # ifdef HWSM4_cbc_encrypt
  95. if (mode == EVP_CIPH_CBC_MODE)
  96. dat->stream.cbc = (cbc128_f) HWSM4_cbc_encrypt;
  97. else
  98. # endif
  99. # ifdef HWSM4_ecb_encrypt
  100. if (mode == EVP_CIPH_ECB_MODE)
  101. dat->stream.ecb = (ecb128_f) HWSM4_ecb_encrypt;
  102. else
  103. # endif
  104. # ifdef HWSM4_ctr32_encrypt_blocks
  105. if (mode == EVP_CIPH_CTR_MODE)
  106. dat->stream.ctr = (ctr128_f) HWSM4_ctr32_encrypt_blocks;
  107. else
  108. # endif
  109. (void)0; /* terminate potentially open 'else' */
  110. } else
  111. #endif
  112. #ifdef VPSM4_CAPABLE
  113. if (VPSM4_CAPABLE) {
  114. vpsm4_set_encrypt_key(key, &dat->ks.ks);
  115. dat->block = (block128_f) vpsm4_encrypt;
  116. dat->stream.cbc = NULL;
  117. if (mode == EVP_CIPH_CBC_MODE)
  118. dat->stream.cbc = (cbc128_f) vpsm4_cbc_encrypt;
  119. else if (mode == EVP_CIPH_ECB_MODE)
  120. dat->stream.ecb = (ecb128_f) vpsm4_ecb_encrypt;
  121. else if (mode == EVP_CIPH_CTR_MODE)
  122. dat->stream.ctr = (ctr128_f) vpsm4_ctr32_encrypt_blocks;
  123. } else
  124. #endif
  125. {
  126. dat->block = (block128_f) ossl_sm4_encrypt;
  127. ossl_sm4_set_key(key, EVP_CIPHER_CTX_get_cipher_data(ctx));
  128. }
  129. return 1;
  130. }
  131. static int sm4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  132. const unsigned char *in, size_t len)
  133. {
  134. EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY,ctx);
  135. if (dat->stream.cbc)
  136. (*dat->stream.cbc) (in, out, len, &dat->ks.ks, ctx->iv,
  137. EVP_CIPHER_CTX_is_encrypting(ctx));
  138. else if (EVP_CIPHER_CTX_is_encrypting(ctx))
  139. CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv,
  140. dat->block);
  141. else
  142. CRYPTO_cbc128_decrypt(in, out, len, &dat->ks,
  143. ctx->iv, dat->block);
  144. return 1;
  145. }
  146. static int sm4_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  147. const unsigned char *in, size_t len)
  148. {
  149. EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY,ctx);
  150. int num = EVP_CIPHER_CTX_get_num(ctx);
  151. CRYPTO_cfb128_encrypt(in, out, len, &dat->ks,
  152. ctx->iv, &num,
  153. EVP_CIPHER_CTX_is_encrypting(ctx), dat->block);
  154. EVP_CIPHER_CTX_set_num(ctx, num);
  155. return 1;
  156. }
  157. static int sm4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  158. const unsigned char *in, size_t len)
  159. {
  160. size_t bl = EVP_CIPHER_CTX_get_block_size(ctx);
  161. size_t i;
  162. EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY,ctx);
  163. if (len < bl)
  164. return 1;
  165. if (dat->stream.ecb != NULL)
  166. (*dat->stream.ecb) (in, out, len, &dat->ks.ks,
  167. EVP_CIPHER_CTX_is_encrypting(ctx));
  168. else
  169. for (i = 0, len -= bl; i <= len; i += bl)
  170. (*dat->block) (in + i, out + i, &dat->ks);
  171. return 1;
  172. }
  173. static int sm4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  174. const unsigned char *in, size_t len)
  175. {
  176. EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY,ctx);
  177. int num = EVP_CIPHER_CTX_get_num(ctx);
  178. CRYPTO_ofb128_encrypt(in, out, len, &dat->ks,
  179. ctx->iv, &num, dat->block);
  180. EVP_CIPHER_CTX_set_num(ctx, num);
  181. return 1;
  182. }
  183. static int sm4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  184. const unsigned char *in, size_t len)
  185. {
  186. int n = EVP_CIPHER_CTX_get_num(ctx);
  187. unsigned int num;
  188. EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY,ctx);
  189. if (n < 0)
  190. return 0;
  191. num = (unsigned int)n;
  192. if (dat->stream.ctr)
  193. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks,
  194. ctx->iv,
  195. EVP_CIPHER_CTX_buf_noconst(ctx),
  196. &num, dat->stream.ctr);
  197. else
  198. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
  199. ctx->iv,
  200. EVP_CIPHER_CTX_buf_noconst(ctx), &num,
  201. dat->block);
  202. EVP_CIPHER_CTX_set_num(ctx, num);
  203. return 1;
  204. }
  205. DEFINE_BLOCK_CIPHERS(NID_sm4, 0)
  206. #endif