Commit 273c458a authored by Heikki Linnakangas's avatar Heikki Linnakangas

Refactor SHA2 functions and move them to src/common/.

This way both frontend and backends can use them. The functions are taken
from pgcrypto, which now fetches the source files it needs from
src/common/.

A new interface is designed for the SHA2 functions, which allow linking
to either OpenSSL or the in-core stuff taken from KAME as needed.

Michael Paquier, reviewed by Robert Haas.

Discussion: https://www.postgresql.org/message-id/CAB7nPqTGKuTM5jiZriHrNaQeVqp5e_iT3X4BFLWY_HyHxLvySQ%40mail.gmail.com
parent 330b84d8
# Source file copied from src/common
/sha2.c
/sha2_openssl.c
# Generated subdirectories
/log/
/results/
......
......@@ -4,7 +4,7 @@ INT_SRCS = md5.c sha1.c sha2.c internal.c internal-sha2.c blf.c rijndael.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,13 @@ SHLIB_LINK += $(filter -leay32, $(LIBS))
SHLIB_LINK += -lws2_32
endif
# Compiling pgcrypto with those two raw files is necessary as long
# as none of their routines are used by the backend code. Note doing
# so can either result in library loading failures or linking resolution
# failures at compilation depending on the environment used.
sha2.c sha2_openssl.c: % : $(top_srcdir)/src/common/%
rm -f $@ && $(LN_S) $< .
rijndael.o: rijndael.tbl
rijndael.tbl:
......
......@@ -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));
......
......@@ -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)
......
/*-------------------------------------------------------------------------
*
* 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);
}
/* contrib/pgcrypto/sha2.h */
/* $OpenBSD: sha2.h,v 1.2 2004/04/28 23:11:57 millert Exp $ */
/*-------------------------------------------------------------------------
*
* 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
......@@ -35,66 +47,69 @@
* $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
*/
#ifndef _SHA2_H
#define _SHA2_H
#ifndef _PG_SHA2_H_
#define _PG_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
#ifdef USE_SSL
#include <openssl/sha.h>
#endif
/*** 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)
/*** 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)
/*** SHA-256/384/512 Context Structures *******************************/
typedef struct _SHA256_CTX
/* 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[SHA256_BLOCK_LENGTH];
} SHA256_CTX;
typedef struct _SHA512_CTX
uint8 buffer[PG_SHA256_BLOCK_LENGTH];
} pg_sha256_ctx;
typedef struct pg_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;
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 */
void SHA224_Init(SHA224_CTX *);
void SHA224_Update(SHA224_CTX *, const uint8 *, size_t);
void SHA224_Final(uint8[SHA224_DIGEST_LENGTH], SHA224_CTX *);
/* 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);
void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX *, const uint8 *, size_t);
void SHA256_Final(uint8[SHA256_DIGEST_LENGTH], SHA256_CTX *);
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);
void SHA384_Init(SHA384_CTX *);
void SHA384_Update(SHA384_CTX *, const uint8 *, size_t);
void SHA384_Final(uint8[SHA384_DIGEST_LENGTH], SHA384_CTX *);
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);
void SHA512_Init(SHA512_CTX *);
void SHA512_Update(SHA512_CTX *, const uint8 *, size_t);
void SHA512_Final(uint8[SHA512_DIGEST_LENGTH], SHA512_CTX *);
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 /* _SHA2_H */
#endif /* _PG_SHA2_H_ */
......@@ -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,13 +430,14 @@ sub mkvcbuild
else
{
$pgcrypto->AddFiles(
'contrib/pgcrypto', 'md5.c',
'sha1.c', 'sha2.c',
'internal.c', 'internal-sha2.c',
'blf.c', 'rijndael.c',
'pgp-mpi-internal.c', 'imath.c');
'contrib/pgcrypto', 'md5.c',
'sha1.c', 'internal.c',
'internal-sha2.c', 'blf.c',
'rijndael.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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment