From 248eeb82f0b780fdaa071a819d950de57a6f0fd9 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Sun, 10 Jul 2005 03:52:56 +0000 Subject: [PATCH] This patch adds implementation of SHA2 to pgcrypto. New hashes: SHA256, SHA384, SHA512. Marko Kreen --- contrib/pgcrypto/Makefile | 7 +- contrib/pgcrypto/internal.c | 206 +++++++++++++++++++++++++++++++++++- 2 files changed, 209 insertions(+), 4 deletions(-) diff --git a/contrib/pgcrypto/Makefile b/contrib/pgcrypto/Makefile index 63f73816d7..85b066d659 100644 --- a/contrib/pgcrypto/Makefile +++ b/contrib/pgcrypto/Makefile @@ -1,19 +1,20 @@ # -# $PostgreSQL: pgsql/contrib/pgcrypto/Makefile,v 1.17 2005/07/06 16:14:42 tgl Exp $ +# $PostgreSQL: pgsql/contrib/pgcrypto/Makefile,v 1.18 2005/07/10 03:52:56 momjian Exp $ # # if you don't have OpenSSL, you can use libc random() or /dev/urandom INT_CFLAGS = -DRAND_SILLY #INT_CFLAGS = -DRAND_DEV=\"/dev/urandom\" -INT_SRCS = md5.c sha1.c internal.c blf.c rijndael.c +INT_SRCS = md5.c sha1.c sha2.c internal.c blf.c rijndael.c +INT_TESTS = sha2 OSSL_CFLAGS = -DRAND_OPENSSL OSSL_SRCS = openssl.c OSSL_TESTS = des 3des cast5 CF_SRCS = $(if $(subst no,,$(with_openssl)), $(OSSL_SRCS), $(INT_SRCS)) -CF_TESTS = $(if $(subst no,,$(with_openssl)), $(OSSL_TESTS)) +CF_TESTS = $(if $(subst no,,$(with_openssl)), $(OSSL_TESTS), $(INT_TESTS)) CF_CFLAGS = $(if $(subst no,,$(with_openssl)), $(OSSL_CFLAGS), $(INT_CFLAGS)) PG_CPPFLAGS = $(CF_CFLAGS) diff --git a/contrib/pgcrypto/internal.c b/contrib/pgcrypto/internal.c index 0a9270ff94..ff034dcf80 100644 --- a/contrib/pgcrypto/internal.c +++ b/contrib/pgcrypto/internal.c @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.16 2005/03/21 05:19:55 neilc Exp $ + * $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.17 2005/07/10 03:52:56 momjian Exp $ */ @@ -36,6 +36,7 @@ #include "md5.h" #include "sha1.h" +#include "sha2.h" #include "blf.h" #include "rijndael.h" @@ -56,6 +57,9 @@ static void init_md5(PX_MD * h); static void init_sha1(PX_MD * h); +static void init_sha256(PX_MD * h); +static void init_sha384(PX_MD * h); +static void init_sha512(PX_MD * h); struct int_digest { @@ -67,6 +71,9 @@ static const struct int_digest int_digest_list[] = { { "md5", init_md5 }, { "sha1", init_sha1 }, + { "sha256", init_sha256 }, + { "sha384", init_sha384 }, + { "sha512", init_sha512 }, { NULL, NULL } }; @@ -164,6 +171,146 @@ int_sha1_free(PX_MD * h) px_free(h); } +/* SHA256 */ + +static unsigned +int_sha256_len(PX_MD * h) +{ + return SHA256_DIGEST_LENGTH; +} + +static unsigned +int_sha256_block_len(PX_MD * h) +{ + return SHA256_BLOCK_LENGTH; +} + +static void +int_sha256_update(PX_MD * h, const uint8 *data, unsigned dlen) +{ + SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr; + + SHA256_Update(ctx, data, dlen); +} + +static void +int_sha256_reset(PX_MD * h) +{ + SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr; + + SHA256_Init(ctx); +} + +static void +int_sha256_finish(PX_MD * h, uint8 *dst) +{ + SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr; + + SHA256_Final(dst, ctx); +} + +static void +int_sha256_free(PX_MD * h) +{ + SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr; + + px_free(ctx); + px_free(h); +} +/* SHA384 */ + +static unsigned +int_sha384_len(PX_MD * h) +{ + return SHA384_DIGEST_LENGTH; +} + +static unsigned +int_sha384_block_len(PX_MD * h) +{ + return SHA384_BLOCK_LENGTH; +} + +static void +int_sha384_update(PX_MD * h, const uint8 *data, unsigned dlen) +{ + SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr; + + SHA384_Update(ctx, data, dlen); +} + +static void +int_sha384_reset(PX_MD * h) +{ + SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr; + + SHA384_Init(ctx); +} + +static void +int_sha384_finish(PX_MD * h, uint8 *dst) +{ + SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr; + + SHA384_Final(dst, ctx); +} + +static void +int_sha384_free(PX_MD * h) +{ + SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr; + + px_free(ctx); + px_free(h); +} + +/* SHA512 */ + +static unsigned +int_sha512_len(PX_MD * h) +{ + return SHA512_DIGEST_LENGTH; +} + +static unsigned +int_sha512_block_len(PX_MD * h) +{ + return SHA512_BLOCK_LENGTH; +} + +static void +int_sha512_update(PX_MD * h, const uint8 *data, unsigned dlen) +{ + SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr; + + SHA512_Update(ctx, data, dlen); +} + +static void +int_sha512_reset(PX_MD * h) +{ + SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr; + + SHA512_Init(ctx); +} + +static void +int_sha512_finish(PX_MD * h, uint8 *dst) +{ + SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr; + + SHA512_Final(dst, ctx); +} + +static void +int_sha512_free(PX_MD * h) +{ + SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr; + + px_free(ctx); + px_free(h); +} + /* init functions */ static void @@ -204,6 +351,63 @@ init_sha1(PX_MD * md) md->reset(md); } +static void +init_sha256(PX_MD * md) +{ + SHA256_CTX *ctx; + + ctx = px_alloc(sizeof(*ctx)); + + md->p.ptr = ctx; + + md->result_size = int_sha256_len; + md->block_size = int_sha256_block_len; + md->reset = int_sha256_reset; + md->update = int_sha256_update; + md->finish = int_sha256_finish; + md->free = int_sha256_free; + + md->reset(md); +} + +static void +init_sha384(PX_MD * md) +{ + SHA384_CTX *ctx; + + ctx = px_alloc(sizeof(*ctx)); + + md->p.ptr = ctx; + + md->result_size = int_sha384_len; + md->block_size = int_sha384_block_len; + md->reset = int_sha384_reset; + md->update = int_sha384_update; + md->finish = int_sha384_finish; + md->free = int_sha384_free; + + md->reset(md); +} + +static void +init_sha512(PX_MD * md) +{ + SHA512_CTX *ctx; + + ctx = px_alloc(sizeof(*ctx)); + + md->p.ptr = ctx; + + md->result_size = int_sha512_len; + md->block_size = int_sha512_block_len; + md->reset = int_sha512_reset; + md->update = int_sha512_update; + md->finish = int_sha512_finish; + md->free = int_sha512_free; + + md->reset(md); +} + /* * ciphers generally */ -- 2.40.0