]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/mac_lib.c
Standard style for all EVP_xxx_free routines
[thirdparty/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->data = 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->data);
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->data);
45 ctx->data = 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->data == 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->data = src->meth->dupctx(src->data);
72 if (dst->data == NULL) {
73 EVP_MAC_CTX_free(dst);
74 return NULL;
75 }
76
77 return dst;
78 }
79
80 EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx)
81 {
82 return ctx->meth;
83 }
84
85 size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx)
86 {
87 size_t sz = 0;
88
89 if (ctx->data != NULL) {
90 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
91
92 params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &sz);
93 if (ctx->meth->get_ctx_params != NULL) {
94 if (ctx->meth->get_ctx_params(ctx->data, 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 int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
109 const OSSL_PARAM params[])
110 {
111 return ctx->meth->init(ctx->data, key, keylen, params);
112 }
113
114 int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
115 {
116 return ctx->meth->update(ctx->data, data, datalen);
117 }
118
119 int EVP_MAC_final(EVP_MAC_CTX *ctx,
120 unsigned char *out, size_t *outl, size_t outsize)
121 {
122 size_t l;
123 int res = 1;
124
125 if (out != NULL)
126 res = ctx->meth->final(ctx->data, out, &l, outsize);
127 else
128 l = EVP_MAC_CTX_get_mac_size(ctx);
129 if (outl != NULL)
130 *outl = l;
131 return res;
132 }
133
134 /*
135 * The {get,set}_params functions return 1 if there is no corresponding
136 * function in the implementation. This is the same as if there was one,
137 * but it didn't recognise any of the given params, i.e. nothing in the
138 * bag of parameters was useful.
139 */
140 int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[])
141 {
142 if (mac->get_params != NULL)
143 return mac->get_params(params);
144 return 1;
145 }
146
147 int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[])
148 {
149 if (ctx->meth->get_ctx_params != NULL)
150 return ctx->meth->get_ctx_params(ctx->data, params);
151 return 1;
152 }
153
154 int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[])
155 {
156 if (ctx->meth->set_ctx_params != NULL)
157 return ctx->meth->set_ctx_params(ctx->data, params);
158 return 1;
159 }
160
161 int EVP_MAC_number(const EVP_MAC *mac)
162 {
163 return mac->name_id;
164 }
165
166 const char *EVP_MAC_name(const EVP_MAC *mac)
167 {
168 if (mac->prov != NULL)
169 return evp_first_name(mac->prov, mac->name_id);
170 return NULL;
171 }
172
173 const char *EVP_MAC_description(const EVP_MAC *mac)
174 {
175 return mac->description;
176 }
177
178 int EVP_MAC_is_a(const EVP_MAC *mac, const char *name)
179 {
180 return evp_is_a(mac->prov, mac->name_id, NULL, name);
181 }
182
183 int EVP_MAC_names_do_all(const EVP_MAC *mac,
184 void (*fn)(const char *name, void *data),
185 void *data)
186 {
187 if (mac->prov != NULL)
188 return evp_names_do_all(mac->prov, mac->name_id, fn, data);
189
190 return 1;
191 }