summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2023-12-29 06:59:24 +0000
committertb <>2023-12-29 06:59:24 +0000
commit9a8673163304ff42da75dd394141d665b246f1bc (patch)
tree80a98cc5eea79e68f3c1cf5f6ced41fb70f7cd42 /src
parentdcce5b53c436a7f7e89c09cb79d8512ee35e639d (diff)
downloadopenbsd-9a8673163304ff42da75dd394141d665b246f1bc.tar.gz
openbsd-9a8673163304ff42da75dd394141d665b246f1bc.tar.bz2
openbsd-9a8673163304ff42da75dd394141d665b246f1bc.zip
Move the copy/copy_ex stuff down below the new/free/clear mess
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/evp/evp_digest.c127
1 files changed, 64 insertions, 63 deletions
diff --git a/src/lib/libcrypto/evp/evp_digest.c b/src/lib/libcrypto/evp/evp_digest.c
index 583c454845..75787d3f7d 100644
--- a/src/lib/libcrypto/evp/evp_digest.c
+++ b/src/lib/libcrypto/evp/evp_digest.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: evp_digest.c,v 1.2 2023/12/29 06:08:01 tb Exp $ */ 1/* $OpenBSD: evp_digest.c,v 1.3 2023/12/29 06:59:24 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -217,68 +217,6 @@ EVP_Digest(const void *data, size_t count,
217 return ret; 217 return ret;
218} 218}
219 219
220int
221EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
222{
223 EVP_MD_CTX_init(out);
224 return EVP_MD_CTX_copy_ex(out, in);
225}
226
227int
228EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
229{
230 unsigned char *tmp_buf;
231
232 if ((in == NULL) || (in->digest == NULL)) {
233 EVPerror(EVP_R_INPUT_NOT_INITIALIZED);
234 return 0;
235 }
236
237 if (out->digest == in->digest) {
238 tmp_buf = out->md_data;
239 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
240 } else
241 tmp_buf = NULL;
242 EVP_MD_CTX_cleanup(out);
243 memcpy(out, in, sizeof *out);
244 out->md_data = NULL;
245 out->pctx = NULL;
246
247 /*
248 * Because of the EVP_PKEY_CTX_dup() below, EVP_MD_CTX_cleanup() needs
249 * to free out->pctx in all cases (even if this flag is set on in).
250 */
251 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
252
253 if (in->md_data && out->digest->ctx_size) {
254 if (tmp_buf) {
255 out->md_data = tmp_buf;
256 } else {
257 out->md_data = calloc(1, out->digest->ctx_size);
258 if (out->md_data == NULL) {
259 EVPerror(ERR_R_MALLOC_FAILURE);
260 return 0;
261 }
262 }
263 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
264 }
265
266 out->update = in->update;
267
268 if (in->pctx) {
269 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
270 if (!out->pctx) {
271 EVP_MD_CTX_cleanup(out);
272 return 0;
273 }
274 }
275
276 if (out->digest->copy)
277 return out->digest->copy(out, in);
278
279 return 1;
280}
281
282EVP_MD_CTX * 220EVP_MD_CTX *
283EVP_MD_CTX_new(void) 221EVP_MD_CTX_new(void)
284{ 222{
@@ -346,6 +284,69 @@ EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
346} 284}
347 285
348int 286int
287EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
288{
289 EVP_MD_CTX_init(out);
290 return EVP_MD_CTX_copy_ex(out, in);
291}
292
293int
294EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
295{
296 unsigned char *tmp_buf;
297
298 if ((in == NULL) || (in->digest == NULL)) {
299 EVPerror(EVP_R_INPUT_NOT_INITIALIZED);
300 return 0;
301 }
302
303 if (out->digest == in->digest) {
304 tmp_buf = out->md_data;
305 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
306 } else
307 tmp_buf = NULL;
308 EVP_MD_CTX_cleanup(out);
309 memcpy(out, in, sizeof *out);
310 out->md_data = NULL;
311 out->pctx = NULL;
312
313 /*
314 * Because of the EVP_PKEY_CTX_dup() below, EVP_MD_CTX_cleanup() needs
315 * to free out->pctx in all cases (even if this flag is set on in).
316 */
317 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
318
319 if (in->md_data && out->digest->ctx_size) {
320 if (tmp_buf) {
321 out->md_data = tmp_buf;
322 } else {
323 out->md_data = calloc(1, out->digest->ctx_size);
324 if (out->md_data == NULL) {
325 EVPerror(ERR_R_MALLOC_FAILURE);
326 return 0;
327 }
328 }
329 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
330 }
331
332 out->update = in->update;
333
334 if (in->pctx) {
335 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
336 if (!out->pctx) {
337 EVP_MD_CTX_cleanup(out);
338 return 0;
339 }
340 }
341
342 if (out->digest->copy)
343 return out->digest->copy(out, in);
344
345 return 1;
346}
347
348
349int
349EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr) 350EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
350{ 351{
351 int ret; 352 int ret;