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