Import of pkgsrc-2014Q1

This commit is contained in:
2014-04-17 16:38:45 +02:00
parent 785076ae39
commit 9a8c06dafb
19365 changed files with 828089 additions and 278039 deletions

7
security/multigest/DESCR Normal file
View File

@@ -0,0 +1,7 @@
The multigest utility calculates message digests of files or, if no
file is specified, standard input. The list of possible algorithms is
given in the libmultigest(3) manual page.
The multigest utility is a simple wrapper for the various different
algorithm implementations, and was designed to be scalable as new
message digest algorithms are developed.

View File

@@ -0,0 +1,19 @@
# $NetBSD: Makefile,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $
DISTNAME= multigest-20140303
CATEGORIES= security
MASTER_SITES= # not used
DISTFILES= # not used
MAINTAINER= agc@NetBSD.org
HOMEPAGE= http://www.NetBSD.org/
COMMENT= Library and utility to calculate multiple message digests
LICENSE= modified-bsd
AUTO_MKDIRS= yes
GNU_CONFIGURE= yes
do-extract:
${CP} -R ${FILESDIR} ${WRKSRC}
.include "../../mk/bsd.pkg.mk"

3
security/multigest/PLIST Normal file
View File

@@ -0,0 +1,3 @@
@comment $NetBSD: PLIST,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $
bin/multigest
man/man1/multigest.1

View File

@@ -0,0 +1 @@
$NetBSD: distinfo,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $

View File

@@ -0,0 +1,20 @@
# $NetBSD: Makefile,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $
.include <bsd.own.mk>
PROG=m
SRCS= blake2.c crc32c.c keccak.c main.c md5c.c multigest.c rmd160.c sha1.c sha2.c tiger.c whirlpool.c
CPPFLAGS+=-I.
CPPFLAGS+=-g -O0
LDFLAGS+=-g -O0
WARNS=6
MAN=multigest.1
.include <bsd.prog.mk>
t: ${PROG}
./${PROG} -o 20.out -a blake2,crc32c,md5,rmd160,sha1,sha256,sha3-224,sha3-256,sha3-384,sha3-512,sha512,size,tiger2,tiger,whirlpool -r ${.CURDIR}/../bin/2.in
diff 20.expected 20.out
rm -f 20.out

View File

@@ -0,0 +1,32 @@
# $NetBSD: Makefile.in,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $
PROG=multigest
OBJS+= blake2.o
OBJS+= crc32c.o
OBJS+= keccak.o
OBJS+= main.o
OBJS+= md5c.o
OBJS+= multigest.o
OBJS+= rmd160.o
OBJS+= sha1.o
OBJS+= sha2.o
OBJS+= tiger.o
OBJS+= whirlpool.o
PREFIX=@PREFIX@
MANDIR=@MANDIR@
all: ${PROG}
${PROG}: ${OBJS}
${CC} ${OBJS} -o ${PROG}
install:
install -c -s ${PROG} ${DESTDIR}${PREFIX}/bin
install -c multigest.1 ${DESTDIR}${MANDIR}/man1
clean:
rm -rf *.core ${OBJS} ${PROG}
cleandist:
rm -rf *.core ${OBJS} ${PROG} Makefile

View File

@@ -0,0 +1,32 @@
# $NetBSD: Makefile.lib.in,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $
LIB= libmultigest.a
OBJS+= blake2.o
OBJS+= crc32c.o
OBJS+= keccak.o
OBJS+= md5c.o
OBJS+= multigest.o
OBJS+= rmd160.o
OBJS+= sha1.o
OBJS+= sha2.o
OBJS+= tiger.o
OBJS+= whirlpool.o
PREFIX=@PREFIX@
MANDIR=@MANDIR@
all: ${LIB}
${LIB}: ${OBJS}
ar crv ${LIB} ${OBJS}
install:
${BSD_INSTALL_LIB} ${LIB} ${DESTDIR}${PREFIX}/lib
${BSD_INSTALL_DATA} multigest.h ${DESTDIR}${PREFIX}/include
${BSD_INSTALL_DATA} libmultigest.3 ${DESTDIR}${PREFIX}/${PKGMANDIR}/man3
clean:
rm -rf *.core ${OBJS} ${LIB}
cleandist:
rm -rf *.core ${OBJS} ${LIB} Makefile

View File

@@ -0,0 +1,35 @@
# $NetBSD: Makefile.libtool.in,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $
LIB= libmultigest.a
OBJS+= blake2.o
OBJS+= crc32c.o
OBJS+= keccak.o
OBJS+= md5c.o
OBJS+= multigest.o
OBJS+= rmd160.o
OBJS+= sha1.o
OBJS+= sha2.o
OBJS+= tiger.o
OBJS+= whirlpool.o
PREFIX=@PREFIX@
MANDIR=@MANDIR@
all: ${LIB}
${LIB}: ${OBJS}
${LIBTOOL} --mode=link --tag=CC cc -o ${.TARGET:.a=.la} ${OBJS:.o=.lo} -rpath ${PREFIX}/lib -version-info 0:0
.c.o:
${LIBTOOL} --mode=compile --tag=CC ${CC} ${CFLAGS} -c $<
install:
libtool --mode=install ${BSD_INSTALL_LIB} libmultigest.la ${DESTDIR}${PREFIX}/lib
${BSD_INSTALL_DATA} multigest.h ${DESTDIR}${PREFIX}/include
${BSD_INSTALL_DATA} libmultigest.3 ${DESTDIR}${PREFIX}/${PKGMANDIR}/man3
clean:
rm -rf *.core ${OBJS} ${LIB}
cleandist:
rm -rf *.core ${OBJS} ${LIB} Makefile

View File

@@ -0,0 +1,419 @@
/*-
* Copyright (c) 2013 Alistair Crooks <agc@NetBSD.org>
* 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 ``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 <stdint.h>
#include <string.h>
#include <stdio.h>
#include "blake2.h"
#define BLAKE2B_SALTBYTES 16
#define BLAKE2B_PERSONALBYTES 16
#define BLAKE2B_OUTBYTES 64
#define BLAKE2B_KEYBYTES 64
typedef struct __blake2b_param {
uint8_t digest_length; // 1
uint8_t key_length; // 2
uint8_t fanout; // 3
uint8_t depth; // 4
uint32_t leaf_length; // 8
uint64_t node_offset; // 16
uint8_t node_depth; // 17
uint8_t inner_length; // 18
uint8_t reserved[14]; // 32
uint8_t salt[BLAKE2B_SALTBYTES]; // 48
uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64
} blake2b_param;
static const uint64_t blake2b_IV[8] = {
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
};
static const uint8_t blake2b_sigma[12][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 },
{ 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 }
};
/* prevents compiler optimizing out memset() */
static inline void secure_zero_memory(void *v, size_t n)
{
volatile uint8_t *p = (volatile uint8_t *)v;
while (n--) {
*p++ = 0;
}
}
static inline uint64_t
rotr64(const uint64_t w, const unsigned c)
{
return (w >> c) | (w << (64 - c));
}
static inline uint64_t
load64(const void *src)
{
const uint8_t *p = (const uint8_t *)src;
uint64_t w;
int indian = 1;
if (*(char *)(void *)&indian) {
return *(const uint64_t *)(src);
}
w = *p++;
w |= (uint64_t)(*p++) << 8;
w |= (uint64_t)(*p++) << 16;
w |= (uint64_t)(*p++) << 24;
w |= (uint64_t)(*p++) << 32;
w |= (uint64_t)(*p++) << 40;
w |= (uint64_t)(*p++) << 48;
w |= (uint64_t)(*p++) << 56;
return w;
}
static inline void
store32(void *dst, uint32_t w)
{
uint8_t *p = (uint8_t *)dst;
int indian = 1;
if (*(char *)(void *)&indian) {
*(uint32_t *)(dst) = w;
}
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w;
}
static inline void
store64(void *dst, uint64_t w)
{
uint8_t *p = (uint8_t *)dst;
int indian = 1;
if (*(char *)(void *)&indian) {
*(uint64_t *)(dst) = w;
}
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w; w >>= 8;
*p++ = (uint8_t)w;
}
static inline int
blake2b_set_lastnode(BLAKE2_CTX *S)
{
S->f[1] = ~0ULL;
return 0;
}
static inline int
blake2b_clear_lastnode(BLAKE2_CTX *S)
{
S->f[1] = 0ULL;
return 0;
}
/* Some helper functions, not necessarily useful */
static inline int
blake2b_set_lastblock(BLAKE2_CTX *S)
{
if (S->last_node) {
blake2b_set_lastnode(S);
}
S->f[0] = ~0ULL;
return 0;
}
static inline int
blake2b_clear_lastblock(BLAKE2_CTX *S)
{
if (S->last_node) {
blake2b_clear_lastnode(S);
}
S->f[0] = 0ULL;
return 0;
}
static inline int
blake2b_increment_counter(BLAKE2_CTX *S, const uint64_t inc)
{
S->t[0] += inc;
S->t[1] += (S->t[0] < inc);
return 0;
}
/* Parameter-related functions */
static inline int
blake2b_param_set_digest_length(blake2b_param *P, const uint8_t digest_length)
{
P->digest_length = digest_length;
return 0;
}
static inline int
blake2b_param_set_fanout(blake2b_param *P, const uint8_t fanout)
{
P->fanout = fanout;
return 0;
}
static inline int
blake2b_param_set_max_depth(blake2b_param *P, const uint8_t depth)
{
P->depth = depth;
return 0;
}
static inline int
blake2b_param_set_leaf_length(blake2b_param *P, const uint32_t leaf_length)
{
store32(&P->leaf_length, leaf_length);
return 0;
}
static inline int
blake2b_param_set_node_offset(blake2b_param *P, const uint64_t node_offset)
{
store64(&P->node_offset, node_offset);
return 0;
}
static inline int
blake2b_param_set_node_depth(blake2b_param *P, const uint8_t node_depth)
{
P->node_depth = node_depth;
return 0;
}
static inline int
blake2b_param_set_inner_length(blake2b_param *P, const uint8_t inner_length)
{
P->inner_length = inner_length;
return 0;
}
static inline int
blake2b_param_set_salt(blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES])
{
memcpy(P->salt, salt, BLAKE2B_SALTBYTES);
return 0;
}
static inline int
blake2b_param_set_personal(blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES])
{
memcpy(P->personal, personal, BLAKE2B_PERSONALBYTES);
return 0;
}
static inline int
blake2b_init0(BLAKE2_CTX *S)
{
int i;
memset(S, 0, sizeof(*S));
for (i = 0; i < 8; ++i) {
S->h[i] = blake2b_IV[i];
}
return 0;
}
/* init xors IV with input parameter block */
static int
blake2b_init_param(BLAKE2_CTX *S, const blake2b_param *P)
{
const uint8_t *p;
size_t i;
blake2b_init0(S);
p = (const uint8_t *)(const void *)(P);
/* IV XOR ParamBlock */
for (i = 0; i < 8; ++i) {
S->h[i] ^= load64(p + sizeof(S->h[i]) * i);
}
return 0;
}
int
blake2b_init(BLAKE2_CTX *S, const uint8_t outlen)
{
blake2b_param P;
if ((!outlen) || (outlen > BLAKE2B_OUTBYTES)) {
return -1;
}
memset(&P, 0x0, sizeof(P));
P.digest_length = outlen;
P.key_length = 0;
P.fanout = 1;
P.depth = 1;
store32(&P.leaf_length, 0);
store64(&P.node_offset, 0);
P.node_depth = 0;
P.inner_length = 0;
memset(P.reserved, 0, sizeof(P.reserved));
memset(P.salt, 0, sizeof(P.salt));
memset(P.personal, 0, sizeof(P.personal));
return blake2b_init_param(S, &P);
}
static int
blake2b_compress(BLAKE2_CTX *S, const uint8_t block[BLAKE2B_BLOCKBYTES])
{
uint64_t m[16];
uint64_t v[16];
int i;
for (i = 0; i < 16; ++i) {
m[i] = load64(block + i * sizeof(m[i]));
}
for (i = 0; i < 8; ++i) {
v[i] = S->h[i];
}
v[8] = blake2b_IV[0];
v[9] = blake2b_IV[1];
v[10] = blake2b_IV[2];
v[11] = blake2b_IV[3];
v[12] = S->t[0] ^ blake2b_IV[4];
v[13] = S->t[1] ^ blake2b_IV[5];
v[14] = S->f[0] ^ blake2b_IV[6];
v[15] = S->f[1] ^ blake2b_IV[7];
#define G(r,i,a,b,c,d) do { \
a = a + b + m[blake2b_sigma[r][i + i]]; \
d = rotr64(d ^ a, 32); \
c = c + d; \
b = rotr64(b ^ c, 24); \
a = a + b + m[blake2b_sigma[r][i + i + 1]]; \
d = rotr64(d ^ a, 16); \
c = c + d; \
b = rotr64(b ^ c, 63); \
} while(/*CONSTCOND*/0)
#define ROUND(r) do { \
G(r,0,v[0],v[4],v[8],v[12]); \
G(r,1,v[1],v[5],v[9],v[13]); \
G(r,2,v[2],v[6],v[10],v[14]); \
G(r,3,v[3],v[7],v[11],v[15]); \
G(r,4,v[0],v[5],v[10],v[15]); \
G(r,5,v[1],v[6],v[11],v[12]); \
G(r,6,v[2],v[7],v[8],v[13]); \
G(r,7,v[3],v[4],v[9],v[14]); \
} while(/*CONSTCOND*/0)
ROUND(0);
ROUND(1);
ROUND(2);
ROUND(3);
ROUND(4);
ROUND(5);
ROUND(6);
ROUND(7);
ROUND(8);
ROUND(9);
ROUND(10);
ROUND(11);
for (i = 0; i < 8; ++i) {
S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
}
#undef G
#undef ROUND
return 0;
}
/* inlen now in bytes */
int
blake2b_update(BLAKE2_CTX *S, const uint8_t *in, uint64_t inlen)
{
while (inlen > 0) {
size_t left = S->buflen;
size_t fill = 2 * BLAKE2B_BLOCKBYTES - left;
if (inlen > fill) {
memcpy(S->buf + left, in, fill); // Fill buffer
S->buflen += fill;
blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
blake2b_compress(S, S->buf); // Compress
memcpy(S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES); // Shift buffer left
S->buflen -= BLAKE2B_BLOCKBYTES;
in += fill;
inlen -= fill;
} else {
// inlen <= fill
memcpy(S->buf + left, in, inlen);
S->buflen += inlen; // Be lazy, do not compress
in += inlen;
inlen -= inlen;
}
}
return 0;
}
/* Is this correct? */
int
blake2b_final(BLAKE2_CTX *S, uint8_t *out, uint8_t outlen)
{
uint8_t buffer[BLAKE2B_OUTBYTES];
int i;
if (S->buflen > BLAKE2B_BLOCKBYTES) {
blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
blake2b_compress(S, S->buf);
S->buflen -= BLAKE2B_BLOCKBYTES;
memcpy(S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen);
}
blake2b_increment_counter(S, S->buflen);
blake2b_set_lastblock(S);
memset(S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen); /* Padding */
blake2b_compress(S, S->buf);
for (i = 0; i < 8; ++i) {
/* Output full hash to temp buffer */
store64(buffer + sizeof(S->h[i]) * i, S->h[i]);
}
memcpy(out, buffer, outlen);
return 0;
}

View File

@@ -0,0 +1,59 @@
/*-
* Copyright (c) 2013 Alistair Crooks <agc@NetBSD.org>
* 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 ``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 BLAKE2_H_
#define BLAKE2_H_ 20130814
#include <inttypes.h>
#define BLAKE2B_BLOCKBYTES 128
typedef struct BLAKE2_CTX {
uint64_t h[8];
uint64_t t[2];
uint64_t f[2];
uint8_t buf[2 * BLAKE2B_BLOCKBYTES];
size_t buflen;
uint8_t last_node;
} BLAKE2_CTX;
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
__BEGIN_DECLS
int blake2b_init(BLAKE2_CTX */*state*/, const uint8_t /*outlen*/);
int blake2b_update(BLAKE2_CTX */*state*/, const uint8_t */*in*/, uint64_t /*inlen*/);
int blake2b_final(BLAKE2_CTX */*state*/, uint8_t */*out*/, uint8_t /*outlen*/);
__END_DECLS
#endif

64
security/multigest/files/configure vendored Executable file
View File

@@ -0,0 +1,64 @@
#! /bin/sh
# $NetBSD: configure,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $
# Copyright (c) 2013 Alistair Crooks <agc@NetBSD.org>
# 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 ``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.
#
# set up defs and paths, etc
prefix=/usr/local
mandir=${prefix}/man
while [ $# -gt 0 ]; do
case "$1" in
--prefix=*) prefix=$(echo "$1" | sed -e 's|--prefix=||') ;;
--mandir=*) mandir=$(echo "$1" | sed -e 's|--mandir=||') ;;
--build=*|--host=*|--infodir=*) ;;
*) break ;;
esac
shift
done
rm -f config.h
echo "#ifndef CONFIG_H_" > config.h
echo "#define CONFIG_H_ $(date '+%Y%m%d')" >> config.h
echo "" >> config.h
# portability macros
echo "#ifndef __UNCONST" >> config.h
echo "#define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))" >> config.h
echo "#endif /* __UNCONST */" >> config.h
echo "" >> config.h
echo "#ifndef USE_ARG" >> config.h
echo "#define USE_ARG(x) /*LINTED*/(void)&(x)" >> config.h
echo "#endif /* USE_ARG */" >> config.h
echo "" >> config.h
echo "#endif /* CONFIG_H_ */" >> config.h
for f in Makefile.in; do
sed -e 's|@PREFIX@|'${prefix}'|g' \
-e 's|@MANDIR@|'${mandir}'|g' $f > $(basename $f .in)
done
exit 0

View File

@@ -0,0 +1,752 @@
/*-
* Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
* Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* a) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* b) 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.
*
* c) Neither the name of Cisco Systems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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 "crc32c.h"
/**
*
* Routine Description:
*
* Computes the CRC32c checksum for the specified buffer using the slicing by 8
* algorithm over 64 bit quantities.
*
* Arguments:
*
* p_running_crc - pointer to the initial or final remainder value
* used in CRC computations. It should be set to
* non-NULL if the mode argument is equal to CONT or END
* p_buf - the packet buffer where crc computations are being performed
* length - the length of p_buf in bytes
* init_bytes - the number of initial bytes that need to be procesed before
* aligning p_buf to multiples of 4 bytes
* mode - can be any of the following: BEGIN, CONT, END, BODY, ALIGN
*
* Return value:
*
* The computed CRC32c value
*/
/*
* Copyright (c) 2004-2006 Intel Corporation - All Rights Reserved
*
*
* This software program is licensed subject to the BSD License, available at
* http://www.opensource.org/licenses/bsd-license.html.
*
* Abstract:
*
* Tables for software CRC generation
*/
/*
* The following CRC lookup table was generated automagically using the
* following model parameters:
*
* Generator Polynomial = ................. 0x1EDC6F41
* Generator Polynomial Length = .......... 32 bits
* Reflected Bits = ....................... TRUE
* Table Generation Offset = .............. 32 bits
* Number of Slices = ..................... 8 slices
* Slice Lengths = ........................ 8 8 8 8 8 8 8 8
* Directory Name = ....................... .\
* File Name = ............................ 8x256_tables.c
*/
static uint32_t crc_tableil8_o32[256] =
{
0x00000000, 0xF26B8303, 0xE13B70F7, 0x1350F3F4, 0xC79A971F, 0x35F1141C, 0x26A1E7E8, 0xD4CA64EB,
0x8AD958CF, 0x78B2DBCC, 0x6BE22838, 0x9989AB3B, 0x4D43CFD0, 0xBF284CD3, 0xAC78BF27, 0x5E133C24,
0x105EC76F, 0xE235446C, 0xF165B798, 0x030E349B, 0xD7C45070, 0x25AFD373, 0x36FF2087, 0xC494A384,
0x9A879FA0, 0x68EC1CA3, 0x7BBCEF57, 0x89D76C54, 0x5D1D08BF, 0xAF768BBC, 0xBC267848, 0x4E4DFB4B,
0x20BD8EDE, 0xD2D60DDD, 0xC186FE29, 0x33ED7D2A, 0xE72719C1, 0x154C9AC2, 0x061C6936, 0xF477EA35,
0xAA64D611, 0x580F5512, 0x4B5FA6E6, 0xB93425E5, 0x6DFE410E, 0x9F95C20D, 0x8CC531F9, 0x7EAEB2FA,
0x30E349B1, 0xC288CAB2, 0xD1D83946, 0x23B3BA45, 0xF779DEAE, 0x05125DAD, 0x1642AE59, 0xE4292D5A,
0xBA3A117E, 0x4851927D, 0x5B016189, 0xA96AE28A, 0x7DA08661, 0x8FCB0562, 0x9C9BF696, 0x6EF07595,
0x417B1DBC, 0xB3109EBF, 0xA0406D4B, 0x522BEE48, 0x86E18AA3, 0x748A09A0, 0x67DAFA54, 0x95B17957,
0xCBA24573, 0x39C9C670, 0x2A993584, 0xD8F2B687, 0x0C38D26C, 0xFE53516F, 0xED03A29B, 0x1F682198,
0x5125DAD3, 0xA34E59D0, 0xB01EAA24, 0x42752927, 0x96BF4DCC, 0x64D4CECF, 0x77843D3B, 0x85EFBE38,
0xDBFC821C, 0x2997011F, 0x3AC7F2EB, 0xC8AC71E8, 0x1C661503, 0xEE0D9600, 0xFD5D65F4, 0x0F36E6F7,
0x61C69362, 0x93AD1061, 0x80FDE395, 0x72966096, 0xA65C047D, 0x5437877E, 0x4767748A, 0xB50CF789,
0xEB1FCBAD, 0x197448AE, 0x0A24BB5A, 0xF84F3859, 0x2C855CB2, 0xDEEEDFB1, 0xCDBE2C45, 0x3FD5AF46,
0x7198540D, 0x83F3D70E, 0x90A324FA, 0x62C8A7F9, 0xB602C312, 0x44694011, 0x5739B3E5, 0xA55230E6,
0xFB410CC2, 0x092A8FC1, 0x1A7A7C35, 0xE811FF36, 0x3CDB9BDD, 0xCEB018DE, 0xDDE0EB2A, 0x2F8B6829,
0x82F63B78, 0x709DB87B, 0x63CD4B8F, 0x91A6C88C, 0x456CAC67, 0xB7072F64, 0xA457DC90, 0x563C5F93,
0x082F63B7, 0xFA44E0B4, 0xE9141340, 0x1B7F9043, 0xCFB5F4A8, 0x3DDE77AB, 0x2E8E845F, 0xDCE5075C,
0x92A8FC17, 0x60C37F14, 0x73938CE0, 0x81F80FE3, 0x55326B08, 0xA759E80B, 0xB4091BFF, 0x466298FC,
0x1871A4D8, 0xEA1A27DB, 0xF94AD42F, 0x0B21572C, 0xDFEB33C7, 0x2D80B0C4, 0x3ED04330, 0xCCBBC033,
0xA24BB5A6, 0x502036A5, 0x4370C551, 0xB11B4652, 0x65D122B9, 0x97BAA1BA, 0x84EA524E, 0x7681D14D,
0x2892ED69, 0xDAF96E6A, 0xC9A99D9E, 0x3BC21E9D, 0xEF087A76, 0x1D63F975, 0x0E330A81, 0xFC588982,
0xB21572C9, 0x407EF1CA, 0x532E023E, 0xA145813D, 0x758FE5D6, 0x87E466D5, 0x94B49521, 0x66DF1622,
0x38CC2A06, 0xCAA7A905, 0xD9F75AF1, 0x2B9CD9F2, 0xFF56BD19, 0x0D3D3E1A, 0x1E6DCDEE, 0xEC064EED,
0xC38D26C4, 0x31E6A5C7, 0x22B65633, 0xD0DDD530, 0x0417B1DB, 0xF67C32D8, 0xE52CC12C, 0x1747422F,
0x49547E0B, 0xBB3FFD08, 0xA86F0EFC, 0x5A048DFF, 0x8ECEE914, 0x7CA56A17, 0x6FF599E3, 0x9D9E1AE0,
0xD3D3E1AB, 0x21B862A8, 0x32E8915C, 0xC083125F, 0x144976B4, 0xE622F5B7, 0xF5720643, 0x07198540,
0x590AB964, 0xAB613A67, 0xB831C993, 0x4A5A4A90, 0x9E902E7B, 0x6CFBAD78, 0x7FAB5E8C, 0x8DC0DD8F,
0xE330A81A, 0x115B2B19, 0x020BD8ED, 0xF0605BEE, 0x24AA3F05, 0xD6C1BC06, 0xC5914FF2, 0x37FACCF1,
0x69E9F0D5, 0x9B8273D6, 0x88D28022, 0x7AB90321, 0xAE7367CA, 0x5C18E4C9, 0x4F48173D, 0xBD23943E,
0xF36E6F75, 0x0105EC76, 0x12551F82, 0xE03E9C81, 0x34F4F86A, 0xC69F7B69, 0xD5CF889D, 0x27A40B9E,
0x79B737BA, 0x8BDCB4B9, 0x988C474D, 0x6AE7C44E, 0xBE2DA0A5, 0x4C4623A6, 0x5F16D052, 0xAD7D5351
};
/*
* end of the CRC lookup table crc_tableil8_o32
*/
/*
* The following CRC lookup table was generated automagically using the
* following model parameters:
*
* Generator Polynomial = ................. 0x1EDC6F41
* Generator Polynomial Length = .......... 32 bits
* Reflected Bits = ....................... TRUE
* Table Generation Offset = .............. 32 bits
* Number of Slices = ..................... 8 slices
* Slice Lengths = ........................ 8 8 8 8 8 8 8 8
* Directory Name = ....................... .\
* File Name = ............................ 8x256_tables.c
*/
static uint32_t crc_tableil8_o40[256] =
{
0x00000000, 0x13A29877, 0x274530EE, 0x34E7A899, 0x4E8A61DC, 0x5D28F9AB, 0x69CF5132, 0x7A6DC945,
0x9D14C3B8, 0x8EB65BCF, 0xBA51F356, 0xA9F36B21, 0xD39EA264, 0xC03C3A13, 0xF4DB928A, 0xE7790AFD,
0x3FC5F181, 0x2C6769F6, 0x1880C16F, 0x0B225918, 0x714F905D, 0x62ED082A, 0x560AA0B3, 0x45A838C4,
0xA2D13239, 0xB173AA4E, 0x859402D7, 0x96369AA0, 0xEC5B53E5, 0xFFF9CB92, 0xCB1E630B, 0xD8BCFB7C,
0x7F8BE302, 0x6C297B75, 0x58CED3EC, 0x4B6C4B9B, 0x310182DE, 0x22A31AA9, 0x1644B230, 0x05E62A47,
0xE29F20BA, 0xF13DB8CD, 0xC5DA1054, 0xD6788823, 0xAC154166, 0xBFB7D911, 0x8B507188, 0x98F2E9FF,
0x404E1283, 0x53EC8AF4, 0x670B226D, 0x74A9BA1A, 0x0EC4735F, 0x1D66EB28, 0x298143B1, 0x3A23DBC6,
0xDD5AD13B, 0xCEF8494C, 0xFA1FE1D5, 0xE9BD79A2, 0x93D0B0E7, 0x80722890, 0xB4958009, 0xA737187E,
0xFF17C604, 0xECB55E73, 0xD852F6EA, 0xCBF06E9D, 0xB19DA7D8, 0xA23F3FAF, 0x96D89736, 0x857A0F41,
0x620305BC, 0x71A19DCB, 0x45463552, 0x56E4AD25, 0x2C896460, 0x3F2BFC17, 0x0BCC548E, 0x186ECCF9,
0xC0D23785, 0xD370AFF2, 0xE797076B, 0xF4359F1C, 0x8E585659, 0x9DFACE2E, 0xA91D66B7, 0xBABFFEC0,
0x5DC6F43D, 0x4E646C4A, 0x7A83C4D3, 0x69215CA4, 0x134C95E1, 0x00EE0D96, 0x3409A50F, 0x27AB3D78,
0x809C2506, 0x933EBD71, 0xA7D915E8, 0xB47B8D9F, 0xCE1644DA, 0xDDB4DCAD, 0xE9537434, 0xFAF1EC43,
0x1D88E6BE, 0x0E2A7EC9, 0x3ACDD650, 0x296F4E27, 0x53028762, 0x40A01F15, 0x7447B78C, 0x67E52FFB,
0xBF59D487, 0xACFB4CF0, 0x981CE469, 0x8BBE7C1E, 0xF1D3B55B, 0xE2712D2C, 0xD69685B5, 0xC5341DC2,
0x224D173F, 0x31EF8F48, 0x050827D1, 0x16AABFA6, 0x6CC776E3, 0x7F65EE94, 0x4B82460D, 0x5820DE7A,
0xFBC3FAF9, 0xE861628E, 0xDC86CA17, 0xCF245260, 0xB5499B25, 0xA6EB0352, 0x920CABCB, 0x81AE33BC,
0x66D73941, 0x7575A136, 0x419209AF, 0x523091D8, 0x285D589D, 0x3BFFC0EA, 0x0F186873, 0x1CBAF004,
0xC4060B78, 0xD7A4930F, 0xE3433B96, 0xF0E1A3E1, 0x8A8C6AA4, 0x992EF2D3, 0xADC95A4A, 0xBE6BC23D,
0x5912C8C0, 0x4AB050B7, 0x7E57F82E, 0x6DF56059, 0x1798A91C, 0x043A316B, 0x30DD99F2, 0x237F0185,
0x844819FB, 0x97EA818C, 0xA30D2915, 0xB0AFB162, 0xCAC27827, 0xD960E050, 0xED8748C9, 0xFE25D0BE,
0x195CDA43, 0x0AFE4234, 0x3E19EAAD, 0x2DBB72DA, 0x57D6BB9F, 0x447423E8, 0x70938B71, 0x63311306,
0xBB8DE87A, 0xA82F700D, 0x9CC8D894, 0x8F6A40E3, 0xF50789A6, 0xE6A511D1, 0xD242B948, 0xC1E0213F,
0x26992BC2, 0x353BB3B5, 0x01DC1B2C, 0x127E835B, 0x68134A1E, 0x7BB1D269, 0x4F567AF0, 0x5CF4E287,
0x04D43CFD, 0x1776A48A, 0x23910C13, 0x30339464, 0x4A5E5D21, 0x59FCC556, 0x6D1B6DCF, 0x7EB9F5B8,
0x99C0FF45, 0x8A626732, 0xBE85CFAB, 0xAD2757DC, 0xD74A9E99, 0xC4E806EE, 0xF00FAE77, 0xE3AD3600,
0x3B11CD7C, 0x28B3550B, 0x1C54FD92, 0x0FF665E5, 0x759BACA0, 0x663934D7, 0x52DE9C4E, 0x417C0439,
0xA6050EC4, 0xB5A796B3, 0x81403E2A, 0x92E2A65D, 0xE88F6F18, 0xFB2DF76F, 0xCFCA5FF6, 0xDC68C781,
0x7B5FDFFF, 0x68FD4788, 0x5C1AEF11, 0x4FB87766, 0x35D5BE23, 0x26772654, 0x12908ECD, 0x013216BA,
0xE64B1C47, 0xF5E98430, 0xC10E2CA9, 0xD2ACB4DE, 0xA8C17D9B, 0xBB63E5EC, 0x8F844D75, 0x9C26D502,
0x449A2E7E, 0x5738B609, 0x63DF1E90, 0x707D86E7, 0x0A104FA2, 0x19B2D7D5, 0x2D557F4C, 0x3EF7E73B,
0xD98EEDC6, 0xCA2C75B1, 0xFECBDD28, 0xED69455F, 0x97048C1A, 0x84A6146D, 0xB041BCF4, 0xA3E32483
};
/*
* end of the CRC lookup table crc_tableil8_o40
*/
/*
* The following CRC lookup table was generated automagically using the
* following model parameters:
*
* Generator Polynomial = ................. 0x1EDC6F41
* Generator Polynomial Length = .......... 32 bits
* Reflected Bits = ....................... TRUE
* Table Generation Offset = .............. 32 bits
* Number of Slices = ..................... 8 slices
* Slice Lengths = ........................ 8 8 8 8 8 8 8 8
* Directory Name = ....................... .\
* File Name = ............................ 8x256_tables.c
*/
static uint32_t crc_tableil8_o48[256] =
{
0x00000000, 0xA541927E, 0x4F6F520D, 0xEA2EC073, 0x9EDEA41A, 0x3B9F3664, 0xD1B1F617, 0x74F06469,
0x38513EC5, 0x9D10ACBB, 0x773E6CC8, 0xD27FFEB6, 0xA68F9ADF, 0x03CE08A1, 0xE9E0C8D2, 0x4CA15AAC,
0x70A27D8A, 0xD5E3EFF4, 0x3FCD2F87, 0x9A8CBDF9, 0xEE7CD990, 0x4B3D4BEE, 0xA1138B9D, 0x045219E3,
0x48F3434F, 0xEDB2D131, 0x079C1142, 0xA2DD833C, 0xD62DE755, 0x736C752B, 0x9942B558, 0x3C032726,
0xE144FB14, 0x4405696A, 0xAE2BA919, 0x0B6A3B67, 0x7F9A5F0E, 0xDADBCD70, 0x30F50D03, 0x95B49F7D,
0xD915C5D1, 0x7C5457AF, 0x967A97DC, 0x333B05A2, 0x47CB61CB, 0xE28AF3B5, 0x08A433C6, 0xADE5A1B8,
0x91E6869E, 0x34A714E0, 0xDE89D493, 0x7BC846ED, 0x0F382284, 0xAA79B0FA, 0x40577089, 0xE516E2F7,
0xA9B7B85B, 0x0CF62A25, 0xE6D8EA56, 0x43997828, 0x37691C41, 0x92288E3F, 0x78064E4C, 0xDD47DC32,
0xC76580D9, 0x622412A7, 0x880AD2D4, 0x2D4B40AA, 0x59BB24C3, 0xFCFAB6BD, 0x16D476CE, 0xB395E4B0,
0xFF34BE1C, 0x5A752C62, 0xB05BEC11, 0x151A7E6F, 0x61EA1A06, 0xC4AB8878, 0x2E85480B, 0x8BC4DA75,
0xB7C7FD53, 0x12866F2D, 0xF8A8AF5E, 0x5DE93D20, 0x29195949, 0x8C58CB37, 0x66760B44, 0xC337993A,
0x8F96C396, 0x2AD751E8, 0xC0F9919B, 0x65B803E5, 0x1148678C, 0xB409F5F2, 0x5E273581, 0xFB66A7FF,
0x26217BCD, 0x8360E9B3, 0x694E29C0, 0xCC0FBBBE, 0xB8FFDFD7, 0x1DBE4DA9, 0xF7908DDA, 0x52D11FA4,
0x1E704508, 0xBB31D776, 0x511F1705, 0xF45E857B, 0x80AEE112, 0x25EF736C, 0xCFC1B31F, 0x6A802161,
0x56830647, 0xF3C29439, 0x19EC544A, 0xBCADC634, 0xC85DA25D, 0x6D1C3023, 0x8732F050, 0x2273622E,
0x6ED23882, 0xCB93AAFC, 0x21BD6A8F, 0x84FCF8F1, 0xF00C9C98, 0x554D0EE6, 0xBF63CE95, 0x1A225CEB,
0x8B277743, 0x2E66E53D, 0xC448254E, 0x6109B730, 0x15F9D359, 0xB0B84127, 0x5A968154, 0xFFD7132A,
0xB3764986, 0x1637DBF8, 0xFC191B8B, 0x595889F5, 0x2DA8ED9C, 0x88E97FE2, 0x62C7BF91, 0xC7862DEF,
0xFB850AC9, 0x5EC498B7, 0xB4EA58C4, 0x11ABCABA, 0x655BAED3, 0xC01A3CAD, 0x2A34FCDE, 0x8F756EA0,
0xC3D4340C, 0x6695A672, 0x8CBB6601, 0x29FAF47F, 0x5D0A9016, 0xF84B0268, 0x1265C21B, 0xB7245065,
0x6A638C57, 0xCF221E29, 0x250CDE5A, 0x804D4C24, 0xF4BD284D, 0x51FCBA33, 0xBBD27A40, 0x1E93E83E,
0x5232B292, 0xF77320EC, 0x1D5DE09F, 0xB81C72E1, 0xCCEC1688, 0x69AD84F6, 0x83834485, 0x26C2D6FB,
0x1AC1F1DD, 0xBF8063A3, 0x55AEA3D0, 0xF0EF31AE, 0x841F55C7, 0x215EC7B9, 0xCB7007CA, 0x6E3195B4,
0x2290CF18, 0x87D15D66, 0x6DFF9D15, 0xC8BE0F6B, 0xBC4E6B02, 0x190FF97C, 0xF321390F, 0x5660AB71,
0x4C42F79A, 0xE90365E4, 0x032DA597, 0xA66C37E9, 0xD29C5380, 0x77DDC1FE, 0x9DF3018D, 0x38B293F3,
0x7413C95F, 0xD1525B21, 0x3B7C9B52, 0x9E3D092C, 0xEACD6D45, 0x4F8CFF3B, 0xA5A23F48, 0x00E3AD36,
0x3CE08A10, 0x99A1186E, 0x738FD81D, 0xD6CE4A63, 0xA23E2E0A, 0x077FBC74, 0xED517C07, 0x4810EE79,
0x04B1B4D5, 0xA1F026AB, 0x4BDEE6D8, 0xEE9F74A6, 0x9A6F10CF, 0x3F2E82B1, 0xD50042C2, 0x7041D0BC,
0xAD060C8E, 0x08479EF0, 0xE2695E83, 0x4728CCFD, 0x33D8A894, 0x96993AEA, 0x7CB7FA99, 0xD9F668E7,
0x9557324B, 0x3016A035, 0xDA386046, 0x7F79F238, 0x0B899651, 0xAEC8042F, 0x44E6C45C, 0xE1A75622,
0xDDA47104, 0x78E5E37A, 0x92CB2309, 0x378AB177, 0x437AD51E, 0xE63B4760, 0x0C158713, 0xA954156D,
0xE5F54FC1, 0x40B4DDBF, 0xAA9A1DCC, 0x0FDB8FB2, 0x7B2BEBDB, 0xDE6A79A5, 0x3444B9D6, 0x91052BA8
};
/*
* end of the CRC lookup table crc_tableil8_o48
*/
/*
* The following CRC lookup table was generated automagically using the
* following model parameters:
*
* Generator Polynomial = ................. 0x1EDC6F41
* Generator Polynomial Length = .......... 32 bits
* Reflected Bits = ....................... TRUE
* Table Generation Offset = .............. 32 bits
* Number of Slices = ..................... 8 slices
* Slice Lengths = ........................ 8 8 8 8 8 8 8 8
* Directory Name = ....................... .\
* File Name = ............................ 8x256_tables.c
*/
static uint32_t crc_tableil8_o56[256] =
{
0x00000000, 0xDD45AAB8, 0xBF672381, 0x62228939, 0x7B2231F3, 0xA6679B4B, 0xC4451272, 0x1900B8CA,
0xF64463E6, 0x2B01C95E, 0x49234067, 0x9466EADF, 0x8D665215, 0x5023F8AD, 0x32017194, 0xEF44DB2C,
0xE964B13D, 0x34211B85, 0x560392BC, 0x8B463804, 0x924680CE, 0x4F032A76, 0x2D21A34F, 0xF06409F7,
0x1F20D2DB, 0xC2657863, 0xA047F15A, 0x7D025BE2, 0x6402E328, 0xB9474990, 0xDB65C0A9, 0x06206A11,
0xD725148B, 0x0A60BE33, 0x6842370A, 0xB5079DB2, 0xAC072578, 0x71428FC0, 0x136006F9, 0xCE25AC41,
0x2161776D, 0xFC24DDD5, 0x9E0654EC, 0x4343FE54, 0x5A43469E, 0x8706EC26, 0xE524651F, 0x3861CFA7,
0x3E41A5B6, 0xE3040F0E, 0x81268637, 0x5C632C8F, 0x45639445, 0x98263EFD, 0xFA04B7C4, 0x27411D7C,
0xC805C650, 0x15406CE8, 0x7762E5D1, 0xAA274F69, 0xB327F7A3, 0x6E625D1B, 0x0C40D422, 0xD1057E9A,
0xABA65FE7, 0x76E3F55F, 0x14C17C66, 0xC984D6DE, 0xD0846E14, 0x0DC1C4AC, 0x6FE34D95, 0xB2A6E72D,
0x5DE23C01, 0x80A796B9, 0xE2851F80, 0x3FC0B538, 0x26C00DF2, 0xFB85A74A, 0x99A72E73, 0x44E284CB,
0x42C2EEDA, 0x9F874462, 0xFDA5CD5B, 0x20E067E3, 0x39E0DF29, 0xE4A57591, 0x8687FCA8, 0x5BC25610,
0xB4868D3C, 0x69C32784, 0x0BE1AEBD, 0xD6A40405, 0xCFA4BCCF, 0x12E11677, 0x70C39F4E, 0xAD8635F6,
0x7C834B6C, 0xA1C6E1D4, 0xC3E468ED, 0x1EA1C255, 0x07A17A9F, 0xDAE4D027, 0xB8C6591E, 0x6583F3A6,
0x8AC7288A, 0x57828232, 0x35A00B0B, 0xE8E5A1B3, 0xF1E51979, 0x2CA0B3C1, 0x4E823AF8, 0x93C79040,
0x95E7FA51, 0x48A250E9, 0x2A80D9D0, 0xF7C57368, 0xEEC5CBA2, 0x3380611A, 0x51A2E823, 0x8CE7429B,
0x63A399B7, 0xBEE6330F, 0xDCC4BA36, 0x0181108E, 0x1881A844, 0xC5C402FC, 0xA7E68BC5, 0x7AA3217D,
0x52A0C93F, 0x8FE56387, 0xEDC7EABE, 0x30824006, 0x2982F8CC, 0xF4C75274, 0x96E5DB4D, 0x4BA071F5,
0xA4E4AAD9, 0x79A10061, 0x1B838958, 0xC6C623E0, 0xDFC69B2A, 0x02833192, 0x60A1B8AB, 0xBDE41213,
0xBBC47802, 0x6681D2BA, 0x04A35B83, 0xD9E6F13B, 0xC0E649F1, 0x1DA3E349, 0x7F816A70, 0xA2C4C0C8,
0x4D801BE4, 0x90C5B15C, 0xF2E73865, 0x2FA292DD, 0x36A22A17, 0xEBE780AF, 0x89C50996, 0x5480A32E,
0x8585DDB4, 0x58C0770C, 0x3AE2FE35, 0xE7A7548D, 0xFEA7EC47, 0x23E246FF, 0x41C0CFC6, 0x9C85657E,
0x73C1BE52, 0xAE8414EA, 0xCCA69DD3, 0x11E3376B, 0x08E38FA1, 0xD5A62519, 0xB784AC20, 0x6AC10698,
0x6CE16C89, 0xB1A4C631, 0xD3864F08, 0x0EC3E5B0, 0x17C35D7A, 0xCA86F7C2, 0xA8A47EFB, 0x75E1D443,
0x9AA50F6F, 0x47E0A5D7, 0x25C22CEE, 0xF8878656, 0xE1873E9C, 0x3CC29424, 0x5EE01D1D, 0x83A5B7A5,
0xF90696D8, 0x24433C60, 0x4661B559, 0x9B241FE1, 0x8224A72B, 0x5F610D93, 0x3D4384AA, 0xE0062E12,
0x0F42F53E, 0xD2075F86, 0xB025D6BF, 0x6D607C07, 0x7460C4CD, 0xA9256E75, 0xCB07E74C, 0x16424DF4,
0x106227E5, 0xCD278D5D, 0xAF050464, 0x7240AEDC, 0x6B401616, 0xB605BCAE, 0xD4273597, 0x09629F2F,
0xE6264403, 0x3B63EEBB, 0x59416782, 0x8404CD3A, 0x9D0475F0, 0x4041DF48, 0x22635671, 0xFF26FCC9,
0x2E238253, 0xF36628EB, 0x9144A1D2, 0x4C010B6A, 0x5501B3A0, 0x88441918, 0xEA669021, 0x37233A99,
0xD867E1B5, 0x05224B0D, 0x6700C234, 0xBA45688C, 0xA345D046, 0x7E007AFE, 0x1C22F3C7, 0xC167597F,
0xC747336E, 0x1A0299D6, 0x782010EF, 0xA565BA57, 0xBC65029D, 0x6120A825, 0x0302211C, 0xDE478BA4,
0x31035088, 0xEC46FA30, 0x8E647309, 0x5321D9B1, 0x4A21617B, 0x9764CBC3, 0xF54642FA, 0x2803E842
};
/*
* end of the CRC lookup table crc_tableil8_o56
*/
/*
* The following CRC lookup table was generated automagically using the
* following model parameters:
*
* Generator Polynomial = ................. 0x1EDC6F41
* Generator Polynomial Length = .......... 32 bits
* Reflected Bits = ....................... TRUE
* Table Generation Offset = .............. 32 bits
* Number of Slices = ..................... 8 slices
* Slice Lengths = ........................ 8 8 8 8 8 8 8 8
* Directory Name = ....................... .\
* File Name = ............................ 8x256_tables.c
*/
static uint32_t crc_tableil8_o64[256] =
{
0x00000000, 0x38116FAC, 0x7022DF58, 0x4833B0F4, 0xE045BEB0, 0xD854D11C, 0x906761E8, 0xA8760E44,
0xC5670B91, 0xFD76643D, 0xB545D4C9, 0x8D54BB65, 0x2522B521, 0x1D33DA8D, 0x55006A79, 0x6D1105D5,
0x8F2261D3, 0xB7330E7F, 0xFF00BE8B, 0xC711D127, 0x6F67DF63, 0x5776B0CF, 0x1F45003B, 0x27546F97,
0x4A456A42, 0x725405EE, 0x3A67B51A, 0x0276DAB6, 0xAA00D4F2, 0x9211BB5E, 0xDA220BAA, 0xE2336406,
0x1BA8B557, 0x23B9DAFB, 0x6B8A6A0F, 0x539B05A3, 0xFBED0BE7, 0xC3FC644B, 0x8BCFD4BF, 0xB3DEBB13,
0xDECFBEC6, 0xE6DED16A, 0xAEED619E, 0x96FC0E32, 0x3E8A0076, 0x069B6FDA, 0x4EA8DF2E, 0x76B9B082,
0x948AD484, 0xAC9BBB28, 0xE4A80BDC, 0xDCB96470, 0x74CF6A34, 0x4CDE0598, 0x04EDB56C, 0x3CFCDAC0,
0x51EDDF15, 0x69FCB0B9, 0x21CF004D, 0x19DE6FE1, 0xB1A861A5, 0x89B90E09, 0xC18ABEFD, 0xF99BD151,
0x37516AAE, 0x0F400502, 0x4773B5F6, 0x7F62DA5A, 0xD714D41E, 0xEF05BBB2, 0xA7360B46, 0x9F2764EA,
0xF236613F, 0xCA270E93, 0x8214BE67, 0xBA05D1CB, 0x1273DF8F, 0x2A62B023, 0x625100D7, 0x5A406F7B,
0xB8730B7D, 0x806264D1, 0xC851D425, 0xF040BB89, 0x5836B5CD, 0x6027DA61, 0x28146A95, 0x10050539,
0x7D1400EC, 0x45056F40, 0x0D36DFB4, 0x3527B018, 0x9D51BE5C, 0xA540D1F0, 0xED736104, 0xD5620EA8,
0x2CF9DFF9, 0x14E8B055, 0x5CDB00A1, 0x64CA6F0D, 0xCCBC6149, 0xF4AD0EE5, 0xBC9EBE11, 0x848FD1BD,
0xE99ED468, 0xD18FBBC4, 0x99BC0B30, 0xA1AD649C, 0x09DB6AD8, 0x31CA0574, 0x79F9B580, 0x41E8DA2C,
0xA3DBBE2A, 0x9BCAD186, 0xD3F96172, 0xEBE80EDE, 0x439E009A, 0x7B8F6F36, 0x33BCDFC2, 0x0BADB06E,
0x66BCB5BB, 0x5EADDA17, 0x169E6AE3, 0x2E8F054F, 0x86F90B0B, 0xBEE864A7, 0xF6DBD453, 0xCECABBFF,
0x6EA2D55C, 0x56B3BAF0, 0x1E800A04, 0x269165A8, 0x8EE76BEC, 0xB6F60440, 0xFEC5B4B4, 0xC6D4DB18,
0xABC5DECD, 0x93D4B161, 0xDBE70195, 0xE3F66E39, 0x4B80607D, 0x73910FD1, 0x3BA2BF25, 0x03B3D089,
0xE180B48F, 0xD991DB23, 0x91A26BD7, 0xA9B3047B, 0x01C50A3F, 0x39D46593, 0x71E7D567, 0x49F6BACB,
0x24E7BF1E, 0x1CF6D0B2, 0x54C56046, 0x6CD40FEA, 0xC4A201AE, 0xFCB36E02, 0xB480DEF6, 0x8C91B15A,
0x750A600B, 0x4D1B0FA7, 0x0528BF53, 0x3D39D0FF, 0x954FDEBB, 0xAD5EB117, 0xE56D01E3, 0xDD7C6E4F,
0xB06D6B9A, 0x887C0436, 0xC04FB4C2, 0xF85EDB6E, 0x5028D52A, 0x6839BA86, 0x200A0A72, 0x181B65DE,
0xFA2801D8, 0xC2396E74, 0x8A0ADE80, 0xB21BB12C, 0x1A6DBF68, 0x227CD0C4, 0x6A4F6030, 0x525E0F9C,
0x3F4F0A49, 0x075E65E5, 0x4F6DD511, 0x777CBABD, 0xDF0AB4F9, 0xE71BDB55, 0xAF286BA1, 0x9739040D,
0x59F3BFF2, 0x61E2D05E, 0x29D160AA, 0x11C00F06, 0xB9B60142, 0x81A76EEE, 0xC994DE1A, 0xF185B1B6,
0x9C94B463, 0xA485DBCF, 0xECB66B3B, 0xD4A70497, 0x7CD10AD3, 0x44C0657F, 0x0CF3D58B, 0x34E2BA27,
0xD6D1DE21, 0xEEC0B18D, 0xA6F30179, 0x9EE26ED5, 0x36946091, 0x0E850F3D, 0x46B6BFC9, 0x7EA7D065,
0x13B6D5B0, 0x2BA7BA1C, 0x63940AE8, 0x5B856544, 0xF3F36B00, 0xCBE204AC, 0x83D1B458, 0xBBC0DBF4,
0x425B0AA5, 0x7A4A6509, 0x3279D5FD, 0x0A68BA51, 0xA21EB415, 0x9A0FDBB9, 0xD23C6B4D, 0xEA2D04E1,
0x873C0134, 0xBF2D6E98, 0xF71EDE6C, 0xCF0FB1C0, 0x6779BF84, 0x5F68D028, 0x175B60DC, 0x2F4A0F70,
0xCD796B76, 0xF56804DA, 0xBD5BB42E, 0x854ADB82, 0x2D3CD5C6, 0x152DBA6A, 0x5D1E0A9E, 0x650F6532,
0x081E60E7, 0x300F0F4B, 0x783CBFBF, 0x402DD013, 0xE85BDE57, 0xD04AB1FB, 0x9879010F, 0xA0686EA3
};
/*
* end of the CRC lookup table crc_tableil8_o64
*/
/*
* The following CRC lookup table was generated automagically using the
* following model parameters:
*
* Generator Polynomial = ................. 0x1EDC6F41
* Generator Polynomial Length = .......... 32 bits
* Reflected Bits = ....................... TRUE
* Table Generation Offset = .............. 32 bits
* Number of Slices = ..................... 8 slices
* Slice Lengths = ........................ 8 8 8 8 8 8 8 8
* Directory Name = ....................... .\
* File Name = ............................ 8x256_tables.c
*/
static uint32_t crc_tableil8_o72[256] =
{
0x00000000, 0xEF306B19, 0xDB8CA0C3, 0x34BCCBDA, 0xB2F53777, 0x5DC55C6E, 0x697997B4, 0x8649FCAD,
0x6006181F, 0x8F367306, 0xBB8AB8DC, 0x54BAD3C5, 0xD2F32F68, 0x3DC34471, 0x097F8FAB, 0xE64FE4B2,
0xC00C303E, 0x2F3C5B27, 0x1B8090FD, 0xF4B0FBE4, 0x72F90749, 0x9DC96C50, 0xA975A78A, 0x4645CC93,
0xA00A2821, 0x4F3A4338, 0x7B8688E2, 0x94B6E3FB, 0x12FF1F56, 0xFDCF744F, 0xC973BF95, 0x2643D48C,
0x85F4168D, 0x6AC47D94, 0x5E78B64E, 0xB148DD57, 0x370121FA, 0xD8314AE3, 0xEC8D8139, 0x03BDEA20,
0xE5F20E92, 0x0AC2658B, 0x3E7EAE51, 0xD14EC548, 0x570739E5, 0xB83752FC, 0x8C8B9926, 0x63BBF23F,
0x45F826B3, 0xAAC84DAA, 0x9E748670, 0x7144ED69, 0xF70D11C4, 0x183D7ADD, 0x2C81B107, 0xC3B1DA1E,
0x25FE3EAC, 0xCACE55B5, 0xFE729E6F, 0x1142F576, 0x970B09DB, 0x783B62C2, 0x4C87A918, 0xA3B7C201,
0x0E045BEB, 0xE13430F2, 0xD588FB28, 0x3AB89031, 0xBCF16C9C, 0x53C10785, 0x677DCC5F, 0x884DA746,
0x6E0243F4, 0x813228ED, 0xB58EE337, 0x5ABE882E, 0xDCF77483, 0x33C71F9A, 0x077BD440, 0xE84BBF59,
0xCE086BD5, 0x213800CC, 0x1584CB16, 0xFAB4A00F, 0x7CFD5CA2, 0x93CD37BB, 0xA771FC61, 0x48419778,
0xAE0E73CA, 0x413E18D3, 0x7582D309, 0x9AB2B810, 0x1CFB44BD, 0xF3CB2FA4, 0xC777E47E, 0x28478F67,
0x8BF04D66, 0x64C0267F, 0x507CEDA5, 0xBF4C86BC, 0x39057A11, 0xD6351108, 0xE289DAD2, 0x0DB9B1CB,
0xEBF65579, 0x04C63E60, 0x307AF5BA, 0xDF4A9EA3, 0x5903620E, 0xB6330917, 0x828FC2CD, 0x6DBFA9D4,
0x4BFC7D58, 0xA4CC1641, 0x9070DD9B, 0x7F40B682, 0xF9094A2F, 0x16392136, 0x2285EAEC, 0xCDB581F5,
0x2BFA6547, 0xC4CA0E5E, 0xF076C584, 0x1F46AE9D, 0x990F5230, 0x763F3929, 0x4283F2F3, 0xADB399EA,
0x1C08B7D6, 0xF338DCCF, 0xC7841715, 0x28B47C0C, 0xAEFD80A1, 0x41CDEBB8, 0x75712062, 0x9A414B7B,
0x7C0EAFC9, 0x933EC4D0, 0xA7820F0A, 0x48B26413, 0xCEFB98BE, 0x21CBF3A7, 0x1577387D, 0xFA475364,
0xDC0487E8, 0x3334ECF1, 0x0788272B, 0xE8B84C32, 0x6EF1B09F, 0x81C1DB86, 0xB57D105C, 0x5A4D7B45,
0xBC029FF7, 0x5332F4EE, 0x678E3F34, 0x88BE542D, 0x0EF7A880, 0xE1C7C399, 0xD57B0843, 0x3A4B635A,
0x99FCA15B, 0x76CCCA42, 0x42700198, 0xAD406A81, 0x2B09962C, 0xC439FD35, 0xF08536EF, 0x1FB55DF6,
0xF9FAB944, 0x16CAD25D, 0x22761987, 0xCD46729E, 0x4B0F8E33, 0xA43FE52A, 0x90832EF0, 0x7FB345E9,
0x59F09165, 0xB6C0FA7C, 0x827C31A6, 0x6D4C5ABF, 0xEB05A612, 0x0435CD0B, 0x308906D1, 0xDFB96DC8,
0x39F6897A, 0xD6C6E263, 0xE27A29B9, 0x0D4A42A0, 0x8B03BE0D, 0x6433D514, 0x508F1ECE, 0xBFBF75D7,
0x120CEC3D, 0xFD3C8724, 0xC9804CFE, 0x26B027E7, 0xA0F9DB4A, 0x4FC9B053, 0x7B757B89, 0x94451090,
0x720AF422, 0x9D3A9F3B, 0xA98654E1, 0x46B63FF8, 0xC0FFC355, 0x2FCFA84C, 0x1B736396, 0xF443088F,
0xD200DC03, 0x3D30B71A, 0x098C7CC0, 0xE6BC17D9, 0x60F5EB74, 0x8FC5806D, 0xBB794BB7, 0x544920AE,
0xB206C41C, 0x5D36AF05, 0x698A64DF, 0x86BA0FC6, 0x00F3F36B, 0xEFC39872, 0xDB7F53A8, 0x344F38B1,
0x97F8FAB0, 0x78C891A9, 0x4C745A73, 0xA344316A, 0x250DCDC7, 0xCA3DA6DE, 0xFE816D04, 0x11B1061D,
0xF7FEE2AF, 0x18CE89B6, 0x2C72426C, 0xC3422975, 0x450BD5D8, 0xAA3BBEC1, 0x9E87751B, 0x71B71E02,
0x57F4CA8E, 0xB8C4A197, 0x8C786A4D, 0x63480154, 0xE501FDF9, 0x0A3196E0, 0x3E8D5D3A, 0xD1BD3623,
0x37F2D291, 0xD8C2B988, 0xEC7E7252, 0x034E194B, 0x8507E5E6, 0x6A378EFF, 0x5E8B4525, 0xB1BB2E3C
};
/*
* end of the CRC lookup table crc_tableil8_o72
*/
/*
* The following CRC lookup table was generated automagically using the
* following model parameters:
*
* Generator Polynomial = ................. 0x1EDC6F41
* Generator Polynomial Length = .......... 32 bits
* Reflected Bits = ....................... TRUE
* Table Generation Offset = .............. 32 bits
* Number of Slices = ..................... 8 slices
* Slice Lengths = ........................ 8 8 8 8 8 8 8 8
* Directory Name = ....................... .\
* File Name = ............................ 8x256_tables.c
*/
static uint32_t crc_tableil8_o80[256] =
{
0x00000000, 0x68032CC8, 0xD0065990, 0xB8057558, 0xA5E0C5D1, 0xCDE3E919, 0x75E69C41, 0x1DE5B089,
0x4E2DFD53, 0x262ED19B, 0x9E2BA4C3, 0xF628880B, 0xEBCD3882, 0x83CE144A, 0x3BCB6112, 0x53C84DDA,
0x9C5BFAA6, 0xF458D66E, 0x4C5DA336, 0x245E8FFE, 0x39BB3F77, 0x51B813BF, 0xE9BD66E7, 0x81BE4A2F,
0xD27607F5, 0xBA752B3D, 0x02705E65, 0x6A7372AD, 0x7796C224, 0x1F95EEEC, 0xA7909BB4, 0xCF93B77C,
0x3D5B83BD, 0x5558AF75, 0xED5DDA2D, 0x855EF6E5, 0x98BB466C, 0xF0B86AA4, 0x48BD1FFC, 0x20BE3334,
0x73767EEE, 0x1B755226, 0xA370277E, 0xCB730BB6, 0xD696BB3F, 0xBE9597F7, 0x0690E2AF, 0x6E93CE67,
0xA100791B, 0xC90355D3, 0x7106208B, 0x19050C43, 0x04E0BCCA, 0x6CE39002, 0xD4E6E55A, 0xBCE5C992,
0xEF2D8448, 0x872EA880, 0x3F2BDDD8, 0x5728F110, 0x4ACD4199, 0x22CE6D51, 0x9ACB1809, 0xF2C834C1,
0x7AB7077A, 0x12B42BB2, 0xAAB15EEA, 0xC2B27222, 0xDF57C2AB, 0xB754EE63, 0x0F519B3B, 0x6752B7F3,
0x349AFA29, 0x5C99D6E1, 0xE49CA3B9, 0x8C9F8F71, 0x917A3FF8, 0xF9791330, 0x417C6668, 0x297F4AA0,
0xE6ECFDDC, 0x8EEFD114, 0x36EAA44C, 0x5EE98884, 0x430C380D, 0x2B0F14C5, 0x930A619D, 0xFB094D55,
0xA8C1008F, 0xC0C22C47, 0x78C7591F, 0x10C475D7, 0x0D21C55E, 0x6522E996, 0xDD279CCE, 0xB524B006,
0x47EC84C7, 0x2FEFA80F, 0x97EADD57, 0xFFE9F19F, 0xE20C4116, 0x8A0F6DDE, 0x320A1886, 0x5A09344E,
0x09C17994, 0x61C2555C, 0xD9C72004, 0xB1C40CCC, 0xAC21BC45, 0xC422908D, 0x7C27E5D5, 0x1424C91D,
0xDBB77E61, 0xB3B452A9, 0x0BB127F1, 0x63B20B39, 0x7E57BBB0, 0x16549778, 0xAE51E220, 0xC652CEE8,
0x959A8332, 0xFD99AFFA, 0x459CDAA2, 0x2D9FF66A, 0x307A46E3, 0x58796A2B, 0xE07C1F73, 0x887F33BB,
0xF56E0EF4, 0x9D6D223C, 0x25685764, 0x4D6B7BAC, 0x508ECB25, 0x388DE7ED, 0x808892B5, 0xE88BBE7D,
0xBB43F3A7, 0xD340DF6F, 0x6B45AA37, 0x034686FF, 0x1EA33676, 0x76A01ABE, 0xCEA56FE6, 0xA6A6432E,
0x6935F452, 0x0136D89A, 0xB933ADC2, 0xD130810A, 0xCCD53183, 0xA4D61D4B, 0x1CD36813, 0x74D044DB,
0x27180901, 0x4F1B25C9, 0xF71E5091, 0x9F1D7C59, 0x82F8CCD0, 0xEAFBE018, 0x52FE9540, 0x3AFDB988,
0xC8358D49, 0xA036A181, 0x1833D4D9, 0x7030F811, 0x6DD54898, 0x05D66450, 0xBDD31108, 0xD5D03DC0,
0x8618701A, 0xEE1B5CD2, 0x561E298A, 0x3E1D0542, 0x23F8B5CB, 0x4BFB9903, 0xF3FEEC5B, 0x9BFDC093,
0x546E77EF, 0x3C6D5B27, 0x84682E7F, 0xEC6B02B7, 0xF18EB23E, 0x998D9EF6, 0x2188EBAE, 0x498BC766,
0x1A438ABC, 0x7240A674, 0xCA45D32C, 0xA246FFE4, 0xBFA34F6D, 0xD7A063A5, 0x6FA516FD, 0x07A63A35,
0x8FD9098E, 0xE7DA2546, 0x5FDF501E, 0x37DC7CD6, 0x2A39CC5F, 0x423AE097, 0xFA3F95CF, 0x923CB907,
0xC1F4F4DD, 0xA9F7D815, 0x11F2AD4D, 0x79F18185, 0x6414310C, 0x0C171DC4, 0xB412689C, 0xDC114454,
0x1382F328, 0x7B81DFE0, 0xC384AAB8, 0xAB878670, 0xB66236F9, 0xDE611A31, 0x66646F69, 0x0E6743A1,
0x5DAF0E7B, 0x35AC22B3, 0x8DA957EB, 0xE5AA7B23, 0xF84FCBAA, 0x904CE762, 0x2849923A, 0x404ABEF2,
0xB2828A33, 0xDA81A6FB, 0x6284D3A3, 0x0A87FF6B, 0x17624FE2, 0x7F61632A, 0xC7641672, 0xAF673ABA,
0xFCAF7760, 0x94AC5BA8, 0x2CA92EF0, 0x44AA0238, 0x594FB2B1, 0x314C9E79, 0x8949EB21, 0xE14AC7E9,
0x2ED97095, 0x46DA5C5D, 0xFEDF2905, 0x96DC05CD, 0x8B39B544, 0xE33A998C, 0x5B3FECD4, 0x333CC01C,
0x60F48DC6, 0x08F7A10E, 0xB0F2D456, 0xD8F1F89E, 0xC5144817, 0xAD1764DF, 0x15121187, 0x7D113D4F
};
/*
* end of the CRC lookup table crc_tableil8_o80
*/
/*
* The following CRC lookup table was generated automagically using the
* following model parameters:
*
* Generator Polynomial = ................. 0x1EDC6F41
* Generator Polynomial Length = .......... 32 bits
* Reflected Bits = ....................... TRUE
* Table Generation Offset = .............. 32 bits
* Number of Slices = ..................... 8 slices
* Slice Lengths = ........................ 8 8 8 8 8 8 8 8
* Directory Name = ....................... .\
* File Name = ............................ 8x256_tables.c
*/
static uint32_t crc_tableil8_o88[256] =
{
0x00000000, 0x493C7D27, 0x9278FA4E, 0xDB448769, 0x211D826D, 0x6821FF4A, 0xB3657823, 0xFA590504,
0x423B04DA, 0x0B0779FD, 0xD043FE94, 0x997F83B3, 0x632686B7, 0x2A1AFB90, 0xF15E7CF9, 0xB86201DE,
0x847609B4, 0xCD4A7493, 0x160EF3FA, 0x5F328EDD, 0xA56B8BD9, 0xEC57F6FE, 0x37137197, 0x7E2F0CB0,
0xC64D0D6E, 0x8F717049, 0x5435F720, 0x1D098A07, 0xE7508F03, 0xAE6CF224, 0x7528754D, 0x3C14086A,
0x0D006599, 0x443C18BE, 0x9F789FD7, 0xD644E2F0, 0x2C1DE7F4, 0x65219AD3, 0xBE651DBA, 0xF759609D,
0x4F3B6143, 0x06071C64, 0xDD439B0D, 0x947FE62A, 0x6E26E32E, 0x271A9E09, 0xFC5E1960, 0xB5626447,
0x89766C2D, 0xC04A110A, 0x1B0E9663, 0x5232EB44, 0xA86BEE40, 0xE1579367, 0x3A13140E, 0x732F6929,
0xCB4D68F7, 0x827115D0, 0x593592B9, 0x1009EF9E, 0xEA50EA9A, 0xA36C97BD, 0x782810D4, 0x31146DF3,
0x1A00CB32, 0x533CB615, 0x8878317C, 0xC1444C5B, 0x3B1D495F, 0x72213478, 0xA965B311, 0xE059CE36,
0x583BCFE8, 0x1107B2CF, 0xCA4335A6, 0x837F4881, 0x79264D85, 0x301A30A2, 0xEB5EB7CB, 0xA262CAEC,
0x9E76C286, 0xD74ABFA1, 0x0C0E38C8, 0x453245EF, 0xBF6B40EB, 0xF6573DCC, 0x2D13BAA5, 0x642FC782,
0xDC4DC65C, 0x9571BB7B, 0x4E353C12, 0x07094135, 0xFD504431, 0xB46C3916, 0x6F28BE7F, 0x2614C358,
0x1700AEAB, 0x5E3CD38C, 0x857854E5, 0xCC4429C2, 0x361D2CC6, 0x7F2151E1, 0xA465D688, 0xED59ABAF,
0x553BAA71, 0x1C07D756, 0xC743503F, 0x8E7F2D18, 0x7426281C, 0x3D1A553B, 0xE65ED252, 0xAF62AF75,
0x9376A71F, 0xDA4ADA38, 0x010E5D51, 0x48322076, 0xB26B2572, 0xFB575855, 0x2013DF3C, 0x692FA21B,
0xD14DA3C5, 0x9871DEE2, 0x4335598B, 0x0A0924AC, 0xF05021A8, 0xB96C5C8F, 0x6228DBE6, 0x2B14A6C1,
0x34019664, 0x7D3DEB43, 0xA6796C2A, 0xEF45110D, 0x151C1409, 0x5C20692E, 0x8764EE47, 0xCE589360,
0x763A92BE, 0x3F06EF99, 0xE44268F0, 0xAD7E15D7, 0x572710D3, 0x1E1B6DF4, 0xC55FEA9D, 0x8C6397BA,
0xB0779FD0, 0xF94BE2F7, 0x220F659E, 0x6B3318B9, 0x916A1DBD, 0xD856609A, 0x0312E7F3, 0x4A2E9AD4,
0xF24C9B0A, 0xBB70E62D, 0x60346144, 0x29081C63, 0xD3511967, 0x9A6D6440, 0x4129E329, 0x08159E0E,
0x3901F3FD, 0x703D8EDA, 0xAB7909B3, 0xE2457494, 0x181C7190, 0x51200CB7, 0x8A648BDE, 0xC358F6F9,
0x7B3AF727, 0x32068A00, 0xE9420D69, 0xA07E704E, 0x5A27754A, 0x131B086D, 0xC85F8F04, 0x8163F223,
0xBD77FA49, 0xF44B876E, 0x2F0F0007, 0x66337D20, 0x9C6A7824, 0xD5560503, 0x0E12826A, 0x472EFF4D,
0xFF4CFE93, 0xB67083B4, 0x6D3404DD, 0x240879FA, 0xDE517CFE, 0x976D01D9, 0x4C2986B0, 0x0515FB97,
0x2E015D56, 0x673D2071, 0xBC79A718, 0xF545DA3F, 0x0F1CDF3B, 0x4620A21C, 0x9D642575, 0xD4585852,
0x6C3A598C, 0x250624AB, 0xFE42A3C2, 0xB77EDEE5, 0x4D27DBE1, 0x041BA6C6, 0xDF5F21AF, 0x96635C88,
0xAA7754E2, 0xE34B29C5, 0x380FAEAC, 0x7133D38B, 0x8B6AD68F, 0xC256ABA8, 0x19122CC1, 0x502E51E6,
0xE84C5038, 0xA1702D1F, 0x7A34AA76, 0x3308D751, 0xC951D255, 0x806DAF72, 0x5B29281B, 0x1215553C,
0x230138CF, 0x6A3D45E8, 0xB179C281, 0xF845BFA6, 0x021CBAA2, 0x4B20C785, 0x906440EC, 0xD9583DCB,
0x613A3C15, 0x28064132, 0xF342C65B, 0xBA7EBB7C, 0x4027BE78, 0x091BC35F, 0xD25F4436, 0x9B633911,
0xA777317B, 0xEE4B4C5C, 0x350FCB35, 0x7C33B612, 0x866AB316, 0xCF56CE31, 0x14124958, 0x5D2E347F,
0xE54C35A1, 0xAC704886, 0x7734CFEF, 0x3E08B2C8, 0xC451B7CC, 0x8D6DCAEB, 0x56294D82, 0x1F1530A5
};
/*
* end of the CRC lookup table crc_tableil8_o88
*/
static uint32_t
crc32c_sb8_64_bit(uint32_t crc,
const unsigned char *p_buf,
uint32_t length,
uint32_t init_bytes)
{
uint32_t li;
uint32_t term1, term2;
uint32_t running_length;
uint32_t end_bytes;
const int indian = 1;
running_length = ((length - init_bytes) / 8) * 8;
end_bytes = length - init_bytes - running_length;
for (li = 0; li < init_bytes; li++)
crc = crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^
(crc >> 8);
for (li = 0; li < running_length / 8; li++) {
if (*(const char *)(const void *)&indian) {
/* little endian */
crc ^= *(const uint32_t *)(const void *) p_buf;
p_buf += 4;
} else {
crc ^= *p_buf++;
crc ^= (*p_buf++) << 8;
crc ^= (*p_buf++) << 16;
crc ^= (*p_buf++) << 24;
}
term1 = crc_tableil8_o88[crc & 0x000000FF] ^
crc_tableil8_o80[(crc >> 8) & 0x000000FF];
term2 = crc >> 16;
crc = term1 ^
crc_tableil8_o72[term2 & 0x000000FF] ^
crc_tableil8_o64[(term2 >> 8) & 0x000000FF];
if (*(const char *)(const void *)&indian) {
term1 = crc_tableil8_o56[(*(const uint32_t *)(const void *) p_buf) & 0x000000FF] ^
crc_tableil8_o48[((*(const uint32_t *)(const void *) p_buf) >> 8) & 0x000000FF];
term2 = (*(const uint32_t *)(const void *) p_buf) >> 16;
crc = crc ^
term1 ^
crc_tableil8_o40[term2 & 0x000000FF] ^
crc_tableil8_o32[(term2 >> 8) & 0x000000FF];
p_buf += 4;
} else {
crc ^= crc_tableil8_o56[*p_buf++];
crc ^= crc_tableil8_o48[*p_buf++];
crc ^= crc_tableil8_o40[*p_buf++];
crc ^= crc_tableil8_o32[*p_buf++];
}
}
for (li = 0; li < end_bytes; li++)
crc = crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^
(crc >> 8);
return crc;
}
/**
*
* Routine Description:
*
* warms the tables
*
* Arguments:
*
* none
*
* Return value:
*
* none
*/
static uint32_t
multitable_crc32c(uint32_t crc32c,
const unsigned char *buffer,
unsigned int length)
{
uint32_t to_even_word;
if (length == 0) {
return (crc32c);
}
to_even_word = (uint32_t)(4 - (((uintptr_t) buffer) & 0x3));
return (crc32c_sb8_64_bit(crc32c, buffer, length, to_even_word));
}
static uint32_t crc_c[256] = {
0x00000000, 0xF26B8303, 0xE13B70F7, 0x1350F3F4,
0xC79A971F, 0x35F1141C, 0x26A1E7E8, 0xD4CA64EB,
0x8AD958CF, 0x78B2DBCC, 0x6BE22838, 0x9989AB3B,
0x4D43CFD0, 0xBF284CD3, 0xAC78BF27, 0x5E133C24,
0x105EC76F, 0xE235446C, 0xF165B798, 0x030E349B,
0xD7C45070, 0x25AFD373, 0x36FF2087, 0xC494A384,
0x9A879FA0, 0x68EC1CA3, 0x7BBCEF57, 0x89D76C54,
0x5D1D08BF, 0xAF768BBC, 0xBC267848, 0x4E4DFB4B,
0x20BD8EDE, 0xD2D60DDD, 0xC186FE29, 0x33ED7D2A,
0xE72719C1, 0x154C9AC2, 0x061C6936, 0xF477EA35,
0xAA64D611, 0x580F5512, 0x4B5FA6E6, 0xB93425E5,
0x6DFE410E, 0x9F95C20D, 0x8CC531F9, 0x7EAEB2FA,
0x30E349B1, 0xC288CAB2, 0xD1D83946, 0x23B3BA45,
0xF779DEAE, 0x05125DAD, 0x1642AE59, 0xE4292D5A,
0xBA3A117E, 0x4851927D, 0x5B016189, 0xA96AE28A,
0x7DA08661, 0x8FCB0562, 0x9C9BF696, 0x6EF07595,
0x417B1DBC, 0xB3109EBF, 0xA0406D4B, 0x522BEE48,
0x86E18AA3, 0x748A09A0, 0x67DAFA54, 0x95B17957,
0xCBA24573, 0x39C9C670, 0x2A993584, 0xD8F2B687,
0x0C38D26C, 0xFE53516F, 0xED03A29B, 0x1F682198,
0x5125DAD3, 0xA34E59D0, 0xB01EAA24, 0x42752927,
0x96BF4DCC, 0x64D4CECF, 0x77843D3B, 0x85EFBE38,
0xDBFC821C, 0x2997011F, 0x3AC7F2EB, 0xC8AC71E8,
0x1C661503, 0xEE0D9600, 0xFD5D65F4, 0x0F36E6F7,
0x61C69362, 0x93AD1061, 0x80FDE395, 0x72966096,
0xA65C047D, 0x5437877E, 0x4767748A, 0xB50CF789,
0xEB1FCBAD, 0x197448AE, 0x0A24BB5A, 0xF84F3859,
0x2C855CB2, 0xDEEEDFB1, 0xCDBE2C45, 0x3FD5AF46,
0x7198540D, 0x83F3D70E, 0x90A324FA, 0x62C8A7F9,
0xB602C312, 0x44694011, 0x5739B3E5, 0xA55230E6,
0xFB410CC2, 0x092A8FC1, 0x1A7A7C35, 0xE811FF36,
0x3CDB9BDD, 0xCEB018DE, 0xDDE0EB2A, 0x2F8B6829,
0x82F63B78, 0x709DB87B, 0x63CD4B8F, 0x91A6C88C,
0x456CAC67, 0xB7072F64, 0xA457DC90, 0x563C5F93,
0x082F63B7, 0xFA44E0B4, 0xE9141340, 0x1B7F9043,
0xCFB5F4A8, 0x3DDE77AB, 0x2E8E845F, 0xDCE5075C,
0x92A8FC17, 0x60C37F14, 0x73938CE0, 0x81F80FE3,
0x55326B08, 0xA759E80B, 0xB4091BFF, 0x466298FC,
0x1871A4D8, 0xEA1A27DB, 0xF94AD42F, 0x0B21572C,
0xDFEB33C7, 0x2D80B0C4, 0x3ED04330, 0xCCBBC033,
0xA24BB5A6, 0x502036A5, 0x4370C551, 0xB11B4652,
0x65D122B9, 0x97BAA1BA, 0x84EA524E, 0x7681D14D,
0x2892ED69, 0xDAF96E6A, 0xC9A99D9E, 0x3BC21E9D,
0xEF087A76, 0x1D63F975, 0x0E330A81, 0xFC588982,
0xB21572C9, 0x407EF1CA, 0x532E023E, 0xA145813D,
0x758FE5D6, 0x87E466D5, 0x94B49521, 0x66DF1622,
0x38CC2A06, 0xCAA7A905, 0xD9F75AF1, 0x2B9CD9F2,
0xFF56BD19, 0x0D3D3E1A, 0x1E6DCDEE, 0xEC064EED,
0xC38D26C4, 0x31E6A5C7, 0x22B65633, 0xD0DDD530,
0x0417B1DB, 0xF67C32D8, 0xE52CC12C, 0x1747422F,
0x49547E0B, 0xBB3FFD08, 0xA86F0EFC, 0x5A048DFF,
0x8ECEE914, 0x7CA56A17, 0x6FF599E3, 0x9D9E1AE0,
0xD3D3E1AB, 0x21B862A8, 0x32E8915C, 0xC083125F,
0x144976B4, 0xE622F5B7, 0xF5720643, 0x07198540,
0x590AB964, 0xAB613A67, 0xB831C993, 0x4A5A4A90,
0x9E902E7B, 0x6CFBAD78, 0x7FAB5E8C, 0x8DC0DD8F,
0xE330A81A, 0x115B2B19, 0x020BD8ED, 0xF0605BEE,
0x24AA3F05, 0xD6C1BC06, 0xC5914FF2, 0x37FACCF1,
0x69E9F0D5, 0x9B8273D6, 0x88D28022, 0x7AB90321,
0xAE7367CA, 0x5C18E4C9, 0x4F48173D, 0xBD23943E,
0xF36E6F75, 0x0105EC76, 0x12551F82, 0xE03E9C81,
0x34F4F86A, 0xC69F7B69, 0xD5CF889D, 0x27A40B9E,
0x79B737BA, 0x8BDCB4B9, 0x988C474D, 0x6AE7C44E,
0xBE2DA0A5, 0x4C4623A6, 0x5F16D052, 0xAD7D5351,
};
#define SCTP_CRC32C(c,d) (c=(c>>8)^crc_c[(c^(d))&0xFF])
static uint32_t
singletable_crc32c(uint32_t crc32c,
const unsigned char *buffer,
unsigned int length)
{
unsigned int i;
for (i = 0; i < length; i++) {
SCTP_CRC32C(crc32c, buffer[i]);
}
return (crc32c);
}
static uint32_t
calculate_crc32c(uint32_t crc32c,
const unsigned char *buffer,
unsigned int length)
{
if (length < 4) {
return (singletable_crc32c(crc32c, buffer, length));
} else {
return (multitable_crc32c(crc32c, buffer, length));
}
}
#define BSWAP32(x) ((((x) & 0xff) << 24) | \
(((x) & 0xff00) << 8) | \
(((x) & 0xff0000) >> 8) | \
(((x) & 0xff000000) >> 24))
static uint32_t
finalize_crc32c(uint32_t crc32c)
{
const int indian = 1;
uint32_t result;
/* Complement the result */
result = ~crc32c;
if (*(const char *)(const void *)&indian) {
/* little-endian */
crc32c = result;
} else {
/* big-endian */
crc32c = BSWAP32(result);
}
return crc32c;
}
/* export from here on */
void
crc32c_init(ctx32_t *ctx)
{
*ctx = 0xffffffff;
}
void
crc32c_update(ctx32_t *ctx, const uint8_t *data, size_t size)
{
*ctx = calculate_crc32c(*ctx, data, (unsigned)size);
}
void
crc32c_final(uint32_t *digest, ctx32_t *ctx)
{
*digest = finalize_crc32c(*ctx);
}
uint32_t
crc32c_data(const uint8_t *data, size_t size)
{
ctx32_t ctx;
crc32c_init(&ctx);
crc32c_update(&ctx, data, (unsigned)size);
crc32c_final(&ctx, &ctx);
return ctx;
}

View File

@@ -0,0 +1,53 @@
/*-
* Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
* 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 ``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 CRC32C_H_
#define CRC32C_H_ 20121007
#include <sys/types.h>
#include <inttypes.h>
typedef uint32_t ctx32_t;
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
__BEGIN_DECLS
void crc32c_init(ctx32_t */*ctx*/);
void crc32c_update(ctx32_t */*ctx*/, const uint8_t */*data*/, size_t /*size*/);
void crc32c_final(uint32_t */*digest*/, ctx32_t */*ctx*/);
uint32_t crc32c_data(const uint8_t */*data*/, size_t /*size*/);
__END_DECLS
#endif

View File

@@ -0,0 +1,411 @@
/*-
* Copyright (c) 2013 Alistair Crooks <agc@NetBSD.org>
* 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 ``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.
*/
/*
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
Michaël Peeters and Gilles Van Assche. For more information, feedback or
questions, please refer to our website: http://keccak.noekeon.org/
Implementation by the designers,
hereby denoted as "the implementer".
To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "keccak.h"
static void
fromBytesToWords(uint64_t *stateAsWords, const uint8_t *state)
{
unsigned int i, j;
for (i = 0; i < (KECCAK_PERMUTATION_SIZE_BITS / 64); i++) {
stateAsWords[i] = 0;
for (j = 0; j < (64/8); j++) {
stateAsWords[i] |= (uint64_t)(state[i*(64/8)+j]) << (8*j);
}
}
}
static void
fromWordsToBytes(uint8_t *state, const uint64_t *stateAsWords)
{
unsigned int i, j;
for (i = 0; i < (KECCAK_PERMUTATION_SIZE_BITS / 64); i++) {
for (j = 0; j < (64/8); j++) {
state[i*(64/8)+j] = (uint8_t)(stateAsWords[i] >> (8*j)) & 0xFF;
}
}
}
#define INDEX(x, y) (((x)%5)+5*((y)%5))
#define ROL64(a, offset) (/*CONSTCOND*/(offset != 0) ? ((((uint64_t)a) << offset) ^ (((uint64_t)a) >> (64-offset))) : a)
static inline void
theta(uint64_t *A)
{
unsigned int x, y;
uint64_t C[5], D[5];
for (x = 0; x < 5; x++) {
C[x] = 0;
for (y = 0; y < 5; y++) {
C[x] ^= A[INDEX(x, y)];
}
}
for (x = 0; x < 5; x++) {
D[x] = ROL64(C[(x+1)%5], 1) ^ C[(x+4)%5];
}
for (x = 0; x < 5; x++) {
for (y = 0; y < 5; y++) {
A[INDEX(x, y)] ^= D[x];
}
}
}
static inline void
rho(KECCAK_CTX *ctx, uint64_t *A)
{
unsigned int x, y;
for (x = 0; x < 5; x++) {
for (y = 0; y < 5; y++) {
A[INDEX(x, y)] = ROL64(A[INDEX(x, y)], ctx->RhoOffsets[INDEX(x, y)]);
}
}
}
static inline void
pi(uint64_t *A)
{
unsigned int x, y;
uint64_t tempA[25];
for (x = 0; x < 5; x++) {
for (y = 0; y < 5; y++) {
tempA[INDEX(x, y)] = A[INDEX(x, y)];
}
}
for (x = 0; x < 5; x++) {
for (y = 0; y < 5; y++) {
A[INDEX(0*x+1*y, 2*x+3*y)] = tempA[INDEX(x, y)];
}
}
}
static inline void
chi(uint64_t *A)
{
unsigned int x, y;
uint64_t C[5];
for (y = 0; y < 5; y++) {
for (x = 0; x < 5; x++) {
C[x] = A[INDEX(x, y)] ^ ((~A[INDEX(x+1, y)]) & A[INDEX(x+2, y)]);
}
for (x = 0; x < 5; x++) {
A[INDEX(x, y)] = C[x];
}
}
}
static inline void
iota(KECCAK_CTX *ctx, uint64_t *A, unsigned int indexRound)
{
A[INDEX(0, 0)] ^= ctx->RoundConstants[indexRound];
}
static inline void
KeccakPermutationOnWords(KECCAK_CTX *ctx, uint64_t *st)
{
unsigned int i;
for (i = 0; i < KECCAK_NUM_ROUNDS; i++) {
theta(st);
rho(ctx, st);
pi(st);
chi(st);
iota(ctx, st, i);
}
}
static void
keccak_permutation(KECCAK_CTX *ctx)
{
const int indian = 1;
uint64_t stateAsWords[KECCAK_PERMUTATION_SIZE_BITS/64];
if (*(const char *)(const void *)&indian) {
/* little endian */
KeccakPermutationOnWords(ctx, (uint64_t*)(void *)ctx->state);
} else {
fromBytesToWords(stateAsWords, ctx->state);
KeccakPermutationOnWords(ctx, stateAsWords);
fromWordsToBytes(ctx->state, stateAsWords);
}
}
static void
KeccakPermutationAfterXor(KECCAK_CTX *ctx, uint8_t *state, const uint8_t *data, unsigned int dataLengthInBytes)
{
unsigned int i;
for (i = 0; i < dataLengthInBytes; i++) {
state[i] ^= data[i];
}
keccak_permutation(ctx);
}
static int
LFSR86540(uint8_t *LFSR)
{
int result = ((*LFSR) & 0x01) != 0;
if (((*LFSR) & 0x80) != 0) {
/* Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1 */
(*LFSR) = ((*LFSR) << 1) ^ 0x71;
} else {
(*LFSR) = (*LFSR) << 1;
}
return result;
}
static void
keccak_initialise_RoundConstants(KECCAK_CTX *ctx)
{
uint8_t LFSRstate = 0x01;
unsigned int i, j, bitPosition;
for (i = 0; i < KECCAK_NUM_ROUNDS; i++) {
ctx->RoundConstants[i] = 0;
for (j = 0; j < 7; j++) {
bitPosition = (1<<j)-1; /*2^j-1 */
if (LFSR86540(&LFSRstate)) {
ctx->RoundConstants[i] ^= (uint64_t)1<<bitPosition;
}
}
}
}
static void
keccak_initialise_RhoOffsets(KECCAK_CTX *ctx)
{
unsigned int x, y, t, newX, newY;
ctx->RhoOffsets[INDEX(0, 0)] = 0;
x = 1;
y = 0;
for (t = 0; t < 24; t++) {
ctx->RhoOffsets[INDEX(x, y)] = ((t+1)*(t+2)/2) % 64;
newX = (0*x+1*y) % 5;
newY = (2*x+3*y) % 5;
x = newX;
y = newY;
}
}
static int
init_sponge(KECCAK_CTX *ctx, unsigned int rate, unsigned int capacity)
{
if (rate+capacity != 1600) {
return 1;
}
if ((rate == 0) || (rate >= 1600) || ((rate % 64) != 0)) {
return 1;
}
keccak_initialise_RoundConstants(ctx);
keccak_initialise_RhoOffsets(ctx);
ctx->rate = rate;
ctx->capacity = capacity;
ctx->fixedOutputLength = 0;
memset(ctx->state, 0x0, sizeof(ctx->state));
memset(ctx->dataQueue, 0x0, sizeof(ctx->dataQueue));
ctx->bitsInQueue = 0;
ctx->squeezing = 0;
ctx->bitsAvailableForSqueezing = 0;
return 0;
}
static inline void
absorb_queue(KECCAK_CTX *ctx)
{
/* state->bitsInQueue is assumed to be equal to state->rate */
KeccakPermutationAfterXor(ctx, ctx->state, ctx->dataQueue, ctx->rate / 8);
ctx->bitsInQueue = 0;
}
static int
absorb(KECCAK_CTX *ctx, const uint8_t *data, uint64_t databitlen)
{
uint64_t i, j, wholeBlocks;
unsigned int partialBlock, partialByte;
const uint8_t *curData;
if ((ctx->bitsInQueue % 8) != 0) {
return 1; /* Only the last call may contain a partial byte */
}
if (ctx->squeezing) {
return 1; /* Too late for additional input */
}
for (i = 0; i < databitlen ; ) {
if ((ctx->bitsInQueue == 0) && (databitlen >= ctx->rate) && (i <= (databitlen-ctx->rate))) {
wholeBlocks = (databitlen-i)/ctx->rate;
curData = &data[(long)i/8];
for (j=0; j<wholeBlocks; j++, curData+=ctx->rate/8) {
KeccakPermutationAfterXor(ctx, ctx->state, curData, ctx->rate / 8);
}
i += wholeBlocks*ctx->rate;
} else {
partialBlock = (unsigned int)(databitlen - i);
if (partialBlock+ctx->bitsInQueue > ctx->rate) {
partialBlock = ctx->rate-ctx->bitsInQueue;
}
partialByte = partialBlock % 8;
partialBlock -= partialByte;
memcpy(ctx->dataQueue+ctx->bitsInQueue/8, &data[(unsigned long)i/8], partialBlock/8);
ctx->bitsInQueue += partialBlock;
i += partialBlock;
if (ctx->bitsInQueue == ctx->rate) {
absorb_queue(ctx);
}
if (partialByte > 0) {
uint8_t mask = (1 << partialByte)-1;
ctx->dataQueue[ctx->bitsInQueue/8] = data[(unsigned long)i/8] & mask;
ctx->bitsInQueue += partialByte;
i += partialByte;
}
}
}
return 0;
}
static void
PadAndSwitchToSqueezingPhase(KECCAK_CTX *ctx)
{
/* Note: the bits are numbered from 0=LSB to 7=MSB */
if (ctx->bitsInQueue + 1 == ctx->rate) {
ctx->dataQueue[ctx->bitsInQueue/8 ] |= 1 << (ctx->bitsInQueue % 8);
absorb_queue(ctx);
memset(ctx->dataQueue, 0, ctx->rate/8);
} else {
memset(ctx->dataQueue + (ctx->bitsInQueue+7)/8, 0, ctx->rate/8 - (ctx->bitsInQueue+7)/8);
ctx->dataQueue[ctx->bitsInQueue/8 ] |= 1 << (ctx->bitsInQueue % 8);
}
ctx->dataQueue[(ctx->rate-1)/8] |= 1 << ((ctx->rate-1) % 8);
absorb_queue(ctx);
memcpy(ctx->dataQueue, ctx->state, ctx->rate/8);
ctx->bitsAvailableForSqueezing = ctx->rate;
ctx->squeezing = 1;
}
static int
squeeze(KECCAK_CTX *ctx, uint8_t *output, uint64_t outputLength)
{
uint64_t i;
unsigned int partialBlock;
if (!ctx->squeezing) {
PadAndSwitchToSqueezingPhase(ctx);
}
if ((outputLength % 8) != 0) {
return 1; /* Only multiple of 8 bits are allowed, truncation can be done at user level */
}
for (i = 0; i < outputLength ; i += partialBlock) {
if (ctx->bitsAvailableForSqueezing == 0) {
keccak_permutation(ctx);
memcpy(ctx->dataQueue, ctx->state, ctx->rate/8);
ctx->bitsAvailableForSqueezing = ctx->rate;
}
partialBlock = ctx->bitsAvailableForSqueezing;
if ((uint64_t)partialBlock > outputLength - i) {
partialBlock = (unsigned int)(outputLength - i);
}
memcpy(&output[(unsigned long)i/8],
ctx->dataQueue+(ctx->rate-ctx->bitsAvailableForSqueezing)/8,
partialBlock/8);
ctx->bitsAvailableForSqueezing -= partialBlock;
}
return 0;
}
/***************************************************************/
HashReturn
KECCAK_Init(KECCAK_CTX *ctx, int hashbitlen)
{
switch(hashbitlen) {
case 0: /* Default parameters, arbitrary length output */
init_sponge((KECCAK_CTX*)ctx, 1024, 576);
break;
case 224:
init_sponge((KECCAK_CTX*)ctx, 1152, 448);
break;
case 256:
init_sponge((KECCAK_CTX*)ctx, 1088, 512);
break;
case 384:
init_sponge((KECCAK_CTX*)ctx, 832, 768);
break;
case 512:
init_sponge((KECCAK_CTX*)ctx, 576, 1024);
break;
default:
return BAD_HASHLEN;
}
ctx->fixedOutputLength = hashbitlen;
return SUCCESS;
}
HashReturn
KECCAK_Update(KECCAK_CTX *ctx, const uint8_t *data, uint64_t databitlen)
{
HashReturn ret;
if ((databitlen % 8) == 0) {
return absorb((KECCAK_CTX*)ctx, data, databitlen);
}
ret = absorb((KECCAK_CTX*)ctx, data, databitlen - (databitlen % 8));
if (ret == SUCCESS) {
uint8_t lastByte;
/* Align the last partial byte to the least significant bits */
lastByte = data[(unsigned long)databitlen/8] >> (8 - (databitlen % 8));
return absorb((KECCAK_CTX*)ctx, &lastByte, databitlen % 8);
}
return ret;
}
HashReturn
KECCAK_Final(KECCAK_CTX *ctx, uint8_t *hashval)
{
return squeeze(ctx, hashval, ctx->fixedOutputLength);
}

View File

@@ -0,0 +1,89 @@
/*-
* Copyright (c) 2013 Alistair Crooks <agc@NetBSD.org>
* 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 ``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.
*/
/*
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
Michaël Peeters and Gilles Van Assche. For more information, feedback or
questions, please refer to our website: http://keccak.noekeon.org/
Implementation by the designers,
hereby denoted as "the implementer".
To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/
#ifndef _KeccakSponge_h_
#define _KeccakSponge_h_ 20130813
#include <inttypes.h>
#define KECCAK_PERMUTATION_SIZE_BITS 1600
#define KECCAK_MAX_RATE_BITS 1536
#define KECCAK_NUM_ROUNDS 24
#define KECCAK_NUM_LANES 25
#if defined(__GNUC__)
#define ALIGN __attribute__ ((aligned(32)))
#elif defined(_MSC_VER)
#define ALIGN __declspec(align(32))
#else
#define ALIGN
#endif
ALIGN typedef struct KECCAK_CTX {
ALIGN uint8_t state[KECCAK_PERMUTATION_SIZE_BITS / 8];
ALIGN uint8_t dataQueue[KECCAK_MAX_RATE_BITS / 8];
uint32_t rate;
uint32_t capacity;
uint32_t bitsInQueue;
uint32_t fixedOutputLength;
int32_t squeezing;
uint32_t bitsAvailableForSqueezing;
uint64_t RoundConstants[KECCAK_NUM_ROUNDS];
uint32_t RhoOffsets[KECCAK_NUM_LANES];
} KECCAK_CTX;
typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2 } HashReturn;
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
__BEGIN_DECLS
HashReturn KECCAK_Init(KECCAK_CTX */*ctx*/, int /*hashbitlen*/);
HashReturn KECCAK_Update(KECCAK_CTX */*ctx*/, const uint8_t */*data*/, uint64_t /*databitlen*/);
HashReturn KECCAK_Final(KECCAK_CTX */*ctx*/, uint8_t */*hashval*/);
__END_DECLS
#endif

View File

@@ -0,0 +1,202 @@
.\" $NetBSD: libmultigest.3,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $
.\"
.\" Copyright (c) 2013,2014 Alistair Crooks <agc@NetBSD.org>
.\" 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 ``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.
.\"
.Dd March 3, 2014
.Dt LIBMULTIGEST 3
.Os
.Sh NAME
.Nm libmultigest
.Nd Multiple digest library
.Sh LIBRARY
.Lb libmultigest
.Sh SYNOPSIS
.In multigest.h
.Ft multigest_t *
.Fo multigest_new
.Fa "void"
.Fc
.Ft int
.Fo multigest_init
.Fa "multigest_t *mg" "const char *algorithms"
.Fc
.Ft int
.Fo multigest_add_subst
.Fa "multigest_t *mg" "const char *substregex" "const char *replacement"
.Fc
.Ft void
.Fo multigest_update
.Fa "multigest_t *mg" "const char *data" "size_t length"
.Fc
.Ft void
.Fo multigest_final
.Fa "unsigned char *rawdigest" "multigest_t *mg"
.Fc
.Ft "uint8_t *"
.Fo multigest_data
.Fa "const char *algorithms" "const char *data" "size_t length"
.Fa "const unsigned char *rawoutput" "const char *substregex"
.Fa "const char *replacement"
.Fc
.Ft "uint8_t *"
.Fo multigest_file
.Fa "const char *algorithms" "const char *filename"
.Fa "const unsigned char *rawoutput" "const char *substregex"
.Fa "const char *replacement"
.Fc
.Ft uint32_t
.Fo multigest_get_rawsize
.Fa "multigest_t *mg"
.Fc
.Ft uint32_t
.Fo multigest_algs_rawsize
.Fa "const char *algnames"
.Fc
.Ft int
.Fo multigest_format_hex
.Fa "unsigned char *rawinput" "const char *algorithms"
.Fa "char *output" "size_t outputsize"
.Fc
.Ft int
.Fo multigest_print_hex
.Fa "unsigned char *rawinput" "const char *algorithms"
.Fa "const char *outname" "const char *filename" "const char *substregex"
.Fa "const char *output_separator" "const char *output_format"
.Fc
.Ft char *
.Fo multigest_format_raw
.Fa "const uint8_t *rawinput" "size_t insize"
.Fa "char *fmtoutput" "size_t outsize"
.Fc
.Sh DESCRIPTION
.Nm
is a library interface to calculate multiple digests
at the same time, without having to re-scan data.
This is to protect against any single digest algorithm
being found to have second pre-images.
Up to 32 digests can be specified on the command line.
.Pp
The data being used as input to the digest can be modified
by using a regular expression-based substitution operation,
so that any Version Control System identifiers can be normalised
before being digested.
If no substitution regular expression is provided,
the input data will be used as-is.
.Pp
The list of digests provided is:
.Bd -literal -offset indent
BLAKE2
CRC32C
MD5
RMD160
SHA1
SHA256
SHA3-224
SHA3-256
SHA3-384
SHA3-512
SHA512
SIZE
TIGER2
TIGER
WHIRLPOOL
.Ed
.Pp
The
.Dv crc32c
checksum is a simple, lightweight checksum, as found in SCTP and iSCSI.
It is useful since there are times when a secure digest is not needed,
but rather an indication of correct transmission, where calculating a heavyweight
digest may be overkill.
The
.Dv TIGER2
digest is different to the
.Dv TIGER
digest in its initialisation only.
Obviously, different values are calculated for the two digests.
The
.Dv BLAKE2
digest is one of the losing finalists in the SHA-3
competition.
The winning SHA-3 contender, the sponge function
.Dv Keccak ,
has been
included here, with 4 different digest lengths, of 224, 256, 384 and 512 bits.
The
.Dv size
pseudo-algorithm simply prints the size of the input data in bytes.
.Pp
As the
.Nm
name suggests, multiple digests can be used to calculate compound digests.
The output from each digest is concatenated on the output.
Digest names are provided to the initialisation function in a comma-separated
list of names.
.Pp
There are two interfaces to the
.Nm
library, one using the lower-level functions
.Fn multigest_init ,
.Fn multigest_update
and
.Fn multigest_final
to calculate the digests.
The other, higher-level interface,
uses the functions
.Fn mutigest_data
and
.Fn multigest_file
to operate on a complete set of bytes, or a file.
.Pp
The output from
.Fn multigest_final
is a string of unsigned bytes, which can be formatted
using
.Fn multigest_format_hex
or printed, possible to an output file, by
.Fn multigest_print_hex .
To format output without printing to a stream,
the
.Fn multigest_format_raw
function can be used.
To find out the rawsize needed (in bytes) for a multigest,
the
.Fn multigest_algs_rawsize
function can be used, passing the digest algorithm names as
arguments to the function, or the
.Fn multigest_get_rawsize
function can be used if the multigest structure has already
been initialised.
.Sh SEE ALSO
.Xr md5 3 ,
.Xr rmd160 3 ,
.Xr sha1 3 ,
.Xr sha2 3
.Sh HISTORY
The
.Nm
library first appeared in
.Nx 7.0 .
.Sh AUTHORS
.An Alistair Crooks Aq Mt agc@NetBSD.org .

View File

@@ -0,0 +1,207 @@
/*-
* Copyright (c) 2013 Alistair Crooks <agc@NetBSD.org>
* 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 ``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 <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "multigest.h"
#define MB(x) ((x) * 1024 * 1024)
/* read input into memory, and then multigest that */
static int
do_input(const char *alg, uint8_t *raw, const char *pat, const char *repl)
{
size_t cc;
size_t rc;
char *data;
if ((data = calloc(1, MB(4))) != NULL) {
for (cc = 0 ; cc < MB(4) ; cc += rc) {
if ((rc = read(fileno(stdin), &data[cc], MB(4) - cc)) <= 0) {
break;
}
}
multigest_data(alg, data, cc, raw, pat, repl);
free(data);
return 1;
}
return 0;
}
/* get the substitution pattern from the arg */
static int
getsubst(const char *arg, char *from, size_t fromsize, char *to, size_t tosize)
{
const char *sep;
const char *sub;
size_t len;
for (sep = arg + 1 ; *sep && *sep != *arg ; sep++) {
if (*sep == '\\') {
sep += 1;
}
}
len = (size_t)(sep - arg) - 1;
len = MIN(fromsize - 1, len);
multigest_unpcstring(arg + 1, len, from, fromsize);
for (sub = ++sep ; *sep && *sep != *arg ; sep++) {
if (*sep == '\\') {
sep += 1;
}
}
len = (size_t)(sep - sub);
len = MIN(tosize - 1, len);
multigest_unpcstring(sub, len, to, tosize);
return 1;
}
/* verify contents of an existing dynamic digest file */
static int
read_check(const char *check)
{
struct stat st;
regmatch_t match[10];
regex_t r;
uint8_t raw[8192];
FILE *fp;
char *provided;
char calc[8192];
char from[128];
char to[128];
char *file;
char *subs;
char *alg;
char *in;
int ret;
regcomp(&r, "([^ ]+) \\(([^)]+)\\) \\((.*)\\) = ([0-9a-zA-Z]+)", REG_EXTENDED);
if ((fp = fopen(check, "r")) == NULL) {
fprintf(stderr, "can't check existing file '%s'\n", check);
return 0;
}
fstat(fileno(fp), &st);
if ((in = calloc(1, st.st_size + 1)) == NULL) {
fclose(fp);
return 0;
}
read(fileno(fp), in, st.st_size);
in[st.st_size] = 0x0;
fclose(fp);
if (regexec(&r, in, 10, match, 0) != 0) {
free(in);
return 0;
}
alg = &in[match[1].rm_so];
file = &in[match[2].rm_so];
subs = &in[match[3].rm_so];
provided = &in[match[4].rm_so];
in[match[1].rm_eo] = in[match[2].rm_eo] = in[match[3].rm_eo] = in[match[4].rm_eo] = 0x0;
getsubst(subs, from, sizeof(from), to, sizeof(to));
multigest_file(alg, file, raw, from, to);
multigest_format_hex(raw, alg, calc, sizeof(calc));
if ((ret = memcmp(calc, provided, match[4].rm_eo - match[4].rm_so)) != 0) {
fprintf(stderr, "multigest: provided digest: '%s', calculated digest: '%s'\n", provided, calc);
}
regfree(&r);
free(in);
return ret == 0;
}
int
main(int argc, char **argv)
{
const char *outname;
const char *format;
const char *check;
const char *alg;
const char *sub;
const char *sep;
uint8_t raw[8192];
char from[128];
char to[128];
int ok;
int i;
alg = "sha1";
format = "multigest";
sep = sub = outname = check = NULL;
from[0] = to[0] = 0x0;
while ((i = getopt(argc, argv, "F:S:a:c:o:rs:")) != -1) {
switch(i) {
case 'F':
format = optarg;
break;
case 'S':
sep = optarg;
break;
case 'a':
alg = optarg;
break;
case 'c':
check = optarg;
break;
case 'o':
outname = optarg;
break;
case 'r':
getsubst(sub = ",\\$(Id|NetBSD)[^\n]*\\$,\044NetBSD\044", from, sizeof(from), to, sizeof(to));
break;
case 's':
getsubst(sub = optarg, from, sizeof(from), to, sizeof(to));
break;
default:
break;
}
}
ok = 1;
if (check) {
if (!read_check(check)) {
ok = 0;
}
} else if (optind == argc) {
if (do_input(alg, raw, from, to)) {
multigest_print_hex(raw, alg, outname, NULL, sub, sep, format);
} else {
ok = 0;
}
} else {
for (i = optind ; i < argc ; i++) {
if (multigest_file(alg, argv[i], raw, from, to) == NULL) {
ok = 0;
} else {
multigest_print_hex(raw, alg, outname, argv[i], sub, sep, format);
}
}
}
exit((ok) ? EXIT_SUCCESS : EXIT_FAILURE);
}

View File

@@ -0,0 +1,69 @@
/* $NetBSD: md5.h,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $ */
/*
* This file is derived from the RSA Data Security, Inc. MD5 Message-Digest
* Algorithm and has been modified by Jason R. Thorpe <thorpej@NetBSD.ORG>
* for portability and formatting.
*/
/*
* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
* rights reserved.
*
* License to copy and use this software is granted provided that it
* is identified as the "RSA Data Security, Inc. MD5 Message-Digest
* Algorithm" in all material mentioning or referencing this software
* or this function.
*
* License is also granted to make and use derivative works provided
* that such works are identified as "derived from the RSA Data
* Security, Inc. MD5 Message-Digest Algorithm" in all material
* mentioning or referencing the derived work.
*
* RSA Data Security, Inc. makes no representations concerning either
* the merchantability of this software or the suitability of this
* software for any particular purpose. It is provided "as is"
* without express or implied warranty of any kind.
*
* These notices must be retained in any copies of any part of this
* documentation and/or software.
*/
#ifndef _SYS_MD5_H_
#define _SYS_MD5_H_
#include <inttypes.h>
#include <stdint.h>
#include <unistd.h>
/* MD5 context. */
typedef struct MD5Context {
uint32_t state[4]; /* state (ABCD) */
uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
unsigned char buffer[64]; /* input buffer */
} MD5_CTX;
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
__BEGIN_DECLS
void MD5Init(MD5_CTX *);
void MD5Update(MD5_CTX *, const uint8_t *, size_t);
void MD5Final(unsigned char[16], MD5_CTX *);
#ifndef _KERNEL
char *MD5End(MD5_CTX *, char *);
char *MD5File(const char *, char *);
char *MD5Data(const uint8_t *, size_t, char *);
#endif /* _KERNEL */
__END_DECLS
#endif /* _SYS_MD5_H_ */

View File

@@ -0,0 +1,331 @@
/* $NetBSD: md5c.c,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $ */
/*
* This file is derived from the RSA Data Security, Inc. MD5 Message-Digest
* Algorithm and has been modifed by Jason R. Thorpe <thorpej@NetBSD.ORG>
* for portability and formatting.
*/
/*
* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
* rights reserved.
*
* License to copy and use this software is granted provided that it
* is identified as the "RSA Data Security, Inc. MD5 Message-Digest
* Algorithm" in all material mentioning or referencing this software
* or this function.
*
* License is also granted to make and use derivative works provided
* that such works are identified as "derived from the RSA Data
* Security, Inc. MD5 Message-Digest Algorithm" in all material
* mentioning or referencing the derived work.
*
* RSA Data Security, Inc. makes no representations concerning either
* the merchantability of this software or the suitability of this
* software for any particular purpose. It is provided "as is"
* without express or implied warranty of any kind.
*
* These notices must be retained in any copies of any part of this
* documentation and/or software.
*/
#include <assert.h>
#include <inttypes.h>
#include <string.h>
#include "md5.h"
typedef unsigned char *POINTER;
typedef uint16_t UINT2;
typedef uint32_t UINT4;
/*
* Constants for MD5Transform routine.
*/
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
static void MD5Transform(UINT4 [4], const unsigned char [64]);
static void Encode(unsigned char *, UINT4 *, unsigned int);
static void Decode(UINT4 *, const unsigned char *, unsigned int);
/*
* Encodes input (UINT4) into output (unsigned char). Assumes len is
* a multiple of 4.
*/
static void
Encode (unsigned char *output, UINT4 *input, unsigned len)
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (unsigned char)(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
}
}
/*
* Decodes input (unsigned char) into output (UINT4). Assumes len is
* a multiple of 4.
*/
static void
Decode (UINT4 *output, const uint8_t *input, unsigned len)
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
}
static const unsigned char PADDING[64] = {
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
};
/*
* F, G, H and I are basic MD5 functions.
*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
/*
* ROTATE_LEFT rotates x left n bits.
*/
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/*
* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* Rotation is separate from addition to prevent recomputation.
*/
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/*
* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
void
MD5Init(MD5_CTX *context)
{
_DIAGASSERT(context != 0);
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants. */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
}
/*
* MD5 block update operation. Continues an MD5 message-digest
* operation, processing another message block, and updating the
* context.
*/
void
MD5Update(MD5_CTX *context, const uint8_t *input, size_t inputLen)
{
unsigned int i, idx, partLen;
_DIAGASSERT(context != 0);
_DIAGASSERT(input != 0);
/* Compute number of bytes mod 64 */
idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
/* Update number of bits */
if ((context->count[0] += ((UINT4)inputLen << 3))
< ((UINT4)inputLen << 3))
context->count[1]++;
context->count[1] += ((UINT4)inputLen >> 29);
partLen = 64 - idx;
/* Transform as many times as possible. */
if (inputLen >= partLen) {
/* LINTED const castaway ok */
memcpy((POINTER)&context->buffer[idx],
input, partLen);
MD5Transform(context->state, context->buffer);
for (i = partLen; i + 63 < inputLen; i += 64)
MD5Transform(context->state, &input[i]);
idx = 0;
} else
i = 0;
/* Buffer remaining input */
/* LINTED const castaway ok */
memcpy((POINTER)&context->buffer[idx], &input[i],
inputLen - i);
}
/*
* MD5 finalization. Ends an MD5 message-digest operation, writing the
* message digest and zeroing the context.
*/
void
MD5Final(unsigned char digest[16], MD5_CTX *context)
{
unsigned char bits[8];
unsigned int idx;
size_t padLen;
_DIAGASSERT(digest != 0);
_DIAGASSERT(context != 0);
/* Save number of bits */
Encode(bits, context->count, 8);
/* Pad out to 56 mod 64. */
idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
padLen = (idx < 56) ? (56 - idx) : (120 - idx);
MD5Update (context, PADDING, padLen);
/* Append length (before padding) */
MD5Update(context, bits, 8);
/* Store state in digest */
Encode(digest, context->state, 16);
/* Zeroize sensitive information. */
memset(context, 0x0, sizeof(*context));
}
/*
* MD5 basic transformation. Transforms state based on block.
*/
static void
MD5Transform(UINT4 state[4], const uint8_t block[64])
{
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
Decode(x, block, 64);
/* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information. */
memset(x, 0x0, sizeof(x));
}

View File

@@ -0,0 +1,111 @@
.\" $NetBSD: multigest.1,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $
.\"
.\" Copyright (c) 2013 Alistair Crooks <agc@NetBSD.org>
.\" 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 ``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.
.\"
.Dd August 17, 2013
.Dt MULTIGEST 1
.Os
.Sh NAME
.Nm multigest
.Nd calculate multiple message digests simultaneously
.Sh SYNOPSIS
.Nm
.Fl r
.Op Fl a Ar algorithms
.Op Fl c Ar digestfile
.Op Fl F Ar output-format
.Op Fl o Ar output
.Op Fl S Ar output-separator
.Op Fl s Ar /regex/replacement/
.Op file ...
.Sh DESCRIPTION
The
.Nm
utility calculates message digests of files or,
if no file is specified, standard input.
The list of possible algorithms is given in the
.Xr libmultigest 3
manual page.
.Pp
The
.Nm
utility is a simple wrapper for the various different
algorithm implementations, which are located in the standard
C library, and was designed to be scalable as new message digest
algorithms are developed.
.Pp
The
.Nm
utility uses the
.Xr libmultigest 3
library.
.Pp
The following options are available:
.Bl -tag -width XregexXreplacementXYYY
.It Fl a Ar algorithms
specify a comma-separated list of algorithms to be calculated
.It Fl c Ar stored-digest-file
check, or verify, a file containing previously-generated digests
.It Fl F Ar output-format
print the output in the format requested.
Possible values are
.Dq openssl ,
.Dq digest ,
and
.Dq multigest .
The default is
.Dq multigest
which means that any replacement text is printed in a separate field.
.It Fl o Ar output-file
write the calculated digests to the output file
.It Fl r
shorthand for input substitution for the RCS Identifiers
.Dq Id
and
.Dq NetBSD
.It Fl S Ar output-separator
separate each individual digest on output with the provided string.
The output separator defaults to the null string, i.e. no separator.
.It Fl s Ar /regex/replacement/
before calculating digests, run the following transformation on the input
to normalise any input.
Using this, the effects of expanding version control system
identifiers is nullified.
.El
.Pp
If no input files are provided, input is taken from standard input.
.Sh EXIT STATUS
.Ex -std multigest
.Sh SEE ALSO
.Xr libmultigest 3
.Sh HISTORY
The
.Nm
utility first appeared in
.Nx 7.0 .
.Sh AUTHORS
The
.Nm
utility was written by
.An Alistair G. Crooks Aq Mt agc@netbsd.org .

View File

@@ -0,0 +1,772 @@
/*-
* Copyright (c) 2013 Alistair Crooks <agc@NetBSD.org>
* 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 ``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 <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#ifdef _KERNEL
#include <inttypes.h>
#include <string.h>
#else
#include <ctype.h>
#include <inttypes.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#endif
/* digests */
#include "md5.h"
#include "rmd160.h"
#include "sha1.h"
#include "sha2.h"
#include "crc32c.h"
#include "tiger.h"
#include "blake2.h"
#include "whirlpool.h"
#include "keccak.h"
#include "multigest.h"
#ifndef __arraycount
#define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
#endif
#ifndef USE_ARG
#define USE_ARG(x) /*LINTED*/(void)&(x)
#endif
#define MB(x) ((x) * 1024 * 1024)
/*
* If you'd like to add a digest to this library and/or utility, then
* you need to write:
*
* 1. a wrapper function for the digest init function
* 2. a wrapper function for the digest update function
* 3. a wrapper function for the digest final function
*
* and then add the entry to the algs table below.
*
* The table-driven nature of this is not strictly necessary, but makes
* it easier to add digest functions.
*
* The table is searched once at initialisation time, and so there is
* only one table scan to find the correct digest algorithm.
* Algorithms are searched in a case-insensitive manner.
* The text for the algorithm name in the algs table is printed as the
* name of the algorithm in the output. Convention has this as upper
* case.
*/
/*****/
static void
wrap_sha1_init(void *v)
{
SHA1Init(v);
}
static void
wrap_md5_init(void *v)
{
MD5Init(v);
}
static void
wrap_sha256_init(void *v)
{
SHA256_Init(v);
}
static void
wrap_sha512_init(void *v)
{
SHA512_Init(v);
}
static void
wrap_rmd160_init(void *v)
{
RMD160Init(v);
}
static void
wrap_crc32c_init(void *v)
{
crc32c_init(v);
}
static void
wrap_tiger_init(void *v)
{
TIGER_Init(v);
}
static void
wrap_tiger2_init(void *v)
{
TIGER2_Init(v);
}
static void
wrap_blake2_init(void *v)
{
blake2b_init(v, 64);
}
static void
wrap_whirlpool_init(void *v)
{
whirlpool_init(v);
}
static void
wrap_keccak224_init(void *v)
{
KECCAK_Init(v, 224);
}
static void
wrap_keccak256_init(void *v)
{
KECCAK_Init(v, 256);
}
static void
wrap_keccak384_init(void *v)
{
KECCAK_Init(v, 384);
}
static void
wrap_keccak512_init(void *v)
{
KECCAK_Init(v, 512);
}
static void
wrap_size_init(void *v)
{
memset(v, 0x0, sizeof(uint64_t));
}
/*****/
static void
wrap_md5_update(void *v, const char *data, unsigned len)
{
MD5Update(v, (const uint8_t *)data, len);
}
static void
wrap_sha1_update(void *v, const char *data, unsigned len)
{
SHA1Update(v, (const uint8_t *)data, len);
}
static void
wrap_sha256_update(void *v, const char *data, unsigned len)
{
SHA256_Update(v, (const uint8_t *)data, len);
}
static void
wrap_sha512_update(void *v, const char *data, unsigned len)
{
SHA512_Update(v, (const uint8_t *)data, len);
}
static void
wrap_rmd160_update(void *v, const char *data, unsigned len)
{
RMD160Update(v, (const uint8_t *)data, len);
}
static void
wrap_crc32c_update(void *v, const char *data, unsigned len)
{
crc32c_update(v, (const uint8_t *)data, len);
}
static void
wrap_tiger_update(void *v, const char *data, unsigned len)
{
TIGER_Update(v, (const uint8_t *)data, len);
}
static void
wrap_blake2_update(void *v, const char *data, unsigned len)
{
blake2b_update(v, (const uint8_t *)data, (uint64_t)len);
}
static void
wrap_whirlpool_update(void *v, const char *data, unsigned len)
{
whirlpool_update(v, (const uint8_t *)data, len);
}
static void
wrap_keccak_update(void *v, const char *data, unsigned len)
{
/* number of bits for keccak */
KECCAK_Update(v, (const uint8_t *)data, (uint64_t)(len * 8));
}
static void
wrap_size_update(void *v, const char *data, unsigned len)
{
uint64_t n;
USE_ARG(data);
memcpy(&n, v, sizeof(n));
n += len;
memcpy(v, &n, sizeof(n));
}
/*****/
static void
wrap_sha1_final(uint8_t *raw, void *v)
{
SHA1Final(raw, v);
}
static void
wrap_sha256_final(uint8_t *raw, void *v)
{
SHA256_Final(raw, v);
}
static void
wrap_sha512_final(uint8_t *raw, void *v)
{
SHA512_Final(raw, v);
}
static void
wrap_rmd160_final(uint8_t *raw, void *v)
{
RMD160Final(raw, v);
}
static void
wrap_md5_final(uint8_t *raw, void *v)
{
MD5Final(raw, v);
}
static void
wrap_crc32c_final(uint8_t *raw, void *v)
{
crc32c_final((ctx32_t *)(void *)raw, v);
}
static void
wrap_tiger_final(uint8_t *raw, void *v)
{
TIGER_Final(raw, v);
}
static void
wrap_blake2_final(uint8_t *raw, void *v)
{
blake2b_final(v, raw, 64);
}
static void
wrap_whirlpool_final(uint8_t *raw, void *v)
{
whirlpool_finalize((char *)raw, v);
}
static void
wrap_keccak_final(uint8_t *raw, void *v)
{
KECCAK_Final(v, raw);
}
static void
wrap_size_final(uint8_t *raw, void *v)
{
const int indian = 1;
uint64_t tmp;
uint64_t w;
memcpy(&w, v, sizeof(w));
if (*(const char *)(const void *)&indian) {
/* little endian */
tmp = (w >> 32) | (w << 32); \
tmp = ((tmp & (uint64_t)0xff00ff00ff00ff00ULL) >> 8) |
((tmp & (uint64_t)0x00ff00ff00ff00ffULL) << 8);
w = ((tmp & (uint64_t)0xffff0000ffff0000ULL) >> 16) |
((tmp & (uint64_t)0x0000ffff0000ffffULL) << 16);
}
memcpy(raw, &w, sizeof(w));
}
/*****/
/* digest algorithm struct */
typedef struct Alg {
const char *name; /* digest name */
size_t namelen; /* length of name */
size_t ctxsize; /* context size */
size_t rawsize; /* rawsize of output */
mg_initfunc_t init; /* digest init function */
mg_updatefunc_t update; /* digest update function */
mg_finalfunc_t final; /* digest final function */
} Alg;
static const Alg algs[] = {
{ "MD5", 3, sizeof(MD5_CTX), 16, wrap_md5_init, wrap_md5_update, wrap_md5_final },
{ "SHA1", 4, sizeof(SHA1_CTX), 20, wrap_sha1_init, wrap_sha1_update, wrap_sha1_final },
{ "SHA256", 6, sizeof(SHA256_CTX), 32, wrap_sha256_init, wrap_sha256_update, wrap_sha256_final },
{ "SHA512", 6, sizeof(SHA512_CTX), 64, wrap_sha512_init, wrap_sha512_update, wrap_sha512_final },
{ "BLAKE2", 6, sizeof(BLAKE2_CTX), 64, wrap_blake2_init, wrap_blake2_update, wrap_blake2_final },
{ "RMD160", 6, sizeof(RMD160_CTX), 20, wrap_rmd160_init, wrap_rmd160_update, wrap_rmd160_final },
{ "RIPEMD160", 9, sizeof(RMD160_CTX), 20, wrap_rmd160_init, wrap_rmd160_update, wrap_rmd160_final },
{ "CRC32C", 6, sizeof(ctx32_t), 4, wrap_crc32c_init, wrap_crc32c_update, wrap_crc32c_final },
{ "TIGER2", 6, sizeof(TIGER_CTX), 24, wrap_tiger2_init, wrap_tiger_update, wrap_tiger_final },
{ "TIGER", 5, sizeof(TIGER_CTX), 24, wrap_tiger_init, wrap_tiger_update, wrap_tiger_final },
{ "WHIRLPOOL", 9, sizeof(whirlpool_context_t), 64, wrap_whirlpool_init, wrap_whirlpool_update, wrap_whirlpool_final },
{ "SHA3-224", 8, sizeof(KECCAK_CTX), 28, wrap_keccak224_init, wrap_keccak_update, wrap_keccak_final },
{ "SHA3-256", 8, sizeof(KECCAK_CTX), 32, wrap_keccak256_init, wrap_keccak_update, wrap_keccak_final },
{ "SHA3-384", 8, sizeof(KECCAK_CTX), 48, wrap_keccak384_init, wrap_keccak_update, wrap_keccak_final },
{ "SHA3-512", 8, sizeof(KECCAK_CTX), 64, wrap_keccak512_init, wrap_keccak_update, wrap_keccak_final },
{ "KECCAK-224", 10, sizeof(KECCAK_CTX), 28, wrap_keccak224_init, wrap_keccak_update, wrap_keccak_final },
{ "KECCAK-256", 10, sizeof(KECCAK_CTX), 32, wrap_keccak256_init, wrap_keccak_update, wrap_keccak_final },
{ "KECCAK-384", 10, sizeof(KECCAK_CTX), 48, wrap_keccak384_init, wrap_keccak_update, wrap_keccak_final },
{ "KECCAK-512", 10, sizeof(KECCAK_CTX), 64, wrap_keccak512_init, wrap_keccak_update, wrap_keccak_final },
{ "SIZE", 4, sizeof(uint64_t), 8, wrap_size_init, wrap_size_update, wrap_size_final },
{ NULL, 0, 0, 0, NULL, NULL, NULL}
};
/* find an algorithm in the table above */
static const Alg *
findalg(const char *algname)
{
const Alg *alg;
for (alg = algs ; algname && alg->name ; alg++) {
if (strncasecmp(algname, alg->name, alg->namelen) == 0) {
return alg;
}
}
return NULL;
}
/* normalise through regexp substitution */
static int
normalise(multigest_t *multigest, const char *data, size_t len, int64_t *from)
{
#ifndef _KERNEL
multigest_dig_t *d;
regmatch_t match[2];
uint32_t i;
*from = 0;
while (multigest->r && len > 0) {
match[0].rm_so = *from;
match[0].rm_eo = len;
if (regexec(multigest->r, data, 2, match, REG_STARTEND) != 0) {
break;
}
for (d = multigest->digs, i = 0 ; i < multigest->digc ; i++, d++) {
(*d->update)(&multigest->ctx[d->ctxoff], &data[*from],
(unsigned)(match[0].rm_so - *from));
if (multigest->repllen) {
(*d->update)(&multigest->ctx[d->ctxoff], multigest->repl,
multigest->repllen);
}
}
*from = match[0].rm_eo;
}
#else
*from = 0;
#endif
return 1;
}
/***************************************************************************/
/* create a new struct and return it */
multigest_t *
multigest_new(void)
{
return calloc(1, sizeof(multigest_t));
}
/* initialise a struct */
int
multigest_init(multigest_t *multigest, const char *algname)
{
multigest_dig_t *d;
const Alg *alg;
uint32_t ctxoff;
uint32_t i;
uint8_t *newv;
if (multigest && algname) {
memset(multigest, 0x0, sizeof(*multigest));
multigest->type = strdup(algname);
for (i = 0, d = multigest->digs, ctxoff = 0 ; *algname ; d++, i++) {
if (i >= __arraycount(multigest->digs)) {
fprintf(stderr, "too many digest types %u\n", i);
break;
}
if ((alg = findalg(algname)) == NULL) {
fprintf(stderr, "no such algorithm '%.10s'\n", algname);
break;
}
if (ctxoff + alg->ctxsize >= multigest->ctxsize) {
if ((newv = realloc(multigest->ctx, multigest->ctxsize + 4096)) == NULL) {
fprintf(stderr, "multigest_init: allocation issues\n");
return 0;
}
multigest->ctx = newv;
multigest->ctxsize += 4096;
}
d->alg = strdup(alg->name);
(*alg->init)(&multigest->ctx[ctxoff]);
d->rawsize = alg->rawsize;
multigest->rawsize += alg->rawsize;
d->ctxoff = ctxoff;
d->update = alg->update;
d->final = alg->final;
ctxoff += (uint32_t)alg->ctxsize;
algname += alg->namelen;
if (*algname == ',') {
algname += 1;
}
multigest->digc += 1;
}
return 1;
}
fprintf(stderr, "!multigest || !algname\n");
return 0;
}
/* add a substitution pattern */
int
multigest_add_subst(multigest_t *multigest, const char *from, const char *to)
{
if (multigest && from && from[0]) {
if ((multigest->r = calloc(1, sizeof(regex_t))) == NULL ||
regcomp(multigest->r, from, REG_EXTENDED) != 0) {
return 0;
}
multigest->pat = strdup(from);
if (to) {
multigest->repl = strdup(to);
multigest->repllen = (uint32_t)strlen(to);
}
return 1;
}
return 0;
}
/* update the digest with the input */
void
multigest_update(multigest_t *multigest, const char *data, size_t len)
{
multigest_dig_t *d;
uint32_t i;
int64_t from;
if (multigest && data) {
normalise(multigest, data, len, &from);
for (d = multigest->digs, i = 0 ; i < multigest->digc ; i++, d++) {
(*d->update)(&multigest->ctx[d->ctxoff], &data[from], (unsigned)(len - from));
}
}
}
/* finalise the digest */
void
multigest_final(multigest_t *multigest, uint8_t *raw)
{
multigest_dig_t *d;
uint32_t rawoff;
uint32_t i;
if (multigest && raw) {
rawoff = 0;
for (d = multigest->digs, i = 0 ; i < multigest->digc ; i++, d++) {
(*d->final)(&raw[rawoff], &multigest->ctx[d->ctxoff]);
rawoff += (uint32_t)d->rawsize;
}
}
}
/* run sed on data and then digest it */
uint8_t *
multigest_data(const char *alg, const char *data, size_t size, uint8_t *raw, const char *pat, const char *repl)
{
multigest_t s;
if (data && alg && raw) {
memset(&s, 0x0, sizeof(s));
multigest_init(&s, alg);
multigest_add_subst(&s, pat, repl);
multigest_update(&s, data, size);
multigest_final(&s, raw);
multigest_free(&s);
return raw;
}
return NULL;
}
/* percent decode (pseudo-RFC1738) a string */
void
multigest_unpcstring(const char *in, size_t isize, char *out, size_t osize)
{
static const char *hexes = "0123456789abcdef";
const char *p[2];
const char *i;
char *o;
for (i = in, o = out ; (size_t)(o - out) < osize - 1 && (size_t)(i - in) < isize && *i ; o++) {
if (*i == '%') {
if ((p[0] = strchr(hexes, i[1])) == NULL ||
(p[1] = strchr(hexes, i[2])) == NULL) {
break;
}
*o = ((char)(p[0] - hexes) * 16) + (p[1] - hexes);
i += 3;
} else {
*o = *i++;
}
}
*o = 0x0;
}
/* print as hex string */
int
multigest_format_hex(uint8_t *raw, const char *algname, char *out, size_t size)
{
const Alg *alg;
size_t rawsize;
size_t i;
for (rawsize = 0 ; *algname ; rawsize += alg->rawsize) {
if ((alg = findalg(algname)) == NULL) {
break;
}
for (i = 0 ; i < alg->rawsize && (rawsize + i) * 2 < size; i++) {
snprintf(&out[(rawsize + i) * 2], 3, "%02hhx", raw[rawsize + i]);
}
algname += alg->namelen;
if (*algname == ',') {
algname += 1;
}
}
return (int)(rawsize + rawsize + 1);
}
/* return the size of output array we'll need */
uint32_t
multigest_get_rawsize(multigest_t *multigest)
{
return (multigest) ? (uint32_t)multigest->rawsize : 0;
}
/* return the size of output array we'll need for the alg names */
uint32_t
multigest_algs_rawsize(const char *alg)
{
multigest_t m;
uint32_t size;
memset(&m, 0x0, sizeof(m));
if (!multigest_init(&m, alg)) {
fprintf(stderr, "multigest_init: failed\n");
return 0;
}
size = multigest_get_rawsize(&m);
multigest_free(&m);
return size;
}
/*****************************************************************/
#ifndef _KERNEL
/* percent encode (pseudo-RFC1738) a string */
static void
pcstring(FILE *fp, const char *s)
{
static const char *pcencodes = "%$\r\n\t ";
for ( ; *s ; s++) {
if (strchr(pcencodes, *s) == NULL) {
fprintf(fp, "%c", *s);
} else {
fprintf(fp, "%%%02hhx", *s);
}
}
}
/* print as hex string */
int
multigest_print_hex(uint8_t *raw, const char *algname, const char *outname,
const char *f, const char *sub, const char *sep, const char *format)
{
const Alg *alg;
size_t rawsize;
size_t i;
FILE *fp;
if (outname == NULL) {
fp = stdout;
} else {
if ((fp = fopen(outname, "w")) == NULL) {
fprintf(stderr, "can't write to '%s'\n", outname);
return 0;
}
}
if (f != NULL) {
for (i = 0 ; algname[i] ; i++) {
fprintf(fp, "%c", toupper((uint8_t)algname[i]));
}
if (format && strcasecmp(format, "openssl") == 0) {
fprintf(fp, "(%s)= ", f);
} else if (format && strcasecmp(format, "digest") == 0) {
fprintf(fp, " (%s) = ", f);
} else {
fprintf(fp, " (%s) (", f);
if (sub) {
pcstring(fp, sub);
}
fprintf(fp, ") = ");
}
}
for (rawsize = 0 ; *algname ; rawsize += alg->rawsize) {
if ((alg = findalg(algname)) == NULL) {
break;
}
for (i = 0 ; i < alg->rawsize ; i++) {
fprintf(fp, "%02hhx", raw[rawsize + i]);
}
if (sep) {
fprintf(fp, "%s", sep);
}
algname += alg->namelen;
if (*algname == ',') {
algname += 1;
}
}
fprintf(fp, "\n");
if (outname != NULL) {
fclose(fp);
}
return 1;
}
/* run sed, then digest on a file */
uint8_t *
multigest_file(const char *alg, const char *f, uint8_t *raw, const char *pat, const char *repl)
{
struct stat st;
multigest_t m;
ssize_t rc;
size_t size;
size_t cc;
char *mapped;
FILE *fp;
if (f && alg && raw) {
memset(&m, 0x0, sizeof(m));
multigest_init(&m, alg);
multigest_add_subst(&m, pat, repl);
if ((fp = fopen(f, "r")) == NULL) {
fprintf(stderr, "can't open '%s'\n", f);
return 0;
}
fstat(fileno(fp), &st);
size = st.st_size;
mapped = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(fp), 0);
if (mapped == MAP_FAILED) {
mapped = calloc(1, MB(1));
for (cc = 0 ; cc < size ; cc += rc) {
if ((rc = read(fileno(fp), mapped, MB(1))) <= 0) {
break;
}
multigest_update(&m, mapped, (size_t)rc);
}
free(mapped);
} else {
multigest_update(&m, mapped, (size_t)size);
munmap(mapped, size);
}
fclose(fp);
multigest_final(&m, raw);
multigest_free(&m);
return raw;
}
return NULL;
}
/* free resources used in a sedded digest */
void
multigest_free(multigest_t *s)
{
uint32_t i;
if (s) {
if (s->ctx) {
free(s->ctx);
}
if (s->pat) {
free(s->pat);
regfree(s->r);
}
if (s->repl) {
free(s->repl);
}
for (i = 0 ; i < s->digc ; i++) {
free(s->digs[i].alg);
}
}
}
/* turn a raw digest into an ascii string */
char *
multigest_format_raw(const uint8_t *in, size_t insize, char *out, size_t outsize)
{
const uint8_t *i;
char *o;
for (o = out, i = in ; (size_t)(i - in) < insize && (size_t)(o - out) < outsize ; i++, o += 2) {
snprintf(o, 3, "%02hhx", *i);
}
*o = 0x0;
return out;
}
#endif

View File

@@ -0,0 +1,97 @@
/*-
* Copyright (c) 2014 Alistair Crooks <agc@NetBSD.org>
* 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 ``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 MULTIGEST_H_
#define MULTIGEST_H_ 20140304
#include <sys/types.h>
#include <inttypes.h>
typedef void (*mg_initfunc_t)(void *);
typedef void (*mg_updatefunc_t)(void *, const char *, unsigned);
typedef void (*mg_finalfunc_t)(uint8_t *, void *);
#define MG_MAX_DIG 32
/* a single digest struct */
typedef struct multigest_dig_t {
char *alg; /* digest name */
size_t ctxoff; /* offset in context */
size_t rawsize; /* length of raw output */
mg_updatefunc_t update; /* digest update function */
mg_finalfunc_t final; /* final update function */
} multigest_dig_t;
/* a multiple digest struct */
typedef struct multigest_t {
void *r; /* regular expression for matching */
char *pat; /* pattern we match */
char *repl; /* replacement text */
unsigned repllen; /* cached length of replacement */
char *type; /* digest type */
size_t rawsize; /* raw size of output digest */
size_t ctxsize; /* allocated size of contexts */
uint8_t *ctx; /* digest contexts */
uint32_t digc; /* # of digests */
multigest_dig_t digs[MG_MAX_DIG]; /* digest algorithms being used */
} multigest_t;
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
__BEGIN_DECLS
/* new and free */
multigest_t *multigest_new(void);
void multigest_free(multigest_t */*s*/);
/* low-level interface */
int multigest_init(multigest_t */*multigest*/, const char */*alg*/);
int multigest_add_subst(multigest_t */*multigest*/, const char */*from*/, const char */*to*/);
void multigest_update(multigest_t */*multigest*/, const char */*data*/, size_t /*len*/);
void multigest_final(multigest_t */*multigest*/, uint8_t */*raw*/);
uint32_t multigest_get_rawsize(multigest_t */*multigest*/);
/* high-level interface */
uint8_t *multigest_data(const char */*alg*/, const char */*data*/, size_t /*size*/, uint8_t */*raw*/, const char */*pat*/, const char */*repl*/);
uint8_t *multigest_file(const char */*alg*/, const char */*f*/, uint8_t */*raw*/, const char */*pat*/, const char */*repl*/);
uint32_t multigest_algs_rawsize(const char */*algs*/);
/* output */
void multigest_unpcstring(const char */*in*/, size_t /*isize*/, char */*out*/, size_t /*osize*/);
int multigest_format_hex(uint8_t */*raw*/, const char */*algname*/, char */*out*/, size_t /*size*/);
int multigest_print_hex(uint8_t */*raw*/, const char */*algname*/, const char */*outname*/, const char */*f*/, const char */*sub*/, const char */*sep*/, const char */*format*/);
char *multigest_format_raw(const uint8_t */*in*/, size_t /*insize*/, char */*out*/, size_t /*outsize*/);
__END_DECLS
#endif

View File

@@ -0,0 +1,441 @@
/* $NetBSD: rmd160.c,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $ */
/********************************************************************\
*
* FILE: rmd160.c
*
* CONTENTS: A sample C-implementation of the RIPEMD-160
* hash-function.
* TARGET: any computer with an ANSI C compiler
*
* AUTHOR: Antoon Bosselaers, ESAT-COSIC
* (Arranged for libc by Todd C. Miller)
* DATE: 1 March 1996
* VERSION: 1.0
*
* Copyright (c) Katholieke Universiteit Leuven
* 1996, All Rights Reserved
*
\********************************************************************/
/* header files */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rmd160.h"
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
/********************************************************************/
/* macro definitions */
/* collect four bytes into one word: */
#define BYTES_TO_DWORD(strptr) \
(((uint32_t) *((strptr)+3) << 24) | \
((uint32_t) *((strptr)+2) << 16) | \
((uint32_t) *((strptr)+1) << 8) | \
((uint32_t) *(strptr)))
/* ROL(x, n) cyclically rotates x over n bits to the left */
/* x must be of an unsigned 32 bits type and 0 <= n < 32. */
#define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/* the three basic functions F(), G() and H() */
#define F(x, y, z) ((x) ^ (y) ^ (z))
#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define H(x, y, z) (((x) | ~(y)) ^ (z))
#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define J(x, y, z) ((x) ^ ((y) | ~(z)))
/* the eight basic operations FF() through III() */
#define FF(a, b, c, d, e, x, s) { \
(a) += F((b), (c), (d)) + (x); \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define GG(a, b, c, d, e, x, s) { \
(a) += G((b), (c), (d)) + (x) + 0x5a827999U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define HH(a, b, c, d, e, x, s) { \
(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define II(a, b, c, d, e, x, s) { \
(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcU; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define JJ(a, b, c, d, e, x, s) { \
(a) += J((b), (c), (d)) + (x) + 0xa953fd4eU; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define FFF(a, b, c, d, e, x, s) { \
(a) += F((b), (c), (d)) + (x); \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define GGG(a, b, c, d, e, x, s) { \
(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define HHH(a, b, c, d, e, x, s) { \
(a) += H((b), (c), (d)) + (x) + 0x6d703ef3U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define III(a, b, c, d, e, x, s) { \
(a) += I((b), (c), (d)) + (x) + 0x5c4dd124U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
#define JJJ(a, b, c, d, e, x, s) { \
(a) += J((b), (c), (d)) + (x) + 0x50a28be6U; \
(a) = ROL((a), (s)) + (e); \
(c) = ROL((c), 10); \
}
/********************************************************************/
void
RMD160Init(RMD160_CTX *context)
{
_DIAGASSERT(context != NULL);
/* ripemd-160 initialization constants */
context->state[0] = 0x67452301U;
context->state[1] = 0xefcdab89U;
context->state[2] = 0x98badcfeU;
context->state[3] = 0x10325476U;
context->state[4] = 0xc3d2e1f0U;
context->length[0] = context->length[1] = 0;
context->buflen = 0;
}
/********************************************************************/
void
RMD160Transform(uint32_t state[5], const uint32_t block[16])
{
uint32_t aa, bb, cc, dd, ee;
uint32_t aaa, bbb, ccc, ddd, eee;
_DIAGASSERT(state != NULL);
_DIAGASSERT(block != NULL);
aa = aaa = state[0];
bb = bbb = state[1];
cc = ccc = state[2];
dd = ddd = state[3];
ee = eee = state[4];
/* round 1 */
FF(aa, bb, cc, dd, ee, block[ 0], 11);
FF(ee, aa, bb, cc, dd, block[ 1], 14);
FF(dd, ee, aa, bb, cc, block[ 2], 15);
FF(cc, dd, ee, aa, bb, block[ 3], 12);
FF(bb, cc, dd, ee, aa, block[ 4], 5);
FF(aa, bb, cc, dd, ee, block[ 5], 8);
FF(ee, aa, bb, cc, dd, block[ 6], 7);
FF(dd, ee, aa, bb, cc, block[ 7], 9);
FF(cc, dd, ee, aa, bb, block[ 8], 11);
FF(bb, cc, dd, ee, aa, block[ 9], 13);
FF(aa, bb, cc, dd, ee, block[10], 14);
FF(ee, aa, bb, cc, dd, block[11], 15);
FF(dd, ee, aa, bb, cc, block[12], 6);
FF(cc, dd, ee, aa, bb, block[13], 7);
FF(bb, cc, dd, ee, aa, block[14], 9);
FF(aa, bb, cc, dd, ee, block[15], 8);
/* round 2 */
GG(ee, aa, bb, cc, dd, block[ 7], 7);
GG(dd, ee, aa, bb, cc, block[ 4], 6);
GG(cc, dd, ee, aa, bb, block[13], 8);
GG(bb, cc, dd, ee, aa, block[ 1], 13);
GG(aa, bb, cc, dd, ee, block[10], 11);
GG(ee, aa, bb, cc, dd, block[ 6], 9);
GG(dd, ee, aa, bb, cc, block[15], 7);
GG(cc, dd, ee, aa, bb, block[ 3], 15);
GG(bb, cc, dd, ee, aa, block[12], 7);
GG(aa, bb, cc, dd, ee, block[ 0], 12);
GG(ee, aa, bb, cc, dd, block[ 9], 15);
GG(dd, ee, aa, bb, cc, block[ 5], 9);
GG(cc, dd, ee, aa, bb, block[ 2], 11);
GG(bb, cc, dd, ee, aa, block[14], 7);
GG(aa, bb, cc, dd, ee, block[11], 13);
GG(ee, aa, bb, cc, dd, block[ 8], 12);
/* round 3 */
HH(dd, ee, aa, bb, cc, block[ 3], 11);
HH(cc, dd, ee, aa, bb, block[10], 13);
HH(bb, cc, dd, ee, aa, block[14], 6);
HH(aa, bb, cc, dd, ee, block[ 4], 7);
HH(ee, aa, bb, cc, dd, block[ 9], 14);
HH(dd, ee, aa, bb, cc, block[15], 9);
HH(cc, dd, ee, aa, bb, block[ 8], 13);
HH(bb, cc, dd, ee, aa, block[ 1], 15);
HH(aa, bb, cc, dd, ee, block[ 2], 14);
HH(ee, aa, bb, cc, dd, block[ 7], 8);
HH(dd, ee, aa, bb, cc, block[ 0], 13);
HH(cc, dd, ee, aa, bb, block[ 6], 6);
HH(bb, cc, dd, ee, aa, block[13], 5);
HH(aa, bb, cc, dd, ee, block[11], 12);
HH(ee, aa, bb, cc, dd, block[ 5], 7);
HH(dd, ee, aa, bb, cc, block[12], 5);
/* round 4 */
II(cc, dd, ee, aa, bb, block[ 1], 11);
II(bb, cc, dd, ee, aa, block[ 9], 12);
II(aa, bb, cc, dd, ee, block[11], 14);
II(ee, aa, bb, cc, dd, block[10], 15);
II(dd, ee, aa, bb, cc, block[ 0], 14);
II(cc, dd, ee, aa, bb, block[ 8], 15);
II(bb, cc, dd, ee, aa, block[12], 9);
II(aa, bb, cc, dd, ee, block[ 4], 8);
II(ee, aa, bb, cc, dd, block[13], 9);
II(dd, ee, aa, bb, cc, block[ 3], 14);
II(cc, dd, ee, aa, bb, block[ 7], 5);
II(bb, cc, dd, ee, aa, block[15], 6);
II(aa, bb, cc, dd, ee, block[14], 8);
II(ee, aa, bb, cc, dd, block[ 5], 6);
II(dd, ee, aa, bb, cc, block[ 6], 5);
II(cc, dd, ee, aa, bb, block[ 2], 12);
/* round 5 */
JJ(bb, cc, dd, ee, aa, block[ 4], 9);
JJ(aa, bb, cc, dd, ee, block[ 0], 15);
JJ(ee, aa, bb, cc, dd, block[ 5], 5);
JJ(dd, ee, aa, bb, cc, block[ 9], 11);
JJ(cc, dd, ee, aa, bb, block[ 7], 6);
JJ(bb, cc, dd, ee, aa, block[12], 8);
JJ(aa, bb, cc, dd, ee, block[ 2], 13);
JJ(ee, aa, bb, cc, dd, block[10], 12);
JJ(dd, ee, aa, bb, cc, block[14], 5);
JJ(cc, dd, ee, aa, bb, block[ 1], 12);
JJ(bb, cc, dd, ee, aa, block[ 3], 13);
JJ(aa, bb, cc, dd, ee, block[ 8], 14);
JJ(ee, aa, bb, cc, dd, block[11], 11);
JJ(dd, ee, aa, bb, cc, block[ 6], 8);
JJ(cc, dd, ee, aa, bb, block[15], 5);
JJ(bb, cc, dd, ee, aa, block[13], 6);
/* parallel round 1 */
JJJ(aaa, bbb, ccc, ddd, eee, block[ 5], 8);
JJJ(eee, aaa, bbb, ccc, ddd, block[14], 9);
JJJ(ddd, eee, aaa, bbb, ccc, block[ 7], 9);
JJJ(ccc, ddd, eee, aaa, bbb, block[ 0], 11);
JJJ(bbb, ccc, ddd, eee, aaa, block[ 9], 13);
JJJ(aaa, bbb, ccc, ddd, eee, block[ 2], 15);
JJJ(eee, aaa, bbb, ccc, ddd, block[11], 15);
JJJ(ddd, eee, aaa, bbb, ccc, block[ 4], 5);
JJJ(ccc, ddd, eee, aaa, bbb, block[13], 7);
JJJ(bbb, ccc, ddd, eee, aaa, block[ 6], 7);
JJJ(aaa, bbb, ccc, ddd, eee, block[15], 8);
JJJ(eee, aaa, bbb, ccc, ddd, block[ 8], 11);
JJJ(ddd, eee, aaa, bbb, ccc, block[ 1], 14);
JJJ(ccc, ddd, eee, aaa, bbb, block[10], 14);
JJJ(bbb, ccc, ddd, eee, aaa, block[ 3], 12);
JJJ(aaa, bbb, ccc, ddd, eee, block[12], 6);
/* parallel round 2 */
III(eee, aaa, bbb, ccc, ddd, block[ 6], 9);
III(ddd, eee, aaa, bbb, ccc, block[11], 13);
III(ccc, ddd, eee, aaa, bbb, block[ 3], 15);
III(bbb, ccc, ddd, eee, aaa, block[ 7], 7);
III(aaa, bbb, ccc, ddd, eee, block[ 0], 12);
III(eee, aaa, bbb, ccc, ddd, block[13], 8);
III(ddd, eee, aaa, bbb, ccc, block[ 5], 9);
III(ccc, ddd, eee, aaa, bbb, block[10], 11);
III(bbb, ccc, ddd, eee, aaa, block[14], 7);
III(aaa, bbb, ccc, ddd, eee, block[15], 7);
III(eee, aaa, bbb, ccc, ddd, block[ 8], 12);
III(ddd, eee, aaa, bbb, ccc, block[12], 7);
III(ccc, ddd, eee, aaa, bbb, block[ 4], 6);
III(bbb, ccc, ddd, eee, aaa, block[ 9], 15);
III(aaa, bbb, ccc, ddd, eee, block[ 1], 13);
III(eee, aaa, bbb, ccc, ddd, block[ 2], 11);
/* parallel round 3 */
HHH(ddd, eee, aaa, bbb, ccc, block[15], 9);
HHH(ccc, ddd, eee, aaa, bbb, block[ 5], 7);
HHH(bbb, ccc, ddd, eee, aaa, block[ 1], 15);
HHH(aaa, bbb, ccc, ddd, eee, block[ 3], 11);
HHH(eee, aaa, bbb, ccc, ddd, block[ 7], 8);
HHH(ddd, eee, aaa, bbb, ccc, block[14], 6);
HHH(ccc, ddd, eee, aaa, bbb, block[ 6], 6);
HHH(bbb, ccc, ddd, eee, aaa, block[ 9], 14);
HHH(aaa, bbb, ccc, ddd, eee, block[11], 12);
HHH(eee, aaa, bbb, ccc, ddd, block[ 8], 13);
HHH(ddd, eee, aaa, bbb, ccc, block[12], 5);
HHH(ccc, ddd, eee, aaa, bbb, block[ 2], 14);
HHH(bbb, ccc, ddd, eee, aaa, block[10], 13);
HHH(aaa, bbb, ccc, ddd, eee, block[ 0], 13);
HHH(eee, aaa, bbb, ccc, ddd, block[ 4], 7);
HHH(ddd, eee, aaa, bbb, ccc, block[13], 5);
/* parallel round 4 */
GGG(ccc, ddd, eee, aaa, bbb, block[ 8], 15);
GGG(bbb, ccc, ddd, eee, aaa, block[ 6], 5);
GGG(aaa, bbb, ccc, ddd, eee, block[ 4], 8);
GGG(eee, aaa, bbb, ccc, ddd, block[ 1], 11);
GGG(ddd, eee, aaa, bbb, ccc, block[ 3], 14);
GGG(ccc, ddd, eee, aaa, bbb, block[11], 14);
GGG(bbb, ccc, ddd, eee, aaa, block[15], 6);
GGG(aaa, bbb, ccc, ddd, eee, block[ 0], 14);
GGG(eee, aaa, bbb, ccc, ddd, block[ 5], 6);
GGG(ddd, eee, aaa, bbb, ccc, block[12], 9);
GGG(ccc, ddd, eee, aaa, bbb, block[ 2], 12);
GGG(bbb, ccc, ddd, eee, aaa, block[13], 9);
GGG(aaa, bbb, ccc, ddd, eee, block[ 9], 12);
GGG(eee, aaa, bbb, ccc, ddd, block[ 7], 5);
GGG(ddd, eee, aaa, bbb, ccc, block[10], 15);
GGG(ccc, ddd, eee, aaa, bbb, block[14], 8);
/* parallel round 5 */
FFF(bbb, ccc, ddd, eee, aaa, block[12] , 8);
FFF(aaa, bbb, ccc, ddd, eee, block[15] , 5);
FFF(eee, aaa, bbb, ccc, ddd, block[10] , 12);
FFF(ddd, eee, aaa, bbb, ccc, block[ 4] , 9);
FFF(ccc, ddd, eee, aaa, bbb, block[ 1] , 12);
FFF(bbb, ccc, ddd, eee, aaa, block[ 5] , 5);
FFF(aaa, bbb, ccc, ddd, eee, block[ 8] , 14);
FFF(eee, aaa, bbb, ccc, ddd, block[ 7] , 6);
FFF(ddd, eee, aaa, bbb, ccc, block[ 6] , 8);
FFF(ccc, ddd, eee, aaa, bbb, block[ 2] , 13);
FFF(bbb, ccc, ddd, eee, aaa, block[13] , 6);
FFF(aaa, bbb, ccc, ddd, eee, block[14] , 5);
FFF(eee, aaa, bbb, ccc, ddd, block[ 0] , 15);
FFF(ddd, eee, aaa, bbb, ccc, block[ 3] , 13);
FFF(ccc, ddd, eee, aaa, bbb, block[ 9] , 11);
FFF(bbb, ccc, ddd, eee, aaa, block[11] , 11);
/* combine results */
ddd += cc + state[1]; /* final result for state[0] */
state[1] = state[2] + dd + eee;
state[2] = state[3] + ee + aaa;
state[3] = state[4] + aa + bbb;
state[4] = state[0] + bb + ccc;
state[0] = ddd;
}
/********************************************************************/
void
RMD160Update(RMD160_CTX *context, const uint8_t *data, size_t nbytes)
{
uint32_t X[16];
uint32_t ofs = 0;
uint32_t i;
const int indian = 1;
uint32_t j;
_DIAGASSERT(context != NULL);
_DIAGASSERT(data != NULL);
/* update length[] */
if (context->length[0] + nbytes < context->length[0])
context->length[1]++; /* overflow to msb of length */
context->length[0] += (unsigned)nbytes;
memset(X, 0x0, sizeof(X));
if ( context->buflen + nbytes < 64 )
{
(void)memcpy(context->bbuffer + context->buflen, data, nbytes);
context->buflen += (unsigned)nbytes;
}
else
{
/* process first block */
ofs = 64 - context->buflen;
(void)memcpy(context->bbuffer + context->buflen, data, ofs);
if (*(const char *)(const void *)&indian) {
/* little endian */
(void)memcpy(X, context->bbuffer, sizeof(X));
} else {
for (j=0; j < 16; j++)
X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j));
}
RMD160Transform(context->state, X);
nbytes -= ofs;
/* process remaining complete blocks */
for (i = 0; i < (nbytes >> 6); i++) {
if (*(const char *)(const void *)&indian) {
/* little endian */
(void)memcpy(X, data + (64 * i) + ofs, sizeof(X));
} else {
for (j=0; j < 16; j++)
X[j] = BYTES_TO_DWORD(data + (64 * i) + (4 * j) + ofs);
}
RMD160Transform(context->state, X);
}
/*
* Put last bytes from data into context's buffer
*/
context->buflen = (unsigned)nbytes & 63;
memcpy(context->bbuffer, data + (64 * i) + ofs, context->buflen);
}
}
/********************************************************************/
void
RMD160Final(uint8_t digest[20], RMD160_CTX *context)
{
uint32_t i;
uint32_t X[16];
const int indian = 1;
uint32_t j;
_DIAGASSERT(digest != NULL);
_DIAGASSERT(context != NULL);
/* append the bit m_n == 1 */
context->bbuffer[context->buflen] = (uint8_t)'\200';
memset(context->bbuffer + context->buflen + 1, 0x0,
63 - context->buflen);
if (*(const char *)(const void *)&indian) {
/* little endian */
(void)memcpy(X, context->bbuffer, sizeof(X));
} else {
for (j=0; j < 16; j++)
X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j));
}
if ((context->buflen) > 55) {
/* length goes to next block */
RMD160Transform(context->state, X);
memset(X, 0x0, sizeof(X));
}
/* append length in bits */
X[14] = context->length[0] << 3;
X[15] = (context->length[0] >> 29) |
(context->length[1] << 3);
RMD160Transform(context->state, X);
if (digest != NULL) {
for (i = 0; i < 20; i += 4) {
/* extracts the 8 least significant bits. */
digest[i] = context->state[i>>2];
digest[i + 1] = (context->state[i>>2] >> 8);
digest[i + 2] = (context->state[i>>2] >> 16);
digest[i + 3] = (context->state[i>>2] >> 24);
}
}
}
/************************ end of file rmd160.c **********************/

View File

@@ -0,0 +1,70 @@
/* $NetBSD: rmd160.h,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $ */
/********************************************************************\
*
* FILE: rmd160.h
*
* CONTENTS: Header file for a sample C-implementation of the
* RIPEMD-160 hash-function.
* TARGET: any computer with an ANSI C compiler
*
* AUTHOR: Antoon Bosselaers, ESAT-COSIC
* DATE: 1 March 1996
* VERSION: 1.0
*
* Copyright (c) Katholieke Universiteit Leuven
* 1996, All Rights Reserved
*
\********************************************************************/
/*
* from OpenBSD: rmd160.h,v 1.4 1999/08/16 09:59:04 millert Exp
*/
#ifndef _RMD160_H_
#define _RMD160_H_
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
typedef struct {
uint32_t state[5]; /* state (ABCDE) */
uint32_t length[2]; /* number of bits */
uint8_t bbuffer[64]; /* overflow buffer */
uint32_t buflen; /* number of chars in bbuffer */
} RMD160_CTX;
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
__BEGIN_DECLS
void RMD160Init(RMD160_CTX *);
void RMD160Transform(uint32_t[5], const uint32_t[16]);
void RMD160Update(RMD160_CTX *, const uint8_t *, size_t);
void RMD160Final(uint8_t[20], RMD160_CTX *);
#ifndef _KERNEL
char *RMD160End(RMD160_CTX *, char *);
char *RMD160File(char *, char *);
char *RMD160Data(const uint8_t *, size_t, char *);
#endif /* _KERNEL */
__END_DECLS
#endif /* !_RMD160_H_ */

View File

@@ -0,0 +1,271 @@
/* $NetBSD: sha1.c,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $ */
/* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */
/*
* SHA-1 in C
* By Steve Reid <steve@edmweb.com>
* 100% Public Domain
*
* Test Vectors (from FIPS PUB 180-1)
* "abc"
* A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
* 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
* A million repetitions of "a"
* 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
#define SHA1HANDSOFF /* Copies data before messing with it. */
#include <assert.h>
#include <string.h>
#include "sha1.h"
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
typedef union {
uint8_t c[64];
uint32_t l[16];
} CHAR64LONG16;
/*
* XXX Kludge until there is resolution regarding mem*() functions
* XXX in the kernel.
*/
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
/*
* blk0() and blk() perform the initial expand.
* I got the idea of expanding during the round function from SSLeay
*/
static inline uint32_t
block0func(CHAR64LONG16 *block, unsigned i)
{
const int indian = 1;
if (*(const char *)(const void *)&indian) {
/* little endian */
return (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) |(rol(block->l[i],8)&0x00FF00FF));
} else {
return block->l[i];
}
}
#define blk0(i) block0func(block, i)
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
^block->l[(i+2)&15]^block->l[i&15],1))
/*
* (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
*/
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
#ifdef __sparc_v9__
void do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
void do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
void do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
void do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
#define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
#define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
#define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
#define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
#define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
void
do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
{
nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3);
nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7);
nR0(c,d,e,a,b, 8); nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); nR0(a,b,c,d,e,15);
nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19);
}
void
do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
{
nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23);
nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27);
nR2(c,d,e,a,b,28); nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); nR2(a,b,c,d,e,35);
nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39);
}
void
do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
{
nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43);
nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47);
nR3(c,d,e,a,b,48); nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); nR3(a,b,c,d,e,55);
nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59);
}
void
do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
{
nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63);
nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67);
nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75);
nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79);
}
#endif
/*
* Hash a single 512-bit block. This is the core of the algorithm.
*/
void
SHA1Transform(uint32_t state[5], const uint8_t buffer[64])
{
uint32_t a, b, c, d, e;
CHAR64LONG16 *block;
#ifdef SHA1HANDSOFF
CHAR64LONG16 workspace;
#endif
_DIAGASSERT(buffer != 0);
_DIAGASSERT(state != 0);
#ifdef SHA1HANDSOFF
block = &workspace;
(void)memcpy(block, buffer, 64);
#else
block = (CHAR64LONG16 *)(void *)buffer;
#endif
/* Copy context->state[] to working vars */
a = state[0];
b = state[1];
c = state[2];
d = state[3];
e = state[4];
#ifdef __sparc_v9__
do_R01(&a, &b, &c, &d, &e, block);
do_R2(&a, &b, &c, &d, &e, block);
do_R3(&a, &b, &c, &d, &e, block);
do_R4(&a, &b, &c, &d, &e, block);
#else
/* 4 rounds of 20 operations each. Loop unrolled. */
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
#endif
/* Add the working vars back into context.state[] */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
/* Wipe variables */
a = b = c = d = e = 0;
}
/*
* SHA1Init - Initialize new context
*/
void
SHA1Init(SHA1_CTX *context)
{
_DIAGASSERT(context != 0);
/* SHA1 initialization constants */
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
context->state[4] = 0xC3D2E1F0;
context->count[0] = context->count[1] = 0;
}
/*
* Run your data through this.
*/
void
SHA1Update(SHA1_CTX *context, const uint8_t *data, size_t len)
{
unsigned int i;
uint32_t j;
_DIAGASSERT(context != 0);
_DIAGASSERT(data != 0);
j = context->count[0];
if ((context->count[0] += len << 3) < j)
context->count[1] += (len>>29)+1;
j = (j >> 3) & 63;
if ((j + len) > 63) {
i = 64 - j;
(void)memcpy(&context->buffer[j], data, i);
SHA1Transform(context->state, context->buffer);
for ( ; i + 63 < len; i += 64)
SHA1Transform(context->state, &data[i]);
j = 0;
} else {
i = 0;
}
(void)memcpy(&context->buffer[j], &data[i], len - i);
}
/*
* Add padding and return the message digest.
*/
void
SHA1Final(uint8_t digest[20], SHA1_CTX* context)
{
unsigned int i;
uint8_t finalcount[8];
_DIAGASSERT(digest != 0);
_DIAGASSERT(context != 0);
for (i = 0; i < 8; i++) {
finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
}
SHA1Update(context, (const uint8_t *)"\200", 1);
while ((context->count[0] & 504) != 448)
SHA1Update(context, (const uint8_t *)"\0", 1);
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
if (digest) {
for (i = 0; i < 20; i++)
digest[i] = (uint8_t)
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
}
}

View File

@@ -0,0 +1,46 @@
/* $NetBSD: sha1.h,v 1.1.1.1 2014/03/05 05:09:44 agc Exp $ */
/*
* SHA-1 in C
* By Steve Reid <steve@edmweb.com>
* 100% Public Domain
*/
#ifndef _SYS_SHA1_H_
#define _SYS_SHA1_H_
#include <inttypes.h>
#include <stdint.h>
#include <unistd.h>
typedef struct {
uint32_t state[5];
uint32_t count[2];
uint8_t buffer[64];
} SHA1_CTX;
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
__BEGIN_DECLS
void SHA1Transform(uint32_t state[5], const uint8_t buffer[64]);
void SHA1Init(SHA1_CTX *context);
void SHA1Update(SHA1_CTX *context, const uint8_t *data, size_t len);
void SHA1Final(uint8_t digest[20], SHA1_CTX *context);
#ifndef _KERNEL
char *SHA1End(SHA1_CTX *, char *);
char *SHA1File(char *, char *);
char *SHA1Data(const uint8_t *, size_t, char *);
#endif /* _KERNEL */
__END_DECLS
#endif /* _SYS_SHA1_H_ */

View File

@@ -0,0 +1,895 @@
/*
* sha2.c
*
* Version 1.0.0beta1
*
* Written by Aaron D. Gifford <me@aarongifford.com>
*
* 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.
*
*/
#include <stdio.h>
#include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
#include <assert.h> /* assert() */
#include "sha2.h"
/*
* ASSERT NOTE:
* Some sanity checking code is included using assert(). On my FreeBSD
* system, this additional code can be removed by compiling with NDEBUG
* defined. Check your own systems manpage on assert() to see how to
* compile WITHOUT the sanity checking code on your system.
*
* UNROLLED TRANSFORM LOOP NOTE:
* You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
* loop version for the hash transform rounds (defined using macros
* later in this file). Either define on the command line, for example:
*
* cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
*
* or define below:
*
* #define SHA2_UNROLL_TRANSFORM
*
*/
/*** SHA-256/384/512 Machine Architecture Definitions *****************/
/*** 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)
/*** ENDIAN REVERSAL functions *******************************************/
static inline void
REVERSE32(uint32_t w, uint32_t *x)
{
const int indian = 1;
uint32_t tmp;
if (*(const char *)(const void *)&indian) {
/* little endian */
tmp = (w >> 16) | (w << 16);
*x = ((tmp & (unsigned)0xff00ff00) >> 8) | ((tmp & (unsigned)0x00ff00ff) << 8);
} else {
*x = w;
}
}
static inline void
REVERSE64(uint64_t w, uint64_t *x)
{
const int indian = 1;
uint64_t tmp;
if (*(const char *)(const void *)&indian) {
/* little endian */
tmp = (w >> 32) | (w << 32); \
tmp = ((tmp & (uint64_t)0xff00ff00ff00ff00ULL) >> 8) |
((tmp & (uint64_t)0x00ff00ff00ff00ffULL) << 8);
*x = ((tmp & (uint64_t)0xffff0000ffff0000ULL) >> 16) |
((tmp & (uint64_t)0x0000ffff0000ffffULL) << 16);
} else {
*x = w;
}
}
/*
* Macro for incrementally adding the unsigned 64-bit integer n to the
* unsigned 128-bit integer (represented using a two-element array of
* 64-bit words):
*/
#define ADDINC128(w,n) do { \
(w)[0] += (uint64_t)(n); \
if ((w)[0] < (n)) { \
(w)[1]++; \
} \
} while (/*CONSTCOND*/0)
/*** THE SIX LOGICAL FUNCTIONS ****************************************/
/*
* Bit shifting and rotation (used by the six SHA-XYZ logical functions:
*
* NOTE: The naming of R and S appears backwards here (R is a SHIFT and
* S is a ROTATION) because the SHA-256/384/512 description document
* (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
* same "backwards" definition.
*/
/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
#define R(b,x) ((x) >> (b))
/* 32-bit Rotate-right (used in SHA-256): */
#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
/* Four of six logical functions used in SHA-256: */
#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
/* Four of six logical functions used in SHA-384 and SHA-512: */
#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
/*** INTERNAL FUNCTION PROTOTYPES *************************************/
/* NOTE: These should not be accessed directly from outside this
* library -- they are intended for private internal visibility/use
* only.
*/
void SHA512_Last(SHA512_CTX*);
void SHA256_Transform(SHA256_CTX*, const uint32_t *);
void SHA512_Transform(SHA512_CTX*, const uint64_t*);
/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
/* Hash constant words K for SHA-256: */
static const uint32_t K256[64] = {
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
};
/* Initial hash value H for SHA-256: */
static const uint32_t sha256_initial_hash_value[8] = {
0x6a09e667UL,
0xbb67ae85UL,
0x3c6ef372UL,
0xa54ff53aUL,
0x510e527fUL,
0x9b05688cUL,
0x1f83d9abUL,
0x5be0cd19UL
};
/* Hash constant words K for SHA-384 and SHA-512: */
static const uint64_t K512[80] = {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
};
/* Initial hash value H for SHA-384 */
static const uint64_t sha384_initial_hash_value[8] = {
0xcbbb9d5dc1059ed8ULL,
0x629a292a367cd507ULL,
0x9159015a3070dd17ULL,
0x152fecd8f70e5939ULL,
0x67332667ffc00b31ULL,
0x8eb44a8768581511ULL,
0xdb0c2e0d64f98fa7ULL,
0x47b5481dbefa4fa4ULL
};
/* Initial hash value H for SHA-512 */
static const uint64_t sha512_initial_hash_value[8] = {
0x6a09e667f3bcc908ULL,
0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL,
0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL,
0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL,
0x5be0cd19137e2179ULL
};
/*** SHA-256: *********************************************************/
void
SHA256_Init(SHA256_CTX* context)
{
if (context == (SHA256_CTX*)0) {
return;
}
memcpy(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
memset(context->buffer, 0x0, SHA256_BLOCK_LENGTH);
context->bitcount = 0;
}
#ifdef SHA2_UNROLL_TRANSFORM
/* Unrolled SHA-256 round macros: */
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
REVERSE32(*data++, &W256[j]); \
T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
K256[j] + W256[j]; \
(d) += T1; \
(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
j++
#define ROUND256(a,b,c,d,e,f,g,h) \
s0 = W256[(j+1)&0x0f]; \
s0 = sigma0_256(s0); \
s1 = W256[(j+14)&0x0f]; \
s1 = sigma1_256(s1); \
T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
(W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
(d) += T1; \
(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
j++
void
SHA256_Transform(SHA256_CTX* context, const uint32_t * data)
{
uint32_t a, b, c, d, e, f, g, h, s0, s1;
uint32_t T1, *W256;
int j;
W256 = (uint32_t *)context->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];
j = 0;
do {
/* Rounds 0 to 15 (unrolled): */
ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
} while (j < 16);
/* Now for the remaining rounds to 64: */
do {
ROUND256(a,b,c,d,e,f,g,h);
ROUND256(h,a,b,c,d,e,f,g);
ROUND256(g,h,a,b,c,d,e,f);
ROUND256(f,g,h,a,b,c,d,e);
ROUND256(e,f,g,h,a,b,c,d);
ROUND256(d,e,f,g,h,a,b,c);
ROUND256(c,d,e,f,g,h,a,b);
ROUND256(b,c,d,e,f,g,h,a);
} 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;
/* Clean up */
a = b = c = d = e = f = g = h = T1 = 0;
}
#else /* SHA2_UNROLL_TRANSFORM */
void
SHA256_Transform(SHA256_CTX* context, const uint32_t * data)
{
uint32_t a, b, c, d, e, f, g, h, s0, s1;
uint32_t T1, T2, *W256;
int j;
W256 = (uint32_t *)(void *)context->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];
j = 0;
do {
/* Copy data while converting to host byte order */
REVERSE32(*data++, &W256[j]);
/* Apply the SHA-256 compression function to update a..h */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
T2 = Sigma0_256(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} while (j < 16);
do {
/* Part of the message block expansion: */
s0 = W256[(j+1)&0x0f];
s0 = sigma0_256(s0);
s1 = W256[(j+14)&0x0f];
s1 = sigma1_256(s1);
/* Apply the SHA-256 compression function to update a..h */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
(W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
T2 = Sigma0_256(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} 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;
/* 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_t *data, size_t len)
{
unsigned int freespace, usedspace;
if (len == 0) {
/* Calling with no data is valid - we do nothing */
return;
}
/* Sanity check: */
assert(context != NULL && data != NULL);
usedspace = (unsigned)(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, (const uint32_t *)(const void *)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, (const uint32_t *)(const void *)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;
}
void
SHA256_Final(uint8_t digest[], SHA256_CTX* context)
{
uint32_t *d = (uint32_t *)(void *)digest;
unsigned int usedspace;
const int indian = 1;
/* Sanity check: */
assert(context != NULL);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != (uint8_t*)0) {
usedspace = (unsigned)(context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
/* Convert FROM host byte order */
REVERSE64(context->bitcount, &context->bitcount);
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], 0x0, SHA256_SHORT_BLOCK_LENGTH - usedspace);
} else {
if (usedspace < SHA256_BLOCK_LENGTH) {
memset(&context->buffer[usedspace], 0x0, SHA256_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
SHA256_Transform(context, (uint32_t *)(void *)context->buffer);
/* And set-up for the last transform: */
memset(context->buffer, 0x0, SHA256_SHORT_BLOCK_LENGTH);
}
} else {
/* Set-up for the last transform: */
memset(context->buffer, 0x0, SHA256_SHORT_BLOCK_LENGTH);
/* Begin padding with a 1 bit: */
*context->buffer = 0x80;
}
/* Set the bit count: */
memcpy(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], &context->bitcount, sizeof(uint64_t));
/* Final transform: */
SHA256_Transform(context, (uint32_t *)(void *)context->buffer);
if (*(const char *)(const void *)&indian) {
/* little endian */
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE32(context->state[j], &context->state[j]);
*d++ = context->state[j];
}
} else {
memcpy(d, context->state, SHA256_DIGEST_LENGTH);
}
}
/* Clean up state data: */
memset(context, 0x0, sizeof(SHA256_CTX));
usedspace = 0;
}
/*** SHA-512: *********************************************************/
void
SHA512_Init(SHA512_CTX* context)
{
if (context == (SHA512_CTX*)0) {
return;
}
memcpy(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
memset(context->buffer, 0x0, SHA512_BLOCK_LENGTH);
context->bitcount[0] = context->bitcount[1] = 0;
}
#ifdef SHA2_UNROLL_TRANSFORM
/* Unrolled SHA-512 round macros: */
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
REVERSE64(*data++, &W512[j]); \
T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
K512[j] + W512[j]; \
(d) += T1, \
(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
j++
#define ROUND512(a,b,c,d,e,f,g,h) \
s0 = W512[(j+1)&0x0f]; \
s0 = sigma0_512(s0); \
s1 = W512[(j+14)&0x0f]; \
s1 = sigma1_512(s1); \
T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
(W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
(d) += T1; \
(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
j++
void
SHA512_Transform(SHA512_CTX* context, const uint64_t* data)
{
uint64_t a, b, c, d, e, f, g, h, s0, s1;
uint64_t T1, *W512 = (uint64_t*)context->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];
j = 0;
do {
ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
} while (j < 16);
/* Now for the remaining rounds up to 79: */
do {
ROUND512(a,b,c,d,e,f,g,h);
ROUND512(h,a,b,c,d,e,f,g);
ROUND512(g,h,a,b,c,d,e,f);
ROUND512(f,g,h,a,b,c,d,e);
ROUND512(e,f,g,h,a,b,c,d);
ROUND512(d,e,f,g,h,a,b,c);
ROUND512(c,d,e,f,g,h,a,b);
ROUND512(b,c,d,e,f,g,h,a);
} 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;
/* Clean up */
a = b = c = d = e = f = g = h = T1 = 0;
}
#else /* SHA2_UNROLL_TRANSFORM */
void
SHA512_Transform(SHA512_CTX* context, const uint64_t* data)
{
uint64_t a, b, c, d, e, f, g, h, s0, s1;
uint64_t T1, T2, *W512 = (uint64_t*)(void *)context->buffer;
const int indian = 1;
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];
j = 0;
do {
if (*(const char *)(const void *)&indian) {
/* little endian */
/* Convert TO host byte order */
REVERSE64(*data++, &W512[j]);
/* Apply the SHA-512 compression function to update a..h */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
} else {
/* Apply the SHA-512 compression function to update a..h with copy */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
}
T2 = Sigma0_512(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} while (j < 16);
do {
/* Part of the message block expansion: */
s0 = W512[(j+1)&0x0f];
s0 = sigma0_512(s0);
s1 = W512[(j+14)&0x0f];
s1 = sigma1_512(s1);
/* Apply the SHA-512 compression function to update a..h */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
(W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
T2 = Sigma0_512(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} 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;
/* Clean up */
a = b = c = d = e = f = g = h = T1 = T2 = 0;
}
#endif /* SHA2_UNROLL_TRANSFORM */
void
SHA512_Update(SHA512_CTX* context, const uint8_t *data, size_t len)
{
unsigned int freespace, usedspace;
if (len == 0) {
/* Calling with no data is valid - we do nothing */
return;
}
/* Sanity check: */
assert(context != NULL && data != NULL);
usedspace = (unsigned)(context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
if (usedspace > 0) {
/* Calculate how much free space is available in the buffer */
freespace = SHA512_BLOCK_LENGTH - usedspace;
if (len >= freespace) {
/* Fill the buffer completely and process it */
memcpy(&context->buffer[usedspace], data, freespace);
ADDINC128(context->bitcount, freespace << 3);
len -= freespace;
data += freespace;
SHA512_Transform(context, (const uint64_t*)(const void *)context->buffer);
} else {
/* The buffer is not yet full */
memcpy(&context->buffer[usedspace], data, len);
ADDINC128(context->bitcount, len << 3);
/* Clean up: */
usedspace = freespace = 0;
return;
}
}
while (len >= SHA512_BLOCK_LENGTH) {
/* Process as many complete blocks as we can */
SHA512_Transform(context, (const uint64_t*)(const void *)data);
ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
len -= SHA512_BLOCK_LENGTH;
data += SHA512_BLOCK_LENGTH;
}
if (len > 0) {
/* There's left-overs, so save 'em */
memcpy(context->buffer, data, len);
ADDINC128(context->bitcount, len << 3);
}
/* Clean up: */
usedspace = freespace = 0;
}
void
SHA512_Last(SHA512_CTX* context)
{
unsigned int usedspace;
const int indian = 1;
usedspace = (unsigned)(context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
if (*(const char *)(const void *)&indian) {
/* little endian */
/* Convert FROM host byte order */
REVERSE64(context->bitcount[0],&context->bitcount[0]);
REVERSE64(context->bitcount[1],&context->bitcount[1]);
}
if (usedspace > 0) {
/* Begin padding with a 1 bit: */
context->buffer[usedspace++] = 0x80;
if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
/* Set-up for the last transform: */
memset(&context->buffer[usedspace], 0x0, SHA512_SHORT_BLOCK_LENGTH - usedspace);
} else {
if (usedspace < SHA512_BLOCK_LENGTH) {
memset(&context->buffer[usedspace], 0x0, SHA512_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
SHA512_Transform(context, (const uint64_t*)(const void *)context->buffer);
/* And set-up for the last transform: */
memset(context->buffer, 0x0, SHA512_BLOCK_LENGTH - 2);
}
} else {
/* Prepare for final transform: */
memset(context->buffer, 0x0, SHA512_SHORT_BLOCK_LENGTH);
/* Begin padding with a 1 bit: */
*context->buffer = 0x80;
}
/* Store the length of input data (in bits): */
memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH], &context->bitcount[1], sizeof(uint64_t));;
memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8], &context->bitcount[0], sizeof(uint64_t));;
/* Final transform: */
SHA512_Transform(context, (const uint64_t*)(const void *)context->buffer);
}
void
SHA512_Final(uint8_t digest[], SHA512_CTX* context)
{
uint64_t *d = (uint64_t*)(void *)digest;
const int indian = 1;
/* Sanity check: */
assert(context != NULL);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != (uint8_t*)0) {
SHA512_Last(context);
/* Save the hash data for output: */
if (*(const char *)(const void *)&indian) {
/* little endian */
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE64(context->state[j],&context->state[j]);
*d++ = context->state[j];
}
} else {
memcpy(d, context->state, SHA512_DIGEST_LENGTH);
}
}
/* Zero out state data */
memset(context, 0x0, sizeof(SHA512_CTX));
}
/*** SHA-384: *********************************************************/
void
SHA384_Init(SHA384_CTX* context)
{
if (context == (SHA384_CTX*)0) {
return;
}
memcpy(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
memset(context->buffer, 0x0, SHA384_BLOCK_LENGTH);
context->bitcount[0] = context->bitcount[1] = 0;
}
void
SHA384_Update(SHA384_CTX* context, const uint8_t * data, size_t len)
{
SHA512_Update((SHA512_CTX*)context, data, len);
}
void
SHA384_Final(uint8_t digest[], SHA384_CTX* context)
{
uint64_t *d = (uint64_t*)(void *)digest;
const int indian = 1;
/* Sanity check: */
assert(context != NULL);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != (uint8_t*)0) {
SHA512_Last((SHA512_CTX*)context);
/* Save the hash data for output: */
if (*(const char *)(const void *)&indian) {
/* little endian */
/* Convert TO host byte order */
int j;
for (j = 0; j < 6; j++) {
REVERSE64(context->state[j],&context->state[j]);
*d++ = context->state[j];
}
} else {
memcpy(d, context->state, SHA384_DIGEST_LENGTH);
}
}
/* Zero out state data */
memset(context, 0x0, sizeof(SHA384_CTX));
}

View File

@@ -0,0 +1,109 @@
/*
* sha2.h
*
* Version 1.0.0beta1
*
* Written by Aaron D. Gifford <me@aarongifford.com>
*
* 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.
*
*/
#ifndef __SHA2_H__
#define __SHA2_H__
#include <inttypes.h>
#include <stdint.h>
#include <unistd.h>
typedef uint8_t sha2_byte; /* Exactly 1 byte */
typedef uint32_t sha2_word32; /* Exactly 4 bytes */
typedef uint64_t sha2_word64; /* Exactly 8 bytes */
/*** SHA-256/384/512 Various Length Definitions ***********************/
#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_t state[8];
uint64_t bitcount;
uint8_t buffer[SHA256_BLOCK_LENGTH];
} SHA256_CTX;
typedef struct _SHA512_CTX {
uint64_t state[8];
uint64_t bitcount[2];
uint8_t buffer[SHA512_BLOCK_LENGTH];
} SHA512_CTX;
typedef SHA512_CTX SHA384_CTX;
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
__BEGIN_DECLS
void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
char *SHA256_File(char *, char *);
void SHA384_Init(SHA384_CTX*);
void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
char *SHA384_File(char *, char *);
void SHA512_Init(SHA512_CTX*);
void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
char *SHA512_File(char *, char *);
__END_DECLS
#endif /* __SHA2_H__ */

View File

@@ -0,0 +1,906 @@
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tiger.h"
#ifndef IS_LITTLE_ENDIAN
#define IS_LITTLE_ENDIAN(x) (*(char *)(void *)&x)
#define IS_BIG_ENDIAN(x) !(*(char *)(void *)&x)
#endif
#define BSWAP64(x) ((((x) & 0xffULL) << 56) | \
(((x) & 0xff00ULL) << 40) | \
(((x) & 0xff0000ULL) << 24) | \
(((x) & 0xff000000ULL) << 8) | \
(((x) & 0xff00000000ULL) >> 8) | \
(((x) & 0xff0000000000ULL) >> 24) | \
(((x) & 0xff000000000000ULL) >> 40) | \
(((x) & 0xff00000000000000ULL) >> 56))
/* sboxes.c: Tiger S boxes */
static uint64_t table[4*256] = {
0x02AAB17CF7E90C5ELL /* 0 */, 0xAC424B03E243A8ECLL /* 1 */,
0x72CD5BE30DD5FCD3LL /* 2 */, 0x6D019B93F6F97F3ALL /* 3 */,
0xCD9978FFD21F9193LL /* 4 */, 0x7573A1C9708029E2LL /* 5 */,
0xB164326B922A83C3LL /* 6 */, 0x46883EEE04915870LL /* 7 */,
0xEAACE3057103ECE6LL /* 8 */, 0xC54169B808A3535CLL /* 9 */,
0x4CE754918DDEC47CLL /* 10 */, 0x0AA2F4DFDC0DF40CLL /* 11 */,
0x10B76F18A74DBEFALL /* 12 */, 0xC6CCB6235AD1AB6ALL /* 13 */,
0x13726121572FE2FFLL /* 14 */, 0x1A488C6F199D921ELL /* 15 */,
0x4BC9F9F4DA0007CALL /* 16 */, 0x26F5E6F6E85241C7LL /* 17 */,
0x859079DBEA5947B6LL /* 18 */, 0x4F1885C5C99E8C92LL /* 19 */,
0xD78E761EA96F864BLL /* 20 */, 0x8E36428C52B5C17DLL /* 21 */,
0x69CF6827373063C1LL /* 22 */, 0xB607C93D9BB4C56ELL /* 23 */,
0x7D820E760E76B5EALL /* 24 */, 0x645C9CC6F07FDC42LL /* 25 */,
0xBF38A078243342E0LL /* 26 */, 0x5F6B343C9D2E7D04LL /* 27 */,
0xF2C28AEB600B0EC6LL /* 28 */, 0x6C0ED85F7254BCACLL /* 29 */,
0x71592281A4DB4FE5LL /* 30 */, 0x1967FA69CE0FED9FLL /* 31 */,
0xFD5293F8B96545DBLL /* 32 */, 0xC879E9D7F2A7600BLL /* 33 */,
0x860248920193194ELL /* 34 */, 0xA4F9533B2D9CC0B3LL /* 35 */,
0x9053836C15957613LL /* 36 */, 0xDB6DCF8AFC357BF1LL /* 37 */,
0x18BEEA7A7A370F57LL /* 38 */, 0x037117CA50B99066LL /* 39 */,
0x6AB30A9774424A35LL /* 40 */, 0xF4E92F02E325249BLL /* 41 */,
0x7739DB07061CCAE1LL /* 42 */, 0xD8F3B49CECA42A05LL /* 43 */,
0xBD56BE3F51382F73LL /* 44 */, 0x45FAED5843B0BB28LL /* 45 */,
0x1C813D5C11BF1F83LL /* 46 */, 0x8AF0E4B6D75FA169LL /* 47 */,
0x33EE18A487AD9999LL /* 48 */, 0x3C26E8EAB1C94410LL /* 49 */,
0xB510102BC0A822F9LL /* 50 */, 0x141EEF310CE6123BLL /* 51 */,
0xFC65B90059DDB154LL /* 52 */, 0xE0158640C5E0E607LL /* 53 */,
0x884E079826C3A3CFLL /* 54 */, 0x930D0D9523C535FDLL /* 55 */,
0x35638D754E9A2B00LL /* 56 */, 0x4085FCCF40469DD5LL /* 57 */,
0xC4B17AD28BE23A4CLL /* 58 */, 0xCAB2F0FC6A3E6A2ELL /* 59 */,
0x2860971A6B943FCDLL /* 60 */, 0x3DDE6EE212E30446LL /* 61 */,
0x6222F32AE01765AELL /* 62 */, 0x5D550BB5478308FELL /* 63 */,
0xA9EFA98DA0EDA22ALL /* 64 */, 0xC351A71686C40DA7LL /* 65 */,
0x1105586D9C867C84LL /* 66 */, 0xDCFFEE85FDA22853LL /* 67 */,
0xCCFBD0262C5EEF76LL /* 68 */, 0xBAF294CB8990D201LL /* 69 */,
0xE69464F52AFAD975LL /* 70 */, 0x94B013AFDF133E14LL /* 71 */,
0x06A7D1A32823C958LL /* 72 */, 0x6F95FE5130F61119LL /* 73 */,
0xD92AB34E462C06C0LL /* 74 */, 0xED7BDE33887C71D2LL /* 75 */,
0x79746D6E6518393ELL /* 76 */, 0x5BA419385D713329LL /* 77 */,
0x7C1BA6B948A97564LL /* 78 */, 0x31987C197BFDAC67LL /* 79 */,
0xDE6C23C44B053D02LL /* 80 */, 0x581C49FED002D64DLL /* 81 */,
0xDD474D6338261571LL /* 82 */, 0xAA4546C3E473D062LL /* 83 */,
0x928FCE349455F860LL /* 84 */, 0x48161BBACAAB94D9LL /* 85 */,
0x63912430770E6F68LL /* 86 */, 0x6EC8A5E602C6641CLL /* 87 */,
0x87282515337DDD2BLL /* 88 */, 0x2CDA6B42034B701BLL /* 89 */,
0xB03D37C181CB096DLL /* 90 */, 0xE108438266C71C6FLL /* 91 */,
0x2B3180C7EB51B255LL /* 92 */, 0xDF92B82F96C08BBCLL /* 93 */,
0x5C68C8C0A632F3BALL /* 94 */, 0x5504CC861C3D0556LL /* 95 */,
0xABBFA4E55FB26B8FLL /* 96 */, 0x41848B0AB3BACEB4LL /* 97 */,
0xB334A273AA445D32LL /* 98 */, 0xBCA696F0A85AD881LL /* 99 */,
0x24F6EC65B528D56CLL /* 100 */, 0x0CE1512E90F4524ALL /* 101 */,
0x4E9DD79D5506D35ALL /* 102 */, 0x258905FAC6CE9779LL /* 103 */,
0x2019295B3E109B33LL /* 104 */, 0xF8A9478B73A054CCLL /* 105 */,
0x2924F2F934417EB0LL /* 106 */, 0x3993357D536D1BC4LL /* 107 */,
0x38A81AC21DB6FF8BLL /* 108 */, 0x47C4FBF17D6016BFLL /* 109 */,
0x1E0FAADD7667E3F5LL /* 110 */, 0x7ABCFF62938BEB96LL /* 111 */,
0xA78DAD948FC179C9LL /* 112 */, 0x8F1F98B72911E50DLL /* 113 */,
0x61E48EAE27121A91LL /* 114 */, 0x4D62F7AD31859808LL /* 115 */,
0xECEBA345EF5CEAEBLL /* 116 */, 0xF5CEB25EBC9684CELL /* 117 */,
0xF633E20CB7F76221LL /* 118 */, 0xA32CDF06AB8293E4LL /* 119 */,
0x985A202CA5EE2CA4LL /* 120 */, 0xCF0B8447CC8A8FB1LL /* 121 */,
0x9F765244979859A3LL /* 122 */, 0xA8D516B1A1240017LL /* 123 */,
0x0BD7BA3EBB5DC726LL /* 124 */, 0xE54BCA55B86ADB39LL /* 125 */,
0x1D7A3AFD6C478063LL /* 126 */, 0x519EC608E7669EDDLL /* 127 */,
0x0E5715A2D149AA23LL /* 128 */, 0x177D4571848FF194LL /* 129 */,
0xEEB55F3241014C22LL /* 130 */, 0x0F5E5CA13A6E2EC2LL /* 131 */,
0x8029927B75F5C361LL /* 132 */, 0xAD139FABC3D6E436LL /* 133 */,
0x0D5DF1A94CCF402FLL /* 134 */, 0x3E8BD948BEA5DFC8LL /* 135 */,
0xA5A0D357BD3FF77ELL /* 136 */, 0xA2D12E251F74F645LL /* 137 */,
0x66FD9E525E81A082LL /* 138 */, 0x2E0C90CE7F687A49LL /* 139 */,
0xC2E8BCBEBA973BC5LL /* 140 */, 0x000001BCE509745FLL /* 141 */,
0x423777BBE6DAB3D6LL /* 142 */, 0xD1661C7EAEF06EB5LL /* 143 */,
0xA1781F354DAACFD8LL /* 144 */, 0x2D11284A2B16AFFCLL /* 145 */,
0xF1FC4F67FA891D1FLL /* 146 */, 0x73ECC25DCB920ADALL /* 147 */,
0xAE610C22C2A12651LL /* 148 */, 0x96E0A810D356B78ALL /* 149 */,
0x5A9A381F2FE7870FLL /* 150 */, 0xD5AD62EDE94E5530LL /* 151 */,
0xD225E5E8368D1427LL /* 152 */, 0x65977B70C7AF4631LL /* 153 */,
0x99F889B2DE39D74FLL /* 154 */, 0x233F30BF54E1D143LL /* 155 */,
0x9A9675D3D9A63C97LL /* 156 */, 0x5470554FF334F9A8LL /* 157 */,
0x166ACB744A4F5688LL /* 158 */, 0x70C74CAAB2E4AEADLL /* 159 */,
0xF0D091646F294D12LL /* 160 */, 0x57B82A89684031D1LL /* 161 */,
0xEFD95A5A61BE0B6BLL /* 162 */, 0x2FBD12E969F2F29ALL /* 163 */,
0x9BD37013FEFF9FE8LL /* 164 */, 0x3F9B0404D6085A06LL /* 165 */,
0x4940C1F3166CFE15LL /* 166 */, 0x09542C4DCDF3DEFBLL /* 167 */,
0xB4C5218385CD5CE3LL /* 168 */, 0xC935B7DC4462A641LL /* 169 */,
0x3417F8A68ED3B63FLL /* 170 */, 0xB80959295B215B40LL /* 171 */,
0xF99CDAEF3B8C8572LL /* 172 */, 0x018C0614F8FCB95DLL /* 173 */,
0x1B14ACCD1A3ACDF3LL /* 174 */, 0x84D471F200BB732DLL /* 175 */,
0xC1A3110E95E8DA16LL /* 176 */, 0x430A7220BF1A82B8LL /* 177 */,
0xB77E090D39DF210ELL /* 178 */, 0x5EF4BD9F3CD05E9DLL /* 179 */,
0x9D4FF6DA7E57A444LL /* 180 */, 0xDA1D60E183D4A5F8LL /* 181 */,
0xB287C38417998E47LL /* 182 */, 0xFE3EDC121BB31886LL /* 183 */,
0xC7FE3CCC980CCBEFLL /* 184 */, 0xE46FB590189BFD03LL /* 185 */,
0x3732FD469A4C57DCLL /* 186 */, 0x7EF700A07CF1AD65LL /* 187 */,
0x59C64468A31D8859LL /* 188 */, 0x762FB0B4D45B61F6LL /* 189 */,
0x155BAED099047718LL /* 190 */, 0x68755E4C3D50BAA6LL /* 191 */,
0xE9214E7F22D8B4DFLL /* 192 */, 0x2ADDBF532EAC95F4LL /* 193 */,
0x32AE3909B4BD0109LL /* 194 */, 0x834DF537B08E3450LL /* 195 */,
0xFA209DA84220728DLL /* 196 */, 0x9E691D9B9EFE23F7LL /* 197 */,
0x0446D288C4AE8D7FLL /* 198 */, 0x7B4CC524E169785BLL /* 199 */,
0x21D87F0135CA1385LL /* 200 */, 0xCEBB400F137B8AA5LL /* 201 */,
0x272E2B66580796BELL /* 202 */, 0x3612264125C2B0DELL /* 203 */,
0x057702BDAD1EFBB2LL /* 204 */, 0xD4BABB8EACF84BE9LL /* 205 */,
0x91583139641BC67BLL /* 206 */, 0x8BDC2DE08036E024LL /* 207 */,
0x603C8156F49F68EDLL /* 208 */, 0xF7D236F7DBEF5111LL /* 209 */,
0x9727C4598AD21E80LL /* 210 */, 0xA08A0896670A5FD7LL /* 211 */,
0xCB4A8F4309EBA9CBLL /* 212 */, 0x81AF564B0F7036A1LL /* 213 */,
0xC0B99AA778199ABDLL /* 214 */, 0x959F1EC83FC8E952LL /* 215 */,
0x8C505077794A81B9LL /* 216 */, 0x3ACAAF8F056338F0LL /* 217 */,
0x07B43F50627A6778LL /* 218 */, 0x4A44AB49F5ECCC77LL /* 219 */,
0x3BC3D6E4B679EE98LL /* 220 */, 0x9CC0D4D1CF14108CLL /* 221 */,
0x4406C00B206BC8A0LL /* 222 */, 0x82A18854C8D72D89LL /* 223 */,
0x67E366B35C3C432CLL /* 224 */, 0xB923DD61102B37F2LL /* 225 */,
0x56AB2779D884271DLL /* 226 */, 0xBE83E1B0FF1525AFLL /* 227 */,
0xFB7C65D4217E49A9LL /* 228 */, 0x6BDBE0E76D48E7D4LL /* 229 */,
0x08DF828745D9179ELL /* 230 */, 0x22EA6A9ADD53BD34LL /* 231 */,
0xE36E141C5622200ALL /* 232 */, 0x7F805D1B8CB750EELL /* 233 */,
0xAFE5C7A59F58E837LL /* 234 */, 0xE27F996A4FB1C23CLL /* 235 */,
0xD3867DFB0775F0D0LL /* 236 */, 0xD0E673DE6E88891ALL /* 237 */,
0x123AEB9EAFB86C25LL /* 238 */, 0x30F1D5D5C145B895LL /* 239 */,
0xBB434A2DEE7269E7LL /* 240 */, 0x78CB67ECF931FA38LL /* 241 */,
0xF33B0372323BBF9CLL /* 242 */, 0x52D66336FB279C74LL /* 243 */,
0x505F33AC0AFB4EAALL /* 244 */, 0xE8A5CD99A2CCE187LL /* 245 */,
0x534974801E2D30BBLL /* 246 */, 0x8D2D5711D5876D90LL /* 247 */,
0x1F1A412891BC038ELL /* 248 */, 0xD6E2E71D82E56648LL /* 249 */,
0x74036C3A497732B7LL /* 250 */, 0x89B67ED96361F5ABLL /* 251 */,
0xFFED95D8F1EA02A2LL /* 252 */, 0xE72B3BD61464D43DLL /* 253 */,
0xA6300F170BDC4820LL /* 254 */, 0xEBC18760ED78A77ALL /* 255 */,
0xE6A6BE5A05A12138LL /* 256 */, 0xB5A122A5B4F87C98LL /* 257 */,
0x563C6089140B6990LL /* 258 */, 0x4C46CB2E391F5DD5LL /* 259 */,
0xD932ADDBC9B79434LL /* 260 */, 0x08EA70E42015AFF5LL /* 261 */,
0xD765A6673E478CF1LL /* 262 */, 0xC4FB757EAB278D99LL /* 263 */,
0xDF11C6862D6E0692LL /* 264 */, 0xDDEB84F10D7F3B16LL /* 265 */,
0x6F2EF604A665EA04LL /* 266 */, 0x4A8E0F0FF0E0DFB3LL /* 267 */,
0xA5EDEEF83DBCBA51LL /* 268 */, 0xFC4F0A2A0EA4371ELL /* 269 */,
0xE83E1DA85CB38429LL /* 270 */, 0xDC8FF882BA1B1CE2LL /* 271 */,
0xCD45505E8353E80DLL /* 272 */, 0x18D19A00D4DB0717LL /* 273 */,
0x34A0CFEDA5F38101LL /* 274 */, 0x0BE77E518887CAF2LL /* 275 */,
0x1E341438B3C45136LL /* 276 */, 0xE05797F49089CCF9LL /* 277 */,
0xFFD23F9DF2591D14LL /* 278 */, 0x543DDA228595C5CDLL /* 279 */,
0x661F81FD99052A33LL /* 280 */, 0x8736E641DB0F7B76LL /* 281 */,
0x15227725418E5307LL /* 282 */, 0xE25F7F46162EB2FALL /* 283 */,
0x48A8B2126C13D9FELL /* 284 */, 0xAFDC541792E76EEALL /* 285 */,
0x03D912BFC6D1898FLL /* 286 */, 0x31B1AAFA1B83F51BLL /* 287 */,
0xF1AC2796E42AB7D9LL /* 288 */, 0x40A3A7D7FCD2EBACLL /* 289 */,
0x1056136D0AFBBCC5LL /* 290 */, 0x7889E1DD9A6D0C85LL /* 291 */,
0xD33525782A7974AALL /* 292 */, 0xA7E25D09078AC09BLL /* 293 */,
0xBD4138B3EAC6EDD0LL /* 294 */, 0x920ABFBE71EB9E70LL /* 295 */,
0xA2A5D0F54FC2625CLL /* 296 */, 0xC054E36B0B1290A3LL /* 297 */,
0xF6DD59FF62FE932BLL /* 298 */, 0x3537354511A8AC7DLL /* 299 */,
0xCA845E9172FADCD4LL /* 300 */, 0x84F82B60329D20DCLL /* 301 */,
0x79C62CE1CD672F18LL /* 302 */, 0x8B09A2ADD124642CLL /* 303 */,
0xD0C1E96A19D9E726LL /* 304 */, 0x5A786A9B4BA9500CLL /* 305 */,
0x0E020336634C43F3LL /* 306 */, 0xC17B474AEB66D822LL /* 307 */,
0x6A731AE3EC9BAAC2LL /* 308 */, 0x8226667AE0840258LL /* 309 */,
0x67D4567691CAECA5LL /* 310 */, 0x1D94155C4875ADB5LL /* 311 */,
0x6D00FD985B813FDFLL /* 312 */, 0x51286EFCB774CD06LL /* 313 */,
0x5E8834471FA744AFLL /* 314 */, 0xF72CA0AEE761AE2ELL /* 315 */,
0xBE40E4CDAEE8E09ALL /* 316 */, 0xE9970BBB5118F665LL /* 317 */,
0x726E4BEB33DF1964LL /* 318 */, 0x703B000729199762LL /* 319 */,
0x4631D816F5EF30A7LL /* 320 */, 0xB880B5B51504A6BELL /* 321 */,
0x641793C37ED84B6CLL /* 322 */, 0x7B21ED77F6E97D96LL /* 323 */,
0x776306312EF96B73LL /* 324 */, 0xAE528948E86FF3F4LL /* 325 */,
0x53DBD7F286A3F8F8LL /* 326 */, 0x16CADCE74CFC1063LL /* 327 */,
0x005C19BDFA52C6DDLL /* 328 */, 0x68868F5D64D46AD3LL /* 329 */,
0x3A9D512CCF1E186ALL /* 330 */, 0x367E62C2385660AELL /* 331 */,
0xE359E7EA77DCB1D7LL /* 332 */, 0x526C0773749ABE6ELL /* 333 */,
0x735AE5F9D09F734BLL /* 334 */, 0x493FC7CC8A558BA8LL /* 335 */,
0xB0B9C1533041AB45LL /* 336 */, 0x321958BA470A59BDLL /* 337 */,
0x852DB00B5F46C393LL /* 338 */, 0x91209B2BD336B0E5LL /* 339 */,
0x6E604F7D659EF19FLL /* 340 */, 0xB99A8AE2782CCB24LL /* 341 */,
0xCCF52AB6C814C4C7LL /* 342 */, 0x4727D9AFBE11727BLL /* 343 */,
0x7E950D0C0121B34DLL /* 344 */, 0x756F435670AD471FLL /* 345 */,
0xF5ADD442615A6849LL /* 346 */, 0x4E87E09980B9957ALL /* 347 */,
0x2ACFA1DF50AEE355LL /* 348 */, 0xD898263AFD2FD556LL /* 349 */,
0xC8F4924DD80C8FD6LL /* 350 */, 0xCF99CA3D754A173ALL /* 351 */,
0xFE477BACAF91BF3CLL /* 352 */, 0xED5371F6D690C12DLL /* 353 */,
0x831A5C285E687094LL /* 354 */, 0xC5D3C90A3708A0A4LL /* 355 */,
0x0F7F903717D06580LL /* 356 */, 0x19F9BB13B8FDF27FLL /* 357 */,
0xB1BD6F1B4D502843LL /* 358 */, 0x1C761BA38FFF4012LL /* 359 */,
0x0D1530C4E2E21F3BLL /* 360 */, 0x8943CE69A7372C8ALL /* 361 */,
0xE5184E11FEB5CE66LL /* 362 */, 0x618BDB80BD736621LL /* 363 */,
0x7D29BAD68B574D0BLL /* 364 */, 0x81BB613E25E6FE5BLL /* 365 */,
0x071C9C10BC07913FLL /* 366 */, 0xC7BEEB7909AC2D97LL /* 367 */,
0xC3E58D353BC5D757LL /* 368 */, 0xEB017892F38F61E8LL /* 369 */,
0xD4EFFB9C9B1CC21ALL /* 370 */, 0x99727D26F494F7ABLL /* 371 */,
0xA3E063A2956B3E03LL /* 372 */, 0x9D4A8B9A4AA09C30LL /* 373 */,
0x3F6AB7D500090FB4LL /* 374 */, 0x9CC0F2A057268AC0LL /* 375 */,
0x3DEE9D2DEDBF42D1LL /* 376 */, 0x330F49C87960A972LL /* 377 */,
0xC6B2720287421B41LL /* 378 */, 0x0AC59EC07C00369CLL /* 379 */,
0xEF4EAC49CB353425LL /* 380 */, 0xF450244EEF0129D8LL /* 381 */,
0x8ACC46E5CAF4DEB6LL /* 382 */, 0x2FFEAB63989263F7LL /* 383 */,
0x8F7CB9FE5D7A4578LL /* 384 */, 0x5BD8F7644E634635LL /* 385 */,
0x427A7315BF2DC900LL /* 386 */, 0x17D0C4AA2125261CLL /* 387 */,
0x3992486C93518E50LL /* 388 */, 0xB4CBFEE0A2D7D4C3LL /* 389 */,
0x7C75D6202C5DDD8DLL /* 390 */, 0xDBC295D8E35B6C61LL /* 391 */,
0x60B369D302032B19LL /* 392 */, 0xCE42685FDCE44132LL /* 393 */,
0x06F3DDB9DDF65610LL /* 394 */, 0x8EA4D21DB5E148F0LL /* 395 */,
0x20B0FCE62FCD496FLL /* 396 */, 0x2C1B912358B0EE31LL /* 397 */,
0xB28317B818F5A308LL /* 398 */, 0xA89C1E189CA6D2CFLL /* 399 */,
0x0C6B18576AAADBC8LL /* 400 */, 0xB65DEAA91299FAE3LL /* 401 */,
0xFB2B794B7F1027E7LL /* 402 */, 0x04E4317F443B5BEBLL /* 403 */,
0x4B852D325939D0A6LL /* 404 */, 0xD5AE6BEEFB207FFCLL /* 405 */,
0x309682B281C7D374LL /* 406 */, 0xBAE309A194C3B475LL /* 407 */,
0x8CC3F97B13B49F05LL /* 408 */, 0x98A9422FF8293967LL /* 409 */,
0x244B16B01076FF7CLL /* 410 */, 0xF8BF571C663D67EELL /* 411 */,
0x1F0D6758EEE30DA1LL /* 412 */, 0xC9B611D97ADEB9B7LL /* 413 */,
0xB7AFD5887B6C57A2LL /* 414 */, 0x6290AE846B984FE1LL /* 415 */,
0x94DF4CDEACC1A5FDLL /* 416 */, 0x058A5BD1C5483AFFLL /* 417 */,
0x63166CC142BA3C37LL /* 418 */, 0x8DB8526EB2F76F40LL /* 419 */,
0xE10880036F0D6D4ELL /* 420 */, 0x9E0523C9971D311DLL /* 421 */,
0x45EC2824CC7CD691LL /* 422 */, 0x575B8359E62382C9LL /* 423 */,
0xFA9E400DC4889995LL /* 424 */, 0xD1823ECB45721568LL /* 425 */,
0xDAFD983B8206082FLL /* 426 */, 0xAA7D29082386A8CBLL /* 427 */,
0x269FCD4403B87588LL /* 428 */, 0x1B91F5F728BDD1E0LL /* 429 */,
0xE4669F39040201F6LL /* 430 */, 0x7A1D7C218CF04ADELL /* 431 */,
0x65623C29D79CE5CELL /* 432 */, 0x2368449096C00BB1LL /* 433 */,
0xAB9BF1879DA503BALL /* 434 */, 0xBC23ECB1A458058ELL /* 435 */,
0x9A58DF01BB401ECCLL /* 436 */, 0xA070E868A85F143DLL /* 437 */,
0x4FF188307DF2239ELL /* 438 */, 0x14D565B41A641183LL /* 439 */,
0xEE13337452701602LL /* 440 */, 0x950E3DCF3F285E09LL /* 441 */,
0x59930254B9C80953LL /* 442 */, 0x3BF299408930DA6DLL /* 443 */,
0xA955943F53691387LL /* 444 */, 0xA15EDECAA9CB8784LL /* 445 */,
0x29142127352BE9A0LL /* 446 */, 0x76F0371FFF4E7AFBLL /* 447 */,
0x0239F450274F2228LL /* 448 */, 0xBB073AF01D5E868BLL /* 449 */,
0xBFC80571C10E96C1LL /* 450 */, 0xD267088568222E23LL /* 451 */,
0x9671A3D48E80B5B0LL /* 452 */, 0x55B5D38AE193BB81LL /* 453 */,
0x693AE2D0A18B04B8LL /* 454 */, 0x5C48B4ECADD5335FLL /* 455 */,
0xFD743B194916A1CALL /* 456 */, 0x2577018134BE98C4LL /* 457 */,
0xE77987E83C54A4ADLL /* 458 */, 0x28E11014DA33E1B9LL /* 459 */,
0x270CC59E226AA213LL /* 460 */, 0x71495F756D1A5F60LL /* 461 */,
0x9BE853FB60AFEF77LL /* 462 */, 0xADC786A7F7443DBFLL /* 463 */,
0x0904456173B29A82LL /* 464 */, 0x58BC7A66C232BD5ELL /* 465 */,
0xF306558C673AC8B2LL /* 466 */, 0x41F639C6B6C9772ALL /* 467 */,
0x216DEFE99FDA35DALL /* 468 */, 0x11640CC71C7BE615LL /* 469 */,
0x93C43694565C5527LL /* 470 */, 0xEA038E6246777839LL /* 471 */,
0xF9ABF3CE5A3E2469LL /* 472 */, 0x741E768D0FD312D2LL /* 473 */,
0x0144B883CED652C6LL /* 474 */, 0xC20B5A5BA33F8552LL /* 475 */,
0x1AE69633C3435A9DLL /* 476 */, 0x97A28CA4088CFDECLL /* 477 */,
0x8824A43C1E96F420LL /* 478 */, 0x37612FA66EEEA746LL /* 479 */,
0x6B4CB165F9CF0E5ALL /* 480 */, 0x43AA1C06A0ABFB4ALL /* 481 */,
0x7F4DC26FF162796BLL /* 482 */, 0x6CBACC8E54ED9B0FLL /* 483 */,
0xA6B7FFEFD2BB253ELL /* 484 */, 0x2E25BC95B0A29D4FLL /* 485 */,
0x86D6A58BDEF1388CLL /* 486 */, 0xDED74AC576B6F054LL /* 487 */,
0x8030BDBC2B45805DLL /* 488 */, 0x3C81AF70E94D9289LL /* 489 */,
0x3EFF6DDA9E3100DBLL /* 490 */, 0xB38DC39FDFCC8847LL /* 491 */,
0x123885528D17B87ELL /* 492 */, 0xF2DA0ED240B1B642LL /* 493 */,
0x44CEFADCD54BF9A9LL /* 494 */, 0x1312200E433C7EE6LL /* 495 */,
0x9FFCC84F3A78C748LL /* 496 */, 0xF0CD1F72248576BBLL /* 497 */,
0xEC6974053638CFE4LL /* 498 */, 0x2BA7B67C0CEC4E4CLL /* 499 */,
0xAC2F4DF3E5CE32EDLL /* 500 */, 0xCB33D14326EA4C11LL /* 501 */,
0xA4E9044CC77E58BCLL /* 502 */, 0x5F513293D934FCEFLL /* 503 */,
0x5DC9645506E55444LL /* 504 */, 0x50DE418F317DE40ALL /* 505 */,
0x388CB31A69DDE259LL /* 506 */, 0x2DB4A83455820A86LL /* 507 */,
0x9010A91E84711AE9LL /* 508 */, 0x4DF7F0B7B1498371LL /* 509 */,
0xD62A2EABC0977179LL /* 510 */, 0x22FAC097AA8D5C0ELL /* 511 */,
0xF49FCC2FF1DAF39BLL /* 512 */, 0x487FD5C66FF29281LL /* 513 */,
0xE8A30667FCDCA83FLL /* 514 */, 0x2C9B4BE3D2FCCE63LL /* 515 */,
0xDA3FF74B93FBBBC2LL /* 516 */, 0x2FA165D2FE70BA66LL /* 517 */,
0xA103E279970E93D4LL /* 518 */, 0xBECDEC77B0E45E71LL /* 519 */,
0xCFB41E723985E497LL /* 520 */, 0xB70AAA025EF75017LL /* 521 */,
0xD42309F03840B8E0LL /* 522 */, 0x8EFC1AD035898579LL /* 523 */,
0x96C6920BE2B2ABC5LL /* 524 */, 0x66AF4163375A9172LL /* 525 */,
0x2174ABDCCA7127FBLL /* 526 */, 0xB33CCEA64A72FF41LL /* 527 */,
0xF04A4933083066A5LL /* 528 */, 0x8D970ACDD7289AF5LL /* 529 */,
0x8F96E8E031C8C25ELL /* 530 */, 0xF3FEC02276875D47LL /* 531 */,
0xEC7BF310056190DDLL /* 532 */, 0xF5ADB0AEBB0F1491LL /* 533 */,
0x9B50F8850FD58892LL /* 534 */, 0x4975488358B74DE8LL /* 535 */,
0xA3354FF691531C61LL /* 536 */, 0x0702BBE481D2C6EELL /* 537 */,
0x89FB24057DEDED98LL /* 538 */, 0xAC3075138596E902LL /* 539 */,
0x1D2D3580172772EDLL /* 540 */, 0xEB738FC28E6BC30DLL /* 541 */,
0x5854EF8F63044326LL /* 542 */, 0x9E5C52325ADD3BBELL /* 543 */,
0x90AA53CF325C4623LL /* 544 */, 0xC1D24D51349DD067LL /* 545 */,
0x2051CFEEA69EA624LL /* 546 */, 0x13220F0A862E7E4FLL /* 547 */,
0xCE39399404E04864LL /* 548 */, 0xD9C42CA47086FCB7LL /* 549 */,
0x685AD2238A03E7CCLL /* 550 */, 0x066484B2AB2FF1DBLL /* 551 */,
0xFE9D5D70EFBF79ECLL /* 552 */, 0x5B13B9DD9C481854LL /* 553 */,
0x15F0D475ED1509ADLL /* 554 */, 0x0BEBCD060EC79851LL /* 555 */,
0xD58C6791183AB7F8LL /* 556 */, 0xD1187C5052F3EEE4LL /* 557 */,
0xC95D1192E54E82FFLL /* 558 */, 0x86EEA14CB9AC6CA2LL /* 559 */,
0x3485BEB153677D5DLL /* 560 */, 0xDD191D781F8C492ALL /* 561 */,
0xF60866BAA784EBF9LL /* 562 */, 0x518F643BA2D08C74LL /* 563 */,
0x8852E956E1087C22LL /* 564 */, 0xA768CB8DC410AE8DLL /* 565 */,
0x38047726BFEC8E1ALL /* 566 */, 0xA67738B4CD3B45AALL /* 567 */,
0xAD16691CEC0DDE19LL /* 568 */, 0xC6D4319380462E07LL /* 569 */,
0xC5A5876D0BA61938LL /* 570 */, 0x16B9FA1FA58FD840LL /* 571 */,
0x188AB1173CA74F18LL /* 572 */, 0xABDA2F98C99C021FLL /* 573 */,
0x3E0580AB134AE816LL /* 574 */, 0x5F3B05B773645ABBLL /* 575 */,
0x2501A2BE5575F2F6LL /* 576 */, 0x1B2F74004E7E8BA9LL /* 577 */,
0x1CD7580371E8D953LL /* 578 */, 0x7F6ED89562764E30LL /* 579 */,
0xB15926FF596F003DLL /* 580 */, 0x9F65293DA8C5D6B9LL /* 581 */,
0x6ECEF04DD690F84CLL /* 582 */, 0x4782275FFF33AF88LL /* 583 */,
0xE41433083F820801LL /* 584 */, 0xFD0DFE409A1AF9B5LL /* 585 */,
0x4325A3342CDB396BLL /* 586 */, 0x8AE77E62B301B252LL /* 587 */,
0xC36F9E9F6655615ALL /* 588 */, 0x85455A2D92D32C09LL /* 589 */,
0xF2C7DEA949477485LL /* 590 */, 0x63CFB4C133A39EBALL /* 591 */,
0x83B040CC6EBC5462LL /* 592 */, 0x3B9454C8FDB326B0LL /* 593 */,
0x56F56A9E87FFD78CLL /* 594 */, 0x2DC2940D99F42BC6LL /* 595 */,
0x98F7DF096B096E2DLL /* 596 */, 0x19A6E01E3AD852BFLL /* 597 */,
0x42A99CCBDBD4B40BLL /* 598 */, 0xA59998AF45E9C559LL /* 599 */,
0x366295E807D93186LL /* 600 */, 0x6B48181BFAA1F773LL /* 601 */,
0x1FEC57E2157A0A1DLL /* 602 */, 0x4667446AF6201AD5LL /* 603 */,
0xE615EBCACFB0F075LL /* 604 */, 0xB8F31F4F68290778LL /* 605 */,
0x22713ED6CE22D11ELL /* 606 */, 0x3057C1A72EC3C93BLL /* 607 */,
0xCB46ACC37C3F1F2FLL /* 608 */, 0xDBB893FD02AAF50ELL /* 609 */,
0x331FD92E600B9FCFLL /* 610 */, 0xA498F96148EA3AD6LL /* 611 */,
0xA8D8426E8B6A83EALL /* 612 */, 0xA089B274B7735CDCLL /* 613 */,
0x87F6B3731E524A11LL /* 614 */, 0x118808E5CBC96749LL /* 615 */,
0x9906E4C7B19BD394LL /* 616 */, 0xAFED7F7E9B24A20CLL /* 617 */,
0x6509EADEEB3644A7LL /* 618 */, 0x6C1EF1D3E8EF0EDELL /* 619 */,
0xB9C97D43E9798FB4LL /* 620 */, 0xA2F2D784740C28A3LL /* 621 */,
0x7B8496476197566FLL /* 622 */, 0x7A5BE3E6B65F069DLL /* 623 */,
0xF96330ED78BE6F10LL /* 624 */, 0xEEE60DE77A076A15LL /* 625 */,
0x2B4BEE4AA08B9BD0LL /* 626 */, 0x6A56A63EC7B8894ELL /* 627 */,
0x02121359BA34FEF4LL /* 628 */, 0x4CBF99F8283703FCLL /* 629 */,
0x398071350CAF30C8LL /* 630 */, 0xD0A77A89F017687ALL /* 631 */,
0xF1C1A9EB9E423569LL /* 632 */, 0x8C7976282DEE8199LL /* 633 */,
0x5D1737A5DD1F7ABDLL /* 634 */, 0x4F53433C09A9FA80LL /* 635 */,
0xFA8B0C53DF7CA1D9LL /* 636 */, 0x3FD9DCBC886CCB77LL /* 637 */,
0xC040917CA91B4720LL /* 638 */, 0x7DD00142F9D1DCDFLL /* 639 */,
0x8476FC1D4F387B58LL /* 640 */, 0x23F8E7C5F3316503LL /* 641 */,
0x032A2244E7E37339LL /* 642 */, 0x5C87A5D750F5A74BLL /* 643 */,
0x082B4CC43698992ELL /* 644 */, 0xDF917BECB858F63CLL /* 645 */,
0x3270B8FC5BF86DDALL /* 646 */, 0x10AE72BB29B5DD76LL /* 647 */,
0x576AC94E7700362BLL /* 648 */, 0x1AD112DAC61EFB8FLL /* 649 */,
0x691BC30EC5FAA427LL /* 650 */, 0xFF246311CC327143LL /* 651 */,
0x3142368E30E53206LL /* 652 */, 0x71380E31E02CA396LL /* 653 */,
0x958D5C960AAD76F1LL /* 654 */, 0xF8D6F430C16DA536LL /* 655 */,
0xC8FFD13F1BE7E1D2LL /* 656 */, 0x7578AE66004DDBE1LL /* 657 */,
0x05833F01067BE646LL /* 658 */, 0xBB34B5AD3BFE586DLL /* 659 */,
0x095F34C9A12B97F0LL /* 660 */, 0x247AB64525D60CA8LL /* 661 */,
0xDCDBC6F3017477D1LL /* 662 */, 0x4A2E14D4DECAD24DLL /* 663 */,
0xBDB5E6D9BE0A1EEBLL /* 664 */, 0x2A7E70F7794301ABLL /* 665 */,
0xDEF42D8A270540FDLL /* 666 */, 0x01078EC0A34C22C1LL /* 667 */,
0xE5DE511AF4C16387LL /* 668 */, 0x7EBB3A52BD9A330ALL /* 669 */,
0x77697857AA7D6435LL /* 670 */, 0x004E831603AE4C32LL /* 671 */,
0xE7A21020AD78E312LL /* 672 */, 0x9D41A70C6AB420F2LL /* 673 */,
0x28E06C18EA1141E6LL /* 674 */, 0xD2B28CBD984F6B28LL /* 675 */,
0x26B75F6C446E9D83LL /* 676 */, 0xBA47568C4D418D7FLL /* 677 */,
0xD80BADBFE6183D8ELL /* 678 */, 0x0E206D7F5F166044LL /* 679 */,
0xE258A43911CBCA3ELL /* 680 */, 0x723A1746B21DC0BCLL /* 681 */,
0xC7CAA854F5D7CDD3LL /* 682 */, 0x7CAC32883D261D9CLL /* 683 */,
0x7690C26423BA942CLL /* 684 */, 0x17E55524478042B8LL /* 685 */,
0xE0BE477656A2389FLL /* 686 */, 0x4D289B5E67AB2DA0LL /* 687 */,
0x44862B9C8FBBFD31LL /* 688 */, 0xB47CC8049D141365LL /* 689 */,
0x822C1B362B91C793LL /* 690 */, 0x4EB14655FB13DFD8LL /* 691 */,
0x1ECBBA0714E2A97BLL /* 692 */, 0x6143459D5CDE5F14LL /* 693 */,
0x53A8FBF1D5F0AC89LL /* 694 */, 0x97EA04D81C5E5B00LL /* 695 */,
0x622181A8D4FDB3F3LL /* 696 */, 0xE9BCD341572A1208LL /* 697 */,
0x1411258643CCE58ALL /* 698 */, 0x9144C5FEA4C6E0A4LL /* 699 */,
0x0D33D06565CF620FLL /* 700 */, 0x54A48D489F219CA1LL /* 701 */,
0xC43E5EAC6D63C821LL /* 702 */, 0xA9728B3A72770DAFLL /* 703 */,
0xD7934E7B20DF87EFLL /* 704 */, 0xE35503B61A3E86E5LL /* 705 */,
0xCAE321FBC819D504LL /* 706 */, 0x129A50B3AC60BFA6LL /* 707 */,
0xCD5E68EA7E9FB6C3LL /* 708 */, 0xB01C90199483B1C7LL /* 709 */,
0x3DE93CD5C295376CLL /* 710 */, 0xAED52EDF2AB9AD13LL /* 711 */,
0x2E60F512C0A07884LL /* 712 */, 0xBC3D86A3E36210C9LL /* 713 */,
0x35269D9B163951CELL /* 714 */, 0x0C7D6E2AD0CDB5FALL /* 715 */,
0x59E86297D87F5733LL /* 716 */, 0x298EF221898DB0E7LL /* 717 */,
0x55000029D1A5AA7ELL /* 718 */, 0x8BC08AE1B5061B45LL /* 719 */,
0xC2C31C2B6C92703ALL /* 720 */, 0x94CC596BAF25EF42LL /* 721 */,
0x0A1D73DB22540456LL /* 722 */, 0x04B6A0F9D9C4179ALL /* 723 */,
0xEFFDAFA2AE3D3C60LL /* 724 */, 0xF7C8075BB49496C4LL /* 725 */,
0x9CC5C7141D1CD4E3LL /* 726 */, 0x78BD1638218E5534LL /* 727 */,
0xB2F11568F850246ALL /* 728 */, 0xEDFABCFA9502BC29LL /* 729 */,
0x796CE5F2DA23051BLL /* 730 */, 0xAAE128B0DC93537CLL /* 731 */,
0x3A493DA0EE4B29AELL /* 732 */, 0xB5DF6B2C416895D7LL /* 733 */,
0xFCABBD25122D7F37LL /* 734 */, 0x70810B58105DC4B1LL /* 735 */,
0xE10FDD37F7882A90LL /* 736 */, 0x524DCAB5518A3F5CLL /* 737 */,
0x3C9E85878451255BLL /* 738 */, 0x4029828119BD34E2LL /* 739 */,
0x74A05B6F5D3CECCBLL /* 740 */, 0xB610021542E13ECALL /* 741 */,
0x0FF979D12F59E2ACLL /* 742 */, 0x6037DA27E4F9CC50LL /* 743 */,
0x5E92975A0DF1847DLL /* 744 */, 0xD66DE190D3E623FELL /* 745 */,
0x5032D6B87B568048LL /* 746 */, 0x9A36B7CE8235216ELL /* 747 */,
0x80272A7A24F64B4ALL /* 748 */, 0x93EFED8B8C6916F7LL /* 749 */,
0x37DDBFF44CCE1555LL /* 750 */, 0x4B95DB5D4B99BD25LL /* 751 */,
0x92D3FDA169812FC0LL /* 752 */, 0xFB1A4A9A90660BB6LL /* 753 */,
0x730C196946A4B9B2LL /* 754 */, 0x81E289AA7F49DA68LL /* 755 */,
0x64669A0F83B1A05FLL /* 756 */, 0x27B3FF7D9644F48BLL /* 757 */,
0xCC6B615C8DB675B3LL /* 758 */, 0x674F20B9BCEBBE95LL /* 759 */,
0x6F31238275655982LL /* 760 */, 0x5AE488713E45CF05LL /* 761 */,
0xBF619F9954C21157LL /* 762 */, 0xEABAC46040A8EAE9LL /* 763 */,
0x454C6FE9F2C0C1CDLL /* 764 */, 0x419CF6496412691CLL /* 765 */,
0xD3DC3BEF265B0F70LL /* 766 */, 0x6D0E60F5C3578A9ELL /* 767 */,
0x5B0E608526323C55LL /* 768 */, 0x1A46C1A9FA1B59F5LL /* 769 */,
0xA9E245A17C4C8FFALL /* 770 */, 0x65CA5159DB2955D7LL /* 771 */,
0x05DB0A76CE35AFC2LL /* 772 */, 0x81EAC77EA9113D45LL /* 773 */,
0x528EF88AB6AC0A0DLL /* 774 */, 0xA09EA253597BE3FFLL /* 775 */,
0x430DDFB3AC48CD56LL /* 776 */, 0xC4B3A67AF45CE46FLL /* 777 */,
0x4ECECFD8FBE2D05ELL /* 778 */, 0x3EF56F10B39935F0LL /* 779 */,
0x0B22D6829CD619C6LL /* 780 */, 0x17FD460A74DF2069LL /* 781 */,
0x6CF8CC8E8510ED40LL /* 782 */, 0xD6C824BF3A6ECAA7LL /* 783 */,
0x61243D581A817049LL /* 784 */, 0x048BACB6BBC163A2LL /* 785 */,
0xD9A38AC27D44CC32LL /* 786 */, 0x7FDDFF5BAAF410ABLL /* 787 */,
0xAD6D495AA804824BLL /* 788 */, 0xE1A6A74F2D8C9F94LL /* 789 */,
0xD4F7851235DEE8E3LL /* 790 */, 0xFD4B7F886540D893LL /* 791 */,
0x247C20042AA4BFDALL /* 792 */, 0x096EA1C517D1327CLL /* 793 */,
0xD56966B4361A6685LL /* 794 */, 0x277DA5C31221057DLL /* 795 */,
0x94D59893A43ACFF7LL /* 796 */, 0x64F0C51CCDC02281LL /* 797 */,
0x3D33BCC4FF6189DBLL /* 798 */, 0xE005CB184CE66AF1LL /* 799 */,
0xFF5CCD1D1DB99BEALL /* 800 */, 0xB0B854A7FE42980FLL /* 801 */,
0x7BD46A6A718D4B9FLL /* 802 */, 0xD10FA8CC22A5FD8CLL /* 803 */,
0xD31484952BE4BD31LL /* 804 */, 0xC7FA975FCB243847LL /* 805 */,
0x4886ED1E5846C407LL /* 806 */, 0x28CDDB791EB70B04LL /* 807 */,
0xC2B00BE2F573417FLL /* 808 */, 0x5C9590452180F877LL /* 809 */,
0x7A6BDDFFF370EB00LL /* 810 */, 0xCE509E38D6D9D6A4LL /* 811 */,
0xEBEB0F00647FA702LL /* 812 */, 0x1DCC06CF76606F06LL /* 813 */,
0xE4D9F28BA286FF0ALL /* 814 */, 0xD85A305DC918C262LL /* 815 */,
0x475B1D8732225F54LL /* 816 */, 0x2D4FB51668CCB5FELL /* 817 */,
0xA679B9D9D72BBA20LL /* 818 */, 0x53841C0D912D43A5LL /* 819 */,
0x3B7EAA48BF12A4E8LL /* 820 */, 0x781E0E47F22F1DDFLL /* 821 */,
0xEFF20CE60AB50973LL /* 822 */, 0x20D261D19DFFB742LL /* 823 */,
0x16A12B03062A2E39LL /* 824 */, 0x1960EB2239650495LL /* 825 */,
0x251C16FED50EB8B8LL /* 826 */, 0x9AC0C330F826016ELL /* 827 */,
0xED152665953E7671LL /* 828 */, 0x02D63194A6369570LL /* 829 */,
0x5074F08394B1C987LL /* 830 */, 0x70BA598C90B25CE1LL /* 831 */,
0x794A15810B9742F6LL /* 832 */, 0x0D5925E9FCAF8C6CLL /* 833 */,
0x3067716CD868744ELL /* 834 */, 0x910AB077E8D7731BLL /* 835 */,
0x6A61BBDB5AC42F61LL /* 836 */, 0x93513EFBF0851567LL /* 837 */,
0xF494724B9E83E9D5LL /* 838 */, 0xE887E1985C09648DLL /* 839 */,
0x34B1D3C675370CFDLL /* 840 */, 0xDC35E433BC0D255DLL /* 841 */,
0xD0AAB84234131BE0LL /* 842 */, 0x08042A50B48B7EAFLL /* 843 */,
0x9997C4EE44A3AB35LL /* 844 */, 0x829A7B49201799D0LL /* 845 */,
0x263B8307B7C54441LL /* 846 */, 0x752F95F4FD6A6CA6LL /* 847 */,
0x927217402C08C6E5LL /* 848 */, 0x2A8AB754A795D9EELL /* 849 */,
0xA442F7552F72943DLL /* 850 */, 0x2C31334E19781208LL /* 851 */,
0x4FA98D7CEAEE6291LL /* 852 */, 0x55C3862F665DB309LL /* 853 */,
0xBD0610175D53B1F3LL /* 854 */, 0x46FE6CB840413F27LL /* 855 */,
0x3FE03792DF0CFA59LL /* 856 */, 0xCFE700372EB85E8FLL /* 857 */,
0xA7BE29E7ADBCE118LL /* 858 */, 0xE544EE5CDE8431DDLL /* 859 */,
0x8A781B1B41F1873ELL /* 860 */, 0xA5C94C78A0D2F0E7LL /* 861 */,
0x39412E2877B60728LL /* 862 */, 0xA1265EF3AFC9A62CLL /* 863 */,
0xBCC2770C6A2506C5LL /* 864 */, 0x3AB66DD5DCE1CE12LL /* 865 */,
0xE65499D04A675B37LL /* 866 */, 0x7D8F523481BFD216LL /* 867 */,
0x0F6F64FCEC15F389LL /* 868 */, 0x74EFBE618B5B13C8LL /* 869 */,
0xACDC82B714273E1DLL /* 870 */, 0xDD40BFE003199D17LL /* 871 */,
0x37E99257E7E061F8LL /* 872 */, 0xFA52626904775AAALL /* 873 */,
0x8BBBF63A463D56F9LL /* 874 */, 0xF0013F1543A26E64LL /* 875 */,
0xA8307E9F879EC898LL /* 876 */, 0xCC4C27A4150177CCLL /* 877 */,
0x1B432F2CCA1D3348LL /* 878 */, 0xDE1D1F8F9F6FA013LL /* 879 */,
0x606602A047A7DDD6LL /* 880 */, 0xD237AB64CC1CB2C7LL /* 881 */,
0x9B938E7225FCD1D3LL /* 882 */, 0xEC4E03708E0FF476LL /* 883 */,
0xFEB2FBDA3D03C12DLL /* 884 */, 0xAE0BCED2EE43889ALL /* 885 */,
0x22CB8923EBFB4F43LL /* 886 */, 0x69360D013CF7396DLL /* 887 */,
0x855E3602D2D4E022LL /* 888 */, 0x073805BAD01F784CLL /* 889 */,
0x33E17A133852F546LL /* 890 */, 0xDF4874058AC7B638LL /* 891 */,
0xBA92B29C678AA14ALL /* 892 */, 0x0CE89FC76CFAADCDLL /* 893 */,
0x5F9D4E0908339E34LL /* 894 */, 0xF1AFE9291F5923B9LL /* 895 */,
0x6E3480F60F4A265FLL /* 896 */, 0xEEBF3A2AB29B841CLL /* 897 */,
0xE21938A88F91B4ADLL /* 898 */, 0x57DFEFF845C6D3C3LL /* 899 */,
0x2F006B0BF62CAAF2LL /* 900 */, 0x62F479EF6F75EE78LL /* 901 */,
0x11A55AD41C8916A9LL /* 902 */, 0xF229D29084FED453LL /* 903 */,
0x42F1C27B16B000E6LL /* 904 */, 0x2B1F76749823C074LL /* 905 */,
0x4B76ECA3C2745360LL /* 906 */, 0x8C98F463B91691BDLL /* 907 */,
0x14BCC93CF1ADE66ALL /* 908 */, 0x8885213E6D458397LL /* 909 */,
0x8E177DF0274D4711LL /* 910 */, 0xB49B73B5503F2951LL /* 911 */,
0x10168168C3F96B6BLL /* 912 */, 0x0E3D963B63CAB0AELL /* 913 */,
0x8DFC4B5655A1DB14LL /* 914 */, 0xF789F1356E14DE5CLL /* 915 */,
0x683E68AF4E51DAC1LL /* 916 */, 0xC9A84F9D8D4B0FD9LL /* 917 */,
0x3691E03F52A0F9D1LL /* 918 */, 0x5ED86E46E1878E80LL /* 919 */,
0x3C711A0E99D07150LL /* 920 */, 0x5A0865B20C4E9310LL /* 921 */,
0x56FBFC1FE4F0682ELL /* 922 */, 0xEA8D5DE3105EDF9BLL /* 923 */,
0x71ABFDB12379187ALL /* 924 */, 0x2EB99DE1BEE77B9CLL /* 925 */,
0x21ECC0EA33CF4523LL /* 926 */, 0x59A4D7521805C7A1LL /* 927 */,
0x3896F5EB56AE7C72LL /* 928 */, 0xAA638F3DB18F75DCLL /* 929 */,
0x9F39358DABE9808ELL /* 930 */, 0xB7DEFA91C00B72ACLL /* 931 */,
0x6B5541FD62492D92LL /* 932 */, 0x6DC6DEE8F92E4D5BLL /* 933 */,
0x353F57ABC4BEEA7ELL /* 934 */, 0x735769D6DA5690CELL /* 935 */,
0x0A234AA642391484LL /* 936 */, 0xF6F9508028F80D9DLL /* 937 */,
0xB8E319A27AB3F215LL /* 938 */, 0x31AD9C1151341A4DLL /* 939 */,
0x773C22A57BEF5805LL /* 940 */, 0x45C7561A07968633LL /* 941 */,
0xF913DA9E249DBE36LL /* 942 */, 0xDA652D9B78A64C68LL /* 943 */,
0x4C27A97F3BC334EFLL /* 944 */, 0x76621220E66B17F4LL /* 945 */,
0x967743899ACD7D0BLL /* 946 */, 0xF3EE5BCAE0ED6782LL /* 947 */,
0x409F753600C879FCLL /* 948 */, 0x06D09A39B5926DB6LL /* 949 */,
0x6F83AEB0317AC588LL /* 950 */, 0x01E6CA4A86381F21LL /* 951 */,
0x66FF3462D19F3025LL /* 952 */, 0x72207C24DDFD3BFBLL /* 953 */,
0x4AF6B6D3E2ECE2EBLL /* 954 */, 0x9C994DBEC7EA08DELL /* 955 */,
0x49ACE597B09A8BC4LL /* 956 */, 0xB38C4766CF0797BALL /* 957 */,
0x131B9373C57C2A75LL /* 958 */, 0xB1822CCE61931E58LL /* 959 */,
0x9D7555B909BA1C0CLL /* 960 */, 0x127FAFDD937D11D2LL /* 961 */,
0x29DA3BADC66D92E4LL /* 962 */, 0xA2C1D57154C2ECBCLL /* 963 */,
0x58C5134D82F6FE24LL /* 964 */, 0x1C3AE3515B62274FLL /* 965 */,
0xE907C82E01CB8126LL /* 966 */, 0xF8ED091913E37FCBLL /* 967 */,
0x3249D8F9C80046C9LL /* 968 */, 0x80CF9BEDE388FB63LL /* 969 */,
0x1881539A116CF19ELL /* 970 */, 0x5103F3F76BD52457LL /* 971 */,
0x15B7E6F5AE47F7A8LL /* 972 */, 0xDBD7C6DED47E9CCFLL /* 973 */,
0x44E55C410228BB1ALL /* 974 */, 0xB647D4255EDB4E99LL /* 975 */,
0x5D11882BB8AAFC30LL /* 976 */, 0xF5098BBB29D3212ALL /* 977 */,
0x8FB5EA14E90296B3LL /* 978 */, 0x677B942157DD025ALL /* 979 */,
0xFB58E7C0A390ACB5LL /* 980 */, 0x89D3674C83BD4A01LL /* 981 */,
0x9E2DA4DF4BF3B93BLL /* 982 */, 0xFCC41E328CAB4829LL /* 983 */,
0x03F38C96BA582C52LL /* 984 */, 0xCAD1BDBD7FD85DB2LL /* 985 */,
0xBBB442C16082AE83LL /* 986 */, 0xB95FE86BA5DA9AB0LL /* 987 */,
0xB22E04673771A93FLL /* 988 */, 0x845358C9493152D8LL /* 989 */,
0xBE2A488697B4541ELL /* 990 */, 0x95A2DC2DD38E6966LL /* 991 */,
0xC02C11AC923C852BLL /* 992 */, 0x2388B1990DF2A87BLL /* 993 */,
0x7C8008FA1B4F37BELL /* 994 */, 0x1F70D0C84D54E503LL /* 995 */,
0x5490ADEC7ECE57D4LL /* 996 */, 0x002B3C27D9063A3ALL /* 997 */,
0x7EAEA3848030A2BFLL /* 998 */, 0xC602326DED2003C0LL /* 999 */,
0x83A7287D69A94086LL /* 1000 */, 0xC57A5FCB30F57A8ALL /* 1001 */,
0xB56844E479EBE779LL /* 1002 */, 0xA373B40F05DCBCE9LL /* 1003 */,
0xD71A786E88570EE2LL /* 1004 */, 0x879CBACDBDE8F6A0LL /* 1005 */,
0x976AD1BCC164A32FLL /* 1006 */, 0xAB21E25E9666D78BLL /* 1007 */,
0x901063AAE5E5C33CLL /* 1008 */, 0x9818B34448698D90LL /* 1009 */,
0xE36487AE3E1E8ABBLL /* 1010 */, 0xAFBDF931893BDCB4LL /* 1011 */,
0x6345A0DC5FBBD519LL /* 1012 */, 0x8628FE269B9465CALL /* 1013 */,
0x1E5D01603F9C51ECLL /* 1014 */, 0x4DE44006A15049B7LL /* 1015 */,
0xBF6C70E5F776CBB1LL /* 1016 */, 0x411218F2EF552BEDLL /* 1017 */,
0xCB0C0708705A36A3LL /* 1018 */, 0xE74D14754F986044LL /* 1019 */,
0xCD56D9430EA8280ELL /* 1020 */, 0xC12591D7535F5065LL /* 1021 */,
0xC83223F1720AEF96LL /* 1022 */, 0xC3A0396F7363A51FLL /* 1023 */
};
/* The following macro denotes that an optimization */
/* for Alpha is required. It is used only for */
/* optimization of time. Otherwise it does nothing. */
#ifdef _LP64
#define OPTIMIZE_FOR_LP64
#endif
/* NOTE that this code is NOT FULLY OPTIMIZED for any */
/* machine. Assembly code might be much faster on some */
/* machines, especially if the code is compiled with */
/* gcc. */
/* The number of passes of the hash function. */
/* Three passes are recommended. */
/* Use four passes when you need extra security. */
/* Must be at least three. */
#define PASSES 3
#define T1 (table)
#define T2 (table+256)
#define T3 (table+(256*2))
#define T4 (table+(256*3))
#define SAVE_ABC do { \
aa = a; \
bb = b; \
cc = c; \
} while (/*CONSTCOND*/0)
#ifdef OPTIMIZE_FOR_LP64
/* This is the official definition of round */
#define ROUND(a,b,c,x,mul) do { \
c ^= x; \
a -= T1[((c)>>(0*8))&0xFF] ^ T2[((c)>>(2*8))&0xFF] ^ \
T3[((c)>>(4*8))&0xFF] ^ T4[((c)>>(6*8))&0xFF] ; \
b += T4[((c)>>(1*8))&0xFF] ^ T3[((c)>>(3*8))&0xFF] ^ \
T2[((c)>>(5*8))&0xFF] ^ T1[((c)>>(7*8))&0xFF] ; \
b *= mul; \
} while (/*CONSTCOND*/ 0)
#else
/* This code works faster when compiled on 32-bit machines */
/* (but works slower on Alpha) */
#define ROUND(a,b,c,x,mul) do { \
c ^= x; \
a -= T1[(uint8_t)(c)] ^ \
T2[(const uint8_t)(((uint32_t)(c))>>(2*8))] ^ \
T3[(const uint8_t)((c)>>(4*8))] ^ \
T4[(const uint8_t)(((uint32_t)((c)>>(4*8)))>>(2*8))] ; \
b += T4[(uint8_t)(((uint32_t)(c))>>(1*8))] ^ \
T3[(const uint8_t)(((uint32_t)(c))>>(3*8))] ^ \
T2[(const uint8_t)(((uint32_t)((c)>>(4*8)))>>(1*8))] ^ \
T1[(const uint8_t)(((uint32_t)((c)>>(4*8)))>>(3*8))]; \
b *= mul; \
} while (/*CONSTCOND*/0)
#endif
#define PASS(a,b,c,mul) do { \
ROUND(a,b,c,x0,mul); \
ROUND(b,c,a,x1,mul); \
ROUND(c,a,b,x2,mul); \
ROUND(a,b,c,x3,mul); \
ROUND(b,c,a,x4,mul); \
ROUND(c,a,b,x5,mul); \
ROUND(a,b,c,x6,mul); \
ROUND(b,c,a,x7,mul); \
} while (/*CONSTCOND*/ 0)
#define KEY_SCHEDULE do { \
x0 -= x7 ^ (uint64_t)0xA5A5A5A5A5A5A5A5LL; \
x1 ^= x0; \
x2 += x1; \
x3 -= x2 ^ ((~x1)<<19); \
x4 ^= x3; \
x5 += x4; \
x6 -= x5 ^ ((~x4)>>23); \
x7 ^= x6; \
x0 += x7; \
x1 -= x0 ^ ((~x7)<<19); \
x2 ^= x1; \
x3 += x2; \
x4 -= x3 ^ ((~x2)>>23); \
x5 ^= x4; \
x6 += x5; \
x7 -= x6 ^ (uint64_t)0x0123456789ABCDEFLL; \
} while (/*CONSTCOND*/ 0)
#define FEEDFORWARD do { \
a ^= aa; \
b -= bb; \
c += cc; \
} while (/*CONSTCOND*/ 0)
#ifdef OPTIMIZE_FOR_LP64
/* The loop is unrolled: works better on Alpha */
#define COMPRESS do { \
SAVE_ABC; \
PASS(a,b,c,5); \
KEY_SCHEDULE; \
PASS(c,a,b,7); \
KEY_SCHEDULE; \
PASS(b,c,a,9); \
for (pass_no = 3; pass_no < PASSES; pass_no++) { \
KEY_SCHEDULE; \
PASS(a,b,c,9); \
tmpa = a; a = c; c = b; b = tmpa; \
} \
FEEDFORWARD; \
} while (/*CONSTCOND*/ 0)
#else
/* loop: works better on PC and Sun (smaller cache?) */
#define COMPRESS do { \
SAVE_ABC; \
for (pass_no = 0; pass_no < PASSES; pass_no++) { \
if (pass_no != 0) { \
KEY_SCHEDULE; \
} \
PASS(a,b,c,(pass_no==0?5:pass_no==1?7:9)); \
tmpa = a; a = c; c = b; b = tmpa; \
} \
FEEDFORWARD; \
} while (/*CONSTCOND*/0)
#endif
#define TIGER_COMPRESS_MACRO(str, state) do { \
uint64_t a, b, c, tmpa; \
uint64_t aa, bb, cc; \
uint64_t x0, x1, x2, x3, x4, x5, x6, x7; \
int pass_no; \
\
a = state[0]; \
b = state[1]; \
c = state[2]; \
\
x0 = str[0]; x1 = str[1]; x2 = str[2]; x3 = str[3]; \
x4 = str[4]; x5 = str[5]; x6 = str[6]; x7 = str[7]; \
\
COMPRESS; \
\
state[0] = a; \
state[1] = b; \
state[2] = c; \
} while (/*CONSTCOND*/ 0)
#ifdef OPTIMIZE_FOR_LP64
/* The compress function is inlined: works better on Alpha. */
/* Still leaves the function above in the code, in case some other */
/* module calls it directly. */
#define tiger_compress(str, state) \
TIGER_COMPRESS_MACRO(((const uint64_t*)(const void *)str), ((uint64_t*)(void *)state))
#else
/* The compress function is a function. Requires smaller cache? */
static void
tiger_compress(const uint64_t *str, uint64_t state[3])
{
TIGER_COMPRESS_MACRO(((const uint64_t*)str), ((uint64_t*)state));
}
#endif
/* weird function to format 8 raw bytes to 16 formatted hex chars */
static void
sprint_uint64(char *buf, uint64_t val)
{
static const char hexdigits[] = "0123456789abcdef";
int indian = 1;
int i;
for (i = 0; i < 8; ++i) {
if (IS_LITTLE_ENDIAN(indian)) {
buf[2 * (7 - i)] = hexdigits[(val >> (56 - 8 * i + 4)) & 15];
buf[(2 * (7 - i)) + 1] = hexdigits[(val >> (56 - 8 * i)) & 15];
} else {
buf[2 * i] = hexdigits[(val >> (56 - 8 * i + 4)) & 15];
buf[(2 * i) + 1] = hexdigits[(val >> (56 - 8 * i)) & 15];
}
}
}
/* common function to initialise context */
static void
initcontext(TIGER_CTX *ctx, uint8_t pad)
{
(void) memset(ctx, 0x0, sizeof(*ctx));
ctx->ctx[0] = 0x0123456789ABCDEFLL;
ctx->ctx[1] = 0xFEDCBA9876543210LL;
ctx->ctx[2] = 0xF096A5B4C3B2E187LL;
ctx->init = 1;
ctx->pad = pad;
}
/* set the version number (0 is same as 1 for Tiger) */
static int
setversion(TIGER_CTX *ctx, int version)
{
switch(version) {
case 0:
case 1:
initcontext(ctx, 0x01);
break;
case 2:
initcontext(ctx, 0x80);
break;
default:
(void) fprintf(stderr, "unknown version %d\n", version);
return 0;
}
return 1;
}
/*****************************************************************************/
void
TIGER_Init(TIGER_CTX *ctx)
{
if (ctx) {
initcontext(ctx, 0x01);
}
}
void
TIGER2_Init(TIGER_CTX *ctx)
{
if (ctx) {
initcontext(ctx, 0x80);
}
}
void
TIGER_Update(TIGER_CTX *ctx, const void *data, size_t length)
{
const uint64_t *str = (const uint64_t *)data;
uint64_t i;
uint64_t j;
union {
uint8_t temp8[64];
uint64_t temp64[8];
} u;
int indian = 1;
if (ctx == NULL || data == NULL) {
return;
}
for(i = length; i >= 64; i -= 64) {
if (IS_BIG_ENDIAN(indian)) {
for (j = 0; j < 64; j++) {
u.temp8[j ^ 7] = ((const uint8_t *)(const void *)str)[j];
}
tiger_compress(u.temp64, ctx->ctx);
} else {
tiger_compress(str, ctx->ctx);
}
str += 8;
}
if (IS_BIG_ENDIAN(indian)) {
for (j = 0; j < i; j++) {
u.temp8[j ^ 7] = ((const uint8_t*)(const void *)str)[j];
}
u.temp8[j ^ 7] = ctx->pad;
for (j++; j&7; j++) {
u.temp8[j ^ 7] = 0;
}
} else {
for (j = 0; j < i; j++) {
u.temp8[j] = ((const uint8_t*)(const void *)str)[j];
}
u.temp8[j++] = ctx->pad;
for (; j&7; j++) {
u.temp8[j] = 0;
}
}
if (j > 56) {
for (; j < 64; j++) {
u.temp8[j] = 0;
}
tiger_compress(u.temp64, ctx->ctx);
j = 0;
}
for (; j < 56; j++) {
u.temp8[j] = 0;
}
((uint64_t *)(void *)(&(u.temp8[56])))[0] = ((uint64_t)length) << 3;
tiger_compress(u.temp64, ctx->ctx);
}
void
TIGER_Final(uint8_t *digest, TIGER_CTX *ctx)
{
uint64_t le[3];
int indian = 1;
int i;
if (digest == NULL || ctx == NULL) {
return;
}
if (!ctx->init) {
TIGER_Init(ctx);
TIGER_Update(ctx, NULL, 0);
}
if (IS_LITTLE_ENDIAN(indian)) {
for (i = 0; i < 3; ++i) {
le[i] = (uint64_t)BSWAP64(ctx->ctx[i]);
}
(void) memcpy(digest, le, 3 * sizeof(le[0]));
} else {
(void) memcpy(digest, ctx->ctx, 3 * sizeof(ctx->ctx[0]));
}
}
char *
TIGER_End(TIGER_CTX *ctx, char *buf)
{
int i;
if (ctx == NULL) {
return NULL;
}
if (buf == NULL && (buf = calloc(1, 49)) == NULL) {
return NULL;
}
if (!ctx->init) {
TIGER_Init(ctx);
TIGER_Update(ctx, NULL, 0);
}
for (i = 0; i < 3; ++i) {
sprint_uint64(buf + i * 16, ctx->ctx[i]);
}
buf[16 * i] = 0x0;
return buf;
}
char *
TIGER_File(char *filename, char *buf, int version)
{
TIGER_CTX ctx;
uint8_t buffer[BUFSIZ];
ssize_t num;
int fd;
int oerrno;
if (filename == NULL || buf == NULL || !setversion(&ctx, version)) {
return NULL;
}
if ((fd = open(filename, O_RDONLY)) < 0) {
return NULL;
}
while ((num = read(fd, buffer, sizeof(buffer))) > 0) {
TIGER_Update(&ctx, buffer, (size_t)num);
}
oerrno = errno;
close(fd);
errno = oerrno;
return (num < 0) ? NULL : TIGER_End(&ctx, buf);
}
char *
TIGER_Data(const uint8_t *data, size_t len, char *buf, int version)
{
TIGER_CTX ctx;
if (data == NULL || buf == NULL || !setversion(&ctx, version)) {
return NULL;
}
TIGER_Update(&ctx, data, len);
return TIGER_End(&ctx, buf);
}

View File

@@ -0,0 +1,65 @@
/*-
* Copyright (c) 2005-2011 Alistair Crooks <agc@NetBSD.org>
* 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 ``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 TIGER_H_
#define TIGER_H_
#include <sys/types.h>
#include <inttypes.h>
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
#define TIGER_DIGEST_LENGTH 24
#define TIGER_DIGEST_STRING_LENGTH ((TIGER_DIGEST_LENGTH * 2) + 1)
typedef struct TIGER_CTX {
uint64_t ctx[3];
int init;
uint8_t pad;
} TIGER_CTX;
__BEGIN_DECLS
void TIGER_Init(TIGER_CTX *);
void TIGER2_Init(TIGER_CTX *);
void TIGER_Update(TIGER_CTX *, const void *, size_t);
void TIGER_Final(uint8_t *, TIGER_CTX *);
char *TIGER_End(TIGER_CTX *, char *);
char *TIGER_File(char *, char *, int);
char *TIGER_Data(const uint8_t *, size_t, char *, int);
__END_DECLS
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,121 @@
#ifndef PORTABLE_C__
#define PORTABLE_C__
/* Definition of minimum-width integer types
*
* u8 -> unsigned integer type, at least 8 bits, equivalent to unsigned char
* u16 -> unsigned integer type, at least 16 bits
* u32 -> unsigned integer type, at least 32 bits
*
* s8, s16, s32 -> signed counterparts of u8, u16, u32
*
* Always use macro's T8(), T16() or T32() to obtain exact-width results,
* i.e., to specify the size of the result of each expression.
*/
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
#define ONE8 0xffU
#define ONE16 0xffffU
#define ONE32 0xffffffffUL
#define LL(v) (v##ULL)
#define ONE64 LL(0xffffffffffffffff)
#define T8(x) ((x) & ONE8)
#define T16(x) ((x) & ONE16)
#define T32(x) ((x) & ONE32)
#define T64(x) ((x) & ONE64)
#define ROTR64(v, n) (((v) >> (n)) | T64((v) << (64 - (n))))
/*
* Note: the test is used to detect native 64-bit architectures;
* if the unsigned long is strictly greater than 32-bit, it is
* assumed to be at least 64-bit. This will not work correctly
* on (old) 36-bit architectures (PDP-11 for instance).
*
* On non-64-bit architectures, "long long" is used.
*/
/*
* U8TO32_BIG(c) returns the 32-bit value stored in big-endian convention
* in the unsigned char array pointed to by c.
*/
#define U8TO32_BIG(c) (((u32)T8(*(c)) << 24) | ((u32)T8(*((c) + 1)) << 16) | ((u32)T8(*((c) + 2)) << 8) | ((u32)T8(*((c) + 3))))
/*
* U8TO32_LITTLE(c) returns the 32-bit value stored in little-endian convention
* in the unsigned char array pointed to by c.
*/
#define U8TO32_LITTLE(c) (((u32)T8(*(c))) | ((u32)T8(*((c) + 1)) << 8) | (u32)T8(*((c) + 2)) << 16) | ((u32)T8(*((c) + 3)) << 24))
/*
* U8TO32_BIG(c, v) stores the 32-bit-value v in big-endian convention
* into the unsigned char array pointed to by c.
*/
#define U32TO8_BIG(c, v) do { u32 x = (v); u8 *d = (c); d[0] = T8(x >> 24); d[1] = T8(x >> 16); d[2] = T8(x >> 8); d[3] = T8(x); } while (0)
/*
* U8TO32_LITTLE(c, v) stores the 32-bit-value v in little-endian convention
* into the unsigned char array pointed to by c.
*/
#define U32TO8_LITTLE(c, v) do { u32 x = (v); u8 *d = (c); d[0] = T8(x); d[1] = T8(x >> 8); d[2] = T8(x >> 16); d[3] = T8(x >> 24); } while (0)
/*
* ROTL32(v, n) returns the value of the 32-bit unsigned value v after
* a rotation of n bits to the left. It might be replaced by the appropriate
* architecture-specific macro.
*
* It evaluates v and n twice.
*
* The compiler might emit a warning if n is the constant 0. The result
* is undefined if n is greater than 31.
*/
#define ROTL32(v, n) (T32((v) << (n)) | ((v) >> (32 - (n))))
/*
* Whirlpool-specific definitions.
*/
#define WHIRLPOOL_DIGEST_BYTES 64
#define WHIRLPOOL_DIGEST_BITS (8*WHIRLPOOL_DIGEST_BYTES) /* 512 */
#define WHIRLPOOL_WBLOCK_BYTES 64
#define WHIRLPOOL_WBLOCK_BITS (8*WHIRLPOOL_WBLOCK_BYTES) /* 512 */
#define WHIRLPOOL_LENGTH_BYTES 32
#define WHIRLPOOL_LENGTH_BITS (8*WHIRLPOOL_LENGTH_BYTES) /* 256 */
typedef struct whirlpool_context_t {
u8 bitLength[WHIRLPOOL_LENGTH_BYTES]; /* global number of hashed bits (256-bit counter) */
u8 buffer[WHIRLPOOL_WBLOCK_BYTES]; /* buffer of data to hash */
int bufferBits; /* current number of bits on the buffer */
int bufferPos; /* current (possibly incomplete) byte slot on the buffer */
u64 hash[WHIRLPOOL_DIGEST_BYTES/8]; /* the hashing state */
} whirlpool_context_t;
#ifndef __BEGIN_DECLS
# if defined(__cplusplus)
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
# else
# define __BEGIN_DECLS
# define __END_DECLS
# endif
#endif
__BEGIN_DECLS
void whirlpool_init(whirlpool_context_t *);
void whirlpool_update(whirlpool_context_t *, const unsigned char *, unsigned int);
void whirlpool_finalize(char *, whirlpool_context_t *);
char *whirlpool_end(whirlpool_context_t *, char *);
char *whirlpool_file(char *, char *);
__END_DECLS
#endif /* PORTABLE_C__ */