diff options
Diffstat (limited to 'sha1')
| -rw-r--r-- | sha1/.gitignore | 1 | ||||
| -rw-r--r-- | sha1/Makefile.am | 27 | ||||
| -rw-r--r-- | sha1/configure.in | 156 | ||||
| -rw-r--r-- | sha1/hmac.c | 62 | ||||
| -rw-r--r-- | sha1/sha1.c | 192 | ||||
| -rw-r--r-- | sha1/sha1.h | 110 | ||||
| -rw-r--r-- | sha1/sha1_hash.c | 90 | ||||
| -rw-r--r-- | sha1/sha256.c | 205 | ||||
| -rw-r--r-- | sha1/sha256_hash.c | 45 | ||||
| -rw-r--r-- | sha1/sha512.c | 232 | ||||
| -rw-r--r-- | sha1/sha512_hash.c | 45 | ||||
| -rw-r--r-- | sha1/testsuite.c | 96 | ||||
| -rw-r--r-- | sha1/testsuite.txt | 9 |
13 files changed, 1270 insertions, 0 deletions
diff --git a/sha1/.gitignore b/sha1/.gitignore new file mode 100644 index 0000000..a1b232f --- /dev/null +++ b/sha1/.gitignore @@ -0,0 +1 @@ +/testsuite diff --git a/sha1/Makefile.am b/sha1/Makefile.am new file mode 100644 index 0000000..dd561e6 --- /dev/null +++ b/sha1/Makefile.am @@ -0,0 +1,27 @@ +# +# Copyright 2001-2008 Double Precision, Inc. +# See COPYING for distribution information. +# + + +if HMACC +HMAC=hmac.c +else +HMAC= +endif + +noinst_LTLIBRARIES=libsha1.la + +libsha1_la_SOURCES=sha1.c sha1.h sha1_hash.c sha256.c sha256_hash.c sha512.c sha512_hash.c $(HMAC) + +noinst_PROGRAMS=testsuite + +testsuite_SOURCES=testsuite.c +testsuite_DEPENDENCIES=libsha1.la +testsuite_LDADD=$(testsuite_DEPENDENCIES) +testsuite_LDFLAGS=-static + +EXTRA_DIST=testsuite.txt hmac.c + +check-am: + ./testsuite | cmp -s - $(srcdir)/testsuite.txt diff --git a/sha1/configure.in b/sha1/configure.in new file mode 100644 index 0000000..54572b4 --- /dev/null +++ b/sha1/configure.in @@ -0,0 +1,156 @@ +dnl Process this file with autoconf to produce a configure script. +dnl +dnl Copyright 2001-2004 Double Precision, Inc. +dnl See COPYING for distribution information. + +AC_PREREQ(2.59) +AC_INIT(libsha1, 1.21, courier-users@lists.sourceforge.net) + +>confdefs.h # Kill PACKAGE_ macros + +AC_CONFIG_SRCDIR([hmac.c]) +AC_CONFIG_AUX_DIR(../..) +AM_CONFIG_HEADER([config.h]) +AM_INIT_AUTOMAKE([foreign no-define]) + +AM_CONDITIONAL(HMACC, test -d ${srcdir}/../libhmac) + +dnl Checks for programs. +AC_PROG_CC +AC_PROG_INSTALL +AC_PROG_LN_S +AC_PROG_LIBTOOL + +if test "$GCC" = yes +then + CFLAGS="-Wall $CFLAGS" +fi +CFLAGS="$CFLAGS -I$srcdir/.. -I.." + +dnl Checks for libraries. + +dnl Checks for header files. + +AC_CHECK_HEADERS(sys/types.h) + +AC_ARG_WITH(int32, +[ --with-int32='type' use 'type' for an unsigned 32 bit integer type + ( default is 'unsigned')], + int32="$withval", [ + + AC_MSG_CHECKING(for uint32_t) + + AC_TRY_COMPILE([ +#if HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif + ],[ + uint32_t i=0; + ], [ AC_MSG_RESULT(yes) ; int32="uint32_t"], [ + + AC_MSG_RESULT(no) + AC_MSG_CHECKING(for u_int_32_t) + + AC_TRY_COMPILE([ +#if HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif + ],[ + u_int32_t i=0; + ], [AC_MSG_RESULT(yes); int32="u_int32_t"],[ + + AC_MSG_RESULT(no) + + AC_CHECK_SIZEOF(unsigned, 0) + if test "$ac_cv_sizeof_unsigned" != 4 + then + AC_CHECK_SIZEOF(unsigned long, 0) + if test "$ac_cv_sizeof_unsigned_long" != 4 + then + AC_CHECK_SIZEOF(unsigned short, 0) + if test "$ac_cv_sizeof_unsigned_short" != 4 + then + AC_ERROR(--with-int32 option is required) + fi + int32="unsigned short" + else + int32="unsigned long" + fi + else + int32="unsigned" + fi + ]) + ]) + ] +) +UINT32="$int32" + +AC_DEFINE_UNQUOTED(SHA1_WORD, $UINT32, [ 32 bit data type ]) + +AC_ARG_WITH(int64, +[ --with-int64='type' use 'type' for an unsigned 64 bit integer type + ( default is 'unsigned')], + int64="$withval", [ + + AC_MSG_CHECKING(for uint64_t) + + AC_TRY_COMPILE([ +#if HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif + ],[ + uint64_t i=0; + ], [ AC_MSG_RESULT(yes) ; int64="uint64_t"], [ + + AC_MSG_RESULT(no) + AC_MSG_CHECKING(for u_int_64_t) + + AC_TRY_COMPILE([ +#if HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif + ],[ + u_int64_t i=0; + ], [AC_MSG_RESULT(yes); int64="u_int64_t"],[ + + AC_MSG_RESULT(no) + + AC_CHECK_SIZEOF(unsigned, 0) + if test "$ac_cv_sizeof_unsigned" != 8 + then + AC_CHECK_SIZEOF(unsigned long, 0) + if test "$ac_cv_sizeof_unsigned_long" != 8 + then + AC_CHECK_SIZEOF(unsigned long long, 0) + if test "$ac_cv_sizeof_unsigned_long_long" != 8 + then + AC_CHECK_SIZEOF(unsigned short, 0) + if test "$ac_cv_sizeof_unsigned_short" != 8 + then + AC_ERROR(--with-int64 option is required) + fi + int64="unsigned short" + else + int64="unsigned long long" + fi + else + int64="unsigned long" + fi + else + int64="unsigned" + fi + ]) + ]) + ] +) +UINT64="$int64" + +AC_DEFINE_UNQUOTED(SHA512_WORD, $UINT64, [ 64 bit data type ]) +dnl Checks for typedefs, structures, and compiler characteristics. +AC_C_CONST +AC_SYS_LARGEFILE + +dnl Checks for library functions. +AC_HEADER_STDC + +AC_OUTPUT(Makefile) diff --git a/sha1/hmac.c b/sha1/hmac.c new file mode 100644 index 0000000..12e1833 --- /dev/null +++ b/sha1/hmac.c @@ -0,0 +1,62 @@ +/* +** Copyright 2001-2005 Double Precision, Inc. +** See COPYING for distribution information. +*/ +#define SHA1_INTERNAL +#include "sha1.h" +#include "../libhmac/hmac.h" + + +static void alloc_context_sha1( void (*func)(void *, void *), void *arg) +{ +struct SHA1_CONTEXT c; + + (*func)((void *)&c, arg); +} + +static void alloc_hash_sha1( void (*func)(unsigned char *, void *), void *arg) +{ +unsigned char c[SHA1_DIGEST_SIZE]; + + (*func)(c, arg); +} + +struct hmac_hashinfo hmac_sha1 = { + "sha1", + SHA1_BLOCK_SIZE, + SHA1_DIGEST_SIZE, + sizeof(struct SHA1_CONTEXT), + (void (*)(void *))sha1_context_init, + (void (*)(void *, const void *, unsigned))sha1_context_hashstream, + (void (*)(void *, unsigned long))sha1_context_endstream, + (void (*)(void *, unsigned char *))sha1_context_digest, + (void (*)(void *, const unsigned char *))sha1_context_restore, + alloc_context_sha1, + alloc_hash_sha1}; + +static void alloc_context_sha256( void (*func)(void *, void *), void *arg) +{ +struct SHA256_CONTEXT c; + + (*func)((void *)&c, arg); +} + +static void alloc_hash_sha256( void (*func)(unsigned char *, void *), void *arg) +{ +unsigned char c[SHA256_DIGEST_SIZE]; + + (*func)(c, arg); +} + +struct hmac_hashinfo hmac_sha256 = { + "sha256", + SHA256_BLOCK_SIZE, + SHA256_DIGEST_SIZE, + sizeof(struct SHA256_CONTEXT), + (void (*)(void *))sha256_context_init, + (void (*)(void *, const void *, unsigned))sha256_context_hashstream, + (void (*)(void *, unsigned long))sha256_context_endstream, + (void (*)(void *, unsigned char *))sha256_context_digest, + (void (*)(void *, const unsigned char *))sha256_context_restore, + alloc_context_sha256, + alloc_hash_sha256}; diff --git a/sha1/sha1.c b/sha1/sha1.c new file mode 100644 index 0000000..089ca3a --- /dev/null +++ b/sha1/sha1.c @@ -0,0 +1,192 @@ +/* +** Copyright 2001 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#define SHA1_INTERNAL +#include "sha1.h" + +#include <string.h> +#include <stdlib.h> + + +#define K0 0x5A827999 +#define K1 0x6ED9EBA1 +#define K2 0x8F1BBCDC +#define K3 0XCA62C1D6 + +#define K20(x) x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x + +static SHA1_WORD K[80] = { K20(K0), K20(K1), K20(K2), K20(K3) }; + +void sha1_context_init(struct SHA1_CONTEXT *c) +{ + if (sizeof(SHA1_WORD) != 4) + abort(); + + c->H[0] = 0x67452301; + c->H[1] = 0xEFCDAB89; + c->H[2] = 0x98BADCFE; + c->H[3] = 0x10325476; + c->H[4] = 0xC3D2E1F0; + c->blk_ptr=0; +} + +void sha1_context_hash(struct SHA1_CONTEXT *c, + const unsigned char blk[SHA1_BLOCK_SIZE]) +{ +SHA1_WORD A,B,C,D,E; +SHA1_WORD TEMP; +SHA1_WORD W[80]; +unsigned i, t; + +#define f(t,B,C,D) ( \ + (t) < 20 ? ( (B) & (C) ) | ( (~(B)) & (D) ) : \ + (t) >= 40 && (t) < 60 ? ( (B) & (C) ) | ( (B) & (D) ) | ( (C) & (D) ):\ + (B) ^ (C) ^ (D) ) + +#define S(a,b) ( ((SHA1_WORD)(a) << (b)) | ((SHA1_WORD)(a) >> (32 - (b)))) + + for (i=t=0; t<16; t++) + { + W[t]= blk[i]; i++; + W[t] = (W[t] << 8) | blk[i]; i++; + W[t] = (W[t] << 8) | blk[i]; i++; + W[t] = (W[t] << 8) | blk[i]; i++; + } + + for (t=16; t<80; t++) + { + TEMP= W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]; + W[t]= S(TEMP, 1); + } + + A=c->H[0]; + B=c->H[1]; + C=c->H[2]; + D=c->H[3]; + E=c->H[4]; + + for (t=0; t<80; t++) + { + TEMP = S(A,5); + TEMP += f(t, B, C, D); + TEMP += E; + TEMP += W[t]; + TEMP += K[t]; + + E=D; + D=C; + C= S(B, 30); + B=A; + A=TEMP; + } + + c->H[0] += A; + c->H[1] += B; + c->H[2] += C; + c->H[3] += D; + c->H[4] += E; +} + +void sha1_context_hashstream(struct SHA1_CONTEXT *c, const void *p, unsigned l) +{ +const unsigned char *cp=(const unsigned char *)p; +unsigned ll; + + while (l) + { + if (c->blk_ptr == 0 && l >= SHA1_BLOCK_SIZE) + { + sha1_context_hash(c, cp); + cp += SHA1_BLOCK_SIZE; + l -= SHA1_BLOCK_SIZE; + continue; + } + + ll=l; + if (ll > SHA1_BLOCK_SIZE - c->blk_ptr) + ll=SHA1_BLOCK_SIZE - c->blk_ptr; + memcpy(c->blk + c->blk_ptr, cp, ll); + c->blk_ptr += ll; + cp += ll; + l -= ll; + if (c->blk_ptr >= SHA1_BLOCK_SIZE) + { + sha1_context_hash(c, c->blk); + c->blk_ptr=0; + } + } +} + +void sha1_context_endstream(struct SHA1_CONTEXT *c, unsigned long l) +{ + unsigned char buf[8]; + static const unsigned char zero[SHA1_BLOCK_SIZE-8]; + + buf[0]=0x80; + sha1_context_hashstream(c, &buf, 1); + while (c->blk_ptr != SHA1_BLOCK_SIZE-8) + { + if (c->blk_ptr > SHA1_BLOCK_SIZE-8) + { + sha1_context_hashstream(c, zero, + SHA1_BLOCK_SIZE - c->blk_ptr); + continue; + } + sha1_context_hashstream(c, zero, + SHA1_BLOCK_SIZE-8-c->blk_ptr); + } + + l *= 8; + buf[7] = l; + buf[6] = (l >>= 8); + buf[5] = (l >>= 8); + buf[4] = (l >> 8); + buf[3]=buf[2]=buf[1]=buf[0]=0; + + sha1_context_hashstream(c, buf, 8); +} + +void sha1_context_digest(struct SHA1_CONTEXT *c, SHA1_DIGEST d) +{ +unsigned char *dp=d + SHA1_DIGEST_SIZE; +unsigned i; + + for ( i=5; i; ) + { + SHA1_WORD w=c->H[--i]; + + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; + } +} + +void sha1_context_restore(struct SHA1_CONTEXT *c, const SHA1_DIGEST d) +{ +const unsigned char *dp=d; +unsigned i; + + for (i=0; i<5; i++) + { + SHA1_WORD w= *dp++; + + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + c->H[i]=w; + } + c->blk_ptr=0; +} + +void sha1_digest(const void *msg, unsigned len, SHA1_DIGEST d) +{ +struct SHA1_CONTEXT c; + + sha1_context_init( &c ); + sha1_context_hashstream(&c, msg, len); + sha1_context_endstream(&c, len); + sha1_context_digest( &c, d ); +} diff --git a/sha1/sha1.h b/sha1/sha1.h new file mode 100644 index 0000000..3bd397c --- /dev/null +++ b/sha1/sha1.h @@ -0,0 +1,110 @@ +#ifndef sha1_h +#define sha1_h + +/* +** Copyright 2001-2008 Double Precision, Inc. +** See COPYING for distribution information. +*/ + + +#if HAVE_CONFIG_H +#include "sha1/config.h" +#endif + +#if HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif + +#define SHA1_DIGEST_SIZE 20 +#define SHA1_BLOCK_SIZE 64 + +#define SHA256_DIGEST_SIZE 32 +#define SHA256_BLOCK_SIZE 64 + +#define SHA512_DIGEST_SIZE 64 +#define SHA512_BLOCK_SIZE 128 + +typedef SHA1_WORD SHA256_WORD; + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned char SHA1_DIGEST[20]; +typedef unsigned char SHA256_DIGEST[32]; +typedef unsigned char SHA512_DIGEST[64]; + +#ifdef SHA1_INTERNAL + +struct SHA1_CONTEXT { + + SHA1_WORD H[5]; + + unsigned char blk[SHA1_BLOCK_SIZE]; + unsigned blk_ptr; + } ; + +struct SHA256_CONTEXT { + + SHA256_WORD H[8]; + + unsigned char blk[SHA256_BLOCK_SIZE]; + unsigned blk_ptr; + } ; + +struct SHA512_CONTEXT { + + SHA512_WORD H[8]; + + unsigned char blk[SHA512_BLOCK_SIZE]; + unsigned blk_ptr; + } ; + +void sha1_context_init(struct SHA1_CONTEXT *); +void sha1_context_hash(struct SHA1_CONTEXT *, + const unsigned char[SHA1_BLOCK_SIZE]); +void sha1_context_hashstream(struct SHA1_CONTEXT *, const void *, unsigned); +void sha1_context_endstream(struct SHA1_CONTEXT *, unsigned long); +void sha1_context_digest(struct SHA1_CONTEXT *, SHA1_DIGEST); +void sha1_context_restore(struct SHA1_CONTEXT *, const SHA1_DIGEST); + +void sha256_context_init(struct SHA256_CONTEXT *); +void sha256_context_hash(struct SHA256_CONTEXT *, + const unsigned char[SHA256_BLOCK_SIZE]); +void sha256_context_hashstream(struct SHA256_CONTEXT *, + const void *, unsigned); +void sha256_context_endstream(struct SHA256_CONTEXT *, unsigned long); +void sha256_context_digest(struct SHA256_CONTEXT *, SHA256_DIGEST); +void sha256_context_restore(struct SHA256_CONTEXT *, const SHA256_DIGEST); + +void sha512_context_init(struct SHA512_CONTEXT *); +void sha512_context_hash(struct SHA512_CONTEXT *, + const unsigned char[SHA512_BLOCK_SIZE]); +void sha512_context_hashstream(struct SHA512_CONTEXT *, + const void *, unsigned); +void sha512_context_endstream(struct SHA512_CONTEXT *, SHA512_WORD); +void sha512_context_digest(struct SHA512_CONTEXT *, SHA512_DIGEST); +void sha512_context_restore(struct SHA512_CONTEXT *, const SHA512_DIGEST); + +#endif + +void sha1_digest(const void *, unsigned, SHA1_DIGEST); +const char *sha1_hash(const char *); + +typedef unsigned char SSHA_RAND[4]; + +const char *ssha_hash(const char *, SSHA_RAND); + +void sha256_digest(const void *, unsigned, SHA256_DIGEST); + +const char *sha256_hash(const char *); + +void sha512_digest(const void *, unsigned, SHA512_DIGEST); + +const char *sha512_hash(const char *); + +#ifdef __cplusplus + } ; +#endif + +#endif diff --git a/sha1/sha1_hash.c b/sha1/sha1_hash.c new file mode 100644 index 0000000..18b330c --- /dev/null +++ b/sha1/sha1_hash.c @@ -0,0 +1,90 @@ +/* +** Copyright 2001-2008 Double Precision, Inc. +** See COPYING for distribution information. +*/ +#define SHA1_INTERNAL +#include "sha1.h" +#include <string.h> + + +static const char base64tab[]= +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +const char *sha1_hash(const char *passw) +{ +SHA1_DIGEST sha1buf; +static char hash_buffer[1+(sizeof(sha1buf)+2)/3*4]; +int a=0,b=0,c=0; +int i, j; +int d, e, f, g; + + sha1_digest(passw, strlen(passw), sha1buf); + + j=0; + + for (i=0; i<sizeof(sha1buf); i += 3) + { + a=sha1buf[i]; + b= i+1 < sizeof(sha1buf) ? sha1buf[i+1]:0; + c= i+2 < sizeof(sha1buf) ? sha1buf[i+2]:0; + + d=base64tab[ a >> 2 ]; + e=base64tab[ ((a & 3 ) << 4) | (b >> 4)]; + f=base64tab[ ((b & 15) << 2) | (c >> 6)]; + g=base64tab[ c & 63 ]; + if (i + 1 >= sizeof(sha1buf)) f='='; + if (i + 2 >= sizeof(sha1buf)) g='='; + hash_buffer[j++]=d; + hash_buffer[j++]=e; + hash_buffer[j++]=f; + hash_buffer[j++]=g; + } + + hash_buffer[j]=0; + return (hash_buffer); +} + +const char *ssha_hash(const char *passw, SSHA_RAND seed) +{ + unsigned char sha1buf[sizeof(SHA1_DIGEST)+sizeof(SSHA_RAND)]; + + static char hash_buffer[1+(sizeof(sha1buf)+2)/3*4]; + int a=0,b=0,c=0; + int i, j; + int d, e, f, g; + struct SHA1_CONTEXT ctx; + + sha1_context_init( &ctx ); + sha1_context_hashstream(&ctx, passw, strlen(passw)); + sha1_context_hashstream(&ctx, seed, sizeof(SSHA_RAND)); + sha1_context_endstream(&ctx, strlen(passw)+sizeof(SSHA_RAND)); + sha1_context_digest( &ctx, sha1buf ); + + for(i=0; i<sizeof(SSHA_RAND); i++) + { + sha1buf[sizeof(SHA1_DIGEST)+i] = seed[i]; + } + + j=0; + + for (i=0; i<sizeof(sha1buf); i += 3) + { + a=sha1buf[i]; + b= i+1 < sizeof(sha1buf) ? sha1buf[i+1]:0; + c= i+2 < sizeof(sha1buf) ? sha1buf[i+2]:0; + + d=base64tab[ a >> 2 ]; + e=base64tab[ ((a & 3 ) << 4) | (b >> 4)]; + f=base64tab[ ((b & 15) << 2) | (c >> 6)]; + g=base64tab[ c & 63 ]; + if (i + 1 >= sizeof(sha1buf)) f='='; + if (i + 2 >= sizeof(sha1buf)) g='='; + hash_buffer[j++]=d; + hash_buffer[j++]=e; + hash_buffer[j++]=f; + hash_buffer[j++]=g; + } + + hash_buffer[j]=0; + return (hash_buffer); +} diff --git a/sha1/sha256.c b/sha1/sha256.c new file mode 100644 index 0000000..15cfae9 --- /dev/null +++ b/sha1/sha256.c @@ -0,0 +1,205 @@ +/* +** Copyright 2005 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#define SHA1_INTERNAL +#include "sha1.h" + +#include <string.h> +#include <stdlib.h> + + +#define ROTR(x,n) ((SHA256_WORD)(((SHA256_WORD)(x) >> (n))|((x) << (32-(n))))) + +#define ROTL(x,n) ((SHA256_WORD)(((SHA256_WORD)(x) << (n))|((x) >> (32-(n))))) + + +#define CH(x,y,z) ((SHA256_WORD)(((x) & (y)) ^ ((~(x))&(z)))) +#define MAJ(x,y,z) ((SHA256_WORD)(((x)&(y))^((x)&(z))^((y)&(z)))) + +#define SUM0(x) ((SHA256_WORD)(ROTR((x),2)^ROTR((x),13)^ROTR((x),22))) +#define SUM1(x) ((SHA256_WORD)(ROTR((x),6)^ROTR((x),11)^ROTR((x),25))) + +#define TH0(x) ((SHA256_WORD)(ROTR((x),7)^ROTR((x),18)^((SHA256_WORD)(x)>>3))) +#define TH1(x) ((SHA256_WORD)(ROTR((x),17)^ROTR((x),19)^((SHA256_WORD)(x)>>10))) + +static const SHA256_WORD K[64]= + {0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, + 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, + 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, + 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, + 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, + 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, + 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, + 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2}; + +void sha256_context_init(struct SHA256_CONTEXT *c) +{ + if (sizeof(SHA256_WORD) != 4) + abort(); + + c->H[0] = 0x6A09E667; + c->H[1] = 0xBB67AE85; + c->H[2] = 0x3C6EF372; + c->H[3] = 0xA54FF53A; + c->H[4] = 0x510E527F; + c->H[5] = 0x9B05688C; + c->H[6] = 0x1F83D9AB; + c->H[7] = 0x5BE0CD19; + c->blk_ptr=0; +} + +void sha256_context_hash(struct SHA256_CONTEXT *cc, + const unsigned char blk[SHA256_BLOCK_SIZE]) +{ + SHA256_WORD W[64]; + unsigned i, t; + SHA256_WORD a,b,c,d,e,f,g,h; + + for (i=t=0; t<16; t++) + { + SHA256_WORD x=blk[i]; i++; + + x=(x << 8) | blk[i]; i++; + x=(x << 8) | blk[i]; i++; + W[t]=(x << 8) | blk[i]; i++; + } + + for (t=16; t<64; t++) + W[t]= TH1(W[t-2]) + W[t-7] + TH0(W[t-15]) + W[t-16]; + + a=cc->H[0]; + b=cc->H[1]; + c=cc->H[2]; + d=cc->H[3]; + e=cc->H[4]; + f=cc->H[5]; + g=cc->H[6]; + h=cc->H[7]; + + for (t=0; t<64; t++) + { + SHA256_WORD T1=h + SUM1(e) + CH(e,f,g) + K[t] + W[t]; + SHA256_WORD T2=SUM0(a)+MAJ(a,b,c); + h=g; + g=f; + f=e; + e=d+T1; + d=c; + c=b; + b=a; + a=T1+T2; + } + + cc->H[0] += a; + cc->H[1] += b; + cc->H[2] += c; + cc->H[3] += d; + cc->H[4] += e; + cc->H[5] += f; + cc->H[6] += g; + cc->H[7] += h; +} + +void sha256_context_hashstream(struct SHA256_CONTEXT *c, const void *p, unsigned l) +{ +const unsigned char *cp=(const unsigned char *)p; +unsigned ll; + + while (l) + { + if (c->blk_ptr == 0 && l >= SHA256_BLOCK_SIZE) + { + sha256_context_hash(c, cp); + cp += SHA256_BLOCK_SIZE; + l -= SHA256_BLOCK_SIZE; + continue; + } + + ll=l; + if (ll > SHA256_BLOCK_SIZE - c->blk_ptr) + ll=SHA256_BLOCK_SIZE - c->blk_ptr; + memcpy(c->blk + c->blk_ptr, cp, ll); + c->blk_ptr += ll; + cp += ll; + l -= ll; + if (c->blk_ptr >= SHA256_BLOCK_SIZE) + { + sha256_context_hash(c, c->blk); + c->blk_ptr=0; + } + } +} + +void sha256_context_endstream(struct SHA256_CONTEXT *c, unsigned long l) +{ + unsigned char buf[8]; + static const unsigned char zero[SHA256_BLOCK_SIZE-8]; + + buf[0]=0x80; + sha256_context_hashstream(c, &buf, 1); + while (c->blk_ptr != SHA256_BLOCK_SIZE-8) + { + if (c->blk_ptr > SHA256_BLOCK_SIZE-8) + { + sha256_context_hashstream(c, zero, + SHA256_BLOCK_SIZE - c->blk_ptr); + continue; + } + sha256_context_hashstream(c, zero, + SHA256_BLOCK_SIZE-8-c->blk_ptr); + } + + l *= 8; + buf[7] = l; + buf[6] = (l >>= 8); + buf[5] = (l >>= 8); + buf[4] = (l >> 8); + buf[3]=buf[2]=buf[1]=buf[0]=0; + + sha256_context_hashstream(c, buf, 8); +} + +void sha256_context_digest(struct SHA256_CONTEXT *c, SHA256_DIGEST d) +{ + unsigned char *dp=d + SHA256_DIGEST_SIZE; + unsigned i; + + for ( i=8; i; ) + { + SHA256_WORD w=c->H[--i]; + + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; + } +} + +void sha256_context_restore(struct SHA256_CONTEXT *c, const SHA256_DIGEST d) +{ + const unsigned char *dp=d; + unsigned i; + + for (i=0; i<8; i++) + { + SHA256_WORD w= *dp++; + + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + c->H[i]=w; + } + c->blk_ptr=0; +} + +void sha256_digest(const void *msg, unsigned len, SHA256_DIGEST d) +{ + struct SHA256_CONTEXT c; + + sha256_context_init( &c ); + sha256_context_hashstream(&c, msg, len); + sha256_context_endstream(&c, len); + sha256_context_digest( &c, d ); +} diff --git a/sha1/sha256_hash.c b/sha1/sha256_hash.c new file mode 100644 index 0000000..a6f7bc1 --- /dev/null +++ b/sha1/sha256_hash.c @@ -0,0 +1,45 @@ +/* +** Copyright 2005 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#include "sha1.h" +#include <string.h> + + +static const char base64tab[]= +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +const char *sha256_hash(const char *passw) +{ +SHA256_DIGEST sha256buf; +static char hash_buffer[1+(sizeof(sha256buf)+2)/3*4]; +int a=0,b=0,c=0; +int i, j; +int d, e, f, g; + + sha256_digest(passw, strlen(passw), sha256buf); + + j=0; + + for (i=0; i<sizeof(sha256buf); i += 3) + { + a=sha256buf[i]; + b= i+1 < sizeof(sha256buf) ? sha256buf[i+1]:0; + c= i+2 < sizeof(sha256buf) ? sha256buf[i+2]:0; + + d=base64tab[ a >> 2 ]; + e=base64tab[ ((a & 3 ) << 4) | (b >> 4)]; + f=base64tab[ ((b & 15) << 2) | (c >> 6)]; + g=base64tab[ c & 63 ]; + if (i + 1 >= sizeof(sha256buf)) f='='; + if (i + 2 >= sizeof(sha256buf)) g='='; + hash_buffer[j++]=d; + hash_buffer[j++]=e; + hash_buffer[j++]=f; + hash_buffer[j++]=g; + } + + hash_buffer[j]=0; + return (hash_buffer); +} diff --git a/sha1/sha512.c b/sha1/sha512.c new file mode 100644 index 0000000..73a2231 --- /dev/null +++ b/sha1/sha512.c @@ -0,0 +1,232 @@ +/* +** Copyright 2008 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#define SHA1_INTERNAL +#include "sha1.h" + +#include <string.h> +#include <stdlib.h> + + +#define ROTR(x,n) ((SHA512_WORD)(((SHA512_WORD)(x) >> (n))|((x) << (64-(n))))) + +#define ROTL(x,n) ((SHA512_WORD)(((SHA512_WORD)(x) << (n))|((x) >> (64-(n))))) + + +#define CH(x,y,z) ((SHA512_WORD)(((x) & (y)) ^ ((~(x))&(z)))) +#define MAJ(x,y,z) ((SHA512_WORD)(((x)&(y))^((x)&(z))^((y)&(z)))) + +#define SUM0(x) ((SHA512_WORD)(ROTR((x),28)^ROTR((x),34)^ROTR((x),39))) +#define SUM1(x) ((SHA512_WORD)(ROTR((x),14)^ROTR((x),18)^ROTR((x),41))) + +#define TH0(x) ((SHA512_WORD)(ROTR((x),1)^ROTR((x),8)^((SHA512_WORD)(x)>>7))) +#define TH1(x) ((SHA512_WORD)(ROTR((x),19)^ROTR((x),61)^((SHA512_WORD)(x)>>6))) + +static const SHA512_WORD K[80]={ + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; + +void sha512_context_init(struct SHA512_CONTEXT *c) +{ + if (sizeof(SHA512_WORD) != 8) + abort(); + + c->H[0] = 0x6a09e667f3bcc908ULL; + c->H[1] = 0xbb67ae8584caa73bULL; + c->H[2] = 0x3c6ef372fe94f82bULL; + c->H[3] = 0xa54ff53a5f1d36f1ULL; + c->H[4] = 0x510e527fade682d1ULL; + c->H[5] = 0x9b05688c2b3e6c1fULL; + c->H[6] = 0x1f83d9abfb41bd6bULL; + c->H[7] = 0x5be0cd19137e2179ULL; + + c->blk_ptr=0; +} + +void sha512_context_hash(struct SHA512_CONTEXT *cc, + const unsigned char blk[SHA512_BLOCK_SIZE]) +{ + SHA512_WORD W[80]; + unsigned i, t; + SHA512_WORD a,b,c,d,e,f,g,h; + + for (i=t=0; t<16; t++) + { + SHA512_WORD x=blk[i]; i++; + + x=(x << 8) | blk[i]; i++; + x=(x << 8) | blk[i]; i++; + x=(x << 8) | blk[i]; i++; + x=(x << 8) | blk[i]; i++; + x=(x << 8) | blk[i]; i++; + x=(x << 8) | blk[i]; i++; + W[t]=(x << 8) | blk[i]; i++; + } + + for (t=16; t<80; t++) + W[t]= TH1(W[t-2]) + W[t-7] + TH0(W[t-15]) + W[t-16]; + + a=cc->H[0]; + b=cc->H[1]; + c=cc->H[2]; + d=cc->H[3]; + e=cc->H[4]; + f=cc->H[5]; + g=cc->H[6]; + h=cc->H[7]; + + for (t=0; t<80; t++) + { + SHA512_WORD T1=h + SUM1(e) + CH(e,f,g) + K[t] + W[t]; + SHA512_WORD T2=SUM0(a)+MAJ(a,b,c); + h=g; + g=f; + f=e; + e=d+T1; + d=c; + c=b; + b=a; + a=T1+T2; + } + + cc->H[0] += a; + cc->H[1] += b; + cc->H[2] += c; + cc->H[3] += d; + cc->H[4] += e; + cc->H[5] += f; + cc->H[6] += g; + cc->H[7] += h; +} + +void sha512_context_hashstream(struct SHA512_CONTEXT *c, const void *p, unsigned l) +{ + const unsigned char *cp=(const unsigned char *)p; + unsigned ll; + + while (l) + { + if (c->blk_ptr == 0 && l >= SHA512_BLOCK_SIZE) + { + sha512_context_hash(c, cp); + cp += SHA512_BLOCK_SIZE; + l -= SHA512_BLOCK_SIZE; + continue; + } + + ll=l; + if (ll > SHA512_BLOCK_SIZE - c->blk_ptr) + ll=SHA512_BLOCK_SIZE - c->blk_ptr; + memcpy(c->blk + c->blk_ptr, cp, ll); + c->blk_ptr += ll; + cp += ll; + l -= ll; + if (c->blk_ptr >= SHA512_BLOCK_SIZE) + { + sha512_context_hash(c, c->blk); + c->blk_ptr=0; + } + } +} + +void sha512_context_endstream(struct SHA512_CONTEXT *c, SHA512_WORD l) +{ + unsigned char buf[16]; + size_t i; + static const unsigned char zero[SHA512_BLOCK_SIZE-8]; + + buf[0]=0x80; + sha512_context_hashstream(c, &buf, 1); + while (c->blk_ptr != SHA512_BLOCK_SIZE-16) + { + if (c->blk_ptr > SHA512_BLOCK_SIZE-16) + { + sha512_context_hashstream(c, zero, + SHA512_BLOCK_SIZE - c->blk_ptr); + continue; + } + sha512_context_hashstream(c, zero, + SHA512_BLOCK_SIZE-16-c->blk_ptr); + } + + l *= 8; + + for (i=0; i<16; i++) + { + buf[15-i]=l; + l >>= 8; + } + + sha512_context_hashstream(c, buf, sizeof(buf)); +} + +void sha512_context_digest(struct SHA512_CONTEXT *c, SHA512_DIGEST d) +{ + unsigned char *dp=d + SHA512_DIGEST_SIZE; + unsigned i; + + for ( i=8; i; ) + { + SHA512_WORD w=c->H[--i]; + + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; w >>= 8; + *--dp=w; + } +} + +void sha512_context_restore(struct SHA512_CONTEXT *c, const SHA512_DIGEST d) +{ + const unsigned char *dp=d; + unsigned i; + + for (i=0; i<8; i++) + { + SHA512_WORD w= *dp++; + + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + w=(w << 8) | *dp++; + c->H[i]=w; + } + c->blk_ptr=0; +} + +void sha512_digest(const void *msg, unsigned len, SHA512_DIGEST d) +{ + struct SHA512_CONTEXT c; + + sha512_context_init( &c ); + sha512_context_hashstream(&c, msg, len); + sha512_context_endstream(&c, len); + sha512_context_digest( &c, d ); +} diff --git a/sha1/sha512_hash.c b/sha1/sha512_hash.c new file mode 100644 index 0000000..bc0d0a2 --- /dev/null +++ b/sha1/sha512_hash.c @@ -0,0 +1,45 @@ +/* +** Copyright 2008 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#include "sha1.h" +#include <string.h> + + +static const char base64tab[]= +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +const char *sha512_hash(const char *passw) +{ +SHA512_DIGEST sha512buf; +static char hash_buffer[1+(sizeof(sha512buf)+2)/3*4]; +int a=0,b=0,c=0; +int i, j; +int d, e, f, g; + + sha512_digest(passw, strlen(passw), sha512buf); + + j=0; + + for (i=0; i<sizeof(sha512buf); i += 3) + { + a=sha512buf[i]; + b= i+1 < sizeof(sha512buf) ? sha512buf[i+1]:0; + c= i+2 < sizeof(sha512buf) ? sha512buf[i+2]:0; + + d=base64tab[ a >> 2 ]; + e=base64tab[ ((a & 3 ) << 4) | (b >> 4)]; + f=base64tab[ ((b & 15) << 2) | (c >> 6)]; + g=base64tab[ c & 63 ]; + if (i + 1 >= sizeof(sha512buf)) f='='; + if (i + 2 >= sizeof(sha512buf)) g='='; + hash_buffer[j++]=d; + hash_buffer[j++]=e; + hash_buffer[j++]=f; + hash_buffer[j++]=g; + } + + hash_buffer[j]=0; + return (hash_buffer); +} diff --git a/sha1/testsuite.c b/sha1/testsuite.c new file mode 100644 index 0000000..4de568e --- /dev/null +++ b/sha1/testsuite.c @@ -0,0 +1,96 @@ +/* +** Copyright 2001-2005 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#include "config.h" +#include "sha1.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +static char foo[1000001]; + +static void sha1() +{ +SHA1_DIGEST digest; +unsigned i, n; + +static char *testcases[]={"abc", +"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", foo}; + + for (n=0; n<sizeof(testcases)/sizeof(testcases[0]); n++) + { + i=strlen(testcases[n]); + sha1_digest(testcases[n], i, digest); + printf( (i < 200 ? "SHA1(%s)=": + "SHA1(%-1.20s...)="), testcases[n]); + + for (i=0; i<20; i++) + { + if (i && (i & 3) == 0) putchar(' '); + printf("%02X", digest[i]); + } + printf("\n"); + } +} + +static void sha256() +{ + SHA256_DIGEST digest; + unsigned i, n; + + static char *testcases[]={"abc", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + foo}; + + for (n=0; n<sizeof(testcases)/sizeof(testcases[0]); n++) + { + i=strlen(testcases[n]); + sha256_digest(testcases[n], i, digest); + printf( (i < 200 ? "SHA256(%s)=": + "SHA256(%-1.20s...)="), testcases[n]); + + for (i=0; i<sizeof(digest); i++) + { + if (i && (i & 3) == 0) putchar(' '); + printf("%02X", digest[i]); + } + printf("\n"); + } +} + +static void sha512() +{ + SHA512_DIGEST digest; + unsigned i, n; + + static char *testcases[]={"abc", + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + foo}; + + for (n=0; n<sizeof(testcases)/sizeof(testcases[0]); n++) + { + i=strlen(testcases[n]); + sha512_digest(testcases[n], i, digest); + printf( (i < 200 ? "SHA512(%s)=": + "SHA512(%-1.20s...)="), testcases[n]); + + for (i=0; i<sizeof(digest); i++) + { + if (i && (i & 7) == 0) putchar(' '); + printf("%02X", digest[i]); + } + printf("\n"); + } +} +int main() +{ + memset(foo, 'a', 1000000); + sha1(); + sha256(); + sha512(); + exit (0); + return (0); +} diff --git a/sha1/testsuite.txt b/sha1/testsuite.txt new file mode 100644 index 0000000..e6a4799 --- /dev/null +++ b/sha1/testsuite.txt @@ -0,0 +1,9 @@ +SHA1(abc)=A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D +SHA1(abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq)=84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 +SHA1(aaaaaaaaaaaaaaaaaaaa...)=34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F +SHA256(abc)=BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD +SHA256(abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq)=248D6A61 D20638B8 E5C02693 0C3E6039 A33CE459 64FF2167 F6ECEDD4 19DB06C1 +SHA256(aaaaaaaaaaaaaaaaaaaa...)=CDC76E5C 9914FB92 81A1C7E2 84D73E67 F1809A48 A497200E 046D39CC C7112CD0 +SHA512(abc)=DDAF35A193617ABA CC417349AE204131 12E6FA4E89A97EA2 0A9EEEE64B55D39A 2192992A274FC1A8 36BA3C23A3FEEBBD 454D4423643CE80E 2A9AC94FA54CA49F +SHA512(abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu)=8E959B75DAE313DA 8CF4F72814FC143F 8F7779C6EB9F7FA1 7299AEADB6889018 501D289E4900F7E4 331B99DEC4B5433A C7D329EEB6DD2654 5E96E55B874BE909 +SHA512(aaaaaaaaaaaaaaaaaaaa...)=E718483D0CE76964 4E2E42C7BC15B463 8E1F98B13B204428 5632A803AFA973EB DE0FF244877EA60A 4CB0432CE577C31B EB009C5C2C49AA2E 4EADB217AD8CC09B |
