sm2_enc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(psm2ctx->key, md, 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. if (dstctx->key != NULL && !EC_KEY_up_ref(dstctx->key)) {
  114. OPENSSL_free(dstctx);
  115. return NULL;
  116. }
  117. if (!ossl_prov_digest_copy(&dstctx->md, &srcctx->md)) {
  118. sm2_freectx(dstctx);
  119. return NULL;
  120. }
  121. return dstctx;
  122. }
  123. static int sm2_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)
  124. {
  125. PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
  126. OSSL_PARAM *p;
  127. if (vpsm2ctx == NULL)
  128. return 0;
  129. p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_DIGEST);
  130. if (p != NULL) {
  131. const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md);
  132. if (!OSSL_PARAM_set_utf8_string(p, md == NULL ? ""
  133. : EVP_MD_name(md)))
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. static const OSSL_PARAM known_gettable_ctx_params[] = {
  139. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0),
  140. OSSL_PARAM_END
  141. };
  142. static const OSSL_PARAM *sm2_gettable_ctx_params(ossl_unused void *vpsm2ctx,
  143. ossl_unused void *provctx)
  144. {
  145. return known_gettable_ctx_params;
  146. }
  147. static int sm2_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])
  148. {
  149. PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
  150. if (psm2ctx == NULL)
  151. return 0;
  152. if (params == NULL)
  153. return 1;
  154. if (!ossl_prov_digest_load_from_params(&psm2ctx->md, params,
  155. psm2ctx->libctx))
  156. return 0;
  157. return 1;
  158. }
  159. static const OSSL_PARAM known_settable_ctx_params[] = {
  160. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0),
  161. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PROPERTIES, NULL, 0),
  162. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_ENGINE, NULL, 0),
  163. OSSL_PARAM_END
  164. };
  165. static const OSSL_PARAM *sm2_settable_ctx_params(ossl_unused void *vpsm2ctx,
  166. ossl_unused void *provctx)
  167. {
  168. return known_settable_ctx_params;
  169. }
  170. const OSSL_DISPATCH ossl_sm2_asym_cipher_functions[] = {
  171. { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))sm2_newctx },
  172. { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))sm2_init },
  173. { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))sm2_asym_encrypt },
  174. { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))sm2_init },
  175. { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))sm2_asym_decrypt },
  176. { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))sm2_freectx },
  177. { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))sm2_dupctx },
  178. { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
  179. (void (*)(void))sm2_get_ctx_params },
  180. { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
  181. (void (*)(void))sm2_gettable_ctx_params },
  182. { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
  183. (void (*)(void))sm2_set_ctx_params },
  184. { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
  185. (void (*)(void))sm2_settable_ctx_params },
  186. { 0, NULL }
  187. };