2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 * ECDSA low level APIs are deprecated for public use, but still ok for
12 * internal use - SM2 implemetation uses ECDSA_size() function.
14 #include "internal/deprecated.h"
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 "prov/implementations.h"
29 #include "prov/provider_ctx.h"
30 #include "crypto/ec.h"
31 #include "crypto/sm2.h"
32 #include "prov/der_sm2.h"
34 static OSSL_FUNC_signature_newctx_fn sm2sig_newctx
;
35 static OSSL_FUNC_signature_sign_init_fn sm2sig_signature_init
;
36 static OSSL_FUNC_signature_verify_init_fn sm2sig_signature_init
;
37 static OSSL_FUNC_signature_sign_fn sm2sig_sign
;
38 static OSSL_FUNC_signature_verify_fn sm2sig_verify
;
39 static OSSL_FUNC_signature_digest_sign_init_fn sm2sig_digest_signverify_init
;
40 static OSSL_FUNC_signature_digest_sign_update_fn sm2sig_digest_signverify_update
;
41 static OSSL_FUNC_signature_digest_sign_final_fn sm2sig_digest_sign_final
;
42 static OSSL_FUNC_signature_digest_verify_init_fn sm2sig_digest_signverify_init
;
43 static OSSL_FUNC_signature_digest_verify_update_fn sm2sig_digest_signverify_update
;
44 static OSSL_FUNC_signature_digest_verify_final_fn sm2sig_digest_verify_final
;
45 static OSSL_FUNC_signature_freectx_fn sm2sig_freectx
;
46 static OSSL_FUNC_signature_dupctx_fn sm2sig_dupctx
;
47 static OSSL_FUNC_signature_get_ctx_params_fn sm2sig_get_ctx_params
;
48 static OSSL_FUNC_signature_gettable_ctx_params_fn sm2sig_gettable_ctx_params
;
49 static OSSL_FUNC_signature_set_ctx_params_fn sm2sig_set_ctx_params
;
50 static OSSL_FUNC_signature_settable_ctx_params_fn sm2sig_settable_ctx_params
;
51 static OSSL_FUNC_signature_get_ctx_md_params_fn sm2sig_get_ctx_md_params
;
52 static OSSL_FUNC_signature_gettable_ctx_md_params_fn sm2sig_gettable_ctx_md_params
;
53 static OSSL_FUNC_signature_set_ctx_md_params_fn sm2sig_set_ctx_md_params
;
54 static OSSL_FUNC_signature_settable_ctx_md_params_fn sm2sig_settable_ctx_md_params
;
57 * What's passed as an actual key is defined by the KEYMGMT interface.
58 * We happen to know that our KEYMGMT simply passes EC structures, so
59 * we use that here too.
67 * Flag to determine if the hash function can be changed (1) or not (0)
68 * Because it's dangerous to change during a DigestSign or DigestVerify
69 * operation, this flag is cleared by their Init function, and set again
70 * by their Final function.
72 unsigned int flag_allow_md
: 1;
74 * Flag to termine if the 'z' digest needs to be computed and fed to the
76 * This flag should be set on initialization and the compuation should
77 * be performed only once, on first update.
79 unsigned int flag_compute_z_digest
: 1;
81 char mdname
[OSSL_MAX_NAME_SIZE
];
83 /* The Algorithm Identifier of the combined signature algorithm */
84 unsigned char aid_buf
[OSSL_MAX_ALGORITHM_ID_SIZE
];
93 /* SM2 ID used for calculating the Z value */
98 static void *sm2sig_newctx(void *provctx
, const char *propq
)
100 PROV_SM2_CTX
*ctx
= OPENSSL_zalloc(sizeof(PROV_SM2_CTX
));
105 ctx
->libctx
= PROV_LIBCTX_OF(provctx
);
106 if (propq
!= NULL
&& (ctx
->propq
= OPENSSL_strdup(propq
)) == NULL
) {
109 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
111 /* don't allow to change MD, and in fact there is no such need */
112 ctx
->flag_allow_md
= 0;
116 static int sm2sig_signature_init(void *vpsm2ctx
, void *ec
)
118 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
120 if (psm2ctx
== NULL
|| ec
== NULL
|| !EC_KEY_up_ref(ec
))
122 EC_KEY_free(psm2ctx
->ec
);
127 static int sm2sig_sign(void *vpsm2ctx
, unsigned char *sig
, size_t *siglen
,
128 size_t sigsize
, const unsigned char *tbs
, size_t tbslen
)
130 PROV_SM2_CTX
*ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
133 /* SM2 uses ECDSA_size as well */
134 size_t ecsize
= ECDSA_size(ctx
->ec
);
141 if (sigsize
< (size_t)ecsize
)
144 if (ctx
->mdsize
!= 0 && tbslen
!= ctx
->mdsize
)
147 ret
= sm2_internal_sign(tbs
, tbslen
, sig
, &sltmp
, ctx
->ec
);
155 static int sm2sig_verify(void *vpsm2ctx
, const unsigned char *sig
, size_t siglen
,
156 const unsigned char *tbs
, size_t tbslen
)
158 PROV_SM2_CTX
*ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
160 if (ctx
->mdsize
!= 0 && tbslen
!= ctx
->mdsize
)
163 return sm2_internal_verify(tbs
, tbslen
, sig
, siglen
, ctx
->ec
);
166 static void free_md(PROV_SM2_CTX
*ctx
)
168 EVP_MD_CTX_free(ctx
->mdctx
);
169 EVP_MD_free(ctx
->md
);
175 static int sm2sig_digest_signverify_init(void *vpsm2ctx
, const char *mdname
,
178 PROV_SM2_CTX
*ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
179 int md_nid
= NID_sm3
;
185 if (!sm2sig_signature_init(vpsm2ctx
, ec
))
188 ctx
->md
= EVP_MD_fetch(ctx
->libctx
, mdname
, ctx
->propq
);
189 ctx
->mdsize
= EVP_MD_size(ctx
->md
);
190 ctx
->mdctx
= EVP_MD_CTX_new();
191 if (ctx
->mdctx
== NULL
)
195 * TODO(3.0) Should we care about DER writing errors?
196 * All it really means is that for some reason, there's no
197 * AlgorithmIdentifier to be had, but the operation itself is
198 * still valid, just as long as it's not used to construct
199 * anything that needs an AlgorithmIdentifier.
202 if (WPACKET_init_der(&pkt
, ctx
->aid_buf
, sizeof(ctx
->aid_buf
))
203 && DER_w_algorithmIdentifier_SM2_with_MD(&pkt
, -1, ctx
->ec
, md_nid
)
204 && WPACKET_finish(&pkt
)) {
205 WPACKET_get_total_written(&pkt
, &ctx
->aid_len
);
206 ctx
->aid
= WPACKET_get_curr(&pkt
);
208 WPACKET_cleanup(&pkt
);
210 if (!EVP_DigestInit_ex(ctx
->mdctx
, ctx
->md
, NULL
))
213 ctx
->flag_compute_z_digest
= 1;
223 static int sm2sig_compute_z_digest(PROV_SM2_CTX
*ctx
)
228 if (ctx
->flag_compute_z_digest
) {
229 /* Only do this once */
230 ctx
->flag_compute_z_digest
= 0;
232 if ((z
= OPENSSL_zalloc(ctx
->mdsize
)) == NULL
233 /* get hashed prefix 'z' of tbs message */
234 || !sm2_compute_z_digest(z
, ctx
->md
, ctx
->id
, ctx
->id_len
, ctx
->ec
)
235 || !EVP_DigestUpdate(ctx
->mdctx
, z
, ctx
->mdsize
))
243 int sm2sig_digest_signverify_update(void *vpsm2ctx
, const unsigned char *data
,
246 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
248 if (psm2ctx
== NULL
|| psm2ctx
->mdctx
== NULL
)
251 return sm2sig_compute_z_digest(psm2ctx
)
252 && EVP_DigestUpdate(psm2ctx
->mdctx
, data
, datalen
);
255 int sm2sig_digest_sign_final(void *vpsm2ctx
, unsigned char *sig
, size_t *siglen
,
258 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
259 unsigned char digest
[EVP_MAX_MD_SIZE
];
260 unsigned int dlen
= 0;
262 if (psm2ctx
== NULL
|| psm2ctx
->mdctx
== NULL
)
266 * If sig is NULL then we're just finding out the sig size. Other fields
267 * are ignored. Defer to sm2sig_sign.
270 if (!(sm2sig_compute_z_digest(psm2ctx
)
271 && EVP_DigestFinal_ex(psm2ctx
->mdctx
, digest
, &dlen
)))
275 return sm2sig_sign(vpsm2ctx
, sig
, siglen
, sigsize
, digest
, (size_t)dlen
);
279 int sm2sig_digest_verify_final(void *vpsm2ctx
, const unsigned char *sig
,
282 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
283 unsigned char digest
[EVP_MAX_MD_SIZE
];
284 unsigned int dlen
= 0;
286 if (psm2ctx
== NULL
|| psm2ctx
->mdctx
== NULL
)
289 /* SM2 always use SM3 so it's not possible to exceed the limit */
290 if (!(sm2sig_compute_z_digest(psm2ctx
)
291 && EVP_DigestFinal_ex(psm2ctx
->mdctx
, digest
, &dlen
)))
294 return sm2sig_verify(vpsm2ctx
, sig
, siglen
, digest
, (size_t)dlen
);
297 static void sm2sig_freectx(void *vpsm2ctx
)
299 PROV_SM2_CTX
*ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
302 EC_KEY_free(ctx
->ec
);
303 OPENSSL_free(ctx
->id
);
307 static void *sm2sig_dupctx(void *vpsm2ctx
)
309 PROV_SM2_CTX
*srcctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
310 PROV_SM2_CTX
*dstctx
;
312 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
319 dstctx
->mdctx
= NULL
;
321 if (srcctx
->ec
!= NULL
&& !EC_KEY_up_ref(srcctx
->ec
))
323 dstctx
->ec
= srcctx
->ec
;
325 if (srcctx
->md
!= NULL
&& !EVP_MD_up_ref(srcctx
->md
))
327 dstctx
->md
= srcctx
->md
;
329 if (srcctx
->mdctx
!= NULL
) {
330 dstctx
->mdctx
= EVP_MD_CTX_new();
331 if (dstctx
->mdctx
== NULL
332 || !EVP_MD_CTX_copy_ex(dstctx
->mdctx
, srcctx
->mdctx
))
336 if (srcctx
->id
!= NULL
) {
337 dstctx
->id
= OPENSSL_malloc(srcctx
->id_len
);
338 if (dstctx
->id
== NULL
)
340 dstctx
->id_len
= srcctx
->id_len
;
341 memcpy(dstctx
->id
, srcctx
->id
, srcctx
->id_len
);
346 sm2sig_freectx(dstctx
);
350 static int sm2sig_get_ctx_params(void *vpsm2ctx
, OSSL_PARAM
*params
)
352 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
355 if (psm2ctx
== NULL
|| params
== NULL
)
358 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_ALGORITHM_ID
);
360 && !OSSL_PARAM_set_octet_string(p
, psm2ctx
->aid
, psm2ctx
->aid_len
))
363 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST_SIZE
);
364 if (p
!= NULL
&& !OSSL_PARAM_set_size_t(p
, psm2ctx
->mdsize
))
367 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
368 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, psm2ctx
->md
== NULL
370 : EVP_MD_name(psm2ctx
->md
)))
376 static const OSSL_PARAM known_gettable_ctx_params
[] = {
377 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
, NULL
, 0),
378 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE
, NULL
),
379 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
383 static const OSSL_PARAM
*sm2sig_gettable_ctx_params(ossl_unused
void *provctx
)
385 return known_gettable_ctx_params
;
388 static int sm2sig_set_ctx_params(void *vpsm2ctx
, const OSSL_PARAM params
[])
390 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
394 if (psm2ctx
== NULL
|| params
== NULL
)
397 p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_DIST_ID
);
403 * If the 'z' digest has already been computed, the ID is set too late
405 if (!psm2ctx
->flag_compute_z_digest
)
408 if (!OSSL_PARAM_get_octet_string(p
, &tmp_id
, 0, &tmp_idlen
))
410 OPENSSL_free(psm2ctx
->id
);
411 psm2ctx
->id
= tmp_id
;
412 psm2ctx
->id_len
= tmp_idlen
;
415 if (psm2ctx
->md
!= NULL
) {
417 * You cannot set the digest name/size when doing a DigestSign or
423 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST_SIZE
);
424 if (p
!= NULL
&& !OSSL_PARAM_get_size_t(p
, &psm2ctx
->mdsize
))
428 * We never actually use the mdname, but we do support getting it later.
429 * This can be useful for applications that want to know the MD that they
432 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
433 mdname
= psm2ctx
->mdname
;
435 && !OSSL_PARAM_get_utf8_string(p
, &mdname
, sizeof(psm2ctx
->mdname
)))
441 static const OSSL_PARAM known_settable_ctx_params
[] = {
442 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE
, NULL
),
443 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
444 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DIST_ID
, NULL
, 0),
448 static const OSSL_PARAM
*sm2sig_settable_ctx_params(ossl_unused
void *provctx
)
451 * TODO(3.0): Should this function return a different set of settable ctx
452 * params if the ctx is being used for a DigestSign/DigestVerify? In that
453 * case it is not allowed to set the digest size/digest name because the
454 * digest is explicitly set as part of the init.
456 return known_settable_ctx_params
;
459 static int sm2sig_get_ctx_md_params(void *vpsm2ctx
, OSSL_PARAM
*params
)
461 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
463 if (psm2ctx
->mdctx
== NULL
)
466 return EVP_MD_CTX_get_params(psm2ctx
->mdctx
, params
);
469 static const OSSL_PARAM
*sm2sig_gettable_ctx_md_params(void *vpsm2ctx
)
471 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
473 if (psm2ctx
->md
== NULL
)
476 return EVP_MD_gettable_ctx_params(psm2ctx
->md
);
479 static int sm2sig_set_ctx_md_params(void *vpsm2ctx
, const OSSL_PARAM params
[])
481 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
483 if (psm2ctx
->mdctx
== NULL
)
486 return EVP_MD_CTX_set_params(psm2ctx
->mdctx
, params
);
489 static const OSSL_PARAM
*sm2sig_settable_ctx_md_params(void *vpsm2ctx
)
491 PROV_SM2_CTX
*psm2ctx
= (PROV_SM2_CTX
*)vpsm2ctx
;
493 if (psm2ctx
->md
== NULL
)
496 return EVP_MD_settable_ctx_params(psm2ctx
->md
);
499 const OSSL_DISPATCH ossl_sm2_signature_functions
[] = {
500 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))sm2sig_newctx
},
501 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))sm2sig_signature_init
},
502 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))sm2sig_sign
},
503 { OSSL_FUNC_SIGNATURE_VERIFY_INIT
, (void (*)(void))sm2sig_signature_init
},
504 { OSSL_FUNC_SIGNATURE_VERIFY
, (void (*)(void))sm2sig_verify
},
505 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
506 (void (*)(void))sm2sig_digest_signverify_init
},
507 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE
,
508 (void (*)(void))sm2sig_digest_signverify_update
},
509 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL
,
510 (void (*)(void))sm2sig_digest_sign_final
},
511 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
512 (void (*)(void))sm2sig_digest_signverify_init
},
513 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE
,
514 (void (*)(void))sm2sig_digest_signverify_update
},
515 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL
,
516 (void (*)(void))sm2sig_digest_verify_final
},
517 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))sm2sig_freectx
},
518 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))sm2sig_dupctx
},
519 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS
, (void (*)(void))sm2sig_get_ctx_params
},
520 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS
,
521 (void (*)(void))sm2sig_gettable_ctx_params
},
522 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS
, (void (*)(void))sm2sig_set_ctx_params
},
523 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS
,
524 (void (*)(void))sm2sig_settable_ctx_params
},
525 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS
,
526 (void (*)(void))sm2sig_get_ctx_md_params
},
527 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS
,
528 (void (*)(void))sm2sig_gettable_ctx_md_params
},
529 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS
,
530 (void (*)(void))sm2sig_set_ctx_md_params
},
531 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS
,
532 (void (*)(void))sm2sig_settable_ctx_md_params
},