]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/cmac/cm_meth.c
Change EVP_MAC method from copy to dup
[thirdparty/openssl.git] / crypto / cmac / cm_meth.c
1 /*
2 * Copyright 2018 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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/evp.h>
15 #include <openssl/cmac.h>
16 #include "internal/evp_int.h"
17
18 /* local CMAC pkey structure */
19
20 /* typedef EVP_MAC_IMPL */
21 struct evp_mac_impl_st {
22 /* tmpcipher and tmpengine are set to NULL after a CMAC_Init call */
23 const EVP_CIPHER *tmpcipher; /* cached CMAC cipher */
24 const ENGINE *tmpengine; /* cached CMAC cipher engine */
25 CMAC_CTX *ctx;
26 };
27
28 static EVP_MAC_IMPL *cmac_new(void)
29 {
30 EVP_MAC_IMPL *cctx;
31
32 if ((cctx = OPENSSL_zalloc(sizeof(*cctx))) == NULL
33 || (cctx->ctx = CMAC_CTX_new()) == NULL) {
34 OPENSSL_free(cctx);
35 cctx = NULL;
36 }
37
38 return cctx;
39 }
40
41 static void cmac_free(EVP_MAC_IMPL *cctx)
42 {
43 if (cctx != NULL) {
44 CMAC_CTX_free(cctx->ctx);
45 OPENSSL_free(cctx);
46 }
47 }
48
49 static EVP_MAC_IMPL *cmac_dup(const EVP_MAC_IMPL *csrc)
50 {
51 EVP_MAC_IMPL *cdst = cmac_new();
52
53 if (cdst == NULL)
54 return NULL;
55
56 if (!CMAC_CTX_copy(cdst->ctx, csrc->ctx)) {
57 cmac_free(cdst);
58 return NULL;
59 }
60
61 cdst->tmpengine = csrc->tmpengine;
62 cdst->tmpcipher = csrc->tmpcipher;
63
64 return cdst;
65 }
66
67 static size_t cmac_size(EVP_MAC_IMPL *cctx)
68 {
69 return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(cctx->ctx));
70 }
71
72 static int cmac_init(EVP_MAC_IMPL *cctx)
73 {
74 int rv = CMAC_Init(cctx->ctx, NULL, 0, cctx->tmpcipher,
75 (ENGINE *)cctx->tmpengine);
76 cctx->tmpcipher = NULL;
77 cctx->tmpengine = NULL;
78
79 return rv;
80 }
81
82 static int cmac_update(EVP_MAC_IMPL *cctx, const unsigned char *data,
83 size_t datalen)
84 {
85 return CMAC_Update(cctx->ctx, data, datalen);
86 }
87
88 static int cmac_final(EVP_MAC_IMPL *cctx, unsigned char *out)
89 {
90 size_t hlen;
91
92 return CMAC_Final(cctx->ctx, out, &hlen);
93 }
94
95 static int cmac_ctrl(EVP_MAC_IMPL *cctx, int cmd, va_list args)
96 {
97 switch (cmd) {
98 case EVP_MAC_CTRL_SET_KEY:
99 {
100 const unsigned char *key = va_arg(args, const unsigned char *);
101 size_t keylen = va_arg(args, size_t);
102 int rv = CMAC_Init(cctx->ctx, key, keylen, cctx->tmpcipher,
103 (ENGINE *)cctx->tmpengine);
104
105 cctx->tmpcipher = NULL;
106 cctx->tmpengine = NULL;
107
108 return rv;
109 }
110 break;
111 case EVP_MAC_CTRL_SET_CIPHER:
112 cctx->tmpcipher = va_arg(args, const EVP_CIPHER *);
113 break;
114 case EVP_MAC_CTRL_SET_ENGINE:
115 cctx->tmpengine = va_arg(args, const ENGINE *);
116 break;
117 default:
118 return -2;
119 }
120 return 1;
121 }
122
123 static int cmac_ctrl_int(EVP_MAC_IMPL *hctx, int cmd, ...)
124 {
125 int rv;
126 va_list args;
127
128 va_start(args, cmd);
129 rv = cmac_ctrl(hctx, cmd, args);
130 va_end(args);
131
132 return rv;
133 }
134
135 static int cmac_ctrl_str_cb(void *hctx, int cmd, void *buf, size_t buflen)
136 {
137 return cmac_ctrl_int(hctx, cmd, buf, buflen);
138 }
139
140 static int cmac_ctrl_str(EVP_MAC_IMPL *cctx, const char *type,
141 const char *value)
142 {
143 if (!value)
144 return 0;
145 if (strcmp(type, "cipher") == 0) {
146 const EVP_CIPHER *c = EVP_get_cipherbyname(value);
147
148 if (c == NULL)
149 return 0;
150 return cmac_ctrl_int(cctx, EVP_MAC_CTRL_SET_CIPHER, c);
151 }
152 if (strcmp(type, "key") == 0)
153 return EVP_str2ctrl(cmac_ctrl_str_cb, cctx, EVP_MAC_CTRL_SET_KEY,
154 value);
155 if (strcmp(type, "hexkey") == 0)
156 return EVP_hex2ctrl(cmac_ctrl_str_cb, cctx, EVP_MAC_CTRL_SET_KEY,
157 value);
158 return -2;
159 }
160
161 const EVP_MAC cmac_meth = {
162 EVP_MAC_CMAC,
163 cmac_new,
164 cmac_dup,
165 cmac_free,
166 cmac_size,
167 cmac_init,
168 cmac_update,
169 cmac_final,
170 cmac_ctrl,
171 cmac_ctrl_str
172 };