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