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