mac: add FIPS error state handling
[openssl.git] / providers / implementations / macs / poly1305_prov.c
1 /*
2  * Copyright 2018-2020 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 #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     POLY1305 poly1305;           /* Poly1305 data */
41 };
42
43 static size_t poly1305_size(void);
44
45 static void *poly1305_new(void *provctx)
46 {
47     struct poly1305_data_st *ctx;
48
49     if (!ossl_prov_is_running())
50         return NULL;
51     ctx = OPENSSL_zalloc(sizeof(*ctx));
52     if (ctx != NULL)
53         ctx->provctx = provctx;
54     return ctx;
55 }
56
57 static void poly1305_free(void *vmacctx)
58 {
59     OPENSSL_free(vmacctx);
60 }
61
62 static void *poly1305_dup(void *vsrc)
63 {
64     struct poly1305_data_st *src = vsrc;
65     struct poly1305_data_st *dst;
66
67     if (!ossl_prov_is_running())
68         return NULL;
69     dst = poly1305_new(src->provctx);
70     if (dst == NULL)
71         return NULL;
72
73     dst->poly1305 = src->poly1305;
74     return dst;
75 }
76
77 static size_t poly1305_size(void)
78 {
79     return POLY1305_DIGEST_SIZE;
80 }
81
82 static int poly1305_init(void *vmacctx)
83 {
84     /* initialize the context in MAC_ctrl function */
85     return ossl_prov_is_running();
86 }
87
88 static int poly1305_update(void *vmacctx, const unsigned char *data,
89                        size_t datalen)
90 {
91     struct poly1305_data_st *ctx = vmacctx;
92
93     if (datalen == 0)
94         return 1;
95
96     /* poly1305 has nothing to return in its update function */
97     Poly1305_Update(&ctx->poly1305, data, datalen);
98     return 1;
99 }
100
101 static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
102                           size_t outsize)
103 {
104     struct poly1305_data_st *ctx = vmacctx;
105
106     if (!ossl_prov_is_running())
107         return 0;
108     Poly1305_Final(&ctx->poly1305, out);
109     *outl = poly1305_size();
110     return 1;
111 }
112
113 static const OSSL_PARAM known_gettable_params[] = {
114     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
115     OSSL_PARAM_END
116 };
117 static const OSSL_PARAM *poly1305_gettable_params(void *provctx)
118 {
119     return known_gettable_params;
120 }
121
122 static int poly1305_get_params(OSSL_PARAM params[])
123 {
124     OSSL_PARAM *p;
125
126     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
127         return OSSL_PARAM_set_size_t(p, poly1305_size());
128
129     return 1;
130 }
131
132 static const OSSL_PARAM known_settable_ctx_params[] = {
133     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
134     OSSL_PARAM_END
135 };
136 static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *provctx)
137 {
138     return known_settable_ctx_params;
139 }
140
141 static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
142 {
143     struct poly1305_data_st *ctx = vmacctx;
144     const OSSL_PARAM *p = NULL;
145
146     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
147         if (p->data_type != OSSL_PARAM_OCTET_STRING
148             || p->data_size != POLY1305_KEY_SIZE) {
149             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
150             return 0;
151         }
152         Poly1305_Init(&ctx->poly1305, p->data);
153     }
154     return 1;
155 }
156
157 const OSSL_DISPATCH poly1305_functions[] = {
158     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
159     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
160     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
161     { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
162     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
163     { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
164     { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
165     { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
166     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
167       (void (*)(void))poly1305_settable_ctx_params },
168     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
169     { 0, NULL }
170 };