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