Skip to content

Commit a8ed6bb

Browse files
committed
Introduce SHA1 implementations in the cryptohash infrastructure
With this commit, SHA1 goes through the implementation provided by OpenSSL via EVP when building the backend with it, and uses as fallback implementation KAME which was located in pgcrypto and already shaped for an integration with a set of init, update and final routines. Structures and routines have been renamed to make things consistent with the fallback implementations of MD5 and SHA2. uuid-ossp has used for ages a shortcut with pgcrypto to fetch a copy of SHA1 if needed. This was built depending on the build options within ./configure, so this cleans up some code and removes the build dependency between pgcrypto and uuid-ossp. Note that this will help with the refactoring of HMAC, as pgcrypto offers the option to use MD5, SHA1 or SHA2, so only the second option was missing to make that possible. Author: Michael Paquier Reviewed-by: Heikki Linnakangas Discussion: https://postgr.es/m/[email protected]
1 parent 3fc81ce commit a8ed6bb

18 files changed

+479
-424
lines changed

configure

+7-12
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,6 @@ XML2_LIBS
705705
XML2_CFLAGS
706706
XML2_CONFIG
707707
with_libxml
708-
UUID_EXTRA_OBJS
709708
with_uuid
710709
with_readline
711710
with_systemd
@@ -8303,30 +8302,26 @@ if test "$with_ossp_uuid" = yes ; then
83038302
with_uuid=ossp
83048303
fi
83058304

8306-
if test "$with_uuid" = bsd ; then
8305+
if test "$with_uuid" != no ; then
8306+
if test "$with_uuid" = bsd ; then
83078307

83088308
$as_echo "#define HAVE_UUID_BSD 1" >>confdefs.h
83098309

8310-
UUID_EXTRA_OBJS="sha1.o"
8311-
elif test "$with_uuid" = e2fs ; then
8310+
elif test "$with_uuid" = e2fs ; then
83128311

83138312
$as_echo "#define HAVE_UUID_E2FS 1" >>confdefs.h
83148313

8315-
UUID_EXTRA_OBJS="sha1.o"
8316-
elif test "$with_uuid" = ossp ; then
8314+
elif test "$with_uuid" = ossp ; then
83178315

83188316
$as_echo "#define HAVE_UUID_OSSP 1" >>confdefs.h
83198317

8320-
UUID_EXTRA_OBJS=""
8321-
elif test "$with_uuid" = no ; then
8322-
UUID_EXTRA_OBJS=""
8323-
else
8324-
as_fn_error $? "--with-uuid must specify one of bsd, e2fs, or ossp" "$LINENO" 5
8318+
else
8319+
as_fn_error $? "--with-uuid must specify one of bsd, e2fs, or ossp" "$LINENO" 5
8320+
fi
83258321
fi
83268322

83278323

83288324

8329-
83308325
#
83318326
# XML
83328327
#

configure.ac

+10-14
Original file line numberDiff line numberDiff line change
@@ -919,22 +919,18 @@ if test "$with_ossp_uuid" = yes ; then
919919
with_uuid=ossp
920920
fi
921921

922-
if test "$with_uuid" = bsd ; then
923-
AC_DEFINE([HAVE_UUID_BSD], 1, [Define to 1 if you have BSD UUID support.])
924-
UUID_EXTRA_OBJS="sha1.o"
925-
elif test "$with_uuid" = e2fs ; then
926-
AC_DEFINE([HAVE_UUID_E2FS], 1, [Define to 1 if you have E2FS UUID support.])
927-
UUID_EXTRA_OBJS="sha1.o"
928-
elif test "$with_uuid" = ossp ; then
929-
AC_DEFINE([HAVE_UUID_OSSP], 1, [Define to 1 if you have OSSP UUID support.])
930-
UUID_EXTRA_OBJS=""
931-
elif test "$with_uuid" = no ; then
932-
UUID_EXTRA_OBJS=""
933-
else
934-
AC_MSG_ERROR([--with-uuid must specify one of bsd, e2fs, or ossp])
922+
if test "$with_uuid" != no ; then
923+
if test "$with_uuid" = bsd ; then
924+
AC_DEFINE([HAVE_UUID_BSD], 1, [Define to 1 if you have BSD UUID support.])
925+
elif test "$with_uuid" = e2fs ; then
926+
AC_DEFINE([HAVE_UUID_E2FS], 1, [Define to 1 if you have E2FS UUID support.])
927+
elif test "$with_uuid" = ossp ; then
928+
AC_DEFINE([HAVE_UUID_OSSP], 1, [Define to 1 if you have OSSP UUID support.])
929+
else
930+
AC_MSG_ERROR([--with-uuid must specify one of bsd, e2fs, or ossp])
931+
fi
935932
fi
936933
AC_SUBST(with_uuid)
937-
AC_SUBST(UUID_EXTRA_OBJS)
938934

939935

940936
#

contrib/pgcrypto/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# contrib/pgcrypto/Makefile
22

3-
INT_SRCS = sha1.c internal.c internal-sha2.c blf.c rijndael.c \
3+
INT_SRCS = internal.c internal-sha2.c blf.c rijndael.c \
44
pgp-mpi-internal.c imath.c
55
INT_TESTS = sha2
66

contrib/pgcrypto/internal.c

+14-20
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,10 @@
3636
#include "blf.h"
3737
#include "px.h"
3838
#include "rijndael.h"
39-
#include "sha1.h"
4039

4140
#include "common/cryptohash.h"
4241
#include "common/md5.h"
43-
44-
#ifndef SHA1_DIGEST_LENGTH
45-
#ifdef SHA1_RESULTLEN
46-
#define SHA1_DIGEST_LENGTH SHA1_RESULTLEN
47-
#else
48-
#define SHA1_DIGEST_LENGTH 20
49-
#endif
50-
#endif
42+
#include "common/sha1.h"
5143

5244
#define SHA1_BLOCK_SIZE 64
5345
#define MD5_BLOCK_SIZE 64
@@ -144,34 +136,36 @@ int_sha1_block_len(PX_MD *h)
144136
static void
145137
int_sha1_update(PX_MD *h, const uint8 *data, unsigned dlen)
146138
{
147-
SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
139+
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
148140

149-
SHA1Update(ctx, data, dlen);
141+
if (pg_cryptohash_update(ctx, data, dlen) < 0)
142+
elog(ERROR, "could not update %s context", "SHA1");
150143
}
151144

152145
static void
153146
int_sha1_reset(PX_MD *h)
154147
{
155-
SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
148+
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
156149

157-
SHA1Init(ctx);
150+
if (pg_cryptohash_init(ctx) < 0)
151+
elog(ERROR, "could not initialize %s context", "SHA1");
158152
}
159153

160154
static void
161155
int_sha1_finish(PX_MD *h, uint8 *dst)
162156
{
163-
SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
157+
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
164158

165-
SHA1Final(dst, ctx);
159+
if (pg_cryptohash_final(ctx, dst) < 0)
160+
elog(ERROR, "could not finalize %s context", "SHA1");
166161
}
167162

168163
static void
169164
int_sha1_free(PX_MD *h)
170165
{
171-
SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
166+
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
172167

173-
px_memset(ctx, 0, sizeof(*ctx));
174-
pfree(ctx);
168+
pg_cryptohash_free(ctx);
175169
pfree(h);
176170
}
177171

@@ -199,9 +193,9 @@ init_md5(PX_MD *md)
199193
static void
200194
init_sha1(PX_MD *md)
201195
{
202-
SHA1_CTX *ctx;
196+
pg_cryptohash_ctx *ctx;
203197

204-
ctx = palloc0(sizeof(*ctx));
198+
ctx = pg_cryptohash_create(PG_SHA1);
205199

206200
md->p.ptr = ctx;
207201

0 commit comments

Comments
 (0)