summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillem Jover <guillem@hadrons.org>2021-02-07 00:09:30 +0100
committerGuillem Jover <guillem@hadrons.org>2021-02-07 01:28:27 +0100
commit847e682f8de452d5f0038458f06ae68ae3959b3a (patch)
tree5c7f97fc9c4b131801f088cd947bc93e26599bc8
parent68f980c90d914e5fd6f7512e00dec334dbb71bed (diff)
Use libmd hashing function implementations instead of embedding our own
This splits the implementation responsibilities, and reduces embedded code copies, which was one of the driving points with this project to start with, so it's nice to give a good example.
-rw-r--r--COPYING15
-rw-r--r--README3
-rw-r--r--configure.ac11
-rw-r--r--include/bsd/md5.h75
-rw-r--r--man/libbsd.75
-rw-r--r--src/Makefile.am15
-rw-r--r--src/getentropy_aix.c8
-rw-r--r--src/getentropy_hpux.c8
-rw-r--r--src/getentropy_hurd.c10
-rw-r--r--src/getentropy_linux.c10
-rw-r--r--src/getentropy_osx.c6
-rw-r--r--src/getentropy_solaris.c8
-rw-r--r--src/hash/.gitignore1
-rw-r--r--src/hash/helper.c105
-rw-r--r--src/hash/md5.c248
-rw-r--r--src/hash/sha512.h53
-rw-r--r--src/hash/sha512c.c318
-rw-r--r--src/libbsd.map2
-rw-r--r--src/md5.c91
-rw-r--r--test/Makefile.am2
20 files changed, 163 insertions, 831 deletions
diff --git a/COPYING b/COPYING
index 34ec432..b51db0e 100644
--- a/COPYING
+++ b/COPYING
@@ -278,8 +278,6 @@ Files:
man/getpeereid.3bsd
man/pidfile.3bsd
src/expand_number.c
- src/hash/sha512.h
- src/hash/sha512c.c
src/pidfile.c
src/reallocf.c
src/timeconv.c
@@ -459,17 +457,6 @@ License: Expat
USE OR OTHER DEALINGS IN THE SOFTWARE.
Files:
- include/bsd/md5.h
- src/hash/md5.c
-Copyright:
- None
-License: public-domain-Colin-Plumb
- This code implements the MD5 message-digest algorithm.
- The algorithm is due to Ron Rivest. This code was
- written by Colin Plumb in 1993, no copyright is claimed.
- This code is in the public domain; do with it what you wish.
-
-Files:
src/explicit_bzero.c
src/chacha_private.h
Copyright:
@@ -479,8 +466,6 @@ License: public-domain
Files:
man/mdX.3bsd
- src/hash/md5hl.c
- src/hash/helper.c
Copyright:
None
License: Beerware
diff --git a/README b/README
index d516db0..9b2bbdb 100644
--- a/README
+++ b/README
@@ -5,6 +5,9 @@ and lacking on others like GNU systems, thus making it easier to port
projects with strong BSD origins, without needing to embed the same
code over and over again on each project.
+A BSD compatible message-digest library is required, on systems where
+this is not provided by its libc or libmd libraries, the canonical
+implementation to use is <https://www.hadrons.org/software/libmd/>.
Releases
--------
diff --git a/configure.ac b/configure.ac
index cf064bb..df05b0e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -63,6 +63,17 @@ AC_SUBST([TESTU01_LIBS])
AM_CONDITIONAL([HAVE_LIBTESTU01],
[test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
+saved_LIBS="$LIBS"
+AC_SEARCH_LIBS([MD5Update], [md], [
+ AC_SEARCH_LIBS([SHA512Update], [md], [
+ MD_LIBS="-lmd"
+ ])
+], [
+ AC_MSG_ERROR([cannot find required message digest functions in libc or libmd])
+])
+AC_SUBST([MD_LIBS])
+LIBS="$saved_LIBS"
+
is_windows=no
AS_CASE([$host_os],
[*-gnu*], [
diff --git a/include/bsd/md5.h b/include/bsd/md5.h
index bf36a30..5bd58d8 100644
--- a/include/bsd/md5.h
+++ b/include/bsd/md5.h
@@ -1,58 +1,31 @@
-/* $OpenBSD: md5.h,v 1.16 2004/06/22 01:57:30 jfb Exp $ */
-
/*
- * This code implements the MD5 message-digest algorithm.
- * The algorithm is due to Ron Rivest. This code was
- * written by Colin Plumb in 1993, no copyright is claimed.
- * This code is in the public domain; do with it what you wish.
+ * Copyright © 2021 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
*
- * Equivalent code is available from RSA Data Security, Inc.
- * This code has been tested against that, and is equivalent,
- * except that you don't need to include two pages of legalese
- * with every copy.
+ * THIS SOFTWARE IS PROVIDED ``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 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.
*/
-#ifndef LIBBSD_MD5_H
-#define LIBBSD_MD5_H
-
-#include <stdint.h>
-
-#define MD5_BLOCK_LENGTH 64
-#define MD5_DIGEST_LENGTH 16
-#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)
-
-typedef struct MD5Context {
- uint32_t state[4]; /* state */
- uint64_t count; /* number of bits, mod 2^64 */
- uint8_t buffer[MD5_BLOCK_LENGTH]; /* input buffer */
-} MD5_CTX;
-
#ifdef LIBBSD_OVERLAY
-#include <sys/cdefs.h>
+#include_next <md5.h>
#else
-#include <bsd/sys/cdefs.h>
+#include <md5.h>
#endif
-#include <sys/types.h>
-
-__BEGIN_DECLS
-void MD5Init(MD5_CTX *);
-void MD5Update(MD5_CTX *, const uint8_t *, size_t)
- __attribute__((__bounded__(__string__,2,3)));
-void MD5Pad(MD5_CTX *);
-void MD5Final(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *)
- __attribute__((__bounded__(__minbytes__,1,MD5_DIGEST_LENGTH)));
-void MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH])
- __attribute__((__bounded__(__minbytes__,1,4)))
- __attribute__((__bounded__(__minbytes__,2,MD5_BLOCK_LENGTH)));
-char *MD5End(MD5_CTX *, char *)
- __attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
-char *MD5File(const char *, char *)
- __attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
-char *MD5FileChunk(const char *, char *, off_t, off_t)
- __attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
-char *MD5Data(const uint8_t *, size_t, char *)
- __attribute__((__bounded__(__string__,1,2)))
- __attribute__((__bounded__(__minbytes__,3,MD5_DIGEST_STRING_LENGTH)));
-__END_DECLS
-
-#endif /* LIBBSD_MD5_H */
diff --git a/man/libbsd.7 b/man/libbsd.7
index 407b3e4..bfaa3ce 100644
--- a/man/libbsd.7
+++ b/man/libbsd.7
@@ -180,9 +180,10 @@ for example.
.It Fn MD5File 3
.It Fn MD5FileChunk 3
.It Fn MD5Data 3
-The set of MD5 digest functions are now provided by the
+The set of MD5 digest functions are now proxies for the implementations
+provided by the
.Nm libmd
-companion library, so it is advised to use that instead.
+companion library, so it is advised to switch to use that directly instead.
.It Fn explicit_bzero 3
This function is provided by
.Nm glibc
diff --git a/src/Makefile.am b/src/Makefile.am
index c13d385..48fb1fa 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,7 +15,6 @@ AM_CPPFLAGS += \
endif
libbsd_la_included_sources = \
- hash/helper.c \
getentropy_aix.c \
getentropy_bsd.c \
getentropy_hpux.c \
@@ -34,10 +33,6 @@ EXTRA_DIST = \
$(libbsd_la_included_sources) \
$(nil)
-CLEANFILES = \
- hash/md5hl.c \
- $(nil)
-
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = \
libbsd.pc \
@@ -53,14 +48,11 @@ pkgconfig_DATA += libbsd-ctor.pc
lib_LIBRARIES += libbsd-ctor.a
endif
-hash/md5hl.c: $(srcdir)/hash/helper.c
- $(AM_V_at) $(MKDIR_P) hash
- $(AM_V_GEN) sed -e 's:hashinc:md5.h:g' -e 's:HASH:MD5:g' $< > $@
-
libbsd_la_DEPENDENCIES = \
$(libbsd_la_included_sources) \
libbsd.map
libbsd_la_LIBADD = \
+ $(MD_LIBS) \
$(CLOCK_GETTIME_LIBS) \
$(ARC4RANDOM_ATFORK_LIBS) \
$(nil)
@@ -95,15 +87,12 @@ libbsd_la_SOURCES = \
funopen.c \
getbsize.c \
getpeereid.c \
- hash/md5.c \
- hash/md5hl.c \
- hash/sha512.h \
- hash/sha512c.c \
heapsort.c \
humanize_number.c \
inet_net_pton.c \
local-elf.h \
local-link.h \
+ md5.c \
merge.c \
nlist.c \
pidfile.c \
diff --git a/src/getentropy_aix.c b/src/getentropy_aix.c
index d759fe0..a963071 100644
--- a/src/getentropy_aix.c
+++ b/src/getentropy_aix.c
@@ -39,7 +39,7 @@
#include <string.h>
#include <termios.h>
-#include "hash/sha512.h"
+#include <sha512.h>
#include <libperfstat.h>
@@ -54,9 +54,9 @@
HD(b); \
} while (0)
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
int getentropy(void *buf, size_t len);
diff --git a/src/getentropy_hpux.c b/src/getentropy_hpux.c
index 5be096a..b21ff4f 100644
--- a/src/getentropy_hpux.c
+++ b/src/getentropy_hpux.c
@@ -42,7 +42,7 @@
#include <unistd.h>
#include <time.h>
-#include "hash/sha512.h"
+#include <sha512.h>
#include <sys/vfs.h>
@@ -59,9 +59,9 @@
HD(b); \
} while (0)
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
int getentropy(void *buf, size_t len);
diff --git a/src/getentropy_hurd.c b/src/getentropy_hurd.c
index 738dc3b..2f76baa 100644
--- a/src/getentropy_hurd.c
+++ b/src/getentropy_hurd.c
@@ -44,7 +44,7 @@
#include <unistd.h>
#include <time.h>
-#include "hash/sha512.h"
+#include <sha512.h>
#ifdef HAVE_GETAUXVAL
#include <sys/auxv.h>
@@ -62,9 +62,9 @@
HD(b); \
} while (0)
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
int getentropy(void *buf, size_t len);
@@ -228,7 +228,7 @@ getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
{
SHA512_CTX *ctx = data;
- SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
+ SHA512_Update(ctx, (uint8_t *)&info->dlpi_addr, sizeof (info->dlpi_addr));
return (0);
}
diff --git a/src/getentropy_linux.c b/src/getentropy_linux.c
index 74d965e..25c986e 100644
--- a/src/getentropy_linux.c
+++ b/src/getentropy_linux.c
@@ -48,7 +48,7 @@
#include <unistd.h>
#include <time.h>
-#include "hash/sha512.h"
+#include <sha512.h>
#include <linux/types.h>
#include <linux/random.h>
@@ -68,9 +68,9 @@
HD(b); \
} while (0)
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
int getentropy(void *buf, size_t len);
@@ -333,7 +333,7 @@ getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
{
SHA512_CTX *ctx = data;
- SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
+ SHA512_Update(ctx, (uint8_t *)&info->dlpi_addr, sizeof (info->dlpi_addr));
return (0);
}
diff --git a/src/getentropy_osx.c b/src/getentropy_osx.c
index bcdbce5..d67f33d 100644
--- a/src/getentropy_osx.c
+++ b/src/getentropy_osx.c
@@ -76,9 +76,9 @@
HD(b); \
} while (0)
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
int getentropy(void *buf, size_t len);
diff --git a/src/getentropy_solaris.c b/src/getentropy_solaris.c
index f0fcdcf..05d3e23 100644
--- a/src/getentropy_solaris.c
+++ b/src/getentropy_solaris.c
@@ -62,9 +62,9 @@
HD(b); \
} while (0)
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
int getentropy(void *buf, size_t len);
@@ -249,7 +249,7 @@ getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
{
SHA512_CTX *ctx = data;
- SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
+ SHA512_Update(ctx, (uint8_t *)&info->dlpi_addr, sizeof (info->dlpi_addr));
return (0);
}
diff --git a/src/hash/.gitignore b/src/hash/.gitignore
deleted file mode 100644
index bfdcb6a..0000000
--- a/src/hash/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-md5hl.c
diff --git a/src/hash/helper.c b/src/hash/helper.c
deleted file mode 100644
index 352f978..0000000
--- a/src/hash/helper.c
+++ /dev/null
@@ -1,105 +0,0 @@
-/** $MirOS: src/lib/libc/hash/helper.c,v 1.5 2007/05/07 15:21:18 tg Exp $ */
-/* $OpenBSD: helper.c,v 1.8 2005/08/08 08:05:35 espie Exp $ */
-
-/*
- * ----------------------------------------------------------------------------
- * "THE BEER-WARE LICENSE" (Revision 42):
- * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
- * can do whatever you want with this stuff. If we meet some day, and you think
- * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
- * ----------------------------------------------------------------------------
- */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#ifdef _WIN32
-#include <io.h>
-#endif
-
-#include <hashinc>
-
-/* ARGSUSED */
-char *
-HASHEnd(HASH_CTX *ctx, char *buf)
-{
- int i;
- uint8_t digest[HASH_DIGEST_LENGTH];
-#ifdef HASH_DIGEST_UPPERCASE
- static const char hex[] = "0123456789ABCDEF";
-#else
- static const char hex[] = "0123456789abcdef";
-#endif
-
- if (buf == NULL && (buf = malloc(HASH_DIGEST_STRING_LENGTH)) == NULL)
- return (NULL);
-
- HASHFinal(digest, ctx);
- for (i = 0; i < HASH_DIGEST_LENGTH; i++) {
- buf[i + i] = hex[digest[i] >> 4];
- buf[i + i + 1] = hex[digest[i] & 0x0f];
- }
- buf[i + i] = '\0';
- memset(digest, 0, sizeof(digest));
- return (buf);
-}
-
-char *
-HASHFileChunk(const char *filename, char *buf, off_t off, off_t len)
-{
- struct stat sb;
- unsigned char buffer[BUFSIZ];
- HASH_CTX ctx;
- int fd, save_errno;
- ssize_t nr;
-
- HASHInit(&ctx);
-
- if ((fd = open(filename, O_RDONLY)) < 0)
- return (NULL);
- if (len == 0) {
- if (fstat(fd, &sb) == -1) {
- close(fd);
- return (NULL);
- }
- len = sb.st_size;
- }
- if ((len < 0) || (off > 0 && lseek(fd, off, SEEK_SET) < 0)) {
- close(fd);
- return (NULL);
- }
-
- while ((nr = read(fd, buffer,
- (size_t)(len ? MIN(BUFSIZ, len) : BUFSIZ))) > 0) {
- HASHUpdate(&ctx, buffer, (size_t)nr);
- if (len > 0 && (len -= nr) == 0)
- break;
- }
-
- save_errno = errno;
- close(fd);
- errno = save_errno;
- return (nr < 0 ? NULL : HASHEnd(&ctx, buf));
-}
-
-char *
-HASHFile(const char *filename, char *buf)
-{
- return (HASHFileChunk(filename, buf, (off_t)0, (off_t)0));
-}
-
-char *
-HASHData(const unsigned char *data, size_t len, char *buf)
-{
- HASH_CTX ctx;
-
- HASHInit(&ctx);
- HASHUpdate(&ctx, data, len);
- return (HASHEnd(&ctx, buf));
-}
diff --git a/src/hash/md5.c b/src/hash/md5.c
deleted file mode 100644
index 7e9672e..0000000
--- a/src/hash/md5.c
+++ /dev/null
@@ -1,248 +0,0 @@
-/* $OpenBSD: md5.c,v 1.8 2005/08/08 08:05:35 espie Exp $ */
-
-/*
- * This code implements the MD5 message-digest algorithm.
- * The algorithm is due to Ron Rivest. This code was
- * written by Colin Plumb in 1993, no copyright is claimed.
- * This code is in the public domain; do with it what you wish.
- *
- * Equivalent code is available from RSA Data Security, Inc.
- * This code has been tested against that, and is equivalent,
- * except that you don't need to include two pages of legalese
- * with every copy.
- *
- * To compute the message digest of a chunk of bytes, declare an
- * MD5Context structure, pass it to MD5Init, call MD5Update as
- * needed on buffers full of bytes, and then call MD5Final, which
- * will fill a supplied 16-byte array with the digest.
- */
-
-#include <sys/types.h>
-#include <string.h>
-#include <md5.h>
-
-#define PUT_64BIT_LE(cp, value) do { \
- (cp)[7] = (value) >> 56; \
- (cp)[6] = (value) >> 48; \
- (cp)[5] = (value) >> 40; \
- (cp)[4] = (value) >> 32; \
- (cp)[3] = (value) >> 24; \
- (cp)[2] = (value) >> 16; \
- (cp)[1] = (value) >> 8; \
- (cp)[0] = (value); } while (0)
-
-#define PUT_32BIT_LE(cp, value) do { \
- (cp)[3] = (value) >> 24; \
- (cp)[2] = (value) >> 16; \
- (cp)[1] = (value) >> 8; \
- (cp)[0] = (value); } while (0)
-
-static uint8_t PADDING[MD5_BLOCK_LENGTH] = {
- 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-
-/*
- * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
- * initialization constants.
- */
-void
-MD5Init(MD5_CTX *ctx)
-{
- ctx->count = 0;
- ctx->state[0] = 0x67452301;
- ctx->state[1] = 0xefcdab89;
- ctx->state[2] = 0x98badcfe;
- ctx->state[3] = 0x10325476;
-}
-
-/*
- * Update context to reflect the concatenation of another buffer full
- * of bytes.
- */
-void
-MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
-{
- size_t have, need;
-
- /* Check how many bytes we already have and how many more we need. */
- have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
- need = MD5_BLOCK_LENGTH - have;
-
- /* Update bitcount */
- ctx->count += (uint64_t)len << 3;
-
- if (len >= need) {
- if (have != 0) {
- memcpy(ctx->buffer + have, input, need);
- MD5Transform(ctx->state, ctx->buffer);
- input += need;
- len -= need;
- have = 0;
- }
-
- /* Process data in MD5_BLOCK_LENGTH-byte chunks. */
- while (len >= MD5_BLOCK_LENGTH) {
- MD5Transform(ctx->state, input);
- input += MD5_BLOCK_LENGTH;
- len -= MD5_BLOCK_LENGTH;
- }
- }
-
- /* Handle any remaining bytes of data. */
- if (len != 0)
- memcpy(ctx->buffer + have, input, len);
-}
-
-/*
- * Pad pad to 64-byte boundary with the bit pattern
- * 1 0* (64-bit count of bits processed, MSB-first)
- */
-void
-MD5Pad(MD5_CTX *ctx)
-{
- uint8_t count[8];
- size_t padlen;
-
- /* Convert count to 8 bytes in little endian order. */
- PUT_64BIT_LE(count, ctx->count);
-
- /* Pad out to 56 mod 64. */
- padlen = MD5_BLOCK_LENGTH -
- ((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
- if (padlen < 1 + 8)
- padlen += MD5_BLOCK_LENGTH;
- MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
- MD5Update(ctx, count, 8);
-}
-
-/*
- * Final wrapup--call MD5Pad, fill in digest and zero out ctx.
- */
-void
-MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
-{
- int i;
-
- MD5Pad(ctx);
- if (digest != NULL) {
- for (i = 0; i < 4; i++)
- PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
- memset(ctx, 0, sizeof(*ctx));
- }
-}
-
-
-/* The four core functions - F1 is optimized somewhat */
-
-/* #define F1(x, y, z) (x & y | ~x & z) */
-#define F1(x, y, z) (z ^ (x & (y ^ z)))
-#define F2(x, y, z) F1(z, x, y)
-#define F3(x, y, z) (x ^ y ^ z)
-#define F4(x, y, z) (y ^ (x | ~z))
-
-/* This is the central step in the MD5 algorithm. */
-#define MD5STEP(f, w, x, y, z, data, s) \
- ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
-
-/*
- * The core of the MD5 algorithm, this alters an existing MD5 hash to
- * reflect the addition of 16 longwords of new data. MD5Update blocks
- * the data and converts bytes into longwords for this routine.
- */
-void
-MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
-{
- uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
-
-#if BYTE_ORDER == LITTLE_ENDIAN
- memcpy(in, block, sizeof(in));
-#else
- for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
- in[a] = (uint32_t)(
- (uint32_t)(block[a * 4 + 0]) |
- (uint32_t)(block[a * 4 + 1]) << 8 |
- (uint32_t)(block[a * 4 + 2]) << 16 |
- (uint32_t)(block[a * 4 + 3]) << 24);
- }
-#endif
-
- a = state[0];
- b = state[1];
- c = state[2];
- d = state[3];
-
- MD5STEP(F1, a, b, c, d, in[ 0] + 0xd76aa478, 7);
- MD5STEP(F1, d, a, b, c, in[ 1] + 0xe8c7b756, 12);
- MD5STEP(F1, c, d, a, b, in[ 2] + 0x242070db, 17);
- MD5STEP(F1, b, c, d, a, in[ 3] + 0xc1bdceee, 22);
- MD5STEP(F1, a, b, c, d, in[ 4] + 0xf57c0faf, 7);
- MD5STEP(F1, d, a, b, c, in[ 5] + 0x4787c62a, 12);
- MD5STEP(F1, c, d, a, b, in[ 6] + 0xa8304613, 17);
- MD5STEP(F1, b, c, d, a, in[ 7] + 0xfd469501, 22);
- MD5STEP(F1, a, b, c, d, in[ 8] + 0x698098d8, 7);
- MD5STEP(F1, d, a, b, c, in[ 9] + 0x8b44f7af, 12);
- MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
- MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
- MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
- MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
- MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
- MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
-
- MD5STEP(F2, a, b, c, d, in[ 1] + 0xf61e2562, 5);
- MD5STEP(F2, d, a, b, c, in[ 6] + 0xc040b340, 9);
- MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
- MD5STEP(F2, b, c, d, a, in[ 0] + 0xe9b6c7aa, 20);
- MD5STEP(F2, a, b, c, d, in[ 5] + 0xd62f105d, 5);
- MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
- MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
- MD5STEP(F2, b, c, d, a, in[ 4] + 0xe7d3fbc8, 20);
- MD5STEP(F2, a, b, c, d, in[ 9] + 0x21e1cde6, 5);
- MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
- MD5STEP(F2, c, d, a, b, in[ 3] + 0xf4d50d87, 14);
- MD5STEP(F2, b, c, d, a, in[ 8] + 0x455a14ed, 20);
- MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
- MD5STEP(F2, d, a, b, c, in[ 2] + 0xfcefa3f8, 9);
- MD5STEP(F2, c, d, a, b, in[ 7] + 0x676f02d9, 14);
- MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
-
- MD5STEP(F3, a, b, c, d, in[ 5] + 0xfffa3942, 4);
- MD5STEP(F3, d, a, b, c, in[ 8] + 0x8771f681, 11);
- MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
- MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
- MD5STEP(F3, a, b, c, d, in[ 1] + 0xa4beea44, 4);
- MD5STEP(F3, d, a, b, c, in[ 4] + 0x4bdecfa9, 11);
- MD5STEP(F3, c, d, a, b, in[ 7] + 0xf6bb4b60, 16);
- MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
- MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
- MD5STEP(F3, d, a, b, c, in[ 0] + 0xeaa127fa, 11);
- MD5STEP(F3, c, d, a, b, in[ 3] + 0xd4ef3085, 16);
- MD5STEP(F3, b, c, d, a, in[ 6] + 0x04881d05, 23);
- MD5STEP(F3, a, b, c, d, in[ 9] + 0xd9d4d039, 4);
- MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
- MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
- MD5STEP(F3, b, c, d, a, in[2 ] + 0xc4ac5665, 23);
-
- MD5STEP(F4, a, b, c, d, in[ 0] + 0xf4292244, 6);
- MD5STEP(F4, d, a, b, c, in[7 ] + 0x432aff97, 10);
- MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
- MD5STEP(F4, b, c, d, a, in[5 ] + 0xfc93a039, 21);
- MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
- MD5STEP(F4, d, a, b, c, in[3 ] + 0x8f0ccc92, 10);
- MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
- MD5STEP(F4, b, c, d, a, in[1 ] + 0x85845dd1, 21);
- MD5STEP(F4, a, b, c, d, in[8 ] + 0x6fa87e4f, 6);
- MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
- MD5STEP(F4, c, d, a, b, in[6 ] + 0xa3014314, 15);
- MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
- MD5STEP(F4, a, b, c, d, in[4 ] + 0xf7537e82, 6);
- MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
- MD5STEP(F4, c, d, a, b, in[2 ] + 0x2ad7d2bb, 15);
- MD5STEP(F4, b, c, d, a, in[9 ] + 0xeb86d391, 21);
-
- state[0] += a;
- state[1] += b;
- state[2] += c;
- state[3] += d;
-}
diff --git a/src/hash/sha512.h b/src/hash/sha512.h
deleted file mode 100644
index 4f368a1..0000000
--- a/src/hash/sha512.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/*-
- * Copyright 2005 Colin Percival
- * 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.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 CONTRIBUTORS 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.
- *
- * $FreeBSD$
- */
-
-#ifndef _SHA512_H_
-#define _SHA512_H_
-
-#include <sys/types.h>
-
-#define SHA512_DIGEST_LENGTH 64
-
-typedef struct SHA512Context {
- uint64_t state[8];
- uint64_t count[2];
- unsigned char buf[128];
-} SHA512_CTX;
-
-__BEGIN_DECLS
-
-void SHA512_Init(SHA512_CTX *);
-void SHA512_Update(SHA512_CTX *, const void *, size_t);
-void SHA512_Final(unsigned char [64], SHA512_CTX *);
-char *SHA512_End(SHA512_CTX *, char *);
-char *SHA512_File(const char *, char *);
-char *SHA512_FileChunk(const char *, char *, off_t, off_t);
-char *SHA512_Data(const void *, unsigned int, char *);
-__END_DECLS
-
-#endif /* !_SHA512_H_ */
diff --git a/src/hash/sha512c.c b/src/hash/sha512c.c
deleted file mode 100644
index b3c8d5e..0000000
--- a/src/hash/sha512c.c
+++ /dev/null
@@ -1,318 +0,0 @@
-/*-
- * Copyright 2005 Colin Percival
- * 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.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 CONTRIBUTORS 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.
- */
-
-#include <sys/cdefs.h>
-#include <sys/endian.h>
-#include <sys/types.h>
-
-#include <string.h>
-
-#include "sha512.h"
-
-#if BYTE_ORDER == BIG_ENDIAN
-
-/* Copy a vector of big-endian uint64_t into a vector of bytes */
-#define be64enc_vect(dst, src, len) \
- memcpy((void *)dst, (const void *)src, (size_t)len)
-
-/* Copy a vector of bytes into a vector of big-endian uint64_t */
-#define be64dec_vect(dst, src, len) \
- memcpy((void *)dst, (const void *)src, (size_t)len)
-
-#else /* BYTE_ORDER != BIG_ENDIAN */
-
-/*
- * Encode a length len/4 vector of (uint64_t) into a length len vector of
- * (unsigned char) in big-endian form. Assumes len is a multiple of 8.
- */
-static void
-be64enc_vect(unsigned char *dst, const uint64_t *src, size_t len)
-{
- size_t i;
-
- for (i = 0; i < len / 8; i++)
- be64enc(dst + i * 8, src[i]);
-}
-
-/*
- * Decode a big-endian length len vector of (unsigned char) into a length
- * len/4 vector of (uint64_t). Assumes len is a multiple of 8.
- */
-static void
-be64dec_vect(uint64_t *dst, const unsigned char *src, size_t len)
-{
- size_t i;
-
- for (i = 0; i < len / 8; i++)
- dst[i] = be64dec(src + i * 8);
-}
-
-#endif /* BYTE_ORDER != BIG_ENDIAN */
-
-/* Elementary functions used by SHA512 */
-#define Ch(x, y, z) ((x & (y ^ z)) ^ z)
-#define Maj(x, y, z) ((x & (y | z)) | (y & z))
-#define SHR(x, n) (x >> n)
-#define ROTR(x, n) ((x >> n) | (x << (64 - n)))
-#define S0(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
-#define S1(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
-#define s0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
-#define s1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHR(x, 6))
-
-/* SHA512 round function */
-#define RND(a, b, c, d, e, f, g, h, k) \
- t0 = h + S1(e) + Ch(e, f, g) + k; \
- t1 = S0(a) + Maj(a, b, c); \
- d += t0; \
- h = t0 + t1;
-
-/* Adjusted round function for rotating state */
-#define RNDr(S, W, i, k) \
- RND(S[(80 - i) % 8], S[(81 - i) % 8], \
- S[(82 - i) % 8], S[(83 - i) % 8], \
- S[(84 - i) % 8], S[(85 - i) % 8], \
- S[(86 - i) % 8], S[(87 - i) % 8], \
- W[i] + k)
-
-/*
- * SHA512 block compression function. The 512-bit state is transformed via
- * the 512-bit input block to produce a new state.
- */
-static void
-SHA512_Transform(uint64_t * state, const unsigned char block[128])
-{
- uint64_t W[80];
- uint64_t S[8];
- uint64_t t0, t1;
- int i;
-
- /* 1. Prepare message schedule W. */
- be64dec_vect(W, block, 128);
- for (i = 16; i < 80; i++)
- W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
-
- /* 2. Initialize working variables. */
- memcpy(S, state, 64);
-
- /* 3. Mix. */
- RNDr(S, W, 0, 0x428a2f98d728ae22ULL);
- RNDr(S, W, 1, 0x7137449123ef65cdULL);
- RNDr(S, W, 2, 0xb5c0fbcfec4d3b2fULL);
- RNDr(S, W, 3, 0xe9b5dba58189dbbcULL);
- RNDr(S, W, 4, 0x3956c25bf348b538ULL);
- RNDr(S, W, 5, 0x59f111f1b605d019ULL);
- RNDr(S, W, 6, 0x923f82a4af194f9bULL);
- RNDr(S, W, 7, 0xab1c5ed5da6d8118ULL);
- RNDr(S, W, 8, 0xd807aa98a3030242ULL);
- RNDr(S, W, 9, 0x12835b0145706fbeULL);
- RNDr(S, W, 10, 0x243185be4ee4b28cULL);
- RNDr(S, W, 11, 0x550c7dc3d5ffb4e2ULL);
- RNDr(S, W, 12, 0x72be5d74f27b896fULL);
- RNDr(S, W, 13, 0x80deb1fe3b1696b1ULL);
- RNDr(S, W, 14, 0x9bdc06a725c71235ULL);
- RNDr(S, W, 15, 0xc19bf174cf692694ULL);
- RNDr(S, W, 16, 0xe49b69c19ef14ad2ULL);
- RNDr(S, W, 17, 0xefbe4786384f25e3ULL);
- RNDr(S, W, 18, 0x0fc19dc68b8cd5b5ULL);
- RNDr(S, W, 19, 0x240ca1cc77ac9c65ULL);
- RNDr(S, W, 20, 0x2de92c6f592b0275ULL);
- RNDr(S, W, 21, 0x4a7484aa6ea6e483ULL);
- RNDr(S, W, 22, 0x5cb0a9dcbd41fbd4ULL);
- RNDr(S, W, 23, 0x76f988da831153b5ULL);
- RNDr(S, W, 24, 0x983e5152ee66dfabULL);
- RNDr(S, W, 25, 0xa831c66d2db43210ULL);
- RNDr(S, W, 26, 0xb00327c898fb213fULL);
- RNDr(S, W, 27, 0xbf597fc7beef0ee4ULL);
- RNDr(S, W, 28, 0xc6e00bf33da88fc2ULL);
- RNDr(S, W, 29, 0xd5a79147930aa725ULL);
- RNDr(S, W, 30, 0x06ca6351e003826fULL);
- RNDr(S, W, 31, 0x142929670a0e6e70ULL);
- RNDr(S, W, 32, 0x27b70a8546d22ffcULL);
- RNDr(S, W, 33, 0x2e1b21385c26c926ULL);
- RNDr(S, W, 34, 0x4d2c6dfc5ac42aedULL);
- RNDr(S, W, 35, 0x53380d139d95b3dfULL);
- RNDr(S, W, 36, 0x650a73548baf63deULL);
- RNDr(S, W, 37, 0x766a0abb3c77b2a8ULL);
- RNDr(S, W, 38, 0x81c2c92e47edaee6ULL);
- RNDr(S, W, 39, 0x92722c851482353bULL);
- RNDr(S, W, 40, 0xa2bfe8a14cf10364ULL);
- RNDr(S, W, 41, 0xa81a664bbc423001ULL);
- RNDr(S, W, 42, 0xc24b8b70d0f89791ULL);
- RNDr(S, W, 43, 0xc76c51a30654be30ULL);
- RNDr(S, W, 44, 0xd192e819d6ef5218ULL);
- RNDr(S, W, 45, 0xd69906245565a910ULL);
- RNDr(S, W, 46, 0xf40e35855771202aULL);
- RNDr(S, W, 47, 0x106aa07032bbd1b8ULL);
- RNDr(S, W, 48, 0x19a4c116b8d2d0c8ULL);
- RNDr(S, W, 49, 0x1e376c085141ab53ULL);
- RNDr(S, W, 50, 0x2748774cdf8eeb99ULL);
- RNDr(S, W, 51, 0x34b0bcb5e19b48a8ULL);
- RNDr(S, W, 52, 0x391c0cb3c5c95a63ULL);
- RNDr(S, W, 53, 0x4ed8aa4ae3418acbULL);
- RNDr(S, W, 54, 0x5b9cca4f7763e373ULL);
- RNDr(S, W, 55, 0x682e6ff3d6b2b8a3ULL);
- RNDr(S, W, 56, 0x748f82ee5defb2fcULL);
- RNDr(S, W, 57, 0x78a5636f43172f60ULL);
- RNDr(S, W, 58, 0x84c87814a1f0ab72ULL);
- RNDr(S, W, 59, 0x8cc702081a6439ecULL);
- RNDr(S, W, 60, 0x90befffa23631e28ULL);
- RNDr(S, W, 61, 0xa4506cebde82bde9ULL);
- RNDr(S, W, 62, 0xbef9a3f7b2c67915ULL);
- RNDr(S, W, 63, 0xc67178f2e372532bULL);
- RNDr(S, W, 64, 0xca273eceea26619cULL);
- RNDr(S, W, 65, 0xd186b8c721c0c207ULL);
- RNDr(S, W, 66, 0xeada7dd6cde0eb1eULL);
- RNDr(S, W, 67, 0xf57d4f7fee6ed178ULL);
- RNDr(S, W, 68, 0x06f067aa72176fbaULL);
- RNDr(S, W, 69, 0x0a637dc5a2c898a6ULL);
- RNDr(S, W, 70, 0x113f9804bef90daeULL);
- RNDr(S, W, 71, 0x1b710b35131c471bULL);
- RNDr(S, W, 72, 0x28db77f523047d84ULL);
- RNDr(S, W, 73, 0x32caab7b40c72493ULL);
- RNDr(S, W, 74, 0x3c9ebe0a15c9bebcULL);
- RNDr(S, W, 75, 0x431d67c49c100d4cULL);
- RNDr(S, W, 76, 0x4cc5d4becb3e42b6ULL);
- RNDr(S, W, 77, 0x597f299cfc657e2aULL);
- RNDr(S, W, 78, 0x5fcb6fab3ad6faecULL);
- RNDr(S, W, 79, 0x6c44198c4a475817ULL);
-
- /* 4. Mix local working variables into global state */
- for (i = 0; i < 8; i++)
- state[i] += S[i];
-}
-
-static unsigned char PAD[128] = {
- 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-
-/* Add padding and terminating bit-count. */
-static void
-SHA512_Pad(SHA512_CTX * ctx)
-{
- unsigned char len[16];
- uint64_t r, plen;
-
- /*
- * Convert length to a vector of bytes -- we do this now rather
- * than later because the length will change after we pad.
- */
- be64enc_vect(len, ctx->count, 16);
-
- /* Add 1--128 bytes so that the resulting length is 112 mod 128 */
- r = (ctx->count[1] >> 3) & 0x7f;
- plen = (r < 112) ? (112 - r) : (240 - r);
- SHA512_Update(ctx, PAD, (size_t)plen);
-
- /* Add the terminating bit-count */
- SHA512_Update(ctx, len, 16);
-}
-
-/* SHA-512 initialization. Begins a SHA-512 operation. */
-void
-SHA512_Init(SHA512_CTX * ctx)
-{
-
- /* Zero bits processed so far */
- ctx->count[0] = ctx->count[1] = 0;
-
- /* Magic initialization constants */
- ctx->state[0] = 0x6a09e667f3bcc908ULL;
- ctx->state[1] = 0xbb67ae8584caa73bULL;
- ctx->state[2] = 0x3c6ef372fe94f82bULL;
- ctx->state[3] = 0xa54ff53a5f1d36f1ULL;
- ctx->state[4] = 0x510e527fade682d1ULL;
- ctx->state[5] = 0x9b05688c2b3e6c1fULL;
- ctx->state[6] = 0x1f83d9abfb41bd6bULL;
- ctx->state[7] = 0x5be0cd19137e2179ULL;
-}
-
-/* Add bytes into the hash */
-void
-SHA512_Update(SHA512_CTX * ctx, const void *in, size_t len)
-{
- uint64_t bitlen[2];
- uint64_t r;
- const unsigned char *src = in;
-
- /* Number of bytes left in the buffer from previous updates */
- r = (ctx->count[1] >> 3) & 0x7f;
-
- /* Convert the length into a number of bits */
- bitlen[1] = ((uint64_t)len) << 3;
- bitlen[0] = ((uint64_t)len) >> 61;
-
- /* Update number of bits */
- if ((ctx->count[1] += bitlen[1]) < bitlen[1])
- ctx->count[0]++;
- ctx->count[0] += bitlen[0];
-
- /* Handle the case where we don't need to perform any transforms */
- if (len < 128 - r) {
- memcpy(&ctx->buf[r], src, len);
- return;
- }
-
- /* Finish the current block */
- memcpy(&ctx->buf[r], src, 128 - r);
- SHA512_Transform(ctx->state, ctx->buf);
- src += 128 - r;
- len -= 128 - r;
-
- /* Perform complete blocks */
- while (len >= 128) {
- SHA512_Transform(ctx->state, src);
- src += 128;
- len -= 128;
- }
-
- /* Copy left over data into buffer */
- memcpy(ctx->buf, src, len);
-}
-
-/*
- * SHA-512 finalization. Pads the input data, exports the hash value,
- * and clears the context state.
- */
-void
-SHA512_Final(unsigned char digest[64], SHA512_CTX * ctx)
-{
-
- /* Add padding */
- SHA512_Pad(ctx);
-
- /* Write the hash */
- be64enc_vect(digest, ctx->state, 64);
-
- /* Clear the context state */
- memset((void *)ctx, 0, sizeof(*ctx));
-}
diff --git a/src/libbsd.map b/src/libbsd.map
index 34e709b..fb2888f 100644
--- a/src/libbsd.map
+++ b/src/libbsd.map
@@ -35,6 +35,8 @@ LIBBSD_0.0 {
strunvis;
strunvisx;
+ /* The following functions are provided via the system libc or libmd
+ * implementations. */
MD5Init;
MD5Update;
MD5Pad;
diff --git a/src/md5.c b/src/md5.c
new file mode 100644
index 0000000..b74ce7d
--- /dev/null
+++ b/src/md5.c
@@ -0,0 +1,91 @@
+/*
+ * Copyright © 2021 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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 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.
+ */
+
+#include <md5.h>
+#include "local-link.h"
+
+void
+bsd_MD5Init(MD5_CTX *context)
+{
+ MD5Init(context);
+}
+libbsd_symver_variant(MD5Init, bsd_MD5Init, LIBBSD_0.0);
+
+void
+bsd_MD5Update(MD5_CTX *context, const uint8_t *data, size_t len)
+{
+ MD5Update(context, data, len);
+}
+libbsd_symver_variant(MD5Update, bsd_MD5Update, LIBBSD_0.0);
+
+void
+bsd_MD5Pad(MD5_CTX *context)
+{
+ MD5Pad(context);
+}
+libbsd_symver_variant(MD5Pad, bsd_MD5Pad, LIBBSD_0.0);
+
+void
+bsd_MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
+{
+ MD5Final(digest, context);
+}
+libbsd_symver_variant(MD5Final, bsd_MD5Final, LIBBSD_0.0);
+
+void
+bsd_MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
+{
+ MD5Transform(state, block);
+}
+libbsd_symver_variant(MD5Transform, bsd_MD5Transform, LIBBSD_0.0);
+
+char *
+bsd_MD5End(MD5_CTX *context, char *buf)
+{
+ return MD5End(context, buf);
+}
+libbsd_symver_variant(MD5End, bsd_MD5End, LIBBSD_0.0);
+
+char *
+bsd_MD5File(const char *filename, char *buf)
+{
+ return MD5File(filename, buf);
+}
+libbsd_symver_variant(MD5File, bsd_MD5File, LIBBSD_0.0);
+
+char *
+bsd_MD5FileChunk(const char *filename, char *buf, off_t offset, off_t length)
+{
+ return MD5FileChunk(filename, buf, offset, length);
+}
+libbsd_symver_variant(MD5FileChunk, bsd_MD5FileChunk, LIBBSD_0.0);
+
+char *
+bsd_MD5Data(const uint8_t *data, size_t len, char *buf)
+{
+ return MD5Data(data, len, buf);
+}
+libbsd_symver_variant(MD5Data, bsd_MD5Data, LIBBSD_0.0);
diff --git a/test/Makefile.am b/test/Makefile.am
index 90fe384..a1c8e68 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -52,6 +52,8 @@ check_PROGRAMS = \
vis-openbsd \
$(nil)
+md5_LDADD = $(LDADD) $(MD_LIBS)
+
if HAVE_LIBTESTU01
arc4random_LDADD = $(LDADD) $(TESTU01_LIBS)