sm2_enc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2020-2021 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 "internal/deprecated.h"
  10. #include <openssl/crypto.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/core_dispatch.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/params.h>
  15. #include <openssl/err.h>
  16. #include <openssl/proverr.h>
  17. #include "crypto/sm2.h"
  18. #include "prov/provider_ctx.h"
  19. #include "prov/implementations.h"
  20. #include "prov/provider_util.h"
  21. static OSSL_FUNC_asym_cipher_newctx_fn sm2_newctx;
  22. static OSSL_FUNC_asym_cipher_encrypt_init_fn sm2_init;
  23. static OSSL_FUNC_asym_cipher_encrypt_fn sm2_asym_encrypt;
  24. static OSSL_FUNC_asym_cipher_decrypt_init_fn sm2_init;
  25. static OSSL_FUNC_asym_cipher_decrypt_fn sm2_asym_decrypt;
  26. static OSSL_FUNC_asym_cipher_freectx_fn sm2_freectx;
  27. static OSSL_FUNC_asym_cipher_dupctx_fn sm2_dupctx;
  28. static OSSL_FUNC_asym_cipher_get_ctx_params_fn sm2_get_ctx_params;
  29. static OSSL_FUNC_asym_cipher_gettable_ctx_params_fn sm2_gettable_ctx_params;
  30. static OSSL_FUNC_asym_cipher_set_ctx_params_fn sm2_set_ctx_params;
  31. static OSSL_FUNC_asym_cipher_settable_ctx_params_fn sm2_settable_ctx_params;
  32. /*
  33. * What's passed as an actual key is defined by the KEYMGMT interface.
  34. * We happen to know that our KEYMGMT simply passes EC_KEY structures, so
  35. * we use that here too.
  36. */
  37. typedef struct {
  38. OSSL_LIB_CTX *libctx;
  39. EC_KEY *key;
  40. PROV_DIGEST md;
  41. } PROV_SM2_CTX;
  42. static void *sm2_newctx(void *provctx)
  43. {
  44. PROV_SM2_CTX *psm2ctx = OPENSSL_zalloc(sizeof(PROV_SM2_CTX));
  45. if (psm2ctx == NULL)
  46. return NULL;
  47. psm2ctx->libctx = PROV_LIBCTX_OF(provctx);
  48. return psm2ctx;
  49. }
  50. static int sm2_init(void *vpsm2ctx, void *vkey, const OSSL_PARAM params[])
  51. {
  52. PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
  53. if (psm2ctx == NULL || vkey == NULL || !EC_KEY_up_ref(vkey))
  54. return 0;
  55. EC_KEY_free(psm2ctx->key);
  56. psm2ctx->key = vkey;
  57. return sm2_set_ctx_params(psm2ctx, params);
  58. }
  59. static const EVP_MD *sm2_get_md(PROV_SM2_CTX *psm2ctx)
  60. {
  61. const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md);
  62. if (md == NULL)
  63. md = ossl_prov_digest_fetch(&psm2ctx->md, psm2ctx->libctx, "SM3", NULL);
  64. return md;
  65. }
  66. static int sm2_asym_encrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen,
  67. size_t outsize, const unsigned char *in,
  68. size_t inlen)
  69. {
  70. PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
  71. const EVP_MD *md = sm2_get_md(psm2ctx);
  72. if (md == NULL)
  73. return 0;
  74. if (out == NULL) {
  75. if (!ossl_sm2_ciphertext_size(psm2ctx->key, md, inlen, outlen)) {
  76. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. return ossl_sm2_encrypt(psm2ctx->key, md, in, inlen, out, outlen);
  82. }
  83. static int sm2_asym_decrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen,
  84. size_t outsize, const unsigned char *in,
  85. size_t inlen)
  86. {
  87. PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
  88. const EVP_MD *md = sm2_get_md(psm2ctx);
  89. if (md == NULL)
  90. return 0;
  91. if (out == NULL) {
  92. if (!ossl_sm2_plaintext_size(in, inlen, outlen))
  93. return 0;
  94. return 1;
  95. }
  96. return ossl_sm2_decrypt(psm2ctx->key, md, in, inlen, out, outlen);
  97. }
  98. static void sm2_freectx(void *vpsm2ctx)
  99. {
  100. PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
  101. EC_KEY_free(psm2ctx->key);
  102. ossl_prov_digest_reset(&psm2ctx->md);
  103. OPENSSL_free(psm2ctx);
  104. }
  105. static void *sm2_dupctx(void *vpsm2ctx)
  106. {
  107. PROV_SM2_CTX *srcctx = (PROV_SM2_CTX *)vpsm2ctx;
  108. PROV_SM2_CTX *dstctx;
  109. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  110. if (dstctx == NULL)
  111. return NULL;
  112. *dstctx = *srcctx;
  113. memset(&dstctx->md, 0, sizeof(dstctx->md));
  114. if (dstctx->key != NULL && !EC_KEY_up_ref(dstctx->key)) {
  115. OPENSSL_free(dstctx);
  116. return NULL;
  117. }
  118. if (!ossl_prov_digest_copy(&dstctx->md, &srcctx->md)) {
  119. sm2_freectx(dstctx);
  120. return NULL;
  121. }
  122. return dstctx;
  123. }
  124. static int sm2_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)
  125. {
  126. PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
  127. OSSL_PARAM *p;
  128. if (vpsm2ctx == NULL)
  129. return 0;
  130. p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_DIGEST);
  131. if (p != NULL) {
  132. const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md);
  133. if (!OSSL_PARAM_set_utf8_string(p, md == NULL ? ""
  134. : EVP_MD_get0_name(md)))
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. static const OSSL_PARAM known_gettable_ctx_params[] = {
  140. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0),
  141. OSSL_PARAM_END
  142. };
  143. static const OSSL_PARAM *sm2_gettable_ctx_params(ossl_unused void *vpsm2ctx,
  144. ossl_unused void *provctx)
  145. {
  146. return known_gettable_ctx_params;
  147. }
  148. static int sm2_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])
  149. {
  150. PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
  151. if (psm2ctx == NULL)
  152. return 0;
  153. if (params == NULL)
  154. return 1;
  155. if (!ossl_prov_digest_load_from_params(&psm2ctx->md, params,
  156. psm2ctx->libctx))
  157. return 0;
  158. return 1;
  159. }
  160. static const OSSL_PARAM known_settable_ctx_params[] = {
  161. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0),
  162. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PROPERTIES, NULL, 0),
  163. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_ENGINE, NULL, 0),
  164. OSSL_PARAM_END
  165. };
  166. static const OSSL_PARAM *sm2_settable_ctx_params(ossl_unused void *vpsm2ctx,
  167. ossl_unused void *provctx)
  168. {
  169. return known_settable_ctx_params;
  170. }
  171. const OSSL_DISPATCH ossl_sm2_asym_cipher_functions[] = {
  172. { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))sm2_newctx },
  173. { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))sm2_init },
  174. { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))sm2_asym_encrypt },
  175. { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))sm2_init },
  176. { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))sm2_asym_decrypt },
  177. { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))sm2_freectx },
  178. { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))sm2_dupctx },
  179. { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
  180. (void (*)(void))sm2_get_ctx_params },
  181. { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
  182. (void (*)(void))sm2_gettable_ctx_params },
  183. { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
  184. (void (*)(void))sm2_set_ctx_params },
  185. { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
  186. (void (*)(void))sm2_settable_ctx_params },
  187. OSSL_DISPATCH_END
  188. };