xref: /freebsd-src/crypto/openssl/providers/implementations/macs/poly1305_prov.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
11*b077aed3SPierre Pronchery #include <openssl/core_names.h>
12*b077aed3SPierre Pronchery #include <openssl/params.h>
13*b077aed3SPierre Pronchery #include <openssl/evp.h>
14*b077aed3SPierre Pronchery #include <openssl/err.h>
15*b077aed3SPierre Pronchery #include <openssl/proverr.h>
16*b077aed3SPierre Pronchery 
17*b077aed3SPierre Pronchery #include "crypto/poly1305.h"
18*b077aed3SPierre Pronchery 
19*b077aed3SPierre Pronchery #include "prov/implementations.h"
20*b077aed3SPierre Pronchery #include "prov/providercommon.h"
21*b077aed3SPierre Pronchery 
22*b077aed3SPierre Pronchery /*
23*b077aed3SPierre Pronchery  * Forward declaration of everything implemented here.  This is not strictly
24*b077aed3SPierre Pronchery  * necessary for the compiler, but provides an assurance that the signatures
25*b077aed3SPierre Pronchery  * of the functions in the dispatch table are correct.
26*b077aed3SPierre Pronchery  */
27*b077aed3SPierre Pronchery static OSSL_FUNC_mac_newctx_fn poly1305_new;
28*b077aed3SPierre Pronchery static OSSL_FUNC_mac_dupctx_fn poly1305_dup;
29*b077aed3SPierre Pronchery static OSSL_FUNC_mac_freectx_fn poly1305_free;
30*b077aed3SPierre Pronchery static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params;
31*b077aed3SPierre Pronchery static OSSL_FUNC_mac_get_params_fn poly1305_get_params;
32*b077aed3SPierre Pronchery static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
33*b077aed3SPierre Pronchery static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params;
34*b077aed3SPierre Pronchery static OSSL_FUNC_mac_init_fn poly1305_init;
35*b077aed3SPierre Pronchery static OSSL_FUNC_mac_update_fn poly1305_update;
36*b077aed3SPierre Pronchery static OSSL_FUNC_mac_final_fn poly1305_final;
37*b077aed3SPierre Pronchery 
38*b077aed3SPierre Pronchery struct poly1305_data_st {
39*b077aed3SPierre Pronchery     void *provctx;
40*b077aed3SPierre Pronchery     int updated;
41*b077aed3SPierre Pronchery     POLY1305 poly1305;           /* Poly1305 data */
42*b077aed3SPierre Pronchery };
43*b077aed3SPierre Pronchery 
poly1305_new(void * provctx)44*b077aed3SPierre Pronchery static void *poly1305_new(void *provctx)
45*b077aed3SPierre Pronchery {
46*b077aed3SPierre Pronchery     struct poly1305_data_st *ctx;
47*b077aed3SPierre Pronchery 
48*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
49*b077aed3SPierre Pronchery         return NULL;
50*b077aed3SPierre Pronchery     ctx = OPENSSL_zalloc(sizeof(*ctx));
51*b077aed3SPierre Pronchery     if (ctx != NULL)
52*b077aed3SPierre Pronchery         ctx->provctx = provctx;
53*b077aed3SPierre Pronchery     return ctx;
54*b077aed3SPierre Pronchery }
55*b077aed3SPierre Pronchery 
poly1305_free(void * vmacctx)56*b077aed3SPierre Pronchery static void poly1305_free(void *vmacctx)
57*b077aed3SPierre Pronchery {
58*b077aed3SPierre Pronchery     OPENSSL_free(vmacctx);
59*b077aed3SPierre Pronchery }
60*b077aed3SPierre Pronchery 
poly1305_dup(void * vsrc)61*b077aed3SPierre Pronchery static void *poly1305_dup(void *vsrc)
62*b077aed3SPierre Pronchery {
63*b077aed3SPierre Pronchery     struct poly1305_data_st *src = vsrc;
64*b077aed3SPierre Pronchery     struct poly1305_data_st *dst;
65*b077aed3SPierre Pronchery 
66*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
67*b077aed3SPierre Pronchery         return NULL;
68*b077aed3SPierre Pronchery     dst = OPENSSL_malloc(sizeof(*dst));
69*b077aed3SPierre Pronchery     if (dst == NULL)
70*b077aed3SPierre Pronchery         return NULL;
71*b077aed3SPierre Pronchery 
72*b077aed3SPierre Pronchery     *dst = *src;
73*b077aed3SPierre Pronchery     return dst;
74*b077aed3SPierre Pronchery }
75*b077aed3SPierre Pronchery 
poly1305_size(void)76*b077aed3SPierre Pronchery static size_t poly1305_size(void)
77*b077aed3SPierre Pronchery {
78*b077aed3SPierre Pronchery     return POLY1305_DIGEST_SIZE;
79*b077aed3SPierre Pronchery }
80*b077aed3SPierre Pronchery 
poly1305_setkey(struct poly1305_data_st * ctx,const unsigned char * key,size_t keylen)81*b077aed3SPierre Pronchery static int poly1305_setkey(struct poly1305_data_st *ctx,
82*b077aed3SPierre Pronchery                            const unsigned char *key, size_t keylen)
83*b077aed3SPierre Pronchery {
84*b077aed3SPierre Pronchery     if (keylen != POLY1305_KEY_SIZE) {
85*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
86*b077aed3SPierre Pronchery         return 0;
87*b077aed3SPierre Pronchery     }
88*b077aed3SPierre Pronchery     Poly1305_Init(&ctx->poly1305, key);
89*b077aed3SPierre Pronchery     ctx->updated = 0;
90*b077aed3SPierre Pronchery     return 1;
91*b077aed3SPierre Pronchery }
92*b077aed3SPierre Pronchery 
poly1305_init(void * vmacctx,const unsigned char * key,size_t keylen,const OSSL_PARAM params[])93*b077aed3SPierre Pronchery static int poly1305_init(void *vmacctx, const unsigned char *key,
94*b077aed3SPierre Pronchery                          size_t keylen, const OSSL_PARAM params[])
95*b077aed3SPierre Pronchery {
96*b077aed3SPierre Pronchery     struct poly1305_data_st *ctx = vmacctx;
97*b077aed3SPierre Pronchery 
98*b077aed3SPierre Pronchery     /* initialize the context in MAC_ctrl function */
99*b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params))
100*b077aed3SPierre Pronchery         return 0;
101*b077aed3SPierre Pronchery     if (key != NULL)
102*b077aed3SPierre Pronchery         return poly1305_setkey(ctx, key, keylen);
103*b077aed3SPierre Pronchery     /* no reinitialization of context with the same key is allowed */
104*b077aed3SPierre Pronchery     return ctx->updated == 0;
105*b077aed3SPierre Pronchery }
106*b077aed3SPierre Pronchery 
poly1305_update(void * vmacctx,const unsigned char * data,size_t datalen)107*b077aed3SPierre Pronchery static int poly1305_update(void *vmacctx, const unsigned char *data,
108*b077aed3SPierre Pronchery                        size_t datalen)
109*b077aed3SPierre Pronchery {
110*b077aed3SPierre Pronchery     struct poly1305_data_st *ctx = vmacctx;
111*b077aed3SPierre Pronchery 
112*b077aed3SPierre Pronchery     ctx->updated = 1;
113*b077aed3SPierre Pronchery     if (datalen == 0)
114*b077aed3SPierre Pronchery         return 1;
115*b077aed3SPierre Pronchery 
116*b077aed3SPierre Pronchery     /* poly1305 has nothing to return in its update function */
117*b077aed3SPierre Pronchery     Poly1305_Update(&ctx->poly1305, data, datalen);
118*b077aed3SPierre Pronchery     return 1;
119*b077aed3SPierre Pronchery }
120*b077aed3SPierre Pronchery 
poly1305_final(void * vmacctx,unsigned char * out,size_t * outl,size_t outsize)121*b077aed3SPierre Pronchery static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
122*b077aed3SPierre Pronchery                           size_t outsize)
123*b077aed3SPierre Pronchery {
124*b077aed3SPierre Pronchery     struct poly1305_data_st *ctx = vmacctx;
125*b077aed3SPierre Pronchery 
126*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
127*b077aed3SPierre Pronchery         return 0;
128*b077aed3SPierre Pronchery     ctx->updated = 1;
129*b077aed3SPierre Pronchery     Poly1305_Final(&ctx->poly1305, out);
130*b077aed3SPierre Pronchery     *outl = poly1305_size();
131*b077aed3SPierre Pronchery     return 1;
132*b077aed3SPierre Pronchery }
133*b077aed3SPierre Pronchery 
134*b077aed3SPierre Pronchery static const OSSL_PARAM known_gettable_params[] = {
135*b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
136*b077aed3SPierre Pronchery     OSSL_PARAM_END
137*b077aed3SPierre Pronchery };
poly1305_gettable_params(void * provctx)138*b077aed3SPierre Pronchery static const OSSL_PARAM *poly1305_gettable_params(void *provctx)
139*b077aed3SPierre Pronchery {
140*b077aed3SPierre Pronchery     return known_gettable_params;
141*b077aed3SPierre Pronchery }
142*b077aed3SPierre Pronchery 
poly1305_get_params(OSSL_PARAM params[])143*b077aed3SPierre Pronchery static int poly1305_get_params(OSSL_PARAM params[])
144*b077aed3SPierre Pronchery {
145*b077aed3SPierre Pronchery     OSSL_PARAM *p;
146*b077aed3SPierre Pronchery 
147*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
148*b077aed3SPierre Pronchery         return OSSL_PARAM_set_size_t(p, poly1305_size());
149*b077aed3SPierre Pronchery 
150*b077aed3SPierre Pronchery     return 1;
151*b077aed3SPierre Pronchery }
152*b077aed3SPierre Pronchery 
153*b077aed3SPierre Pronchery static const OSSL_PARAM known_settable_ctx_params[] = {
154*b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
155*b077aed3SPierre Pronchery     OSSL_PARAM_END
156*b077aed3SPierre Pronchery };
poly1305_settable_ctx_params(ossl_unused void * ctx,ossl_unused void * provctx)157*b077aed3SPierre Pronchery static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx,
158*b077aed3SPierre Pronchery                                                       ossl_unused void *provctx)
159*b077aed3SPierre Pronchery {
160*b077aed3SPierre Pronchery     return known_settable_ctx_params;
161*b077aed3SPierre Pronchery }
162*b077aed3SPierre Pronchery 
poly1305_set_ctx_params(void * vmacctx,const OSSL_PARAM * params)163*b077aed3SPierre Pronchery static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
164*b077aed3SPierre Pronchery {
165*b077aed3SPierre Pronchery     struct poly1305_data_st *ctx = vmacctx;
166*b077aed3SPierre Pronchery     const OSSL_PARAM *p;
167*b077aed3SPierre Pronchery 
168*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL
169*b077aed3SPierre Pronchery             && !poly1305_setkey(ctx, p->data, p->data_size))
170*b077aed3SPierre Pronchery         return 0;
171*b077aed3SPierre Pronchery     return 1;
172*b077aed3SPierre Pronchery }
173*b077aed3SPierre Pronchery 
174*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_poly1305_functions[] = {
175*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
176*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
177*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
178*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
179*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
180*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
181*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
182*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
183*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
184*b077aed3SPierre Pronchery       (void (*)(void))poly1305_settable_ctx_params },
185*b077aed3SPierre Pronchery     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
186*b077aed3SPierre Pronchery     { 0, NULL }
187*b077aed3SPierre Pronchery };
188