Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / crypto / evp / mac_lib.c
1 /*
2  * Copyright 2018-2021 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 <string.h>
11 #include <stdarg.h>
12 #include <openssl/evp.h>
13 #include <openssl/err.h>
14 #include <openssl/core.h>
15 #include <openssl/core_names.h>
16 #include <openssl/types.h>
17 #include "internal/nelem.h"
18 #include "crypto/evp.h"
19 #include "internal/provider.h"
20 #include "evp_local.h"
21
22 EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac)
23 {
24     EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX));
25
26     if (ctx == NULL
27         || (ctx->algctx = mac->newctx(ossl_provider_ctx(mac->prov))) == NULL
28         || !EVP_MAC_up_ref(mac)) {
29         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
30         if (ctx != NULL)
31             mac->freectx(ctx->algctx);
32         OPENSSL_free(ctx);
33         ctx = NULL;
34     } else {
35         ctx->meth = mac;
36     }
37     return ctx;
38 }
39
40 void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx)
41 {
42     if (ctx == NULL)
43         return;
44     ctx->meth->freectx(ctx->algctx);
45     ctx->algctx = NULL;
46     /* refcnt-- */
47     EVP_MAC_free(ctx->meth);
48     OPENSSL_free(ctx);
49 }
50
51 EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src)
52 {
53     EVP_MAC_CTX *dst;
54
55     if (src->algctx == NULL)
56         return NULL;
57
58     dst = OPENSSL_malloc(sizeof(*dst));
59     if (dst == NULL) {
60         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
61         return NULL;
62     }
63
64     *dst = *src;
65     if (!EVP_MAC_up_ref(dst->meth)) {
66         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
67         OPENSSL_free(dst);
68         return NULL;
69     }
70
71     dst->algctx = src->meth->dupctx(src->algctx);
72     if (dst->algctx == NULL) {
73         EVP_MAC_CTX_free(dst);
74         return NULL;
75     }
76
77     return dst;
78 }
79
80 EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx)
81 {
82     return ctx->meth;
83 }
84
85 static size_t get_size_t_ctx_param(EVP_MAC_CTX *ctx, const char *name)
86 {
87     size_t sz = 0;
88
89     if (ctx->algctx != NULL) {
90         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
91
92         params[0] = OSSL_PARAM_construct_size_t(name, &sz);
93         if (ctx->meth->get_ctx_params != NULL) {
94             if (ctx->meth->get_ctx_params(ctx->algctx, params))
95                 return sz;
96         } else if (ctx->meth->get_params != NULL) {
97             if (ctx->meth->get_params(params))
98                 return sz;
99         }
100     }
101     /*
102      * If the MAC hasn't been initialized yet, or there is no size to get,
103      * we return zero
104      */
105     return 0;
106 }
107
108 size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx)
109 {
110     return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_SIZE);
111 }
112
113 size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx)
114 {
115     return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_BLOCK_SIZE);
116 }
117
118 int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
119                  const OSSL_PARAM params[])
120 {
121     return ctx->meth->init(ctx->algctx, key, keylen, params);
122 }
123
124 int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
125 {
126     return ctx->meth->update(ctx->algctx, data, datalen);
127 }
128
129 static int evp_mac_final(EVP_MAC_CTX *ctx, int xof,
130                          unsigned char *out, size_t *outl, size_t outsize)
131 {
132     size_t l;
133     int res;
134     OSSL_PARAM params[2];
135
136     if (ctx == NULL || ctx->meth == NULL) {
137         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
138         return 0;
139     }
140     if (ctx->meth->final == NULL) {
141         ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
142         return 0;
143     }
144
145     if (out == NULL) {
146         if (outl == NULL) {
147             ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
148             return 0;
149         }
150         *outl = EVP_MAC_CTX_get_mac_size(ctx);
151         return 1;
152     }
153     if (xof) {
154         params[0] = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_XOF, &xof);
155         params[1] = OSSL_PARAM_construct_end();
156
157         if (EVP_MAC_CTX_set_params(ctx, params) <= 0) {
158             ERR_raise(ERR_LIB_EVP, EVP_R_SETTING_XOF_FAILED);
159             return 0;
160         }
161     }
162     res = ctx->meth->final(ctx->algctx, out, &l, outsize);
163     if (outl != NULL)
164         *outl = l;
165     return res;
166 }
167
168 int EVP_MAC_final(EVP_MAC_CTX *ctx,
169                   unsigned char *out, size_t *outl, size_t outsize)
170 {
171     return evp_mac_final(ctx, 0, out, outl, outsize);
172 }
173
174 int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize)
175 {
176     return evp_mac_final(ctx, 1, out, NULL, outsize);
177 }
178
179 /*
180  * The {get,set}_params functions return 1 if there is no corresponding
181  * function in the implementation.  This is the same as if there was one,
182  * but it didn't recognise any of the given params, i.e. nothing in the
183  * bag of parameters was useful.
184  */
185 int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[])
186 {
187     if (mac->get_params != NULL)
188         return mac->get_params(params);
189     return 1;
190 }
191
192 int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[])
193 {
194     if (ctx->meth->get_ctx_params != NULL)
195         return ctx->meth->get_ctx_params(ctx->algctx, params);
196     return 1;
197 }
198
199 int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[])
200 {
201     if (ctx->meth->set_ctx_params != NULL)
202         return ctx->meth->set_ctx_params(ctx->algctx, params);
203     return 1;
204 }
205
206 int evp_mac_get_number(const EVP_MAC *mac)
207 {
208     return mac->name_id;
209 }
210
211 const char *EVP_MAC_get0_name(const EVP_MAC *mac)
212 {
213     return mac->type_name;
214 }
215
216 const char *EVP_MAC_get0_description(const EVP_MAC *mac)
217 {
218     return mac->description;
219 }
220
221 int EVP_MAC_is_a(const EVP_MAC *mac, const char *name)
222 {
223     return evp_is_a(mac->prov, mac->name_id, NULL, name);
224 }
225
226 int EVP_MAC_names_do_all(const EVP_MAC *mac,
227                          void (*fn)(const char *name, void *data),
228                          void *data)
229 {
230     if (mac->prov != NULL)
231         return evp_names_do_all(mac->prov, mac->name_id, fn, data);
232
233     return 1;
234 }
235
236 unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx,
237                          const char *name, const char *propq,
238                          const char *subalg, const OSSL_PARAM *params,
239                          const void *key, size_t keylen,
240                          const unsigned char *data, size_t datalen,
241                          unsigned char *out, size_t outsize, size_t *outlen)
242 {
243     EVP_MAC *mac = EVP_MAC_fetch(libctx, name, propq);
244     OSSL_PARAM subalg_param[] = { OSSL_PARAM_END, OSSL_PARAM_END };
245     EVP_MAC_CTX *ctx  = NULL;
246     size_t len = 0;
247     unsigned char *res = NULL;
248
249     if (outlen != NULL)
250         *outlen = 0;
251     if (mac == NULL)
252         return NULL;
253     if (subalg != NULL) {
254         const OSSL_PARAM *defined_params = EVP_MAC_settable_ctx_params(mac);
255         const char *param_name = OSSL_MAC_PARAM_DIGEST;
256
257         /*
258          * The underlying algorithm may be a cipher or a digest.
259          * We don't know which it is, but we can ask the MAC what it
260          * should be and bet on that.
261          */
262         if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
263             param_name = OSSL_MAC_PARAM_CIPHER;
264             if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
265                 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
266                 goto err;
267             }
268         }
269         subalg_param[0] =
270             OSSL_PARAM_construct_utf8_string(param_name, (char *)subalg, 0);
271     }
272     /* Single-shot - on NULL key input, set dummy key value for EVP_MAC_Init. */
273     if (key == NULL && keylen == 0)
274         key = data;
275     if ((ctx = EVP_MAC_CTX_new(mac)) != NULL
276             && EVP_MAC_CTX_set_params(ctx, subalg_param)
277             && EVP_MAC_CTX_set_params(ctx, params)
278             && EVP_MAC_init(ctx, key, keylen, params)
279             && EVP_MAC_update(ctx, data, datalen)
280             && EVP_MAC_final(ctx, out, &len, outsize)) {
281         if (out == NULL) {
282             out = OPENSSL_malloc(len);
283             if (out != NULL && !EVP_MAC_final(ctx, out, NULL, len)) {
284                 OPENSSL_free(out);
285                 out = NULL;
286             }
287         }
288         res = out;
289         if (res != NULL && outlen != NULL)
290             *outlen = len;
291     }
292
293  err:
294     EVP_MAC_CTX_free(ctx);
295     EVP_MAC_free(mac);
296     return res;
297 }