xref: /openbsd-src/lib/libcrypto/cmac/cm_pmeth.c (revision d6237f54e72b691997986b4b8b4e3cda78a61464)
1*d6237f54Stb /* $OpenBSD: cm_pmeth.c,v 1.12 2023/12/28 21:56:12 tb Exp $ */
2ec07fdf1Sdjm /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3ec07fdf1Sdjm  * project 2010.
4ec07fdf1Sdjm  */
5ec07fdf1Sdjm /* ====================================================================
6ec07fdf1Sdjm  * Copyright (c) 2010 The OpenSSL Project.  All rights reserved.
7ec07fdf1Sdjm  *
8ec07fdf1Sdjm  * Redistribution and use in source and binary forms, with or without
9ec07fdf1Sdjm  * modification, are permitted provided that the following conditions
10ec07fdf1Sdjm  * are met:
11ec07fdf1Sdjm  *
12ec07fdf1Sdjm  * 1. Redistributions of source code must retain the above copyright
13ec07fdf1Sdjm  *    notice, this list of conditions and the following disclaimer.
14ec07fdf1Sdjm  *
15ec07fdf1Sdjm  * 2. Redistributions in binary form must reproduce the above copyright
16ec07fdf1Sdjm  *    notice, this list of conditions and the following disclaimer in
17ec07fdf1Sdjm  *    the documentation and/or other materials provided with the
18ec07fdf1Sdjm  *    distribution.
19ec07fdf1Sdjm  *
20ec07fdf1Sdjm  * 3. All advertising materials mentioning features or use of this
21ec07fdf1Sdjm  *    software must display the following acknowledgment:
22ec07fdf1Sdjm  *    "This product includes software developed by the OpenSSL Project
23ec07fdf1Sdjm  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24ec07fdf1Sdjm  *
25ec07fdf1Sdjm  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26ec07fdf1Sdjm  *    endorse or promote products derived from this software without
27ec07fdf1Sdjm  *    prior written permission. For written permission, please contact
28ec07fdf1Sdjm  *    licensing@OpenSSL.org.
29ec07fdf1Sdjm  *
30ec07fdf1Sdjm  * 5. Products derived from this software may not be called "OpenSSL"
31ec07fdf1Sdjm  *    nor may "OpenSSL" appear in their names without prior written
32ec07fdf1Sdjm  *    permission of the OpenSSL Project.
33ec07fdf1Sdjm  *
34ec07fdf1Sdjm  * 6. Redistributions of any form whatsoever must retain the following
35ec07fdf1Sdjm  *    acknowledgment:
36ec07fdf1Sdjm  *    "This product includes software developed by the OpenSSL Project
37ec07fdf1Sdjm  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38ec07fdf1Sdjm  *
39ec07fdf1Sdjm  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40ec07fdf1Sdjm  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41ec07fdf1Sdjm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42ec07fdf1Sdjm  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43ec07fdf1Sdjm  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44ec07fdf1Sdjm  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45ec07fdf1Sdjm  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46ec07fdf1Sdjm  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47ec07fdf1Sdjm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48ec07fdf1Sdjm  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49ec07fdf1Sdjm  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50ec07fdf1Sdjm  * OF THE POSSIBILITY OF SUCH DAMAGE.
51ec07fdf1Sdjm  * ====================================================================
52ec07fdf1Sdjm  */
53ec07fdf1Sdjm 
54ec07fdf1Sdjm #include <stdio.h>
55a8913c44Sjsing #include <string.h>
56a8913c44Sjsing 
57b6ab114eSjsing #include <openssl/cmac.h>
58b6ab114eSjsing #include <openssl/evp.h>
59ec07fdf1Sdjm #include <openssl/x509.h>
60ec07fdf1Sdjm #include <openssl/x509v3.h>
61b6ab114eSjsing 
62c9675a23Stb #include "evp_local.h"
63ec07fdf1Sdjm 
64ec07fdf1Sdjm /* The context structure and "key" is simply a CMAC_CTX */
65ec07fdf1Sdjm 
6645c944e0Smiod static int
pkey_cmac_init(EVP_PKEY_CTX * ctx)6745c944e0Smiod pkey_cmac_init(EVP_PKEY_CTX *ctx)
68ec07fdf1Sdjm {
69ec07fdf1Sdjm 	ctx->data = CMAC_CTX_new();
70ec07fdf1Sdjm 	if (!ctx->data)
71ec07fdf1Sdjm 		return 0;
72ec07fdf1Sdjm 	ctx->keygen_info_count = 0;
73ec07fdf1Sdjm 	return 1;
74ec07fdf1Sdjm }
75ec07fdf1Sdjm 
7645c944e0Smiod static int
pkey_cmac_copy(EVP_PKEY_CTX * dst,EVP_PKEY_CTX * src)7745c944e0Smiod pkey_cmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
78ec07fdf1Sdjm {
79ec07fdf1Sdjm 	if (!pkey_cmac_init(dst))
80ec07fdf1Sdjm 		return 0;
81ec07fdf1Sdjm 	if (!CMAC_CTX_copy(dst->data, src->data))
82ec07fdf1Sdjm 		return 0;
83ec07fdf1Sdjm 	return 1;
84ec07fdf1Sdjm }
85ec07fdf1Sdjm 
8645c944e0Smiod static void
pkey_cmac_cleanup(EVP_PKEY_CTX * ctx)8745c944e0Smiod pkey_cmac_cleanup(EVP_PKEY_CTX *ctx)
88ec07fdf1Sdjm {
89ec07fdf1Sdjm 	CMAC_CTX_free(ctx->data);
90ec07fdf1Sdjm }
91ec07fdf1Sdjm 
9245c944e0Smiod static int
pkey_cmac_keygen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)9345c944e0Smiod pkey_cmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
94ec07fdf1Sdjm {
95*d6237f54Stb 	CMAC_CTX *cmkey;
96*d6237f54Stb 	int ret = 0;
9745c944e0Smiod 
98*d6237f54Stb 	if ((cmkey = CMAC_CTX_new()) == NULL)
99*d6237f54Stb 		goto err;
100*d6237f54Stb 	if (!CMAC_CTX_copy(cmkey, ctx->data))
101*d6237f54Stb 		goto err;
102*d6237f54Stb 	if (!EVP_PKEY_assign(pkey, EVP_PKEY_CMAC, cmkey))
103*d6237f54Stb 		goto err;
104*d6237f54Stb 	cmkey = NULL;
105*d6237f54Stb 
106*d6237f54Stb 	ret = 1;
107*d6237f54Stb 
108*d6237f54Stb  err:
109ec07fdf1Sdjm 	CMAC_CTX_free(cmkey);
110ec07fdf1Sdjm 
111*d6237f54Stb 	return ret;
112ec07fdf1Sdjm }
113ec07fdf1Sdjm 
11445c944e0Smiod static int
int_update(EVP_MD_CTX * ctx,const void * data,size_t count)11545c944e0Smiod int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
116ec07fdf1Sdjm {
117ec07fdf1Sdjm 	if (!CMAC_Update(ctx->pctx->data, data, count))
118ec07fdf1Sdjm 		return 0;
119ec07fdf1Sdjm 	return 1;
120ec07fdf1Sdjm }
121ec07fdf1Sdjm 
12245c944e0Smiod static int
cmac_signctx_init(EVP_PKEY_CTX * ctx,EVP_MD_CTX * mctx)12345c944e0Smiod cmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
124ec07fdf1Sdjm {
125ec07fdf1Sdjm 	EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
126ec07fdf1Sdjm 	mctx->update = int_update;
127ec07fdf1Sdjm 	return 1;
128ec07fdf1Sdjm }
129ec07fdf1Sdjm 
13045c944e0Smiod static int
cmac_signctx(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,EVP_MD_CTX * mctx)13145c944e0Smiod cmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
132ec07fdf1Sdjm     EVP_MD_CTX *mctx)
133ec07fdf1Sdjm {
134ec07fdf1Sdjm 	return CMAC_Final(ctx->data, sig, siglen);
135ec07fdf1Sdjm }
136ec07fdf1Sdjm 
13745c944e0Smiod static int
pkey_cmac_ctrl(EVP_PKEY_CTX * ctx,int type,int p1,void * p2)13845c944e0Smiod pkey_cmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
139ec07fdf1Sdjm {
140ec07fdf1Sdjm 	CMAC_CTX *cmctx = ctx->data;
141ec07fdf1Sdjm 
14245c944e0Smiod 	switch (type) {
143ec07fdf1Sdjm 	case EVP_PKEY_CTRL_SET_MAC_KEY:
144ec07fdf1Sdjm 		if (!p2 || p1 < 0)
145ec07fdf1Sdjm 			return 0;
146ec07fdf1Sdjm 		if (!CMAC_Init(cmctx, p2, p1, NULL, NULL))
147ec07fdf1Sdjm 			return 0;
148ec07fdf1Sdjm 		break;
149ec07fdf1Sdjm 
150ec07fdf1Sdjm 	case EVP_PKEY_CTRL_CIPHER:
151592331b2Stb 		if (!CMAC_Init(cmctx, NULL, 0, p2, NULL))
152ec07fdf1Sdjm 			return 0;
153ec07fdf1Sdjm 		break;
154ec07fdf1Sdjm 
155ec07fdf1Sdjm 	case EVP_PKEY_CTRL_MD:
1560ee28c09Stb 		if (ctx->pkey && !CMAC_CTX_copy(ctx->data, ctx->pkey->pkey.ptr))
157ec07fdf1Sdjm 			return 0;
158ec07fdf1Sdjm 		if (!CMAC_Init(cmctx, NULL, 0, NULL, NULL))
159ec07fdf1Sdjm 			return 0;
160ec07fdf1Sdjm 		break;
161ec07fdf1Sdjm 
162ec07fdf1Sdjm 	default:
163ec07fdf1Sdjm 		return -2;
164ec07fdf1Sdjm 	}
165ec07fdf1Sdjm 	return 1;
166ec07fdf1Sdjm }
167ec07fdf1Sdjm 
16845c944e0Smiod static int
pkey_cmac_ctrl_str(EVP_PKEY_CTX * ctx,const char * type,const char * value)16945c944e0Smiod pkey_cmac_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
170ec07fdf1Sdjm {
171ec07fdf1Sdjm 	if (!value)
172ec07fdf1Sdjm 		return 0;
17345c944e0Smiod 	if (!strcmp(type, "key")) {
174ec07fdf1Sdjm 		void *p = (void *)value;
175ec07fdf1Sdjm 		return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
176ec07fdf1Sdjm 		    strlen(p), p);
177ec07fdf1Sdjm 	}
17845c944e0Smiod 	if (!strcmp(type, "cipher")) {
179ec07fdf1Sdjm 		const EVP_CIPHER *c;
18045c944e0Smiod 
181ec07fdf1Sdjm 		c = EVP_get_cipherbyname(value);
182ec07fdf1Sdjm 		if (!c)
183ec07fdf1Sdjm 			return 0;
184ec07fdf1Sdjm 		return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_CIPHER, -1, (void *)c);
185ec07fdf1Sdjm 	}
18645c944e0Smiod 	if (!strcmp(type, "hexkey")) {
187ec07fdf1Sdjm 		unsigned char *key;
188ec07fdf1Sdjm 		int r;
189ec07fdf1Sdjm 		long keylen;
19045c944e0Smiod 
191ec07fdf1Sdjm 		key = string_to_hex(value, &keylen);
192ec07fdf1Sdjm 		if (!key)
193ec07fdf1Sdjm 			return 0;
194ec07fdf1Sdjm 		r = pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
1956f3a6cb1Sbeck 		free(key);
196ec07fdf1Sdjm 		return r;
197ec07fdf1Sdjm 	}
19845c944e0Smiod 
199ec07fdf1Sdjm 	return -2;
200ec07fdf1Sdjm }
201ec07fdf1Sdjm 
202564bda7fSjsing const EVP_PKEY_METHOD cmac_pkey_meth = {
203e402ce74Smiod 	.pkey_id = EVP_PKEY_CMAC,
204e402ce74Smiod 	.flags = EVP_PKEY_FLAG_SIGCTX_CUSTOM,
205ec07fdf1Sdjm 
206e402ce74Smiod 	.init = pkey_cmac_init,
207e402ce74Smiod 	.copy = pkey_cmac_copy,
208e402ce74Smiod 	.cleanup = pkey_cmac_cleanup,
209ec07fdf1Sdjm 
210e402ce74Smiod 	.keygen = pkey_cmac_keygen,
211ec07fdf1Sdjm 
212e402ce74Smiod 	.signctx_init = cmac_signctx_init,
213e402ce74Smiod 	.signctx = cmac_signctx,
214ec07fdf1Sdjm 
215e402ce74Smiod 	.ctrl = pkey_cmac_ctrl,
216e402ce74Smiod 	.ctrl_str = pkey_cmac_ctrl_str
217ec07fdf1Sdjm };
218