]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/macs/siphash_prov.c
siphash: Fail finalization on uninitialized siphash context
[thirdparty/openssl.git] / providers / implementations / macs / siphash_prov.c
CommitLineData
4657693d 1/*
a28d06f3 2 * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
4657693d
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
7f6b035b 10#include <string.h>
23c48d94 11#include <openssl/core_dispatch.h>
7f6b035b
MC
12#include <openssl/core_names.h>
13#include <openssl/params.h>
14#include <openssl/evp.h>
15#include <openssl/err.h>
2741128e 16#include <openssl/proverr.h>
7f6b035b 17
25f2138b 18#include "crypto/siphash.h"
4657693d 19
af3e7e1b 20#include "prov/implementations.h"
5b104a81 21#include "prov/providercommon.h"
4657693d
RL
22
23/*
24 * Forward declaration of everything implemented here. This is not strictly
25 * necessary for the compiler, but provides an assurance that the signatures
26 * of the functions in the dispatch table are correct.
27 */
363b1e5d
DMSP
28static OSSL_FUNC_mac_newctx_fn siphash_new;
29static OSSL_FUNC_mac_dupctx_fn siphash_dup;
30static OSSL_FUNC_mac_freectx_fn siphash_free;
31static OSSL_FUNC_mac_gettable_ctx_params_fn siphash_gettable_ctx_params;
32static OSSL_FUNC_mac_get_ctx_params_fn siphash_get_ctx_params;
eee323c3 33static OSSL_FUNC_mac_settable_ctx_params_fn siphash_settable_ctx_params;
363b1e5d 34static OSSL_FUNC_mac_set_ctx_params_fn siphash_set_params;
363b1e5d
DMSP
35static OSSL_FUNC_mac_init_fn siphash_init;
36static OSSL_FUNC_mac_update_fn siphash_update;
37static OSSL_FUNC_mac_final_fn siphash_final;
4657693d
RL
38
39struct siphash_data_st {
40 void *provctx;
41 SIPHASH siphash; /* Siphash data */
c9ddc5af 42 SIPHASH sipcopy; /* Siphash data copy for reinitialization */
8f5d64b1 43 unsigned int crounds, drounds;
4657693d
RL
44};
45
8f5d64b1
P
46static unsigned int crounds(struct siphash_data_st *ctx)
47{
48 return ctx->crounds != 0 ? ctx->crounds : SIPHASH_C_ROUNDS;
49}
50
51static unsigned int drounds(struct siphash_data_st *ctx)
52{
53 return ctx->drounds != 0 ? ctx->drounds : SIPHASH_D_ROUNDS;
54}
55
4657693d
RL
56static void *siphash_new(void *provctx)
57{
5b104a81 58 struct siphash_data_st *ctx;
4657693d 59
5b104a81
P
60 if (!ossl_prov_is_running())
61 return NULL;
62 ctx = OPENSSL_zalloc(sizeof(*ctx));
41a6d557
P
63 if (ctx != NULL)
64 ctx->provctx = provctx;
4657693d
RL
65 return ctx;
66}
67
68static void siphash_free(void *vmacctx)
69{
70 OPENSSL_free(vmacctx);
71}
72
73static void *siphash_dup(void *vsrc)
74{
75 struct siphash_data_st *ssrc = vsrc;
5b104a81 76 struct siphash_data_st *sdst;
4657693d 77
5b104a81
P
78 if (!ossl_prov_is_running())
79 return NULL;
80 sdst = siphash_new(ssrc->provctx);
4657693d
RL
81 if (sdst == NULL)
82 return NULL;
83
84 sdst->siphash = ssrc->siphash;
85 return sdst;
86}
87
88static size_t siphash_size(void *vmacctx)
89{
90 struct siphash_data_st *ctx = vmacctx;
91
92 return SipHash_hash_size(&ctx->siphash);
93}
94
8f5d64b1
P
95static int siphash_setkey(struct siphash_data_st *ctx,
96 const unsigned char *key, size_t keylen)
4657693d 97{
c9ddc5af
TM
98 int ret;
99
8f5d64b1
P
100 if (keylen != SIPHASH_KEY_SIZE)
101 return 0;
c9ddc5af
TM
102 ret = SipHash_Init(&ctx->siphash, key, crounds(ctx), drounds(ctx));
103 if (ret)
104 ctx->sipcopy = ctx->siphash;
105 return ret;
8f5d64b1
P
106}
107
108static int siphash_init(void *vmacctx, const unsigned char *key, size_t keylen,
109 const OSSL_PARAM params[])
110{
111 struct siphash_data_st *ctx = vmacctx;
112
113 if (!ossl_prov_is_running() || !siphash_set_params(ctx, params))
114 return 0;
115 /* Without a key, there is not much to do here,
116 * The actual initialization happens through controls.
117 */
c9ddc5af
TM
118 if (key == NULL) {
119 ctx->siphash = ctx->sipcopy;
8f5d64b1 120 return 1;
c9ddc5af 121 }
8f5d64b1 122 return siphash_setkey(ctx, key, keylen);
4657693d
RL
123}
124
125static int siphash_update(void *vmacctx, const unsigned char *data,
8ce04db8 126 size_t datalen)
4657693d
RL
127{
128 struct siphash_data_st *ctx = vmacctx;
129
0bc193dd
MC
130 if (datalen == 0)
131 return 1;
132
4657693d
RL
133 SipHash_Update(&ctx->siphash, data, datalen);
134 return 1;
135}
136
137static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
138 size_t outsize)
139{
140 struct siphash_data_st *ctx = vmacctx;
141 size_t hlen = siphash_size(ctx);
142
5b104a81 143 if (!ossl_prov_is_running() || outsize < hlen)
4657693d
RL
144 return 0;
145
146 *outl = hlen;
147 return SipHash_Final(&ctx->siphash, out, hlen);
148}
149
eee323c3
P
150static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *ctx,
151 ossl_unused void *provctx)
4657693d 152{
8f5d64b1
P
153 static const OSSL_PARAM known_gettable_ctx_params[] = {
154 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
155 OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL),
156 OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL),
157 OSSL_PARAM_END
158 };
159
4657693d
RL
160 return known_gettable_ctx_params;
161}
162
92d9d0ae 163static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
4657693d 164{
8f5d64b1 165 struct siphash_data_st *ctx = vmacctx;
4657693d
RL
166 OSSL_PARAM *p;
167
8f5d64b1
P
168 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
169 && !OSSL_PARAM_set_size_t(p, siphash_size(vmacctx)))
170 return 0;
171 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_C_ROUNDS)) != NULL
172 && !OSSL_PARAM_set_uint(p, crounds(ctx)))
173 return 0;
174 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_D_ROUNDS)) != NULL
175 && !OSSL_PARAM_set_uint(p, drounds(ctx)))
176 return 0;
4657693d
RL
177 return 1;
178}
179
eee323c3
P
180static const OSSL_PARAM *siphash_settable_ctx_params(ossl_unused void *ctx,
181 void *provctx)
4657693d 182{
8f5d64b1
P
183 static const OSSL_PARAM known_settable_ctx_params[] = {
184 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
185 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
186 OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL),
187 OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL),
188 OSSL_PARAM_END
189 };
190
4657693d
RL
191 return known_settable_ctx_params;
192}
193
194static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
195{
196 struct siphash_data_st *ctx = vmacctx;
197 const OSSL_PARAM *p = NULL;
ae7d90a1 198 size_t size;
4657693d 199
5a6b62bb
P
200 if (params == NULL)
201 return 1;
202
703170d4 203 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
4657693d
RL
204 if (!OSSL_PARAM_get_size_t(p, &size)
205 || !SipHash_set_hash_size(&ctx->siphash, size))
206 return 0;
207 }
8f5d64b1
P
208 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_C_ROUNDS)) != NULL
209 && !OSSL_PARAM_get_uint(p, &ctx->crounds))
210 return 0;
211 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_D_ROUNDS)) != NULL
212 && !OSSL_PARAM_get_uint(p, &ctx->drounds))
213 return 0;
4657693d
RL
214 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
215 if (p->data_type != OSSL_PARAM_OCTET_STRING
8f5d64b1 216 || !siphash_setkey(ctx, p->data, p->data_size))
ae7d90a1 217 return 0;
4657693d
RL
218 return 1;
219}
220
1be63951 221const OSSL_DISPATCH ossl_siphash_functions[] = {
4657693d
RL
222 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
223 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
224 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
225 { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
226 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
227 { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
228 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
229 (void (*)(void))siphash_gettable_ctx_params },
92d9d0ae 230 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
4657693d 231 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
eee323c3 232 (void (*)(void))siphash_settable_ctx_params },
92d9d0ae 233 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
4657693d
RL
234 { 0, NULL }
235};