2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * ECDSA low level APIs are deprecated for public use, but still ok for
12 * internal use - SM2 implemetation uses ECDSA_size() function.
14 #include "internal/deprecated.h"
16 #include <string.h> /* memcpy */
17 #include <openssl/crypto.h>
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/dsa.h>
21 #include <openssl/params.h>
22 #include <openssl/evp.h>
23 #include <openssl/err.h>
24 #include "internal/nelem.h"
25 #include "internal/sizes.h"
26 #include "internal/cryptlib.h"
27 #include "prov/providercommonerr.h"
28 #include "prov/implementations.h"
29 #include "prov/provider_ctx.h"
30 #include "crypto/ec.h"
31 #include "crypto/sm2.h"
32 #include "prov/der_sm2.h"
34 static OSSL_FUNC_signature_newctx_fn sm2sig_newctx;
35 static OSSL_FUNC_signature_sign_init_fn sm2sig_signature_init;
36 static OSSL_FUNC_signature_verify_init_fn sm2sig_signature_init;
37 static OSSL_FUNC_signature_sign_fn sm2sig_sign;
38 static OSSL_FUNC_signature_verify_fn sm2sig_verify;
39 static OSSL_FUNC_signature_digest_sign_init_fn sm2sig_digest_signverify_init;
40 static OSSL_FUNC_signature_digest_sign_update_fn sm2sig_digest_signverify_update;
41 static OSSL_FUNC_signature_digest_sign_final_fn sm2sig_digest_sign_final;
42 static OSSL_FUNC_signature_digest_verify_init_fn sm2sig_digest_signverify_init;
43 static OSSL_FUNC_signature_digest_verify_update_fn sm2sig_digest_signverify_update;
44 static OSSL_FUNC_signature_digest_verify_final_fn sm2sig_digest_verify_final;
45 static OSSL_FUNC_signature_freectx_fn sm2sig_freectx;
46 static OSSL_FUNC_signature_dupctx_fn sm2sig_dupctx;
47 static OSSL_FUNC_signature_get_ctx_params_fn sm2sig_get_ctx_params;
48 static OSSL_FUNC_signature_gettable_ctx_params_fn sm2sig_gettable_ctx_params;
49 static OSSL_FUNC_signature_set_ctx_params_fn sm2sig_set_ctx_params;
50 static OSSL_FUNC_signature_settable_ctx_params_fn sm2sig_settable_ctx_params;
51 static OSSL_FUNC_signature_get_ctx_md_params_fn sm2sig_get_ctx_md_params;
52 static OSSL_FUNC_signature_gettable_ctx_md_params_fn sm2sig_gettable_ctx_md_params;
53 static OSSL_FUNC_signature_set_ctx_md_params_fn sm2sig_set_ctx_md_params;
54 static OSSL_FUNC_signature_settable_ctx_md_params_fn sm2sig_settable_ctx_md_params;
57 * What's passed as an actual key is defined by the KEYMGMT interface.
58 * We happen to know that our KEYMGMT simply passes EC structures, so
59 * we use that here too.
67 * Flag to determine if the hash function can be changed (1) or not (0)
68 * Because it's dangerous to change during a DigestSign or DigestVerify
69 * operation, this flag is cleared by their Init function, and set again
70 * by their Final function.
72 unsigned int flag_allow_md : 1;
74 * Flag to termine if the 'z' digest needs to be computed and fed to the
76 * This flag should be set on initialization and the compuation should
77 * be performed only once, on first update.
79 unsigned int flag_compute_z_digest : 1;
81 char mdname[OSSL_MAX_NAME_SIZE];
83 /* The Algorithm Identifier of the combined signature algorithm */
84 unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
93 /* SM2 ID used for calculating the Z value */
98 /* TODO: it seems SM2 doesn't need this */
99 static int sm2sig_get_md_nid(const EVP_MD *md)
102 * Because the EC library deals with NIDs, we need to translate.
103 * We do so using EVP_MD_is_a(), and therefore need a name to NID
106 static const OSSL_ITEM name_to_nid[] = {
107 { NID_sm3, OSSL_DIGEST_NAME_SM3 },
110 int mdnid = NID_undef;
115 for (i = 0; i < OSSL_NELEM(name_to_nid); i++) {
116 if (EVP_MD_is_a(md, name_to_nid[i].ptr)) {
117 mdnid = (int)name_to_nid[i].id;
122 if (mdnid == NID_undef)
123 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
129 static void *sm2sig_newctx(void *provctx, const char *propq)
131 PROV_SM2_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_SM2_CTX));
136 ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
137 if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) {
140 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
142 /* don't allow to change MD, and in fact there is no such need */
143 ctx->flag_allow_md = 0;
147 static int sm2sig_signature_init(void *vpsm2ctx, void *ec)
149 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
151 if (psm2ctx == NULL || ec == NULL || !EC_KEY_up_ref(ec))
153 EC_KEY_free(psm2ctx->ec);
158 static int sm2sig_sign(void *vpsm2ctx, unsigned char *sig, size_t *siglen,
159 size_t sigsize, const unsigned char *tbs, size_t tbslen)
161 PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
164 /* SM2 uses ECDSA_size as well */
165 size_t ecsize = ECDSA_size(ctx->ec);
172 if (sigsize < (size_t)ecsize)
175 if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
178 ret = sm2_internal_sign(tbs, tbslen, sig, &sltmp, ctx->ec);
186 static int sm2sig_verify(void *vpsm2ctx, const unsigned char *sig, size_t siglen,
187 const unsigned char *tbs, size_t tbslen)
189 PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
191 if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
194 return sm2_internal_verify(tbs, tbslen, sig, siglen, ctx->ec);
197 static void free_md(PROV_SM2_CTX *ctx)
199 EVP_MD_CTX_free(ctx->mdctx);
200 EVP_MD_free(ctx->md);
206 static int sm2sig_digest_signverify_init(void *vpsm2ctx, const char *mdname,
209 PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
210 int md_nid = NID_undef;
216 if (!sm2sig_signature_init(vpsm2ctx, ec))
219 ctx->md = EVP_MD_fetch(ctx->libctx, mdname, ctx->propq);
220 if ((md_nid = sm2sig_get_md_nid(ctx->md)) == NID_undef)
223 ctx->mdsize = EVP_MD_size(ctx->md);
224 ctx->mdctx = EVP_MD_CTX_new();
225 if (ctx->mdctx == NULL)
229 * TODO(3.0) Should we care about DER writing errors?
230 * All it really means is that for some reason, there's no
231 * AlgorithmIdentifier to be had, but the operation itself is
232 * still valid, just as long as it's not used to construct
233 * anything that needs an AlgorithmIdentifier.
236 if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
237 && DER_w_algorithmIdentifier_SM2_with_MD(&pkt, -1, ctx->ec, md_nid)
238 && WPACKET_finish(&pkt)) {
239 WPACKET_get_total_written(&pkt, &ctx->aid_len);
240 ctx->aid = WPACKET_get_curr(&pkt);
242 WPACKET_cleanup(&pkt);
244 if (!EVP_DigestInit_ex(ctx->mdctx, ctx->md, NULL))
247 ctx->flag_compute_z_digest = 1;
257 static int sm2sig_compute_z_digest(PROV_SM2_CTX *ctx)
262 if (ctx->flag_compute_z_digest) {
263 /* Only do this once */
264 ctx->flag_compute_z_digest = 0;
266 if ((z = OPENSSL_zalloc(ctx->mdsize)) == NULL
267 /* get hashed prefix 'z' of tbs message */
268 || !sm2_compute_z_digest(z, ctx->md, ctx->id, ctx->id_len, ctx->ec)
269 || !EVP_DigestUpdate(ctx->mdctx, z, ctx->mdsize))
277 int sm2sig_digest_signverify_update(void *vpsm2ctx, const unsigned char *data,
280 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
282 if (psm2ctx == NULL || psm2ctx->mdctx == NULL)
285 return sm2sig_compute_z_digest(psm2ctx)
286 && EVP_DigestUpdate(psm2ctx->mdctx, data, datalen);
289 int sm2sig_digest_sign_final(void *vpsm2ctx, unsigned char *sig, size_t *siglen,
292 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
293 unsigned char digest[EVP_MAX_MD_SIZE];
294 unsigned int dlen = 0;
296 if (psm2ctx == NULL || psm2ctx->mdctx == NULL)
300 * If sig is NULL then we're just finding out the sig size. Other fields
301 * are ignored. Defer to sm2sig_sign.
304 if (!(sm2sig_compute_z_digest(psm2ctx)
305 && EVP_DigestFinal_ex(psm2ctx->mdctx, digest, &dlen)))
309 return sm2sig_sign(vpsm2ctx, sig, siglen, sigsize, digest, (size_t)dlen);
313 int sm2sig_digest_verify_final(void *vpsm2ctx, const unsigned char *sig,
316 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
317 unsigned char digest[EVP_MAX_MD_SIZE];
318 unsigned int dlen = 0;
320 if (psm2ctx == NULL || psm2ctx->mdctx == NULL)
323 /* SM2 always use SM3 so it's not possible to exceed the limit */
324 if (!(sm2sig_compute_z_digest(psm2ctx)
325 && EVP_DigestFinal_ex(psm2ctx->mdctx, digest, &dlen)))
328 return sm2sig_verify(vpsm2ctx, sig, siglen, digest, (size_t)dlen);
331 static void sm2sig_freectx(void *vpsm2ctx)
333 PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
336 EC_KEY_free(ctx->ec);
337 OPENSSL_free(ctx->id);
341 static void *sm2sig_dupctx(void *vpsm2ctx)
343 PROV_SM2_CTX *srcctx = (PROV_SM2_CTX *)vpsm2ctx;
344 PROV_SM2_CTX *dstctx;
346 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
353 dstctx->mdctx = NULL;
355 if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec))
357 dstctx->ec = srcctx->ec;
359 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
361 dstctx->md = srcctx->md;
363 if (srcctx->mdctx != NULL) {
364 dstctx->mdctx = EVP_MD_CTX_new();
365 if (dstctx->mdctx == NULL
366 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
370 if (srcctx->id != NULL) {
371 dstctx->id = OPENSSL_malloc(srcctx->id_len);
372 if (dstctx->id == NULL)
374 dstctx->id_len = srcctx->id_len;
375 memcpy(dstctx->id, srcctx->id, srcctx->id_len);
380 sm2sig_freectx(dstctx);
384 static int sm2sig_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)
386 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
389 if (psm2ctx == NULL || params == NULL)
392 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
394 && !OSSL_PARAM_set_octet_string(p, psm2ctx->aid, psm2ctx->aid_len))
397 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
398 if (p != NULL && !OSSL_PARAM_set_size_t(p, psm2ctx->mdsize))
401 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
402 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, psm2ctx->md == NULL
404 : EVP_MD_name(psm2ctx->md)))
410 static const OSSL_PARAM known_gettable_ctx_params[] = {
411 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
412 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
413 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
417 static const OSSL_PARAM *sm2sig_gettable_ctx_params(ossl_unused void *provctx)
419 return known_gettable_ctx_params;
422 static int sm2sig_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])
424 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
428 if (psm2ctx == NULL || params == NULL)
431 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DIST_ID);
437 * If the 'z' digest has already been computed, the ID is set too late
439 if (!psm2ctx->flag_compute_z_digest)
442 if (!OSSL_PARAM_get_octet_string(p, &tmp_id, 0, &tmp_idlen))
444 OPENSSL_free(psm2ctx->id);
445 psm2ctx->id = tmp_id;
446 psm2ctx->id_len = tmp_idlen;
449 if (psm2ctx->md != NULL) {
451 * You cannot set the digest name/size when doing a DigestSign or
457 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
458 if (p != NULL && !OSSL_PARAM_get_size_t(p, &psm2ctx->mdsize))
462 * We never actually use the mdname, but we do support getting it later.
463 * This can be useful for applications that want to know the MD that they
466 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
467 mdname = psm2ctx->mdname;
469 && !OSSL_PARAM_get_utf8_string(p, &mdname, sizeof(psm2ctx->mdname)))
475 static const OSSL_PARAM known_settable_ctx_params[] = {
476 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
477 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
478 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DIST_ID, NULL, 0),
482 static const OSSL_PARAM *sm2sig_settable_ctx_params(ossl_unused void *provctx)
485 * TODO(3.0): Should this function return a different set of settable ctx
486 * params if the ctx is being used for a DigestSign/DigestVerify? In that
487 * case it is not allowed to set the digest size/digest name because the
488 * digest is explicitly set as part of the init.
490 return known_settable_ctx_params;
493 static int sm2sig_get_ctx_md_params(void *vpsm2ctx, OSSL_PARAM *params)
495 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
497 if (psm2ctx->mdctx == NULL)
500 return EVP_MD_CTX_get_params(psm2ctx->mdctx, params);
503 static const OSSL_PARAM *sm2sig_gettable_ctx_md_params(void *vpsm2ctx)
505 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
507 if (psm2ctx->md == NULL)
510 return EVP_MD_gettable_ctx_params(psm2ctx->md);
513 static int sm2sig_set_ctx_md_params(void *vpsm2ctx, const OSSL_PARAM params[])
515 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
517 if (psm2ctx->mdctx == NULL)
520 return EVP_MD_CTX_set_params(psm2ctx->mdctx, params);
523 static const OSSL_PARAM *sm2sig_settable_ctx_md_params(void *vpsm2ctx)
525 PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
527 if (psm2ctx->md == NULL)
530 return EVP_MD_settable_ctx_params(psm2ctx->md);
533 const OSSL_DISPATCH sm2_signature_functions[] = {
534 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))sm2sig_newctx },
535 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))sm2sig_signature_init },
536 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))sm2sig_sign },
537 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))sm2sig_signature_init },
538 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))sm2sig_verify },
539 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
540 (void (*)(void))sm2sig_digest_signverify_init },
541 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
542 (void (*)(void))sm2sig_digest_signverify_update },
543 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
544 (void (*)(void))sm2sig_digest_sign_final },
545 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
546 (void (*)(void))sm2sig_digest_signverify_init },
547 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
548 (void (*)(void))sm2sig_digest_signverify_update },
549 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
550 (void (*)(void))sm2sig_digest_verify_final },
551 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))sm2sig_freectx },
552 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))sm2sig_dupctx },
553 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))sm2sig_get_ctx_params },
554 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
555 (void (*)(void))sm2sig_gettable_ctx_params },
556 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))sm2sig_set_ctx_params },
557 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
558 (void (*)(void))sm2sig_settable_ctx_params },
559 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
560 (void (*)(void))sm2sig_get_ctx_md_params },
561 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
562 (void (*)(void))sm2sig_gettable_ctx_md_params },
563 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
564 (void (*)(void))sm2sig_set_ctx_md_params },
565 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
566 (void (*)(void))sm2sig_settable_ctx_md_params },