Skip to content

Commit

Permalink
Provide EVP_MD_CTX_new(), EVP_MD_CTX_free() and EVP_MD_CTX_reset().
Browse files Browse the repository at this point in the history
  • Loading branch information
jsing committed Feb 17, 2018
1 parent 0f8b76a commit 651a8b5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
3 changes: 3 additions & 0 deletions src/lib/libcrypto/Symbols.list
Original file line number Diff line number Diff line change
Expand Up @@ -1274,8 +1274,11 @@ EVP_MD_CTX_copy_ex
EVP_MD_CTX_create
EVP_MD_CTX_ctrl
EVP_MD_CTX_destroy
EVP_MD_CTX_free
EVP_MD_CTX_init
EVP_MD_CTX_md
EVP_MD_CTX_new
EVP_MD_CTX_reset
EVP_MD_CTX_set_flags
EVP_MD_CTX_test_flags
EVP_MD_block_size
Expand Down
59 changes: 40 additions & 19 deletions src/lib/libcrypto/evp/digest.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: digest.c,v 1.28 2017/05/02 03:59:44 deraadt Exp $ */
/* $OpenBSD: digest.c,v 1.29 2018/02/17 14:55:31 jsing Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
Expand Down Expand Up @@ -122,18 +122,6 @@
#include <openssl/engine.h>
#endif

void
EVP_MD_CTX_init(EVP_MD_CTX *ctx)
{
memset(ctx, 0, sizeof *ctx);
}

EVP_MD_CTX *
EVP_MD_CTX_create(void)
{
return calloc(1, sizeof(EVP_MD_CTX));
}

int
EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
{
Expand Down Expand Up @@ -339,20 +327,53 @@ EVP_Digest(const void *data, size_t count,
return ret;
}

EVP_MD_CTX *
EVP_MD_CTX_new(void)
{
return calloc(1, sizeof(EVP_MD_CTX));
}

void
EVP_MD_CTX_free(EVP_MD_CTX *ctx)
{
if (ctx == NULL)
return;

EVP_MD_CTX_cleanup(ctx);

free(ctx);
}

void
EVP_MD_CTX_init(EVP_MD_CTX *ctx)
{
memset(ctx, 0, sizeof(*ctx));
}

int
EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
{
return EVP_MD_CTX_cleanup(ctx);
}

EVP_MD_CTX *
EVP_MD_CTX_create(void)
{
return EVP_MD_CTX_new();
}

void
EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
{
if (ctx) {
EVP_MD_CTX_cleanup(ctx);
free(ctx);
}
EVP_MD_CTX_free(ctx);
}

/* This call frees resources associated with the context */
int
EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
{
/* Don't assume ctx->md_data was cleaned in EVP_Digest_Final,
/*
* Don't assume ctx->md_data was cleaned in EVP_Digest_Final,
* because sometimes only copies of the context are ever finalised.
*/
if (ctx->digest && ctx->digest->cleanup &&
Expand All @@ -368,7 +389,7 @@ EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
* functional reference we held for this reason. */
ENGINE_finish(ctx->engine);
#endif
memset(ctx, 0, sizeof *ctx);
memset(ctx, 0, sizeof(*ctx));

return 1;
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/libcrypto/evp/evp.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: evp.h,v 1.55 2018/02/17 13:47:36 tb Exp $ */
/* $OpenBSD: evp.h,v 1.56 2018/02/17 14:55:31 jsing Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
Expand Down Expand Up @@ -535,15 +535,19 @@ int EVP_Cipher(EVP_CIPHER_CTX *c, unsigned char *out, const unsigned char *in,
#define EVP_delete_digest_alias(alias) \
OBJ_NAME_remove(alias,OBJ_NAME_TYPE_MD_METH|OBJ_NAME_ALIAS);

EVP_MD_CTX *EVP_MD_CTX_new(void);
void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
EVP_MD_CTX *EVP_MD_CTX_create(void);
void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);

int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
Expand Down

0 comments on commit 651a8b5

Please sign in to comment.