sm2_pmeth.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright 2006-2018 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/cryptlib.h"
  10. #include <openssl/asn1t.h>
  11. #include <openssl/ec.h>
  12. #include <openssl/evp.h>
  13. #include "crypto/evp.h"
  14. #include "crypto/sm2.h"
  15. #include "crypto/sm2err.h"
  16. /* EC pkey context structure */
  17. typedef struct {
  18. /* Key and paramgen group */
  19. EC_GROUP *gen_group;
  20. /* message digest */
  21. const EVP_MD *md;
  22. /* Distinguishing Identifier, ISO/IEC 15946-3 */
  23. uint8_t *id;
  24. size_t id_len;
  25. /* id_set indicates if the 'id' field is set (1) or not (0) */
  26. int id_set;
  27. } SM2_PKEY_CTX;
  28. static int pkey_sm2_init(EVP_PKEY_CTX *ctx)
  29. {
  30. SM2_PKEY_CTX *smctx;
  31. if ((smctx = OPENSSL_zalloc(sizeof(*smctx))) == NULL) {
  32. SM2err(SM2_F_PKEY_SM2_INIT, ERR_R_MALLOC_FAILURE);
  33. return 0;
  34. }
  35. ctx->data = smctx;
  36. return 1;
  37. }
  38. static void pkey_sm2_cleanup(EVP_PKEY_CTX *ctx)
  39. {
  40. SM2_PKEY_CTX *smctx = ctx->data;
  41. if (smctx != NULL) {
  42. EC_GROUP_free(smctx->gen_group);
  43. OPENSSL_free(smctx->id);
  44. OPENSSL_free(smctx);
  45. ctx->data = NULL;
  46. }
  47. }
  48. static int pkey_sm2_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
  49. {
  50. SM2_PKEY_CTX *dctx, *sctx;
  51. if (!pkey_sm2_init(dst))
  52. return 0;
  53. sctx = src->data;
  54. dctx = dst->data;
  55. if (sctx->gen_group != NULL) {
  56. dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
  57. if (dctx->gen_group == NULL) {
  58. pkey_sm2_cleanup(dst);
  59. return 0;
  60. }
  61. }
  62. if (sctx->id != NULL) {
  63. dctx->id = OPENSSL_malloc(sctx->id_len);
  64. if (dctx->id == NULL) {
  65. SM2err(SM2_F_PKEY_SM2_COPY, ERR_R_MALLOC_FAILURE);
  66. pkey_sm2_cleanup(dst);
  67. return 0;
  68. }
  69. memcpy(dctx->id, sctx->id, sctx->id_len);
  70. }
  71. dctx->id_len = sctx->id_len;
  72. dctx->id_set = sctx->id_set;
  73. dctx->md = sctx->md;
  74. return 1;
  75. }
  76. static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  77. const unsigned char *tbs, size_t tbslen)
  78. {
  79. int ret;
  80. unsigned int sltmp;
  81. EC_KEY *ec = ctx->pkey->pkey.ec;
  82. const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec);
  83. if (sig_sz <= 0) {
  84. return 0;
  85. }
  86. if (sig == NULL) {
  87. *siglen = (size_t)sig_sz;
  88. return 1;
  89. }
  90. if (*siglen < (size_t)sig_sz) {
  91. SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL);
  92. return 0;
  93. }
  94. ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec);
  95. if (ret <= 0)
  96. return ret;
  97. *siglen = (size_t)sltmp;
  98. return 1;
  99. }
  100. static int pkey_sm2_verify(EVP_PKEY_CTX *ctx,
  101. const unsigned char *sig, size_t siglen,
  102. const unsigned char *tbs, size_t tbslen)
  103. {
  104. EC_KEY *ec = ctx->pkey->pkey.ec;
  105. return sm2_verify(tbs, tbslen, sig, siglen, ec);
  106. }
  107. static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx,
  108. unsigned char *out, size_t *outlen,
  109. const unsigned char *in, size_t inlen)
  110. {
  111. EC_KEY *ec = ctx->pkey->pkey.ec;
  112. SM2_PKEY_CTX *dctx = ctx->data;
  113. const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
  114. if (out == NULL) {
  115. if (!sm2_ciphertext_size(ec, md, inlen, outlen))
  116. return -1;
  117. else
  118. return 1;
  119. }
  120. return sm2_encrypt(ec, md, in, inlen, out, outlen);
  121. }
  122. static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx,
  123. unsigned char *out, size_t *outlen,
  124. const unsigned char *in, size_t inlen)
  125. {
  126. EC_KEY *ec = ctx->pkey->pkey.ec;
  127. SM2_PKEY_CTX *dctx = ctx->data;
  128. const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
  129. if (out == NULL) {
  130. if (!sm2_plaintext_size(ec, md, inlen, outlen))
  131. return -1;
  132. else
  133. return 1;
  134. }
  135. return sm2_decrypt(ec, md, in, inlen, out, outlen);
  136. }
  137. static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  138. {
  139. SM2_PKEY_CTX *smctx = ctx->data;
  140. EC_GROUP *group;
  141. uint8_t *tmp_id;
  142. switch (type) {
  143. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  144. group = EC_GROUP_new_by_curve_name(p1);
  145. if (group == NULL) {
  146. SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE);
  147. return 0;
  148. }
  149. EC_GROUP_free(smctx->gen_group);
  150. smctx->gen_group = group;
  151. return 1;
  152. case EVP_PKEY_CTRL_EC_PARAM_ENC:
  153. if (smctx->gen_group == NULL) {
  154. SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_NO_PARAMETERS_SET);
  155. return 0;
  156. }
  157. EC_GROUP_set_asn1_flag(smctx->gen_group, p1);
  158. return 1;
  159. case EVP_PKEY_CTRL_MD:
  160. smctx->md = p2;
  161. return 1;
  162. case EVP_PKEY_CTRL_GET_MD:
  163. *(const EVP_MD **)p2 = smctx->md;
  164. return 1;
  165. case EVP_PKEY_CTRL_SET1_ID:
  166. if (p1 > 0) {
  167. tmp_id = OPENSSL_malloc(p1);
  168. if (tmp_id == NULL) {
  169. SM2err(SM2_F_PKEY_SM2_CTRL, ERR_R_MALLOC_FAILURE);
  170. return 0;
  171. }
  172. memcpy(tmp_id, p2, p1);
  173. OPENSSL_free(smctx->id);
  174. smctx->id = tmp_id;
  175. } else {
  176. /* set null-ID */
  177. OPENSSL_free(smctx->id);
  178. smctx->id = NULL;
  179. }
  180. smctx->id_len = (size_t)p1;
  181. smctx->id_set = 1;
  182. return 1;
  183. case EVP_PKEY_CTRL_GET1_ID:
  184. memcpy(p2, smctx->id, smctx->id_len);
  185. return 1;
  186. case EVP_PKEY_CTRL_GET1_ID_LEN:
  187. *(size_t *)p2 = smctx->id_len;
  188. return 1;
  189. case EVP_PKEY_CTRL_DIGESTINIT:
  190. /* nothing to be inited, this is to suppress the error... */
  191. return 1;
  192. default:
  193. return -2;
  194. }
  195. }
  196. static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx,
  197. const char *type, const char *value)
  198. {
  199. uint8_t *hex_id;
  200. long hex_len = 0;
  201. int ret = 0;
  202. if (strcmp(type, "ec_paramgen_curve") == 0) {
  203. int nid = NID_undef;
  204. if (((nid = EC_curve_nist2nid(value)) == NID_undef)
  205. && ((nid = OBJ_sn2nid(value)) == NID_undef)
  206. && ((nid = OBJ_ln2nid(value)) == NID_undef)) {
  207. SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE);
  208. return 0;
  209. }
  210. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  211. } else if (strcmp(type, "ec_param_enc") == 0) {
  212. int param_enc;
  213. if (strcmp(value, "explicit") == 0)
  214. param_enc = 0;
  215. else if (strcmp(value, "named_curve") == 0)
  216. param_enc = OPENSSL_EC_NAMED_CURVE;
  217. else
  218. return -2;
  219. return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
  220. } else if (strcmp(type, "sm2_id") == 0) {
  221. return pkey_sm2_ctrl(ctx, EVP_PKEY_CTRL_SET1_ID,
  222. (int)strlen(value), (void *)value);
  223. } else if (strcmp(type, "sm2_hex_id") == 0) {
  224. /*
  225. * TODO(3.0): reconsider the name "sm2_hex_id", OR change
  226. * OSSL_PARAM_construct_from_text() / OSSL_PARAM_allocate_from_text()
  227. * to handle infix "_hex_"
  228. */
  229. hex_id = OPENSSL_hexstr2buf((const char *)value, &hex_len);
  230. if (hex_id == NULL) {
  231. SM2err(SM2_F_PKEY_SM2_CTRL_STR, ERR_R_PASSED_INVALID_ARGUMENT);
  232. return 0;
  233. }
  234. ret = pkey_sm2_ctrl(ctx, EVP_PKEY_CTRL_SET1_ID, (int)hex_len,
  235. (void *)hex_id);
  236. OPENSSL_free(hex_id);
  237. return ret;
  238. }
  239. return -2;
  240. }
  241. static int pkey_sm2_digest_custom(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  242. {
  243. uint8_t z[EVP_MAX_MD_SIZE];
  244. SM2_PKEY_CTX *smctx = ctx->data;
  245. EC_KEY *ec = ctx->pkey->pkey.ec;
  246. const EVP_MD *md = EVP_MD_CTX_md(mctx);
  247. int mdlen = EVP_MD_size(md);
  248. if (!smctx->id_set) {
  249. /*
  250. * An ID value must be set. The specifications are not clear whether a
  251. * NULL is allowed. We only allow it if set explicitly for maximum
  252. * flexibility.
  253. */
  254. SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_ID_NOT_SET);
  255. return 0;
  256. }
  257. if (mdlen < 0) {
  258. SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_INVALID_DIGEST);
  259. return 0;
  260. }
  261. /* get hashed prefix 'z' of tbs message */
  262. if (!sm2_compute_z_digest(z, md, smctx->id, smctx->id_len, ec))
  263. return 0;
  264. return EVP_DigestUpdate(mctx, z, (size_t)mdlen);
  265. }
  266. static const EVP_PKEY_METHOD sm2_pkey_meth = {
  267. EVP_PKEY_SM2,
  268. 0,
  269. pkey_sm2_init,
  270. pkey_sm2_copy,
  271. pkey_sm2_cleanup,
  272. 0,
  273. 0,
  274. 0,
  275. 0,
  276. 0,
  277. pkey_sm2_sign,
  278. 0,
  279. pkey_sm2_verify,
  280. 0, 0,
  281. 0, 0, 0, 0,
  282. 0,
  283. pkey_sm2_encrypt,
  284. 0,
  285. pkey_sm2_decrypt,
  286. 0,
  287. 0,
  288. pkey_sm2_ctrl,
  289. pkey_sm2_ctrl_str,
  290. 0, 0,
  291. 0, 0, 0,
  292. pkey_sm2_digest_custom
  293. };
  294. const EVP_PKEY_METHOD *sm2_pkey_method(void)
  295. {
  296. return &sm2_pkey_meth;
  297. }