/src/openssl111/crypto/evp/digest.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/objects.h> |
13 | | #include <openssl/evp.h> |
14 | | #include <openssl/engine.h> |
15 | | #include "crypto/evp.h" |
16 | | #include "evp_local.h" |
17 | | |
18 | | |
19 | | static void cleanup_old_md_data(EVP_MD_CTX *ctx, int force) |
20 | 0 | { |
21 | 0 | if (ctx->digest != NULL) { |
22 | 0 | if (ctx->digest->cleanup != NULL |
23 | 0 | && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED)) |
24 | 0 | ctx->digest->cleanup(ctx); |
25 | 0 | if (ctx->md_data != NULL && ctx->digest->ctx_size > 0 |
26 | 0 | && (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE) |
27 | 0 | || force)) { |
28 | 0 | OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size); |
29 | 0 | ctx->md_data = NULL; |
30 | 0 | } |
31 | 0 | } |
32 | 0 | } |
33 | | |
34 | | /* This call frees resources associated with the context */ |
35 | | int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) |
36 | 0 | { |
37 | 0 | if (ctx == NULL) |
38 | 0 | return 1; |
39 | | |
40 | | /* |
41 | | * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because |
42 | | * sometimes only copies of the context are ever finalised. |
43 | | */ |
44 | 0 | cleanup_old_md_data(ctx, 0); |
45 | | |
46 | | /* |
47 | | * pctx should be freed by the user of EVP_MD_CTX |
48 | | * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set |
49 | | */ |
50 | 0 | if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) |
51 | 0 | EVP_PKEY_CTX_free(ctx->pctx); |
52 | 0 | #ifndef OPENSSL_NO_ENGINE |
53 | 0 | ENGINE_finish(ctx->engine); |
54 | 0 | #endif |
55 | 0 | OPENSSL_cleanse(ctx, sizeof(*ctx)); |
56 | |
|
57 | 0 | return 1; |
58 | 0 | } |
59 | | |
60 | | EVP_MD_CTX *EVP_MD_CTX_new(void) |
61 | 0 | { |
62 | 0 | return OPENSSL_zalloc(sizeof(EVP_MD_CTX)); |
63 | 0 | } |
64 | | |
65 | | void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
66 | 0 | { |
67 | 0 | EVP_MD_CTX_reset(ctx); |
68 | 0 | OPENSSL_free(ctx); |
69 | 0 | } |
70 | | |
71 | | int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) |
72 | 0 | { |
73 | 0 | EVP_MD_CTX_reset(ctx); |
74 | 0 | return EVP_DigestInit_ex(ctx, type, NULL); |
75 | 0 | } |
76 | | |
77 | | int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) |
78 | 0 | { |
79 | 0 | EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
80 | 0 | #ifndef OPENSSL_NO_ENGINE |
81 | | /* |
82 | | * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so |
83 | | * this context may already have an ENGINE! Try to avoid releasing the |
84 | | * previous handle, re-querying for an ENGINE, and having a |
85 | | * reinitialisation, when it may all be unnecessary. |
86 | | */ |
87 | 0 | if (ctx->engine && ctx->digest && |
88 | 0 | (type == NULL || (type->type == ctx->digest->type))) |
89 | 0 | goto skip_to_init; |
90 | | |
91 | 0 | if (type) { |
92 | | /* |
93 | | * Ensure an ENGINE left lying around from last time is cleared (the |
94 | | * previous check attempted to avoid this if the same ENGINE and |
95 | | * EVP_MD could be used). |
96 | | */ |
97 | 0 | ENGINE_finish(ctx->engine); |
98 | 0 | if (impl != NULL) { |
99 | 0 | if (!ENGINE_init(impl)) { |
100 | 0 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); |
101 | 0 | return 0; |
102 | 0 | } |
103 | 0 | } else { |
104 | | /* Ask if an ENGINE is reserved for this job */ |
105 | 0 | impl = ENGINE_get_digest_engine(type->type); |
106 | 0 | } |
107 | 0 | if (impl != NULL) { |
108 | | /* There's an ENGINE for this job ... (apparently) */ |
109 | 0 | const EVP_MD *d = ENGINE_get_digest(impl, type->type); |
110 | |
|
111 | 0 | if (d == NULL) { |
112 | 0 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); |
113 | 0 | ENGINE_finish(impl); |
114 | 0 | return 0; |
115 | 0 | } |
116 | | /* We'll use the ENGINE's private digest definition */ |
117 | 0 | type = d; |
118 | | /* |
119 | | * Store the ENGINE functional reference so we know 'type' came |
120 | | * from an ENGINE and we need to release it when done. |
121 | | */ |
122 | 0 | ctx->engine = impl; |
123 | 0 | } else |
124 | 0 | ctx->engine = NULL; |
125 | 0 | } else { |
126 | 0 | if (!ctx->digest) { |
127 | 0 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET); |
128 | 0 | return 0; |
129 | 0 | } |
130 | 0 | type = ctx->digest; |
131 | 0 | } |
132 | 0 | #endif |
133 | 0 | if (ctx->digest != type) { |
134 | 0 | cleanup_old_md_data(ctx, 1); |
135 | |
|
136 | 0 | ctx->digest = type; |
137 | 0 | if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) { |
138 | 0 | ctx->update = type->update; |
139 | 0 | ctx->md_data = OPENSSL_zalloc(type->ctx_size); |
140 | 0 | if (ctx->md_data == NULL) { |
141 | 0 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE); |
142 | 0 | return 0; |
143 | 0 | } |
144 | 0 | } |
145 | 0 | } |
146 | 0 | #ifndef OPENSSL_NO_ENGINE |
147 | 0 | skip_to_init: |
148 | 0 | #endif |
149 | 0 | if (ctx->pctx) { |
150 | 0 | int r; |
151 | 0 | r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG, |
152 | 0 | EVP_PKEY_CTRL_DIGESTINIT, 0, ctx); |
153 | 0 | if (r <= 0 && (r != -2)) |
154 | 0 | return 0; |
155 | 0 | } |
156 | 0 | if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) |
157 | 0 | return 1; |
158 | 0 | return ctx->digest->init(ctx); |
159 | 0 | } |
160 | | |
161 | | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count) |
162 | 0 | { |
163 | 0 | if (count == 0) |
164 | 0 | return 1; |
165 | | |
166 | 0 | return ctx->update(ctx, data, count); |
167 | 0 | } |
168 | | |
169 | | /* The caller can assume that this removes any secret data from the context */ |
170 | | int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) |
171 | 0 | { |
172 | 0 | int ret; |
173 | 0 | ret = EVP_DigestFinal_ex(ctx, md, size); |
174 | 0 | EVP_MD_CTX_reset(ctx); |
175 | 0 | return ret; |
176 | 0 | } |
177 | | |
178 | | /* The caller can assume that this removes any secret data from the context */ |
179 | | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) |
180 | 0 | { |
181 | 0 | int ret; |
182 | |
|
183 | 0 | OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); |
184 | 0 | ret = ctx->digest->final(ctx, md); |
185 | 0 | if (size != NULL) |
186 | 0 | *size = ctx->digest->md_size; |
187 | 0 | if (ctx->digest->cleanup) { |
188 | 0 | ctx->digest->cleanup(ctx); |
189 | 0 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
190 | 0 | } |
191 | 0 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
192 | 0 | return ret; |
193 | 0 | } |
194 | | |
195 | | int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size) |
196 | 0 | { |
197 | 0 | int ret = 0; |
198 | |
|
199 | 0 | if (ctx->digest->flags & EVP_MD_FLAG_XOF |
200 | 0 | && size <= INT_MAX |
201 | 0 | && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) { |
202 | 0 | ret = ctx->digest->final(ctx, md); |
203 | |
|
204 | 0 | if (ctx->digest->cleanup != NULL) { |
205 | 0 | ctx->digest->cleanup(ctx); |
206 | 0 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
207 | 0 | } |
208 | 0 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
209 | 0 | } else { |
210 | 0 | EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH); |
211 | 0 | } |
212 | |
|
213 | 0 | return ret; |
214 | 0 | } |
215 | | |
216 | | int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
217 | 0 | { |
218 | 0 | EVP_MD_CTX_reset(out); |
219 | 0 | return EVP_MD_CTX_copy_ex(out, in); |
220 | 0 | } |
221 | | |
222 | | int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
223 | 0 | { |
224 | 0 | unsigned char *tmp_buf; |
225 | 0 | if ((in == NULL) || (in->digest == NULL)) { |
226 | 0 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED); |
227 | 0 | return 0; |
228 | 0 | } |
229 | 0 | #ifndef OPENSSL_NO_ENGINE |
230 | | /* Make sure it's safe to copy a digest context using an ENGINE */ |
231 | 0 | if (in->engine && !ENGINE_init(in->engine)) { |
232 | 0 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB); |
233 | 0 | return 0; |
234 | 0 | } |
235 | 0 | #endif |
236 | | |
237 | 0 | if (out->digest == in->digest) { |
238 | 0 | tmp_buf = out->md_data; |
239 | 0 | EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE); |
240 | 0 | } else |
241 | 0 | tmp_buf = NULL; |
242 | 0 | EVP_MD_CTX_reset(out); |
243 | 0 | memcpy(out, in, sizeof(*out)); |
244 | | |
245 | | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ |
246 | 0 | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); |
247 | | |
248 | | /* Null these variables, since they are getting fixed up |
249 | | * properly below. Anything else may cause a memleak and/or |
250 | | * double free if any of the memory allocations below fail |
251 | | */ |
252 | 0 | out->md_data = NULL; |
253 | 0 | out->pctx = NULL; |
254 | |
|
255 | 0 | if (in->md_data && out->digest->ctx_size) { |
256 | 0 | if (tmp_buf) |
257 | 0 | out->md_data = tmp_buf; |
258 | 0 | else { |
259 | 0 | out->md_data = OPENSSL_malloc(out->digest->ctx_size); |
260 | 0 | if (out->md_data == NULL) { |
261 | 0 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE); |
262 | 0 | return 0; |
263 | 0 | } |
264 | 0 | } |
265 | 0 | memcpy(out->md_data, in->md_data, out->digest->ctx_size); |
266 | 0 | } |
267 | | |
268 | 0 | out->update = in->update; |
269 | |
|
270 | 0 | if (in->pctx) { |
271 | 0 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); |
272 | 0 | if (!out->pctx) { |
273 | 0 | EVP_MD_CTX_reset(out); |
274 | 0 | return 0; |
275 | 0 | } |
276 | 0 | } |
277 | | |
278 | 0 | if (out->digest->copy) |
279 | 0 | return out->digest->copy(out, in); |
280 | | |
281 | 0 | return 1; |
282 | 0 | } |
283 | | |
284 | | int EVP_Digest(const void *data, size_t count, |
285 | | unsigned char *md, unsigned int *size, const EVP_MD *type, |
286 | | ENGINE *impl) |
287 | 0 | { |
288 | 0 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
289 | 0 | int ret; |
290 | |
|
291 | 0 | if (ctx == NULL) |
292 | 0 | return 0; |
293 | 0 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT); |
294 | 0 | ret = EVP_DigestInit_ex(ctx, type, impl) |
295 | 0 | && EVP_DigestUpdate(ctx, data, count) |
296 | 0 | && EVP_DigestFinal_ex(ctx, md, size); |
297 | 0 | EVP_MD_CTX_free(ctx); |
298 | |
|
299 | 0 | return ret; |
300 | 0 | } |
301 | | |
302 | | int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2) |
303 | 0 | { |
304 | 0 | if (ctx->digest && ctx->digest->md_ctrl) { |
305 | 0 | int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2); |
306 | 0 | if (ret <= 0) |
307 | 0 | return 0; |
308 | 0 | return 1; |
309 | 0 | } |
310 | 0 | return 0; |
311 | 0 | } |