pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/digest/files digest: Add blake2s hash.



details:   https://anonhg.NetBSD.org/pkgsrc/rev/33c5892d0cd7
branches:  trunk
changeset: 459016:33c5892d0cd7
user:      nia <nia%pkgsrc.org@localhost>
date:      Tue Sep 28 13:05:08 2021 +0000

description:
digest: Add blake2s hash.

diffstat:

 pkgtools/digest/files/Makefile.in  |    4 +-
 pkgtools/digest/files/blake2s-hl.c |  128 +++++++++++++
 pkgtools/digest/files/blake2s.c    |  339 +++++++++++++++++++++++++++++++++++++
 pkgtools/digest/files/blake2s.h    |   55 ++++++
 pkgtools/digest/files/configure    |    2 +-
 pkgtools/digest/files/digest.1     |    8 +-
 pkgtools/digest/files/digest.c     |    6 +-
 7 files changed, 536 insertions(+), 6 deletions(-)

diffs (truncated from 620 to 300 lines):

diff -r 9e123c30f8eb -r 33c5892d0cd7 pkgtools/digest/files/Makefile.in
--- a/pkgtools/digest/files/Makefile.in Tue Sep 28 13:01:37 2021 +0000
+++ b/pkgtools/digest/files/Makefile.in Tue Sep 28 13:05:08 2021 +0000
@@ -1,4 +1,4 @@
-# $Id: Makefile.in,v 1.7 2018/10/06 14:32:45 nia Exp $
+# $Id: Makefile.in,v 1.8 2021/09/28 13:05:08 nia Exp $
 
 @SET_MAKE@
 SHELL = @SHELL@
@@ -44,7 +44,7 @@
 
 digest_OBJS = digest.o keccak.o md5c.o md5hl.o rmd160.o \
 rmd160hl.o sha1.o sha1hl.o sha2.o sha2hl.o sha3.o sha3hl.o \
-tiger.o whirlpool.o blake2b.o blake2b-hl.o
+tiger.o whirlpool.o blake2b.o blake2b-hl.o blake2s.o blake2s-hl.o
 
 SRCS= digest.c keccak.c md5c.c md5hl.c rmd160.c \
 rmd160hl.c sha1.c sha1hl.c sha2.c sha2hl.c sha3.c sha3hl.c \
diff -r 9e123c30f8eb -r 33c5892d0cd7 pkgtools/digest/files/blake2s-hl.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/pkgtools/digest/files/blake2s-hl.c        Tue Sep 28 13:05:08 2021 +0000
@@ -0,0 +1,128 @@
+/* $NetBSD: blake2s-hl.c,v 1.1 2021/09/28 13:05:08 nia Exp $   */
+
+/*
+ * blake2s-hl.c
+ * This code is derived from sha2hl.c, hence the following licence
+ * reproduction.
+ *
+ * This code is not a verbatim copy, since some routines have been added,
+ * and some bugs have been fixed.
+ *
+ * Version 1.0.0beta1
+ *
+ * Written by Aaron D. Gifford <me%aarongifford.com@localhost>
+ *
+ * Copyright 2000 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(S) 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(S) 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.
+ *
+ */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include "blake2s.h"
+
+static const char blake2_hex_digits[] = "0123456789abcdef";
+
+#ifndef MEMSET_BZERO
+#define MEMSET_BZERO(p,l)      memset((p), 0, (l))
+#endif
+
+#ifndef _DIAGASSERT
+#define _DIAGASSERT(cond)      assert(cond)
+#endif
+
+void
+BLAKE2s_Init(struct blake2s *ctx)
+{
+       blake2s_init(ctx, BLAKE2S_MAX_DIGEST, NULL, 0);
+}
+
+void
+BLAKE2s_Update(struct blake2s *ctx, const uint8_t *input, size_t len)
+{
+       blake2s_update(ctx, input, len);
+}
+
+char *
+BLAKE2s_File(char *filename, char *buf)
+{
+       unsigned char   buffer[BUFSIZ * 20];
+       struct blake2s  ctx;
+       int             fd, num, oerrno;
+
+       _DIAGASSERT(filename != NULL);
+       /* XXX: buf may be NULL ? */
+
+       BLAKE2s_Init(&ctx);
+
+       if ((fd = open(filename, O_RDONLY)) < 0)
+               return (0);
+
+       while ((num = read(fd, buffer, sizeof(buffer))) > 0)
+               blake2s_update(&ctx, buffer, (size_t) num);
+
+       oerrno = errno;
+       close(fd);
+       errno = oerrno;
+       BLAKE2s_End(&ctx, buf);
+       return (num < 0 ? 0 : buf);
+}
+
+char *
+BLAKE2s_End(struct blake2s *ctx, char buffer[])
+{
+       unsigned char digest[BLAKE2S_MAX_DIGEST], *d = digest;
+       unsigned char *ret;
+       int i;
+
+       assert(ctx != NULL);
+
+       if ((ret = buffer) != NULL) {
+               blake2s_final(ctx, digest);
+
+               for (i = 0; i < BLAKE2S_MAX_DIGEST; i++) {
+                       *buffer++ = blake2_hex_digits[(*d & 0xf0) >> 4];
+                       *buffer++ = blake2_hex_digits[*d & 0x0f];
+                       d++;
+               }
+               *buffer = '\0';
+       } else {
+               (void)MEMSET_BZERO(ctx, sizeof(struct blake2s));
+       }
+       (void)MEMSET_BZERO(digest, BLAKE2S_MAX_DIGEST);
+       return ret;
+}
diff -r 9e123c30f8eb -r 33c5892d0cd7 pkgtools/digest/files/blake2s.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/pkgtools/digest/files/blake2s.c   Tue Sep 28 13:05:08 2021 +0000
@@ -0,0 +1,339 @@
+/*-
+ * Copyright (c) 2015 Taylor R. Campbell
+ * 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.
+ */
+
+#define        _POSIX_C_SOURCE 200809L
+
+#include <assert.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "blake2s.h"
+
+void *(*volatile blake2s_explicit_memset_impl)(void *, int, size_t) = &memset;
+static void *
+explicit_memset(void *buf, int c, size_t n)
+{
+
+       return (*blake2s_explicit_memset_impl)(buf, c, n);
+}
+
+static inline uint32_t
+rotr32(uint32_t x, unsigned c)
+{
+
+       return ((x >> c) | (x << (32 - c)));
+}
+
+static inline uint32_t
+le32dec(const void *buf)
+{
+       const uint8_t *p = buf;
+
+       return (((uint32_t)p[0]) |
+           ((uint32_t)p[1] << 8) |
+           ((uint32_t)p[2] << 16) |
+           ((uint32_t)p[3] << 24));
+}
+
+static inline void
+le32enc(void *buf, uint32_t v)
+{
+       uint8_t *p = buf;
+
+       *p++ = v; v >>= 8;
+       *p++ = v; v >>= 8;
+       *p++ = v; v >>= 8;
+       *p++ = v;
+}
+
+#define        BLAKE2S_G(VA, VB, VC, VD, X, Y) do                                    \
+{                                                                            \
+       (VA) = (VA) + (VB) + (X);                                             \
+       (VD) = rotr32((VD) ^ (VA), 16);                                       \
+       (VC) = (VC) + (VD);                                                   \
+       (VB) = rotr32((VB) ^ (VC), 12);                                       \
+       (VA) = (VA) + (VB) + (Y);                                             \
+       (VD) = rotr32((VD) ^ (VA), 8);                                        \
+       (VC) = (VC) + (VD);                                                   \
+       (VB) = rotr32((VB) ^ (VC), 7);                                        \
+} while (0)
+
+static const uint32_t blake2s_iv[8] = {
+       0x6a09e667U, 0xbb67ae85U, 0x3c6ef372U, 0xa54ff53aU,
+       0x510e527fU, 0x9b05688cU, 0x1f83d9abU, 0x5be0cd19U,
+};
+
+static const uint8_t blake2s_sigma[10][16] = {
+       {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 },
+       { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 },
+       { 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4 },
+       {  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8 },
+       {  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13 },
+       {  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9 },
+       { 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11 },
+       { 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10 },
+       {  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5 },
+       { 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13,  0 },
+};
+
+static void
+blake2s_compress(uint32_t h[8], uint64_t c, uint32_t last,
+    const uint8_t in[64])
+{
+       uint32_t v0,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15;
+       uint32_t m[16];
+       unsigned i;
+
+       /* Load the variables: first 8 from state, next 8 from IV.  */
+       v0 = h[0];
+       v1 = h[1];
+       v2 = h[2];
+       v3 = h[3];
+       v4 = h[4];
+       v5 = h[5];
+       v6 = h[6];
+       v7 = h[7];
+       v8 = blake2s_iv[0];
+       v9 = blake2s_iv[1];
+       v10 = blake2s_iv[2];
+       v11 = blake2s_iv[3];
+       v12 = blake2s_iv[4];
+       v13 = blake2s_iv[5];
+       v14 = blake2s_iv[6];
+       v15 = blake2s_iv[7];
+
+       /* Incorporate the block counter and whether this is last.  */
+       v12 ^= c & 0xffffffffU;
+       v13 ^= c >> 32;
+       v14 ^= last;
+
+       /* Load the message block.  */
+       for (i = 0; i < 16; i++)
+               m[i] = le32dec(in + 4*i);
+
+       /* Transform the variables.  */
+       for (i = 0; i < 10; i++) {
+               const uint8_t *sigma = blake2s_sigma[i];
+
+               BLAKE2S_G(v0, v4,  v8, v12, m[sigma[ 0]], m[sigma[ 1]]);
+               BLAKE2S_G(v1, v5,  v9, v13, m[sigma[ 2]], m[sigma[ 3]]);
+               BLAKE2S_G(v2, v6, v10, v14, m[sigma[ 4]], m[sigma[ 5]]);
+               BLAKE2S_G(v3, v7, v11, v15, m[sigma[ 6]], m[sigma[ 7]]);
+               BLAKE2S_G(v0, v5, v10, v15, m[sigma[ 8]], m[sigma[ 9]]);
+               BLAKE2S_G(v1, v6, v11, v12, m[sigma[10]], m[sigma[11]]);



Home | Main Index | Thread Index | Old Index