/src/openssl34/providers/implementations/macs/poly1305_prov.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2018-2026 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 | | #include <openssl/proverr.h> |
16 | | |
17 | | #include "crypto/poly1305.h" |
18 | | |
19 | | #include "prov/implementations.h" |
20 | | #include "prov/providercommon.h" |
21 | | |
22 | | /* |
23 | | * Forward declaration of everything implemented here. This is not strictly |
24 | | * necessary for the compiler, but provides an assurance that the signatures |
25 | | * of the functions in the dispatch table are correct. |
26 | | */ |
27 | | static OSSL_FUNC_mac_newctx_fn poly1305_new; |
28 | | static OSSL_FUNC_mac_dupctx_fn poly1305_dup; |
29 | | static OSSL_FUNC_mac_freectx_fn poly1305_free; |
30 | | static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params; |
31 | | static OSSL_FUNC_mac_get_params_fn poly1305_get_params; |
32 | | static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params; |
33 | | static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params; |
34 | | static OSSL_FUNC_mac_init_fn poly1305_init; |
35 | | static OSSL_FUNC_mac_update_fn poly1305_update; |
36 | | static OSSL_FUNC_mac_final_fn poly1305_final; |
37 | | |
38 | | struct poly1305_data_st { |
39 | | void *provctx; |
40 | | int updated; |
41 | | int key_set; |
42 | | POLY1305 poly1305; /* Poly1305 data */ |
43 | | }; |
44 | | |
45 | | static void *poly1305_new(void *provctx) |
46 | 54 | { |
47 | 54 | struct poly1305_data_st *ctx; |
48 | | |
49 | 54 | if (!ossl_prov_is_running()) |
50 | 0 | return NULL; |
51 | 54 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
52 | 54 | if (ctx != NULL) |
53 | 54 | ctx->provctx = provctx; |
54 | 54 | return ctx; |
55 | 54 | } |
56 | | |
57 | | static void poly1305_free(void *vmacctx) |
58 | 54 | { |
59 | 54 | OPENSSL_free(vmacctx); |
60 | 54 | } |
61 | | |
62 | | static void *poly1305_dup(void *vsrc) |
63 | 0 | { |
64 | 0 | struct poly1305_data_st *src = vsrc; |
65 | 0 | struct poly1305_data_st *dst; |
66 | |
|
67 | 0 | if (!ossl_prov_is_running()) |
68 | 0 | return NULL; |
69 | 0 | dst = OPENSSL_malloc(sizeof(*dst)); |
70 | 0 | if (dst == NULL) |
71 | 0 | return NULL; |
72 | | |
73 | 0 | *dst = *src; |
74 | 0 | return dst; |
75 | 0 | } |
76 | | |
77 | | static size_t poly1305_size(void) |
78 | 560 | { |
79 | 560 | return POLY1305_DIGEST_SIZE; |
80 | 560 | } |
81 | | |
82 | | static int poly1305_setkey(struct poly1305_data_st *ctx, |
83 | | const unsigned char *key, size_t keylen) |
84 | 336 | { |
85 | 336 | if (key == NULL || keylen != POLY1305_KEY_SIZE) { |
86 | 52 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
87 | 52 | return 0; |
88 | 52 | } |
89 | 284 | Poly1305_Init(&ctx->poly1305, key); |
90 | 284 | ctx->updated = 0; |
91 | 284 | ctx->key_set = 1; |
92 | 284 | return 1; |
93 | 336 | } |
94 | | |
95 | | static int poly1305_init(void *vmacctx, const unsigned char *key, |
96 | | size_t keylen, const OSSL_PARAM params[]) |
97 | 332 | { |
98 | 332 | struct poly1305_data_st *ctx = vmacctx; |
99 | | |
100 | | /* initialize the context in MAC_ctrl function */ |
101 | 332 | if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params)) |
102 | 47 | return 0; |
103 | 285 | if (key != NULL) |
104 | 285 | return poly1305_setkey(ctx, key, keylen); |
105 | | /* no reinitialization of context with the same key is allowed */ |
106 | 0 | return ctx->updated == 0; |
107 | 285 | } |
108 | | |
109 | | static int poly1305_update(void *vmacctx, const unsigned char *data, |
110 | | size_t datalen) |
111 | 310 | { |
112 | 310 | struct poly1305_data_st *ctx = vmacctx; |
113 | | |
114 | 310 | if (!ctx->key_set) { |
115 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
116 | 0 | return 0; |
117 | 0 | } |
118 | 310 | ctx->updated = 1; |
119 | 310 | if (datalen == 0) |
120 | 0 | return 1; |
121 | | |
122 | | /* poly1305 has nothing to return in its update function */ |
123 | 310 | Poly1305_Update(&ctx->poly1305, data, datalen); |
124 | 310 | return 1; |
125 | 310 | } |
126 | | |
127 | | static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl, |
128 | | size_t outsize) |
129 | 280 | { |
130 | 280 | struct poly1305_data_st *ctx = vmacctx; |
131 | | |
132 | 280 | if (!ossl_prov_is_running()) |
133 | 0 | return 0; |
134 | 280 | if (!ctx->key_set) { |
135 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
136 | 0 | return 0; |
137 | 0 | } |
138 | 280 | ctx->updated = 1; |
139 | 280 | Poly1305_Final(&ctx->poly1305, out); |
140 | 280 | *outl = poly1305_size(); |
141 | 280 | return 1; |
142 | 280 | } |
143 | | |
144 | | static const OSSL_PARAM known_gettable_params[] = { |
145 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
146 | | OSSL_PARAM_END |
147 | | }; |
148 | | static const OSSL_PARAM *poly1305_gettable_params(void *provctx) |
149 | 0 | { |
150 | 0 | return known_gettable_params; |
151 | 0 | } |
152 | | |
153 | | static int poly1305_get_params(OSSL_PARAM params[]) |
154 | 280 | { |
155 | 280 | OSSL_PARAM *p; |
156 | | |
157 | 280 | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL) |
158 | 280 | return OSSL_PARAM_set_size_t(p, poly1305_size()); |
159 | | |
160 | 0 | return 1; |
161 | 280 | } |
162 | | |
163 | | static const OSSL_PARAM known_settable_ctx_params[] = { |
164 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), |
165 | | OSSL_PARAM_END |
166 | | }; |
167 | | static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx, |
168 | | ossl_unused void *provctx) |
169 | 63 | { |
170 | 63 | return known_settable_ctx_params; |
171 | 63 | } |
172 | | |
173 | | static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params) |
174 | 309 | { |
175 | 309 | struct poly1305_data_st *ctx = vmacctx; |
176 | 309 | const OSSL_PARAM *p; |
177 | | |
178 | 309 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL |
179 | 25 | && !poly1305_setkey(ctx, p->data, p->data_size)) |
180 | 23 | return 0; |
181 | 286 | return 1; |
182 | 309 | } |
183 | | |
184 | | const OSSL_DISPATCH ossl_poly1305_functions[] = { |
185 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new }, |
186 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup }, |
187 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free }, |
188 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init }, |
189 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update }, |
190 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final }, |
191 | | { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params }, |
192 | | { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params }, |
193 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
194 | | (void (*)(void))poly1305_settable_ctx_params }, |
195 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params }, |
196 | | OSSL_DISPATCH_END |
197 | | }; |