Cleanup: move remaining providers/common/include/internal/*.h
[openssl.git] / providers / implementations / 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 <string.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/params.h>
14 #include <openssl/evp.h>
15 #include <openssl/err.h>
16
17 #include "crypto/siphash.h"
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  */
23 #include "../../../crypto/siphash/siphash_local.h"
24
25 #include "prov/providercommonerr.h"
26 #include "prov/implementations.h"
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  */
33 static OSSL_OP_mac_newctx_fn siphash_new;
34 static OSSL_OP_mac_dupctx_fn siphash_dup;
35 static OSSL_OP_mac_freectx_fn siphash_free;
36 static OSSL_OP_mac_gettable_ctx_params_fn siphash_gettable_ctx_params;
37 static OSSL_OP_mac_get_ctx_params_fn siphash_get_ctx_params;
38 static OSSL_OP_mac_settable_ctx_params_fn siphash_settable_params;
39 static OSSL_OP_mac_set_ctx_params_fn siphash_set_params;
40 static OSSL_OP_mac_size_fn siphash_size;
41 static OSSL_OP_mac_init_fn siphash_init;
42 static OSSL_OP_mac_update_fn siphash_update;
43 static OSSL_OP_mac_final_fn siphash_final;
44
45 struct siphash_data_st {
46     void *provctx;
47     SIPHASH siphash;             /* Siphash data */
48 };
49
50 static void *siphash_new(void *provctx)
51 {
52     struct siphash_data_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
53
54     ctx->provctx = provctx;
55     return ctx;
56 }
57
58 static void siphash_free(void *vmacctx)
59 {
60     OPENSSL_free(vmacctx);
61 }
62
63 static void *siphash_dup(void *vsrc)
64 {
65     struct siphash_data_st *ssrc = vsrc;
66     struct siphash_data_st *sdst = siphash_new(ssrc->provctx);
67
68     if (sdst == NULL)
69         return NULL;
70
71     sdst->siphash = ssrc->siphash;
72     return sdst;
73 }
74
75 static size_t siphash_size(void *vmacctx)
76 {
77     struct siphash_data_st *ctx = vmacctx;
78
79     return SipHash_hash_size(&ctx->siphash);
80 }
81
82 static int siphash_init(void *vmacctx)
83 {
84     /* Not much to do here, actual initialization happens through controls */
85     return 1;
86 }
87
88 static int siphash_update(void *vmacctx, const unsigned char *data,
89                        size_t datalen)
90 {
91     struct siphash_data_st *ctx = vmacctx;
92
93     SipHash_Update(&ctx->siphash, data, datalen);
94     return 1;
95 }
96
97 static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
98                          size_t outsize)
99 {
100     struct siphash_data_st *ctx = vmacctx;
101     size_t hlen = siphash_size(ctx);
102
103     if (outsize < hlen)
104         return 0;
105
106     *outl = hlen;
107     return SipHash_Final(&ctx->siphash, out, hlen);
108 }
109
110 static const OSSL_PARAM known_gettable_ctx_params[] = {
111     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
112     OSSL_PARAM_END
113 };
114 static const OSSL_PARAM *siphash_gettable_ctx_params(void)
115 {
116     return known_gettable_ctx_params;
117 }
118
119 static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
120 {
121     OSSL_PARAM *p;
122
123     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
124         return OSSL_PARAM_set_size_t(p, siphash_size(vmacctx));
125
126     return 1;
127 }
128
129 static const OSSL_PARAM known_settable_ctx_params[] = {
130     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
131     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
132     OSSL_PARAM_END
133 };
134 static const OSSL_PARAM *siphash_settable_params(void)
135 {
136     return known_settable_ctx_params;
137 }
138
139 static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
140 {
141     struct siphash_data_st *ctx = vmacctx;
142     const OSSL_PARAM *p = NULL;
143
144     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
145         size_t size;
146
147         if (!OSSL_PARAM_get_size_t(p, &size)
148             || !SipHash_set_hash_size(&ctx->siphash, size))
149             return 0;
150     }
151     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
152         if (p->data_type != OSSL_PARAM_OCTET_STRING
153             || p->data_size != SIPHASH_KEY_SIZE
154             || !SipHash_Init(&ctx->siphash, p->data, 0, 0))
155             return 0;
156     return 1;
157 }
158
159 const OSSL_DISPATCH siphash_functions[] = {
160     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
161     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
162     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
163     { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
164     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
165     { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
166     { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
167       (void (*)(void))siphash_gettable_ctx_params },
168     { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
169     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
170       (void (*)(void))siphash_settable_params },
171     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
172     { 0, NULL }
173 };