xref: /freebsd-src/crypto/openssl/providers/implementations/signature/sm2_sig.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1b077aed3SPierre Pronchery /*
2b077aed3SPierre Pronchery  * Copyright 2020-2022 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
12b077aed3SPierre Pronchery  * internal use - SM2 implemetation 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     /*
69b077aed3SPierre Pronchery      * Flag to termine if the 'z' digest needs to be computed and fed to the
70b077aed3SPierre Pronchery      * hash function.
71b077aed3SPierre Pronchery      * This flag should be set on initialization and the compuation 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     unsigned char *aid;
81b077aed3SPierre Pronchery     size_t  aid_len;
82b077aed3SPierre Pronchery 
83b077aed3SPierre Pronchery     /* main digest */
84b077aed3SPierre Pronchery     EVP_MD *md;
85b077aed3SPierre Pronchery     EVP_MD_CTX *mdctx;
86b077aed3SPierre Pronchery     size_t mdsize;
87b077aed3SPierre Pronchery 
88b077aed3SPierre Pronchery     /* SM2 ID used for calculating the Z value */
89b077aed3SPierre Pronchery     unsigned char *id;
90b077aed3SPierre Pronchery     size_t id_len;
91b077aed3SPierre Pronchery } PROV_SM2_CTX;
92b077aed3SPierre Pronchery 
sm2sig_set_mdname(PROV_SM2_CTX * psm2ctx,const char * mdname)93b077aed3SPierre Pronchery static int sm2sig_set_mdname(PROV_SM2_CTX *psm2ctx, const char *mdname)
94b077aed3SPierre Pronchery {
95b077aed3SPierre Pronchery     if (psm2ctx->md == NULL) /* We need an SM3 md to compare with */
96b077aed3SPierre Pronchery         psm2ctx->md = EVP_MD_fetch(psm2ctx->libctx, psm2ctx->mdname,
97b077aed3SPierre Pronchery                                    psm2ctx->propq);
98b077aed3SPierre Pronchery     if (psm2ctx->md == NULL)
99b077aed3SPierre Pronchery         return 0;
100b077aed3SPierre Pronchery 
101b077aed3SPierre Pronchery     if (mdname == NULL)
102b077aed3SPierre Pronchery         return 1;
103b077aed3SPierre Pronchery 
104b077aed3SPierre Pronchery     if (strlen(mdname) >= sizeof(psm2ctx->mdname)
105b077aed3SPierre Pronchery         || !EVP_MD_is_a(psm2ctx->md, mdname)) {
106b077aed3SPierre Pronchery         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, "digest=%s",
107b077aed3SPierre Pronchery                        mdname);
108b077aed3SPierre Pronchery         return 0;
109b077aed3SPierre Pronchery     }
110b077aed3SPierre Pronchery 
111b077aed3SPierre Pronchery     OPENSSL_strlcpy(psm2ctx->mdname, mdname, sizeof(psm2ctx->mdname));
112b077aed3SPierre Pronchery     return 1;
113b077aed3SPierre Pronchery }
114b077aed3SPierre Pronchery 
sm2sig_newctx(void * provctx,const char * propq)115b077aed3SPierre Pronchery static void *sm2sig_newctx(void *provctx, const char *propq)
116b077aed3SPierre Pronchery {
117b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_SM2_CTX));
118b077aed3SPierre Pronchery 
119b077aed3SPierre Pronchery     if (ctx == NULL)
120b077aed3SPierre Pronchery         return NULL;
121b077aed3SPierre Pronchery 
122b077aed3SPierre Pronchery     ctx->libctx = PROV_LIBCTX_OF(provctx);
123b077aed3SPierre Pronchery     if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) {
124b077aed3SPierre Pronchery         OPENSSL_free(ctx);
125b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
126b077aed3SPierre Pronchery         return NULL;
127b077aed3SPierre Pronchery     }
128b077aed3SPierre Pronchery     ctx->mdsize = SM3_DIGEST_LENGTH;
129b077aed3SPierre Pronchery     strcpy(ctx->mdname, OSSL_DIGEST_NAME_SM3);
130b077aed3SPierre Pronchery     return ctx;
131b077aed3SPierre Pronchery }
132b077aed3SPierre Pronchery 
sm2sig_signature_init(void * vpsm2ctx,void * ec,const OSSL_PARAM params[])133b077aed3SPierre Pronchery static int sm2sig_signature_init(void *vpsm2ctx, void *ec,
134b077aed3SPierre Pronchery                                  const OSSL_PARAM params[])
135b077aed3SPierre Pronchery {
136b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
137b077aed3SPierre Pronchery 
138b077aed3SPierre Pronchery     if (!ossl_prov_is_running()
139b077aed3SPierre Pronchery             || psm2ctx == NULL)
140b077aed3SPierre Pronchery         return 0;
141b077aed3SPierre Pronchery 
142b077aed3SPierre Pronchery     if (ec == NULL && psm2ctx->ec == NULL) {
143b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
144b077aed3SPierre Pronchery         return 0;
145b077aed3SPierre Pronchery     }
146b077aed3SPierre Pronchery 
147b077aed3SPierre Pronchery     if (ec != NULL) {
148b077aed3SPierre Pronchery         if (!EC_KEY_up_ref(ec))
149b077aed3SPierre Pronchery             return 0;
150b077aed3SPierre Pronchery         EC_KEY_free(psm2ctx->ec);
151b077aed3SPierre Pronchery         psm2ctx->ec = ec;
152b077aed3SPierre Pronchery     }
153b077aed3SPierre Pronchery 
154b077aed3SPierre Pronchery     return sm2sig_set_ctx_params(psm2ctx, params);
155b077aed3SPierre Pronchery }
156b077aed3SPierre Pronchery 
sm2sig_sign(void * vpsm2ctx,unsigned char * sig,size_t * siglen,size_t sigsize,const unsigned char * tbs,size_t tbslen)157b077aed3SPierre Pronchery static int sm2sig_sign(void *vpsm2ctx, unsigned char *sig, size_t *siglen,
158b077aed3SPierre Pronchery                        size_t sigsize, const unsigned char *tbs, size_t tbslen)
159b077aed3SPierre Pronchery {
160b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
161b077aed3SPierre Pronchery     int ret;
162b077aed3SPierre Pronchery     unsigned int sltmp;
163b077aed3SPierre Pronchery     /* SM2 uses ECDSA_size as well */
164b077aed3SPierre Pronchery     size_t ecsize = ECDSA_size(ctx->ec);
165b077aed3SPierre Pronchery 
166b077aed3SPierre Pronchery     if (sig == NULL) {
167b077aed3SPierre Pronchery         *siglen = ecsize;
168b077aed3SPierre Pronchery         return 1;
169b077aed3SPierre Pronchery     }
170b077aed3SPierre Pronchery 
171b077aed3SPierre Pronchery     if (sigsize < (size_t)ecsize)
172b077aed3SPierre Pronchery         return 0;
173b077aed3SPierre Pronchery 
174b077aed3SPierre Pronchery     if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
175b077aed3SPierre Pronchery         return 0;
176b077aed3SPierre Pronchery 
177b077aed3SPierre Pronchery     ret = ossl_sm2_internal_sign(tbs, tbslen, sig, &sltmp, ctx->ec);
178b077aed3SPierre Pronchery     if (ret <= 0)
179b077aed3SPierre Pronchery         return 0;
180b077aed3SPierre Pronchery 
181b077aed3SPierre Pronchery     *siglen = sltmp;
182b077aed3SPierre Pronchery     return 1;
183b077aed3SPierre Pronchery }
184b077aed3SPierre Pronchery 
sm2sig_verify(void * vpsm2ctx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen)185b077aed3SPierre Pronchery static int sm2sig_verify(void *vpsm2ctx, const unsigned char *sig, size_t siglen,
186b077aed3SPierre Pronchery                          const unsigned char *tbs, size_t tbslen)
187b077aed3SPierre Pronchery {
188b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
189b077aed3SPierre Pronchery 
190b077aed3SPierre Pronchery     if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
191b077aed3SPierre Pronchery         return 0;
192b077aed3SPierre Pronchery 
193b077aed3SPierre Pronchery     return ossl_sm2_internal_verify(tbs, tbslen, sig, siglen, ctx->ec);
194b077aed3SPierre Pronchery }
195b077aed3SPierre Pronchery 
free_md(PROV_SM2_CTX * ctx)196b077aed3SPierre Pronchery static void free_md(PROV_SM2_CTX *ctx)
197b077aed3SPierre Pronchery {
198b077aed3SPierre Pronchery     EVP_MD_CTX_free(ctx->mdctx);
199b077aed3SPierre Pronchery     EVP_MD_free(ctx->md);
200b077aed3SPierre Pronchery     ctx->mdctx = NULL;
201b077aed3SPierre Pronchery     ctx->md = NULL;
202b077aed3SPierre Pronchery }
203b077aed3SPierre Pronchery 
sm2sig_digest_signverify_init(void * vpsm2ctx,const char * mdname,void * ec,const OSSL_PARAM params[])204b077aed3SPierre Pronchery static int sm2sig_digest_signverify_init(void *vpsm2ctx, const char *mdname,
205b077aed3SPierre Pronchery                                          void *ec, const OSSL_PARAM params[])
206b077aed3SPierre Pronchery {
207b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
208b077aed3SPierre Pronchery     int md_nid;
209b077aed3SPierre Pronchery     WPACKET pkt;
210b077aed3SPierre Pronchery     int ret = 0;
211b077aed3SPierre Pronchery 
212b077aed3SPierre Pronchery     if (!sm2sig_signature_init(vpsm2ctx, ec, params)
213b077aed3SPierre Pronchery         || !sm2sig_set_mdname(ctx, mdname))
214b077aed3SPierre Pronchery         return ret;
215b077aed3SPierre Pronchery 
216b077aed3SPierre Pronchery     if (ctx->mdctx == NULL) {
217b077aed3SPierre Pronchery         ctx->mdctx = EVP_MD_CTX_new();
218b077aed3SPierre Pronchery         if (ctx->mdctx == NULL)
219b077aed3SPierre Pronchery             goto error;
220b077aed3SPierre Pronchery     }
221b077aed3SPierre Pronchery 
222b077aed3SPierre Pronchery     md_nid = EVP_MD_get_type(ctx->md);
223b077aed3SPierre Pronchery 
224b077aed3SPierre Pronchery     /*
225b077aed3SPierre Pronchery      * We do not care about DER writing errors.
226b077aed3SPierre Pronchery      * All it really means is that for some reason, there's no
227b077aed3SPierre Pronchery      * AlgorithmIdentifier to be had, but the operation itself is
228b077aed3SPierre Pronchery      * still valid, just as long as it's not used to construct
229b077aed3SPierre Pronchery      * anything that needs an AlgorithmIdentifier.
230b077aed3SPierre Pronchery      */
231b077aed3SPierre Pronchery     ctx->aid_len = 0;
232b077aed3SPierre Pronchery     if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
233b077aed3SPierre Pronchery         && ossl_DER_w_algorithmIdentifier_SM2_with_MD(&pkt, -1, ctx->ec, md_nid)
234b077aed3SPierre Pronchery         && WPACKET_finish(&pkt)) {
235b077aed3SPierre Pronchery         WPACKET_get_total_written(&pkt, &ctx->aid_len);
236b077aed3SPierre Pronchery         ctx->aid = WPACKET_get_curr(&pkt);
237b077aed3SPierre Pronchery     }
238b077aed3SPierre Pronchery     WPACKET_cleanup(&pkt);
239b077aed3SPierre Pronchery 
240b077aed3SPierre Pronchery     if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params))
241b077aed3SPierre Pronchery         goto error;
242b077aed3SPierre Pronchery 
243b077aed3SPierre Pronchery     ctx->flag_compute_z_digest = 1;
244b077aed3SPierre Pronchery 
245b077aed3SPierre Pronchery     ret = 1;
246b077aed3SPierre Pronchery 
247b077aed3SPierre Pronchery  error:
248b077aed3SPierre Pronchery     return ret;
249b077aed3SPierre Pronchery }
250b077aed3SPierre Pronchery 
sm2sig_compute_z_digest(PROV_SM2_CTX * ctx)251b077aed3SPierre Pronchery static int sm2sig_compute_z_digest(PROV_SM2_CTX *ctx)
252b077aed3SPierre Pronchery {
253b077aed3SPierre Pronchery     uint8_t *z = NULL;
254b077aed3SPierre Pronchery     int ret = 1;
255b077aed3SPierre Pronchery 
256b077aed3SPierre Pronchery     if (ctx->flag_compute_z_digest) {
257b077aed3SPierre Pronchery         /* Only do this once */
258b077aed3SPierre Pronchery         ctx->flag_compute_z_digest = 0;
259b077aed3SPierre Pronchery 
260b077aed3SPierre Pronchery         if ((z = OPENSSL_zalloc(ctx->mdsize)) == NULL
261b077aed3SPierre Pronchery             /* get hashed prefix 'z' of tbs message */
262b077aed3SPierre Pronchery             || !ossl_sm2_compute_z_digest(z, ctx->md, ctx->id, ctx->id_len,
263b077aed3SPierre Pronchery                                           ctx->ec)
264b077aed3SPierre Pronchery             || !EVP_DigestUpdate(ctx->mdctx, z, ctx->mdsize))
265b077aed3SPierre Pronchery             ret = 0;
266b077aed3SPierre Pronchery         OPENSSL_free(z);
267b077aed3SPierre Pronchery     }
268b077aed3SPierre Pronchery 
269b077aed3SPierre Pronchery     return ret;
270b077aed3SPierre Pronchery }
271b077aed3SPierre Pronchery 
sm2sig_digest_signverify_update(void * vpsm2ctx,const unsigned char * data,size_t datalen)272b077aed3SPierre Pronchery int sm2sig_digest_signverify_update(void *vpsm2ctx, const unsigned char *data,
273b077aed3SPierre Pronchery                                     size_t datalen)
274b077aed3SPierre Pronchery {
275b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
276b077aed3SPierre Pronchery 
277b077aed3SPierre Pronchery     if (psm2ctx == NULL || psm2ctx->mdctx == NULL)
278b077aed3SPierre Pronchery         return 0;
279b077aed3SPierre Pronchery 
280b077aed3SPierre Pronchery     return sm2sig_compute_z_digest(psm2ctx)
281b077aed3SPierre Pronchery         && EVP_DigestUpdate(psm2ctx->mdctx, data, datalen);
282b077aed3SPierre Pronchery }
283b077aed3SPierre Pronchery 
sm2sig_digest_sign_final(void * vpsm2ctx,unsigned char * sig,size_t * siglen,size_t sigsize)284b077aed3SPierre Pronchery int sm2sig_digest_sign_final(void *vpsm2ctx, unsigned char *sig, size_t *siglen,
285b077aed3SPierre Pronchery                              size_t sigsize)
286b077aed3SPierre Pronchery {
287b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
288b077aed3SPierre Pronchery     unsigned char digest[EVP_MAX_MD_SIZE];
289b077aed3SPierre Pronchery     unsigned int dlen = 0;
290b077aed3SPierre Pronchery 
291b077aed3SPierre Pronchery     if (psm2ctx == NULL || psm2ctx->mdctx == NULL)
292b077aed3SPierre Pronchery         return 0;
293b077aed3SPierre Pronchery 
294b077aed3SPierre Pronchery     /*
295b077aed3SPierre Pronchery      * If sig is NULL then we're just finding out the sig size. Other fields
296b077aed3SPierre Pronchery      * are ignored. Defer to sm2sig_sign.
297b077aed3SPierre Pronchery      */
298b077aed3SPierre Pronchery     if (sig != NULL) {
299b077aed3SPierre Pronchery         if (!(sm2sig_compute_z_digest(psm2ctx)
300b077aed3SPierre Pronchery               && EVP_DigestFinal_ex(psm2ctx->mdctx, digest, &dlen)))
301b077aed3SPierre Pronchery             return 0;
302b077aed3SPierre Pronchery     }
303b077aed3SPierre Pronchery 
304b077aed3SPierre Pronchery     return sm2sig_sign(vpsm2ctx, sig, siglen, sigsize, digest, (size_t)dlen);
305b077aed3SPierre Pronchery }
306b077aed3SPierre Pronchery 
307b077aed3SPierre Pronchery 
sm2sig_digest_verify_final(void * vpsm2ctx,const unsigned char * sig,size_t siglen)308b077aed3SPierre Pronchery int sm2sig_digest_verify_final(void *vpsm2ctx, const unsigned char *sig,
309b077aed3SPierre Pronchery                                size_t siglen)
310b077aed3SPierre Pronchery {
311b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
312b077aed3SPierre Pronchery     unsigned char digest[EVP_MAX_MD_SIZE];
313b077aed3SPierre Pronchery     unsigned int dlen = 0;
314b077aed3SPierre Pronchery 
315b077aed3SPierre Pronchery     if (psm2ctx == NULL
316b077aed3SPierre Pronchery         || psm2ctx->mdctx == NULL
317b077aed3SPierre Pronchery         || EVP_MD_get_size(psm2ctx->md) > (int)sizeof(digest))
318b077aed3SPierre Pronchery         return 0;
319b077aed3SPierre Pronchery 
320b077aed3SPierre Pronchery     if (!(sm2sig_compute_z_digest(psm2ctx)
321b077aed3SPierre Pronchery           && EVP_DigestFinal_ex(psm2ctx->mdctx, digest, &dlen)))
322b077aed3SPierre Pronchery         return 0;
323b077aed3SPierre Pronchery 
324b077aed3SPierre Pronchery     return sm2sig_verify(vpsm2ctx, sig, siglen, digest, (size_t)dlen);
325b077aed3SPierre Pronchery }
326b077aed3SPierre Pronchery 
sm2sig_freectx(void * vpsm2ctx)327b077aed3SPierre Pronchery static void sm2sig_freectx(void *vpsm2ctx)
328b077aed3SPierre Pronchery {
329b077aed3SPierre Pronchery     PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx;
330b077aed3SPierre Pronchery 
331b077aed3SPierre Pronchery     free_md(ctx);
332b077aed3SPierre Pronchery     EC_KEY_free(ctx->ec);
333*e0c4386eSCy Schubert     OPENSSL_free(ctx->propq);
334b077aed3SPierre Pronchery     OPENSSL_free(ctx->id);
335b077aed3SPierre Pronchery     OPENSSL_free(ctx);
336b077aed3SPierre Pronchery }
337b077aed3SPierre Pronchery 
sm2sig_dupctx(void * vpsm2ctx)338b077aed3SPierre Pronchery static void *sm2sig_dupctx(void *vpsm2ctx)
339b077aed3SPierre Pronchery {
340b077aed3SPierre Pronchery     PROV_SM2_CTX *srcctx = (PROV_SM2_CTX *)vpsm2ctx;
341b077aed3SPierre Pronchery     PROV_SM2_CTX *dstctx;
342b077aed3SPierre Pronchery 
343b077aed3SPierre Pronchery     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
344b077aed3SPierre Pronchery     if (dstctx == NULL)
345b077aed3SPierre Pronchery         return NULL;
346b077aed3SPierre Pronchery 
347b077aed3SPierre Pronchery     *dstctx = *srcctx;
348b077aed3SPierre Pronchery     dstctx->ec = NULL;
349*e0c4386eSCy Schubert     dstctx->propq = NULL;
350b077aed3SPierre Pronchery     dstctx->md = NULL;
351b077aed3SPierre Pronchery     dstctx->mdctx = NULL;
352*e0c4386eSCy Schubert     dstctx->id = NULL;
353b077aed3SPierre Pronchery 
354b077aed3SPierre Pronchery     if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec))
355b077aed3SPierre Pronchery         goto err;
356b077aed3SPierre Pronchery     dstctx->ec = srcctx->ec;
357b077aed3SPierre Pronchery 
358*e0c4386eSCy Schubert     if (srcctx->propq != NULL) {
359*e0c4386eSCy Schubert         dstctx->propq = OPENSSL_strdup(srcctx->propq);
360*e0c4386eSCy Schubert         if (dstctx->propq == NULL)
361*e0c4386eSCy Schubert             goto err;
362*e0c4386eSCy Schubert     }
363*e0c4386eSCy Schubert 
364b077aed3SPierre Pronchery     if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
365b077aed3SPierre Pronchery         goto err;
366b077aed3SPierre Pronchery     dstctx->md = srcctx->md;
367b077aed3SPierre Pronchery 
368b077aed3SPierre Pronchery     if (srcctx->mdctx != NULL) {
369b077aed3SPierre Pronchery         dstctx->mdctx = EVP_MD_CTX_new();
370b077aed3SPierre Pronchery         if (dstctx->mdctx == NULL
371b077aed3SPierre Pronchery                 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
372b077aed3SPierre Pronchery             goto err;
373b077aed3SPierre Pronchery     }
374b077aed3SPierre Pronchery 
375b077aed3SPierre Pronchery     if (srcctx->id != NULL) {
376b077aed3SPierre Pronchery         dstctx->id = OPENSSL_malloc(srcctx->id_len);
377b077aed3SPierre Pronchery         if (dstctx->id == NULL)
378b077aed3SPierre Pronchery             goto err;
379b077aed3SPierre Pronchery         dstctx->id_len = srcctx->id_len;
380b077aed3SPierre Pronchery         memcpy(dstctx->id, srcctx->id, srcctx->id_len);
381b077aed3SPierre Pronchery     }
382b077aed3SPierre Pronchery 
383b077aed3SPierre Pronchery     return dstctx;
384b077aed3SPierre Pronchery  err:
385b077aed3SPierre Pronchery     sm2sig_freectx(dstctx);
386b077aed3SPierre Pronchery     return NULL;
387b077aed3SPierre Pronchery }
388b077aed3SPierre Pronchery 
sm2sig_get_ctx_params(void * vpsm2ctx,OSSL_PARAM * params)389b077aed3SPierre Pronchery static int sm2sig_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)
390b077aed3SPierre Pronchery {
391b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
392b077aed3SPierre Pronchery     OSSL_PARAM *p;
393b077aed3SPierre Pronchery 
394b077aed3SPierre Pronchery     if (psm2ctx == NULL)
395b077aed3SPierre Pronchery         return 0;
396b077aed3SPierre Pronchery 
397b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
398b077aed3SPierre Pronchery     if (p != NULL
399b077aed3SPierre Pronchery         && !OSSL_PARAM_set_octet_string(p, psm2ctx->aid, psm2ctx->aid_len))
400b077aed3SPierre Pronchery         return 0;
401b077aed3SPierre Pronchery 
402b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
403b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, psm2ctx->mdsize))
404b077aed3SPierre Pronchery         return 0;
405b077aed3SPierre Pronchery 
406b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
407b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, psm2ctx->md == NULL
408b077aed3SPierre Pronchery                                                     ? psm2ctx->mdname
409b077aed3SPierre Pronchery                                                     : EVP_MD_get0_name(psm2ctx->md)))
410b077aed3SPierre Pronchery         return 0;
411b077aed3SPierre Pronchery 
412b077aed3SPierre Pronchery     return 1;
413b077aed3SPierre Pronchery }
414b077aed3SPierre Pronchery 
415b077aed3SPierre Pronchery static const OSSL_PARAM known_gettable_ctx_params[] = {
416b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
417b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
418b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
419b077aed3SPierre Pronchery     OSSL_PARAM_END
420b077aed3SPierre Pronchery };
421b077aed3SPierre Pronchery 
sm2sig_gettable_ctx_params(ossl_unused void * vpsm2ctx,ossl_unused void * provctx)422b077aed3SPierre Pronchery static const OSSL_PARAM *sm2sig_gettable_ctx_params(ossl_unused void *vpsm2ctx,
423b077aed3SPierre Pronchery                                                     ossl_unused void *provctx)
424b077aed3SPierre Pronchery {
425b077aed3SPierre Pronchery     return known_gettable_ctx_params;
426b077aed3SPierre Pronchery }
427b077aed3SPierre Pronchery 
sm2sig_set_ctx_params(void * vpsm2ctx,const OSSL_PARAM params[])428b077aed3SPierre Pronchery static int sm2sig_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])
429b077aed3SPierre Pronchery {
430b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
431b077aed3SPierre Pronchery     const OSSL_PARAM *p;
432b077aed3SPierre Pronchery     size_t mdsize;
433b077aed3SPierre Pronchery 
434b077aed3SPierre Pronchery     if (psm2ctx == NULL)
435b077aed3SPierre Pronchery         return 0;
436b077aed3SPierre Pronchery     if (params == NULL)
437b077aed3SPierre Pronchery         return 1;
438b077aed3SPierre Pronchery 
439b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DIST_ID);
440b077aed3SPierre Pronchery     if (p != NULL) {
441b077aed3SPierre Pronchery         void *tmp_id = NULL;
442b077aed3SPierre Pronchery         size_t tmp_idlen = 0;
443b077aed3SPierre Pronchery 
444b077aed3SPierre Pronchery         /*
445b077aed3SPierre Pronchery          * If the 'z' digest has already been computed, the ID is set too late
446b077aed3SPierre Pronchery          */
447b077aed3SPierre Pronchery         if (!psm2ctx->flag_compute_z_digest)
448b077aed3SPierre Pronchery             return 0;
449b077aed3SPierre Pronchery 
450b077aed3SPierre Pronchery         if (p->data_size != 0
451b077aed3SPierre Pronchery             && !OSSL_PARAM_get_octet_string(p, &tmp_id, 0, &tmp_idlen))
452b077aed3SPierre Pronchery             return 0;
453b077aed3SPierre Pronchery         OPENSSL_free(psm2ctx->id);
454b077aed3SPierre Pronchery         psm2ctx->id = tmp_id;
455b077aed3SPierre Pronchery         psm2ctx->id_len = tmp_idlen;
456b077aed3SPierre Pronchery     }
457b077aed3SPierre Pronchery 
458b077aed3SPierre Pronchery     /*
459b077aed3SPierre Pronchery      * The following code checks that the size is the same as the SM3 digest
460b077aed3SPierre Pronchery      * size returning an error otherwise.
461b077aed3SPierre Pronchery      * If there is ever any different digest algorithm allowed with SM2
462b077aed3SPierre Pronchery      * this needs to be adjusted accordingly.
463b077aed3SPierre Pronchery      */
464b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
465b077aed3SPierre Pronchery     if (p != NULL && (!OSSL_PARAM_get_size_t(p, &mdsize)
466b077aed3SPierre Pronchery                       || mdsize != psm2ctx->mdsize))
467b077aed3SPierre Pronchery         return 0;
468b077aed3SPierre Pronchery 
469b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
470b077aed3SPierre Pronchery     if (p != NULL) {
471b077aed3SPierre Pronchery         char *mdname = NULL;
472b077aed3SPierre Pronchery 
473b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_utf8_string(p, &mdname, 0))
474b077aed3SPierre Pronchery             return 0;
475b077aed3SPierre Pronchery         if (!sm2sig_set_mdname(psm2ctx, mdname)) {
476b077aed3SPierre Pronchery             OPENSSL_free(mdname);
477b077aed3SPierre Pronchery             return 0;
478b077aed3SPierre Pronchery         }
479b077aed3SPierre Pronchery         OPENSSL_free(mdname);
480b077aed3SPierre Pronchery     }
481b077aed3SPierre Pronchery 
482b077aed3SPierre Pronchery     return 1;
483b077aed3SPierre Pronchery }
484b077aed3SPierre Pronchery 
485b077aed3SPierre Pronchery static const OSSL_PARAM known_settable_ctx_params[] = {
486b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
487b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
488b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DIST_ID, NULL, 0),
489b077aed3SPierre Pronchery     OSSL_PARAM_END
490b077aed3SPierre Pronchery };
491b077aed3SPierre Pronchery 
sm2sig_settable_ctx_params(ossl_unused void * vpsm2ctx,ossl_unused void * provctx)492b077aed3SPierre Pronchery static const OSSL_PARAM *sm2sig_settable_ctx_params(ossl_unused void *vpsm2ctx,
493b077aed3SPierre Pronchery                                                     ossl_unused void *provctx)
494b077aed3SPierre Pronchery {
495b077aed3SPierre Pronchery     return known_settable_ctx_params;
496b077aed3SPierre Pronchery }
497b077aed3SPierre Pronchery 
sm2sig_get_ctx_md_params(void * vpsm2ctx,OSSL_PARAM * params)498b077aed3SPierre Pronchery static int sm2sig_get_ctx_md_params(void *vpsm2ctx, OSSL_PARAM *params)
499b077aed3SPierre Pronchery {
500b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
501b077aed3SPierre Pronchery 
502b077aed3SPierre Pronchery     if (psm2ctx->mdctx == NULL)
503b077aed3SPierre Pronchery         return 0;
504b077aed3SPierre Pronchery 
505b077aed3SPierre Pronchery     return EVP_MD_CTX_get_params(psm2ctx->mdctx, params);
506b077aed3SPierre Pronchery }
507b077aed3SPierre Pronchery 
sm2sig_gettable_ctx_md_params(void * vpsm2ctx)508b077aed3SPierre Pronchery static const OSSL_PARAM *sm2sig_gettable_ctx_md_params(void *vpsm2ctx)
509b077aed3SPierre Pronchery {
510b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
511b077aed3SPierre Pronchery 
512b077aed3SPierre Pronchery     if (psm2ctx->md == NULL)
513b077aed3SPierre Pronchery         return 0;
514b077aed3SPierre Pronchery 
515b077aed3SPierre Pronchery     return EVP_MD_gettable_ctx_params(psm2ctx->md);
516b077aed3SPierre Pronchery }
517b077aed3SPierre Pronchery 
sm2sig_set_ctx_md_params(void * vpsm2ctx,const OSSL_PARAM params[])518b077aed3SPierre Pronchery static int sm2sig_set_ctx_md_params(void *vpsm2ctx, const OSSL_PARAM params[])
519b077aed3SPierre Pronchery {
520b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
521b077aed3SPierre Pronchery 
522b077aed3SPierre Pronchery     if (psm2ctx->mdctx == NULL)
523b077aed3SPierre Pronchery         return 0;
524b077aed3SPierre Pronchery 
525b077aed3SPierre Pronchery     return EVP_MD_CTX_set_params(psm2ctx->mdctx, params);
526b077aed3SPierre Pronchery }
527b077aed3SPierre Pronchery 
sm2sig_settable_ctx_md_params(void * vpsm2ctx)528b077aed3SPierre Pronchery static const OSSL_PARAM *sm2sig_settable_ctx_md_params(void *vpsm2ctx)
529b077aed3SPierre Pronchery {
530b077aed3SPierre Pronchery     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
531b077aed3SPierre Pronchery 
532b077aed3SPierre Pronchery     if (psm2ctx->md == NULL)
533b077aed3SPierre Pronchery         return 0;
534b077aed3SPierre Pronchery 
535b077aed3SPierre Pronchery     return EVP_MD_settable_ctx_params(psm2ctx->md);
536b077aed3SPierre Pronchery }
537b077aed3SPierre Pronchery 
538b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_sm2_signature_functions[] = {
539b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))sm2sig_newctx },
540b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))sm2sig_signature_init },
541b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))sm2sig_sign },
542b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))sm2sig_signature_init },
543b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))sm2sig_verify },
544b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
545b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_signverify_init },
546b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
547b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_signverify_update },
548b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
549b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_sign_final },
550b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
551b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_signverify_init },
552b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
553b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_signverify_update },
554b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
555b077aed3SPierre Pronchery       (void (*)(void))sm2sig_digest_verify_final },
556b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))sm2sig_freectx },
557b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))sm2sig_dupctx },
558b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))sm2sig_get_ctx_params },
559b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
560b077aed3SPierre Pronchery       (void (*)(void))sm2sig_gettable_ctx_params },
561b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))sm2sig_set_ctx_params },
562b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
563b077aed3SPierre Pronchery       (void (*)(void))sm2sig_settable_ctx_params },
564b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
565b077aed3SPierre Pronchery       (void (*)(void))sm2sig_get_ctx_md_params },
566b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
567b077aed3SPierre Pronchery       (void (*)(void))sm2sig_gettable_ctx_md_params },
568b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
569b077aed3SPierre Pronchery       (void (*)(void))sm2sig_set_ctx_md_params },
570b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
571b077aed3SPierre Pronchery       (void (*)(void))sm2sig_settable_ctx_md_params },
572b077aed3SPierre Pronchery     { 0, NULL }
573b077aed3SPierre Pronchery };
574