]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/macs/blake2_mac_impl.c
Ensure EVP_MAC_update() passes the length even if it is 0
[thirdparty/openssl.git] / providers / implementations / macs / blake2_mac_impl.c
CommitLineData
55a0a117 1/*
fbd2ece1 2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
55a0a117
RL
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
23c48d94 10#include <openssl/core_dispatch.h>
7f6b035b
MC
11#include <openssl/core_names.h>
12#include <openssl/params.h>
55a0a117 13
7c214f10 14#include "prov/blake2.h"
7f6b035b 15#include "internal/cryptlib.h"
ddd21319 16#include "prov/providercommonerr.h"
af3e7e1b 17#include "prov/implementations.h"
55a0a117
RL
18
19/*
20 * Forward declaration of everything implemented here. This is not strictly
21 * necessary for the compiler, but provides an assurance that the signatures
22 * of the functions in the dispatch table are correct.
23 */
363b1e5d
DMSP
24static OSSL_FUNC_mac_newctx_fn blake2_mac_new;
25static OSSL_FUNC_mac_dupctx_fn blake2_mac_dup;
26static OSSL_FUNC_mac_freectx_fn blake2_mac_free;
27static OSSL_FUNC_mac_gettable_ctx_params_fn blake2_gettable_ctx_params;
28static OSSL_FUNC_mac_get_ctx_params_fn blake2_get_ctx_params;
29static OSSL_FUNC_mac_settable_ctx_params_fn blake2_mac_settable_ctx_params;
30static OSSL_FUNC_mac_set_ctx_params_fn blake2_mac_set_ctx_params;
31static OSSL_FUNC_mac_init_fn blake2_mac_init;
32static OSSL_FUNC_mac_update_fn blake2_mac_update;
33static OSSL_FUNC_mac_final_fn blake2_mac_final;
55a0a117
RL
34
35struct blake2_mac_data_st {
36 BLAKE2_CTX ctx;
37 BLAKE2_PARAM params;
38 unsigned char key[BLAKE2_KEYBYTES];
39};
40
41static size_t blake2_mac_size(void *vmacctx);
42
43static void *blake2_mac_new(void *unused_provctx)
44{
45 struct blake2_mac_data_st *macctx = OPENSSL_zalloc(sizeof(*macctx));
46
47 if (macctx != NULL) {
48 BLAKE2_PARAM_INIT(&macctx->params);
49 /* ctx initialization is deferred to BLAKE2b_Init() */
50 }
51 return macctx;
52}
53
54static void *blake2_mac_dup(void *vsrc)
55{
56 struct blake2_mac_data_st *dst;
57 struct blake2_mac_data_st *src = vsrc;
58
59 dst = OPENSSL_zalloc(sizeof(*dst));
60 if (dst == NULL)
61 return NULL;
62
63 *dst = *src;
64 return dst;
65}
66
67static void blake2_mac_free(void *vmacctx)
68{
69 struct blake2_mac_data_st *macctx = vmacctx;
70
71 if (macctx != NULL) {
72 OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
73 OPENSSL_free(macctx);
74 }
75}
76
77static int blake2_mac_init(void *vmacctx)
78{
79 struct blake2_mac_data_st *macctx = vmacctx;
80
81 /* Check key has been set */
82 if (macctx->params.key_length == 0) {
83 ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
84 return 0;
85 }
86
87 return BLAKE2_INIT_KEY(&macctx->ctx, &macctx->params, macctx->key);
88}
89
90static int blake2_mac_update(void *vmacctx,
91 const unsigned char *data, size_t datalen)
92{
93 struct blake2_mac_data_st *macctx = vmacctx;
94
0bc193dd
MC
95 if (datalen == 0)
96 return 1;
97
55a0a117
RL
98 return BLAKE2_UPDATE(&macctx->ctx, data, datalen);
99}
100
101static int blake2_mac_final(void *vmacctx,
102 unsigned char *out, size_t *outl,
103 size_t outsize)
104{
105 struct blake2_mac_data_st *macctx = vmacctx;
106
5f6a0b2f 107 *outl = blake2_mac_size(macctx);
55a0a117
RL
108 return BLAKE2_FINAL(out, &macctx->ctx);
109}
110
111static const OSSL_PARAM known_gettable_ctx_params[] = {
703170d4 112 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
55a0a117
RL
113 OSSL_PARAM_END
114};
1017ab21 115static const OSSL_PARAM *blake2_gettable_ctx_params(ossl_unused void *provctx)
55a0a117
RL
116{
117 return known_gettable_ctx_params;
118}
119
92d9d0ae 120static int blake2_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
55a0a117
RL
121{
122 OSSL_PARAM *p;
123
703170d4 124 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
55a0a117
RL
125 return OSSL_PARAM_set_size_t(p, blake2_mac_size(vmacctx));
126
127 return 1;
128}
129
130static const OSSL_PARAM known_settable_ctx_params[] = {
55a0a117
RL
131 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
132 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
133 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0),
134 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_SALT, NULL, 0),
135 OSSL_PARAM_END
136};
1017ab21 137static const OSSL_PARAM *blake2_mac_settable_ctx_params(ossl_unused void *p_ctx)
55a0a117
RL
138{
139 return known_settable_ctx_params;
140}
141
142/*
143 * ALL parameters should be set before init().
144 */
92d9d0ae 145static int blake2_mac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
55a0a117
RL
146{
147 struct blake2_mac_data_st *macctx = vmacctx;
148 const OSSL_PARAM *p;
149
703170d4 150 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
55a0a117
RL
151 size_t size;
152
153 if (!OSSL_PARAM_get_size_t(p, &size)
154 || size < 1
155 || size > BLAKE2_OUTBYTES) {
156 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_XOF_OR_INVALID_LENGTH);
157 return 0;
158 }
159 BLAKE2_PARAM_SET_DIGEST_LENGTH(&macctx->params, (uint8_t)size);
160 }
161
162 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
163 size_t len;
164 void *key_p = macctx->key;
165
166 if (!OSSL_PARAM_get_octet_string(p, &key_p, BLAKE2_KEYBYTES, &len)) {
167 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
168 return 0;
169 }
170 /* Pad with zeroes at the end */
171 memset(macctx->key + len, 0, BLAKE2_KEYBYTES - len);
172
173 BLAKE2_PARAM_SET_KEY_LENGTH(&macctx->params, (uint8_t)len);
174 }
175
176 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CUSTOM))
177 != NULL) {
178 /*
179 * The OSSL_PARAM API doesn't provide direct pointer use, so we
180 * must handle the OSSL_PARAM structure ourselves here
181 */
182 if (p->data_size > BLAKE2_PERSONALBYTES) {
183 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);
184 return 0;
185 }
186 BLAKE2_PARAM_SET_PERSONAL(&macctx->params, p->data, p->data_size);
187 }
188
189 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SALT)) != NULL) {
190 /*
191 * The OSSL_PARAM API doesn't provide direct pointer use, so we
192 * must handle the OSSL_PARAM structure ourselves here as well
193 */
194 if (p->data_size > BLAKE2_SALTBYTES) {
195 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
196 return 0;
197 }
198 BLAKE2_PARAM_SET_SALT(&macctx->params, p->data, p->data_size);
199 }
200 return 1;
201}
202
203static size_t blake2_mac_size(void *vmacctx)
204{
205 struct blake2_mac_data_st *macctx = vmacctx;
206
207 return macctx->params.digest_length;
208}
209
210const OSSL_DISPATCH BLAKE2_FUNCTIONS[] = {
211 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))blake2_mac_new },
212 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))blake2_mac_dup },
213 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))blake2_mac_free },
214 { OSSL_FUNC_MAC_INIT, (void (*)(void))blake2_mac_init },
215 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))blake2_mac_update },
216 { OSSL_FUNC_MAC_FINAL, (void (*)(void))blake2_mac_final },
217 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
218 (void (*)(void))blake2_gettable_ctx_params },
92d9d0ae 219 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))blake2_get_ctx_params },
55a0a117
RL
220 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
221 (void (*)(void))blake2_mac_settable_ctx_params },
92d9d0ae 222 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))blake2_mac_set_ctx_params },
55a0a117
RL
223 { 0, NULL }
224};