]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/macs/poly1305_prov.c
poly1305: Properly copy the whole context on dup
[thirdparty/openssl.git] / providers / implementations / macs / poly1305_prov.c
CommitLineData
ae0b6b92 1/*
a28d06f3 2 * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
ae0b6b92
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
23c48d94 10#include <openssl/core_dispatch.h>
7f6b035b
MC
11#include <openssl/core_names.h>
12#include <openssl/params.h>
13#include <openssl/evp.h>
14#include <openssl/err.h>
2741128e 15#include <openssl/proverr.h>
ae0b6b92 16
25f2138b 17#include "crypto/poly1305.h"
ae0b6b92 18
af3e7e1b 19#include "prov/implementations.h"
5b104a81 20#include "prov/providercommon.h"
ae0b6b92
RL
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 */
363b1e5d
DMSP
27static OSSL_FUNC_mac_newctx_fn poly1305_new;
28static OSSL_FUNC_mac_dupctx_fn poly1305_dup;
29static OSSL_FUNC_mac_freectx_fn poly1305_free;
30static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params;
31static OSSL_FUNC_mac_get_params_fn poly1305_get_params;
32static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
33static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params;
34static OSSL_FUNC_mac_init_fn poly1305_init;
35static OSSL_FUNC_mac_update_fn poly1305_update;
36static OSSL_FUNC_mac_final_fn poly1305_final;
ae0b6b92
RL
37
38struct poly1305_data_st {
39 void *provctx;
c9ddc5af 40 int updated;
ae0b6b92
RL
41 POLY1305 poly1305; /* Poly1305 data */
42};
43
ae0b6b92
RL
44static void *poly1305_new(void *provctx)
45{
5b104a81 46 struct poly1305_data_st *ctx;
ae0b6b92 47
5b104a81
P
48 if (!ossl_prov_is_running())
49 return NULL;
50 ctx = OPENSSL_zalloc(sizeof(*ctx));
ebe19ab8
P
51 if (ctx != NULL)
52 ctx->provctx = provctx;
ae0b6b92
RL
53 return ctx;
54}
55
56static void poly1305_free(void *vmacctx)
57{
58 OPENSSL_free(vmacctx);
59}
60
61static void *poly1305_dup(void *vsrc)
62{
63 struct poly1305_data_st *src = vsrc;
5b104a81 64 struct poly1305_data_st *dst;
ae0b6b92 65
5b104a81
P
66 if (!ossl_prov_is_running())
67 return NULL;
bbe909d0 68 dst = OPENSSL_malloc(sizeof(*dst));
ae0b6b92
RL
69 if (dst == NULL)
70 return NULL;
71
bbe909d0 72 *dst = *src;
ae0b6b92
RL
73 return dst;
74}
75
76static size_t poly1305_size(void)
77{
78 return POLY1305_DIGEST_SIZE;
79}
80
1dfe9753
P
81static int poly1305_setkey(struct poly1305_data_st *ctx,
82 const unsigned char *key, size_t keylen)
ae0b6b92 83{
1dfe9753
P
84 if (keylen != POLY1305_KEY_SIZE) {
85 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
86 return 0;
87 }
88 Poly1305_Init(&ctx->poly1305, key);
bbe909d0 89 ctx->updated = 0;
1dfe9753
P
90 return 1;
91}
92
93static int poly1305_init(void *vmacctx, const unsigned char *key,
94 size_t keylen, const OSSL_PARAM params[])
95{
96 struct poly1305_data_st *ctx = vmacctx;
97
ae0b6b92 98 /* initialize the context in MAC_ctrl function */
1dfe9753
P
99 if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params))
100 return 0;
101 if (key != NULL)
102 return poly1305_setkey(ctx, key, keylen);
c9ddc5af
TM
103 /* no reinitialization of context with the same key is allowed */
104 return ctx->updated == 0;
ae0b6b92
RL
105}
106
107static int poly1305_update(void *vmacctx, const unsigned char *data,
108 size_t datalen)
109{
110 struct poly1305_data_st *ctx = vmacctx;
111
c9ddc5af 112 ctx->updated = 1;
0bc193dd
MC
113 if (datalen == 0)
114 return 1;
115
ae0b6b92
RL
116 /* poly1305 has nothing to return in its update function */
117 Poly1305_Update(&ctx->poly1305, data, datalen);
118 return 1;
119}
120
121static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
122 size_t outsize)
123{
124 struct poly1305_data_st *ctx = vmacctx;
125
5b104a81
P
126 if (!ossl_prov_is_running())
127 return 0;
c9ddc5af 128 ctx->updated = 1;
ae0b6b92 129 Poly1305_Final(&ctx->poly1305, out);
5f6a0b2f 130 *outl = poly1305_size();
ae0b6b92
RL
131 return 1;
132}
133
134static const OSSL_PARAM known_gettable_params[] = {
703170d4 135 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
ae0b6b92
RL
136 OSSL_PARAM_END
137};
af5e1e85 138static const OSSL_PARAM *poly1305_gettable_params(void *provctx)
ae0b6b92
RL
139{
140 return known_gettable_params;
141}
142
143static int poly1305_get_params(OSSL_PARAM params[])
144{
145 OSSL_PARAM *p;
146
703170d4 147 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
ae0b6b92
RL
148 return OSSL_PARAM_set_size_t(p, poly1305_size());
149
150 return 1;
151}
152
153static const OSSL_PARAM known_settable_ctx_params[] = {
154 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
155 OSSL_PARAM_END
156};
eee323c3
P
157static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx,
158 ossl_unused void *provctx)
ae0b6b92
RL
159{
160 return known_settable_ctx_params;
161}
162
92d9d0ae 163static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
ae0b6b92
RL
164{
165 struct poly1305_data_st *ctx = vmacctx;
1dfe9753
P
166 const OSSL_PARAM *p;
167
168 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL
169 && !poly1305_setkey(ctx, p->data, p->data_size))
170 return 0;
ae0b6b92
RL
171 return 1;
172}
173
1be63951 174const OSSL_DISPATCH ossl_poly1305_functions[] = {
ae0b6b92
RL
175 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
176 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
177 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
178 { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
179 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
180 { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
181 { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
182 { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
183 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
184 (void (*)(void))poly1305_settable_ctx_params },
92d9d0ae 185 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
ae0b6b92
RL
186 { 0, NULL }
187};