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