X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblutil%2Fsha1.c;h=9d97ceb169c165118afe8b363458c9095079cb1f;hb=583cbc8c25073b23ba2dec6254d9ecef21907a86;hp=a6153f1f8a2e5c90b66483dff9b9c8110c6bc524;hpb=92b28cf3973c0d8c112d08cec8308b66464e9aab;p=openldap diff --git a/libraries/liblutil/sha1.c b/libraries/liblutil/sha1.c index a6153f1f8a..9d97ceb169 100644 --- a/libraries/liblutil/sha1.c +++ b/libraries/liblutil/sha1.c @@ -1,3 +1,8 @@ +/* $OpenLDAP$ */ +/* + * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ /* Acquired from: * $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */ @@ -15,6 +20,9 @@ * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F */ +/* + * This code assumes uint32 is 32 bits and char is 8 bits + */ #include "portable.h" #include @@ -22,12 +30,11 @@ /* include socket.h to get sys/types.h and/or winsock2.h */ #include -#if defined(HAVE_SYS_PARAM_H) -#include -#endif +#include #include "lutil_sha1.h" +/* undefining this will cause pointer alignment errors */ #define SHA1HANDSOFF /* Copies data before messing with it. */ #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) @@ -36,13 +43,13 @@ * I got the idea of expanding during the round function from SSLeay */ #if BYTE_ORDER == LITTLE_ENDIAN -# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ - |(rol(block->l[i],8)&0x00FF00FF)) +# define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \ + |(rol(block[i],8)&0x00FF00FF)) #else -# define blk0(i) block->l[i] +# define blk0(i) block[i] #endif -#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ - ^block->l[(i+2)&15]^block->l[i&15],1)) +#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ + ^block[(i+2)&15]^block[i&15],1)) /* * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 @@ -57,23 +64,16 @@ /* * Hash a single 512-bit block. This is the core of the algorithm. */ -void ldap_SHA1Transform(state, buffer) - uint32 state[5]; - const unsigned char buffer[64]; +void +lutil_SHA1Transform( uint32 *state, const unsigned char *buffer ) { uint32 a, b, c, d, e; - typedef union { - unsigned char c[64]; - u_int l[16]; - } CHAR64LONG16; - CHAR64LONG16 *block; #ifdef SHA1HANDSOFF - static unsigned char workspace[64]; - block = (CHAR64LONG16 *)workspace; - (void)memcpy(block, buffer, 64); + uint32 block[16]; + (void)AC_MEMCPY(block, buffer, 64); #else - block = (CHAR64LONG16 *)buffer; + uint32 *block = (u_int32 *) buffer; #endif /* Copy context->state[] to working vars */ @@ -118,10 +118,10 @@ void ldap_SHA1Transform(state, buffer) /* - * ldap_SHA1Init - Initialize new context + * lutil_SHA1Init - Initialize new context */ -void ldap_SHA1Init(context) - ldap_SHA1_CTX *context; +void +lutil_SHA1Init( lutil_SHA1_CTX *context ) { /* SHA1 initialization constants */ @@ -137,10 +137,12 @@ void ldap_SHA1Init(context) /* * Run your data through this. */ -void ldap_SHA1Update(context, data, len) - ldap_SHA1_CTX *context; - const unsigned char *data; - u_int len; +void +lutil_SHA1Update( + lutil_SHA1_CTX *context, + const unsigned char *data, + uint32 len +) { u_int i, j; @@ -149,24 +151,23 @@ void ldap_SHA1Update(context, data, len) context->count[1] += (len>>29)+1; j = (j >> 3) & 63; if ((j + len) > 63) { - (void)memcpy(&context->buffer[j], data, (i = 64-j)); - ldap_SHA1Transform(context->state, context->buffer); + (void)AC_MEMCPY(&context->buffer[j], data, (i = 64-j)); + lutil_SHA1Transform(context->state, context->buffer); for ( ; i + 63 < len; i += 64) - ldap_SHA1Transform(context->state, &data[i]); + lutil_SHA1Transform(context->state, &data[i]); j = 0; } else { i = 0; } - (void)memcpy(&context->buffer[j], &data[i], len - i); + (void)AC_MEMCPY(&context->buffer[j], &data[i], len - i); } /* * Add padding and return the message digest. */ -void ldap_SHA1Final(digest, context) - unsigned char digest[20]; - ldap_SHA1_CTX* context; +void +lutil_SHA1Final( unsigned char *digest, lutil_SHA1_CTX *context ) { u_int i; unsigned char finalcount[8]; @@ -175,10 +176,10 @@ void ldap_SHA1Final(digest, context) finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ } - ldap_SHA1Update(context, (unsigned char *)"\200", 1); + lutil_SHA1Update(context, (unsigned char *)"\200", 1); while ((context->count[0] & 504) != 448) - ldap_SHA1Update(context, (unsigned char *)"\0", 1); - ldap_SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ + lutil_SHA1Update(context, (unsigned char *)"\0", 1); + lutil_SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ if (digest) { for (i = 0; i < 20; i++) @@ -202,7 +203,7 @@ static char rcsid[] = "$OpenBSD: sha1hl.c,v 1.1 1997/07/12 20:06:03 millert Exp #endif /* LIBC_SCCS and not lint */ #include -#include +#include #include #include @@ -225,9 +226,7 @@ static char rcsid[] = "$OpenBSD: sha1hl.c,v 1.1 1997/07/12 20:06:03 millert Exp /* ARGSUSED */ char * -ldap_SHA1End(ctx, buf) - ldap_SHA1_CTX *ctx; - char *buf; +lutil_SHA1End( lutil_SHA1_CTX *ctx, char *buf ) { int i; char *p = buf; @@ -237,7 +236,7 @@ ldap_SHA1End(ctx, buf) if (p == NULL && (p = malloc(41)) == NULL) return 0; - ldap_SHA1Final(digest,ctx); + lutil_SHA1Final(digest,ctx); for (i = 0; i < 20; i++) { p[i + i] = hex[digest[i] >> 4]; p[i + i + 1] = hex[digest[i] & 0x0f]; @@ -247,37 +246,32 @@ ldap_SHA1End(ctx, buf) } char * -ldap_SHA1File (filename, buf) - char *filename; - char *buf; +lutil_SHA1File( char *filename, char *buf ) { unsigned char buffer[BUFSIZ]; - ldap_SHA1_CTX ctx; + lutil_SHA1_CTX ctx; int fd, num, oerrno; - ldap_SHA1Init(&ctx); + lutil_SHA1Init(&ctx); if ((fd = open(filename,O_RDONLY)) < 0) return(0); while ((num = read(fd, buffer, sizeof(buffer))) > 0) - ldap_SHA1Update(&ctx, buffer, num); + lutil_SHA1Update(&ctx, buffer, num); oerrno = errno; close(fd); errno = oerrno; - return(num < 0 ? 0 : ldap_SHA1End(&ctx, buf)); + return(num < 0 ? 0 : lutil_SHA1End(&ctx, buf)); } char * -ldap_SHA1Data (data, len, buf) - const unsigned char *data; - size_t len; - char *buf; +lutil_SHA1Data( const unsigned char *data, size_t len, char *buf ) { - ldap_SHA1_CTX ctx; + lutil_SHA1_CTX ctx; - ldap_SHA1Init(&ctx); - ldap_SHA1Update(&ctx, data, len); - return(ldap_SHA1End(&ctx, buf)); + lutil_SHA1Init(&ctx); + lutil_SHA1Update(&ctx, data, len); + return(lutil_SHA1End(&ctx, buf)); }