Make the naming scheme for dispatched functions more consistent
[openssl.git] / providers / implementations / macs / poly1305_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/core_dispatch.h>
11 #include <openssl/core_names.h>
12 #include <openssl/params.h>
13 #include <openssl/evp.h>
14 #include <openssl/err.h>
15
16 #include "crypto/poly1305.h"
17
18 #include "prov/providercommonerr.h"
19 #include "prov/implementations.h"
20
21 /*
22  * Forward declaration of everything implemented here.  This is not strictly
23  * necessary for the compiler, but provides an assurance that the signatures
24  * of the functions in the dispatch table are correct.
25  */
26 static OSSL_FUNC_mac_newctx_fn poly1305_new;
27 static OSSL_FUNC_mac_dupctx_fn poly1305_dup;
28 static OSSL_FUNC_mac_freectx_fn poly1305_free;
29 static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params;
30 static OSSL_FUNC_mac_get_params_fn poly1305_get_params;
31 static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
32 static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params;
33 static OSSL_FUNC_mac_init_fn poly1305_init;
34 static OSSL_FUNC_mac_update_fn poly1305_update;
35 static OSSL_FUNC_mac_final_fn poly1305_final;
36
37 struct poly1305_data_st {
38     void *provctx;
39     POLY1305 poly1305;           /* Poly1305 data */
40 };
41
42 static size_t poly1305_size(void);
43
44 static void *poly1305_new(void *provctx)
45 {
46     struct poly1305_data_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
47
48     if (ctx != NULL)
49         ctx->provctx = provctx;
50     return ctx;
51 }
52
53 static void poly1305_free(void *vmacctx)
54 {
55     OPENSSL_free(vmacctx);
56 }
57
58 static void *poly1305_dup(void *vsrc)
59 {
60     struct poly1305_data_st *src = vsrc;
61     struct poly1305_data_st *dst = poly1305_new(src->provctx);
62
63     if (dst == NULL)
64         return NULL;
65
66     dst->poly1305 = src->poly1305;
67     return dst;
68 }
69
70 static size_t poly1305_size(void)
71 {
72     return POLY1305_DIGEST_SIZE;
73 }
74
75 static int poly1305_init(void *vmacctx)
76 {
77     /* initialize the context in MAC_ctrl function */
78     return 1;
79 }
80
81 static int poly1305_update(void *vmacctx, const unsigned char *data,
82                        size_t datalen)
83 {
84     struct poly1305_data_st *ctx = vmacctx;
85
86     /* poly1305 has nothing to return in its update function */
87     Poly1305_Update(&ctx->poly1305, data, datalen);
88     return 1;
89 }
90
91 static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
92                           size_t outsize)
93 {
94     struct poly1305_data_st *ctx = vmacctx;
95
96     Poly1305_Final(&ctx->poly1305, out);
97     return 1;
98 }
99
100 static const OSSL_PARAM known_gettable_params[] = {
101     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
102     OSSL_PARAM_END
103 };
104 static const OSSL_PARAM *poly1305_gettable_params(void)
105 {
106     return known_gettable_params;
107 }
108
109 static int poly1305_get_params(OSSL_PARAM params[])
110 {
111     OSSL_PARAM *p;
112
113     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
114         return OSSL_PARAM_set_size_t(p, poly1305_size());
115
116     return 1;
117 }
118
119 static const OSSL_PARAM known_settable_ctx_params[] = {
120     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
121     OSSL_PARAM_END
122 };
123 static const OSSL_PARAM *poly1305_settable_ctx_params(void)
124 {
125     return known_settable_ctx_params;
126 }
127
128 static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
129 {
130     struct poly1305_data_st *ctx = vmacctx;
131     const OSSL_PARAM *p = NULL;
132
133     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
134         if (p->data_type != OSSL_PARAM_OCTET_STRING
135             || p->data_size != POLY1305_KEY_SIZE) {
136             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
137             return 0;
138         }
139         Poly1305_Init(&ctx->poly1305, p->data);
140     }
141     return 1;
142 }
143
144 const OSSL_DISPATCH poly1305_functions[] = {
145     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
146     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
147     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
148     { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
149     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
150     { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
151     { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
152     { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
153     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
154       (void (*)(void))poly1305_settable_ctx_params },
155     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
156     { 0, NULL }
157 };