xref: /freebsd/crypto/openssl/providers/implementations/signature/sm2_sig.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1b077aed3SPierre Pronchery /*
2*e7be843bSPierre Pronchery  * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery  */
9b077aed3SPierre Pronchery 
10b077aed3SPierre Pronchery /*
11b077aed3SPierre Pronchery  * ECDSA low level APIs are deprecated for public use, but still ok for
12*e7be843bSPierre Pronchery  * internal use - SM2 implementation uses ECDSA_size() function.
13b077aed3SPierre Pronchery  */
14b077aed3SPierre Pronchery #include "internal/deprecated.h"
15b077aed3SPierre Pronchery 
16b077aed3SPierre Pronchery #include <string.h> /* memcpy */
17b077aed3SPierre Pronchery #include <openssl/crypto.h>
18b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
19b077aed3SPierre Pronchery #include <openssl/core_names.h>
20b077aed3SPierre Pronchery #include <openssl/dsa.h>
21b077aed3SPierre Pronchery #include <openssl/params.h>
22b077aed3SPierre Pronchery #include <openssl/evp.h>
23b077aed3SPierre Pronchery #include <openssl/err.h>
24b077aed3SPierre Pronchery #include <openssl/proverr.h>
25b077aed3SPierre Pronchery #include "internal/nelem.h"
26b077aed3SPierre Pronchery #include "internal/sizes.h"
27b077aed3SPierre Pronchery #include "internal/cryptlib.h"
28b077aed3SPierre Pronchery #include "internal/sm3.h"
29b077aed3SPierre Pronchery #include "prov/implementations.h"
30b077aed3SPierre Pronchery #include "prov/providercommon.h"
31b077aed3SPierre Pronchery #include "prov/provider_ctx.h"
32b077aed3SPierre Pronchery #include "crypto/ec.h"
33b077aed3SPierre Pronchery #include "crypto/sm2.h"
34b077aed3SPierre Pronchery #include "prov/der_sm2.h"
35b077aed3SPierre Pronchery 
36b077aed3SPierre Pronchery static OSSL_FUNC_signature_newctx_fn sm2sig_newctx;
37b077aed3SPierre Pronchery static OSSL_FUNC_signature_sign_init_fn sm2sig_signature_init;
38b077aed3SPierre Pronchery static OSSL_FUNC_signature_verify_init_fn sm2sig_signature_init;
39b077aed3SPierre Pronchery static OSSL_FUNC_signature_sign_fn sm2sig_sign;
40b077aed3SPierre Pronchery static OSSL_FUNC_signature_verify_fn sm2sig_verify;
41b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_init_fn sm2sig_digest_signverify_init;
42b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_update_fn sm2sig_digest_signverify_update;
43b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_final_fn sm2sig_digest_sign_final;
44b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_verify_init_fn sm2sig_digest_signverify_init;
45b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_verify_update_fn sm2sig_digest_signverify_update;
46b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_verify_final_fn sm2sig_digest_verify_final;
47b077aed3SPierre Pronchery static OSSL_FUNC_signature_freectx_fn sm2sig_freectx;
48b077aed3SPierre Pronchery static OSSL_FUNC_signature_dupctx_fn sm2sig_dupctx;
49b077aed3SPierre Pronchery static OSSL_FUNC_signature_get_ctx_params_fn sm2sig_get_ctx_params;
50b077aed3SPierre Pronchery static OSSL_FUNC_signature_gettable_ctx_params_fn sm2sig_gettable_ctx_params;
51b077aed3SPierre Pronchery static OSSL_FUNC_signature_set_ctx_params_fn sm2sig_set_ctx_params;
52b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_params_fn sm2sig_settable_ctx_params;
53b077aed3SPierre Pronchery static OSSL_FUNC_signature_get_ctx_md_params_fn sm2sig_get_ctx_md_params;
54b077aed3SPierre Pronchery static OSSL_FUNC_signature_gettable_ctx_md_params_fn sm2sig_gettable_ctx_md_params;
55b077aed3SPierre Pronchery static OSSL_FUNC_signature_set_ctx_md_params_fn sm2sig_set_ctx_md_params;
56b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_md_params_fn sm2sig_settable_ctx_md_params;
57b077aed3SPierre Pronchery 
58b077aed3SPierre Pronchery /*
59b077aed3SPierre Pronchery  * What's passed as an actual key is defined by the KEYMGMT interface.
60b077aed3SPierre Pronchery  * We happen to know that our KEYMGMT simply passes EC structures, so
61b077aed3SPierre Pronchery  * we use that here too.
62b077aed3SPierre Pronchery  */
63b077aed3SPierre Pronchery typedef struct {
64b077aed3SPierre Pronchery     OSSL_LIB_CTX *libctx;
65b077aed3SPierre Pronchery     char *propq;
66b077aed3SPierre Pronchery     EC_KEY *ec;
67b077aed3SPierre Pronchery 
68b077aed3SPierre Pronchery     /*
69*e7be843bSPierre Pronchery      * Flag to determine if the 'z' digest needs to be computed and fed to the
70b077aed3SPierre Pronchery      * hash function.
71*e7be843bSPierre Pronchery      * This flag should be set on initialization and the computation should
72b077aed3SPierre Pronchery      * be performed only once, on first update.
73b077aed3SPierre Pronchery      */
74b077aed3SPierre Pronchery     unsigned int flag_compute_z_digest : 1;
75b077aed3SPierre Pronchery 
76b077aed3SPierre Pronchery     char mdname[OSSL_MAX_NAME_SIZE];
77b077aed3SPierre Pronchery 
78b077aed3SPierre Pronchery     /* The Algorithm Identifier of the combined signature algorithm */
79b077aed3SPierre Pronchery     unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
80b077aed3SPierre Pronchery     size_t  aid_len;
81b077aed3SPierre Pronchery 
82b077aed3SPierre Pronchery     /* main digest */
83b077aed3SPierre Pronchery     EVP_MD *md;
84b077aed3SPierre Pronchery     EVP_MD_CTX *mdctx;
85b077aed3SPierre Pronchery     size_t mdsize;
86b077aed3SPierre Pronchery 
87b077aed3SPierre Pronchery     /* SM2 ID used for calculating the Z value */
88b077aed3SPierre Pronchery     unsigned char *id;
89b077aed3SPierre Pronchery     size_t id_len;
90b077aed3SPierre Pronchery } PROV_SM2_CTX;
91b077aed3SPierre Pronchery 
sm2sig_set_mdname(PROV_SM2_CTX * psm2ctx,const char * mdname)92b077aed3SPierre Pronchery static int sm2sig_set_mdname(PROV_SM2_CTX *psm2ctx, const char *mdname)
93b077aed3SPierre Pronchery {
94b077aed3SPierre Pronchery     if (psm2ctx->md == NULL) /* We need an SM3 md to compare with */
95b077aed3SPierre Pronchery         psm2ctx->md = EVP_MD_fetch(psm2ctx->libctx, psm2ctx->mdname,
96b077aed3SPierre Pronchery                                    psm2ctx->propq);
97b077aed3SPierre Pronchery     if (psm2ctx->md == NULL)
98b077aed3SPierre Pronchery         return 0;
99b077aed3SPierre Pronchery 
100*e7be843bSPierre Pronchery     /* XOF digests don't work */
101*e7be843bSPierre Pronchery     if (EVP_MD_xof(psm2ctx->md)) {
102*e7be843bSPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
103*e7be843bSPierre Pronchery         return 0;
104*e7be843bSPierre Pronchery     }
105*e7be843bSPierre Pronchery 
106b077aed3SPierre Pronchery     if (mdname == NULL)
107b077aed3SPierre Pronchery         return 1;
108b077aed3SPierre Pronchery 
109b077aed3SPierre Pronchery     if (strlen(mdname) >= sizeof(psm2ctx->mdname)
110b077aed3SPierre Pronchery         || !EVP_MD_is_a(psm2ctx->md, mdname)) {
111b077aed3SPierre Pronchery         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, "digest=%s",
112b077aed3SPierre Pronchery                        mdname);
113b077aed3SPierre Pronchery         return 0;
114b077aed3SPierre Pronchery     }
115b077aed3SPierre Pronchery 
116b077aed3SPierre Pronchery     OPENSSL_strlcpy(psm2ctx->mdname, mdname, sizeof(psm2ctx->mdname));
117b077aed3SPierre Pronchery     return 1;
118b077aed3SPierre Pronchery }
119b077aed3SPierre Pronchery 
sm2sig_newctx(void * provctx,const char * propq)120b077aed3SPierre Pronchery static void *sm2sig_newctx(void *provctx, const char *propq)
121b077aed3SPierre Pronchery {
122b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_SM2_CTX));
123b077aed3SPierre Pronchery 
124b077aed3SPierre Pronchery     if (ctx == NULL)
125b077aed3SPierre Pronchery         return NULL;
126b077aed3SPierre Pronchery 
127b077aed3SPierre Pronchery     ctx->libctx = PROV_LIBCTX_OF(provctx);
128b077aed3SPierre Pronchery     if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) {
129b077aed3SPierre Pronchery         OPENSSL_free(ctx);
130b077aed3SPierre Pronchery         return NULL;
131b077aed3SPierre Pronchery     }
132b077aed3SPierre Pronchery     ctx->mdsize = SM3_DIGEST_LENGTH;
133b077aed3SPierre Pronchery     strcpy(ctx->mdname, OSSL_DIGEST_NAME_SM3);
134b077aed3SPierre Pronchery     return ctx;
135b077aed3SPierre Pronchery }
136b077aed3SPierre Pronchery 
sm2sig_signature_init(void * vpsm2ctx,void * ec,const OSSL_PARAM params[])137b077aed3SPierre Pronchery static int sm2sig_signature_init(void *vpsm2ctx, void *ec,
138b077aed3SPierre Pronchery                                  const OSSL_PARAM params[])
139b077aed3SPierre Pronchery {
140b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
141b077aed3SPierre Pronchery 
142b077aed3SPierre Pronchery     if (!ossl_prov_is_running()
143b077aed3SPierre Pronchery             || psm2ctx == NULL)
144b077aed3SPierre Pronchery         return 0;
145b077aed3SPierre Pronchery 
146b077aed3SPierre Pronchery     if (ec == NULL && psm2ctx->ec == NULL) {
147b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
148b077aed3SPierre Pronchery         return 0;
149b077aed3SPierre Pronchery     }
150b077aed3SPierre Pronchery 
151b077aed3SPierre Pronchery     if (ec != NULL) {
152b077aed3SPierre Pronchery         if (!EC_KEY_up_ref(ec))
153b077aed3SPierre Pronchery             return 0;
154b077aed3SPierre Pronchery         EC_KEY_free(psm2ctx->ec);
155b077aed3SPierre Pronchery         psm2ctx->ec = ec;
156b077aed3SPierre Pronchery     }
157b077aed3SPierre Pronchery 
158b077aed3SPierre Pronchery     return sm2sig_set_ctx_params(psm2ctx, params);
159b077aed3SPierre Pronchery }
160b077aed3SPierre Pronchery 
sm2sig_sign(void * vpsm2ctx,unsigned char * sig,size_t * siglen,size_t sigsize,const unsigned char * tbs,size_t tbslen)161b077aed3SPierre Pronchery static int sm2sig_sign(void *vpsm2ctx, unsigned char *sig, size_t *siglen,
162b077aed3SPierre Pronchery                        size_t sigsize, const unsigned char *tbs, size_t tbslen)
163b077aed3SPierre Pronchery {
164b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
165b077aed3SPierre Pronchery     int ret;
166b077aed3SPierre Pronchery     unsigned int sltmp;
167b077aed3SPierre Pronchery     /* SM2 uses ECDSA_size as well */
168b077aed3SPierre Pronchery     size_t ecsize = ECDSA_size(ctx->ec);
169b077aed3SPierre Pronchery 
170b077aed3SPierre Pronchery     if (sig == NULL) {
171b077aed3SPierre Pronchery         *siglen = ecsize;
172b077aed3SPierre Pronchery         return 1;
173b077aed3SPierre Pronchery     }
174b077aed3SPierre Pronchery 
175b077aed3SPierre Pronchery     if (sigsize < (size_t)ecsize)
176b077aed3SPierre Pronchery         return 0;
177b077aed3SPierre Pronchery 
178b077aed3SPierre Pronchery     if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
179b077aed3SPierre Pronchery         return 0;
180b077aed3SPierre Pronchery 
181b077aed3SPierre Pronchery     ret = ossl_sm2_internal_sign(tbs, tbslen, sig, &sltmp, ctx->ec);
182b077aed3SPierre Pronchery     if (ret <= 0)
183b077aed3SPierre Pronchery         return 0;
184b077aed3SPierre Pronchery 
185b077aed3SPierre Pronchery     *siglen = sltmp;
186b077aed3SPierre Pronchery     return 1;
187b077aed3SPierre Pronchery }
188b077aed3SPierre Pronchery 
sm2sig_verify(void * vpsm2ctx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen)189b077aed3SPierre Pronchery static int sm2sig_verify(void *vpsm2ctx, const unsigned char *sig, size_t siglen,
190b077aed3SPierre Pronchery                          const unsigned char *tbs, size_t tbslen)
191b077aed3SPierre Pronchery {
192b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
193b077aed3SPierre Pronchery 
194b077aed3SPierre Pronchery     if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
195b077aed3SPierre Pronchery         return 0;
196b077aed3SPierre Pronchery 
197b077aed3SPierre Pronchery     return ossl_sm2_internal_verify(tbs, tbslen, sig, siglen, ctx->ec);
198b077aed3SPierre Pronchery }
199b077aed3SPierre Pronchery 
free_md(PROV_SM2_CTX * ctx)200b077aed3SPierre Pronchery static void free_md(PROV_SM2_CTX *ctx)
201b077aed3SPierre Pronchery {
202b077aed3SPierre Pronchery     EVP_MD_CTX_free(ctx->mdctx);
203b077aed3SPierre Pronchery     EVP_MD_free(ctx->md);
204b077aed3SPierre Pronchery     ctx->mdctx = NULL;
205b077aed3SPierre Pronchery     ctx->md = NULL;
206b077aed3SPierre Pronchery }
207b077aed3SPierre Pronchery 
sm2sig_digest_signverify_init(void * vpsm2ctx,const char * mdname,void * ec,const OSSL_PARAM params[])208b077aed3SPierre Pronchery static int sm2sig_digest_signverify_init(void *vpsm2ctx, const char *mdname,
209b077aed3SPierre Pronchery                                          void *ec, const OSSL_PARAM params[])
210b077aed3SPierre Pronchery {
211b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
212b077aed3SPierre Pronchery     int md_nid;
213b077aed3SPierre Pronchery     WPACKET pkt;
214b077aed3SPierre Pronchery     int ret = 0;
215*e7be843bSPierre Pronchery     unsigned char *aid = NULL;
216b077aed3SPierre Pronchery 
217b077aed3SPierre Pronchery     if (!sm2sig_signature_init(vpsm2ctx, ec, params)
218b077aed3SPierre Pronchery         || !sm2sig_set_mdname(ctx, mdname))
219b077aed3SPierre Pronchery         return ret;
220b077aed3SPierre Pronchery 
221b077aed3SPierre Pronchery     if (ctx->mdctx == NULL) {
222b077aed3SPierre Pronchery         ctx->mdctx = EVP_MD_CTX_new();
223b077aed3SPierre Pronchery         if (ctx->mdctx == NULL)
224b077aed3SPierre Pronchery             goto error;
225b077aed3SPierre Pronchery     }
226b077aed3SPierre Pronchery 
227b077aed3SPierre Pronchery     md_nid = EVP_MD_get_type(ctx->md);
228b077aed3SPierre Pronchery 
229b077aed3SPierre Pronchery     /*
230b077aed3SPierre Pronchery      * We do not care about DER writing errors.
231b077aed3SPierre Pronchery      * All it really means is that for some reason, there's no
232b077aed3SPierre Pronchery      * AlgorithmIdentifier to be had, but the operation itself is
233b077aed3SPierre Pronchery      * still valid, just as long as it's not used to construct
234b077aed3SPierre Pronchery      * anything that needs an AlgorithmIdentifier.
235b077aed3SPierre Pronchery      */
236b077aed3SPierre Pronchery     ctx->aid_len = 0;
237b077aed3SPierre Pronchery     if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
238b077aed3SPierre Pronchery         && ossl_DER_w_algorithmIdentifier_SM2_with_MD(&pkt, -1, ctx->ec, md_nid)
239b077aed3SPierre Pronchery         && WPACKET_finish(&pkt)) {
240b077aed3SPierre Pronchery         WPACKET_get_total_written(&pkt, &ctx->aid_len);
241*e7be843bSPierre Pronchery         aid = WPACKET_get_curr(&pkt);
242b077aed3SPierre Pronchery     }
243b077aed3SPierre Pronchery     WPACKET_cleanup(&pkt);
244*e7be843bSPierre Pronchery     if (aid != NULL && ctx->aid_len != 0)
245*e7be843bSPierre Pronchery         memmove(ctx->aid_buf, aid, ctx->aid_len);
246b077aed3SPierre Pronchery 
247b077aed3SPierre Pronchery     if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params))
248b077aed3SPierre Pronchery         goto error;
249b077aed3SPierre Pronchery 
250b077aed3SPierre Pronchery     ctx->flag_compute_z_digest = 1;
251b077aed3SPierre Pronchery 
252b077aed3SPierre Pronchery     ret = 1;
253b077aed3SPierre Pronchery 
254b077aed3SPierre Pronchery  error:
255b077aed3SPierre Pronchery     return ret;
256b077aed3SPierre Pronchery }
257b077aed3SPierre Pronchery 
sm2sig_compute_z_digest(PROV_SM2_CTX * ctx)258b077aed3SPierre Pronchery static int sm2sig_compute_z_digest(PROV_SM2_CTX *ctx)
259b077aed3SPierre Pronchery {
260b077aed3SPierre Pronchery     uint8_t *z = NULL;
261b077aed3SPierre Pronchery     int ret = 1;
262b077aed3SPierre Pronchery 
263b077aed3SPierre Pronchery     if (ctx->flag_compute_z_digest) {
264b077aed3SPierre Pronchery         /* Only do this once */
265b077aed3SPierre Pronchery         ctx->flag_compute_z_digest = 0;
266b077aed3SPierre Pronchery 
267b077aed3SPierre Pronchery         if ((z = OPENSSL_zalloc(ctx->mdsize)) == NULL
268b077aed3SPierre Pronchery             /* get hashed prefix 'z' of tbs message */
269b077aed3SPierre Pronchery             || !ossl_sm2_compute_z_digest(z, ctx->md, ctx->id, ctx->id_len,
270b077aed3SPierre Pronchery                                           ctx->ec)
271b077aed3SPierre Pronchery             || !EVP_DigestUpdate(ctx->mdctx, z, ctx->mdsize))
272b077aed3SPierre Pronchery             ret = 0;
273b077aed3SPierre Pronchery         OPENSSL_free(z);
274b077aed3SPierre Pronchery     }
275b077aed3SPierre Pronchery 
276b077aed3SPierre Pronchery     return ret;
277b077aed3SPierre Pronchery }
278b077aed3SPierre Pronchery 
sm2sig_digest_signverify_update(void * vpsm2ctx,const unsigned char * data,size_t datalen)279b077aed3SPierre Pronchery int sm2sig_digest_signverify_update(void *vpsm2ctx, const unsigned char *data,
280b077aed3SPierre Pronchery                                     size_t datalen)
281b077aed3SPierre Pronchery {
282b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
283b077aed3SPierre Pronchery 
284b077aed3SPierre Pronchery     if (psm2ctx == NULL || psm2ctx->mdctx == NULL)
285b077aed3SPierre Pronchery         return 0;
286b077aed3SPierre Pronchery 
287b077aed3SPierre Pronchery     return sm2sig_compute_z_digest(psm2ctx)
288b077aed3SPierre Pronchery         && EVP_DigestUpdate(psm2ctx->mdctx, data, datalen);
289b077aed3SPierre Pronchery }
290b077aed3SPierre Pronchery 
sm2sig_digest_sign_final(void * vpsm2ctx,unsigned char * sig,size_t * siglen,size_t sigsize)291b077aed3SPierre Pronchery int sm2sig_digest_sign_final(void *vpsm2ctx, unsigned char *sig, size_t *siglen,
292b077aed3SPierre Pronchery                              size_t sigsize)
293b077aed3SPierre Pronchery {
294b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
295b077aed3SPierre Pronchery     unsigned char digest[EVP_MAX_MD_SIZE];
296b077aed3SPierre Pronchery     unsigned int dlen = 0;
297b077aed3SPierre Pronchery 
298b077aed3SPierre Pronchery     if (psm2ctx == NULL || psm2ctx->mdctx == NULL)
299b077aed3SPierre Pronchery         return 0;
300b077aed3SPierre Pronchery 
301b077aed3SPierre Pronchery     /*
302b077aed3SPierre Pronchery      * If sig is NULL then we're just finding out the sig size. Other fields
303b077aed3SPierre Pronchery      * are ignored. Defer to sm2sig_sign.
304b077aed3SPierre Pronchery      */
305b077aed3SPierre Pronchery     if (sig != NULL) {
306b077aed3SPierre Pronchery         if (!(sm2sig_compute_z_digest(psm2ctx)
307b077aed3SPierre Pronchery               && EVP_DigestFinal_ex(psm2ctx->mdctx, digest, &dlen)))
308b077aed3SPierre Pronchery             return 0;
309b077aed3SPierre Pronchery     }
310b077aed3SPierre Pronchery 
311b077aed3SPierre Pronchery     return sm2sig_sign(vpsm2ctx, sig, siglen, sigsize, digest, (size_t)dlen);
312b077aed3SPierre Pronchery }
313b077aed3SPierre Pronchery 
314b077aed3SPierre Pronchery 
sm2sig_digest_verify_final(void * vpsm2ctx,const unsigned char * sig,size_t siglen)315b077aed3SPierre Pronchery int sm2sig_digest_verify_final(void *vpsm2ctx, const unsigned char *sig,
316b077aed3SPierre Pronchery                                size_t siglen)
317b077aed3SPierre Pronchery {
318b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
319b077aed3SPierre Pronchery     unsigned char digest[EVP_MAX_MD_SIZE];
320b077aed3SPierre Pronchery     unsigned int dlen = 0;
321*e7be843bSPierre Pronchery     int md_size;
322b077aed3SPierre Pronchery 
323*e7be843bSPierre Pronchery     if (psm2ctx == NULL || psm2ctx->mdctx == NULL)
324*e7be843bSPierre Pronchery         return 0;
325*e7be843bSPierre Pronchery 
326*e7be843bSPierre Pronchery     md_size = EVP_MD_get_size(psm2ctx->md);
327*e7be843bSPierre Pronchery     if (md_size <= 0 || md_size > (int)sizeof(digest))
328b077aed3SPierre Pronchery         return 0;
329b077aed3SPierre Pronchery 
330b077aed3SPierre Pronchery     if (!(sm2sig_compute_z_digest(psm2ctx)
331b077aed3SPierre Pronchery           && EVP_DigestFinal_ex(psm2ctx->mdctx, digest, &dlen)))
332b077aed3SPierre Pronchery         return 0;
333b077aed3SPierre Pronchery 
334b077aed3SPierre Pronchery     return sm2sig_verify(vpsm2ctx, sig, siglen, digest, (size_t)dlen);
335b077aed3SPierre Pronchery }
336b077aed3SPierre Pronchery 
sm2sig_freectx(void * vpsm2ctx)337b077aed3SPierre Pronchery static void sm2sig_freectx(void *vpsm2ctx)
338b077aed3SPierre Pronchery {
339b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
340b077aed3SPierre Pronchery 
341b077aed3SPierre Pronchery     free_md(ctx);
342b077aed3SPierre Pronchery     EC_KEY_free(ctx->ec);
343e0c4386eSCy Schubert     OPENSSL_free(ctx->propq);
344b077aed3SPierre Pronchery     OPENSSL_free(ctx->id);
345b077aed3SPierre Pronchery     OPENSSL_free(ctx);
346b077aed3SPierre Pronchery }
347b077aed3SPierre Pronchery 
sm2sig_dupctx(void * vpsm2ctx)348b077aed3SPierre Pronchery static void *sm2sig_dupctx(void *vpsm2ctx)
349b077aed3SPierre Pronchery {
350b077aed3SPierre Pronchery     PROV_SM2_CTX *srcctx = (PROV_SM2_CTX *)vpsm2ctx;
351b077aed3SPierre Pronchery     PROV_SM2_CTX *dstctx;
352b077aed3SPierre Pronchery 
353b077aed3SPierre Pronchery     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
354b077aed3SPierre Pronchery     if (dstctx == NULL)
355b077aed3SPierre Pronchery         return NULL;
356b077aed3SPierre Pronchery 
357b077aed3SPierre Pronchery     *dstctx = *srcctx;
358b077aed3SPierre Pronchery     dstctx->ec = NULL;
359e0c4386eSCy Schubert     dstctx->propq = NULL;
360b077aed3SPierre Pronchery     dstctx->md = NULL;
361b077aed3SPierre Pronchery     dstctx->mdctx = NULL;
362e0c4386eSCy Schubert     dstctx->id = NULL;
363b077aed3SPierre Pronchery 
364b077aed3SPierre Pronchery     if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec))
365b077aed3SPierre Pronchery         goto err;
366b077aed3SPierre Pronchery     dstctx->ec = srcctx->ec;
367b077aed3SPierre Pronchery 
368e0c4386eSCy Schubert     if (srcctx->propq != NULL) {
369e0c4386eSCy Schubert         dstctx->propq = OPENSSL_strdup(srcctx->propq);
370e0c4386eSCy Schubert         if (dstctx->propq == NULL)
371e0c4386eSCy Schubert             goto err;
372e0c4386eSCy Schubert     }
373e0c4386eSCy Schubert 
374b077aed3SPierre Pronchery     if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
375b077aed3SPierre Pronchery         goto err;
376b077aed3SPierre Pronchery     dstctx->md = srcctx->md;
377b077aed3SPierre Pronchery 
378b077aed3SPierre Pronchery     if (srcctx->mdctx != NULL) {
379b077aed3SPierre Pronchery         dstctx->mdctx = EVP_MD_CTX_new();
380b077aed3SPierre Pronchery         if (dstctx->mdctx == NULL
381b077aed3SPierre Pronchery                 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
382b077aed3SPierre Pronchery             goto err;
383b077aed3SPierre Pronchery     }
384b077aed3SPierre Pronchery 
385b077aed3SPierre Pronchery     if (srcctx->id != NULL) {
386b077aed3SPierre Pronchery         dstctx->id = OPENSSL_malloc(srcctx->id_len);
387b077aed3SPierre Pronchery         if (dstctx->id == NULL)
388b077aed3SPierre Pronchery             goto err;
389b077aed3SPierre Pronchery         dstctx->id_len = srcctx->id_len;
390b077aed3SPierre Pronchery         memcpy(dstctx->id, srcctx->id, srcctx->id_len);
391b077aed3SPierre Pronchery     }
392b077aed3SPierre Pronchery 
393b077aed3SPierre Pronchery     return dstctx;
394b077aed3SPierre Pronchery  err:
395b077aed3SPierre Pronchery     sm2sig_freectx(dstctx);
396b077aed3SPierre Pronchery     return NULL;
397b077aed3SPierre Pronchery }
398b077aed3SPierre Pronchery 
sm2sig_get_ctx_params(void * vpsm2ctx,OSSL_PARAM * params)399b077aed3SPierre Pronchery static int sm2sig_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)
400b077aed3SPierre Pronchery {
401b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
402b077aed3SPierre Pronchery     OSSL_PARAM *p;
403b077aed3SPierre Pronchery 
404b077aed3SPierre Pronchery     if (psm2ctx == NULL)
405b077aed3SPierre Pronchery         return 0;
406b077aed3SPierre Pronchery 
407b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
408b077aed3SPierre Pronchery     if (p != NULL
409*e7be843bSPierre Pronchery         && !OSSL_PARAM_set_octet_string(p,
410*e7be843bSPierre Pronchery                                         psm2ctx->aid_len == 0 ? NULL : psm2ctx->aid_buf,
411*e7be843bSPierre Pronchery                                         psm2ctx->aid_len))
412b077aed3SPierre Pronchery         return 0;
413b077aed3SPierre Pronchery 
414b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
415b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, psm2ctx->mdsize))
416b077aed3SPierre Pronchery         return 0;
417b077aed3SPierre Pronchery 
418b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
419b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, psm2ctx->md == NULL
420b077aed3SPierre Pronchery                                                     ? psm2ctx->mdname
421b077aed3SPierre Pronchery                                                     : EVP_MD_get0_name(psm2ctx->md)))
422b077aed3SPierre Pronchery         return 0;
423b077aed3SPierre Pronchery 
424b077aed3SPierre Pronchery     return 1;
425b077aed3SPierre Pronchery }
426b077aed3SPierre Pronchery 
427b077aed3SPierre Pronchery static const OSSL_PARAM known_gettable_ctx_params[] = {
428b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
429b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
430b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
431b077aed3SPierre Pronchery     OSSL_PARAM_END
432b077aed3SPierre Pronchery };
433b077aed3SPierre Pronchery 
sm2sig_gettable_ctx_params(ossl_unused void * vpsm2ctx,ossl_unused void * provctx)434b077aed3SPierre Pronchery static const OSSL_PARAM *sm2sig_gettable_ctx_params(ossl_unused void *vpsm2ctx,
435b077aed3SPierre Pronchery                                                     ossl_unused void *provctx)
436b077aed3SPierre Pronchery {
437b077aed3SPierre Pronchery     return known_gettable_ctx_params;
438b077aed3SPierre Pronchery }
439b077aed3SPierre Pronchery 
sm2sig_set_ctx_params(void * vpsm2ctx,const OSSL_PARAM params[])440b077aed3SPierre Pronchery static int sm2sig_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])
441b077aed3SPierre Pronchery {
442b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
443b077aed3SPierre Pronchery     const OSSL_PARAM *p;
444b077aed3SPierre Pronchery     size_t mdsize;
445b077aed3SPierre Pronchery 
446b077aed3SPierre Pronchery     if (psm2ctx == NULL)
447b077aed3SPierre Pronchery         return 0;
448*e7be843bSPierre Pronchery     if (ossl_param_is_empty(params))
449b077aed3SPierre Pronchery         return 1;
450b077aed3SPierre Pronchery 
451b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DIST_ID);
452b077aed3SPierre Pronchery     if (p != NULL) {
453b077aed3SPierre Pronchery         void *tmp_id = NULL;
454b077aed3SPierre Pronchery         size_t tmp_idlen = 0;
455b077aed3SPierre Pronchery 
456b077aed3SPierre Pronchery         /*
457b077aed3SPierre Pronchery          * If the 'z' digest has already been computed, the ID is set too late
458b077aed3SPierre Pronchery          */
459b077aed3SPierre Pronchery         if (!psm2ctx->flag_compute_z_digest)
460b077aed3SPierre Pronchery             return 0;
461b077aed3SPierre Pronchery 
462b077aed3SPierre Pronchery         if (p->data_size != 0
463b077aed3SPierre Pronchery             && !OSSL_PARAM_get_octet_string(p, &tmp_id, 0, &tmp_idlen))
464b077aed3SPierre Pronchery             return 0;
465b077aed3SPierre Pronchery         OPENSSL_free(psm2ctx->id);
466b077aed3SPierre Pronchery         psm2ctx->id = tmp_id;
467b077aed3SPierre Pronchery         psm2ctx->id_len = tmp_idlen;
468b077aed3SPierre Pronchery     }
469b077aed3SPierre Pronchery 
470b077aed3SPierre Pronchery     /*
471b077aed3SPierre Pronchery      * The following code checks that the size is the same as the SM3 digest
472b077aed3SPierre Pronchery      * size returning an error otherwise.
473b077aed3SPierre Pronchery      * If there is ever any different digest algorithm allowed with SM2
474b077aed3SPierre Pronchery      * this needs to be adjusted accordingly.
475b077aed3SPierre Pronchery      */
476b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
477b077aed3SPierre Pronchery     if (p != NULL && (!OSSL_PARAM_get_size_t(p, &mdsize)
478b077aed3SPierre Pronchery                       || mdsize != psm2ctx->mdsize))
479b077aed3SPierre Pronchery         return 0;
480b077aed3SPierre Pronchery 
481b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
482b077aed3SPierre Pronchery     if (p != NULL) {
483b077aed3SPierre Pronchery         char *mdname = NULL;
484b077aed3SPierre Pronchery 
485b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_utf8_string(p, &mdname, 0))
486b077aed3SPierre Pronchery             return 0;
487b077aed3SPierre Pronchery         if (!sm2sig_set_mdname(psm2ctx, mdname)) {
488b077aed3SPierre Pronchery             OPENSSL_free(mdname);
489b077aed3SPierre Pronchery             return 0;
490b077aed3SPierre Pronchery         }
491b077aed3SPierre Pronchery         OPENSSL_free(mdname);
492b077aed3SPierre Pronchery     }
493b077aed3SPierre Pronchery 
494b077aed3SPierre Pronchery     return 1;
495b077aed3SPierre Pronchery }
496b077aed3SPierre Pronchery 
497b077aed3SPierre Pronchery static const OSSL_PARAM known_settable_ctx_params[] = {
498b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
499b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
500b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DIST_ID, NULL, 0),
501b077aed3SPierre Pronchery     OSSL_PARAM_END
502b077aed3SPierre Pronchery };
503b077aed3SPierre Pronchery 
sm2sig_settable_ctx_params(ossl_unused void * vpsm2ctx,ossl_unused void * provctx)504b077aed3SPierre Pronchery static const OSSL_PARAM *sm2sig_settable_ctx_params(ossl_unused void *vpsm2ctx,
505b077aed3SPierre Pronchery                                                     ossl_unused void *provctx)
506b077aed3SPierre Pronchery {
507b077aed3SPierre Pronchery     return known_settable_ctx_params;
508b077aed3SPierre Pronchery }
509b077aed3SPierre Pronchery 
sm2sig_get_ctx_md_params(void * vpsm2ctx,OSSL_PARAM * params)510b077aed3SPierre Pronchery static int sm2sig_get_ctx_md_params(void *vpsm2ctx, OSSL_PARAM *params)
511b077aed3SPierre Pronchery {
512b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
513b077aed3SPierre Pronchery 
514b077aed3SPierre Pronchery     if (psm2ctx->mdctx == NULL)
515b077aed3SPierre Pronchery         return 0;
516b077aed3SPierre Pronchery 
517b077aed3SPierre Pronchery     return EVP_MD_CTX_get_params(psm2ctx->mdctx, params);
518b077aed3SPierre Pronchery }
519b077aed3SPierre Pronchery 
sm2sig_gettable_ctx_md_params(void * vpsm2ctx)520b077aed3SPierre Pronchery static const OSSL_PARAM *sm2sig_gettable_ctx_md_params(void *vpsm2ctx)
521b077aed3SPierre Pronchery {
522b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
523b077aed3SPierre Pronchery 
524b077aed3SPierre Pronchery     if (psm2ctx->md == NULL)
525b077aed3SPierre Pronchery         return 0;
526b077aed3SPierre Pronchery 
527b077aed3SPierre Pronchery     return EVP_MD_gettable_ctx_params(psm2ctx->md);
528b077aed3SPierre Pronchery }
529b077aed3SPierre Pronchery 
sm2sig_set_ctx_md_params(void * vpsm2ctx,const OSSL_PARAM params[])530b077aed3SPierre Pronchery static int sm2sig_set_ctx_md_params(void *vpsm2ctx, const OSSL_PARAM params[])
531b077aed3SPierre Pronchery {
532b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
533b077aed3SPierre Pronchery 
534b077aed3SPierre Pronchery     if (psm2ctx->mdctx == NULL)
535b077aed3SPierre Pronchery         return 0;
536b077aed3SPierre Pronchery 
537b077aed3SPierre Pronchery     return EVP_MD_CTX_set_params(psm2ctx->mdctx, params);
538b077aed3SPierre Pronchery }
539b077aed3SPierre Pronchery 
sm2sig_settable_ctx_md_params(void * vpsm2ctx)540b077aed3SPierre Pronchery static const OSSL_PARAM *sm2sig_settable_ctx_md_params(void *vpsm2ctx)
541b077aed3SPierre Pronchery {
542b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
543b077aed3SPierre Pronchery 
544b077aed3SPierre Pronchery     if (psm2ctx->md == NULL)
545b077aed3SPierre Pronchery         return 0;
546b077aed3SPierre Pronchery 
547b077aed3SPierre Pronchery     return EVP_MD_settable_ctx_params(psm2ctx->md);
548b077aed3SPierre Pronchery }
549b077aed3SPierre Pronchery 
550b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_sm2_signature_functions[] = {
551b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))sm2sig_newctx },
552b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))sm2sig_signature_init },
553b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))sm2sig_sign },
554b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))sm2sig_signature_init },
555b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))sm2sig_verify },
556b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
557b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_signverify_init },
558b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
559b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_signverify_update },
560b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
561b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_sign_final },
562b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
563b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_signverify_init },
564b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
565b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_signverify_update },
566b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
567b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_verify_final },
568b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))sm2sig_freectx },
569b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))sm2sig_dupctx },
570b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))sm2sig_get_ctx_params },
571b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
572b077aed3SPierre Pronchery       (void (*)(void))sm2sig_gettable_ctx_params },
573b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))sm2sig_set_ctx_params },
574b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
575b077aed3SPierre Pronchery       (void (*)(void))sm2sig_settable_ctx_params },
576b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
577b077aed3SPierre Pronchery       (void (*)(void))sm2sig_get_ctx_md_params },
578b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
579b077aed3SPierre Pronchery       (void (*)(void))sm2sig_gettable_ctx_md_params },
580b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
581b077aed3SPierre Pronchery       (void (*)(void))sm2sig_set_ctx_md_params },
582b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
583b077aed3SPierre Pronchery       (void (*)(void))sm2sig_settable_ctx_md_params },
584*e7be843bSPierre Pronchery     OSSL_DISPATCH_END
585b077aed3SPierre Pronchery };
586