rand: set up EVP and DRBG infrastructure for RAND from providers.
[openssl.git] / crypto / evp / pkey_mac.c
1 /*
2  * Copyright 2018-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 #include <string.h>
11 #include <openssl/err.h>
12 #include <openssl/evp.h>
13 #include <openssl/engine.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include "crypto/evp.h"
17 #include "evp_local.h"
18
19 /* MAC PKEY context structure */
20
21 typedef struct {
22     EVP_MAC_CTX *ctx;
23
24     /*
25      * We know of two MAC types:
26      *
27      * 1. those who take a secret in raw form, i.e. raw data as a
28      *    ASN1_OCTET_STRING embedded in a EVP_PKEY.  So far, that's
29      *    all of them but CMAC.
30      * 2. those who take a secret with associated cipher in very generic
31      *    form, i.e. a complete EVP_MAC_CTX embedded in a PKEY.  So far,
32      *    only CMAC does this.
33      *
34      * (one might wonder why the second form isn't used for all)
35      */
36 #define MAC_TYPE_RAW    1   /* HMAC like MAC type (all but CMAC so far) */
37 #define MAC_TYPE_MAC    2   /* CMAC like MAC type (only CMAC known so far) */
38     int type;
39
40     /* The following is only used for MAC_TYPE_RAW implementations */
41     struct {
42         const EVP_MD *md;           /* temp storage of MD */
43         ASN1_OCTET_STRING ktmp;     /* temp storage for key */
44     } raw_data;
45 } MAC_PKEY_CTX;
46
47 static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx);
48
49 static int pkey_mac_init(EVP_PKEY_CTX *ctx)
50 {
51     MAC_PKEY_CTX *hctx;
52     /* We're being smart and using the same base NIDs for PKEY and for MAC */
53     int nid = ctx->pmeth->pkey_id;
54     EVP_MAC *mac;
55
56     ERR_set_mark();
57     mac = EVP_MAC_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propquery);
58     ERR_pop_to_mark();
59
60     /*
61      * mac == NULL may actually be ok in some situations. In an
62      * EVP_PKEY_new_mac_key() call a temporary EVP_PKEY_CTX is created with
63      * default libctx. We don't actually need the underlying MAC to be present
64      * to successfully set the key in that case. The resulting EVP_PKEY could
65      * then be used in some other libctx where the MAC *is* present
66      */
67
68     if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) {
69         EVPerr(EVP_F_PKEY_MAC_INIT, ERR_R_MALLOC_FAILURE);
70         return 0;
71     }
72
73     if (mac != NULL) {
74         hctx->ctx = EVP_MAC_new_ctx(mac);
75         if (hctx->ctx == NULL) {
76             OPENSSL_free(hctx);
77             return 0;
78         }
79     }
80
81     if (nid == EVP_PKEY_CMAC) {
82         hctx->type = MAC_TYPE_MAC;
83     } else {
84         hctx->type = MAC_TYPE_RAW;
85         hctx->raw_data.ktmp.type = V_ASN1_OCTET_STRING;
86     }
87
88     pkey_mac_cleanup(ctx);
89     EVP_PKEY_CTX_set_data(ctx, hctx);
90     ctx->keygen_info_count = 0;
91
92     return 1;
93 }
94
95 static int pkey_mac_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
96 {
97     MAC_PKEY_CTX *sctx, *dctx;
98
99     sctx = EVP_PKEY_CTX_get_data(src);
100
101     if (sctx->ctx == NULL) {
102         /* This actually means the fetch failed during the init call */
103         EVPerr(0, EVP_R_FETCH_FAILED);
104         return 0;
105     }
106
107     if (sctx->ctx->data == NULL)
108         return 0;
109
110     dctx = OPENSSL_zalloc(sizeof(*dctx));
111     if (dctx == NULL) {
112         EVPerr(EVP_F_PKEY_MAC_COPY, ERR_R_MALLOC_FAILURE);
113         return 0;
114     }
115
116     EVP_PKEY_CTX_set_data(dst, dctx);
117     dst->keygen_info_count = 0;
118
119     dctx->ctx = EVP_MAC_dup_ctx(sctx->ctx);
120     if (dctx->ctx == NULL)
121         goto err;
122
123     /*
124      * Normally, nothing special would be done with the MAC method.  In
125      * this particular case, though, the MAC method was fetched internally
126      * by pkey_mac_init() above or by EVP_PKEY_new_CMAC_key() and passed
127      * via the EVP_MAC_CTX, so it is effectively like every new EVP_MAC_CTX
128      * fetches the MAC method anew in this case.  Therefore, its reference
129      * count must be adjusted here.
130      */
131     if (!EVP_MAC_up_ref(EVP_MAC_get_ctx_mac(dctx->ctx)))
132         goto err;
133
134     dctx->type = sctx->type;
135
136     switch (dctx->type) {
137     case MAC_TYPE_RAW:
138         dctx->raw_data.md = sctx->raw_data.md;
139         if (ASN1_STRING_get0_data(&sctx->raw_data.ktmp) != NULL &&
140             !ASN1_STRING_copy(&dctx->raw_data.ktmp, &sctx->raw_data.ktmp))
141             goto err;
142         break;
143     case MAC_TYPE_MAC:
144         /* Nothing more to do */
145         break;
146     default:
147         /* This should be dead code */
148         return 0;
149     }
150     return 1;
151  err:
152     pkey_mac_cleanup(dst);
153     return 0;
154 }
155
156 static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx)
157 {
158     /*
159      * For the exact same reasons the MAC reference count is incremented
160      * in pkey_mac_copy() above, it must be explicitly freed here.
161      */
162
163     MAC_PKEY_CTX *hctx = ctx == NULL ? NULL : EVP_PKEY_CTX_get_data(ctx);
164
165     if (hctx != NULL) {
166         EVP_MAC *mac = hctx->ctx != NULL ? EVP_MAC_get_ctx_mac(hctx->ctx)
167                                          : NULL;
168
169         switch (hctx->type) {
170         case MAC_TYPE_RAW:
171             OPENSSL_clear_free(hctx->raw_data.ktmp.data,
172                                hctx->raw_data.ktmp.length);
173             break;
174         }
175         EVP_MAC_free_ctx(hctx->ctx);
176         EVP_MAC_free(mac);
177         OPENSSL_free(hctx);
178         EVP_PKEY_CTX_set_data(ctx, NULL);
179     }
180 }
181
182 static int pkey_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
183 {
184     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
185     int nid = ctx->pmeth->pkey_id;
186
187     switch (hctx->type) {
188     case MAC_TYPE_RAW:
189         {
190             ASN1_OCTET_STRING *hkey = NULL;
191
192             if (!hctx->raw_data.ktmp.data)
193                 return 0;
194             hkey = ASN1_OCTET_STRING_dup(&hctx->raw_data.ktmp);
195             if (!hkey)
196                 return 0;
197             EVP_PKEY_assign(pkey, nid, hkey);
198         }
199         break;
200     case MAC_TYPE_MAC:
201         {
202             EVP_MAC_CTX *cmkey;
203
204             if (hctx->ctx == NULL) {
205                 /* This actually means the fetch failed during the init call */
206                 EVPerr(0, EVP_R_FETCH_FAILED);
207                 return 0;
208             }
209
210             cmkey = EVP_MAC_dup_ctx(hctx->ctx);
211             if (cmkey == NULL)
212                 return 0;
213             if (!EVP_MAC_up_ref(EVP_MAC_get_ctx_mac(hctx->ctx)))
214                 return 0;
215             EVP_PKEY_assign(pkey, nid, cmkey);
216         }
217         break;
218     default:
219         /* This should be dead code */
220         return 0;
221     }
222
223     return 1;
224 }
225
226 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
227 {
228     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
229
230     if (!EVP_MAC_update(hctx->ctx, data, count))
231         return 0;
232     return 1;
233 }
234
235 static int pkey_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
236 {
237     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
238     ASN1_OCTET_STRING *key = NULL;
239     int rv = 1;
240     /*
241      * For MACs with the EVP_PKEY_FLAG_SIGCTX_CUSTOM flag set and that
242      * gets the key passed as an ASN.1 OCTET STRING, we set the key here,
243      * as this may be only time it's set during a DigestSign.
244      *
245      * MACs that pass around the key in form of EVP_MAC_CTX are setting
246      * the key through other mechanisms.  (this is only CMAC for now)
247      */
248     int set_key =
249         hctx->type == MAC_TYPE_RAW
250         && (ctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) != 0;
251
252     if (hctx->ctx == NULL) {
253         /* This actually means the fetch failed during the init call */
254         EVPerr(0, EVP_R_FETCH_FAILED);
255         return 0;
256     }
257
258     if (set_key) {
259         if (!EVP_MAC_is_a(EVP_MAC_get_ctx_mac(hctx->ctx),
260                           OBJ_nid2sn(EVP_PKEY_id(EVP_PKEY_CTX_get0_pkey(ctx)))))
261             return 0;
262         key = EVP_PKEY_get0(EVP_PKEY_CTX_get0_pkey(ctx));
263         if (key == NULL)
264             return 0;
265     }
266
267     EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
268     EVP_MD_CTX_set_update_fn(mctx, int_update);
269
270     /* Some MACs don't support this control...  that's fine */
271     {
272         OSSL_PARAM params[3];
273         size_t params_n = 0;
274         int flags = EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT);
275
276         /* TODO(3.0) "flags" isn't quite right, i.e. a quick hack for now */
277         params[params_n++] =
278             OSSL_PARAM_construct_int(OSSL_MAC_PARAM_FLAGS, &flags);
279         if (set_key)
280             params[params_n++] =
281                 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
282                                                   key->data, key->length);
283         params[params_n++] = OSSL_PARAM_construct_end();
284         rv = EVP_MAC_set_ctx_params(hctx->ctx, params);
285     }
286     return rv;
287 }
288
289 static int pkey_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
290                              size_t *siglen, EVP_MD_CTX *mctx)
291 {
292     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
293
294     return EVP_MAC_final(hctx->ctx, sig, siglen, EVP_MAC_size(hctx->ctx));
295 }
296
297 static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
298 {
299     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
300
301     switch (type) {
302
303     case EVP_PKEY_CTRL_CIPHER:
304         switch (hctx->type) {
305         case MAC_TYPE_RAW:
306             return -2;       /* The raw types don't support ciphers */
307         case MAC_TYPE_MAC:
308             {
309                 OSSL_PARAM params[3];
310                 size_t params_n = 0;
311                 char *ciphname = (char *)OBJ_nid2sn(EVP_CIPHER_nid(p2));
312
313 #ifndef OPENSSL_NO_ENGINE
314                 if (ctx->engine != NULL) {
315                     char *engid = (char *)ENGINE_get_id(ctx->engine);
316
317                     params[params_n++] =
318                         OSSL_PARAM_construct_utf8_string("engine", engid, 0);
319                 }
320 #endif
321                 params[params_n++] =
322                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
323                                                      ciphname, 0);
324                 params[params_n] = OSSL_PARAM_construct_end();
325
326                 if (hctx->ctx == NULL) {
327                     /*
328                      * This actually means the fetch failed during the init call
329                      */
330                     EVPerr(0, EVP_R_FETCH_FAILED);
331                     return 0;
332                 }
333
334                 if (!EVP_MAC_set_ctx_params(hctx->ctx, params)
335                     || !EVP_MAC_init(hctx->ctx))
336                     return 0;
337             }
338             break;
339         default:
340             /* This should be dead code */
341             return 0;
342         }
343         break;
344
345     case EVP_PKEY_CTRL_MD:
346         switch (hctx->type) {
347         case MAC_TYPE_RAW:
348             hctx->raw_data.md = p2;
349             break;
350         case MAC_TYPE_MAC: {
351                 EVP_MAC_CTX *new_mac_ctx;
352
353                 if (ctx->pkey == NULL)
354                     return 0;
355                 new_mac_ctx = EVP_MAC_dup_ctx(ctx->pkey->pkey.ptr);
356                 if (new_mac_ctx == NULL)
357                     return 0;
358                 EVP_MAC_free_ctx(hctx->ctx);
359                 hctx->ctx = new_mac_ctx;
360             }
361             break;
362         default:
363             /* This should be dead code */
364             return 0;
365         }
366         break;
367
368     case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
369         {
370             OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
371             size_t size = (size_t)p1;
372             size_t verify = 0;
373
374             /*
375              * We verify that the length is actually set by getting back
376              * the same parameter and checking that it matches what we
377              * tried to set.
378              * TODO(3.0) when we have a more direct mechanism to check if
379              * a parameter was used, we must refactor this to use that.
380              */
381
382             params[0] =
383                 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &size);
384
385             if (hctx->ctx == NULL) {
386                 /*
387                  * This actually means the fetch failed during the init call
388                  */
389                 EVPerr(0, EVP_R_FETCH_FAILED);
390                 return 0;
391             }
392
393             if (!EVP_MAC_set_ctx_params(hctx->ctx, params))
394                 return 0;
395
396             params[0] =
397                 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &verify);
398
399             if (!EVP_MAC_get_ctx_params(hctx->ctx, params))
400                 return 0;
401
402             /*
403              * Since EVP_MAC_{get,set}_ctx_params() returned successfully,
404              * we can only assume that the size was ignored, i.e. this
405              * control is unsupported.
406              */
407             if (verify != size)
408                 return -2;
409         }
410         break;
411     case EVP_PKEY_CTRL_SET_MAC_KEY:
412         switch (hctx->type) {
413         case MAC_TYPE_RAW:
414             if ((!p2 && p1 > 0) || (p1 < -1))
415                 return 0;
416             if (!ASN1_OCTET_STRING_set(&hctx->raw_data.ktmp, p2, p1))
417                 return 0;
418             break;
419         case MAC_TYPE_MAC:
420             {
421                 OSSL_PARAM params[2];
422                 size_t params_n = 0;
423
424                 params[params_n++] =
425                     OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
426                                                       p2, p1);
427                 params[params_n] = OSSL_PARAM_construct_end();
428
429                 if (hctx->ctx == NULL) {
430                     /*
431                      * This actually means the fetch failed during the init call
432                      */
433                     EVPerr(0, EVP_R_FETCH_FAILED);
434                     return 0;
435                 }
436
437                 return EVP_MAC_set_ctx_params(hctx->ctx, params);
438             }
439             break;
440         default:
441             /* This should be dead code */
442             return 0;
443         }
444         break;
445
446     case EVP_PKEY_CTRL_DIGESTINIT:
447         switch (hctx->type) {
448         case MAC_TYPE_RAW:
449             if (hctx->ctx == NULL) {
450                 /* This actually means the fetch failed during the init call */
451                 EVPerr(0, EVP_R_FETCH_FAILED);
452                 return 0;
453             }
454
455             /* Ensure that we have attached the implementation */
456             if (!EVP_MAC_init(hctx->ctx))
457                 return 0;
458             {
459                 ASN1_OCTET_STRING *key =
460                     (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
461                 OSSL_PARAM params[4];
462                 size_t params_n = 0;
463                 char *mdname =
464                     (char *)OBJ_nid2sn(EVP_MD_nid(hctx->raw_data.md));
465
466 #ifndef OPENSSL_NO_ENGINE
467                 if (ctx->engine != NULL) {
468                     char *engid = (char *)ENGINE_get_id(ctx->engine);
469
470                     params[params_n++] =
471                         OSSL_PARAM_construct_utf8_string("engine", engid, 0);
472                 }
473 #endif
474                 params[params_n++] =
475                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
476                                                      mdname, 0);
477                 params[params_n++] =
478                     OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
479                                                       key->data, key->length);
480                 params[params_n] = OSSL_PARAM_construct_end();
481
482                 return EVP_MAC_set_ctx_params(hctx->ctx, params);
483             }
484             break;
485         case MAC_TYPE_MAC:
486             return -2;       /* The mac types don't support ciphers */
487         default:
488             /* This should be dead code */
489             return 0;
490         }
491         break;
492
493     default:
494         return -2;
495
496     }
497     return 1;
498 }
499
500 static int pkey_mac_ctrl_str(EVP_PKEY_CTX *ctx,
501                              const char *type, const char *value)
502 {
503     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
504     const EVP_MAC *mac;
505     OSSL_PARAM params[2];
506     int ok = 0;
507
508     if (hctx == NULL) {
509         EVPerr(0, EVP_R_NULL_MAC_PKEY_CTX);
510         return 0;
511     }
512     if (hctx->ctx == NULL) {
513         /* This actually means the fetch failed during the init call */
514         EVPerr(0, EVP_R_FETCH_FAILED);
515         return 0;
516     }
517     mac = EVP_MAC_get_ctx_mac(hctx->ctx);
518
519     /*
520      * Translation of some control names that are equivalent to a single
521      * parameter name.
522      *
523      * "md" and "digest" are the same thing, we use the single "digest"
524      *
525      * "digestsize" was a setting control in siphash, but naming wise,
526      * it's really the same as "size".
527      */
528     if (strcmp(type, "md") == 0)
529         type = OSSL_MAC_PARAM_DIGEST;
530     else if (strcmp(type, "digestsize") == 0)
531         type = OSSL_MAC_PARAM_SIZE;
532
533     if (!OSSL_PARAM_allocate_from_text(&params[0],
534                                        EVP_MAC_settable_ctx_params(mac),
535                                        type, value, strlen(value) + 1, NULL))
536         return 0;
537     params[1] = OSSL_PARAM_construct_end();
538
539     ok = EVP_MAC_set_ctx_params(hctx->ctx, params);
540     OPENSSL_free(params[0].data);
541     return ok;
542 }
543
544 static const EVP_PKEY_METHOD cmac_pkey_meth = {
545     EVP_PKEY_CMAC,
546     EVP_PKEY_FLAG_SIGCTX_CUSTOM,
547     pkey_mac_init,
548     pkey_mac_copy,
549     pkey_mac_cleanup,
550
551     0, 0,
552
553     0,
554     pkey_mac_keygen,
555
556     0, 0,
557
558     0, 0,
559
560     0, 0,
561
562     pkey_mac_signctx_init,
563     pkey_mac_signctx,
564
565     0, 0,
566
567     0, 0,
568
569     0, 0,
570
571     0, 0,
572
573     pkey_mac_ctrl,
574     pkey_mac_ctrl_str
575 };
576
577 const EVP_PKEY_METHOD *cmac_pkey_method(void)
578 {
579     return &cmac_pkey_meth;
580 }
581
582 static const EVP_PKEY_METHOD hmac_pkey_meth = {
583     EVP_PKEY_HMAC,
584     0,
585     pkey_mac_init,
586     pkey_mac_copy,
587     pkey_mac_cleanup,
588
589     0, 0,
590
591     0,
592     pkey_mac_keygen,
593
594     0, 0,
595
596     0, 0,
597
598     0, 0,
599
600     pkey_mac_signctx_init,
601     pkey_mac_signctx,
602
603     0, 0,
604
605     0, 0,
606
607     0, 0,
608
609     0, 0,
610
611     pkey_mac_ctrl,
612     pkey_mac_ctrl_str
613 };
614
615 const EVP_PKEY_METHOD *hmac_pkey_method(void)
616 {
617     return &hmac_pkey_meth;
618 }
619
620 static const EVP_PKEY_METHOD siphash_pkey_meth = {
621     EVP_PKEY_SIPHASH,
622     EVP_PKEY_FLAG_SIGCTX_CUSTOM,
623     pkey_mac_init,
624     pkey_mac_copy,
625     pkey_mac_cleanup,
626
627     0, 0,
628
629     0,
630     pkey_mac_keygen,
631
632     0, 0,
633
634     0, 0,
635
636     0, 0,
637
638     pkey_mac_signctx_init,
639     pkey_mac_signctx,
640
641     0, 0,
642
643     0, 0,
644
645     0, 0,
646
647     0, 0,
648
649     pkey_mac_ctrl,
650     pkey_mac_ctrl_str
651 };
652
653 const EVP_PKEY_METHOD *siphash_pkey_method(void)
654 {
655     return &siphash_pkey_meth;
656 }
657
658 static const EVP_PKEY_METHOD poly1305_pkey_meth = {
659     EVP_PKEY_POLY1305,
660     EVP_PKEY_FLAG_SIGCTX_CUSTOM,
661     pkey_mac_init,
662     pkey_mac_copy,
663     pkey_mac_cleanup,
664
665     0, 0,
666
667     0,
668     pkey_mac_keygen,
669
670     0, 0,
671
672     0, 0,
673
674     0, 0,
675
676     pkey_mac_signctx_init,
677     pkey_mac_signctx,
678
679     0, 0,
680
681     0, 0,
682
683     0, 0,
684
685     0, 0,
686
687     pkey_mac_ctrl,
688     pkey_mac_ctrl_str
689 };
690
691 const EVP_PKEY_METHOD *poly1305_pkey_method(void)
692 {
693     return &poly1305_pkey_meth;
694 }