2 * Copyright 2018 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;
54 EVP_MAC *mac = EVP_MAC_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propquery);
57 EVPerr(EVP_F_PKEY_MAC_INIT, EVP_R_FETCH_FAILED);
60 if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) {
61 EVPerr(EVP_F_PKEY_MAC_INIT, ERR_R_MALLOC_FAILURE);
65 hctx->ctx = EVP_MAC_CTX_new(mac);
66 if (hctx->ctx == NULL) {
71 if (nid == EVP_PKEY_CMAC) {
72 hctx->type = MAC_TYPE_MAC;
74 hctx->type = MAC_TYPE_RAW;
75 hctx->raw_data.ktmp.type = V_ASN1_OCTET_STRING;
78 pkey_mac_cleanup(ctx);
79 EVP_PKEY_CTX_set_data(ctx, hctx);
80 ctx->keygen_info_count = 0;
85 static int pkey_mac_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
87 MAC_PKEY_CTX *sctx, *dctx;
89 sctx = EVP_PKEY_CTX_get_data(src);
90 if (sctx->ctx->data == NULL)
93 dctx = OPENSSL_zalloc(sizeof(*dctx));
95 EVPerr(EVP_F_PKEY_MAC_COPY, ERR_R_MALLOC_FAILURE);
99 EVP_PKEY_CTX_set_data(dst, dctx);
100 dst->keygen_info_count = 0;
102 dctx->ctx = EVP_MAC_CTX_dup(sctx->ctx);
103 if (dctx->ctx == NULL)
107 * Normally, nothing special would be done with the MAC method. In
108 * this particular case, though, the MAC method was fetched internally
109 * by pkey_mac_init() above or by EVP_PKEY_new_CMAC_key() and passed
110 * via the EVP_MAC_CTX, so it is effectively like every new EVP_MAC_CTX
111 * fetches the MAC method anew in this case. Therefore, its reference
112 * count must be adjusted here.
114 if (!EVP_MAC_up_ref(EVP_MAC_CTX_mac(dctx->ctx)))
117 dctx->type = sctx->type;
119 switch (dctx->type) {
121 dctx->raw_data.md = sctx->raw_data.md;
122 if (ASN1_STRING_get0_data(&sctx->raw_data.ktmp) != NULL &&
123 !ASN1_STRING_copy(&dctx->raw_data.ktmp, &sctx->raw_data.ktmp))
127 /* Nothing more to do */
130 /* This should be dead code */
135 pkey_mac_cleanup(dst);
139 static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx)
142 * For the exact same reasons the MAC reference count is incremented
143 * in pkey_mac_copy() above, it must be explicitly freed here.
146 MAC_PKEY_CTX *hctx = ctx == NULL ? NULL : EVP_PKEY_CTX_get_data(ctx);
149 EVP_MAC *mac = EVP_MAC_CTX_mac(hctx->ctx);
151 switch (hctx->type) {
153 OPENSSL_clear_free(hctx->raw_data.ktmp.data,
154 hctx->raw_data.ktmp.length);
157 EVP_MAC_CTX_free(hctx->ctx);
160 EVP_PKEY_CTX_set_data(ctx, NULL);
164 static int pkey_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
166 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
167 int nid = ctx->pmeth->pkey_id;
169 switch (hctx->type) {
172 ASN1_OCTET_STRING *hkey = NULL;
174 if (!hctx->raw_data.ktmp.data)
176 hkey = ASN1_OCTET_STRING_dup(&hctx->raw_data.ktmp);
179 EVP_PKEY_assign(pkey, nid, hkey);
184 EVP_MAC_CTX *cmkey = EVP_MAC_CTX_dup(hctx->ctx);
188 if (!EVP_MAC_up_ref(EVP_MAC_CTX_mac(hctx->ctx)))
190 EVP_PKEY_assign(pkey, nid, cmkey);
194 /* This should be dead code */
201 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
203 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
205 if (!EVP_MAC_update(hctx->ctx, data, count))
210 static int pkey_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
212 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
213 ASN1_OCTET_STRING *key = NULL;
216 * For MACs with the EVP_PKEY_FLAG_SIGCTX_CUSTOM flag set and that
217 * gets the key passed as an ASN.1 OCTET STRING, we set the key here,
218 * as this may be only time it's set during a DigestSign.
220 * MACs that pass around the key in form of EVP_MAC_CTX are setting
221 * the key through other mechanisms. (this is only CMAC for now)
224 hctx->type == MAC_TYPE_RAW
225 && (ctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) != 0;
228 if (!EVP_MAC_is_a(EVP_MAC_CTX_mac(hctx->ctx),
229 OBJ_nid2sn(EVP_PKEY_id(EVP_PKEY_CTX_get0_pkey(ctx)))))
231 key = EVP_PKEY_get0(EVP_PKEY_CTX_get0_pkey(ctx));
236 EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
237 EVP_MD_CTX_set_update_fn(mctx, int_update);
239 /* Some MACs don't support this control... that's fine */
241 OSSL_PARAM params[3];
243 int flags = EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT);
245 /* TODO(3.0) "flags" isn't quite right, i.e. a quick hack for now */
247 OSSL_PARAM_construct_int(OSSL_MAC_PARAM_FLAGS, &flags);
250 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
251 key->data, key->length);
252 params[params_n++] = OSSL_PARAM_construct_end();
253 rv = EVP_MAC_CTX_set_params(hctx->ctx, params);
258 static int pkey_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
259 size_t *siglen, EVP_MD_CTX *mctx)
261 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
263 return EVP_MAC_final(hctx->ctx, sig, siglen, EVP_MAC_size(hctx->ctx));
266 static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
268 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
272 case EVP_PKEY_CTRL_CIPHER:
273 switch (hctx->type) {
275 return -2; /* The raw types don't support ciphers */
278 OSSL_PARAM params[3];
280 char *ciphname = (char *)OBJ_nid2sn(EVP_CIPHER_nid(p2));
281 #ifndef OPENSSL_NO_ENGINE
282 char *engineid = (char *)ENGINE_get_id(ctx->engine);
285 OSSL_PARAM_construct_utf8_string("engine", engineid, 0);
288 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
290 params[params_n] = OSSL_PARAM_construct_end();
292 if (!EVP_MAC_CTX_set_params(hctx->ctx, params)
293 || !EVP_MAC_init(hctx->ctx))
298 /* This should be dead code */
303 case EVP_PKEY_CTRL_MD:
304 switch (hctx->type) {
306 hctx->raw_data.md = p2;
309 EVP_MAC_CTX *new_mac_ctx;
311 if (ctx->pkey == NULL)
313 new_mac_ctx = EVP_MAC_CTX_dup((EVP_MAC_CTX *)ctx->pkey
315 if (new_mac_ctx == NULL)
317 EVP_MAC_CTX_free(hctx->ctx);
318 hctx->ctx = new_mac_ctx;
322 /* This should be dead code */
327 case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
329 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
330 size_t size = (size_t)p1;
334 * We verify that the length is actually set by getting back
335 * the same parameter and checking that it matches what we
337 * TODO(3.0) when we have a more direct mechanism to check if
338 * a parameter was used, we must refactor this to use that.
342 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &size);
344 if (!EVP_MAC_CTX_set_params(hctx->ctx, params))
348 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &verify);
350 if (!EVP_MAC_CTX_get_params(hctx->ctx, params))
354 * Since EVP_MAC_CTX_{get,set}_params() returned successfully,
355 * we can only assume that the size was ignored, i.e. this
356 * control is unsupported.
362 case EVP_PKEY_CTRL_SET_MAC_KEY:
363 switch (hctx->type) {
365 if ((!p2 && p1 > 0) || (p1 < -1))
367 if (!ASN1_OCTET_STRING_set(&hctx->raw_data.ktmp, p2, p1))
372 OSSL_PARAM params[2];
376 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
378 params[params_n] = OSSL_PARAM_construct_end();
380 return EVP_MAC_CTX_set_params(hctx->ctx, params);
384 /* This should be dead code */
389 case EVP_PKEY_CTRL_DIGESTINIT:
390 switch (hctx->type) {
392 /* Ensure that we have attached the implementation */
393 if (!EVP_MAC_init(hctx->ctx))
396 ASN1_OCTET_STRING *key =
397 (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
398 OSSL_PARAM params[4];
401 (char *)OBJ_nid2sn(EVP_MD_nid(hctx->raw_data.md));
402 #ifndef OPENSSL_NO_ENGINE
403 char *engineid = ctx->engine == NULL
404 ? NULL : (char *)ENGINE_get_id(ctx->engine);
406 if (engineid != NULL)
408 OSSL_PARAM_construct_utf8_string("engine", engineid, 0);
411 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
414 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
415 key->data, key->length);
416 params[params_n] = OSSL_PARAM_construct_end();
418 return EVP_MAC_CTX_set_params(hctx->ctx, params);
422 return -2; /* The mac types don't support ciphers */
424 /* This should be dead code */
436 static int pkey_mac_ctrl_str(EVP_PKEY_CTX *ctx,
437 const char *type, const char *value)
439 MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
440 const EVP_MAC *mac = EVP_MAC_CTX_mac(hctx->ctx);
441 OSSL_PARAM params[2];
445 * Translation of some control names that are equivalent to a single
448 * "md" and "digest" are the same thing, we use the single "digest"
450 * "digestsize" was a setting control in siphash, but naming wise,
451 * it's really the same as "size".
453 if (strcmp(type, "md") == 0)
454 type = OSSL_MAC_PARAM_DIGEST;
455 else if (strcmp(type, "digestsize") == 0)
456 type = OSSL_MAC_PARAM_SIZE;
458 if (!OSSL_PARAM_allocate_from_text(¶ms[0],
459 EVP_MAC_settable_ctx_params(mac),
460 type, value, strlen(value) + 1, NULL))
462 params[1] = OSSL_PARAM_construct_end();
463 ok = EVP_MAC_CTX_set_params(hctx->ctx, params);
464 OPENSSL_free(params[0].data);
468 static const EVP_PKEY_METHOD cmac_pkey_meth = {
470 EVP_PKEY_FLAG_SIGCTX_CUSTOM,
486 pkey_mac_signctx_init,
501 const EVP_PKEY_METHOD *cmac_pkey_method(void)
503 return &cmac_pkey_meth;
506 static const EVP_PKEY_METHOD hmac_pkey_meth = {
524 pkey_mac_signctx_init,
539 const EVP_PKEY_METHOD *hmac_pkey_method(void)
541 return &hmac_pkey_meth;
544 static const EVP_PKEY_METHOD siphash_pkey_meth = {
546 EVP_PKEY_FLAG_SIGCTX_CUSTOM,
562 pkey_mac_signctx_init,
577 const EVP_PKEY_METHOD *siphash_pkey_method(void)
579 return &siphash_pkey_meth;
582 static const EVP_PKEY_METHOD poly1305_pkey_meth = {
584 EVP_PKEY_FLAG_SIGCTX_CUSTOM,
600 pkey_mac_signctx_init,
615 const EVP_PKEY_METHOD *poly1305_pkey_method(void)
617 return &poly1305_pkey_meth;