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