/src/openssl/providers/implementations/signature/sm2_sig.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-2024 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 | | /* |
12 | | * ECDSA low level APIs are deprecated for public use, but still ok for |
13 | | * internal use - SM2 implementation uses ECDSA_size() function. |
14 | | */ |
15 | | #include "internal/deprecated.h" |
16 | | |
17 | | #include <string.h> /* memcpy */ |
18 | | #include <openssl/crypto.h> |
19 | | #include <openssl/core_dispatch.h> |
20 | | #include <openssl/core_names.h> |
21 | | #include <openssl/dsa.h> |
22 | | #include <openssl/params.h> |
23 | | #include <openssl/evp.h> |
24 | | #include <openssl/err.h> |
25 | | #include <openssl/proverr.h> |
26 | | #include "internal/nelem.h" |
27 | | #include "internal/sizes.h" |
28 | | #include "internal/cryptlib.h" |
29 | | #include "internal/sm3.h" |
30 | | #include "prov/implementations.h" |
31 | | #include "prov/providercommon.h" |
32 | | #include "prov/provider_ctx.h" |
33 | | #include "crypto/ec.h" |
34 | | #include "crypto/sm2.h" |
35 | | #include "prov/der_sm2.h" |
36 | | |
37 | | static OSSL_FUNC_signature_newctx_fn sm2sig_newctx; |
38 | | static OSSL_FUNC_signature_sign_init_fn sm2sig_signature_init; |
39 | | static OSSL_FUNC_signature_verify_init_fn sm2sig_signature_init; |
40 | | static OSSL_FUNC_signature_sign_fn sm2sig_sign; |
41 | | static OSSL_FUNC_signature_verify_fn sm2sig_verify; |
42 | | static OSSL_FUNC_signature_digest_sign_init_fn sm2sig_digest_signverify_init; |
43 | | static OSSL_FUNC_signature_digest_sign_update_fn sm2sig_digest_signverify_update; |
44 | | static OSSL_FUNC_signature_digest_sign_final_fn sm2sig_digest_sign_final; |
45 | | static OSSL_FUNC_signature_digest_verify_init_fn sm2sig_digest_signverify_init; |
46 | | static OSSL_FUNC_signature_digest_verify_update_fn sm2sig_digest_signverify_update; |
47 | | static OSSL_FUNC_signature_digest_verify_final_fn sm2sig_digest_verify_final; |
48 | | static OSSL_FUNC_signature_freectx_fn sm2sig_freectx; |
49 | | static OSSL_FUNC_signature_dupctx_fn sm2sig_dupctx; |
50 | | static OSSL_FUNC_signature_get_ctx_params_fn sm2sig_get_ctx_params; |
51 | | static OSSL_FUNC_signature_gettable_ctx_params_fn sm2sig_gettable_ctx_params; |
52 | | static OSSL_FUNC_signature_set_ctx_params_fn sm2sig_set_ctx_params; |
53 | | static OSSL_FUNC_signature_settable_ctx_params_fn sm2sig_settable_ctx_params; |
54 | | static OSSL_FUNC_signature_get_ctx_md_params_fn sm2sig_get_ctx_md_params; |
55 | | static OSSL_FUNC_signature_gettable_ctx_md_params_fn sm2sig_gettable_ctx_md_params; |
56 | | static OSSL_FUNC_signature_set_ctx_md_params_fn sm2sig_set_ctx_md_params; |
57 | | static OSSL_FUNC_signature_settable_ctx_md_params_fn sm2sig_settable_ctx_md_params; |
58 | | |
59 | | /* |
60 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
61 | | * We happen to know that our KEYMGMT simply passes EC structures, so |
62 | | * we use that here too. |
63 | | */ |
64 | | typedef struct { |
65 | | OSSL_LIB_CTX *libctx; |
66 | | char *propq; |
67 | | EC_KEY *ec; |
68 | | |
69 | | /* |
70 | | * Flag to determine if the 'z' digest needs to be computed and fed to the |
71 | | * hash function. |
72 | | * This flag should be set on initialization and the computation should |
73 | | * be performed only once, on first update. |
74 | | */ |
75 | | unsigned int flag_compute_z_digest : 1; |
76 | | |
77 | | char mdname[OSSL_MAX_NAME_SIZE]; |
78 | | |
79 | | /* The Algorithm Identifier of the combined signature algorithm */ |
80 | | unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE]; |
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 | | /* XOF digests don't work */ |
102 | 0 | if (EVP_MD_xof(psm2ctx->md)) { |
103 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
104 | 0 | return 0; |
105 | 0 | } |
106 | | |
107 | 0 | if (mdname == NULL) |
108 | 0 | return 1; |
109 | | |
110 | 0 | if (strlen(mdname) >= sizeof(psm2ctx->mdname) |
111 | 0 | || !EVP_MD_is_a(psm2ctx->md, mdname)) { |
112 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, "digest=%s", |
113 | 0 | mdname); |
114 | 0 | return 0; |
115 | 0 | } |
116 | | |
117 | 0 | OPENSSL_strlcpy(psm2ctx->mdname, mdname, sizeof(psm2ctx->mdname)); |
118 | 0 | return 1; |
119 | 0 | } |
120 | | |
121 | | static void *sm2sig_newctx(void *provctx, const char *propq) |
122 | 0 | { |
123 | 0 | PROV_SM2_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_SM2_CTX)); |
124 | |
|
125 | 0 | if (ctx == NULL) |
126 | 0 | return NULL; |
127 | | |
128 | 0 | ctx->libctx = PROV_LIBCTX_OF(provctx); |
129 | 0 | if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) { |
130 | 0 | OPENSSL_free(ctx); |
131 | 0 | return NULL; |
132 | 0 | } |
133 | 0 | ctx->mdsize = SM3_DIGEST_LENGTH; |
134 | 0 | strcpy(ctx->mdname, OSSL_DIGEST_NAME_SM3); |
135 | 0 | return ctx; |
136 | 0 | } |
137 | | |
138 | | static int sm2sig_signature_init(void *vpsm2ctx, void *ec, |
139 | | const OSSL_PARAM params[]) |
140 | 0 | { |
141 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
142 | |
|
143 | 0 | if (!ossl_prov_is_running() |
144 | 0 | || psm2ctx == NULL) |
145 | 0 | return 0; |
146 | | |
147 | 0 | if (ec == NULL && psm2ctx->ec == NULL) { |
148 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
149 | 0 | return 0; |
150 | 0 | } |
151 | | |
152 | 0 | if (ec != NULL) { |
153 | 0 | if (!EC_KEY_up_ref(ec)) |
154 | 0 | return 0; |
155 | 0 | EC_KEY_free(psm2ctx->ec); |
156 | 0 | psm2ctx->ec = ec; |
157 | 0 | } |
158 | | |
159 | 0 | return sm2sig_set_ctx_params(psm2ctx, params); |
160 | 0 | } |
161 | | |
162 | | static int sm2sig_sign(void *vpsm2ctx, unsigned char *sig, size_t *siglen, |
163 | | size_t sigsize, const unsigned char *tbs, size_t tbslen) |
164 | 0 | { |
165 | 0 | PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx; |
166 | 0 | int ret; |
167 | 0 | unsigned int sltmp; |
168 | | /* SM2 uses ECDSA_size as well */ |
169 | 0 | size_t ecsize = ECDSA_size(ctx->ec); |
170 | |
|
171 | 0 | if (sig == NULL) { |
172 | 0 | *siglen = ecsize; |
173 | 0 | return 1; |
174 | 0 | } |
175 | | |
176 | 0 | if (sigsize < (size_t)ecsize) |
177 | 0 | return 0; |
178 | | |
179 | 0 | if (ctx->mdsize != 0 && tbslen != ctx->mdsize) |
180 | 0 | return 0; |
181 | | |
182 | 0 | ret = ossl_sm2_internal_sign(tbs, (int)tbslen, sig, &sltmp, ctx->ec); |
183 | 0 | if (ret <= 0) |
184 | 0 | return 0; |
185 | | |
186 | 0 | *siglen = sltmp; |
187 | 0 | return 1; |
188 | 0 | } |
189 | | |
190 | | static int sm2sig_verify(void *vpsm2ctx, const unsigned char *sig, size_t siglen, |
191 | | const unsigned char *tbs, size_t tbslen) |
192 | 0 | { |
193 | 0 | PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx; |
194 | |
|
195 | 0 | if (ctx->mdsize != 0 && tbslen != ctx->mdsize) |
196 | 0 | return 0; |
197 | | |
198 | 0 | return ossl_sm2_internal_verify(tbs, (int)tbslen, sig, (int)siglen, ctx->ec); |
199 | 0 | } |
200 | | |
201 | | static void free_md(PROV_SM2_CTX *ctx) |
202 | 0 | { |
203 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
204 | 0 | EVP_MD_free(ctx->md); |
205 | 0 | ctx->mdctx = NULL; |
206 | 0 | ctx->md = NULL; |
207 | 0 | } |
208 | | |
209 | | static int sm2sig_digest_signverify_init(void *vpsm2ctx, const char *mdname, |
210 | | void *ec, const OSSL_PARAM params[]) |
211 | 0 | { |
212 | 0 | PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx; |
213 | 0 | int md_nid; |
214 | 0 | WPACKET pkt; |
215 | 0 | int ret = 0; |
216 | 0 | unsigned char *aid = NULL; |
217 | |
|
218 | 0 | if (!sm2sig_signature_init(vpsm2ctx, ec, params) |
219 | 0 | || !sm2sig_set_mdname(ctx, mdname)) |
220 | 0 | return ret; |
221 | | |
222 | 0 | if (ctx->mdctx == NULL) { |
223 | 0 | ctx->mdctx = EVP_MD_CTX_new(); |
224 | 0 | if (ctx->mdctx == NULL) |
225 | 0 | goto error; |
226 | 0 | } |
227 | | |
228 | 0 | md_nid = EVP_MD_get_type(ctx->md); |
229 | | |
230 | | /* |
231 | | * We do not care about DER writing errors. |
232 | | * All it really means is that for some reason, there's no |
233 | | * AlgorithmIdentifier to be had, but the operation itself is |
234 | | * still valid, just as long as it's not used to construct |
235 | | * anything that needs an AlgorithmIdentifier. |
236 | | */ |
237 | 0 | ctx->aid_len = 0; |
238 | 0 | if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf)) |
239 | 0 | && ossl_DER_w_algorithmIdentifier_SM2_with_MD(&pkt, -1, ctx->ec, md_nid) |
240 | 0 | && WPACKET_finish(&pkt)) { |
241 | 0 | WPACKET_get_total_written(&pkt, &ctx->aid_len); |
242 | 0 | aid = WPACKET_get_curr(&pkt); |
243 | 0 | } |
244 | 0 | WPACKET_cleanup(&pkt); |
245 | 0 | if (aid != NULL && ctx->aid_len != 0) |
246 | 0 | memmove(ctx->aid_buf, aid, ctx->aid_len); |
247 | |
|
248 | 0 | if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params)) |
249 | 0 | goto error; |
250 | | |
251 | 0 | ctx->flag_compute_z_digest = 1; |
252 | |
|
253 | 0 | ret = 1; |
254 | |
|
255 | 0 | error: |
256 | 0 | return ret; |
257 | 0 | } |
258 | | |
259 | | static int sm2sig_compute_z_digest(PROV_SM2_CTX *ctx) |
260 | 0 | { |
261 | 0 | uint8_t *z = NULL; |
262 | 0 | int ret = 1; |
263 | |
|
264 | 0 | if (ctx->flag_compute_z_digest) { |
265 | | /* Only do this once */ |
266 | 0 | ctx->flag_compute_z_digest = 0; |
267 | |
|
268 | 0 | if ((z = OPENSSL_zalloc(ctx->mdsize)) == NULL |
269 | | /* get hashed prefix 'z' of tbs message */ |
270 | 0 | || !ossl_sm2_compute_z_digest(z, ctx->md, ctx->id, ctx->id_len, |
271 | 0 | ctx->ec) |
272 | 0 | || !EVP_DigestUpdate(ctx->mdctx, z, ctx->mdsize)) |
273 | 0 | ret = 0; |
274 | 0 | OPENSSL_free(z); |
275 | 0 | } |
276 | |
|
277 | 0 | return ret; |
278 | 0 | } |
279 | | |
280 | | int sm2sig_digest_signverify_update(void *vpsm2ctx, const unsigned char *data, |
281 | | size_t datalen) |
282 | 0 | { |
283 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
284 | |
|
285 | 0 | if (psm2ctx == NULL || psm2ctx->mdctx == NULL) |
286 | 0 | return 0; |
287 | | |
288 | 0 | return sm2sig_compute_z_digest(psm2ctx) |
289 | 0 | && EVP_DigestUpdate(psm2ctx->mdctx, data, datalen); |
290 | 0 | } |
291 | | |
292 | | int sm2sig_digest_sign_final(void *vpsm2ctx, unsigned char *sig, size_t *siglen, |
293 | | size_t sigsize) |
294 | 0 | { |
295 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
296 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
297 | 0 | unsigned int dlen = 0; |
298 | |
|
299 | 0 | if (psm2ctx == NULL || psm2ctx->mdctx == NULL) |
300 | 0 | return 0; |
301 | | |
302 | | /* |
303 | | * If sig is NULL then we're just finding out the sig size. Other fields |
304 | | * are ignored. Defer to sm2sig_sign. |
305 | | */ |
306 | 0 | if (sig != NULL) { |
307 | 0 | if (!(sm2sig_compute_z_digest(psm2ctx) |
308 | 0 | && EVP_DigestFinal_ex(psm2ctx->mdctx, digest, &dlen))) |
309 | 0 | return 0; |
310 | 0 | } |
311 | | |
312 | 0 | return sm2sig_sign(vpsm2ctx, sig, siglen, sigsize, digest, (size_t)dlen); |
313 | 0 | } |
314 | | |
315 | | |
316 | | int sm2sig_digest_verify_final(void *vpsm2ctx, const unsigned char *sig, |
317 | | size_t siglen) |
318 | 0 | { |
319 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
320 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
321 | 0 | unsigned int dlen = 0; |
322 | 0 | int md_size; |
323 | |
|
324 | 0 | if (psm2ctx == NULL || psm2ctx->mdctx == NULL) |
325 | 0 | return 0; |
326 | | |
327 | 0 | md_size = EVP_MD_get_size(psm2ctx->md); |
328 | 0 | if (md_size <= 0 || md_size > (int)sizeof(digest)) |
329 | 0 | return 0; |
330 | | |
331 | 0 | if (!(sm2sig_compute_z_digest(psm2ctx) |
332 | 0 | && EVP_DigestFinal_ex(psm2ctx->mdctx, digest, &dlen))) |
333 | 0 | return 0; |
334 | | |
335 | 0 | return sm2sig_verify(vpsm2ctx, sig, siglen, digest, (size_t)dlen); |
336 | 0 | } |
337 | | |
338 | | static void sm2sig_freectx(void *vpsm2ctx) |
339 | 0 | { |
340 | 0 | PROV_SM2_CTX *ctx = (PROV_SM2_CTX *)vpsm2ctx; |
341 | |
|
342 | 0 | free_md(ctx); |
343 | 0 | EC_KEY_free(ctx->ec); |
344 | 0 | OPENSSL_free(ctx->propq); |
345 | 0 | OPENSSL_free(ctx->id); |
346 | 0 | OPENSSL_free(ctx); |
347 | 0 | } |
348 | | |
349 | | static void *sm2sig_dupctx(void *vpsm2ctx) |
350 | 0 | { |
351 | 0 | PROV_SM2_CTX *srcctx = (PROV_SM2_CTX *)vpsm2ctx; |
352 | 0 | PROV_SM2_CTX *dstctx; |
353 | |
|
354 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
355 | 0 | if (dstctx == NULL) |
356 | 0 | return NULL; |
357 | | |
358 | 0 | *dstctx = *srcctx; |
359 | 0 | dstctx->ec = NULL; |
360 | 0 | dstctx->propq = NULL; |
361 | 0 | dstctx->md = NULL; |
362 | 0 | dstctx->mdctx = NULL; |
363 | 0 | dstctx->id = NULL; |
364 | |
|
365 | 0 | if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec)) |
366 | 0 | goto err; |
367 | 0 | dstctx->ec = srcctx->ec; |
368 | |
|
369 | 0 | if (srcctx->propq != NULL) { |
370 | 0 | dstctx->propq = OPENSSL_strdup(srcctx->propq); |
371 | 0 | if (dstctx->propq == NULL) |
372 | 0 | goto err; |
373 | 0 | } |
374 | | |
375 | 0 | if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) |
376 | 0 | goto err; |
377 | 0 | dstctx->md = srcctx->md; |
378 | |
|
379 | 0 | if (srcctx->mdctx != NULL) { |
380 | 0 | dstctx->mdctx = EVP_MD_CTX_new(); |
381 | 0 | if (dstctx->mdctx == NULL |
382 | 0 | || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) |
383 | 0 | goto err; |
384 | 0 | } |
385 | | |
386 | 0 | if (srcctx->id != NULL) { |
387 | 0 | dstctx->id = OPENSSL_malloc(srcctx->id_len); |
388 | 0 | if (dstctx->id == NULL) |
389 | 0 | goto err; |
390 | 0 | dstctx->id_len = srcctx->id_len; |
391 | 0 | memcpy(dstctx->id, srcctx->id, srcctx->id_len); |
392 | 0 | } |
393 | | |
394 | 0 | return dstctx; |
395 | 0 | err: |
396 | 0 | sm2sig_freectx(dstctx); |
397 | 0 | return NULL; |
398 | 0 | } |
399 | | |
400 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
401 | | #ifndef sm2sig_get_ctx_params_list |
402 | | static const OSSL_PARAM sm2sig_get_ctx_params_list[] = { |
403 | | OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0), |
404 | | OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), |
405 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
406 | | OSSL_PARAM_END |
407 | | }; |
408 | | #endif |
409 | | |
410 | | #ifndef sm2sig_get_ctx_params_st |
411 | | struct sm2sig_get_ctx_params_st { |
412 | | OSSL_PARAM *algid; |
413 | | OSSL_PARAM *digest; |
414 | | OSSL_PARAM *size; |
415 | | }; |
416 | | #endif |
417 | | |
418 | | #ifndef sm2sig_get_ctx_params_decoder |
419 | | static int sm2sig_get_ctx_params_decoder |
420 | | (const OSSL_PARAM *p, struct sm2sig_get_ctx_params_st *r) |
421 | 0 | { |
422 | 0 | const char *s; |
423 | |
|
424 | 0 | memset(r, 0, sizeof(*r)); |
425 | 0 | if (p != NULL) |
426 | 0 | for (; (s = p->key) != NULL; p++) |
427 | 0 | switch(s[0]) { |
428 | 0 | default: |
429 | 0 | break; |
430 | 0 | case 'a': |
431 | 0 | if (ossl_likely(strcmp("lgorithm-id", s + 1) == 0)) { |
432 | | /* SIGNATURE_PARAM_ALGORITHM_ID */ |
433 | 0 | if (ossl_unlikely(r->algid != NULL)) { |
434 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
435 | 0 | "param %s is repeated", s); |
436 | 0 | return 0; |
437 | 0 | } |
438 | 0 | r->algid = (OSSL_PARAM *)p; |
439 | 0 | } |
440 | 0 | break; |
441 | 0 | case 'd': |
442 | 0 | switch(s[1]) { |
443 | 0 | default: |
444 | 0 | break; |
445 | 0 | case 'i': |
446 | 0 | switch(s[2]) { |
447 | 0 | default: |
448 | 0 | break; |
449 | 0 | case 'g': |
450 | 0 | switch(s[3]) { |
451 | 0 | default: |
452 | 0 | break; |
453 | 0 | case 'e': |
454 | 0 | switch(s[4]) { |
455 | 0 | default: |
456 | 0 | break; |
457 | 0 | case 's': |
458 | 0 | switch(s[5]) { |
459 | 0 | default: |
460 | 0 | break; |
461 | 0 | case 't': |
462 | 0 | switch(s[6]) { |
463 | 0 | default: |
464 | 0 | break; |
465 | 0 | case '-': |
466 | 0 | if (ossl_likely(strcmp("size", s + 7) == 0)) { |
467 | | /* SIGNATURE_PARAM_DIGEST_SIZE */ |
468 | 0 | if (ossl_unlikely(r->size != NULL)) { |
469 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
470 | 0 | "param %s is repeated", s); |
471 | 0 | return 0; |
472 | 0 | } |
473 | 0 | r->size = (OSSL_PARAM *)p; |
474 | 0 | } |
475 | 0 | break; |
476 | 0 | case '\0': |
477 | 0 | if (ossl_unlikely(r->digest != NULL)) { |
478 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
479 | 0 | "param %s is repeated", s); |
480 | 0 | return 0; |
481 | 0 | } |
482 | 0 | r->digest = (OSSL_PARAM *)p; |
483 | 0 | } |
484 | 0 | } |
485 | 0 | } |
486 | 0 | } |
487 | 0 | } |
488 | 0 | } |
489 | 0 | } |
490 | 0 | return 1; |
491 | 0 | } |
492 | | #endif |
493 | | /* End of machine generated */ |
494 | | |
495 | | static int sm2sig_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params) |
496 | 0 | { |
497 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
498 | 0 | struct sm2sig_get_ctx_params_st p; |
499 | |
|
500 | 0 | if (psm2ctx == NULL || !sm2sig_get_ctx_params_decoder(params, &p)) |
501 | 0 | return 0; |
502 | | |
503 | 0 | if (p.algid != NULL |
504 | 0 | && !OSSL_PARAM_set_octet_string(p.algid, |
505 | 0 | psm2ctx->aid_len == 0 ? NULL : psm2ctx->aid_buf, |
506 | 0 | psm2ctx->aid_len)) |
507 | 0 | return 0; |
508 | | |
509 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, psm2ctx->mdsize)) |
510 | 0 | return 0; |
511 | | |
512 | 0 | if (p.digest != NULL |
513 | 0 | && !OSSL_PARAM_set_utf8_string(p.digest, psm2ctx->md == NULL |
514 | 0 | ? psm2ctx->mdname |
515 | 0 | : EVP_MD_get0_name(psm2ctx->md))) |
516 | 0 | return 0; |
517 | | |
518 | 0 | return 1; |
519 | 0 | } |
520 | | |
521 | | static const OSSL_PARAM *sm2sig_gettable_ctx_params(ossl_unused void *vpsm2ctx, |
522 | | ossl_unused void *provctx) |
523 | 0 | { |
524 | 0 | return sm2sig_get_ctx_params_list; |
525 | 0 | } |
526 | | |
527 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
528 | | #ifndef sm2sig_set_ctx_params_list |
529 | | static const OSSL_PARAM sm2sig_set_ctx_params_list[] = { |
530 | | OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), |
531 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
532 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DIST_ID, NULL, 0), |
533 | | OSSL_PARAM_END |
534 | | }; |
535 | | #endif |
536 | | |
537 | | #ifndef sm2sig_set_ctx_params_st |
538 | | struct sm2sig_set_ctx_params_st { |
539 | | OSSL_PARAM *digest; |
540 | | OSSL_PARAM *distid; |
541 | | OSSL_PARAM *size; |
542 | | }; |
543 | | #endif |
544 | | |
545 | | #ifndef sm2sig_set_ctx_params_decoder |
546 | | static int sm2sig_set_ctx_params_decoder |
547 | | (const OSSL_PARAM *p, struct sm2sig_set_ctx_params_st *r) |
548 | 0 | { |
549 | 0 | const char *s; |
550 | |
|
551 | 0 | memset(r, 0, sizeof(*r)); |
552 | 0 | if (p != NULL) |
553 | 0 | for (; (s = p->key) != NULL; p++) |
554 | 0 | switch(s[0]) { |
555 | 0 | default: |
556 | 0 | break; |
557 | 0 | case 'd': |
558 | 0 | switch(s[1]) { |
559 | 0 | default: |
560 | 0 | break; |
561 | 0 | case 'i': |
562 | 0 | switch(s[2]) { |
563 | 0 | default: |
564 | 0 | break; |
565 | 0 | case 'g': |
566 | 0 | switch(s[3]) { |
567 | 0 | default: |
568 | 0 | break; |
569 | 0 | case 'e': |
570 | 0 | switch(s[4]) { |
571 | 0 | default: |
572 | 0 | break; |
573 | 0 | case 's': |
574 | 0 | switch(s[5]) { |
575 | 0 | default: |
576 | 0 | break; |
577 | 0 | case 't': |
578 | 0 | switch(s[6]) { |
579 | 0 | default: |
580 | 0 | break; |
581 | 0 | case '-': |
582 | 0 | if (ossl_likely(strcmp("size", s + 7) == 0)) { |
583 | | /* SIGNATURE_PARAM_DIGEST_SIZE */ |
584 | 0 | if (ossl_unlikely(r->size != NULL)) { |
585 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
586 | 0 | "param %s is repeated", s); |
587 | 0 | return 0; |
588 | 0 | } |
589 | 0 | r->size = (OSSL_PARAM *)p; |
590 | 0 | } |
591 | 0 | break; |
592 | 0 | case '\0': |
593 | 0 | if (ossl_unlikely(r->digest != NULL)) { |
594 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
595 | 0 | "param %s is repeated", s); |
596 | 0 | return 0; |
597 | 0 | } |
598 | 0 | r->digest = (OSSL_PARAM *)p; |
599 | 0 | } |
600 | 0 | } |
601 | 0 | } |
602 | 0 | } |
603 | 0 | break; |
604 | 0 | case 's': |
605 | 0 | if (ossl_likely(strcmp("tid", s + 3) == 0)) { |
606 | | /* PKEY_PARAM_DIST_ID */ |
607 | 0 | if (ossl_unlikely(r->distid != NULL)) { |
608 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
609 | 0 | "param %s is repeated", s); |
610 | 0 | return 0; |
611 | 0 | } |
612 | 0 | r->distid = (OSSL_PARAM *)p; |
613 | 0 | } |
614 | 0 | } |
615 | 0 | } |
616 | 0 | } |
617 | 0 | return 1; |
618 | 0 | } |
619 | | #endif |
620 | | /* End of machine generated */ |
621 | | |
622 | | static int sm2sig_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[]) |
623 | 0 | { |
624 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
625 | 0 | struct sm2sig_set_ctx_params_st p; |
626 | 0 | size_t mdsize; |
627 | |
|
628 | 0 | if (psm2ctx == NULL || !sm2sig_set_ctx_params_decoder(params, &p)) |
629 | 0 | return 0; |
630 | | |
631 | 0 | if (p.distid != NULL) { |
632 | 0 | void *tmp_id = NULL; |
633 | 0 | size_t tmp_idlen = 0; |
634 | | |
635 | | /* |
636 | | * If the 'z' digest has already been computed, the ID is set too late |
637 | | */ |
638 | 0 | if (!psm2ctx->flag_compute_z_digest) |
639 | 0 | return 0; |
640 | | |
641 | 0 | if (p.distid->data_size != 0 |
642 | 0 | && !OSSL_PARAM_get_octet_string(p.distid, &tmp_id, 0, &tmp_idlen)) |
643 | 0 | return 0; |
644 | 0 | OPENSSL_free(psm2ctx->id); |
645 | 0 | psm2ctx->id = tmp_id; |
646 | 0 | psm2ctx->id_len = tmp_idlen; |
647 | 0 | } |
648 | | |
649 | | /* |
650 | | * The following code checks that the size is the same as the SM3 digest |
651 | | * size returning an error otherwise. |
652 | | * If there is ever any different digest algorithm allowed with SM2 |
653 | | * this needs to be adjusted accordingly. |
654 | | */ |
655 | 0 | if (p.size != NULL && (!OSSL_PARAM_get_size_t(p.size, &mdsize) |
656 | 0 | || mdsize != psm2ctx->mdsize)) |
657 | 0 | return 0; |
658 | | |
659 | 0 | if (p.digest != NULL) { |
660 | 0 | char *mdname = NULL; |
661 | |
|
662 | 0 | if (!OSSL_PARAM_get_utf8_string(p.digest, &mdname, 0)) |
663 | 0 | return 0; |
664 | 0 | if (!sm2sig_set_mdname(psm2ctx, mdname)) { |
665 | 0 | OPENSSL_free(mdname); |
666 | 0 | return 0; |
667 | 0 | } |
668 | 0 | OPENSSL_free(mdname); |
669 | 0 | } |
670 | | |
671 | 0 | return 1; |
672 | 0 | } |
673 | | |
674 | | static const OSSL_PARAM *sm2sig_settable_ctx_params(ossl_unused void *vpsm2ctx, |
675 | | ossl_unused void *provctx) |
676 | 0 | { |
677 | 0 | return sm2sig_set_ctx_params_list; |
678 | 0 | } |
679 | | |
680 | | static int sm2sig_get_ctx_md_params(void *vpsm2ctx, OSSL_PARAM *params) |
681 | 0 | { |
682 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
683 | |
|
684 | 0 | if (psm2ctx->mdctx == NULL) |
685 | 0 | return 0; |
686 | | |
687 | 0 | return EVP_MD_CTX_get_params(psm2ctx->mdctx, params); |
688 | 0 | } |
689 | | |
690 | | static const OSSL_PARAM *sm2sig_gettable_ctx_md_params(void *vpsm2ctx) |
691 | 0 | { |
692 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
693 | |
|
694 | 0 | if (psm2ctx->md == NULL) |
695 | 0 | return 0; |
696 | | |
697 | 0 | return EVP_MD_gettable_ctx_params(psm2ctx->md); |
698 | 0 | } |
699 | | |
700 | | static int sm2sig_set_ctx_md_params(void *vpsm2ctx, const OSSL_PARAM params[]) |
701 | 0 | { |
702 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
703 | |
|
704 | 0 | if (psm2ctx->mdctx == NULL) |
705 | 0 | return 0; |
706 | | |
707 | 0 | return EVP_MD_CTX_set_params(psm2ctx->mdctx, params); |
708 | 0 | } |
709 | | |
710 | | static const OSSL_PARAM *sm2sig_settable_ctx_md_params(void *vpsm2ctx) |
711 | 0 | { |
712 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
713 | |
|
714 | 0 | if (psm2ctx->md == NULL) |
715 | 0 | return 0; |
716 | | |
717 | 0 | return EVP_MD_settable_ctx_params(psm2ctx->md); |
718 | 0 | } |
719 | | |
720 | | const OSSL_DISPATCH ossl_sm2_signature_functions[] = { |
721 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))sm2sig_newctx }, |
722 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))sm2sig_signature_init }, |
723 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))sm2sig_sign }, |
724 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))sm2sig_signature_init }, |
725 | | { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))sm2sig_verify }, |
726 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, |
727 | | (void (*)(void))sm2sig_digest_signverify_init }, |
728 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, |
729 | | (void (*)(void))sm2sig_digest_signverify_update }, |
730 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, |
731 | | (void (*)(void))sm2sig_digest_sign_final }, |
732 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, |
733 | | (void (*)(void))sm2sig_digest_signverify_init }, |
734 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, |
735 | | (void (*)(void))sm2sig_digest_signverify_update }, |
736 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, |
737 | | (void (*)(void))sm2sig_digest_verify_final }, |
738 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))sm2sig_freectx }, |
739 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))sm2sig_dupctx }, |
740 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))sm2sig_get_ctx_params }, |
741 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, |
742 | | (void (*)(void))sm2sig_gettable_ctx_params }, |
743 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))sm2sig_set_ctx_params }, |
744 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, |
745 | | (void (*)(void))sm2sig_settable_ctx_params }, |
746 | | { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, |
747 | | (void (*)(void))sm2sig_get_ctx_md_params }, |
748 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, |
749 | | (void (*)(void))sm2sig_gettable_ctx_md_params }, |
750 | | { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, |
751 | | (void (*)(void))sm2sig_set_ctx_md_params }, |
752 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, |
753 | | (void (*)(void))sm2sig_settable_ctx_md_params }, |
754 | | OSSL_DISPATCH_END |
755 | | }; |