Commit 26188931 authored by Ben Laurie's avatar Ben Laurie
Browse files

Make MD functions take EVP_MD_CTX * instead of void *, add copy() function.

parent 36026dfc
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -12,6 +12,21 @@
         *) applies to 0.9.6a/0.9.6b/0.9.6c and 0.9.7
         +) applies to 0.9.7 only

  +) Add a copy() function to EVP_MD.
     [Ben Laurie]

  +) Make EVP_MD routines take a context pointer instead of just the
     md_data voud pointer.
     [Ben Laurie]

  +) Add flags to EVP_MD and EVP_MD_CTX. EVP_MD_FLAG_ONESHOT indicates
     that the digest can only process a single chunk of data
     (typically because it is provided by a piece of
     hardware). EVP_MD_CTX_FLAG_ONESHOT indicates that the application
     is only going to provide a single chunk of data, and hence the
     framework needn't accumulate the data for oneshot drivers.
     [Ben Laurie]

  +) As with "ERR", make it possible to replace the underlying "ex_data"
     functions. This change also alters the storage and management of global
     ex_data state - it's now all inside ex_data.c and all "class" code (eg.
+9 −1
Original line number Diff line number Diff line
@@ -513,7 +513,15 @@ names.o: ../../include/openssl/sha.h ../../include/openssl/stack.h
names.o: ../../include/openssl/symhacks.h ../../include/openssl/types.h
names.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
names.o: ../cryptlib.h names.c
openbsd_hw.o: openbsd_hw.c
openbsd_hw.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
openbsd_hw.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h
openbsd_hw.o: ../../include/openssl/e_os2.h ../../include/openssl/evp.h
openbsd_hw.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
openbsd_hw.o: ../../include/openssl/opensslconf.h
openbsd_hw.o: ../../include/openssl/opensslv.h ../../include/openssl/rsa.h
openbsd_hw.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
openbsd_hw.o: ../../include/openssl/symhacks.h ../../include/openssl/types.h
openbsd_hw.o: evp_locl.h openbsd_hw.c
p5_crpt.o: ../../e_os.h ../../include/openssl/asn1.h
p5_crpt.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
p5_crpt.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
+20 −10
Original line number Diff line number Diff line
@@ -84,29 +84,30 @@ int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
	{
	if(ctx->digest != type)
		{
		if(ctx->md_data != NULL)
		if(ctx->digest && ctx->digest->ctx_size)
			OPENSSL_free(ctx->md_data);
		ctx->digest=type;
		if(type->ctx_size)
#ifdef CRYPTO_MDEBUG
			ctx->md_data=CRYPTO_malloc(type->ctx_size,file,line);
#else
			ctx->md_data=OPENSSL_malloc(type->ctx_size);
#endif
		}
	return type->init(ctx->md_data);
	return type->init(ctx);
	}

int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data,
	     unsigned int count)
	{
	return ctx->digest->update(ctx->md_data,data,(unsigned long)count);
	return ctx->digest->update(ctx,data,(unsigned long)count);
	}

/* The caller can assume that this removes any secret data from the context */
int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
	{
	int ret;
	ret=ctx->digest->final(md,ctx->md_data);
	ret=ctx->digest->final(ctx,md);
	if (size != NULL)
		*size=ctx->digest->md_size;
	/* FIXME: add a cleanup function to the ctx? */
@@ -120,11 +121,19 @@ int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
        EVPerr(EVP_F_EVP_MD_CTX_COPY,EVP_R_INPUT_NOT_INITIALIZED);
	return 0;
    }

    EVP_MD_CTX_cleanup(out);
    memcpy(out,in,sizeof *out);

    if(out->digest->ctx_size)
	{
	out->md_data=OPENSSL_malloc(out->digest->ctx_size);
    /* FIXME: we really need a per-MD copy function */
	memcpy(out->md_data,in->md_data,out->digest->ctx_size);
	}

    if(out->digest->copy)
	return out->digest->copy(out,in);

    return 1;
}

@@ -135,6 +144,7 @@ int EVP_Digest(void *data, unsigned int count,
	int ret;

	EVP_MD_CTX_init(&ctx);
	EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT);
	ret=EVP_DigestInit(&ctx, type)
	  && EVP_DigestUpdate(&ctx, data, count)
	  && EVP_DigestFinal(&ctx, md, size);
@@ -155,7 +165,7 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
	/* Don't assume ctx->md_data was cleaned in EVP_Digest_Final,
	 * because sometimes only copies of the context are ever finalised.
	 */
	if(ctx->md_data)
	if(ctx->digest && ctx->digest->ctx_size && ctx->md_data)
		{
		memset(ctx->md_data,0,ctx->digest->ctx_size);
		OPENSSL_free(ctx->md_data);
+17 −6
Original line number Diff line number Diff line
@@ -217,10 +217,13 @@ struct env_md_st
	int type;
	int pkey_type;
	int md_size;
	int (*init)();
	int (*update)();
	int (*final)();
	unsigned long flags;
	int (*init)(EVP_MD_CTX *ctx);
	int (*update)(EVP_MD_CTX *ctx,const void *data,unsigned long count);
	int (*final)(EVP_MD_CTX *ctx,unsigned char *md);
	int (*copy)(EVP_MD_CTX *to,const EVP_MD_CTX *from);

	/* FIXME: prototype these some day */
	int (*sign)();
	int (*verify)();
	int required_pkey_type[5]; /*EVP_PKEY_xxx */
@@ -228,7 +231,8 @@ struct env_md_st
	int ctx_size; /* how big does the ctx->md_data need to be */
	} /* EVP_MD */;


#define EVP_MD_FLAG_ONESHOT	0x0001 /* digest can only handle a single
					* block */

#define EVP_PKEY_NULL_method	NULL,NULL,{0,0,0,0}

@@ -254,11 +258,17 @@ struct env_md_st

#endif /* !EVP_MD */

typedef struct env_md_ctx_st
struct env_md_ctx_st
	{
	const EVP_MD *digest;
	unsigned long flags;
	void *md_data;
	} EVP_MD_CTX;
	} /* EVP_MD_CTX */;

/* values for EVP_MD_CTX flags */

#define EVP_MD_CTX_FLAG_ONESHOT		0x0001 /* digest update will be called
						* once only */

struct evp_cipher_st
	{
@@ -443,6 +453,7 @@ int EVP_MD_CTX_cleanup(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_copy(EVP_MD_CTX *out,const EVP_MD_CTX *in);  
#define EVP_MD_CTX_set_flags(ctx,flgs) ((ctx)->flags|=(flgs))
#ifdef CRYPTO_MDEBUG
int	EVP_DigestInit_dbg(EVP_MD_CTX *ctx, const EVP_MD *type,
			   const char *file,int line);
+14 −3
Original line number Diff line number Diff line
@@ -63,14 +63,25 @@
#include <openssl/x509.h>

#ifndef OPENSSL_NO_SHA
static int init(EVP_MD_CTX *ctx)
	{ return SHA1_Init(ctx->md_data); }

static int update(EVP_MD_CTX *ctx,const void *data,unsigned long count)
	{ return SHA1_Update(ctx->md_data,data,count); }

static int final(EVP_MD_CTX *ctx,unsigned char *md)
	{ return SHA1_Final(md,ctx->md_data); }

static const EVP_MD dsa_md=
	{
	NID_dsaWithSHA,
	NID_dsaWithSHA,
	SHA_DIGEST_LENGTH,
	SHA1_Init,
	SHA1_Update,
	SHA1_Final,
	0,
	init,
	update,
	final,
	NULL,
	EVP_PKEY_DSA_method,
	SHA_CBLOCK,
	sizeof(EVP_MD *)+sizeof(SHA_CTX),
Loading