]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/macs/siphash_prov.c
30-test_evp.t: On no-dh, no-dsa, no-ec, no-sm2, and no-gost configurations disable...
[thirdparty/openssl.git] / providers / implementations / macs / siphash_prov.c
CommitLineData
4657693d 1/*
fbd2ece1 2 * Copyright 2018-2020 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>
16
25f2138b 17#include "crypto/siphash.h"
4657693d
RL
18/*
19 * TODO(3.0) when siphash has moved entirely to our providers, this
20 * header should be moved to the provider include directory. For the
21 * moment, crypto/siphash/siphash_ameth.c has us stuck.
22 */
7f6b035b 23#include "../../../crypto/siphash/siphash_local.h"
4657693d 24
ddd21319 25#include "prov/providercommonerr.h"
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;
39static OSSL_FUNC_mac_settable_ctx_params_fn siphash_settable_params;
40static OSSL_FUNC_mac_set_ctx_params_fn siphash_set_params;
41static OSSL_FUNC_mac_size_fn siphash_size;
42static OSSL_FUNC_mac_init_fn siphash_init;
43static OSSL_FUNC_mac_update_fn siphash_update;
44static OSSL_FUNC_mac_final_fn siphash_final;
4657693d
RL
45
46struct siphash_data_st {
47 void *provctx;
48 SIPHASH siphash; /* Siphash data */
49};
50
51static void *siphash_new(void *provctx)
52{
5b104a81 53 struct siphash_data_st *ctx;
4657693d 54
5b104a81
P
55 if (!ossl_prov_is_running())
56 return NULL;
57 ctx = OPENSSL_zalloc(sizeof(*ctx));
41a6d557
P
58 if (ctx != NULL)
59 ctx->provctx = provctx;
4657693d
RL
60 return ctx;
61}
62
63static void siphash_free(void *vmacctx)
64{
65 OPENSSL_free(vmacctx);
66}
67
68static void *siphash_dup(void *vsrc)
69{
70 struct siphash_data_st *ssrc = vsrc;
5b104a81 71 struct siphash_data_st *sdst;
4657693d 72
5b104a81
P
73 if (!ossl_prov_is_running())
74 return NULL;
75 sdst = siphash_new(ssrc->provctx);
4657693d
RL
76 if (sdst == NULL)
77 return NULL;
78
79 sdst->siphash = ssrc->siphash;
80 return sdst;
81}
82
83static size_t siphash_size(void *vmacctx)
84{
85 struct siphash_data_st *ctx = vmacctx;
86
87 return SipHash_hash_size(&ctx->siphash);
88}
89
90static int siphash_init(void *vmacctx)
91{
92 /* Not much to do here, actual initialization happens through controls */
5b104a81 93 return ossl_prov_is_running();
4657693d
RL
94}
95
96static int siphash_update(void *vmacctx, const unsigned char *data,
97 size_t datalen)
98{
99 struct siphash_data_st *ctx = vmacctx;
100
0bc193dd
MC
101 if (datalen == 0)
102 return 1;
103
4657693d
RL
104 SipHash_Update(&ctx->siphash, data, datalen);
105 return 1;
106}
107
108static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
109 size_t outsize)
110{
111 struct siphash_data_st *ctx = vmacctx;
112 size_t hlen = siphash_size(ctx);
113
5b104a81 114 if (!ossl_prov_is_running() || outsize < hlen)
4657693d
RL
115 return 0;
116
117 *outl = hlen;
118 return SipHash_Final(&ctx->siphash, out, hlen);
119}
120
121static const OSSL_PARAM known_gettable_ctx_params[] = {
703170d4 122 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
4657693d
RL
123 OSSL_PARAM_END
124};
1017ab21 125static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *provctx)
4657693d
RL
126{
127 return known_gettable_ctx_params;
128}
129
92d9d0ae 130static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
4657693d
RL
131{
132 OSSL_PARAM *p;
133
703170d4 134 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
4657693d
RL
135 return OSSL_PARAM_set_size_t(p, siphash_size(vmacctx));
136
137 return 1;
138}
139
140static const OSSL_PARAM known_settable_ctx_params[] = {
703170d4 141 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
4657693d
RL
142 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
143 OSSL_PARAM_END
144};
af5e1e85 145static const OSSL_PARAM *siphash_settable_params(void *provctx)
4657693d
RL
146{
147 return known_settable_ctx_params;
148}
149
150static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
151{
152 struct siphash_data_st *ctx = vmacctx;
153 const OSSL_PARAM *p = NULL;
154
703170d4 155 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
4657693d
RL
156 size_t size;
157
158 if (!OSSL_PARAM_get_size_t(p, &size)
159 || !SipHash_set_hash_size(&ctx->siphash, size))
160 return 0;
161 }
162 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
163 if (p->data_type != OSSL_PARAM_OCTET_STRING
164 || p->data_size != SIPHASH_KEY_SIZE
165 || !SipHash_Init(&ctx->siphash, p->data, 0, 0))
166 return 0;
167 return 1;
168}
169
170const OSSL_DISPATCH siphash_functions[] = {
171 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
172 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
173 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
174 { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
175 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
176 { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
177 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
178 (void (*)(void))siphash_gettable_ctx_params },
92d9d0ae 179 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
4657693d
RL
180 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
181 (void (*)(void))siphash_settable_params },
92d9d0ae 182 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
4657693d
RL
183 { 0, NULL }
184};