]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/macs/siphash_prov.c
rand: add FIPS error state handling
[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"
4657693d
RL
27
28/*
29 * Forward declaration of everything implemented here. This is not strictly
30 * necessary for the compiler, but provides an assurance that the signatures
31 * of the functions in the dispatch table are correct.
32 */
363b1e5d
DMSP
33static OSSL_FUNC_mac_newctx_fn siphash_new;
34static OSSL_FUNC_mac_dupctx_fn siphash_dup;
35static OSSL_FUNC_mac_freectx_fn siphash_free;
36static OSSL_FUNC_mac_gettable_ctx_params_fn siphash_gettable_ctx_params;
37static OSSL_FUNC_mac_get_ctx_params_fn siphash_get_ctx_params;
38static OSSL_FUNC_mac_settable_ctx_params_fn siphash_settable_params;
39static OSSL_FUNC_mac_set_ctx_params_fn siphash_set_params;
40static OSSL_FUNC_mac_size_fn siphash_size;
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{
52 struct siphash_data_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
53
41a6d557
P
54 if (ctx != NULL)
55 ctx->provctx = provctx;
4657693d
RL
56 return ctx;
57}
58
59static void siphash_free(void *vmacctx)
60{
61 OPENSSL_free(vmacctx);
62}
63
64static void *siphash_dup(void *vsrc)
65{
66 struct siphash_data_st *ssrc = vsrc;
67 struct siphash_data_st *sdst = siphash_new(ssrc->provctx);
68
69 if (sdst == NULL)
70 return NULL;
71
72 sdst->siphash = ssrc->siphash;
73 return sdst;
74}
75
76static size_t siphash_size(void *vmacctx)
77{
78 struct siphash_data_st *ctx = vmacctx;
79
80 return SipHash_hash_size(&ctx->siphash);
81}
82
83static int siphash_init(void *vmacctx)
84{
85 /* Not much to do here, actual initialization happens through controls */
86 return 1;
87}
88
89static int siphash_update(void *vmacctx, const unsigned char *data,
90 size_t datalen)
91{
92 struct siphash_data_st *ctx = vmacctx;
93
0bc193dd
MC
94 if (datalen == 0)
95 return 1;
96
4657693d
RL
97 SipHash_Update(&ctx->siphash, data, datalen);
98 return 1;
99}
100
101static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
102 size_t outsize)
103{
104 struct siphash_data_st *ctx = vmacctx;
105 size_t hlen = siphash_size(ctx);
106
107 if (outsize < hlen)
108 return 0;
109
110 *outl = hlen;
111 return SipHash_Final(&ctx->siphash, out, hlen);
112}
113
114static const OSSL_PARAM known_gettable_ctx_params[] = {
703170d4 115 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
4657693d
RL
116 OSSL_PARAM_END
117};
1017ab21 118static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *provctx)
4657693d
RL
119{
120 return known_gettable_ctx_params;
121}
122
92d9d0ae 123static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
4657693d
RL
124{
125 OSSL_PARAM *p;
126
703170d4 127 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
4657693d
RL
128 return OSSL_PARAM_set_size_t(p, siphash_size(vmacctx));
129
130 return 1;
131}
132
133static const OSSL_PARAM known_settable_ctx_params[] = {
703170d4 134 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
4657693d
RL
135 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
136 OSSL_PARAM_END
137};
af5e1e85 138static const OSSL_PARAM *siphash_settable_params(void *provctx)
4657693d
RL
139{
140 return known_settable_ctx_params;
141}
142
143static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
144{
145 struct siphash_data_st *ctx = vmacctx;
146 const OSSL_PARAM *p = NULL;
147
703170d4 148 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
4657693d
RL
149 size_t size;
150
151 if (!OSSL_PARAM_get_size_t(p, &size)
152 || !SipHash_set_hash_size(&ctx->siphash, size))
153 return 0;
154 }
155 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
156 if (p->data_type != OSSL_PARAM_OCTET_STRING
157 || p->data_size != SIPHASH_KEY_SIZE
158 || !SipHash_Init(&ctx->siphash, p->data, 0, 0))
159 return 0;
160 return 1;
161}
162
163const OSSL_DISPATCH siphash_functions[] = {
164 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
165 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
166 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
167 { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
168 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
169 { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
170 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
171 (void (*)(void))siphash_gettable_ctx_params },
92d9d0ae 172 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
4657693d
RL
173 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
174 (void (*)(void))siphash_settable_params },
92d9d0ae 175 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
4657693d
RL
176 { 0, NULL }
177};