2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
19 /* MAC PKEY context structure */
25 * We know of two MAC types:
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.
34 * (one might wonder why the second form isn't used for all)
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) */
40 /* The following is only used for MAC_TYPE_RAW implementations */
42 const EVP_MD *md; /* temp storage of MD */
43 ASN1_OCTET_STRING ktmp; /* temp storage for key */
47 static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx);
49 static int pkey_mac_init(EVP_PKEY_CTX *ctx)
52 /* We're being smart and using the same base NIDs for PKEY and for MAC */
53 int nid = ctx->pmeth->pkey_id;
57 mac = EVP_MAC_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propquery);
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
68 if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) {
69 EVPerr(EVP_F_PKEY_MAC_INIT, ERR_R_MALLOC_FAILURE);
74 hctx->ctx = EVP_MAC_CTX_new(mac);
75 if (hctx->ctx == NULL) {
81 if (nid == EVP_PKEY_CMAC) {
82 hctx->type = MAC_TYPE_MAC;
84 hctx->type = MAC_TYPE_RAW;
85 hctx->raw_data.ktmp.type = V_ASN1_OCTET_STRING;
88 pkey_mac_cleanup(ctx);
89 EVP_PKEY_CTX_set_data(ctx, hctx);
90 ctx->keygen_info_count = 0;
95 static int pkey_mac_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
97 MAC_PKEY_CTX *sctx, *dctx;
99 sctx = EVP_PKEY_CTX_get_data(src);
101 if (sctx->ctx == NULL) {
102 /* This actually means the fetch failed during the init call */
103 EVPerr(0, EVP_R_FETCH_FAILED);
107 if (sctx->ctx->data == NULL)
110 dctx = OPENSSL_zalloc(sizeof(*dctx));
112 EVPerr(EVP_F_PKEY_MAC_COPY, ERR_R_MALLOC_FAILURE);
116 EVP_PKEY_CTX_set_data(dst, dctx);
117 dst->keygen_info_count = 0;
119 dctx->ctx = EVP_MAC_CTX_dup(sctx->ctx);
120 if (dctx->ctx == NULL)
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.
131 if (!EVP_MAC_up_ref(EVP_MAC_CTX_mac(dctx->ctx)))
134 dctx->type = sctx->type;
136 switch (dctx->type) {
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))
144 /* Nothing more to do */
147 /* This should be dead code */
152 pkey_mac_cleanup(dst);
156 static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx)
159 * For the exact same reasons the MAC reference count is incremented
160 * in pkey_mac_copy() above, it must be explicitly freed here.
163 MAC_PKEY_CTX *hctx = ctx == NULL ? NULL : EVP_PKEY_CTX_get_data(ctx);
166 EVP_MAC *mac = hctx->ctx != NULL ? EVP_MAC_CTX_mac(hctx->ctx) : NULL;
168 switch (hctx->type) {
170 OPENSSL_clear_free(hctx->raw_data.ktmp.data,
171 hctx->raw_data.ktmp.length);
174 EVP_MAC_CTX_free(hctx->ctx);
177 EVP_PKEY_CTX_set_data(ctx, NULL);
181 static int pkey_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
183 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
184 int nid = ctx->pmeth->pkey_id;
186 switch (hctx->type) {
189 ASN1_OCTET_STRING *hkey = NULL;
191 if (!hctx->raw_data.ktmp.data)
193 hkey = ASN1_OCTET_STRING_dup(&hctx->raw_data.ktmp);
196 EVP_PKEY_assign(pkey, nid, hkey);
203 if (hctx->ctx == NULL) {
204 /* This actually means the fetch failed during the init call */
205 EVPerr(0, EVP_R_FETCH_FAILED);
209 cmkey = EVP_MAC_CTX_dup(hctx->ctx);
212 if (!EVP_MAC_up_ref(EVP_MAC_CTX_mac(hctx->ctx)))
214 EVP_PKEY_assign(pkey, nid, cmkey);
218 /* This should be dead code */
225 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
227 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
229 if (!EVP_MAC_update(hctx->ctx, data, count))
234 static int pkey_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
236 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
237 ASN1_OCTET_STRING *key = NULL;
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.
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)
248 hctx->type == MAC_TYPE_RAW
249 && (ctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) != 0;
251 if (hctx->ctx == NULL) {
252 /* This actually means the fetch failed during the init call */
253 EVPerr(0, EVP_R_FETCH_FAILED);
258 if (!EVP_MAC_is_a(EVP_MAC_CTX_mac(hctx->ctx),
259 OBJ_nid2sn(EVP_PKEY_id(EVP_PKEY_CTX_get0_pkey(ctx)))))
261 key = EVP_PKEY_get0(EVP_PKEY_CTX_get0_pkey(ctx));
266 EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
267 EVP_MD_CTX_set_update_fn(mctx, int_update);
269 /* Some MACs don't support this control... that's fine */
271 OSSL_PARAM params[3];
273 int flags = EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT);
275 /* TODO(3.0) "flags" isn't quite right, i.e. a quick hack for now */
277 OSSL_PARAM_construct_int(OSSL_MAC_PARAM_FLAGS, &flags);
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);
288 static int pkey_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
289 size_t *siglen, EVP_MD_CTX *mctx)
291 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
293 return EVP_MAC_final(hctx->ctx, sig, siglen, EVP_MAC_size(hctx->ctx));
296 static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
298 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
302 case EVP_PKEY_CTRL_CIPHER:
303 switch (hctx->type) {
305 return -2; /* The raw types don't support ciphers */
308 OSSL_PARAM params[3];
310 char *ciphname = (char *)OBJ_nid2sn(EVP_CIPHER_nid(p2));
311 #ifndef OPENSSL_NO_ENGINE
312 char *engineid = (char *)ENGINE_get_id(ctx->engine);
315 OSSL_PARAM_construct_utf8_string("engine", engineid, 0);
318 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
320 params[params_n] = OSSL_PARAM_construct_end();
322 if (hctx->ctx == NULL) {
324 * This actually means the fetch failed during the init call
326 EVPerr(0, EVP_R_FETCH_FAILED);
330 if (!EVP_MAC_CTX_set_params(hctx->ctx, params)
331 || !EVP_MAC_init(hctx->ctx))
336 /* This should be dead code */
341 case EVP_PKEY_CTRL_MD:
342 switch (hctx->type) {
344 hctx->raw_data.md = p2;
347 EVP_MAC_CTX *new_mac_ctx;
349 if (ctx->pkey == NULL)
351 new_mac_ctx = EVP_MAC_CTX_dup(ctx->pkey->pkey.ptr);
352 if (new_mac_ctx == NULL)
354 EVP_MAC_CTX_free(hctx->ctx);
355 hctx->ctx = new_mac_ctx;
359 /* This should be dead code */
364 case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
366 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
367 size_t size = (size_t)p1;
371 * We verify that the length is actually set by getting back
372 * the same parameter and checking that it matches what we
374 * TODO(3.0) when we have a more direct mechanism to check if
375 * a parameter was used, we must refactor this to use that.
379 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &size);
381 if (hctx->ctx == NULL) {
383 * This actually means the fetch failed during the init call
385 EVPerr(0, EVP_R_FETCH_FAILED);
389 if (!EVP_MAC_CTX_set_params(hctx->ctx, params))
393 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &verify);
395 if (!EVP_MAC_CTX_get_params(hctx->ctx, params))
399 * Since EVP_MAC_CTX_{get,set}_params() returned successfully,
400 * we can only assume that the size was ignored, i.e. this
401 * control is unsupported.
407 case EVP_PKEY_CTRL_SET_MAC_KEY:
408 switch (hctx->type) {
410 if ((!p2 && p1 > 0) || (p1 < -1))
412 if (!ASN1_OCTET_STRING_set(&hctx->raw_data.ktmp, p2, p1))
417 OSSL_PARAM params[2];
421 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
423 params[params_n] = OSSL_PARAM_construct_end();
425 if (hctx->ctx == NULL) {
427 * This actually means the fetch failed during the init call
429 EVPerr(0, EVP_R_FETCH_FAILED);
433 return EVP_MAC_CTX_set_params(hctx->ctx, params);
437 /* This should be dead code */
442 case EVP_PKEY_CTRL_DIGESTINIT:
443 switch (hctx->type) {
445 if (hctx->ctx == NULL) {
446 /* This actually means the fetch failed during the init call */
447 EVPerr(0, EVP_R_FETCH_FAILED);
451 /* Ensure that we have attached the implementation */
452 if (!EVP_MAC_init(hctx->ctx))
455 ASN1_OCTET_STRING *key =
456 (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
457 OSSL_PARAM params[4];
460 (char *)OBJ_nid2sn(EVP_MD_nid(hctx->raw_data.md));
461 #ifndef OPENSSL_NO_ENGINE
462 char *engineid = ctx->engine == NULL
463 ? NULL : (char *)ENGINE_get_id(ctx->engine);
465 if (engineid != NULL)
467 OSSL_PARAM_construct_utf8_string("engine", engineid, 0);
470 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
473 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
474 key->data, key->length);
475 params[params_n] = OSSL_PARAM_construct_end();
477 return EVP_MAC_CTX_set_params(hctx->ctx, params);
481 return -2; /* The mac types don't support ciphers */
483 /* This should be dead code */
495 static int pkey_mac_ctrl_str(EVP_PKEY_CTX *ctx,
496 const char *type, const char *value)
498 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
499 const EVP_MAC *mac = EVP_MAC_CTX_mac(hctx->ctx);
500 OSSL_PARAM params[2];
504 * Translation of some control names that are equivalent to a single
507 * "md" and "digest" are the same thing, we use the single "digest"
509 * "digestsize" was a setting control in siphash, but naming wise,
510 * it's really the same as "size".
512 if (strcmp(type, "md") == 0)
513 type = OSSL_MAC_PARAM_DIGEST;
514 else if (strcmp(type, "digestsize") == 0)
515 type = OSSL_MAC_PARAM_SIZE;
517 if (!OSSL_PARAM_allocate_from_text(¶ms[0],
518 EVP_MAC_settable_ctx_params(mac),
519 type, value, strlen(value) + 1, NULL))
521 params[1] = OSSL_PARAM_construct_end();
523 if (hctx->ctx == NULL) {
524 /* This actually means the fetch failed during the init call */
525 EVPerr(0, EVP_R_FETCH_FAILED);
529 ok = EVP_MAC_CTX_set_params(hctx->ctx, params);
530 OPENSSL_free(params[0].data);
534 static const EVP_PKEY_METHOD cmac_pkey_meth = {
536 EVP_PKEY_FLAG_SIGCTX_CUSTOM,
552 pkey_mac_signctx_init,
567 const EVP_PKEY_METHOD *cmac_pkey_method(void)
569 return &cmac_pkey_meth;
572 static const EVP_PKEY_METHOD hmac_pkey_meth = {
590 pkey_mac_signctx_init,
605 const EVP_PKEY_METHOD *hmac_pkey_method(void)
607 return &hmac_pkey_meth;
610 static const EVP_PKEY_METHOD siphash_pkey_meth = {
612 EVP_PKEY_FLAG_SIGCTX_CUSTOM,
628 pkey_mac_signctx_init,
643 const EVP_PKEY_METHOD *siphash_pkey_method(void)
645 return &siphash_pkey_meth;
648 static const EVP_PKEY_METHOD poly1305_pkey_meth = {
650 EVP_PKEY_FLAG_SIGCTX_CUSTOM,
666 pkey_mac_signctx_init,
681 const EVP_PKEY_METHOD *poly1305_pkey_method(void)
683 return &poly1305_pkey_meth;