]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/default/macs/siphash_prov.c
Rename ctx_{get,set}_params to {get,set}_ctx_params
[thirdparty/openssl.git] / providers / default / macs / siphash_prov.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_SIPHASH
12
13 # include <string.h>
14 # include <openssl/core_numbers.h>
15 # include <openssl/core_names.h>
16 # include <openssl/params.h>
17 # include <openssl/evp.h>
18 # include <openssl/err.h>
19
20 # include "internal/siphash.h"
21 /*
22 * TODO(3.0) when siphash has moved entirely to our providers, this
23 * header should be moved to the provider include directory. For the
24 * moment, crypto/siphash/siphash_ameth.c has us stuck.
25 */
26 # include "../../../crypto/siphash/siphash_local.h"
27
28 # include "internal/providercommonerr.h"
29 # include "internal/provider_algs.h"
30
31 /*
32 * Forward declaration of everything implemented here. This is not strictly
33 * necessary for the compiler, but provides an assurance that the signatures
34 * of the functions in the dispatch table are correct.
35 */
36 static OSSL_OP_mac_newctx_fn siphash_new;
37 static OSSL_OP_mac_dupctx_fn siphash_dup;
38 static OSSL_OP_mac_freectx_fn siphash_free;
39 static OSSL_OP_mac_gettable_ctx_params_fn siphash_gettable_ctx_params;
40 static OSSL_OP_mac_get_ctx_params_fn siphash_get_ctx_params;
41 static OSSL_OP_mac_settable_ctx_params_fn siphash_settable_params;
42 static OSSL_OP_mac_set_ctx_params_fn siphash_set_params;
43 static OSSL_OP_mac_size_fn siphash_size;
44 static OSSL_OP_mac_init_fn siphash_init;
45 static OSSL_OP_mac_update_fn siphash_update;
46 static OSSL_OP_mac_final_fn siphash_final;
47
48 struct siphash_data_st {
49 void *provctx;
50 SIPHASH siphash; /* Siphash data */
51 };
52
53 static void *siphash_new(void *provctx)
54 {
55 struct siphash_data_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
56
57 ctx->provctx = provctx;
58 return ctx;
59 }
60
61 static void siphash_free(void *vmacctx)
62 {
63 OPENSSL_free(vmacctx);
64 }
65
66 static void *siphash_dup(void *vsrc)
67 {
68 struct siphash_data_st *ssrc = vsrc;
69 struct siphash_data_st *sdst = siphash_new(ssrc->provctx);
70
71 if (sdst == NULL)
72 return NULL;
73
74 sdst->siphash = ssrc->siphash;
75 return sdst;
76 }
77
78 static size_t siphash_size(void *vmacctx)
79 {
80 struct siphash_data_st *ctx = vmacctx;
81
82 return SipHash_hash_size(&ctx->siphash);
83 }
84
85 static int siphash_init(void *vmacctx)
86 {
87 /* Not much to do here, actual initialization happens through controls */
88 return 1;
89 }
90
91 static int siphash_update(void *vmacctx, const unsigned char *data,
92 size_t datalen)
93 {
94 struct siphash_data_st *ctx = vmacctx;
95
96 SipHash_Update(&ctx->siphash, data, datalen);
97 return 1;
98 }
99
100 static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
101 size_t outsize)
102 {
103 struct siphash_data_st *ctx = vmacctx;
104 size_t hlen = siphash_size(ctx);
105
106 if (outsize < hlen)
107 return 0;
108
109 *outl = hlen;
110 return SipHash_Final(&ctx->siphash, out, hlen);
111 }
112
113 static const OSSL_PARAM known_gettable_ctx_params[] = {
114 OSSL_PARAM_size_t(OSSL_MAC_PARAM_OUTLEN, NULL),
115 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), /* Same as "outlen" */
116 OSSL_PARAM_size_t(OSSL_MAC_PARAM_DIGESTSIZE, NULL), /* Same as "outlen" */
117 OSSL_PARAM_END
118 };
119 static const OSSL_PARAM *siphash_gettable_ctx_params(void)
120 {
121 return known_gettable_ctx_params;
122 }
123
124 static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
125 {
126 OSSL_PARAM *p;
127
128 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_OUTLEN)) != NULL
129 || (p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
130 || (p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_DIGESTSIZE)) != NULL)
131 return OSSL_PARAM_set_size_t(p, siphash_size(vmacctx));
132
133 return 1;
134 }
135
136 static const OSSL_PARAM known_settable_ctx_params[] = {
137 OSSL_PARAM_size_t(OSSL_MAC_PARAM_OUTLEN, NULL),
138 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), /* Same as "outlen" */
139 OSSL_PARAM_size_t(OSSL_MAC_PARAM_DIGESTSIZE, NULL), /* Same as "outlen" */
140 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
141 OSSL_PARAM_END
142 };
143 static const OSSL_PARAM *siphash_settable_params(void)
144 {
145 return known_settable_ctx_params;
146 }
147
148 static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
149 {
150 struct siphash_data_st *ctx = vmacctx;
151 const OSSL_PARAM *p = NULL;
152
153 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_OUTLEN)) != NULL
154 || ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_DIGESTSIZE))
155 != NULL)
156 || ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE))
157 != NULL)) {
158 size_t size;
159
160 if (!OSSL_PARAM_get_size_t(p, &size)
161 || !SipHash_set_hash_size(&ctx->siphash, size))
162 return 0;
163 }
164 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
165 if (p->data_type != OSSL_PARAM_OCTET_STRING
166 || p->data_size != SIPHASH_KEY_SIZE
167 || !SipHash_Init(&ctx->siphash, p->data, 0, 0))
168 return 0;
169 return 1;
170 }
171
172 const OSSL_DISPATCH siphash_functions[] = {
173 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
174 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
175 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
176 { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
177 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
178 { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
179 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
180 (void (*)(void))siphash_gettable_ctx_params },
181 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
182 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
183 (void (*)(void))siphash_settable_params },
184 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
185 { 0, NULL }
186 };
187
188 #endif