3503aac6d306855e22ca7f610da93c67b8cd02d5
[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_CTX_new(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_CTX_dup(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_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_CTX_mac(hctx->ctx) : NULL;
167
168         switch (hctx->type) {
169         case MAC_TYPE_RAW:
170             OPENSSL_clear_free(hctx->raw_data.ktmp.data,
171                                hctx->raw_data.ktmp.length);
172             break;
173         }
174         EVP_MAC_CTX_free(hctx->ctx);
175         EVP_MAC_free(mac);
176         OPENSSL_free(hctx);
177         EVP_PKEY_CTX_set_data(ctx, NULL);
178     }
179 }
180
181 static int pkey_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
182 {
183     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
184     int nid = ctx->pmeth->pkey_id;
185
186     switch (hctx->type) {
187     case MAC_TYPE_RAW:
188         {
189             ASN1_OCTET_STRING *hkey = NULL;
190
191             if (!hctx->raw_data.ktmp.data)
192                 return 0;
193             hkey = ASN1_OCTET_STRING_dup(&hctx->raw_data.ktmp);
194             if (!hkey)
195                 return 0;
196             EVP_PKEY_assign(pkey, nid, hkey);
197         }
198         break;
199     case MAC_TYPE_MAC:
200         {
201             EVP_MAC_CTX *cmkey;
202
203             if (hctx->ctx == NULL) {
204                 /* This actually means the fetch failed during the init call */
205                 EVPerr(0, EVP_R_FETCH_FAILED);
206                 return 0;
207             }
208
209             cmkey = EVP_MAC_CTX_dup(hctx->ctx);
210             if (cmkey == NULL)
211                 return 0;
212             if (!EVP_MAC_up_ref(EVP_MAC_CTX_mac(hctx->ctx)))
213                 return 0;
214             EVP_PKEY_assign(pkey, nid, cmkey);
215         }
216         break;
217     default:
218         /* This should be dead code */
219         return 0;
220     }
221
222     return 1;
223 }
224
225 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
226 {
227     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
228
229     if (!EVP_MAC_update(hctx->ctx, data, count))
230         return 0;
231     return 1;
232 }
233
234 static int pkey_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
235 {
236     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
237     ASN1_OCTET_STRING *key = NULL;
238     int rv = 1;
239     /*
240      * For MACs with the EVP_PKEY_FLAG_SIGCTX_CUSTOM flag set and that
241      * gets the key passed as an ASN.1 OCTET STRING, we set the key here,
242      * as this may be only time it's set during a DigestSign.
243      *
244      * MACs that pass around the key in form of EVP_MAC_CTX are setting
245      * the key through other mechanisms.  (this is only CMAC for now)
246      */
247     int set_key =
248         hctx->type == MAC_TYPE_RAW
249         && (ctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) != 0;
250
251     if (hctx->ctx == NULL) {
252         /* This actually means the fetch failed during the init call */
253         EVPerr(0, EVP_R_FETCH_FAILED);
254         return 0;
255     }
256
257     if (set_key) {
258         if (!EVP_MAC_is_a(EVP_MAC_CTX_mac(hctx->ctx),
259                           OBJ_nid2sn(EVP_PKEY_id(EVP_PKEY_CTX_get0_pkey(ctx)))))
260             return 0;
261         key = EVP_PKEY_get0(EVP_PKEY_CTX_get0_pkey(ctx));
262         if (key == NULL)
263             return 0;
264     }
265
266     EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
267     EVP_MD_CTX_set_update_fn(mctx, int_update);
268
269     /* Some MACs don't support this control...  that's fine */
270     {
271         OSSL_PARAM params[3];
272         size_t params_n = 0;
273         int flags = EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT);
274
275         /* TODO(3.0) "flags" isn't quite right, i.e. a quick hack for now */
276         params[params_n++] =
277             OSSL_PARAM_construct_int(OSSL_MAC_PARAM_FLAGS, &flags);
278         if (set_key)
279             params[params_n++] =
280                 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
281                                                   key->data, key->length);
282         params[params_n++] = OSSL_PARAM_construct_end();
283         rv = EVP_MAC_CTX_set_params(hctx->ctx, params);
284     }
285     return rv;
286 }
287
288 static int pkey_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
289                              size_t *siglen, EVP_MD_CTX *mctx)
290 {
291     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
292
293     return EVP_MAC_final(hctx->ctx, sig, siglen, EVP_MAC_size(hctx->ctx));
294 }
295
296 static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
297 {
298     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
299
300     switch (type) {
301
302     case EVP_PKEY_CTRL_CIPHER:
303         switch (hctx->type) {
304         case MAC_TYPE_RAW:
305             return -2;       /* The raw types don't support ciphers */
306         case MAC_TYPE_MAC:
307             {
308                 OSSL_PARAM params[3];
309                 size_t params_n = 0;
310                 char *ciphname = (char *)OBJ_nid2sn(EVP_CIPHER_nid(p2));
311
312 #ifndef OPENSSL_NO_ENGINE
313                 if (ctx->engine != NULL) {
314                     char *engid = (char *)ENGINE_get_id(ctx->engine);
315
316                     params[params_n++] =
317                         OSSL_PARAM_construct_utf8_string("engine", engid, 0);
318                 }
319 #endif
320                 params[params_n++] =
321                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
322                                                      ciphname, 0);
323                 params[params_n] = OSSL_PARAM_construct_end();
324
325                 if (hctx->ctx == NULL) {
326                     /*
327                      * This actually means the fetch failed during the init call
328                      */
329                     EVPerr(0, EVP_R_FETCH_FAILED);
330                     return 0;
331                 }
332
333                 if (!EVP_MAC_CTX_set_params(hctx->ctx, params)
334                     || !EVP_MAC_init(hctx->ctx))
335                     return 0;
336             }
337             break;
338         default:
339             /* This should be dead code */
340             return 0;
341         }
342         break;
343
344     case EVP_PKEY_CTRL_MD:
345         switch (hctx->type) {
346         case MAC_TYPE_RAW:
347             hctx->raw_data.md = p2;
348             break;
349         case MAC_TYPE_MAC: {
350                 EVP_MAC_CTX *new_mac_ctx;
351
352                 if (ctx->pkey == NULL)
353                     return 0;
354                 new_mac_ctx = EVP_MAC_CTX_dup(ctx->pkey->pkey.ptr);
355                 if (new_mac_ctx == NULL)
356                     return 0;
357                 EVP_MAC_CTX_free(hctx->ctx);
358                 hctx->ctx = new_mac_ctx;
359             }
360             break;
361         default:
362             /* This should be dead code */
363             return 0;
364         }
365         break;
366
367     case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
368         {
369             OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
370             size_t size = (size_t)p1;
371             size_t verify = 0;
372
373             /*
374              * We verify that the length is actually set by getting back
375              * the same parameter and checking that it matches what we
376              * tried to set.
377              * TODO(3.0) when we have a more direct mechanism to check if
378              * a parameter was used, we must refactor this to use that.
379              */
380
381             params[0] =
382                 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &size);
383
384             if (hctx->ctx == NULL) {
385                 /*
386                  * This actually means the fetch failed during the init call
387                  */
388                 EVPerr(0, EVP_R_FETCH_FAILED);
389                 return 0;
390             }
391
392             if (!EVP_MAC_CTX_set_params(hctx->ctx, params))
393                 return 0;
394
395             params[0] =
396                 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &verify);
397
398             if (!EVP_MAC_CTX_get_params(hctx->ctx, params))
399                 return 0;
400
401             /*
402              * Since EVP_MAC_CTX_{get,set}_params() returned successfully,
403              * we can only assume that the size was ignored, i.e. this
404              * control is unsupported.
405              */
406             if (verify != size)
407                 return -2;
408         }
409         break;
410     case EVP_PKEY_CTRL_SET_MAC_KEY:
411         switch (hctx->type) {
412         case MAC_TYPE_RAW:
413             if ((!p2 && p1 > 0) || (p1 < -1))
414                 return 0;
415             if (!ASN1_OCTET_STRING_set(&hctx->raw_data.ktmp, p2, p1))
416                 return 0;
417             break;
418         case MAC_TYPE_MAC:
419             {
420                 OSSL_PARAM params[2];
421                 size_t params_n = 0;
422
423                 params[params_n++] =
424                     OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
425                                                       p2, p1);
426                 params[params_n] = OSSL_PARAM_construct_end();
427
428                 if (hctx->ctx == NULL) {
429                     /*
430                      * This actually means the fetch failed during the init call
431                      */
432                     EVPerr(0, EVP_R_FETCH_FAILED);
433                     return 0;
434                 }
435
436                 return EVP_MAC_CTX_set_params(hctx->ctx, params);
437             }
438             break;
439         default:
440             /* This should be dead code */
441             return 0;
442         }
443         break;
444
445     case EVP_PKEY_CTRL_DIGESTINIT:
446         switch (hctx->type) {
447         case MAC_TYPE_RAW:
448             if (hctx->ctx == NULL) {
449                 /* This actually means the fetch failed during the init call */
450                 EVPerr(0, EVP_R_FETCH_FAILED);
451                 return 0;
452             }
453
454             /* Ensure that we have attached the implementation */
455             if (!EVP_MAC_init(hctx->ctx))
456                 return 0;
457             {
458                 ASN1_OCTET_STRING *key =
459                     (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
460                 OSSL_PARAM params[4];
461                 size_t params_n = 0;
462                 char *mdname =
463                     (char *)OBJ_nid2sn(EVP_MD_nid(hctx->raw_data.md));
464
465 #ifndef OPENSSL_NO_ENGINE
466                 if (ctx->engine != NULL) {
467                     char *engid = (char *)ENGINE_get_id(ctx->engine);
468
469                     params[params_n++] =
470                         OSSL_PARAM_construct_utf8_string("engine", engid, 0);
471                 }
472 #endif
473                 params[params_n++] =
474                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
475                                                      mdname, 0);
476                 params[params_n++] =
477                     OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
478                                                       key->data, key->length);
479                 params[params_n] = OSSL_PARAM_construct_end();
480
481                 return EVP_MAC_CTX_set_params(hctx->ctx, params);
482             }
483             break;
484         case MAC_TYPE_MAC:
485             return -2;       /* The mac types don't support ciphers */
486         default:
487             /* This should be dead code */
488             return 0;
489         }
490         break;
491
492     default:
493         return -2;
494
495     }
496     return 1;
497 }
498
499 static int pkey_mac_ctrl_str(EVP_PKEY_CTX *ctx,
500                              const char *type, const char *value)
501 {
502     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
503     const EVP_MAC *mac;
504     OSSL_PARAM params[2];
505     int ok = 0;
506
507     if (hctx == NULL) {
508         EVPerr(0, EVP_R_NULL_MAC_PKEY_CTX);
509         return 0;
510     }
511     if (hctx->ctx == NULL) {
512         /* This actually means the fetch failed during the init call */
513         EVPerr(0, EVP_R_FETCH_FAILED);
514         return 0;
515     }
516     mac = EVP_MAC_CTX_mac(hctx->ctx);
517
518     /*
519      * Translation of some control names that are equivalent to a single
520      * parameter name.
521      *
522      * "md" and "digest" are the same thing, we use the single "digest"
523      *
524      * "digestsize" was a setting control in siphash, but naming wise,
525      * it's really the same as "size".
526      */
527     if (strcmp(type, "md") == 0)
528         type = OSSL_MAC_PARAM_DIGEST;
529     else if (strcmp(type, "digestsize") == 0)
530         type = OSSL_MAC_PARAM_SIZE;
531
532     if (!OSSL_PARAM_allocate_from_text(&params[0],
533                                        EVP_MAC_settable_ctx_params(mac),
534                                        type, value, strlen(value) + 1, NULL))
535         return 0;
536     params[1] = OSSL_PARAM_construct_end();
537
538     ok = EVP_MAC_CTX_set_params(hctx->ctx, params);
539     OPENSSL_free(params[0].data);
540     return ok;
541 }
542
543 static const EVP_PKEY_METHOD cmac_pkey_meth = {
544     EVP_PKEY_CMAC,
545     EVP_PKEY_FLAG_SIGCTX_CUSTOM,
546     pkey_mac_init,
547     pkey_mac_copy,
548     pkey_mac_cleanup,
549
550     0, 0,
551
552     0,
553     pkey_mac_keygen,
554
555     0, 0,
556
557     0, 0,
558
559     0, 0,
560
561     pkey_mac_signctx_init,
562     pkey_mac_signctx,
563
564     0, 0,
565
566     0, 0,
567
568     0, 0,
569
570     0, 0,
571
572     pkey_mac_ctrl,
573     pkey_mac_ctrl_str
574 };
575
576 const EVP_PKEY_METHOD *cmac_pkey_method(void)
577 {
578     return &cmac_pkey_meth;
579 }
580
581 static const EVP_PKEY_METHOD hmac_pkey_meth = {
582     EVP_PKEY_HMAC,
583     0,
584     pkey_mac_init,
585     pkey_mac_copy,
586     pkey_mac_cleanup,
587
588     0, 0,
589
590     0,
591     pkey_mac_keygen,
592
593     0, 0,
594
595     0, 0,
596
597     0, 0,
598
599     pkey_mac_signctx_init,
600     pkey_mac_signctx,
601
602     0, 0,
603
604     0, 0,
605
606     0, 0,
607
608     0, 0,
609
610     pkey_mac_ctrl,
611     pkey_mac_ctrl_str
612 };
613
614 const EVP_PKEY_METHOD *hmac_pkey_method(void)
615 {
616     return &hmac_pkey_meth;
617 }
618
619 static const EVP_PKEY_METHOD siphash_pkey_meth = {
620     EVP_PKEY_SIPHASH,
621     EVP_PKEY_FLAG_SIGCTX_CUSTOM,
622     pkey_mac_init,
623     pkey_mac_copy,
624     pkey_mac_cleanup,
625
626     0, 0,
627
628     0,
629     pkey_mac_keygen,
630
631     0, 0,
632
633     0, 0,
634
635     0, 0,
636
637     pkey_mac_signctx_init,
638     pkey_mac_signctx,
639
640     0, 0,
641
642     0, 0,
643
644     0, 0,
645
646     0, 0,
647
648     pkey_mac_ctrl,
649     pkey_mac_ctrl_str
650 };
651
652 const EVP_PKEY_METHOD *siphash_pkey_method(void)
653 {
654     return &siphash_pkey_meth;
655 }
656
657 static const EVP_PKEY_METHOD poly1305_pkey_meth = {
658     EVP_PKEY_POLY1305,
659     EVP_PKEY_FLAG_SIGCTX_CUSTOM,
660     pkey_mac_init,
661     pkey_mac_copy,
662     pkey_mac_cleanup,
663
664     0, 0,
665
666     0,
667     pkey_mac_keygen,
668
669     0, 0,
670
671     0, 0,
672
673     0, 0,
674
675     pkey_mac_signctx_init,
676     pkey_mac_signctx,
677
678     0, 0,
679
680     0, 0,
681
682     0, 0,
683
684     0, 0,
685
686     pkey_mac_ctrl,
687     pkey_mac_ctrl_str
688 };
689
690 const EVP_PKEY_METHOD *poly1305_pkey_method(void)
691 {
692     return &poly1305_pkey_meth;
693 }