diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ef9281f --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.kdev4 +*.o +*.a +sha +sha-test diff --git a/COPYRIGHT b/COPYRIGHT new file mode 100644 index 0000000..bac1fc4 --- /dev/null +++ b/COPYRIGHT @@ -0,0 +1,24 @@ + Copyright (c) 2014-2015 Carsten Larsen + Copyright (c) 2001-2003 Allan Saddi + 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 ALLAN SADDI AND HIS 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 ALLAN SADDI OR HIS 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. diff --git a/ChangeLog b/ChangeLog index ccfdee4..007062f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2015-06-06 Carsten Larsen + + * Native Amiga version + * Version 1.0.5 Amiga version released + +2014-11-21 Carsten Larsen + * Initial Amiga version + * Version 1.0.4 for AmigaOS released + 2003-07-25 Allan Saddi * Minor tweaks to sha code to make more ANSI C compliant. diff --git a/Makefile b/Makefile index 1799cb5..10eae5c 100644 --- a/Makefile +++ b/Makefile @@ -1,55 +1,70 @@ -################################################################## -# Generic options -OPTIONS = -Dlint -DHAVE_CONFIG_H -I. +# Portable Makefile generated by configure -################################################################## -# Standard build -#CC = cc48 -CLIBS = -CFLAGS = -Wall -CONFIG = -I./def/ +all: sha sha1 sha256 sha384 sha512 test -################################################################## -# Amiga OS build -CCA = vc +aos68k -CLIBSA = -L$(VBCC)/PosixLib/AmigaOS3/ -CFLAGSA = -c99 -O2 -cpu=68000 -I$(VBCC)/PosixLib/include/ -CFLAGSA2 = -c99 -O2 -cpu=68020 -I$(VBCC)/PosixLib/include/ -CONFIGA = -I./aos/ +CC = m68k-amigaos-gcc +CFLAGS = -O2 -m68060 -DAOS3 -noixemul -I. -Wall -Werror -################################################################## -# Sources -PROGNAME = sha -PROGNAMETEST = shatest -SOURCES = sha.c sha1.c sha256.c sha384.c sha512.c getopt.c -SOURCESTEST = shatest.c sha1.c sha256.c sha384.c sha512.c getopt.c +platform.h: compiler.h + touch platform.h -HEADERS = sha1.h sha256.h sha384.h sha512.h version.h -OBJECTS = shatest.o sha.o sha1.o sha256.o sha384.o sha512.o getopt.o +sha1.h: platform.h + touch sha1.h -################################################################## -# Targets -all: sha shatest +sha256.h: platform.h + touch sha256.h -aos: aossha aossha2 aosshatest aosshatest2 +sha384.h: platform.h + touch sha384.h -sha: - ${CC} ${CFLAGS} ${OPTIONS} ${CONFIG} -o ${PROGNAME} ${SOURCES} ${CLIBS} +sha512.h: platform.h + touch sha512.h -shatest: - ${CC} ${CFLAGS} ${OPTIONS} ${CONFIG} -o ${PROGNAMETEST} ${SOURCESTEST} ${CLIBS} +sha.o: sha.c platform.h sha1.h sha256.h sha384.h sha512.h -aossha: - ${CCA} ${CFLAGSA} ${OPTIONS} ${CONFIGA} -o ${PROGNAME} ${SOURCES} ${CLIBSA} +sha1.o: sha1.c compiler.h sha1.h -aossha2: - ${CCA} ${CFLAGSA2} ${OPTIONS} ${CONFIGA} -o ${PROGNAME}2 ${SOURCES} ${CLIBSA} +sha1_amiga.o: sha1_amiga.c platform.h sha1.h -aosshatest: - ${CCA} ${CFLAGSA} ${OPTIONS} ${CONFIGA} -o ${PROGNAMETEST} ${SOURCESTEST} ${CLIBSA} +sha256.o: sha256.c compiler.h sha256.h -aosshatest2: - ${CCA} ${CFLAGSA2} ${OPTIONS} ${CONFIGA} -o ${PROGNAMETEST}2 ${SOURCESTEST} ${CLIBSA} +sha256_amiga.o: sha256_amiga.c platform.h sha256.h + +sha384.o: sha384.c compiler.h sha384.h + +sha384_amiga.o: sha384_amiga.c platform.h sha384.h + +sha512.o: sha512.c compiler.h sha512.h + +sha512_amiga.o: sha512_amiga.c platform.h sha512.h + +sha_amiga.o: sha_amiga.c platform.h + +sha_io.o: sha_io.c platform.h + +shatest.o: shatest.c platform.h sha1.h sha256.h sha384.h sha512.h + + +sha: sha.o sha1.o sha256.o sha384.o sha512.o sha_amiga.o sha_io.o + ${CC} ${CFLAGS} -o sha sha.o sha1.o sha256.o sha384.o sha512.o sha_amiga.o sha_io.o + +sha1: sha1.o sha1_amiga.o sha_io.o + ${CC} ${CFLAGS} -o sha1 sha1.o sha1_amiga.o sha_io.o + +sha256: sha256.o sha256_amiga.o sha_io.o + ${CC} ${CFLAGS} -o sha256 sha256.o sha256_amiga.o sha_io.o + +sha384: sha384.o sha384_amiga.o sha_io.o + ${CC} ${CFLAGS} -o sha384 sha384.o sha384_amiga.o sha_io.o + +sha512: sha512.o sha512_amiga.o sha_io.o + ${CC} ${CFLAGS} -o sha512 sha512.o sha512_amiga.o sha_io.o + +test: sha1.o sha256.o sha384.o sha512.o sha_io.o shatest.o + ${CC} ${CFLAGS} -o shatest sha1.o sha256.o sha384.o sha512.o sha_io.o shatest.o clean: - rm -f ${PROGNAME} ${PROGNAME}2 ${PROGNAMETEST} ${PROGNAMETEST}2 ${OBJECTS} + rm -f sha.o sha1.o sha1_amiga.o sha256.o sha256_amiga.o sha384.o sha384_amiga.o sha512.o sha512_amiga.o sha_amiga.o sha_io.o shatest.o sha shatest sha1 sha256 sha384 sha512 + +depend: + @echo Dependencies already done diff --git a/Makefile.gcc b/Makefile.gcc deleted file mode 100644 index 21a4924..0000000 --- a/Makefile.gcc +++ /dev/null @@ -1,55 +0,0 @@ -################################################################## -# Generic options -OPTIONS = -Dlint -DHAVE_CONFIG_H -I. - -################################################################## -# Standard build -#CC = cc47 -CLIBS = -CFLAGS = -Wall -CONFIG = -I./def/ - -################################################################## -# Amiga OS build -CCA = m68k-amigaos-gcc -CLIBSA = -CFLAGSA = -O2 -m68000 -resident -noixemul -s -Wall -CFLAGSA2 = -O2 -m68020 -resident -noixemul -s -Wall -CONFIGA = -I./aos/ - -################################################################## -# Sources -PROGNAME = sha -PROGNAMETEST = shatest -SOURCES = sha.c sha1.c sha256.c sha384.c sha512.c getopt.c -SOURCESTEST = shatest.c sha1.c sha256.c sha384.c sha512.c getopt.c - -HEADERS = sha1.h sha256.h sha384.h sha512.h version.h -OBJECTS = shatest.o sha.o sha1.o sha256.o sha384.o sha512.o getopt.o - -################################################################## -# Targets -all: sha shatest - -aos: aossha aossha2 aosshatest aosshatest2 - -sha: - ${CC} ${CFLAGS} ${OPTIONS} ${CONFIG} -o ${PROGNAME} ${SOURCES} ${CLIBS} - -shatest: - ${CC} ${CFLAGS} ${OPTIONS} ${CONFIG} -o ${PROGNAMETEST} ${SOURCESTEST} ${CLIBS} - -aossha: - ${CCA} ${CFLAGSA} ${OPTIONS} ${CONFIGA} -o ${PROGNAME} ${SOURCES} ${CLIBSA} - -aossha2: - ${CCA} ${CFLAGSA2} ${OPTIONS} ${CONFIGA} -o ${PROGNAME}2 ${SOURCES} ${CLIBSA} - -aosshatest: - ${CCA} ${CFLAGSA} ${OPTIONS} ${CONFIGA} -o ${PROGNAMETEST} ${SOURCESTEST} ${CLIBSA} - -aosshatest2: - ${CCA} ${CFLAGSA2} ${OPTIONS} ${CONFIGA} -o ${PROGNAMETEST}2 ${SOURCESTEST} ${CLIBSA} - -clean: - rm -f ${PROGNAME} ${PROGNAME}2 ${PROGNAMETEST} ${PROGNAMETEST}2 ${OBJECTS} diff --git a/builddist b/builddist new file mode 100644 index 0000000..89d7642 --- /dev/null +++ b/builddist @@ -0,0 +1,114 @@ +#!/bin/sh +# +# Copyright (c) 2015 Carsten Larsen +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# + +rm -Rf dist +mkdir dist + +sh configure AMIGA -m68000 +echo 'Building Amiga MC68000 binaries' +mkdir dist/68000 +make +cp sha dist/68000/ +cp sha1 dist/68000/ +cp sha256 dist/68000/ +cp sha384 dist/68000/ +cp sha512 dist/68000/ +cp shatest dist/68000/ +make clean +sh configure AMIGA -m68020 +echo 'Building Amiga MC68020 binaries' +mkdir dist/68020 +make +cp sha dist/68020/ +cp sha1 dist/68020/ +cp sha256 dist/68020/ +cp sha384 dist/68020/ +cp sha512 dist/68020/ +cp shatest dist/68020/ +make clean +sh configure AMIGA "-m68020 -m68881" +echo 'Building Amiga MC68020 FPU binaries' +mkdir dist/68020f +make +cp sha dist/68020f/ +cp sha1 dist/68020f/ +cp sha256 dist/68020f/ +cp sha384 dist/68020f/ +cp sha512 dist/68020f/ +cp shatest dist/68020f/ +make clean +sh configure AMIGA -m68030 +echo 'Building Amiga MC68030 binaries' +mkdir dist/68030 +make +cp sha dist/68030/ +cp sha1 dist/68030/ +cp sha256 dist/68030/ +cp sha384 dist/68030/ +cp sha512 dist/68030/ +cp shatest dist/68030/ +make clean +sh configure AMIGA "-m68030 -m68881" +echo 'Building Amiga MC68030 FPU binaries' +mkdir dist/68030f +make +cp sha dist/68030f/ +cp sha1 dist/68030f/ +cp sha256 dist/68030f/ +cp sha384 dist/68030f/ +cp sha512 dist/68030f/ +cp shatest dist/68030f/ +make clean +sh configure AMIGA -m68040 +echo 'Building Amiga MC68040 binaries' +mkdir dist/68040 +make +cp sha dist/68040/ +cp sha1 dist/68040/ +cp sha256 dist/68040/ +cp sha384 dist/68040/ +cp sha512 dist/68040/ +cp shatest dist/68040/ +make clean +sh configure AMIGA -m68060 +echo 'Building Amiga MC68060 binaries' +rm -Rf dist/68060 +mkdir dist/68060 +make +cp sha dist/68060/ +cp sha1 dist/68060/ +cp sha256 dist/68060/ +cp sha384 dist/68060/ +cp sha512 dist/68060/ +cp shatest dist/68060/ +make clean + +cp COPYRIGHT dist/ +cp ChangeLog dist/ +cp sha.guide dist/ + +echo 'Distribution build done.' diff --git a/compiler.h b/compiler.h new file mode 100644 index 0000000..d11863e --- /dev/null +++ b/compiler.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * 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 _SHA_COMPILER_H +#define _SHA_COMPILER_H + +#ifdef __GNUC__ +# if (__GNUC__ == 2 && __GNUC_MINOR__ == 95) +# include +# include +typedef u_int8_t uint8_t; +typedef u_int16_t uint16_t; +typedef u_int32_t uint32_t; +typedef u_int64_t uint64_t; +# endif +# ifdef AROS +# include +# endif +#endif + +#endif diff --git a/configure b/configure new file mode 100755 index 0000000..bf86520 --- /dev/null +++ b/configure @@ -0,0 +1,218 @@ +#!/bin/sh +# +# Copyright (c) 2015 Carsten Larsen +# Copyright (c) 2014 Poul-Henning Kamp +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# + +set -e + +################################################# +HDRS=' + compiler.h + platform.h + sha1.h + sha256.h + sha384.h + sha512.h +' +SRCS=' + sha.c + sha1.c + sha1_amiga.c + sha256.c + sha256_amiga.c + sha384.c + sha384_amiga.c + sha512.c + sha512_amiga.c + sha_amiga.c + sha_io.c + shatest.c +' +################################################# +SRCS1=' + sha.c + sha1.c + sha256.c + sha384.c + sha512.c + sha_amiga.c + sha_io.c +' +SRCS2=' + sha1.c + sha256.c + sha384.c + sha512.c + sha_io.c + shatest.c +' +SRCSS1=' + sha1.c + sha1_amiga.c + sha_io.c +' +SRCSS256=' + sha256.c + sha256_amiga.c + sha_io.c +' +SRCSS384=' + sha384.c + sha384_amiga.c + sha_io.c +' +SRCSS512=' + sha512.c + sha512_amiga.c + sha_io.c +' +################################################# + +if [ -n "$1" ] && [ $1 = "help" ] ; then + echo "Usage: sh configure [AMIGA|AROS] [flags]" + echo "Example: sh configure AMIGA -mc68020" + exit +fi + +if make -v 2>&1 | grep GNU > /dev/null 2>&1 ; then + echo "make is GNU make." + VALID=true +fi + +if $VALID ; then + ( + echo '# Portable Makefile generated by configure' + echo '' + echo 'all: sha sha1 sha256 sha384 sha512 test' + echo '' + + if [ -n "$2" ] ; then + ARCH="$2 " + else + ARCH="" + fi + + if [ -n "$1" ] && [ $1 = "AMIGA" ] ; then + echo 'CC = m68k-amigaos-gcc' + echo "CFLAGS = -O2 ${ARCH}-DAOS3 -noixemul -I. -Wall -Werror" + elif [ -n "$1" ] && [ $1 = "AROS" ] ; then + echo 'CC = gcc' + echo "CFLAGS = -O2 ${ARCH}-DAROS -I. -Wall -Werror" + else + echo "CFLAGS = -O2 ${ARCH}-Wall -Werror" + fi + echo '' + ################################################# + for f in ${HDRS} + do + b=`basename $f .h` + i=`sed -n -e '/#include.*"/{ + s/"$// + s/.*"// + p + }' $f | sort -u` + if [ "x${i}" != "x" ] ; then + echo "${b}.h: " ${i} + echo " touch ${b}.h" + echo + fi + done + + l="" + for f in ${SRCS} + do + b=`basename $f .c` + i=`sed -n -e '/#include.*"/{ + s/"$// + s/.*"// + p + }' $f | sort -u` + echo "${b}.o: ${b}.c" ${i} + echo + l="${l} ${b}.o" + done + ################################################# + BIN1="" + for f in ${SRCS1} + do + BIN1="${BIN1} `basename $f .c`.o" + done + + BIN2="" + for f in ${SRCS2} + do + BIN2="${BIN2} `basename $f .c`.o" + done + ################################################# + BINS1="" + for f in ${SRCSS1} + do + BINS1="${BINS1} `basename $f .c`.o" + done + BINS256="" + for f in ${SRCSS256} + do + BINS256="${BINS256} `basename $f .c`.o" + done + BINS384="" + for f in ${SRCSS384} + do + BINS384="${BINS384} `basename $f .c`.o" + done + BINS512="" + for f in ${SRCSS512} + do + BINS512="${BINS512} `basename $f .c`.o" + done + ################################################# + echo + echo "sha: ${BIN1}" + echo " \${CC} \${CFLAGS} -o sha ${BIN1}" + echo + echo "sha1: ${BINS1}" + echo " \${CC} \${CFLAGS} -o sha1 ${BINS1}" + echo + echo "sha256: ${BINS256}" + echo " \${CC} \${CFLAGS} -o sha256 ${BINS256}" + echo + echo "sha384: ${BINS384}" + echo " \${CC} \${CFLAGS} -o sha384 ${BINS384}" + echo + echo "sha512: ${BINS512}" + echo " \${CC} \${CFLAGS} -o sha512 ${BINS512}" + echo + echo "test: ${BIN2}" + echo " \${CC} \${CFLAGS} -o shatest ${BIN2}" + echo + echo "clean:" + echo " rm -f ${l} sha shatest sha1 sha256 sha384 sha512" + echo + echo "depend:" + echo " @echo Dependencies already done" + ) > Makefile + + echo "Makefile generated" +fi \ No newline at end of file diff --git a/getopt.c b/getopt.c deleted file mode 100644 index a30a8eb..0000000 --- a/getopt.c +++ /dev/null @@ -1,142 +0,0 @@ -/* $NetBSD: getopt.c,v 1.28 2009/03/20 13:56:57 joerg Exp $ */ - -/* - * Copyright (c) 1987, 1993, 1994 - * The Regents of the University of California. 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 University 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 REGENTS 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 REGENTS 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 -//__RCSID("$NetBSD: getopt.c,v 1.28 2009/03/20 13:56:57 joerg Exp $"); - -#ifndef __UNCONST -#define __UNCONST(a) ((void *)(unsigned long)(const void *)(a)) -#endif /* !__UNCONST */ - -//#include "namespace.h" -extern char *prog; -char* getprogname2() { return prog; } - -//#include -#include -#include -#include -#include -#include - -#ifdef __weak_alias -__weak_alias(getopt,_getopt) -#endif - -int opterr = 1, /* if error message should be printed */ - optind = 1, /* index into parent argv vector */ - optopt, /* character checked for validity */ - optreset; /* reset getopt */ -char* optarg; /* argument associated with option */ - -#define BADCH (int)'?' -#define BADARG (int)':' -#define EMSG "" - -/* - * getopt -- - * Parse argc/argv argument vector. - */ -int -getopt(int nargc, char * const nargv[], const char *ostr) -{ - static const char *place = EMSG; /* option letter processing */ - char *oli; /* option letter list index */ - - //_DIAGASSERT(nargv != NULL); - //_DIAGASSERT(ostr != NULL); - - if (optreset || *place == 0) { /* update scanning pointer */ - optreset = 0; - place = nargv[optind]; - if (optind >= nargc || *place++ != '-') { - /* Argument is absent or is not an option */ - place = EMSG; - return (-1); - } - optopt = *place++; - if (optopt == '-' && *place == 0) { - /* "--" => end of options */ - ++optind; - place = EMSG; - return (-1); - } - if (optopt == 0) { - /* Solitary '-', treat as a '-' option - if the program (eg su) is looking for it. */ - place = EMSG; - if (strchr(ostr, '-') == NULL) - return -1; - optopt = '-'; - } - } else - optopt = *place++; - - /* See if option letter is one the caller wanted... */ - if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) { - if (*place == 0) - ++optind; - if (opterr && *ostr != ':') - (void)fprintf(stderr, - "%s: unknown option -- %c\n", getprogname2(), - optopt); - return (BADCH); - } - - /* Does this option need an argument? */ - if (oli[1] != ':') { - /* don't need argument */ - optarg = NULL; - if (*place == 0) - ++optind; - } else { - /* Option-argument is either the rest of this argument or the - entire next argument. */ - if (*place) - optarg = __UNCONST(place); - else if (nargc > ++optind) - optarg = nargv[optind]; - else { - /* option-argument absent */ - place = EMSG; - if (*ostr == ':') - return (BADARG); - if (opterr) - (void)fprintf(stderr, - "%s: option requires an argument -- %c\n", - getprogname2(), optopt); - return (BADCH); - } - place = EMSG; - ++optind; - } - return (optopt); /* return option letter */ -} diff --git a/platform.h b/platform.h new file mode 100644 index 0000000..70d632b --- /dev/null +++ b/platform.h @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * 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 _SHA_PLATFORM_H +#define _SHA_PLATFORM_H +/***************************************************************/ +#include +#include +#include +#include "compiler.h" +/***************************************************************/ +#if defined(AOS3) || defined(AOS4) +# include +# include +# include +# include +# include +# include +# define ARGPTR STRPTR +# define ARGS_FORMAT ARGSFORMAT +#endif +#if defined(AROS) || defined(MORPHOS) +# define ARGPTR CONST_STRPTR +# define ARGS_FORMAT (CONST_STRPTR)ARGSFORMAT +#endif +#ifndef ARGPTR +# error Platform must be defined +#endif +/***************************************************************/ +#ifndef VERSION_PROG +# define VERSION_PROG "sha" +#endif +#define VERSION_MAJOR 1 +#define VERSION_MINOR 0 +#define VERSION_PATCHLEVEL 5 +#define VERSION_STRING "1.05" +#define VERSION_DATE "08.06.2015" +/***************************************************************/ +#ifdef mc68000 +# define ACPU "MC68000+" +# define WORDS_BIGENDIAN 1 +# define SHA_BUFFER_SIZE 65536 +#endif +#ifdef mc68020 +# undef ACPU +# define ACPU "MC68020" +#endif +#ifdef mc68030 +# undef ACPU +# define ACPU "MC68030" +#endif +#ifdef mc68040 +# undef ACPU +# define ACPU "MC68040" +#endif +#ifdef mc68060 +# undef ACPU +# define ACPU "MC68060" +#endif +/***************************************************************/ +#if defined(INTELCPU) || defined(i386) || defined(i486) || \ + defined(intel) || defined(x86) || defined(i86pc) || \ + defined(__i386__) || defined(_M_IX86) +# ifdef ACPU +# undef ACPU +# endif +# define ACPU "i386" +# define RUNTIME_ENDIAN 1 +# define SHA_BUFFER_SIZE 65536 +#endif +#ifdef __powerpc__ +# define ACPU "PowerPC" +# define RUNTIME_ENDIAN 1 +# define SHA_BUFFER_SIZE 65536 +#endif +#if defined(__x86_64__) +# define ACPU "amd64" +# define RUNTIME_ENDIAN 1 +# define SHA_BUFFER_SIZE 65536 +#endif +#ifndef ACPU +# error what cpu is this ?! +#endif +/**************************************************************** + * Assemble version string using format: + * sha 1.05 (08.06.2015) + */ +#define VERSION_STRING_AMIGA \ + "\0$VER: " \ + VERSION_PROG " " \ + VERSION_STRING " (" \ + VERSION_DATE ") " \ + ACPU +/***************************************************************/ +struct RDArgs *rdargs; +struct shaargs; + +int shaFile (char *name, FILE *f, int which); +void alloc_buffer(void); +void clean_exit(void); +void args_error(char *program); + +extern uint8_t *buffer; +extern void *alloced_buffer; + +#endif + diff --git a/sha.c b/sha.c index e14ca40..ae706b5 100644 --- a/sha.c +++ b/sha.c @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2001-2003 Allan Saddi * All rights reserved. * @@ -23,54 +24,15 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: sha.c 351 2003-02-23 23:24:40Z asaddi $ */ -#ifdef HAVE_CONFIG_H -#include -#endif /* HAVE_CONFIG_H */ - -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif - -#include -#include -#include -#include -//#if HAVE_UNISTD_H -//#include -//#endif /* HAVE_UNISTD_H */ - +#include "platform.h" #include "sha1.h" #include "sha256.h" #include "sha384.h" #include "sha512.h" -#include "version.h" - -extern int getopt(int nargc, char * const nargv[], const char *ostr); -extern int opterr, /* if error message should be printed */ - optind, /* index into parent argv vector */ - optopt, /* character checked for validity */ - optreset; /* reset getopt */ -extern char *optarg; /* argument associated with option */ - -#ifndef lint -static const char rcsid[] = - "$Id: sha.c 351 2003-02-23 23:24:40Z asaddi $"; -#endif /* !lint */ - -char *prog; - -#define SHA_BUFFER_SIZE 65536 -static uint8_t *buffer; - -static int shaFile (char *name, FILE *f, int which) +int shaFile (char *name, FILE *f, int which) { union { SHA1Context sha1; @@ -158,134 +120,3 @@ static int shaFile (char *name, FILE *f, int which) return success; } - -static void -help (int which) -{ - fprintf (stderr, "\nOptions:\n" - "\t-1\tUse SHA-1 %s\n" - "\t-2\tUse SHA-256 %s\n" - "\t-3\tUse SHA-384 %s\n" - "\t-5\tUse SHA-512 %s\n" - "\t-V\tDisplay version information\n" - "\t-h\tDisplay this summary\n\n" - "Only one of -1, -2, -3, -5 may be specified\n", - which == 1 ? "(Default)" : "", - which == 2 ? "(Default)" : "", - which == 3 ? "(Default)" : "", - which == 5 ? "(Default)" : ""); -} - -static void -usage (void) -{ - fprintf (stderr, "Usage: %s [-1235Vh] [file ...]\n", prog); -} - -static void -burnBuffer (void) -{ - memset (buffer, 0, SHA_BUFFER_SIZE); -} - -int -main (int argc, char *argv[]) -{ - int ch; - char *whichStr; - int whichDef = 1; - int which = 0; - long offs; - int i; - FILE *f; - int failure = 0; - - prog = argv[0]; - - if ((whichStr = getenv ("SHA_DEFAULT")) && *whichStr) { - switch (*whichStr) { - case '1': - whichDef = 1; - break; - case '2': - whichDef = 2; - break; - case '3': - whichDef = 3; - break; - case '5': - whichDef = 5; - break; - } - } - - while ((ch = getopt (argc, argv, "1235Vh")) != -1) { - switch (ch) { - case '1': - case '2': - case '3': - case '5': - if (!which) - which = ch - '0'; - else { - usage (); - help (whichDef); - exit (1); - } - break; - case 'V': - fprintf (stderr, "%s version " VERSION_STRING " (" VERSION_DATE ")\n", prog); - exit (1); - case 'h': - usage (); - help (whichDef); - exit (1); - case '?': - default: - usage (); - exit (1); - } - } - argc -= optind; - argv += optind; - - if (!which) - which = whichDef; - - /* Allocate a suitable buffer. */ - if (!(buffer = malloc (SHA_BUFFER_SIZE + 7))) { - perror (prog); - exit (1); - } - - /* Ensure it is on a 64-bit boundary. */ - if ((offs = (long) buffer & 7L)) - buffer += 8 - offs; - - atexit (burnBuffer); - - /* If given no arguments, process stdin. */ - if (!argc) { - //if (shaFile (NULL, stdin, which)) - // exit (0); - //else - usage (); - help (whichDef); - - exit (1); - } - - for (i = 0; i < argc; i++) { - if ((f = fopen (argv[i], "rb"))) { - if (!shaFile (argv[i], f, which)) - failure = 1; - fclose (f); - } - else { - fprintf (stderr, "%s: %s\n", argv[i], "No such file or directory"); - failure = 1; - } - } - - exit (failure); -} diff --git a/sha.guide b/sha.guide new file mode 100644 index 0000000..57a3086 --- /dev/null +++ b/sha.guide @@ -0,0 +1,39 @@ +@database sha.guide +@author Carsten Larsen +@(c) Carsten Larsen +@$VER: sha.guide 1.05 (11.06.2015) +@node Main "User manual" +@title "File hashing utility" +@{bg filltext}@{b}SHA SHA@{ub}@{bg background} + +@{b}NAME@{ub} + sha - file hashing utility + +@{b}USAGE@{ub} + @{b}sha@{ub} [@{b}SHA1@{ub}|@{b}SHA256@{ub}|@{b}SHA384@{ub}|@{b}SHA512@{ub}] [@{u}file@{uu} ...] + +@{b}DESCRIPTION@{ub} + sha is a simple program that hashes files. It uses the National Institute + of Standards and Technology's Secure Hash Algorithm. It can use SHA-1, + SHA-256, SHA-384, or SHA-512, which generate respectively, hashes of 160, + 256, 384, or 512 bits. @{b}sha@{ub} can be used in scripts to do, for example, + file integrity checking. + +@{b}OPTIONS@{ub} + @{b}SHA1@{ub} Use SHA1, which produces a 160-bit hash (40 hex digits). + + @{b}SHA256@{ub} Use SHA256, which produces a 256-bit hash (64 hex digits). + + @{b}SHA384@{ub} Use SHA384, which produces a 384-bit hash (96 hex digits). + + @{b}SHA512@{ub} Use SHA512, which produces a 512-bit hash (128 hex digits). + + @{u}file@{uu} ... + One or more files to hash. + +@{b}AUTHOR@{ub} + The original sha utility was developed by Allan Saddi. The Amiga version + is being developed and maintained by Carsten Larsen. + +@{bg filltext}@{b} June 11, 2015 SHA@{ub}@{bg background} +@endnode diff --git a/sha1.c b/sha1.c index 513e4e0..8f04c2c 100644 --- a/sha1.c +++ b/sha1.c @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2001-2003 Allan Saddi * All rights reserved. * @@ -23,7 +24,6 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: sha1.c 680 2003-07-25 21:57:38Z asaddi $ */ /* @@ -37,27 +37,9 @@ * 34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f */ -#ifdef HAVE_CONFIG_H -#include -#endif /* HAVE_CONFIG_H */ - -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif - -#include - +#include "compiler.h" #include "sha1.h" -#ifndef lint -static const char rcsid[] = - "$Id: sha1.c 680 2003-07-25 21:57:38Z asaddi $"; -#endif /* !lint */ - #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) @@ -95,9 +77,9 @@ static const char rcsid[] = static inline uint64_t _byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); } #endif /* WORDS_BIGENDIAN */ @@ -113,460 +95,460 @@ static inline uint64_t _byteswap64(uint64_t x) static inline uint64_t __byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); } static inline uint32_t _byteswap(int littleEndian, uint32_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP(x); + if (!littleEndian) + return x; + else + return _BYTESWAP(x); } static inline uint64_t _byteswap64(int littleEndian, uint64_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP64(x); + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); } static inline void setEndian(int *littleEndianp) { - union { - uint32_t w; - uint8_t b[4]; - } endian; + union { + uint32_t w; + uint8_t b[4]; + } endian; - endian.w = 1L; - *littleEndianp = endian.b[0] != 0; + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; } #endif /* !RUNTIME_ENDIAN */ static const uint8_t padding[64] = { - 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void SHA1Init (SHA1Context *sc) { #ifdef RUNTIME_ENDIAN - setEndian (&sc->littleEndian); + setEndian (&sc->littleEndian); #endif /* RUNTIME_ENDIAN */ - sc->totalLength = 0LL; - sc->hash[0] = 0x67452301L; - sc->hash[1] = 0xefcdab89L; - sc->hash[2] = 0x98badcfeL; - sc->hash[3] = 0x10325476L; - sc->hash[4] = 0xc3d2e1f0L; - sc->bufferLength = 0L; + sc->totalLength = 0LL; + sc->hash[0] = 0x67452301L; + sc->hash[1] = 0xefcdab89L; + sc->hash[2] = 0x98badcfeL; + sc->hash[3] = 0x10325476L; + sc->hash[4] = 0xc3d2e1f0L; + sc->bufferLength = 0L; } static void burnStack (int size) { - char buf[128]; + char buf[128]; - memset (buf, 0, sizeof (buf)); - size -= sizeof (buf); - if (size > 0) - burnStack (size); + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); } static void SHA1Guts (SHA1Context *sc, const uint32_t *cbuf) { - uint32_t buf[80]; - uint32_t *W, *W3, *W8, *W14, *W16; - uint32_t a, b, c, d, e, temp; - int i; + uint32_t buf[80]; + uint32_t *W, *W3, *W8, *W14, *W16; + uint32_t a, b, c, d, e, temp; + int i; - W = buf; + W = buf; - for (i = 15; i >= 0; i--) { - *(W++) = BYTESWAP(*cbuf); - cbuf++; - } + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP(*cbuf); + cbuf++; + } - W16 = &buf[0]; - W14 = &buf[2]; - W8 = &buf[8]; - W3 = &buf[13]; + W16 = &buf[0]; + W14 = &buf[2]; + W8 = &buf[8]; + W3 = &buf[13]; - for (i = 63; i >= 0; i--) { - *W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++); - *W = ROTL(*W, 1); - W++; - } + for (i = 63; i >= 0; i--) { + *W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++); + *W = ROTL(*W, 1); + W++; + } - a = sc->hash[0]; - b = sc->hash[1]; - c = sc->hash[2]; - d = sc->hash[3]; - e = sc->hash[4]; + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; - W = buf; + W = buf; #ifndef SHA1_UNROLL #define SHA1_UNROLL 20 #endif /* !SHA1_UNROLL */ #if SHA1_UNROLL == 1 - for (i = 19; i >= 0; i--) - DO_ROUND(F_0_19, K_0_19); + for (i = 19; i >= 0; i--) + DO_ROUND(F_0_19, K_0_19); - for (i = 19; i >= 0; i--) - DO_ROUND(F_20_39, K_20_39); + for (i = 19; i >= 0; i--) + DO_ROUND(F_20_39, K_20_39); - for (i = 19; i >= 0; i--) - DO_ROUND(F_40_59, K_40_59); + for (i = 19; i >= 0; i--) + DO_ROUND(F_40_59, K_40_59); - for (i = 19; i >= 0; i--) - DO_ROUND(F_60_79, K_60_79); + for (i = 19; i >= 0; i--) + DO_ROUND(F_60_79, K_60_79); #elif SHA1_UNROLL == 2 - for (i = 9; i >= 0; i--) { - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - } + for (i = 9; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } - for (i = 9; i >= 0; i--) { - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - } + for (i = 9; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } - for (i = 9; i >= 0; i--) { - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - } + for (i = 9; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } - for (i = 9; i >= 0; i--) { - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - } + for (i = 9; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } #elif SHA1_UNROLL == 4 - for (i = 4; i >= 0; i--) { - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - } + for (i = 4; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } - for (i = 4; i >= 0; i--) { - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - } + for (i = 4; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } - for (i = 4; i >= 0; i--) { - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - } + for (i = 4; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } - for (i = 4; i >= 0; i--) { - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - } + for (i = 4; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } #elif SHA1_UNROLL == 5 - for (i = 3; i >= 0; i--) { - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } - for (i = 3; i >= 0; i--) { - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } - for (i = 3; i >= 0; i--) { - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } - for (i = 3; i >= 0; i--) { - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } #elif SHA1_UNROLL == 10 - for (i = 1; i >= 0; i--) { - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } - for (i = 1; i >= 0; i--) { - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } - for (i = 1; i >= 0; i--) { - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } - for (i = 1; i >= 0; i--) { - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } #elif SHA1_UNROLL == 20 - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); - DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); #else /* SHA1_UNROLL */ #error SHA1_UNROLL must be 1, 2, 4, 5, 10 or 20! #endif - sc->hash[0] += a; - sc->hash[1] += b; - sc->hash[2] += c; - sc->hash[3] += d; - sc->hash[4] += e; + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; } void SHA1Update (SHA1Context *sc, const void *vdata, uint32_t len) { - const uint8_t *data = vdata; - uint32_t bufferBytesLeft; - uint32_t bytesToCopy; - int needBurn = 0; + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + int needBurn = 0; #ifdef SHA1_FAST_COPY - if (sc->bufferLength) { - bufferBytesLeft = 64L - sc->bufferLength; + if (sc->bufferLength) { + bufferBytesLeft = 64L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - sc->totalLength += bytesToCopy * 8L; + sc->totalLength += bytesToCopy * 8L; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 64L) { - SHA1Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; + if (sc->bufferLength == 64L) { + SHA1Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } } - } - while (len > 63) { - sc->totalLength += 512L; + while (len > 63) { + sc->totalLength += 512L; - SHA1Guts (sc, data); - needBurn = 1; + SHA1Guts (sc, data); + needBurn = 1; - data += 64L; - len -= 64L; - } + data += 64L; + len -= 64L; + } - if (len) { - memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); - sc->totalLength += len * 8L; + sc->totalLength += len * 8L; - sc->bufferLength += len; - } + sc->bufferLength += len; + } #else /* SHA1_FAST_COPY */ - while (len) { - bufferBytesLeft = 64L - sc->bufferLength; + while (len) { + bufferBytesLeft = 64L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - sc->totalLength += bytesToCopy * 8L; + sc->totalLength += bytesToCopy * 8L; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 64L) { - SHA1Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; + if (sc->bufferLength == 64L) { + SHA1Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } } - } #endif /* SHA1_FAST_COPY */ - if (needBurn) - burnStack (sizeof (uint32_t[86]) + sizeof (uint32_t *[5]) + sizeof (int)); + if (needBurn) + burnStack (sizeof (uint32_t[86]) + sizeof (uint32_t *[5]) + sizeof (int)); } void SHA1Final (SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE]) { - uint32_t bytesToPad; - uint64_t lengthPad; - int i; + uint32_t bytesToPad; + uint64_t lengthPad; + int i; - bytesToPad = 120L - sc->bufferLength; - if (bytesToPad > 64L) - bytesToPad -= 64L; + bytesToPad = 120L - sc->bufferLength; + if (bytesToPad > 64L) + bytesToPad -= 64L; - lengthPad = BYTESWAP64(sc->totalLength); + lengthPad = BYTESWAP64(sc->totalLength); - SHA1Update (sc, padding, bytesToPad); - SHA1Update (sc, &lengthPad, 8L); + SHA1Update (sc, padding, bytesToPad); + SHA1Update (sc, &lengthPad, 8L); - if (hash) { - for (i = 0; i < SHA1_HASH_WORDS; i++) { + if (hash) { + for (i = 0; i < SHA1_HASH_WORDS; i++) { #ifdef SHA1_FAST_COPY - *((uint32_t *) hash) = BYTESWAP(sc->hash[i]); + *((uint32_t *) hash) = BYTESWAP(sc->hash[i]); #else /* SHA1_FAST_COPY */ - hash[0] = (uint8_t) (sc->hash[i] >> 24); - hash[1] = (uint8_t) (sc->hash[i] >> 16); - hash[2] = (uint8_t) (sc->hash[i] >> 8); - hash[3] = (uint8_t) sc->hash[i]; + hash[0] = (uint8_t) (sc->hash[i] >> 24); + hash[1] = (uint8_t) (sc->hash[i] >> 16); + hash[2] = (uint8_t) (sc->hash[i] >> 8); + hash[3] = (uint8_t) sc->hash[i]; #endif /* SHA1_FAST_COPY */ - hash += 4; + hash += 4; + } } - } } #ifdef SHA1_TEST @@ -578,49 +560,50 @@ SHA1Final (SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE]) int main (int argc, char *argv[]) { - SHA1Context foo; - uint8_t hash[SHA1_HASH_SIZE]; - char buf[1000]; - int i; + SHA1Context foo; + uint8_t hash[SHA1_HASH_SIZE]; + char buf[1000]; + int i; - SHA1Init (&foo); - SHA1Update (&foo, "abc", 3); - SHA1Final (&foo, hash); + SHA1Init (&foo); + SHA1Update (&foo, "abc", 3); + SHA1Final (&foo, hash); - for (i = 0; i < SHA1_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA1_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - SHA1Init (&foo); - SHA1Update (&foo, - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - 56); - SHA1Final (&foo, hash); + SHA1Init (&foo); + SHA1Update (&foo, + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + 56); + SHA1Final (&foo, hash); - for (i = 0; i < SHA1_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA1_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - SHA1Init (&foo); - memset (buf, 'a', sizeof (buf)); - for (i = 0; i < 1000; i++) - SHA1Update (&foo, buf, sizeof (buf)); - SHA1Final (&foo, hash); + SHA1Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA1Update (&foo, buf, sizeof (buf)); + SHA1Final (&foo, hash); - for (i = 0; i < SHA1_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA1_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - exit (0); + exit (0); } #endif /* SHA1_TEST */ + diff --git a/sha1.h b/sha1.h index 7a6beb7..d1e767a 100644 --- a/sha1.h +++ b/sha1.h @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2001-2003 Allan Saddi * All rights reserved. * @@ -23,19 +24,12 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: sha1.h 347 2003-02-23 22:11:49Z asaddi $ */ #ifndef _SHA1_H #define _SHA1_H -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif +#include "platform.h" #define SHA1_HASH_SIZE 20 @@ -43,15 +37,15 @@ #define SHA1_HASH_WORDS 5 struct _SHA1Context { - uint64_t totalLength; - uint32_t hash[SHA1_HASH_WORDS]; - uint32_t bufferLength; - union { - uint32_t words[16]; - uint8_t bytes[64]; - } buffer; + uint64_t totalLength; + uint32_t hash[SHA1_HASH_WORDS]; + uint32_t bufferLength; + union { + uint32_t words[16]; + uint8_t bytes[64]; + } buffer; #ifdef RUNTIME_ENDIAN - int littleEndian; + int littleEndian; #endif /* RUNTIME_ENDIAN */ }; diff --git a/sha1_amiga.c b/sha1_amiga.c new file mode 100644 index 0000000..a8bf7d7 --- /dev/null +++ b/sha1_amiga.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * 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. + * + */ + +#define ARGSFORMAT "FILES/M/A" +#define VERSION_PROG "sha1" +#include "platform.h" +#include "sha1.h" + +const char *vers = VERSION_STRING_AMIGA; + +struct shaargs { + char **files; +}; + +int main(int argc, char **argv) +{ + int i; + FILE *f; + int failure = 0; + struct shaargs args = { NULL }; + + atexit(clean_exit); + alloc_buffer(); + + rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); + if (!rdargs) + { + args_error(argv[0]); + exit(5); + } + + for (i = 0; args.files[i]; i++) { + if ((f = fopen(args.files[i], "rb"))) { + if (!shaFile(args.files[i], f, 0)) + failure = 1; + fclose(f); + } + else { + fprintf(stderr, "%s: %s\n",args.files[i], "No such file or directory"); + failure = 1; + } + } + + exit(failure); +} + +int shaFile (char *name, FILE *f, int which) +{ + SHA1Context s; + size_t len; + uint8_t hash[SHA1_HASH_SIZE]; + int hashLen, i; + int success = 1; + + SHA1Init (&s); + + while ((len = fread (buffer, 1, SHA_BUFFER_SIZE, f)) > 0) { + SHA1Update (&s, buffer, len); + } + + if (ferror (f)) { + fprintf (stderr, "%s: %s\n", name, "Read error"); + success = 0; + } + else { + SHA1Final (&s, hash); + hashLen = SHA1_HASH_SIZE; + + for (i = 0; i < hashLen; i++) + printf ("%02x", hash[i]); + + if (name) + printf (" %s\n", name); + else + printf ("\n"); + } + + memset(&s, 0, sizeof(s)); + + return success; +} + diff --git a/sha256.c b/sha256.c index ba31091..a183863 100644 --- a/sha256.c +++ b/sha256.c @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2001-2003 Allan Saddi * All rights reserved. * @@ -23,7 +24,6 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: sha256.c 680 2003-07-25 21:57:49Z asaddi $ */ /* @@ -37,27 +37,9 @@ * cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0 */ -#ifdef HAVE_CONFIG_H -#include -#endif /* HAVE_CONFIG_H */ - -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif - -#include - +#include "compiler.h" #include "sha256.h" -#ifndef lint -static const char rcsid[] = - "$Id: sha256.c 680 2003-07-25 21:57:49Z asaddi $"; -#endif /* !lint */ - #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) @@ -82,22 +64,22 @@ static const char rcsid[] = } static const uint32_t K[64] = { - 0x428a2f98L, 0x71374491L, 0xb5c0fbcfL, 0xe9b5dba5L, - 0x3956c25bL, 0x59f111f1L, 0x923f82a4L, 0xab1c5ed5L, - 0xd807aa98L, 0x12835b01L, 0x243185beL, 0x550c7dc3L, - 0x72be5d74L, 0x80deb1feL, 0x9bdc06a7L, 0xc19bf174L, - 0xe49b69c1L, 0xefbe4786L, 0x0fc19dc6L, 0x240ca1ccL, - 0x2de92c6fL, 0x4a7484aaL, 0x5cb0a9dcL, 0x76f988daL, - 0x983e5152L, 0xa831c66dL, 0xb00327c8L, 0xbf597fc7L, - 0xc6e00bf3L, 0xd5a79147L, 0x06ca6351L, 0x14292967L, - 0x27b70a85L, 0x2e1b2138L, 0x4d2c6dfcL, 0x53380d13L, - 0x650a7354L, 0x766a0abbL, 0x81c2c92eL, 0x92722c85L, - 0xa2bfe8a1L, 0xa81a664bL, 0xc24b8b70L, 0xc76c51a3L, - 0xd192e819L, 0xd6990624L, 0xf40e3585L, 0x106aa070L, - 0x19a4c116L, 0x1e376c08L, 0x2748774cL, 0x34b0bcb5L, - 0x391c0cb3L, 0x4ed8aa4aL, 0x5b9cca4fL, 0x682e6ff3L, - 0x748f82eeL, 0x78a5636fL, 0x84c87814L, 0x8cc70208L, - 0x90befffaL, 0xa4506cebL, 0xbef9a3f7L, 0xc67178f2L + 0x428a2f98L, 0x71374491L, 0xb5c0fbcfL, 0xe9b5dba5L, + 0x3956c25bL, 0x59f111f1L, 0x923f82a4L, 0xab1c5ed5L, + 0xd807aa98L, 0x12835b01L, 0x243185beL, 0x550c7dc3L, + 0x72be5d74L, 0x80deb1feL, 0x9bdc06a7L, 0xc19bf174L, + 0xe49b69c1L, 0xefbe4786L, 0x0fc19dc6L, 0x240ca1ccL, + 0x2de92c6fL, 0x4a7484aaL, 0x5cb0a9dcL, 0x76f988daL, + 0x983e5152L, 0xa831c66dL, 0xb00327c8L, 0xbf597fc7L, + 0xc6e00bf3L, 0xd5a79147L, 0x06ca6351L, 0x14292967L, + 0x27b70a85L, 0x2e1b2138L, 0x4d2c6dfcL, 0x53380d13L, + 0x650a7354L, 0x766a0abbL, 0x81c2c92eL, 0x92722c85L, + 0xa2bfe8a1L, 0xa81a664bL, 0xc24b8b70L, 0xc76c51a3L, + 0xd192e819L, 0xd6990624L, 0xf40e3585L, 0x106aa070L, + 0x19a4c116L, 0x1e376c08L, 0x2748774cL, 0x34b0bcb5L, + 0x391c0cb3L, 0x4ed8aa4aL, 0x5b9cca4fL, 0x682e6ff3L, + 0x748f82eeL, 0x78a5636fL, 0x84c87814L, 0x8cc70208L, + 0x90befffaL, 0xa4506cebL, 0xbef9a3f7L, 0xc67178f2L }; #ifndef RUNTIME_ENDIAN @@ -115,9 +97,9 @@ static const uint32_t K[64] = { static inline uint64_t _byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); } #endif /* WORDS_BIGENDIAN */ @@ -133,295 +115,389 @@ static inline uint64_t _byteswap64(uint64_t x) static inline uint64_t __byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); } static inline uint32_t _byteswap(int littleEndian, uint32_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP(x); + if (!littleEndian) + return x; + else + return _BYTESWAP(x); } static inline uint64_t _byteswap64(int littleEndian, uint64_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP64(x); + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); } static inline void setEndian(int *littleEndianp) { - union { - uint32_t w; - uint8_t b[4]; - } endian; + union { + uint32_t w; + uint8_t b[4]; + } endian; - endian.w = 1L; - *littleEndianp = endian.b[0] != 0; + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; } #endif /* !RUNTIME_ENDIAN */ static const uint8_t padding[64] = { - 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void SHA256Init (SHA256Context *sc) { #ifdef RUNTIME_ENDIAN - setEndian (&sc->littleEndian); + setEndian (&sc->littleEndian); #endif /* RUNTIME_ENDIAN */ - sc->totalLength = 0LL; - sc->hash[0] = 0x6a09e667L; - sc->hash[1] = 0xbb67ae85L; - sc->hash[2] = 0x3c6ef372L; - sc->hash[3] = 0xa54ff53aL; - sc->hash[4] = 0x510e527fL; - sc->hash[5] = 0x9b05688cL; - sc->hash[6] = 0x1f83d9abL; - sc->hash[7] = 0x5be0cd19L; - sc->bufferLength = 0L; + sc->totalLength = 0LL; + sc->hash[0] = 0x6a09e667L; + sc->hash[1] = 0xbb67ae85L; + sc->hash[2] = 0x3c6ef372L; + sc->hash[3] = 0xa54ff53aL; + sc->hash[4] = 0x510e527fL; + sc->hash[5] = 0x9b05688cL; + sc->hash[6] = 0x1f83d9abL; + sc->hash[7] = 0x5be0cd19L; + sc->bufferLength = 0L; } static void burnStack (int size) { - char buf[128]; + char buf[128]; - memset (buf, 0, sizeof (buf)); - size -= sizeof (buf); - if (size > 0) - burnStack (size); + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); } static void SHA256Guts (SHA256Context *sc, const uint32_t *cbuf) { - uint32_t buf[64]; - uint32_t *W, *W2, *W7, *W15, *W16; - uint32_t a, b, c, d, e, f, g, h; - uint32_t t1, t2; - const uint32_t *Kp; - int i; + uint32_t buf[64]; + uint32_t *W, *W2, *W7, *W15, *W16; + uint32_t a, b, c, d, e, f, g, h; + uint32_t t1, t2; + const uint32_t *Kp; + int i; - W = buf; + W = buf; - for (i = 15; i >= 0; i--) { - *(W++) = BYTESWAP(*cbuf); - cbuf++; - } + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP(*cbuf); + cbuf++; + } - W16 = &buf[0]; - W15 = &buf[1]; - W7 = &buf[9]; - W2 = &buf[14]; + W16 = &buf[0]; + W15 = &buf[1]; + W7 = &buf[9]; + W2 = &buf[14]; - for (i = 47; i >= 0; i--) { - *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); - W2++; - W15++; - } + for (i = 47; i >= 0; i--) { + *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); + W2++; + W15++; + } - a = sc->hash[0]; - b = sc->hash[1]; - c = sc->hash[2]; - d = sc->hash[3]; - e = sc->hash[4]; - f = sc->hash[5]; - g = sc->hash[6]; - h = sc->hash[7]; + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; + f = sc->hash[5]; + g = sc->hash[6]; + h = sc->hash[7]; - Kp = K; - W = buf; + Kp = K; + W = buf; #ifndef SHA256_UNROLL #define SHA256_UNROLL 1 #endif /* !SHA256_UNROLL */ #if SHA256_UNROLL == 1 - for (i = 63; i >= 0; i--) - DO_ROUND(); + for (i = 63; i >= 0; i--) + DO_ROUND(); #elif SHA256_UNROLL == 2 - for (i = 31; i >= 0; i--) { - DO_ROUND(); DO_ROUND(); - } + for (i = 31; i >= 0; i--) { + DO_ROUND(); + DO_ROUND(); + } #elif SHA256_UNROLL == 4 - for (i = 15; i >= 0; i--) { - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - } + for (i = 15; i >= 0; i--) { + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + } #elif SHA256_UNROLL == 8 - for (i = 7; i >= 0; i--) { - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - } + for (i = 7; i >= 0; i--) { + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + } #elif SHA256_UNROLL == 16 - for (i = 3; i >= 0; i--) { - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - } + for (i = 3; i >= 0; i--) { + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + } #elif SHA256_UNROLL == 32 - for (i = 1; i >= 0; i--) { - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - } + for (i = 1; i >= 0; i--) { + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + } #elif SHA256_UNROLL == 64 - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); - DO_ROUND(); DO_ROUND(); DO_ROUND(); DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); + DO_ROUND(); #else #error "SHA256_UNROLL must be 1, 2, 4, 8, 16, 32, or 64!" #endif - sc->hash[0] += a; - sc->hash[1] += b; - sc->hash[2] += c; - sc->hash[3] += d; - sc->hash[4] += e; - sc->hash[5] += f; - sc->hash[6] += g; - sc->hash[7] += h; + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; + sc->hash[5] += f; + sc->hash[6] += g; + sc->hash[7] += h; } void SHA256Update (SHA256Context *sc, const void *vdata, uint32_t len) { - const uint8_t *data = vdata; - uint32_t bufferBytesLeft; - uint32_t bytesToCopy; - int needBurn = 0; + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + int needBurn = 0; #ifdef SHA256_FAST_COPY - if (sc->bufferLength) { - bufferBytesLeft = 64L - sc->bufferLength; + if (sc->bufferLength) { + bufferBytesLeft = 64L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - sc->totalLength += bytesToCopy * 8L; + sc->totalLength += bytesToCopy * 8L; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 64L) { - SHA256Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; + if (sc->bufferLength == 64L) { + SHA256Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } } - } - while (len > 63L) { - sc->totalLength += 512L; + while (len > 63L) { + sc->totalLength += 512L; - SHA256Guts (sc, data); - needBurn = 1; + SHA256Guts (sc, data); + needBurn = 1; - data += 64L; - len -= 64L; - } + data += 64L; + len -= 64L; + } - if (len) { - memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); - sc->totalLength += len * 8L; + sc->totalLength += len * 8L; - sc->bufferLength += len; - } + sc->bufferLength += len; + } #else /* SHA256_FAST_COPY */ - while (len) { - bufferBytesLeft = 64L - sc->bufferLength; + while (len) { + bufferBytesLeft = 64L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - sc->totalLength += bytesToCopy * 8L; + sc->totalLength += bytesToCopy * 8L; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 64L) { - SHA256Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; + if (sc->bufferLength == 64L) { + SHA256Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } } - } #endif /* SHA256_FAST_COPY */ - if (needBurn) - burnStack (sizeof (uint32_t[74]) + sizeof (uint32_t *[6]) + sizeof (int)); + if (needBurn) + burnStack (sizeof (uint32_t[74]) + sizeof (uint32_t *[6]) + sizeof (int)); } void SHA256Final (SHA256Context *sc, uint8_t hash[SHA256_HASH_SIZE]) { - uint32_t bytesToPad; - uint64_t lengthPad; - int i; + uint32_t bytesToPad; + uint64_t lengthPad; + int i; - bytesToPad = 120L - sc->bufferLength; - if (bytesToPad > 64L) - bytesToPad -= 64L; + bytesToPad = 120L - sc->bufferLength; + if (bytesToPad > 64L) + bytesToPad -= 64L; - lengthPad = BYTESWAP64(sc->totalLength); + lengthPad = BYTESWAP64(sc->totalLength); - SHA256Update (sc, padding, bytesToPad); - SHA256Update (sc, &lengthPad, 8L); + SHA256Update (sc, padding, bytesToPad); + SHA256Update (sc, &lengthPad, 8L); - if (hash) { - for (i = 0; i < SHA256_HASH_WORDS; i++) { + if (hash) { + for (i = 0; i < SHA256_HASH_WORDS; i++) { #ifdef SHA256_FAST_COPY - *((uint32_t *) hash) = BYTESWAP(sc->hash[i]); + *((uint32_t *) hash) = BYTESWAP(sc->hash[i]); #else /* SHA256_FAST_COPY */ - hash[0] = (uint8_t) (sc->hash[i] >> 24); - hash[1] = (uint8_t) (sc->hash[i] >> 16); - hash[2] = (uint8_t) (sc->hash[i] >> 8); - hash[3] = (uint8_t) sc->hash[i]; + hash[0] = (uint8_t) (sc->hash[i] >> 24); + hash[1] = (uint8_t) (sc->hash[i] >> 16); + hash[2] = (uint8_t) (sc->hash[i] >> 8); + hash[3] = (uint8_t) sc->hash[i]; #endif /* SHA256_FAST_COPY */ - hash += 4; + hash += 4; + } } - } } #ifdef SHA256_TEST @@ -433,49 +509,49 @@ SHA256Final (SHA256Context *sc, uint8_t hash[SHA256_HASH_SIZE]) int main (int argc, char *argv[]) { - SHA256Context foo; - uint8_t hash[SHA256_HASH_SIZE]; - char buf[1000]; - int i; + SHA256Context foo; + uint8_t hash[SHA256_HASH_SIZE]; + char buf[1000]; + int i; - SHA256Init (&foo); - SHA256Update (&foo, "abc", 3); - SHA256Final (&foo, hash); + SHA256Init (&foo); + SHA256Update (&foo, "abc", 3); + SHA256Final (&foo, hash); - for (i = 0; i < SHA256_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA256_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - SHA256Init (&foo); - SHA256Update (&foo, - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - 56); - SHA256Final (&foo, hash); + SHA256Init (&foo); + SHA256Update (&foo, + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + 56); + SHA256Final (&foo, hash); - for (i = 0; i < SHA256_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA256_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - SHA256Init (&foo); - memset (buf, 'a', sizeof (buf)); - for (i = 0; i < 1000; i++) - SHA256Update (&foo, buf, sizeof (buf)); - SHA256Final (&foo, hash); + SHA256Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA256Update (&foo, buf, sizeof (buf)); + SHA256Final (&foo, hash); - for (i = 0; i < SHA256_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 4)) - printf (" "); - } - printf ("\n"); + for (i = 0; i < SHA256_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); - exit (0); + exit (0); } #endif /* SHA256_TEST */ diff --git a/sha256.h b/sha256.h index 76bc01c..a1e167b 100644 --- a/sha256.h +++ b/sha256.h @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2001-2003 Allan Saddi * All rights reserved. * @@ -23,19 +24,12 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: sha256.h 348 2003-02-23 22:12:06Z asaddi $ */ #ifndef _SHA256_H #define _SHA256_H -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif +#include "platform.h" #define SHA256_HASH_SIZE 32 @@ -43,15 +37,15 @@ #define SHA256_HASH_WORDS 8 struct _SHA256Context { - uint64_t totalLength; - uint32_t hash[SHA256_HASH_WORDS]; - uint32_t bufferLength; - union { - uint32_t words[16]; - uint8_t bytes[64]; - } buffer; + uint64_t totalLength; + uint32_t hash[SHA256_HASH_WORDS]; + uint32_t bufferLength; + union { + uint32_t words[16]; + uint8_t bytes[64]; + } buffer; #ifdef RUNTIME_ENDIAN - int littleEndian; + int littleEndian; #endif /* RUNTIME_ENDIAN */ }; diff --git a/sha256_amiga.c b/sha256_amiga.c new file mode 100644 index 0000000..6ce6e51 --- /dev/null +++ b/sha256_amiga.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * 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. + * + */ + +#define ARGSFORMAT "FILES/M/A" +#define VERSION_PROG "sha256" +#include "platform.h" +#include "sha256.h" + +const char *vers = VERSION_STRING_AMIGA; + +struct shaargs { + char **files; +}; + +int main(int argc, char **argv) +{ + int i; + FILE *f; + int failure = 0; + struct shaargs args = { NULL }; + + atexit(clean_exit); + alloc_buffer(); + + rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); + if (!rdargs) + { + args_error(argv[0]); + exit(5); + } + + for (i = 0; args.files[i]; i++) { + if ((f = fopen(args.files[i], "rb"))) { + if (!shaFile(args.files[i], f, 0)) + failure = 1; + fclose(f); + } + else { + fprintf(stderr, "%s: %s\n",args.files[i], "No such file or directory"); + failure = 1; + } + } + + exit(failure); +} + +int shaFile (char *name, FILE *f, int which) +{ + SHA256Context s; + size_t len; + uint8_t hash[SHA256_HASH_SIZE]; + int hashLen, i; + int success = 1; + + SHA256Init (&s); + + while ((len = fread (buffer, 1, SHA_BUFFER_SIZE, f)) > 0) { + SHA256Update (&s, buffer, len); + } + + if (ferror (f)) { + fprintf (stderr, "%s: %s\n", name, "Read error"); + success = 0; + } + else { + SHA256Final (&s, hash); + hashLen = SHA256_HASH_SIZE; + + for (i = 0; i < hashLen; i++) + printf ("%02x", hash[i]); + + if (name) + printf (" %s\n", name); + else + printf ("\n"); + } + + memset(&s, 0, sizeof(s)); + + return success; +} diff --git a/sha384.c b/sha384.c index bc5c83f..3a540ca 100644 --- a/sha384.c +++ b/sha384.c @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2001-2003 Allan Saddi * All rights reserved. * @@ -23,7 +24,6 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: sha384.c 680 2003-07-25 21:57:57Z asaddi $ */ /* @@ -40,27 +40,9 @@ * 07b8b3dc38ecc4eb ae97ddd87f3d8985 */ -#ifdef HAVE_CONFIG_H -#include -#endif /* HAVE_CONFIG_H */ - -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif - -#include - +#include "compiler.h" #include "sha384.h" -#ifndef lint -static const char rcsid[] = - "$Id: sha384.c 680 2003-07-25 21:57:57Z asaddi $"; -#endif /* !lint */ - #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) #define ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n)))) @@ -87,46 +69,46 @@ static const char rcsid[] = } static const uint64_t K[80] = { - 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, - 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL, - 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, - 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL, - 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, - 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL, - 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, - 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL, - 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, - 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL, - 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, - 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL, - 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, - 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL, - 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, - 0x06ca6351e003826fLL, 0x142929670a0e6e70LL, - 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, - 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL, - 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, - 0x81c2c92e47edaee6LL, 0x92722c851482353bLL, - 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, - 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL, - 0xd192e819d6ef5218LL, 0xd69906245565a910LL, - 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL, - 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, - 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL, - 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, - 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL, - 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, - 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL, - 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, - 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL, - 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, - 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL, - 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, - 0x113f9804bef90daeLL, 0x1b710b35131c471bLL, - 0x28db77f523047d84LL, 0x32caab7b40c72493LL, - 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL, - 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, - 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL + 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, + 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL, + 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, + 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL, + 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, + 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL, + 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, + 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL, + 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, + 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL, + 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, + 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL, + 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, + 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL, + 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, + 0x06ca6351e003826fLL, 0x142929670a0e6e70LL, + 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, + 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL, + 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, + 0x81c2c92e47edaee6LL, 0x92722c851482353bLL, + 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, + 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL, + 0xd192e819d6ef5218LL, 0xd69906245565a910LL, + 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL, + 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, + 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL, + 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, + 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL, + 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, + 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL, + 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, + 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL, + 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, + 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL, + 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, + 0x113f9804bef90daeLL, 0x1b710b35131c471bLL, + 0x28db77f523047d84LL, 0x32caab7b40c72493LL, + 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL, + 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, + 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL }; #ifndef RUNTIME_ENDIAN @@ -144,9 +126,9 @@ static const uint64_t K[80] = { static inline uint64_t _byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); } #endif /* WORDS_BIGENDIAN */ @@ -162,266 +144,266 @@ static inline uint64_t _byteswap64(uint64_t x) static inline uint64_t __byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); } static inline uint32_t _byteswap(int littleEndian, uint32_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP(x); + if (!littleEndian) + return x; + else + return _BYTESWAP(x); } static inline uint64_t _byteswap64(int littleEndian, uint64_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP64(x); + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); } static inline void setEndian(int *littleEndianp) { - union { - uint32_t w; - uint8_t b[4]; - } endian; + union { + uint32_t w; + uint8_t b[4]; + } endian; - endian.w = 1L; - *littleEndianp = endian.b[0] != 0; + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; } #endif /* !RUNTIME_ENDIAN */ static const uint8_t padding[128] = { - 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void SHA384Init (SHA384Context *sc) { #ifdef RUNTIME_ENDIAN - setEndian (&sc->littleEndian); + setEndian (&sc->littleEndian); #endif /* RUNTIME_ENDIAN */ - sc->totalLength[0] = 0LL; - sc->totalLength[1] = 0LL; - sc->hash[0] = 0xcbbb9d5dc1059ed8LL; - sc->hash[1] = 0x629a292a367cd507LL; - sc->hash[2] = 0x9159015a3070dd17LL; - sc->hash[3] = 0x152fecd8f70e5939LL; - sc->hash[4] = 0x67332667ffc00b31LL; - sc->hash[5] = 0x8eb44a8768581511LL; - sc->hash[6] = 0xdb0c2e0d64f98fa7LL; - sc->hash[7] = 0x47b5481dbefa4fa4LL; - sc->bufferLength = 0L; + sc->totalLength[0] = 0LL; + sc->totalLength[1] = 0LL; + sc->hash[0] = 0xcbbb9d5dc1059ed8LL; + sc->hash[1] = 0x629a292a367cd507LL; + sc->hash[2] = 0x9159015a3070dd17LL; + sc->hash[3] = 0x152fecd8f70e5939LL; + sc->hash[4] = 0x67332667ffc00b31LL; + sc->hash[5] = 0x8eb44a8768581511LL; + sc->hash[6] = 0xdb0c2e0d64f98fa7LL; + sc->hash[7] = 0x47b5481dbefa4fa4LL; + sc->bufferLength = 0L; } static void burnStack (int size) { - char buf[128]; + char buf[128]; - memset (buf, 0, sizeof (buf)); - size -= sizeof (buf); - if (size > 0) - burnStack (size); + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); } static void SHA384Guts (SHA384Context *sc, const uint64_t *cbuf) { - uint64_t buf[80]; - uint64_t *W, *W2, *W7, *W15, *W16; - uint64_t a, b, c, d, e, f, g, h; - uint64_t t1, t2; - const uint64_t *Kp; - int i; + uint64_t buf[80]; + uint64_t *W, *W2, *W7, *W15, *W16; + uint64_t a, b, c, d, e, f, g, h; + uint64_t t1, t2; + const uint64_t *Kp; + int i; - W = buf; + W = buf; - for (i = 15; i >= 0; i--) { - *(W++) = BYTESWAP64(*cbuf); - cbuf++; - } + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP64(*cbuf); + cbuf++; + } - W16 = &buf[0]; - W15 = &buf[1]; - W7 = &buf[9]; - W2 = &buf[14]; + W16 = &buf[0]; + W15 = &buf[1]; + W7 = &buf[9]; + W2 = &buf[14]; - for (i = 63; i >= 0; i--) { - *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); - W2++; - W15++; - } + for (i = 63; i >= 0; i--) { + *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); + W2++; + W15++; + } - a = sc->hash[0]; - b = sc->hash[1]; - c = sc->hash[2]; - d = sc->hash[3]; - e = sc->hash[4]; - f = sc->hash[5]; - g = sc->hash[6]; - h = sc->hash[7]; + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; + f = sc->hash[5]; + g = sc->hash[6]; + h = sc->hash[7]; - Kp = K; - W = buf; + Kp = K; + W = buf; - for (i = 79; i >= 0; i--) - DO_ROUND(); + for (i = 79; i >= 0; i--) + DO_ROUND(); - sc->hash[0] += a; - sc->hash[1] += b; - sc->hash[2] += c; - sc->hash[3] += d; - sc->hash[4] += e; - sc->hash[5] += f; - sc->hash[6] += g; - sc->hash[7] += h; + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; + sc->hash[5] += f; + sc->hash[6] += g; + sc->hash[7] += h; } void SHA384Update (SHA384Context *sc, const void *vdata, uint32_t len) { - const uint8_t *data = vdata; - uint32_t bufferBytesLeft; - uint32_t bytesToCopy; - uint64_t carryCheck; - int needBurn = 0; + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + uint64_t carryCheck; + int needBurn = 0; #ifdef SHA384_FAST_COPY - if (sc->bufferLength) { - bufferBytesLeft = 128L - sc->bufferLength; + if (sc->bufferLength) { + bufferBytesLeft = 128L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += bytesToCopy * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 128L) { - SHA384Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; + if (sc->bufferLength == 128L) { + SHA384Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } } - } - while (len > 127) { - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += 1024L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + while (len > 127) { + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += 1024L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - SHA384Guts (sc, data); - needBurn = 1; + SHA384Guts (sc, data); + needBurn = 1; - data += 128L; - len -= 128L; - } + data += 128L; + len -= 128L; + } - if (len) { - memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += len * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += len * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += len; - } + sc->bufferLength += len; + } #else /* SHA384_FAST_COPY */ - while (len) { - bufferBytesLeft = 128L - sc->bufferLength; + while (len) { + bufferBytesLeft = 128L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += bytesToCopy * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 128L) { - SHA384Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; + if (sc->bufferLength == 128L) { + SHA384Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } } - } #endif /* SHA384_FAST_COPY */ - if (needBurn) - burnStack (sizeof (uint64_t[90]) + sizeof (uint64_t *[6]) + sizeof (int)); + if (needBurn) + burnStack (sizeof (uint64_t[90]) + sizeof (uint64_t *[6]) + sizeof (int)); } void SHA384Final (SHA384Context *sc, uint8_t hash[SHA384_HASH_SIZE]) { - uint32_t bytesToPad; - uint64_t lengthPad[2]; - int i; + uint32_t bytesToPad; + uint64_t lengthPad[2]; + int i; - bytesToPad = 240L - sc->bufferLength; - if (bytesToPad > 128L) - bytesToPad -= 128L; + bytesToPad = 240L - sc->bufferLength; + if (bytesToPad > 128L) + bytesToPad -= 128L; - lengthPad[0] = BYTESWAP64(sc->totalLength[0]); - lengthPad[1] = BYTESWAP64(sc->totalLength[1]); + lengthPad[0] = BYTESWAP64(sc->totalLength[0]); + lengthPad[1] = BYTESWAP64(sc->totalLength[1]); - SHA384Update (sc, padding, bytesToPad); - SHA384Update (sc, lengthPad, 16L); + SHA384Update (sc, padding, bytesToPad); + SHA384Update (sc, lengthPad, 16L); - if (hash) { - for (i = 0; i < SHA384_HASH_WORDS; i++) { + if (hash) { + for (i = 0; i < SHA384_HASH_WORDS; i++) { #ifdef SHA384_FAST_COPY - *((uint64_t *) hash) = BYTESWAP64(sc->hash[i]); + *((uint64_t *) hash) = BYTESWAP64(sc->hash[i]); #else /* SHA384_FAST_COPY */ - hash[0] = (uint8_t) (sc->hash[i] >> 56); - hash[1] = (uint8_t) (sc->hash[i] >> 48); - hash[2] = (uint8_t) (sc->hash[i] >> 40); - hash[3] = (uint8_t) (sc->hash[i] >> 32); - hash[4] = (uint8_t) (sc->hash[i] >> 24); - hash[5] = (uint8_t) (sc->hash[i] >> 16); - hash[6] = (uint8_t) (sc->hash[i] >> 8); - hash[7] = (uint8_t) sc->hash[i]; + hash[0] = (uint8_t) (sc->hash[i] >> 56); + hash[1] = (uint8_t) (sc->hash[i] >> 48); + hash[2] = (uint8_t) (sc->hash[i] >> 40); + hash[3] = (uint8_t) (sc->hash[i] >> 32); + hash[4] = (uint8_t) (sc->hash[i] >> 24); + hash[5] = (uint8_t) (sc->hash[i] >> 16); + hash[6] = (uint8_t) (sc->hash[i] >> 8); + hash[7] = (uint8_t) sc->hash[i]; #endif /* SHA384_FAST_COPY */ - hash += 8; + hash += 8; + } } - } } #ifdef SHA384_TEST @@ -433,56 +415,56 @@ SHA384Final (SHA384Context *sc, uint8_t hash[SHA384_HASH_SIZE]) int main (int argc, char *argv[]) { - SHA384Context foo; - uint8_t hash[SHA384_HASH_SIZE]; - char buf[1000]; - int i; + SHA384Context foo; + uint8_t hash[SHA384_HASH_SIZE]; + char buf[1000]; + int i; - SHA384Init (&foo); - SHA384Update (&foo, "abc", 3); - SHA384Final (&foo, hash); + SHA384Init (&foo); + SHA384Update (&foo, "abc", 3); + SHA384Final (&foo, hash); - for (i = 0; i < SHA384_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } - printf ("\n"); + for (i = 0; i < SHA384_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } + printf ("\n"); - SHA384Init (&foo); - SHA384Update (&foo, - "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" - "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - 112); - SHA384Final (&foo, hash); + SHA384Init (&foo); + SHA384Update (&foo, + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + 112); + SHA384Final (&foo, hash); - for (i = 0; i < SHA384_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } - printf ("\n"); + for (i = 0; i < SHA384_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } + printf ("\n"); - SHA384Init (&foo); - memset (buf, 'a', sizeof (buf)); - for (i = 0; i < 1000; i++) - SHA384Update (&foo, buf, sizeof (buf)); - SHA384Final (&foo, hash); + SHA384Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA384Update (&foo, buf, sizeof (buf)); + SHA384Final (&foo, hash); - for (i = 0; i < SHA384_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } - printf ("\n"); + for (i = 0; i < SHA384_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } + printf ("\n"); - exit (0); + exit (0); } #endif /* SHA384_TEST */ diff --git a/sha384.h b/sha384.h index 443cd6e..c7c17ed 100644 --- a/sha384.h +++ b/sha384.h @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2001-2003 Allan Saddi * All rights reserved. * @@ -23,19 +24,12 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: sha384.h 349 2003-02-23 22:12:21Z asaddi $ */ #ifndef _SHA384_H #define _SHA384_H -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif +#include "platform.h" #define SHA384_HASH_SIZE 48 @@ -43,15 +37,15 @@ #define SHA384_HASH_WORDS 6 struct _SHA384Context { - uint64_t totalLength[2]; - uint64_t hash[SHA384_HASH_WORDS + 2]; - uint32_t bufferLength; - union { - uint64_t words[16]; - uint8_t bytes[128]; - } buffer; + uint64_t totalLength[2]; + uint64_t hash[SHA384_HASH_WORDS + 2]; + uint32_t bufferLength; + union { + uint64_t words[16]; + uint8_t bytes[128]; + } buffer; #ifdef RUNTIME_ENDIAN - int littleEndian; + int littleEndian; #endif /* RUNTIME_ENDIAN */ }; diff --git a/sha384_amiga.c b/sha384_amiga.c new file mode 100644 index 0000000..dd448ef --- /dev/null +++ b/sha384_amiga.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * 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. + * + */ + +#define ARGSFORMAT "FILES/M/A" +#define VERSION_PROG "sha384" +#include "platform.h" +#include "sha384.h" + +const char *vers = VERSION_STRING_AMIGA; + +struct shaargs { + char **files; +}; + +int main(int argc, char **argv) +{ + int i; + FILE *f; + int failure = 0; + struct shaargs args = { NULL }; + + atexit(clean_exit); + alloc_buffer(); + + rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); + if (!rdargs) + { + args_error(argv[0]); + exit(5); + } + + for (i = 0; args.files[i]; i++) { + if ((f = fopen(args.files[i], "rb"))) { + if (!shaFile(args.files[i], f, 0)) + failure = 1; + fclose(f); + } + else { + fprintf(stderr, "%s: %s\n",args.files[i], "No such file or directory"); + failure = 1; + } + } + + exit(failure); +} + +int shaFile (char *name, FILE *f, int which) +{ + SHA384Context s; + size_t len; + uint8_t hash[SHA384_HASH_SIZE]; + int hashLen, i; + int success = 1; + + SHA384Init (&s); + + while ((len = fread (buffer, 1, SHA_BUFFER_SIZE, f)) > 0) { + SHA384Update (&s, buffer, len); + } + + if (ferror (f)) { + fprintf (stderr, "%s: %s\n", name, "Read error"); + success = 0; + } + else { + SHA384Final (&s, hash); + hashLen = SHA384_HASH_SIZE; + + for (i = 0; i < hashLen; i++) + printf ("%02x", hash[i]); + + if (name) + printf (" %s\n", name); + else + printf ("\n"); + } + + memset(&s, 0, sizeof(s)); + + return success; +} diff --git a/sha512.c b/sha512.c index 618389a..8a8b824 100644 --- a/sha512.c +++ b/sha512.c @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2001-2003 Allan Saddi * All rights reserved. * @@ -23,7 +24,6 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: sha512.c 680 2003-07-25 21:58:07Z asaddi $ */ /* @@ -40,27 +40,9 @@ * de0ff244877ea60a 4cb0432ce577c31b eb009c5c2c49aa2e 4eadb217ad8cc09b */ -#ifdef HAVE_CONFIG_H -#include -#endif /* HAVE_CONFIG_H */ - -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif - -#include - +#include "compiler.h" #include "sha512.h" -#ifndef lint -static const char rcsid[] = - "$Id: sha512.c 680 2003-07-25 21:58:07Z asaddi $"; -#endif /* !lint */ - #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) #define ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n)))) @@ -87,46 +69,46 @@ static const char rcsid[] = } static const uint64_t K[80] = { - 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, - 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL, - 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, - 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL, - 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, - 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL, - 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, - 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL, - 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, - 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL, - 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, - 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL, - 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, - 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL, - 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, - 0x06ca6351e003826fLL, 0x142929670a0e6e70LL, - 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, - 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL, - 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, - 0x81c2c92e47edaee6LL, 0x92722c851482353bLL, - 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, - 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL, - 0xd192e819d6ef5218LL, 0xd69906245565a910LL, - 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL, - 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, - 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL, - 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, - 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL, - 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, - 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL, - 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, - 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL, - 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, - 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL, - 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, - 0x113f9804bef90daeLL, 0x1b710b35131c471bLL, - 0x28db77f523047d84LL, 0x32caab7b40c72493LL, - 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL, - 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, - 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL + 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, + 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL, + 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, + 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL, + 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, + 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL, + 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, + 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL, + 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, + 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL, + 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, + 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL, + 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, + 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL, + 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, + 0x06ca6351e003826fLL, 0x142929670a0e6e70LL, + 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, + 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL, + 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, + 0x81c2c92e47edaee6LL, 0x92722c851482353bLL, + 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, + 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL, + 0xd192e819d6ef5218LL, 0xd69906245565a910LL, + 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL, + 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, + 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL, + 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, + 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL, + 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, + 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL, + 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, + 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL, + 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, + 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL, + 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, + 0x113f9804bef90daeLL, 0x1b710b35131c471bLL, + 0x28db77f523047d84LL, 0x32caab7b40c72493LL, + 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL, + 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, + 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL }; #ifndef RUNTIME_ENDIAN @@ -144,9 +126,9 @@ static const uint64_t K[80] = { static inline uint64_t _byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); } #endif /* WORDS_BIGENDIAN */ @@ -162,266 +144,266 @@ static inline uint64_t _byteswap64(uint64_t x) static inline uint64_t __byteswap64(uint64_t x) { - uint32_t a = x >> 32; - uint32_t b = (uint32_t) x; - return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); } static inline uint32_t _byteswap(int littleEndian, uint32_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP(x); + if (!littleEndian) + return x; + else + return _BYTESWAP(x); } static inline uint64_t _byteswap64(int littleEndian, uint64_t x) { - if (!littleEndian) - return x; - else - return _BYTESWAP64(x); + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); } static inline void setEndian(int *littleEndianp) { - union { - uint32_t w; - uint8_t b[4]; - } endian; + union { + uint32_t w; + uint8_t b[4]; + } endian; - endian.w = 1L; - *littleEndianp = endian.b[0] != 0; + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; } #endif /* !RUNTIME_ENDIAN */ static const uint8_t padding[128] = { - 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void SHA512Init (SHA512Context *sc) { #ifdef RUNTIME_ENDIAN - setEndian (&sc->littleEndian); + setEndian (&sc->littleEndian); #endif /* RUNTIME_ENDIAN */ - sc->totalLength[0] = 0LL; - sc->totalLength[1] = 0LL; - sc->hash[0] = 0x6a09e667f3bcc908LL; - sc->hash[1] = 0xbb67ae8584caa73bLL; - sc->hash[2] = 0x3c6ef372fe94f82bLL; - sc->hash[3] = 0xa54ff53a5f1d36f1LL; - sc->hash[4] = 0x510e527fade682d1LL; - sc->hash[5] = 0x9b05688c2b3e6c1fLL; - sc->hash[6] = 0x1f83d9abfb41bd6bLL; - sc->hash[7] = 0x5be0cd19137e2179LL; - sc->bufferLength = 0L; + sc->totalLength[0] = 0LL; + sc->totalLength[1] = 0LL; + sc->hash[0] = 0x6a09e667f3bcc908LL; + sc->hash[1] = 0xbb67ae8584caa73bLL; + sc->hash[2] = 0x3c6ef372fe94f82bLL; + sc->hash[3] = 0xa54ff53a5f1d36f1LL; + sc->hash[4] = 0x510e527fade682d1LL; + sc->hash[5] = 0x9b05688c2b3e6c1fLL; + sc->hash[6] = 0x1f83d9abfb41bd6bLL; + sc->hash[7] = 0x5be0cd19137e2179LL; + sc->bufferLength = 0L; } static void burnStack (int size) { - char buf[128]; + char buf[128]; - memset (buf, 0, sizeof (buf)); - size -= sizeof (buf); - if (size > 0) - burnStack (size); + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); } static void SHA512Guts (SHA512Context *sc, const uint64_t *cbuf) { - uint64_t buf[80]; - uint64_t *W, *W2, *W7, *W15, *W16; - uint64_t a, b, c, d, e, f, g, h; - uint64_t t1, t2; - const uint64_t *Kp; - int i; + uint64_t buf[80]; + uint64_t *W, *W2, *W7, *W15, *W16; + uint64_t a, b, c, d, e, f, g, h; + uint64_t t1, t2; + const uint64_t *Kp; + int i; - W = buf; + W = buf; - for (i = 15; i >= 0; i--) { - *(W++) = BYTESWAP64(*cbuf); - cbuf++; - } + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP64(*cbuf); + cbuf++; + } - W16 = &buf[0]; - W15 = &buf[1]; - W7 = &buf[9]; - W2 = &buf[14]; + W16 = &buf[0]; + W15 = &buf[1]; + W7 = &buf[9]; + W2 = &buf[14]; - for (i = 63; i >= 0; i--) { - *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); - W2++; - W15++; - } + for (i = 63; i >= 0; i--) { + *(W++) = sigma1(*W2) + *(W7++) + sigma0(*W15) + *(W16++); + W2++; + W15++; + } - a = sc->hash[0]; - b = sc->hash[1]; - c = sc->hash[2]; - d = sc->hash[3]; - e = sc->hash[4]; - f = sc->hash[5]; - g = sc->hash[6]; - h = sc->hash[7]; + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; + f = sc->hash[5]; + g = sc->hash[6]; + h = sc->hash[7]; - Kp = K; - W = buf; + Kp = K; + W = buf; - for (i = 79; i >= 0; i--) - DO_ROUND(); + for (i = 79; i >= 0; i--) + DO_ROUND(); - sc->hash[0] += a; - sc->hash[1] += b; - sc->hash[2] += c; - sc->hash[3] += d; - sc->hash[4] += e; - sc->hash[5] += f; - sc->hash[6] += g; - sc->hash[7] += h; + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; + sc->hash[5] += f; + sc->hash[6] += g; + sc->hash[7] += h; } void SHA512Update (SHA512Context *sc, const void *vdata, uint32_t len) { - const uint8_t *data = vdata; - uint32_t bufferBytesLeft; - uint32_t bytesToCopy; - uint64_t carryCheck; - int needBurn = 0; + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + uint64_t carryCheck; + int needBurn = 0; #ifdef SHA512_FAST_COPY - if (sc->bufferLength) { - bufferBytesLeft = 128L - sc->bufferLength; + if (sc->bufferLength) { + bufferBytesLeft = 128L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += bytesToCopy * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 128L) { - SHA512Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; + if (sc->bufferLength == 128L) { + SHA512Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } } - } - while (len > 127) { - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += 1024L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + while (len > 127) { + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += 1024L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - SHA512Guts (sc, data); - needBurn = 1; + SHA512Guts (sc, data); + needBurn = 1; - data += 128L; - len -= 128L; - } + data += 128L; + len -= 128L; + } - if (len) { - memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += len * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += len * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += len; - } + sc->bufferLength += len; + } #else /* SHA512_FAST_COPY */ - while (len) { - bufferBytesLeft = 128L - sc->bufferLength; + while (len) { + bufferBytesLeft = 128L - sc->bufferLength; - bytesToCopy = bufferBytesLeft; - if (bytesToCopy > len) - bytesToCopy = len; + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; - memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); - carryCheck = sc->totalLength[1]; - sc->totalLength[1] += bytesToCopy * 8L; - if (sc->totalLength[1] < carryCheck) - sc->totalLength[0]++; + carryCheck = sc->totalLength[1]; + sc->totalLength[1] += bytesToCopy * 8L; + if (sc->totalLength[1] < carryCheck) + sc->totalLength[0]++; - sc->bufferLength += bytesToCopy; - data += bytesToCopy; - len -= bytesToCopy; + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; - if (sc->bufferLength == 128L) { - SHA512Guts (sc, sc->buffer.words); - needBurn = 1; - sc->bufferLength = 0L; + if (sc->bufferLength == 128L) { + SHA512Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } } - } #endif /* SHA512_FAST_COPY */ - if (needBurn) - burnStack (sizeof (uint64_t[90]) + sizeof (uint64_t *[6]) + sizeof (int)); + if (needBurn) + burnStack (sizeof (uint64_t[90]) + sizeof (uint64_t *[6]) + sizeof (int)); } void SHA512Final (SHA512Context *sc, uint8_t hash[SHA512_HASH_SIZE]) { - uint32_t bytesToPad; - uint64_t lengthPad[2]; - int i; + uint32_t bytesToPad; + uint64_t lengthPad[2]; + int i; - bytesToPad = 240L - sc->bufferLength; - if (bytesToPad > 128L) - bytesToPad -= 128L; + bytesToPad = 240L - sc->bufferLength; + if (bytesToPad > 128L) + bytesToPad -= 128L; - lengthPad[0] = BYTESWAP64(sc->totalLength[0]); - lengthPad[1] = BYTESWAP64(sc->totalLength[1]); + lengthPad[0] = BYTESWAP64(sc->totalLength[0]); + lengthPad[1] = BYTESWAP64(sc->totalLength[1]); - SHA512Update (sc, padding, bytesToPad); - SHA512Update (sc, lengthPad, 16L); + SHA512Update (sc, padding, bytesToPad); + SHA512Update (sc, lengthPad, 16L); - if (hash) { - for (i = 0; i < SHA512_HASH_WORDS; i++) { + if (hash) { + for (i = 0; i < SHA512_HASH_WORDS; i++) { #ifdef SHA384_FAST_COPY - *((uint64_t *) hash) = BYTESWAP64(sc->hash[i]); + *((uint64_t *) hash) = BYTESWAP64(sc->hash[i]); #else /* SHA384_FAST_COPY */ - hash[0] = (uint8_t) (sc->hash[i] >> 56); - hash[1] = (uint8_t) (sc->hash[i] >> 48); - hash[2] = (uint8_t) (sc->hash[i] >> 40); - hash[3] = (uint8_t) (sc->hash[i] >> 32); - hash[4] = (uint8_t) (sc->hash[i] >> 24); - hash[5] = (uint8_t) (sc->hash[i] >> 16); - hash[6] = (uint8_t) (sc->hash[i] >> 8); - hash[7] = (uint8_t) sc->hash[i]; + hash[0] = (uint8_t) (sc->hash[i] >> 56); + hash[1] = (uint8_t) (sc->hash[i] >> 48); + hash[2] = (uint8_t) (sc->hash[i] >> 40); + hash[3] = (uint8_t) (sc->hash[i] >> 32); + hash[4] = (uint8_t) (sc->hash[i] >> 24); + hash[5] = (uint8_t) (sc->hash[i] >> 16); + hash[6] = (uint8_t) (sc->hash[i] >> 8); + hash[7] = (uint8_t) sc->hash[i]; #endif /* SHA384_FAST_COPY */ - hash += 8; + hash += 8; + } } - } } #ifdef SHA512_TEST @@ -433,53 +415,53 @@ SHA512Final (SHA512Context *sc, uint8_t hash[SHA512_HASH_SIZE]) int main (int argc, char *argv[]) { - SHA512Context foo; - uint8_t hash[SHA512_HASH_SIZE]; - char buf[1000]; - int i; + SHA512Context foo; + uint8_t hash[SHA512_HASH_SIZE]; + char buf[1000]; + int i; - SHA512Init (&foo); - SHA512Update (&foo, "abc", 3); - SHA512Final (&foo, hash); + SHA512Init (&foo); + SHA512Update (&foo, "abc", 3); + SHA512Final (&foo, hash); - for (i = 0; i < SHA512_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } + for (i = 0; i < SHA512_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } - SHA512Init (&foo); - SHA512Update (&foo, - "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" - "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - 112); - SHA512Final (&foo, hash); + SHA512Init (&foo); + SHA512Update (&foo, + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + 112); + SHA512Final (&foo, hash); - for (i = 0; i < SHA512_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } + for (i = 0; i < SHA512_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } - SHA512Init (&foo); - memset (buf, 'a', sizeof (buf)); - for (i = 0; i < 1000; i++) - SHA512Update (&foo, buf, sizeof (buf)); - SHA512Final (&foo, hash); + SHA512Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA512Update (&foo, buf, sizeof (buf)); + SHA512Final (&foo, hash); - for (i = 0; i < SHA512_HASH_SIZE;) { - printf ("%02x", hash[i++]); - if (!(i % 8)) - printf (" "); - if (!(i % 32)) - printf ("\n"); - } + for (i = 0; i < SHA512_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 8)) + printf (" "); + if (!(i % 32)) + printf ("\n"); + } - exit (0); + exit (0); } #endif /* SHA512_TEST */ diff --git a/sha512.h b/sha512.h index f590a2e..549e73e 100644 --- a/sha512.h +++ b/sha512.h @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2001-2003 Allan Saddi * All rights reserved. * @@ -23,19 +24,12 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: sha512.h 350 2003-02-23 22:12:33Z asaddi $ */ #ifndef _SHA512_H #define _SHA512_H -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif +#include "platform.h" #define SHA512_HASH_SIZE 64 @@ -43,15 +37,15 @@ #define SHA512_HASH_WORDS 8 struct _SHA512Context { - uint64_t totalLength[2]; - uint64_t hash[SHA512_HASH_WORDS]; - uint32_t bufferLength; - union { - uint64_t words[16]; - uint8_t bytes[128]; - } buffer; + uint64_t totalLength[2]; + uint64_t hash[SHA512_HASH_WORDS]; + uint32_t bufferLength; + union { + uint64_t words[16]; + uint8_t bytes[128]; + } buffer; #ifdef RUNTIME_ENDIAN - int littleEndian; + int littleEndian; #endif /* RUNTIME_ENDIAN */ }; diff --git a/sha512_amiga.c b/sha512_amiga.c new file mode 100644 index 0000000..b716ccc --- /dev/null +++ b/sha512_amiga.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * 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. + * + */ + +#define ARGSFORMAT "FILES/M/A" +#define VERSION_PROG "sha512" +#include "platform.h" +#include "sha512.h" + +const char *vers = VERSION_STRING_AMIGA; + +struct shaargs { + char **files; +}; + +int main(int argc, char **argv) +{ + int i; + FILE *f; + int failure = 0; + struct shaargs args = { NULL }; + + atexit(clean_exit); + alloc_buffer(); + + rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); + if (!rdargs) + { + args_error(argv[0]); + exit(5); + } + + for (i = 0; args.files[i]; i++) { + if ((f = fopen(args.files[i], "rb"))) { + if (!shaFile(args.files[i], f, 0)) + failure = 1; + fclose(f); + } + else { + fprintf(stderr, "%s: %s\n",args.files[i], "No such file or directory"); + failure = 1; + } + } + + exit(failure); +} + +int shaFile (char *name, FILE *f, int which) +{ + SHA512Context s; + size_t len; + uint8_t hash[SHA512_HASH_SIZE]; + int hashLen, i; + int success = 1; + + SHA512Init (&s); + + while ((len = fread (buffer, 1, SHA_BUFFER_SIZE, f)) > 0) { + SHA512Update (&s, buffer, len); + } + + if (ferror (f)) { + fprintf (stderr, "%s: %s\n", name, "Read error"); + success = 0; + } + else { + SHA512Final (&s, hash); + hashLen = SHA512_HASH_SIZE; + + for (i = 0; i < hashLen; i++) + printf ("%02x", hash[i]); + + if (name) + printf (" %s\n", name); + else + printf ("\n"); + } + + memset(&s, 0, sizeof(s)); + + return success; +} diff --git a/sha_amiga.c b/sha_amiga.c new file mode 100644 index 0000000..9c82526 --- /dev/null +++ b/sha_amiga.c @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * 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. + * + */ + +#define ARGSFORMAT "S1=SHA1/S,S2=SHA256/S,S3=SHA384/S,S5=SHA512/K,FILES/M,H=HELP/K" +#include "platform.h" + +const char *vers = VERSION_STRING_AMIGA; + +struct shaargs { + long sha1; + long sha256; + long sha384; + long sha512; + char **files; + long help; +}; + +int main(int argc, char **argv) +{ + int i; + FILE *f; + int opt = 0; + int which = 0; + int failure = 0; + struct shaargs args = { + FALSE, FALSE, FALSE, FALSE, NULL, FALSE + }; + + atexit(clean_exit); + alloc_buffer(); + + rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); + if (!rdargs) + { + args_error(argv[0]); + exit(5); + } + + if (args.sha1) { + which = 1; + opt++; + } + + if (args.sha256) { + which = 2; + opt++; + } + + if (args.sha384) { + which = 3; + opt++; + } + + if (args.sha512) { + which = 4; + opt++; + } + + if (args.help || opt != 1) { + fprintf (stderr, + "Options:\n" + "S1\tUse SHA-1\n" + "S2\tUse SHA-256\n" + "S3\tUse SHA-384\n" + "S5\tUse SHA-512\n" + "H\tDisplay this summary\n\n" + "Only one of S1, S2, S3, S5 may be specified\n"); + exit(1); + } + + for (i = 0; args.files[i]; i++) { + if ((f = fopen(args.files[i], "rb"))) { + if (!shaFile(args.files[i], f, which)) + failure = 1; + fclose(f); + } + else { + fprintf(stderr, "%s: %s\n",args.files[i], "No such file or directory"); + failure = 1; + } + } + + exit(failure); +} + diff --git a/sha_io.c b/sha_io.c new file mode 100644 index 0000000..41936d0 --- /dev/null +++ b/sha_io.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * 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 "platform.h" + +uint8_t *buffer = NULL; +void *alloced_buffer = NULL; +struct RDArgs *rdargs = NULL; + +void args_error(char *program) { + fprintf(stderr, "%s: %s\n", program, "Wrong arguments"); +} + +void alloc_buffer(void) { + long offs; + + if (!(buffer = AllocVec(SHA_BUFFER_SIZE + 7, MEMF_ANY | MEMF_CLEAR))) { + exit(1); + } + + alloced_buffer = buffer; + + if ((offs = (long) buffer & 7L)) + buffer += 8 - offs; +} + +void clean_exit() +{ + if (alloced_buffer != NULL) { + memset(buffer, 0, SHA_BUFFER_SIZE); + FreeVec(alloced_buffer); + } + + if (rdargs) { + FreeArgs(rdargs); + rdargs = NULL; + } +} diff --git a/shatest.c b/shatest.c index b1dfe76..c6ea685 100644 --- a/shatest.c +++ b/shatest.c @@ -1,4 +1,5 @@ /*- + * Copyright (c) 2014-2015 Carsten Larsen * Copyright (c) 2003 Allan Saddi * All rights reserved. * @@ -23,32 +24,16 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id$ */ -#ifdef HAVE_CONFIG_H -#include -#endif /* HAVE_CONFIG_H */ - -#if HAVE_INTTYPES_H -# include -#else -# if HAVE_STDINT_H -# include -# endif -#endif - -#include -#include -#include - +#define VERSION_PROG "shatest" +#include "platform.h" #include "sha1.h" #include "sha256.h" #include "sha384.h" #include "sha512.h" -// NOTICE: Dummy -char *prog; +const char *vers = VERSION_STRING_AMIGA; static uint8_t tv1[3] = { 0x61, 0x62, 0x63 @@ -165,11 +150,12 @@ main (int argc, char *argv[]) uint8_t sha384[SHA384_HASH_SIZE]; uint8_t sha512[SHA512_HASH_SIZE]; } h; - uint8_t buf[1000]; int i; int retval = 0; - - memset (buf, 0x61, sizeof (buf)); + int testbuffer = 1000; + + alloc_buffer(); + memset (buffer, 0x61, testbuffer); printf ("SHA-1:\n"); @@ -198,7 +184,7 @@ main (int argc, char *argv[]) printf (" Test vector #3: "); SHA1Init (&s.sha1); for (i = 0; i < 1000; i++) - SHA1Update (&s.sha1, buf, sizeof (buf)); + SHA1Update (&s.sha1, buffer, testbuffer); SHA1Final (&s.sha1, h.sha1); if (!memcmp (h.sha1, res1[2], sizeof (h.sha1))) printf ("PASS\n"); @@ -234,7 +220,7 @@ main (int argc, char *argv[]) printf (" Test vector #3: "); SHA256Init (&s.sha256); for (i = 0; i < 1000; i++) - SHA256Update (&s.sha256, buf, sizeof (buf)); + SHA256Update (&s.sha256, buffer, testbuffer); SHA256Final (&s.sha256, h.sha256); if (!memcmp (h.sha256, res256[2], sizeof (h.sha256))) printf ("PASS\n"); @@ -270,7 +256,7 @@ main (int argc, char *argv[]) printf (" Test vector #3: "); SHA384Init (&s.sha384); for (i = 0; i < 1000; i++) - SHA384Update (&s.sha384, buf, sizeof (buf)); + SHA384Update (&s.sha384, buffer, testbuffer); SHA384Final (&s.sha384, h.sha384); if (!memcmp (h.sha384, res384[2], sizeof (h.sha384))) printf ("PASS\n"); @@ -306,7 +292,7 @@ main (int argc, char *argv[]) printf (" Test vector #3: "); SHA512Init (&s.sha512); for (i = 0; i < 1000; i++) - SHA512Update (&s.sha512, buf, sizeof (buf)); + SHA512Update (&s.sha512, buffer, testbuffer); SHA512Final (&s.sha512, h.sha512); if (!memcmp (h.sha512, res512[2], sizeof (h.sha512))) printf ("PASS\n"); @@ -315,5 +301,7 @@ main (int argc, char *argv[]) retval = 1; } + clean_exit(); + return retval; } diff --git a/version.h b/version.h deleted file mode 100644 index e018412..0000000 --- a/version.h +++ /dev/null @@ -1,5 +0,0 @@ -#define VERSION_MAJOR 1 -#define VERSION_MINOR 0 -#define VERSION_PATCHLEVEL 4 -#define VERSION_STRING "1.0.4" -#define VERSION_DATE "2003-Jul-25"