1c9496f6bSchristos /*
2*4724848cSchristos * Copyright 2010-2016 The OpenSSL Project Authors. All Rights Reserved.
3c9496f6bSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8c9496f6bSchristos */
9c9496f6bSchristos
10c9496f6bSchristos #include <stdio.h>
11*4724848cSchristos #include "internal/cryptlib.h"
12c9496f6bSchristos #include <openssl/x509.h>
13c9496f6bSchristos #include <openssl/x509v3.h>
14c9496f6bSchristos #include <openssl/evp.h>
15c9496f6bSchristos #include <openssl/cmac.h>
16*4724848cSchristos #include "crypto/evp.h"
17c9496f6bSchristos
18c9496f6bSchristos /* The context structure and "key" is simply a CMAC_CTX */
19c9496f6bSchristos
pkey_cmac_init(EVP_PKEY_CTX * ctx)20c9496f6bSchristos static int pkey_cmac_init(EVP_PKEY_CTX *ctx)
21c9496f6bSchristos {
22c9496f6bSchristos ctx->data = CMAC_CTX_new();
23*4724848cSchristos if (ctx->data == NULL)
24c9496f6bSchristos return 0;
25c9496f6bSchristos ctx->keygen_info_count = 0;
26c9496f6bSchristos return 1;
27c9496f6bSchristos }
28c9496f6bSchristos
pkey_cmac_copy(EVP_PKEY_CTX * dst,EVP_PKEY_CTX * src)29c9496f6bSchristos static int pkey_cmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
30c9496f6bSchristos {
31c9496f6bSchristos if (!pkey_cmac_init(dst))
32c9496f6bSchristos return 0;
33c9496f6bSchristos if (!CMAC_CTX_copy(dst->data, src->data))
34c9496f6bSchristos return 0;
35c9496f6bSchristos return 1;
36c9496f6bSchristos }
37c9496f6bSchristos
pkey_cmac_cleanup(EVP_PKEY_CTX * ctx)38c9496f6bSchristos static void pkey_cmac_cleanup(EVP_PKEY_CTX *ctx)
39c9496f6bSchristos {
40c9496f6bSchristos CMAC_CTX_free(ctx->data);
41c9496f6bSchristos }
42c9496f6bSchristos
pkey_cmac_keygen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)43c9496f6bSchristos static int pkey_cmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
44c9496f6bSchristos {
45c9496f6bSchristos CMAC_CTX *cmkey = CMAC_CTX_new();
46c9496f6bSchristos CMAC_CTX *cmctx = ctx->data;
47*4724848cSchristos if (cmkey == NULL)
48c9496f6bSchristos return 0;
49c9496f6bSchristos if (!CMAC_CTX_copy(cmkey, cmctx)) {
50c9496f6bSchristos CMAC_CTX_free(cmkey);
51c9496f6bSchristos return 0;
52c9496f6bSchristos }
53c9496f6bSchristos EVP_PKEY_assign(pkey, EVP_PKEY_CMAC, cmkey);
54c9496f6bSchristos
55c9496f6bSchristos return 1;
56c9496f6bSchristos }
57c9496f6bSchristos
int_update(EVP_MD_CTX * ctx,const void * data,size_t count)58c9496f6bSchristos static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
59c9496f6bSchristos {
60*4724848cSchristos if (!CMAC_Update(EVP_MD_CTX_pkey_ctx(ctx)->data, data, count))
61c9496f6bSchristos return 0;
62c9496f6bSchristos return 1;
63c9496f6bSchristos }
64c9496f6bSchristos
cmac_signctx_init(EVP_PKEY_CTX * ctx,EVP_MD_CTX * mctx)65c9496f6bSchristos static int cmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
66c9496f6bSchristos {
67c9496f6bSchristos EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
68*4724848cSchristos EVP_MD_CTX_set_update_fn(mctx, int_update);
69c9496f6bSchristos return 1;
70c9496f6bSchristos }
71c9496f6bSchristos
cmac_signctx(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,EVP_MD_CTX * mctx)72c9496f6bSchristos static int cmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
73c9496f6bSchristos EVP_MD_CTX *mctx)
74c9496f6bSchristos {
75c9496f6bSchristos return CMAC_Final(ctx->data, sig, siglen);
76c9496f6bSchristos }
77c9496f6bSchristos
pkey_cmac_ctrl(EVP_PKEY_CTX * ctx,int type,int p1,void * p2)78c9496f6bSchristos static int pkey_cmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
79c9496f6bSchristos {
80c9496f6bSchristos CMAC_CTX *cmctx = ctx->data;
81c9496f6bSchristos switch (type) {
82c9496f6bSchristos
83c9496f6bSchristos case EVP_PKEY_CTRL_SET_MAC_KEY:
84c9496f6bSchristos if (!p2 || p1 < 0)
85c9496f6bSchristos return 0;
86c9496f6bSchristos if (!CMAC_Init(cmctx, p2, p1, NULL, NULL))
87c9496f6bSchristos return 0;
88c9496f6bSchristos break;
89c9496f6bSchristos
90c9496f6bSchristos case EVP_PKEY_CTRL_CIPHER:
91c9496f6bSchristos if (!CMAC_Init(cmctx, NULL, 0, p2, ctx->engine))
92c9496f6bSchristos return 0;
93c9496f6bSchristos break;
94c9496f6bSchristos
95c9496f6bSchristos case EVP_PKEY_CTRL_MD:
96c9496f6bSchristos if (ctx->pkey && !CMAC_CTX_copy(ctx->data,
97c9496f6bSchristos (CMAC_CTX *)ctx->pkey->pkey.ptr))
98c9496f6bSchristos return 0;
99c9496f6bSchristos if (!CMAC_Init(cmctx, NULL, 0, NULL, NULL))
100c9496f6bSchristos return 0;
101c9496f6bSchristos break;
102c9496f6bSchristos
103c9496f6bSchristos default:
104c9496f6bSchristos return -2;
105c9496f6bSchristos
106c9496f6bSchristos }
107c9496f6bSchristos return 1;
108c9496f6bSchristos }
109c9496f6bSchristos
pkey_cmac_ctrl_str(EVP_PKEY_CTX * ctx,const char * type,const char * value)110c9496f6bSchristos static int pkey_cmac_ctrl_str(EVP_PKEY_CTX *ctx,
111c9496f6bSchristos const char *type, const char *value)
112c9496f6bSchristos {
113c9496f6bSchristos if (!value) {
114c9496f6bSchristos return 0;
115c9496f6bSchristos }
116*4724848cSchristos if (strcmp(type, "cipher") == 0) {
117c9496f6bSchristos const EVP_CIPHER *c;
118c9496f6bSchristos c = EVP_get_cipherbyname(value);
119c9496f6bSchristos if (!c)
120c9496f6bSchristos return 0;
121c9496f6bSchristos return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_CIPHER, -1, (void *)c);
122c9496f6bSchristos }
123*4724848cSchristos if (strcmp(type, "key") == 0)
124*4724848cSchristos return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
125*4724848cSchristos if (strcmp(type, "hexkey") == 0)
126*4724848cSchristos return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
127c9496f6bSchristos return -2;
128c9496f6bSchristos }
129c9496f6bSchristos
130c9496f6bSchristos const EVP_PKEY_METHOD cmac_pkey_meth = {
131c9496f6bSchristos EVP_PKEY_CMAC,
132c9496f6bSchristos EVP_PKEY_FLAG_SIGCTX_CUSTOM,
133c9496f6bSchristos pkey_cmac_init,
134c9496f6bSchristos pkey_cmac_copy,
135c9496f6bSchristos pkey_cmac_cleanup,
136c9496f6bSchristos
137c9496f6bSchristos 0, 0,
138c9496f6bSchristos
139c9496f6bSchristos 0,
140c9496f6bSchristos pkey_cmac_keygen,
141c9496f6bSchristos
142c9496f6bSchristos 0, 0,
143c9496f6bSchristos
144c9496f6bSchristos 0, 0,
145c9496f6bSchristos
146c9496f6bSchristos 0, 0,
147c9496f6bSchristos
148c9496f6bSchristos cmac_signctx_init,
149c9496f6bSchristos cmac_signctx,
150c9496f6bSchristos
151c9496f6bSchristos 0, 0,
152c9496f6bSchristos
153c9496f6bSchristos 0, 0,
154c9496f6bSchristos
155c9496f6bSchristos 0, 0,
156c9496f6bSchristos
157c9496f6bSchristos 0, 0,
158c9496f6bSchristos
159c9496f6bSchristos pkey_cmac_ctrl,
160c9496f6bSchristos pkey_cmac_ctrl_str
161c9496f6bSchristos };
162