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