From 920623508bdc9dad446365b912db35527de1d067 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Wed, 12 Oct 2016 16:04:42 +0900
Subject: [PATCH 1/9] Refactor SHA2 functions and move them to src/common/

This way both frontend and backends can refer to them if needed. Those
functions are taken from pgcrypto, which now fetches directly the source
files it needs from src/common/ when compiling its library.

A new interface, which is more PG-like is designed for those SHA2 functions,
allowing to link to either OpenSSL or the in-core stuff taken from KAME
as need be, which is the most flexible solution.
---
 contrib/pgcrypto/.gitignore             |   4 +
 contrib/pgcrypto/Makefile               |   5 +-
 contrib/pgcrypto/fortuna.c              |  12 +-
 contrib/pgcrypto/internal-sha2.c        |  82 ++--
 contrib/pgcrypto/sha2.h                 | 100 -----
 src/common/Makefile                     |   6 +
 {contrib/pgcrypto => src/common}/sha2.c | 722 ++++++++++++++++++--------------
 src/common/sha2_openssl.c               | 102 +++++
 src/include/common/sha2.h               | 115 +++++
 src/tools/msvc/Mkvcbuild.pm             |  22 +-
 10 files changed, 699 insertions(+), 471 deletions(-)
 delete mode 100644 contrib/pgcrypto/sha2.h
 rename {contrib/pgcrypto => src/common}/sha2.c (65%)
 create mode 100644 src/common/sha2_openssl.c
 create mode 100644 src/include/common/sha2.h

diff --git a/contrib/pgcrypto/.gitignore b/contrib/pgcrypto/.gitignore
index 5dcb3ff..30619bf 100644
--- a/contrib/pgcrypto/.gitignore
+++ b/contrib/pgcrypto/.gitignore
@@ -1,3 +1,7 @@
+# Source file copied from src/common
+/sha2.c
+/sha2_openssl.c
+
 # Generated subdirectories
 /log/
 /results/
diff --git a/contrib/pgcrypto/Makefile b/contrib/pgcrypto/Makefile
index 805db76..4085abb 100644
--- a/contrib/pgcrypto/Makefile
+++ b/contrib/pgcrypto/Makefile
@@ -4,7 +4,7 @@ INT_SRCS = md5.c sha1.c sha2.c internal.c internal-sha2.c blf.c rijndael.c \
 		fortuna.c random.c pgp-mpi-internal.c imath.c
 INT_TESTS = sha2
 
-OSSL_SRCS = openssl.c pgp-mpi-openssl.c
+OSSL_SRCS = openssl.c pgp-mpi-openssl.c sha2_openssl.c
 OSSL_TESTS = sha2 des 3des cast5
 
 ZLIB_TST = pgp-compression
@@ -59,6 +59,9 @@ SHLIB_LINK += $(filter -leay32, $(LIBS))
 SHLIB_LINK += -lws2_32
 endif
 
+sha2.c sha2_openssl.c: % : $(top_srcdir)/src/common/%
+	rm -f $@ && $(LN_S) $< .
+
 rijndael.o: rijndael.tbl
 
 rijndael.tbl:
diff --git a/contrib/pgcrypto/fortuna.c b/contrib/pgcrypto/fortuna.c
index 5028203..ba74db6 100644
--- a/contrib/pgcrypto/fortuna.c
+++ b/contrib/pgcrypto/fortuna.c
@@ -34,9 +34,9 @@
 #include <sys/time.h>
 #include <time.h>
 
+#include "common/sha2.h"
 #include "px.h"
 #include "rijndael.h"
-#include "sha2.h"
 #include "fortuna.h"
 
 
@@ -112,7 +112,7 @@
 #define CIPH_BLOCK		16
 
 /* for internal wrappers */
-#define MD_CTX			SHA256_CTX
+#define MD_CTX			pg_sha256_ctx
 #define CIPH_CTX		rijndael_ctx
 
 struct fortuna_state
@@ -154,22 +154,22 @@ ciph_encrypt(CIPH_CTX * ctx, const uint8 *in, uint8 *out)
 static void
 md_init(MD_CTX * ctx)
 {
-	SHA256_Init(ctx);
+	pg_sha256_init(ctx);
 }
 
 static void
 md_update(MD_CTX * ctx, const uint8 *data, int len)
 {
-	SHA256_Update(ctx, data, len);
+	pg_sha256_update(ctx, data, len);
 }
 
 static void
 md_result(MD_CTX * ctx, uint8 *dst)
 {
-	SHA256_CTX	tmp;
+	pg_sha256_ctx	tmp;
 
 	memcpy(&tmp, ctx, sizeof(*ctx));
-	SHA256_Final(dst, &tmp);
+	pg_sha256_final(&tmp, dst);
 	px_memset(&tmp, 0, sizeof(tmp));
 }
 
diff --git a/contrib/pgcrypto/internal-sha2.c b/contrib/pgcrypto/internal-sha2.c
index 55ec7e1..e06f554 100644
--- a/contrib/pgcrypto/internal-sha2.c
+++ b/contrib/pgcrypto/internal-sha2.c
@@ -33,8 +33,8 @@
 
 #include <time.h>
 
+#include "common/sha2.h"
 #include "px.h"
-#include "sha2.h"
 
 void		init_sha224(PX_MD *h);
 void		init_sha256(PX_MD *h);
@@ -46,43 +46,43 @@ void		init_sha512(PX_MD *h);
 static unsigned
 int_sha224_len(PX_MD *h)
 {
-	return SHA224_DIGEST_LENGTH;
+	return PG_SHA224_DIGEST_LENGTH;
 }
 
 static unsigned
 int_sha224_block_len(PX_MD *h)
 {
-	return SHA224_BLOCK_LENGTH;
+	return PG_SHA224_BLOCK_LENGTH;
 }
 
 static void
 int_sha224_update(PX_MD *h, const uint8 *data, unsigned dlen)
 {
-	SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
+	pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
 
-	SHA224_Update(ctx, data, dlen);
+	pg_sha224_update(ctx, data, dlen);
 }
 
 static void
 int_sha224_reset(PX_MD *h)
 {
-	SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
+	pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
 
-	SHA224_Init(ctx);
+	pg_sha224_init(ctx);
 }
 
 static void
 int_sha224_finish(PX_MD *h, uint8 *dst)
 {
-	SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
+	pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
 
-	SHA224_Final(dst, ctx);
+	pg_sha224_final(ctx, dst);
 }
 
 static void
 int_sha224_free(PX_MD *h)
 {
-	SHA224_CTX *ctx = (SHA224_CTX *) h->p.ptr;
+	pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
 	px_free(ctx);
@@ -94,43 +94,43 @@ int_sha224_free(PX_MD *h)
 static unsigned
 int_sha256_len(PX_MD *h)
 {
-	return SHA256_DIGEST_LENGTH;
+	return PG_SHA256_DIGEST_LENGTH;
 }
 
 static unsigned
 int_sha256_block_len(PX_MD *h)
 {
-	return SHA256_BLOCK_LENGTH;
+	return PG_SHA256_BLOCK_LENGTH;
 }
 
 static void
 int_sha256_update(PX_MD *h, const uint8 *data, unsigned dlen)
 {
-	SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
+	pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
 
-	SHA256_Update(ctx, data, dlen);
+	pg_sha256_update(ctx, data, dlen);
 }
 
 static void
 int_sha256_reset(PX_MD *h)
 {
-	SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
+	pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
 
-	SHA256_Init(ctx);
+	pg_sha256_init(ctx);
 }
 
 static void
 int_sha256_finish(PX_MD *h, uint8 *dst)
 {
-	SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
+	pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
 
-	SHA256_Final(dst, ctx);
+	pg_sha256_final(ctx, dst);
 }
 
 static void
 int_sha256_free(PX_MD *h)
 {
-	SHA256_CTX *ctx = (SHA256_CTX *) h->p.ptr;
+	pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
 	px_free(ctx);
@@ -142,43 +142,43 @@ int_sha256_free(PX_MD *h)
 static unsigned
 int_sha384_len(PX_MD *h)
 {
-	return SHA384_DIGEST_LENGTH;
+	return PG_SHA384_DIGEST_LENGTH;
 }
 
 static unsigned
 int_sha384_block_len(PX_MD *h)
 {
-	return SHA384_BLOCK_LENGTH;
+	return PG_SHA384_BLOCK_LENGTH;
 }
 
 static void
 int_sha384_update(PX_MD *h, const uint8 *data, unsigned dlen)
 {
-	SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
+	pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
 
-	SHA384_Update(ctx, data, dlen);
+	pg_sha384_update(ctx, data, dlen);
 }
 
 static void
 int_sha384_reset(PX_MD *h)
 {
-	SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
+	pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
 
-	SHA384_Init(ctx);
+	pg_sha384_init(ctx);
 }
 
 static void
 int_sha384_finish(PX_MD *h, uint8 *dst)
 {
-	SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
+	pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
 
-	SHA384_Final(dst, ctx);
+	pg_sha384_final(ctx, dst);
 }
 
 static void
 int_sha384_free(PX_MD *h)
 {
-	SHA384_CTX *ctx = (SHA384_CTX *) h->p.ptr;
+	pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
 	px_free(ctx);
@@ -190,43 +190,43 @@ int_sha384_free(PX_MD *h)
 static unsigned
 int_sha512_len(PX_MD *h)
 {
-	return SHA512_DIGEST_LENGTH;
+	return PG_SHA512_DIGEST_LENGTH;
 }
 
 static unsigned
 int_sha512_block_len(PX_MD *h)
 {
-	return SHA512_BLOCK_LENGTH;
+	return PG_SHA512_BLOCK_LENGTH;
 }
 
 static void
 int_sha512_update(PX_MD *h, const uint8 *data, unsigned dlen)
 {
-	SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
+	pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
 
-	SHA512_Update(ctx, data, dlen);
+	pg_sha512_update(ctx, data, dlen);
 }
 
 static void
 int_sha512_reset(PX_MD *h)
 {
-	SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
+	pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
 
-	SHA512_Init(ctx);
+	pg_sha512_init(ctx);
 }
 
 static void
 int_sha512_finish(PX_MD *h, uint8 *dst)
 {
-	SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
+	pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
 
-	SHA512_Final(dst, ctx);
+	pg_sha512_final(ctx, dst);
 }
 
 static void
 int_sha512_free(PX_MD *h)
 {
-	SHA512_CTX *ctx = (SHA512_CTX *) h->p.ptr;
+	pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
 
 	px_memset(ctx, 0, sizeof(*ctx));
 	px_free(ctx);
@@ -238,7 +238,7 @@ int_sha512_free(PX_MD *h)
 void
 init_sha224(PX_MD *md)
 {
-	SHA224_CTX *ctx;
+	pg_sha224_ctx *ctx;
 
 	ctx = px_alloc(sizeof(*ctx));
 	memset(ctx, 0, sizeof(*ctx));
@@ -258,7 +258,7 @@ init_sha224(PX_MD *md)
 void
 init_sha256(PX_MD *md)
 {
-	SHA256_CTX *ctx;
+	pg_sha256_ctx *ctx;
 
 	ctx = px_alloc(sizeof(*ctx));
 	memset(ctx, 0, sizeof(*ctx));
@@ -278,7 +278,7 @@ init_sha256(PX_MD *md)
 void
 init_sha384(PX_MD *md)
 {
-	SHA384_CTX *ctx;
+	pg_sha384_ctx *ctx;
 
 	ctx = px_alloc(sizeof(*ctx));
 	memset(ctx, 0, sizeof(*ctx));
@@ -298,7 +298,7 @@ init_sha384(PX_MD *md)
 void
 init_sha512(PX_MD *md)
 {
-	SHA512_CTX *ctx;
+	pg_sha512_ctx *ctx;
 
 	ctx = px_alloc(sizeof(*ctx));
 	memset(ctx, 0, sizeof(*ctx));
diff --git a/contrib/pgcrypto/sha2.h b/contrib/pgcrypto/sha2.h
deleted file mode 100644
index 501f0e0..0000000
--- a/contrib/pgcrypto/sha2.h
+++ /dev/null
@@ -1,100 +0,0 @@
-/*	contrib/pgcrypto/sha2.h */
-/*	$OpenBSD: sha2.h,v 1.2 2004/04/28 23:11:57 millert Exp $	*/
-
-/*
- * FILE:	sha2.h
- * AUTHOR:	Aaron D. Gifford <me@aarongifford.com>
- *
- * Copyright (c) 2000-2001, Aaron D. Gifford
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *	  notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *	  notice, this list of conditions and the following disclaimer in the
- *	  documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the copyright holder nor the names of contributors
- *	  may be used to endorse or promote products derived from this software
- *	  without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
- */
-
-#ifndef _SHA2_H
-#define _SHA2_H
-
-/* avoid conflict with OpenSSL */
-#define SHA256_Init pg_SHA256_Init
-#define SHA256_Update pg_SHA256_Update
-#define SHA256_Final pg_SHA256_Final
-#define SHA384_Init pg_SHA384_Init
-#define SHA384_Update pg_SHA384_Update
-#define SHA384_Final pg_SHA384_Final
-#define SHA512_Init pg_SHA512_Init
-#define SHA512_Update pg_SHA512_Update
-#define SHA512_Final pg_SHA512_Final
-
-/*** SHA-224/256/384/512 Various Length Definitions ***********************/
-#define SHA224_BLOCK_LENGTH		64
-#define SHA224_DIGEST_LENGTH		28
-#define SHA224_DIGEST_STRING_LENGTH (SHA224_DIGEST_LENGTH * 2 + 1)
-#define SHA256_BLOCK_LENGTH		64
-#define SHA256_DIGEST_LENGTH		32
-#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
-#define SHA384_BLOCK_LENGTH		128
-#define SHA384_DIGEST_LENGTH		48
-#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
-#define SHA512_BLOCK_LENGTH		128
-#define SHA512_DIGEST_LENGTH		64
-#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
-
-
-/*** SHA-256/384/512 Context Structures *******************************/
-typedef struct _SHA256_CTX
-{
-	uint32		state[8];
-	uint64		bitcount;
-	uint8		buffer[SHA256_BLOCK_LENGTH];
-} SHA256_CTX;
-typedef struct _SHA512_CTX
-{
-	uint64		state[8];
-	uint64		bitcount[2];
-	uint8		buffer[SHA512_BLOCK_LENGTH];
-} SHA512_CTX;
-
-typedef SHA256_CTX SHA224_CTX;
-typedef SHA512_CTX SHA384_CTX;
-
-void		SHA224_Init(SHA224_CTX *);
-void		SHA224_Update(SHA224_CTX *, const uint8 *, size_t);
-void		SHA224_Final(uint8[SHA224_DIGEST_LENGTH], SHA224_CTX *);
-
-void		SHA256_Init(SHA256_CTX *);
-void		SHA256_Update(SHA256_CTX *, const uint8 *, size_t);
-void		SHA256_Final(uint8[SHA256_DIGEST_LENGTH], SHA256_CTX *);
-
-void		SHA384_Init(SHA384_CTX *);
-void		SHA384_Update(SHA384_CTX *, const uint8 *, size_t);
-void		SHA384_Final(uint8[SHA384_DIGEST_LENGTH], SHA384_CTX *);
-
-void		SHA512_Init(SHA512_CTX *);
-void		SHA512_Update(SHA512_CTX *, const uint8 *, size_t);
-void		SHA512_Final(uint8[SHA512_DIGEST_LENGTH], SHA512_CTX *);
-
-#endif   /* _SHA2_H */
diff --git a/src/common/Makefile b/src/common/Makefile
index 03dfaa1..5ddfff8 100644
--- a/src/common/Makefile
+++ b/src/common/Makefile
@@ -44,6 +44,12 @@ OBJS_COMMON = config_info.o controldata_utils.o exec.o ip.o keywords.o \
 	md5.o pg_lzcompress.o pgfnames.o psprintf.o relpath.o rmtree.o \
 	string.o username.o wait_error.o
 
+ifeq ($(with_openssl),yes)
+OBJS_COMMON += sha2_openssl.o
+else
+OBJS_COMMON += sha2.o
+endif
+
 OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o file_utils.o restricted_token.o
 
 OBJS_SRV = $(OBJS_COMMON:%.o=%_srv.o)
diff --git a/contrib/pgcrypto/sha2.c b/src/common/sha2.c
similarity index 65%
rename from contrib/pgcrypto/sha2.c
rename to src/common/sha2.c
index 231f9df..ea33fbc 100644
--- a/contrib/pgcrypto/sha2.c
+++ b/src/common/sha2.c
@@ -1,4 +1,18 @@
-/*	$OpenBSD: sha2.c,v 1.6 2004/05/03 02:57:36 millert Exp $	*/
+/*-------------------------------------------------------------------------
+ *
+ * sha2.c
+ *	  Set of SHA functions for SHA-224, SHA-256, SHA-384 and SHA-512.
+ *
+ * This is the set of in-core functions used when there are no other
+ * alternative options like OpenSSL.
+ *
+ * Portions Copyright (c) 2016, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *	  src/common/sha2.c
+ *
+ *-------------------------------------------------------------------------
+ */
 
 /*
  * FILE:	sha2.c
@@ -33,15 +47,19 @@
  *
  * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
  *
- * contrib/pgcrypto/sha2.c
+ * src/common/sha2.c
  */
 
+
+#ifndef FRONTEND
 #include "postgres.h"
+#else
+#include "postgres_fe.h"
+#endif
 
 #include <sys/param.h>
 
-#include "px.h"
-#include "sha2.h"
+#include "common/sha2.h"
 
 /*
  * UNROLLED TRANSFORM LOOP NOTE:
@@ -58,11 +76,9 @@
  */
 
 /*** SHA-256/384/512 Various Length Definitions ***********************/
-/* NOTE: Most of these are in sha2.h */
-#define SHA256_SHORT_BLOCK_LENGTH	(SHA256_BLOCK_LENGTH - 8)
-#define SHA384_SHORT_BLOCK_LENGTH	(SHA384_BLOCK_LENGTH - 16)
-#define SHA512_SHORT_BLOCK_LENGTH	(SHA512_BLOCK_LENGTH - 16)
-
+#define PG_SHA256_SHORT_BLOCK_LENGTH	(PG_SHA256_BLOCK_LENGTH - 8)
+#define PG_SHA384_SHORT_BLOCK_LENGTH	(PG_SHA384_BLOCK_LENGTH - 16)
+#define PG_SHA512_SHORT_BLOCK_LENGTH	(PG_SHA512_BLOCK_LENGTH - 16)
 
 /*** ENDIAN REVERSAL MACROS *******************************************/
 #ifndef WORDS_BIGENDIAN
@@ -130,10 +146,9 @@
  * library -- they are intended for private internal visibility/use
  * only.
  */
-static void SHA512_Last(SHA512_CTX *);
-static void SHA256_Transform(SHA256_CTX *, const uint8 *);
-static void SHA512_Transform(SHA512_CTX *, const uint8 *);
-
+static void pg_sha512_last(pg_sha512_ctx *ctx);
+static void pg_sha256_transform(pg_sha256_ctx *ctx, const uint8 *data);
+static void pg_sha512_transform(pg_sha512_ctx *ctx, const uint8 *data);
 
 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
 /* Hash constant words K for SHA-256: */
@@ -249,15 +264,54 @@ static const uint64 sha512_initial_hash_value[8] = {
 };
 
 
-/*** SHA-256: *********************************************************/
-void
-SHA256_Init(SHA256_CTX *context)
+static void
+pg_sha512_last(pg_sha512_ctx *ctx)
 {
-	if (context == NULL)
-		return;
-	memcpy(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
-	memset(context->buffer, 0, SHA256_BLOCK_LENGTH);
-	context->bitcount = 0;
+	unsigned int usedspace;
+
+	usedspace = (ctx->bitcount[0] >> 3) % PG_SHA512_BLOCK_LENGTH;
+#ifndef WORDS_BIGENDIAN
+	/* Convert FROM host byte order */
+	REVERSE64(ctx->bitcount[0], ctx->bitcount[0]);
+	REVERSE64(ctx->bitcount[1], ctx->bitcount[1]);
+#endif
+	if (usedspace > 0)
+	{
+		/* Begin padding with a 1 bit: */
+		ctx->buffer[usedspace++] = 0x80;
+
+		if (usedspace <= PG_SHA512_SHORT_BLOCK_LENGTH)
+		{
+			/* Set-up for the last transform: */
+			memset(&ctx->buffer[usedspace], 0, PG_SHA512_SHORT_BLOCK_LENGTH - usedspace);
+		}
+		else
+		{
+			if (usedspace < PG_SHA512_BLOCK_LENGTH)
+			{
+				memset(&ctx->buffer[usedspace], 0, PG_SHA512_BLOCK_LENGTH - usedspace);
+			}
+			/* Do second-to-last transform: */
+			pg_sha512_transform(ctx, ctx->buffer);
+
+			/* And set-up for the last transform: */
+			memset(ctx->buffer, 0, PG_SHA512_BLOCK_LENGTH - 2);
+		}
+	}
+	else
+	{
+		/* Prepare for final transform: */
+		memset(ctx->buffer, 0, PG_SHA512_SHORT_BLOCK_LENGTH);
+
+		/* Begin padding with a 1 bit: */
+		*ctx->buffer = 0x80;
+	}
+	/* Store the length of input data (in bits): */
+	*(uint64 *) &ctx->buffer[PG_SHA512_SHORT_BLOCK_LENGTH] = ctx->bitcount[1];
+	*(uint64 *) &ctx->buffer[PG_SHA512_SHORT_BLOCK_LENGTH + 8] = ctx->bitcount[0];
+
+	/* Final transform: */
+	pg_sha512_transform(ctx, ctx->buffer);
 }
 
 #ifdef SHA2_UNROLL_TRANSFORM
@@ -286,8 +340,13 @@ SHA256_Init(SHA256_CTX *context)
 	j++;									\
 } while(0)
 
+/*
+ * Perform a round of transformation on a SHA-256 by using the given input
+ * data.  This basically shuffles data around and uses the input data to
+ * add some extra randomness in the SHA-256 generation.
+ */
 static void
-SHA256_Transform(SHA256_CTX *context, const uint8 *data)
+pg_sha256_transform(pg_sha256_ctx *ctx, const uint8 *data)
 {
 	uint32		a,
 				b,
@@ -303,17 +362,17 @@ SHA256_Transform(SHA256_CTX *context, const uint8 *data)
 			   *W256;
 	int			j;
 
-	W256 = (uint32 *) context->buffer;
+	W256 = (uint32 *) ctx->buffer;
 
 	/* Initialize registers with the prev. intermediate value */
-	a = context->state[0];
-	b = context->state[1];
-	c = context->state[2];
-	d = context->state[3];
-	e = context->state[4];
-	f = context->state[5];
-	g = context->state[6];
-	h = context->state[7];
+	a = ctx->state[0];
+	b = ctx->state[1];
+	c = ctx->state[2];
+	d = ctx->state[3];
+	e = ctx->state[4];
+	f = ctx->state[5];
+	g = ctx->state[6];
+	h = ctx->state[7];
 
 	j = 0;
 	do
@@ -343,22 +402,27 @@ SHA256_Transform(SHA256_CTX *context, const uint8 *data)
 	} while (j < 64);
 
 	/* Compute the current intermediate hash value */
-	context->state[0] += a;
-	context->state[1] += b;
-	context->state[2] += c;
-	context->state[3] += d;
-	context->state[4] += e;
-	context->state[5] += f;
-	context->state[6] += g;
-	context->state[7] += h;
+	ctx->state[0] += a;
+	ctx->state[1] += b;
+	ctx->state[2] += c;
+	ctx->state[3] += d;
+	ctx->state[4] += e;
+	ctx->state[5] += f;
+	ctx->state[6] += g;
+	ctx->state[7] += h;
 
 	/* Clean up */
 	a = b = c = d = e = f = g = h = T1 = 0;
 }
 #else							/* SHA2_UNROLL_TRANSFORM */
 
+/*
+ * Perform a round of transformation on a SHA-256 by using the given input
+ * data.  This basically shuffles data around and uses the input data to
+ * add some extra randomness in the SHA-256 generation.
+ */
 static void
-SHA256_Transform(SHA256_CTX *context, const uint8 *data)
+pg_sha256_transform(pg_sha256_ctx *ctx, const uint8 *data)
 {
 	uint32		a,
 				b,
@@ -375,17 +439,17 @@ SHA256_Transform(SHA256_CTX *context, const uint8 *data)
 			   *W256;
 	int			j;
 
-	W256 = (uint32 *) context->buffer;
+	W256 = (uint32 *) ctx->buffer;
 
 	/* Initialize registers with the prev. intermediate value */
-	a = context->state[0];
-	b = context->state[1];
-	c = context->state[2];
-	d = context->state[3];
-	e = context->state[4];
-	f = context->state[5];
-	g = context->state[6];
-	h = context->state[7];
+	a = ctx->state[0];
+	b = ctx->state[1];
+	c = ctx->state[2];
+	d = ctx->state[3];
+	e = ctx->state[4];
+	f = ctx->state[5];
+	g = ctx->state[6];
+	h = ctx->state[7];
 
 	j = 0;
 	do
@@ -433,159 +497,20 @@ SHA256_Transform(SHA256_CTX *context, const uint8 *data)
 	} while (j < 64);
 
 	/* Compute the current intermediate hash value */
-	context->state[0] += a;
-	context->state[1] += b;
-	context->state[2] += c;
-	context->state[3] += d;
-	context->state[4] += e;
-	context->state[5] += f;
-	context->state[6] += g;
-	context->state[7] += h;
+	ctx->state[0] += a;
+	ctx->state[1] += b;
+	ctx->state[2] += c;
+	ctx->state[3] += d;
+	ctx->state[4] += e;
+	ctx->state[5] += f;
+	ctx->state[6] += g;
+	ctx->state[7] += h;
 
 	/* Clean up */
 	a = b = c = d = e = f = g = h = T1 = T2 = 0;
 }
 #endif   /* SHA2_UNROLL_TRANSFORM */
 
-void
-SHA256_Update(SHA256_CTX *context, const uint8 *data, size_t len)
-{
-	size_t		freespace,
-				usedspace;
-
-	/* Calling with no data is valid (we do nothing) */
-	if (len == 0)
-		return;
-
-	usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
-	if (usedspace > 0)
-	{
-		/* Calculate how much free space is available in the buffer */
-		freespace = SHA256_BLOCK_LENGTH - usedspace;
-
-		if (len >= freespace)
-		{
-			/* Fill the buffer completely and process it */
-			memcpy(&context->buffer[usedspace], data, freespace);
-			context->bitcount += freespace << 3;
-			len -= freespace;
-			data += freespace;
-			SHA256_Transform(context, context->buffer);
-		}
-		else
-		{
-			/* The buffer is not yet full */
-			memcpy(&context->buffer[usedspace], data, len);
-			context->bitcount += len << 3;
-			/* Clean up: */
-			usedspace = freespace = 0;
-			return;
-		}
-	}
-	while (len >= SHA256_BLOCK_LENGTH)
-	{
-		/* Process as many complete blocks as we can */
-		SHA256_Transform(context, data);
-		context->bitcount += SHA256_BLOCK_LENGTH << 3;
-		len -= SHA256_BLOCK_LENGTH;
-		data += SHA256_BLOCK_LENGTH;
-	}
-	if (len > 0)
-	{
-		/* There's left-overs, so save 'em */
-		memcpy(context->buffer, data, len);
-		context->bitcount += len << 3;
-	}
-	/* Clean up: */
-	usedspace = freespace = 0;
-}
-
-static void
-SHA256_Last(SHA256_CTX *context)
-{
-	unsigned int usedspace;
-
-	usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
-#ifndef WORDS_BIGENDIAN
-	/* Convert FROM host byte order */
-	REVERSE64(context->bitcount, context->bitcount);
-#endif
-	if (usedspace > 0)
-	{
-		/* Begin padding with a 1 bit: */
-		context->buffer[usedspace++] = 0x80;
-
-		if (usedspace <= SHA256_SHORT_BLOCK_LENGTH)
-		{
-			/* Set-up for the last transform: */
-			memset(&context->buffer[usedspace], 0, SHA256_SHORT_BLOCK_LENGTH - usedspace);
-		}
-		else
-		{
-			if (usedspace < SHA256_BLOCK_LENGTH)
-			{
-				memset(&context->buffer[usedspace], 0, SHA256_BLOCK_LENGTH - usedspace);
-			}
-			/* Do second-to-last transform: */
-			SHA256_Transform(context, context->buffer);
-
-			/* And set-up for the last transform: */
-			memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
-		}
-	}
-	else
-	{
-		/* Set-up for the last transform: */
-		memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
-
-		/* Begin padding with a 1 bit: */
-		*context->buffer = 0x80;
-	}
-	/* Set the bit count: */
-	*(uint64 *) &context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
-
-	/* Final transform: */
-	SHA256_Transform(context, context->buffer);
-}
-
-void
-SHA256_Final(uint8 digest[], SHA256_CTX *context)
-{
-	/* If no digest buffer is passed, we don't bother doing this: */
-	if (digest != NULL)
-	{
-		SHA256_Last(context);
-
-#ifndef WORDS_BIGENDIAN
-		{
-			/* Convert TO host byte order */
-			int			j;
-
-			for (j = 0; j < 8; j++)
-			{
-				REVERSE32(context->state[j], context->state[j]);
-			}
-		}
-#endif
-		memcpy(digest, context->state, SHA256_DIGEST_LENGTH);
-	}
-
-	/* Clean up state data: */
-	px_memset(context, 0, sizeof(*context));
-}
-
-
-/*** SHA-512: *********************************************************/
-void
-SHA512_Init(SHA512_CTX *context)
-{
-	if (context == NULL)
-		return;
-	memcpy(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
-	memset(context->buffer, 0, SHA512_BLOCK_LENGTH);
-	context->bitcount[0] = context->bitcount[1] = 0;
-}
-
 #ifdef SHA2_UNROLL_TRANSFORM
 
 /* Unrolled SHA-512 round macros: */
@@ -615,8 +540,13 @@ SHA512_Init(SHA512_CTX *context)
 	j++;									\
 } while(0)
 
+/*
+ * Perform a round of transformation on a SHA-512 by using the given input
+ * data.  This basically shuffles data around and uses the input data to
+ * add some extra randomness in the SHA-512 generation.
+ */
 static void
-SHA512_Transform(SHA512_CTX *context, const uint8 *data)
+pg_sha512_transform(pg_sha512_ctx *ctx, const uint8 *data)
 {
 	uint64		a,
 				b,
@@ -629,18 +559,18 @@ SHA512_Transform(SHA512_CTX *context, const uint8 *data)
 				s0,
 				s1;
 	uint64		T1,
-			   *W512 = (uint64 *) context->buffer;
+			   *W512 = (uint64 *) ctx->buffer;
 	int			j;
 
 	/* Initialize registers with the prev. intermediate value */
-	a = context->state[0];
-	b = context->state[1];
-	c = context->state[2];
-	d = context->state[3];
-	e = context->state[4];
-	f = context->state[5];
-	g = context->state[6];
-	h = context->state[7];
+	a = ctx->state[0];
+	b = ctx->state[1];
+	c = ctx->state[2];
+	d = ctx->state[3];
+	e = ctx->state[4];
+	f = ctx->state[5];
+	g = ctx->state[6];
+	h = ctx->state[7];
 
 	j = 0;
 	do
@@ -669,22 +599,27 @@ SHA512_Transform(SHA512_CTX *context, const uint8 *data)
 	} while (j < 80);
 
 	/* Compute the current intermediate hash value */
-	context->state[0] += a;
-	context->state[1] += b;
-	context->state[2] += c;
-	context->state[3] += d;
-	context->state[4] += e;
-	context->state[5] += f;
-	context->state[6] += g;
-	context->state[7] += h;
+	ctx->state[0] += a;
+	ctx->state[1] += b;
+	ctx->state[2] += c;
+	ctx->state[3] += d;
+	ctx->state[4] += e;
+	ctx->state[5] += f;
+	ctx->state[6] += g;
+	ctx->state[7] += h;
 
 	/* Clean up */
 	a = b = c = d = e = f = g = h = T1 = 0;
 }
 #else							/* SHA2_UNROLL_TRANSFORM */
 
+/*
+ * Perform a round of transformation on a SHA-512 by using the given input
+ * data.  This basically shuffles data around and uses the input data to
+ * add some extra randomness in the SHA-512 generation.
+ */
 static void
-SHA512_Transform(SHA512_CTX *context, const uint8 *data)
+pg_sha512_transform(pg_sha512_ctx *ctx, const uint8 *data)
 {
 	uint64		a,
 				b,
@@ -698,18 +633,18 @@ SHA512_Transform(SHA512_CTX *context, const uint8 *data)
 				s1;
 	uint64		T1,
 				T2,
-			   *W512 = (uint64 *) context->buffer;
+			   *W512 = (uint64 *) ctx->buffer;
 	int			j;
 
 	/* Initialize registers with the prev. intermediate value */
-	a = context->state[0];
-	b = context->state[1];
-	c = context->state[2];
-	d = context->state[3];
-	e = context->state[4];
-	f = context->state[5];
-	g = context->state[6];
-	h = context->state[7];
+	a = ctx->state[0];
+	b = ctx->state[1];
+	c = ctx->state[2];
+	d = ctx->state[3];
+	e = ctx->state[4];
+	f = ctx->state[5];
+	g = ctx->state[6];
+	h = ctx->state[7];
 
 	j = 0;
 	do
@@ -759,22 +694,89 @@ SHA512_Transform(SHA512_CTX *context, const uint8 *data)
 	} while (j < 80);
 
 	/* Compute the current intermediate hash value */
-	context->state[0] += a;
-	context->state[1] += b;
-	context->state[2] += c;
-	context->state[3] += d;
-	context->state[4] += e;
-	context->state[5] += f;
-	context->state[6] += g;
-	context->state[7] += h;
+	ctx->state[0] += a;
+	ctx->state[1] += b;
+	ctx->state[2] += c;
+	ctx->state[3] += d;
+	ctx->state[4] += e;
+	ctx->state[5] += f;
+	ctx->state[6] += g;
+	ctx->state[7] += h;
 
 	/* Clean up */
 	a = b = c = d = e = f = g = h = T1 = T2 = 0;
 }
 #endif   /* SHA2_UNROLL_TRANSFORM */
 
+static void
+pg_sha256_last(pg_sha256_ctx *ctx)
+{
+	unsigned int usedspace;
+
+	usedspace = (ctx->bitcount >> 3) % PG_SHA256_BLOCK_LENGTH;
+#ifndef WORDS_BIGENDIAN
+	/* Convert FROM host byte order */
+	REVERSE64(ctx->bitcount, ctx->bitcount);
+#endif
+	if (usedspace > 0)
+	{
+		/* Begin padding with a 1 bit: */
+		ctx->buffer[usedspace++] = 0x80;
+
+		if (usedspace <= PG_SHA256_SHORT_BLOCK_LENGTH)
+		{
+			/* Set-up for the last transform: */
+			memset(&ctx->buffer[usedspace], 0, PG_SHA256_SHORT_BLOCK_LENGTH - usedspace);
+		}
+		else
+		{
+			if (usedspace < PG_SHA256_BLOCK_LENGTH)
+			{
+				memset(&ctx->buffer[usedspace], 0, PG_SHA256_BLOCK_LENGTH - usedspace);
+			}
+			/* Do second-to-last transform: */
+			pg_sha256_transform(ctx, ctx->buffer);
+
+			/* And set-up for the last transform: */
+			memset(ctx->buffer, 0, PG_SHA256_SHORT_BLOCK_LENGTH);
+		}
+	}
+	else
+	{
+		/* Set-up for the last transform: */
+		memset(ctx->buffer, 0, PG_SHA256_SHORT_BLOCK_LENGTH);
+
+		/* Begin padding with a 1 bit: */
+		*ctx->buffer = 0x80;
+	}
+	/* Set the bit count: */
+	*(uint64 *) &ctx->buffer[PG_SHA256_SHORT_BLOCK_LENGTH] = ctx->bitcount;
+
+	/* Final transform: */
+	pg_sha256_transform(ctx, ctx->buffer);
+}
+
+/*
+ * pg_sha256_init
+ * Initialize calculation of SHA-256.
+ */
+void
+pg_sha256_init(pg_sha256_ctx *ctx)
+{
+	if (ctx == NULL)
+		return;
+	memcpy(ctx->state, sha256_initial_hash_value, PG_SHA256_DIGEST_LENGTH);
+	memset(ctx->buffer, 0, PG_SHA256_BLOCK_LENGTH);
+	ctx->bitcount = 0;
+}
+
+
+/*
+ * pg_sha256_update
+ * Update SHA-256 using given input data.
+ */
 void
-SHA512_Update(SHA512_CTX *context, const uint8 *data, size_t len)
+pg_sha256_update(pg_sha256_ctx *ctx, const uint8 *data, size_t len)
 {
 	size_t		freespace,
 				usedspace;
@@ -783,106 +785,165 @@ SHA512_Update(SHA512_CTX *context, const uint8 *data, size_t len)
 	if (len == 0)
 		return;
 
-	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
+	usedspace = (ctx->bitcount >> 3) % PG_SHA256_BLOCK_LENGTH;
 	if (usedspace > 0)
 	{
 		/* Calculate how much free space is available in the buffer */
-		freespace = SHA512_BLOCK_LENGTH - usedspace;
+		freespace = PG_SHA256_BLOCK_LENGTH - usedspace;
 
 		if (len >= freespace)
 		{
 			/* Fill the buffer completely and process it */
-			memcpy(&context->buffer[usedspace], data, freespace);
-			ADDINC128(context->bitcount, freespace << 3);
+			memcpy(&ctx->buffer[usedspace], data, freespace);
+			ctx->bitcount += freespace << 3;
 			len -= freespace;
 			data += freespace;
-			SHA512_Transform(context, context->buffer);
+			pg_sha256_transform(ctx, ctx->buffer);
 		}
 		else
 		{
 			/* The buffer is not yet full */
-			memcpy(&context->buffer[usedspace], data, len);
-			ADDINC128(context->bitcount, len << 3);
+			memcpy(&ctx->buffer[usedspace], data, len);
+			ctx->bitcount += len << 3;
 			/* Clean up: */
 			usedspace = freespace = 0;
 			return;
 		}
 	}
-	while (len >= SHA512_BLOCK_LENGTH)
+	while (len >= PG_SHA256_BLOCK_LENGTH)
 	{
 		/* Process as many complete blocks as we can */
-		SHA512_Transform(context, data);
-		ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
-		len -= SHA512_BLOCK_LENGTH;
-		data += SHA512_BLOCK_LENGTH;
+		pg_sha256_transform(ctx, data);
+		ctx->bitcount += PG_SHA256_BLOCK_LENGTH << 3;
+		len -= PG_SHA256_BLOCK_LENGTH;
+		data += PG_SHA256_BLOCK_LENGTH;
 	}
 	if (len > 0)
 	{
 		/* There's left-overs, so save 'em */
-		memcpy(context->buffer, data, len);
-		ADDINC128(context->bitcount, len << 3);
+		memcpy(ctx->buffer, data, len);
+		ctx->bitcount += len << 3;
 	}
 	/* Clean up: */
 	usedspace = freespace = 0;
 }
 
-static void
-SHA512_Last(SHA512_CTX *context)
+
+/*
+ * pg_sha256_final
+ * Finalize calculation of SHA-256 and save result to be reused by caller.
+ */
+void
+pg_sha256_final(pg_sha256_ctx *ctx, uint8 *dest)
 {
-	unsigned int usedspace;
+	/* If no destination buffer is passed, we don't bother doing this: */
+	if (dest != NULL)
+	{
+		pg_sha256_last(ctx);
 
-	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
 #ifndef WORDS_BIGENDIAN
-	/* Convert FROM host byte order */
-	REVERSE64(context->bitcount[0], context->bitcount[0]);
-	REVERSE64(context->bitcount[1], context->bitcount[1]);
+		{
+			/* Convert TO host byte order */
+			int			j;
+
+			for (j = 0; j < 8; j++)
+			{
+				REVERSE32(ctx->state[j], ctx->state[j]);
+			}
+		}
 #endif
+		memcpy(dest, ctx->state, PG_SHA256_DIGEST_LENGTH);
+	}
+
+	/* Clean up state data: */
+	memset(ctx, 0, sizeof(pg_sha256_ctx));
+}
+
+
+/*
+ * pg_sha512_init
+ * Initialize calculation of SHA-512.
+ */
+void
+pg_sha512_init(pg_sha512_ctx *ctx)
+{
+	if (ctx == NULL)
+		return;
+	memcpy(ctx->state, sha512_initial_hash_value, PG_SHA512_DIGEST_LENGTH);
+	memset(ctx->buffer, 0, PG_SHA512_BLOCK_LENGTH);
+	ctx->bitcount[0] = ctx->bitcount[1] = 0;
+}
+
+
+/*
+ * pg_sha512_update
+ * Update SHA-512 using given input data.
+ */
+void
+pg_sha512_update(pg_sha512_ctx *ctx, const uint8 *data, size_t len)
+{
+	size_t		freespace,
+				usedspace;
+
+	/* Calling with no data is valid (we do nothing) */
+	if (len == 0)
+		return;
+
+	usedspace = (ctx->bitcount[0] >> 3) % PG_SHA512_BLOCK_LENGTH;
 	if (usedspace > 0)
 	{
-		/* Begin padding with a 1 bit: */
-		context->buffer[usedspace++] = 0x80;
+		/* Calculate how much free space is available in the buffer */
+		freespace = PG_SHA512_BLOCK_LENGTH - usedspace;
 
-		if (usedspace <= SHA512_SHORT_BLOCK_LENGTH)
+		if (len >= freespace)
 		{
-			/* Set-up for the last transform: */
-			memset(&context->buffer[usedspace], 0, SHA512_SHORT_BLOCK_LENGTH - usedspace);
+			/* Fill the buffer completely and process it */
+			memcpy(&ctx->buffer[usedspace], data, freespace);
+			ADDINC128(ctx->bitcount, freespace << 3);
+			len -= freespace;
+			data += freespace;
+			pg_sha512_transform(ctx, ctx->buffer);
 		}
 		else
 		{
-			if (usedspace < SHA512_BLOCK_LENGTH)
-			{
-				memset(&context->buffer[usedspace], 0, SHA512_BLOCK_LENGTH - usedspace);
-			}
-			/* Do second-to-last transform: */
-			SHA512_Transform(context, context->buffer);
-
-			/* And set-up for the last transform: */
-			memset(context->buffer, 0, SHA512_BLOCK_LENGTH - 2);
+			/* The buffer is not yet full */
+			memcpy(&ctx->buffer[usedspace], data, len);
+			ADDINC128(ctx->bitcount, len << 3);
+			/* Clean up: */
+			usedspace = freespace = 0;
+			return;
 		}
 	}
-	else
+	while (len >= PG_SHA512_BLOCK_LENGTH)
 	{
-		/* Prepare for final transform: */
-		memset(context->buffer, 0, SHA512_SHORT_BLOCK_LENGTH);
-
-		/* Begin padding with a 1 bit: */
-		*context->buffer = 0x80;
+		/* Process as many complete blocks as we can */
+		pg_sha512_transform(ctx, data);
+		ADDINC128(ctx->bitcount, PG_SHA512_BLOCK_LENGTH << 3);
+		len -= PG_SHA512_BLOCK_LENGTH;
+		data += PG_SHA512_BLOCK_LENGTH;
 	}
-	/* Store the length of input data (in bits): */
-	*(uint64 *) &context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
-	*(uint64 *) &context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8] = context->bitcount[0];
-
-	/* Final transform: */
-	SHA512_Transform(context, context->buffer);
+	if (len > 0)
+	{
+		/* There's left-overs, so save 'em */
+		memcpy(ctx->buffer, data, len);
+		ADDINC128(ctx->bitcount, len << 3);
+	}
+	/* Clean up: */
+	usedspace = freespace = 0;
 }
 
+
+/*
+ * pg_sha512_final
+ * Finalize calculation of SHA-512 and save result to be reused by caller.
+ */
 void
-SHA512_Final(uint8 digest[], SHA512_CTX *context)
+pg_sha512_final(pg_sha512_ctx *ctx, uint8 *dest)
 {
-	/* If no digest buffer is passed, we don't bother doing this: */
-	if (digest != NULL)
+	/* If no destination buffer is passed, we don't bother doing this: */
+	if (dest != NULL)
 	{
-		SHA512_Last(context);
+		pg_sha512_last(ctx);
 
 		/* Save the hash data for output: */
 #ifndef WORDS_BIGENDIAN
@@ -892,42 +953,55 @@ SHA512_Final(uint8 digest[], SHA512_CTX *context)
 
 			for (j = 0; j < 8; j++)
 			{
-				REVERSE64(context->state[j], context->state[j]);
+				REVERSE64(ctx->state[j], ctx->state[j]);
 			}
 		}
 #endif
-		memcpy(digest, context->state, SHA512_DIGEST_LENGTH);
+		memcpy(dest, ctx->state, PG_SHA512_DIGEST_LENGTH);
 	}
 
 	/* Zero out state data */
-	px_memset(context, 0, sizeof(*context));
+	memset(ctx, 0, sizeof(pg_sha512_ctx));
 }
 
 
-/*** SHA-384: *********************************************************/
+/*
+ * pg_sha384_init
+ * Initialize calculation of SHA-384.
+ */
 void
-SHA384_Init(SHA384_CTX *context)
+pg_sha384_init(pg_sha384_ctx *ctx)
 {
-	if (context == NULL)
+	if (ctx == NULL)
 		return;
-	memcpy(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
-	memset(context->buffer, 0, SHA384_BLOCK_LENGTH);
-	context->bitcount[0] = context->bitcount[1] = 0;
+	memcpy(ctx->state, sha384_initial_hash_value, PG_SHA512_DIGEST_LENGTH);
+	memset(ctx->buffer, 0, PG_SHA384_BLOCK_LENGTH);
+	ctx->bitcount[0] = ctx->bitcount[1] = 0;
 }
 
+
+/*
+ * pg_sha384_update
+ * Update SHA-384 using given input data.
+ */
 void
-SHA384_Update(SHA384_CTX *context, const uint8 *data, size_t len)
+pg_sha384_update(pg_sha384_ctx *ctx, const uint8 *data, size_t len)
 {
-	SHA512_Update((SHA512_CTX *) context, data, len);
+	pg_sha512_update((pg_sha512_ctx *) ctx, data, len);
 }
 
+
+/*
+ * pg_sha384_final
+ * Finalize calculation of SHA-384 and save result to be reused by caller.
+ */
 void
-SHA384_Final(uint8 digest[], SHA384_CTX *context)
+pg_sha384_final(pg_sha384_ctx *ctx, uint8 *dest)
 {
-	/* If no digest buffer is passed, we don't bother doing this: */
-	if (digest != NULL)
+	/* If no destination buffer is passed, we don't bother doing this: */
+	if (dest != NULL)
 	{
-		SHA512_Last((SHA512_CTX *) context);
+		pg_sha512_last((pg_sha512_ctx *) ctx);
 
 		/* Save the hash data for output: */
 #ifndef WORDS_BIGENDIAN
@@ -937,41 +1011,55 @@ SHA384_Final(uint8 digest[], SHA384_CTX *context)
 
 			for (j = 0; j < 6; j++)
 			{
-				REVERSE64(context->state[j], context->state[j]);
+				REVERSE64(ctx->state[j], ctx->state[j]);
 			}
 		}
 #endif
-		memcpy(digest, context->state, SHA384_DIGEST_LENGTH);
+		memcpy(dest, ctx->state, PG_SHA384_DIGEST_LENGTH);
 	}
 
 	/* Zero out state data */
-	px_memset(context, 0, sizeof(*context));
+	memset(ctx, 0, sizeof(pg_sha384_ctx));
 }
 
-/*** SHA-224: *********************************************************/
+
+/*
+ * pg_sha224_init
+ * Initialize calculation of SHA-224.
+ */
 void
-SHA224_Init(SHA224_CTX *context)
+pg_sha224_init(pg_sha224_ctx *ctx)
 {
-	if (context == NULL)
+	if (ctx == NULL)
 		return;
-	memcpy(context->state, sha224_initial_hash_value, SHA256_DIGEST_LENGTH);
-	memset(context->buffer, 0, SHA256_BLOCK_LENGTH);
-	context->bitcount = 0;
+	memcpy(ctx->state, sha224_initial_hash_value, PG_SHA256_DIGEST_LENGTH);
+	memset(ctx->buffer, 0, PG_SHA256_BLOCK_LENGTH);
+	ctx->bitcount = 0;
 }
 
+
+/*
+ * pg_sha224_update
+ * Update SHA-224 using given input data.
+ */
 void
-SHA224_Update(SHA224_CTX *context, const uint8 *data, size_t len)
+pg_sha224_update(pg_sha224_ctx *ctx, const uint8 *data, size_t len)
 {
-	SHA256_Update((SHA256_CTX *) context, data, len);
+	pg_sha256_update((pg_sha256_ctx *) ctx, data, len);
 }
 
+
+/*
+ * pg_sha224_final
+ * Finalize calculation of SHA-224 and save result to be reused by caller.
+ */
 void
-SHA224_Final(uint8 digest[], SHA224_CTX *context)
+pg_sha224_final(pg_sha224_ctx *ctx, uint8 *dest)
 {
-	/* If no digest buffer is passed, we don't bother doing this: */
-	if (digest != NULL)
+	/* If no destination buffer is passed, we don't bother doing this: */
+	if (dest != NULL)
 	{
-		SHA256_Last(context);
+		pg_sha256_last(ctx);
 
 #ifndef WORDS_BIGENDIAN
 		{
@@ -980,13 +1068,13 @@ SHA224_Final(uint8 digest[], SHA224_CTX *context)
 
 			for (j = 0; j < 8; j++)
 			{
-				REVERSE32(context->state[j], context->state[j]);
+				REVERSE32(ctx->state[j], ctx->state[j]);
 			}
 		}
 #endif
-		memcpy(digest, context->state, SHA224_DIGEST_LENGTH);
+		memcpy(dest, ctx->state, PG_SHA224_DIGEST_LENGTH);
 	}
 
 	/* Clean up state data: */
-	px_memset(context, 0, sizeof(*context));
+	memset(ctx, 0, sizeof(pg_sha224_ctx));
 }
diff --git a/src/common/sha2_openssl.c b/src/common/sha2_openssl.c
new file mode 100644
index 0000000..91d0c39
--- /dev/null
+++ b/src/common/sha2_openssl.c
@@ -0,0 +1,102 @@
+/*-------------------------------------------------------------------------
+ *
+ * sha2_openssl.c
+ *	  Set of wrapper routines on top of OpenSSL to support SHA-224
+ *	  SHA-256, SHA-384 and SHA-512 functions.
+ *
+ * This should only be used if code is compiled with OpenSSL support.
+ *
+ * Portions Copyright (c) 2016, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *        src/common/sha2_openssl.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef FRONTEND
+#include "postgres.h"
+#else
+#include "postgres_fe.h"
+#endif
+
+#include <openssl/sha.h>
+
+#include "common/sha2.h"
+
+
+/* Interface routines for SHA-256 */
+void
+pg_sha256_init(pg_sha256_ctx *ctx)
+{
+	SHA256_Init((SHA256_CTX *) ctx);
+}
+
+void
+pg_sha256_update(pg_sha256_ctx *ctx, const uint8 *data, size_t len)
+{
+	SHA256_Update((SHA256_CTX *) ctx, data, len);
+}
+
+void
+pg_sha256_final(pg_sha256_ctx *ctx, uint8 *dest)
+{
+	SHA256_Final(dest, (SHA256_CTX *) ctx);
+}
+
+/* Interface routines for SHA-512 */
+void
+pg_sha512_init(pg_sha512_ctx *ctx)
+{
+	SHA512_Init((SHA512_CTX *) ctx);
+}
+
+void
+pg_sha512_update(pg_sha512_ctx *ctx, const uint8 *data, size_t len)
+{
+	SHA512_Update((SHA512_CTX *) ctx, data, len);
+}
+
+void
+pg_sha512_final(pg_sha512_ctx *ctx, uint8 *dest)
+{
+	SHA512_Final(dest, (SHA512_CTX *) ctx);
+}
+
+/* Interface routines for SHA-384 */
+void
+pg_sha384_init(pg_sha384_ctx *ctx)
+{
+	SHA384_Init((SHA512_CTX *) ctx);
+}
+
+void
+pg_sha384_update(pg_sha384_ctx *ctx, const uint8 *data, size_t len)
+{
+	SHA384_Update((SHA512_CTX *) ctx, data, len);
+}
+
+void
+pg_sha384_final(pg_sha384_ctx *ctx, uint8 *dest)
+{
+	SHA384_Final(dest, (SHA512_CTX *) ctx);
+}
+
+/* Interface routines for SHA-224 */
+void
+pg_sha224_init(pg_sha224_ctx *ctx)
+{
+	SHA224_Init((SHA256_CTX *) ctx);
+}
+
+void
+pg_sha224_update(pg_sha224_ctx *ctx, const uint8 *data, size_t len)
+{
+	SHA224_Update((SHA256_CTX *) ctx, data, len);
+}
+
+void
+pg_sha224_final(pg_sha224_ctx *ctx, uint8 *dest)
+{
+	SHA224_Final(dest, (SHA256_CTX *) ctx);
+}
diff --git a/src/include/common/sha2.h b/src/include/common/sha2.h
new file mode 100644
index 0000000..015a905
--- /dev/null
+++ b/src/include/common/sha2.h
@@ -0,0 +1,115 @@
+/*-------------------------------------------------------------------------
+ *
+ * sha2.h
+ *	  Generic headers for SHA224, 256, 384 AND 512 functions of PostgreSQL.
+ *
+ * Portions Copyright (c) 2016, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *        src/include/common/sha2.h
+ *
+ *-------------------------------------------------------------------------
+ */
+
+/*  $OpenBSD: sha2.h,v 1.2 2004/04/28 23:11:57 millert Exp $    */
+
+/*
+ * FILE:	sha2.h
+ * AUTHOR:	Aaron D. Gifford <me@aarongifford.com>
+ *
+ * Copyright (c) 2000-2001, Aaron D. Gifford
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *	  notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *	  notice, this list of conditions and the following disclaimer in the
+ *	  documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of contributors
+ *	  may be used to endorse or promote products derived from this software
+ *	  without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
+ */
+
+#ifndef _PG_SHA2_H_
+#define _PG_SHA2_H_
+
+#ifdef USE_SSL
+#include <openssl/sha.h>
+#endif
+
+/*** SHA224/256/384/512 Various Length Definitions ***********************/
+#define PG_SHA224_BLOCK_LENGTH			64
+#define PG_SHA224_DIGEST_LENGTH			28
+#define PG_SHA224_DIGEST_STRING_LENGTH	(PG_SHA224_DIGEST_LENGTH * 2 + 1)
+#define PG_SHA256_BLOCK_LENGTH			64
+#define PG_SHA256_DIGEST_LENGTH			32
+#define PG_SHA256_DIGEST_STRING_LENGTH	(PG_SHA256_DIGEST_LENGTH * 2 + 1)
+#define PG_SHA384_BLOCK_LENGTH			128
+#define PG_SHA384_DIGEST_LENGTH			48
+#define PG_SHA384_DIGEST_STRING_LENGTH	(PG_SHA384_DIGEST_LENGTH * 2 + 1)
+#define PG_SHA512_BLOCK_LENGTH			128
+#define PG_SHA512_DIGEST_LENGTH			64
+#define PG_SHA512_DIGEST_STRING_LENGTH	(PG_SHA512_DIGEST_LENGTH * 2 + 1)
+
+/* Context Structures for SHA-1/224/256/384/512 */
+#ifdef USE_SSL
+typedef SHA256_CTX pg_sha256_ctx;
+typedef SHA512_CTX pg_sha512_ctx;
+typedef SHA256_CTX pg_sha224_ctx;
+typedef SHA512_CTX pg_sha384_ctx;
+#else
+typedef struct pg_sha256_ctx
+{
+	uint32		state[8];
+	uint64		bitcount;
+	uint8		buffer[PG_SHA256_BLOCK_LENGTH];
+} pg_sha256_ctx;
+typedef struct pg_sha512_ctx
+{
+	uint64		state[8];
+	uint64		bitcount[2];
+	uint8		buffer[PG_SHA512_BLOCK_LENGTH];
+} pg_sha512_ctx;
+typedef struct pg_sha256_ctx pg_sha224_ctx;
+typedef struct pg_sha512_ctx pg_sha384_ctx;
+#endif	/* USE_SSL */
+
+/* Interface routines for SHA224/256/384/512 */
+extern void pg_sha224_init(pg_sha224_ctx *ctx);
+extern void	pg_sha224_update(pg_sha224_ctx *ctx,
+						const uint8 *input0, size_t len);
+extern void pg_sha224_final(pg_sha224_ctx *ctx, uint8 *dest);
+
+extern void pg_sha256_init(pg_sha256_ctx *ctx);
+extern void pg_sha256_update(pg_sha256_ctx *ctx,
+						const uint8 *input0, size_t len);
+extern void pg_sha256_final(pg_sha256_ctx *ctx, uint8 *dest);
+
+extern void pg_sha384_init(pg_sha384_ctx *ctx);
+extern void pg_sha384_update(pg_sha384_ctx *ctx,
+						const uint8 *, size_t len);
+extern void pg_sha384_final(pg_sha384_ctx *ctx, uint8 *dest);
+
+extern void pg_sha512_init(pg_sha512_ctx *ctx);
+extern void pg_sha512_update(pg_sha512_ctx *ctx,
+						const uint8 *input0, size_t len);
+extern void pg_sha512_final(pg_sha512_ctx *ctx, uint8 *dest);
+
+#endif   /* _PG_SHA2_H_ */
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index de764dd..21c4600 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -114,6 +114,15 @@ sub mkvcbuild
 	  md5.c pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
 	  string.c username.c wait_error.c);
 
+	if ($solution->{options}->{openssl})
+	{
+		push(@pgcommonallfiles, 'sha2_openssl.c');
+	}
+	else
+	{
+		push(@pgcommonallfiles, 'sha2.c');
+	}
+
 	our @pgcommonfrontendfiles = (
 		@pgcommonallfiles, qw(fe_memutils.c file_utils.c
 		  restricted_token.c));
@@ -421,14 +430,15 @@ sub mkvcbuild
 	else
 	{
 		$pgcrypto->AddFiles(
-			'contrib/pgcrypto',   'md5.c',
-			'sha1.c',             'sha2.c',
-			'internal.c',         'internal-sha2.c',
-			'blf.c',              'rijndael.c',
-			'fortuna.c',          'random.c',
-			'pgp-mpi-internal.c', 'imath.c');
+			'contrib/pgcrypto', 'md5.c',
+			'sha1.c',           'internal.c',
+			'internal-sha2.c',  'blf.c',
+			'rijndael.c',       'fortuna.c',
+			'random.c',         'pgp-mpi-internal.c',
+            'imath.c');
 	}
 	$pgcrypto->AddReference($postgres);
+	$pgcrypto->AddReference($libpgcommon);
 	$pgcrypto->AddLibrary('ws2_32.lib');
 	my $mf = Project::read_file('contrib/pgcrypto/Makefile');
 	GenerateContribSqlFiles('pgcrypto', $mf);
-- 
2.10.2

