]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/macs/siphash_prov.c
siphash: Add the C and D round parameters for SipHash.
[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
RL
19/*
20 * TODO(3.0) when siphash has moved entirely to our providers, this
21 * header should be moved to the provider include directory. For the
22 * moment, crypto/siphash/siphash_ameth.c has us stuck.
23 */
7f6b035b 24#include "../../../crypto/siphash/siphash_local.h"
4657693d 25
af3e7e1b 26#include "prov/implementations.h"
5b104a81 27#include "prov/providercommon.h"
4657693d
RL
28
29/*
30 * Forward declaration of everything implemented here. This is not strictly
31 * necessary for the compiler, but provides an assurance that the signatures
32 * of the functions in the dispatch table are correct.
33 */
363b1e5d
DMSP
34static OSSL_FUNC_mac_newctx_fn siphash_new;
35static OSSL_FUNC_mac_dupctx_fn siphash_dup;
36static OSSL_FUNC_mac_freectx_fn siphash_free;
37static OSSL_FUNC_mac_gettable_ctx_params_fn siphash_gettable_ctx_params;
38static OSSL_FUNC_mac_get_ctx_params_fn siphash_get_ctx_params;
eee323c3 39static OSSL_FUNC_mac_settable_ctx_params_fn siphash_settable_ctx_params;
363b1e5d 40static OSSL_FUNC_mac_set_ctx_params_fn siphash_set_params;
363b1e5d
DMSP
41static OSSL_FUNC_mac_init_fn siphash_init;
42static OSSL_FUNC_mac_update_fn siphash_update;
43static OSSL_FUNC_mac_final_fn siphash_final;
4657693d
RL
44
45struct siphash_data_st {
46 void *provctx;
47 SIPHASH siphash; /* Siphash data */
48};
49
50static void *siphash_new(void *provctx)
51{
5b104a81 52 struct siphash_data_st *ctx;
4657693d 53
5b104a81
P
54 if (!ossl_prov_is_running())
55 return NULL;
56 ctx = OPENSSL_zalloc(sizeof(*ctx));
41a6d557
P
57 if (ctx != NULL)
58 ctx->provctx = provctx;
4657693d
RL
59 return ctx;
60}
61
62static void siphash_free(void *vmacctx)
63{
64 OPENSSL_free(vmacctx);
65}
66
67static void *siphash_dup(void *vsrc)
68{
69 struct siphash_data_st *ssrc = vsrc;
5b104a81 70 struct siphash_data_st *sdst;
4657693d 71
5b104a81
P
72 if (!ossl_prov_is_running())
73 return NULL;
74 sdst = siphash_new(ssrc->provctx);
4657693d
RL
75 if (sdst == NULL)
76 return NULL;
77
78 sdst->siphash = ssrc->siphash;
79 return sdst;
80}
81
82static size_t siphash_size(void *vmacctx)
83{
84 struct siphash_data_st *ctx = vmacctx;
85
86 return SipHash_hash_size(&ctx->siphash);
87}
88
89static int siphash_init(void *vmacctx)
90{
5b104a81 91 return ossl_prov_is_running();
4657693d
RL
92}
93
94static int siphash_update(void *vmacctx, const unsigned char *data,
8ce04db8 95 size_t datalen)
4657693d
RL
96{
97 struct siphash_data_st *ctx = vmacctx;
98
0bc193dd
MC
99 if (datalen == 0)
100 return 1;
101
4657693d
RL
102 SipHash_Update(&ctx->siphash, data, datalen);
103 return 1;
104}
105
106static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
107 size_t outsize)
108{
109 struct siphash_data_st *ctx = vmacctx;
110 size_t hlen = siphash_size(ctx);
111
5b104a81 112 if (!ossl_prov_is_running() || outsize < hlen)
4657693d
RL
113 return 0;
114
115 *outl = hlen;
116 return SipHash_Final(&ctx->siphash, out, hlen);
117}
118
119static const OSSL_PARAM known_gettable_ctx_params[] = {
703170d4 120 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
4657693d
RL
121 OSSL_PARAM_END
122};
eee323c3
P
123static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *ctx,
124 ossl_unused void *provctx)
4657693d
RL
125{
126 return known_gettable_ctx_params;
127}
128
92d9d0ae 129static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
4657693d
RL
130{
131 OSSL_PARAM *p;
132
703170d4 133 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
4657693d
RL
134 return OSSL_PARAM_set_size_t(p, siphash_size(vmacctx));
135
136 return 1;
137}
138
139static const OSSL_PARAM known_settable_ctx_params[] = {
703170d4 140 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
4657693d 141 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
ae7d90a1
P
142 OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL),
143 OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL),
4657693d
RL
144 OSSL_PARAM_END
145};
eee323c3
P
146
147static const OSSL_PARAM *siphash_settable_ctx_params(ossl_unused void *ctx,
148 void *provctx)
4657693d
RL
149{
150 return known_settable_ctx_params;
151}
152
153static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
154{
155 struct siphash_data_st *ctx = vmacctx;
156 const OSSL_PARAM *p = NULL;
ae7d90a1
P
157 unsigned int u;
158 size_t size;
4657693d 159
703170d4 160 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
4657693d
RL
161 if (!OSSL_PARAM_get_size_t(p, &size)
162 || !SipHash_set_hash_size(&ctx->siphash, size))
163 return 0;
164 }
165 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
166 if (p->data_type != OSSL_PARAM_OCTET_STRING
167 || p->data_size != SIPHASH_KEY_SIZE
168 || !SipHash_Init(&ctx->siphash, p->data, 0, 0))
169 return 0;
ae7d90a1
P
170 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_C_ROUNDS)) != NULL) {
171 if (!OSSL_PARAM_get_uint(p, &ctx->siphash.crounds))
172 return 0;
173 if (ctx->siphash.crounds == 0)
174 ctx->siphash.crounds = SIPHASH_C_ROUNDS;
175 }
176 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_D_ROUNDS)) != NULL) {
177 if (!OSSL_PARAM_get_uint(p, &ctx->siphash.drounds))
178 return 0;
179 if (ctx->siphash.drounds == 0)
180 ctx->siphash.drounds = SIPHASH_D_ROUNDS;
181 }
4657693d
RL
182 return 1;
183}
184
1be63951 185const OSSL_DISPATCH ossl_siphash_functions[] = {
4657693d
RL
186 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
187 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
188 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
189 { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
190 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
191 { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
192 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
193 (void (*)(void))siphash_gettable_ctx_params },
92d9d0ae 194 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
4657693d 195 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
eee323c3 196 (void (*)(void))siphash_settable_ctx_params },
92d9d0ae 197 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
4657693d
RL
198 { 0, NULL }
199};