/* sha1.c - SHA1 hash function
- * Copyright (C) 1998 Free Software Foundation, Inc.
+ * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
*
* Please see below for more legal information!
*
#include <stdlib.h>
#include <string.h>
#include <assert.h>
-#include "g10lib.h"
+#include "util.h"
#include "memory.h"
-#include "dynload.h"
+#include "algorithms.h"
#include "bithelp.h"
int count;
} SHA1_CONTEXT;
-
+static void
+burn_stack (int bytes)
+{
+ char buf[128];
+
+ wipememory(buf,sizeof buf);
+ bytes -= sizeof buf;
+ if (bytes > 0)
+ burn_stack (bytes);
+}
void
d = hd->h3;
e = hd->h4;
- #ifdef BIG_ENDIAN_HOST
+#ifdef BIG_ENDIAN_HOST
memcpy( x, data, 64 );
- #else
+#else
{ int i;
byte *p2;
for(i=0, p2=(byte*)x; i < 16; i++, p2 += 4 ) {
p2[0] = *data++;
}
}
- #endif
+#endif
#define K1 0x5A827999L
#define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
- , (x[i&0x0f] = (tm << 1) | (tm >> 31)) )
+ , (x[i&0x0f] = rol(tm,1)) )
#define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
+ f( b, c, d ) \
{
if( hd->count == 64 ) { /* flush the buffer */
transform( hd, hd->buf );
+ burn_stack (88+4*sizeof(void*));
hd->count = 0;
hd->nblocks++;
}
inlen -= 64;
inbuf += 64;
}
+ burn_stack (88+4*sizeof(void*));
for( ; inlen && hd->count < 64; inlen-- )
hd->buf[hd->count++] = *inbuf++;
}
sha1_write(hd, NULL, 0); /* flush */;
- msb = 0;
t = hd->nblocks;
- if( (lsb = t << 6) < t ) /* multiply by 64 to make a byte count */
- msb++;
- msb += t >> 26;
+ /* multiply by 64 to make a byte count */
+ lsb = t << 6;
+ msb = t >> 26;
+ /* add the count */
t = lsb;
- if( (lsb = t + hd->count) < t ) /* add the count */
+ if( (lsb += hd->count) < t )
msb++;
+ /* multiply by 8 to make a bit count */
t = lsb;
- if( (lsb = t << 3) < t ) /* multiply by 8 to make a bit count */
- msb++;
- msb += t >> 29;
+ lsb <<= 3;
+ msb <<= 3;
+ msb |= t >> 29;
if( hd->count < 56 ) { /* enough room */
hd->buf[hd->count++] = 0x80; /* pad */
hd->buf[62] = lsb >> 8;
hd->buf[63] = lsb ;
transform( hd, hd->buf );
+ burn_stack (88+4*sizeof(void*));
p = hd->buf;
- #ifdef BIG_ENDIAN_HOST
- #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
- #else /* little endian */
- #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
+#ifdef BIG_ENDIAN_HOST
+#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
+#else /* little endian */
+#define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
*p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
- #endif
+#endif
X(0);
X(1);
X(2);
X(3);
X(4);
- #undef X
-
+#undef X
}
static byte *
* Returns: A pointer to string describing the algorithm or NULL if
* the ALGO is invalid.
*/
-static const char *
+const char *
sha1_get_info( int algo, size_t *contextsize,
byte **r_asnoid, int *r_asnlen, int *r_mdlen,
void (**r_init)( void *c ),
return "SHA1";
}
-
-
-
-#ifndef IS_MODULE
-static
-#endif
-const char * const gnupgext_version = "SHA1 ($Revision$)";
-
-static struct {
- int class;
- int version;
- int value;
- void (*func)(void);
-} func_table[] = {
- { 10, 1, 0, (void(*)(void))sha1_get_info },
- { 11, 1, 2 },
-};
-
-
-#ifndef IS_MODULE
-static
-#endif
-void *
-gnupgext_enum_func( int what, int *sequence, int *class, int *vers )
-{
- void *ret;
- int i = *sequence;
-
- do {
- if( i >= DIM(func_table) || i < 0 ) {
- return NULL;
- }
- *class = func_table[i].class;
- *vers = func_table[i].version;
- switch( *class ) {
- case 11:
- case 21:
- case 31:
- ret = &func_table[i].value;
- break;
- default:
- ret = func_table[i].func;
- break;
- }
- i++;
- } while( what && what != *class );
-
- *sequence = i;
- return ret;
-}
-
-
-
-
-#ifndef IS_MODULE
-void
-sha1_constructor(void)
-{
- register_internal_cipher_extension( gnupgext_version, gnupgext_enum_func );
-}
-#endif
-