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