prov: prefix all OSSL_DISPATCH tables names with ossl_
[openssl.git] / providers / implementations / signature / ecdsa.c
1 /*
2  * Copyright 2020 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.
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 "internal/nelem.h"
25 #include "internal/sizes.h"
26 #include "internal/cryptlib.h"
27 #include "prov/providercommon.h"
28 #include "prov/providercommonerr.h"
29 #include "prov/implementations.h"
30 #include "prov/provider_ctx.h"
31 #include "prov/securitycheck.h"
32 #include "crypto/ec.h"
33 #include "prov/der_ec.h"
34
35 static OSSL_FUNC_signature_newctx_fn ecdsa_newctx;
36 static OSSL_FUNC_signature_sign_init_fn ecdsa_sign_init;
37 static OSSL_FUNC_signature_verify_init_fn ecdsa_verify_init;
38 static OSSL_FUNC_signature_sign_fn ecdsa_sign;
39 static OSSL_FUNC_signature_verify_fn ecdsa_verify;
40 static OSSL_FUNC_signature_digest_sign_init_fn ecdsa_digest_sign_init;
41 static OSSL_FUNC_signature_digest_sign_update_fn ecdsa_digest_signverify_update;
42 static OSSL_FUNC_signature_digest_sign_final_fn ecdsa_digest_sign_final;
43 static OSSL_FUNC_signature_digest_verify_init_fn ecdsa_digest_verify_init;
44 static OSSL_FUNC_signature_digest_verify_update_fn ecdsa_digest_signverify_update;
45 static OSSL_FUNC_signature_digest_verify_final_fn ecdsa_digest_verify_final;
46 static OSSL_FUNC_signature_freectx_fn ecdsa_freectx;
47 static OSSL_FUNC_signature_dupctx_fn ecdsa_dupctx;
48 static OSSL_FUNC_signature_get_ctx_params_fn ecdsa_get_ctx_params;
49 static OSSL_FUNC_signature_gettable_ctx_params_fn ecdsa_gettable_ctx_params;
50 static OSSL_FUNC_signature_set_ctx_params_fn ecdsa_set_ctx_params;
51 static OSSL_FUNC_signature_settable_ctx_params_fn ecdsa_settable_ctx_params;
52 static OSSL_FUNC_signature_get_ctx_md_params_fn ecdsa_get_ctx_md_params;
53 static OSSL_FUNC_signature_gettable_ctx_md_params_fn ecdsa_gettable_ctx_md_params;
54 static OSSL_FUNC_signature_set_ctx_md_params_fn ecdsa_set_ctx_md_params;
55 static OSSL_FUNC_signature_settable_ctx_md_params_fn ecdsa_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 DSA structures, so
60  * we use that here too.
61  */
62
63 typedef struct {
64     OPENSSL_CTX *libctx;
65     char *propq;
66     EC_KEY *ec;
67     char mdname[OSSL_MAX_NAME_SIZE];
68
69     /* The Algorithm Identifier of the combined signature algorithm */
70     unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
71     unsigned char *aid;
72     size_t  aid_len;
73     size_t mdsize;
74     int operation;
75
76     EVP_MD *md;
77     EVP_MD_CTX *mdctx;
78     /*
79      * Internally used to cache the results of calling the EC group
80      * sign_setup() methods which are then passed to the sign operation.
81      * This is used by CAVS failure tests to terminate a loop if the signature
82      * is not valid.
83      * This could of also been done with a simple flag.
84      */
85     BIGNUM *kinv;
86     BIGNUM *r;
87 #if !defined(OPENSSL_NO_ACVP_TESTS)
88     /*
89      * This indicates that KAT (CAVS) test is running. Externally an app will
90      * override the random callback such that the generated private key and k
91      * are known.
92      * Normal operation will loop to choose a new k if the signature is not
93      * valid - but for this mode of operation it forces a failure instead.
94      */
95     unsigned int kattest;
96 #endif
97 } PROV_ECDSA_CTX;
98
99 static void *ecdsa_newctx(void *provctx, const char *propq)
100 {
101     PROV_ECDSA_CTX *ctx;
102
103     if (!ossl_prov_is_running())
104         return NULL;
105
106     ctx = OPENSSL_zalloc(sizeof(PROV_ECDSA_CTX));
107     if (ctx == NULL)
108         return NULL;
109
110     ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
111     if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) {
112         OPENSSL_free(ctx);
113         ctx = NULL;
114         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
115     }
116     return ctx;
117 }
118
119 static int ecdsa_signverify_init(void *vctx, void *ec, int operation)
120 {
121     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
122
123     if (!ossl_prov_is_running()
124             || ctx == NULL
125             || ec == NULL
126             || !EC_KEY_up_ref(ec))
127         return 0;
128     EC_KEY_free(ctx->ec);
129     ctx->ec = ec;
130     ctx->operation = operation;
131     return ec_check_key(ec, operation == EVP_PKEY_OP_SIGN);
132 }
133
134 static int ecdsa_sign_init(void *vctx, void *ec)
135 {
136     return ecdsa_signverify_init(vctx, ec, EVP_PKEY_OP_SIGN);
137 }
138
139 static int ecdsa_verify_init(void *vctx, void *ec)
140 {
141     return ecdsa_signverify_init(vctx, ec, EVP_PKEY_OP_VERIFY);
142 }
143
144 static int ecdsa_sign(void *vctx, unsigned char *sig, size_t *siglen,
145                       size_t sigsize, const unsigned char *tbs, size_t tbslen)
146 {
147     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
148     int ret;
149     unsigned int sltmp;
150     size_t ecsize = ECDSA_size(ctx->ec);
151
152     if (!ossl_prov_is_running())
153         return 0;
154
155     if (sig == NULL) {
156         *siglen = ecsize;
157         return 1;
158     }
159
160 #if !defined(OPENSSL_NO_ACVP_TESTS)
161     if (ctx->kattest && !ECDSA_sign_setup(ctx->ec, NULL, &ctx->kinv, &ctx->r))
162         return 0;
163 #endif
164
165     if (sigsize < (size_t)ecsize)
166         return 0;
167
168     if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
169         return 0;
170
171     ret = ECDSA_sign_ex(0, tbs, tbslen, sig, &sltmp, ctx->kinv, ctx->r, ctx->ec);
172     if (ret <= 0)
173         return 0;
174
175     *siglen = sltmp;
176     return 1;
177 }
178
179 static int ecdsa_verify(void *vctx, const unsigned char *sig, size_t siglen,
180                         const unsigned char *tbs, size_t tbslen)
181 {
182     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
183
184     if (!ossl_prov_is_running() || (ctx->mdsize != 0 && tbslen != ctx->mdsize))
185         return 0;
186
187     return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->ec);
188 }
189
190 static void free_md(PROV_ECDSA_CTX *ctx)
191 {
192     OPENSSL_free(ctx->propq);
193     EVP_MD_CTX_free(ctx->mdctx);
194     EVP_MD_free(ctx->md);
195     ctx->propq = NULL;
196     ctx->mdctx = NULL;
197     ctx->md = NULL;
198     ctx->mdsize = 0;
199 }
200
201 static int ecdsa_digest_signverify_init(void *vctx, const char *mdname,
202                                         void *ec, int operation)
203 {
204     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
205     int md_nid = NID_undef;
206     WPACKET pkt;
207     int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
208
209     if (!ossl_prov_is_running())
210         return 0;
211
212     free_md(ctx);
213
214     if (!ecdsa_signverify_init(vctx, ec, operation))
215         return 0;
216
217     ctx->md = EVP_MD_fetch(ctx->libctx, mdname, ctx->propq);
218     md_nid = digest_get_approved_nid_with_sha1(ctx->md, sha1_allowed);
219     if (md_nid == NID_undef)
220         goto error;
221
222     ctx->mdsize = EVP_MD_size(ctx->md);
223     ctx->mdctx = EVP_MD_CTX_new();
224     if (ctx->mdctx == NULL)
225         goto error;
226
227     /*
228      * TODO(3.0) Should we care about DER writing errors?
229      * All it really means is that for some reason, there's no
230      * AlgorithmIdentifier to be had, but the operation itself is
231      * still valid, just as long as it's not used to construct
232      * anything that needs an AlgorithmIdentifier.
233      */
234     ctx->aid_len = 0;
235     if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
236         && DER_w_algorithmIdentifier_ECDSA_with_MD(&pkt, -1, ctx->ec, md_nid)
237         && WPACKET_finish(&pkt)) {
238         WPACKET_get_total_written(&pkt, &ctx->aid_len);
239         ctx->aid = WPACKET_get_curr(&pkt);
240     }
241     WPACKET_cleanup(&pkt);
242
243     if (!EVP_DigestInit_ex(ctx->mdctx, ctx->md, NULL))
244         goto error;
245     return 1;
246 error:
247     free_md(ctx);
248     return 0;
249 }
250
251 static int ecdsa_digest_sign_init(void *vctx, const char *mdname, void *ec)
252 {
253     return ecdsa_digest_signverify_init(vctx, mdname, ec, EVP_PKEY_OP_SIGN);
254 }
255
256 static int ecdsa_digest_verify_init(void *vctx, const char *mdname, void *ec)
257 {
258     return ecdsa_digest_signverify_init(vctx, mdname, ec, EVP_PKEY_OP_VERIFY);
259 }
260
261 int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data,
262                                    size_t datalen)
263 {
264     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
265
266     if (ctx == NULL || ctx->mdctx == NULL)
267         return 0;
268
269     return EVP_DigestUpdate(ctx->mdctx, data, datalen);
270 }
271
272 int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen,
273                             size_t sigsize)
274 {
275     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
276     unsigned char digest[EVP_MAX_MD_SIZE];
277     unsigned int dlen = 0;
278
279     if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL)
280         return 0;
281
282     /*
283      * If sig is NULL then we're just finding out the sig size. Other fields
284      * are ignored. Defer to ecdsa_sign.
285      */
286     if (sig != NULL) {
287         /*
288          * TODO(3.0): There is the possibility that some externally provided
289          * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
290          * but that problem is much larger than just in DSA.
291          */
292         if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
293             return 0;
294     }
295
296     return ecdsa_sign(vctx, sig, siglen, sigsize, digest, (size_t)dlen);
297 }
298
299 int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig,
300                               size_t siglen)
301 {
302     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
303     unsigned char digest[EVP_MAX_MD_SIZE];
304     unsigned int dlen = 0;
305
306     if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL)
307         return 0;
308
309     /*
310      * TODO(3.0): There is the possibility that some externally provided
311      * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
312      * but that problem is much larger than just in DSA.
313      */
314     if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
315         return 0;
316
317     return ecdsa_verify(ctx, sig, siglen, digest, (size_t)dlen);
318 }
319
320 static void ecdsa_freectx(void *vctx)
321 {
322     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
323
324     free_md(ctx);
325     EC_KEY_free(ctx->ec);
326     BN_clear_free(ctx->kinv);
327     BN_clear_free(ctx->r);
328     OPENSSL_free(ctx);
329 }
330
331 static void *ecdsa_dupctx(void *vctx)
332 {
333     PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx;
334     PROV_ECDSA_CTX *dstctx;
335
336     if (!ossl_prov_is_running())
337         return NULL;
338
339     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
340     if (dstctx == NULL)
341         return NULL;
342
343     *dstctx = *srcctx;
344     dstctx->ec = NULL;
345     dstctx->md = NULL;
346     dstctx->mdctx = NULL;
347
348     if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec))
349         goto err;
350     /* Test KATS should not need to be supported */
351     if (srcctx->kinv != NULL || srcctx->r != NULL)
352         goto err;
353     dstctx->ec = srcctx->ec;
354
355     if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
356         goto err;
357     dstctx->md = srcctx->md;
358
359     if (srcctx->mdctx != NULL) {
360         dstctx->mdctx = EVP_MD_CTX_new();
361         if (dstctx->mdctx == NULL
362                 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
363             goto err;
364     }
365
366     return dstctx;
367  err:
368     ecdsa_freectx(dstctx);
369     return NULL;
370 }
371
372 static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params)
373 {
374     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
375     OSSL_PARAM *p;
376
377     if (ctx == NULL || params == NULL)
378         return 0;
379
380     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
381     if (p != NULL && !OSSL_PARAM_set_octet_string(p, ctx->aid, ctx->aid_len))
382         return 0;
383
384     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
385     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->mdsize))
386         return 0;
387
388     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
389     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ctx->md == NULL
390                                                     ? ctx->mdname
391                                                     : EVP_MD_name(ctx->md)))
392         return 0;
393
394     return 1;
395 }
396
397 static const OSSL_PARAM known_gettable_ctx_params[] = {
398     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
399     OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
400     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
401     OSSL_PARAM_END
402 };
403
404 static const OSSL_PARAM *ecdsa_gettable_ctx_params(ossl_unused void *provctx)
405 {
406     return known_gettable_ctx_params;
407 }
408
409 static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[])
410 {
411     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
412     const OSSL_PARAM *p;
413     char *mdname;
414
415     if (ctx == NULL || params == NULL)
416         return 0;
417
418     if (ctx->md != NULL) {
419         /*
420          * You cannot set the digest name/size when doing a DigestSign or
421          * DigestVerify.
422          */
423         return 1;
424     }
425 #if !defined(OPENSSL_NO_ACVP_TESTS)
426     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_KAT);
427     if (p != NULL && !OSSL_PARAM_get_uint(p, &ctx->kattest))
428         return 0;
429 #endif
430
431     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
432     if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->mdsize))
433         return 0;
434
435     /*
436      * We never actually use the mdname, but we do support getting it later.
437      * This can be useful for applications that want to know the MD that they
438      * previously set.
439      */
440     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
441     mdname = ctx->mdname;
442     if (p != NULL
443             && !OSSL_PARAM_get_utf8_string(p, &mdname, sizeof(ctx->mdname)))
444         return 0;
445
446     return 1;
447 }
448
449 static const OSSL_PARAM known_settable_ctx_params[] = {
450     OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
451     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
452     OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL),
453     OSSL_PARAM_END
454 };
455
456 static const OSSL_PARAM *ecdsa_settable_ctx_params(ossl_unused void *provctx)
457 {
458     /*
459      * TODO(3.0): Should this function return a different set of settable ctx
460      * params if the ctx is being used for a DigestSign/DigestVerify? In that
461      * case it is not allowed to set the digest size/digest name because the
462      * digest is explicitly set as part of the init.
463      */
464     return known_settable_ctx_params;
465 }
466
467 static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params)
468 {
469     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
470
471     if (ctx->mdctx == NULL)
472         return 0;
473
474     return EVP_MD_CTX_get_params(ctx->mdctx, params);
475 }
476
477 static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx)
478 {
479     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
480
481     if (ctx->md == NULL)
482         return 0;
483
484     return EVP_MD_gettable_ctx_params(ctx->md);
485 }
486
487 static int ecdsa_set_ctx_md_params(void *vctx, const OSSL_PARAM params[])
488 {
489     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
490
491     if (ctx->mdctx == NULL)
492         return 0;
493
494     return EVP_MD_CTX_set_params(ctx->mdctx, params);
495 }
496
497 static const OSSL_PARAM *ecdsa_settable_ctx_md_params(void *vctx)
498 {
499     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
500
501     if (ctx->md == NULL)
502         return 0;
503
504     return EVP_MD_settable_ctx_params(ctx->md);
505 }
506
507 const OSSL_DISPATCH ecossl_dsa_signature_functions[] = {
508     { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx },
509     { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_sign_init },
510     { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign },
511     { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_verify_init },
512     { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify },
513     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
514       (void (*)(void))ecdsa_digest_sign_init },
515     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
516       (void (*)(void))ecdsa_digest_signverify_update },
517     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
518       (void (*)(void))ecdsa_digest_sign_final },
519     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
520       (void (*)(void))ecdsa_digest_verify_init },
521     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
522       (void (*)(void))ecdsa_digest_signverify_update },
523     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
524       (void (*)(void))ecdsa_digest_verify_final },
525     { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx },
526     { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx },
527     { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params },
528     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
529       (void (*)(void))ecdsa_gettable_ctx_params },
530     { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params },
531     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
532       (void (*)(void))ecdsa_settable_ctx_params },
533     { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
534       (void (*)(void))ecdsa_get_ctx_md_params },
535     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
536       (void (*)(void))ecdsa_gettable_ctx_md_params },
537     { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
538       (void (*)(void))ecdsa_set_ctx_md_params },
539     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
540       (void (*)(void))ecdsa_settable_ctx_md_params },
541     { 0, NULL }
542 };