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