mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-02 22:46:30 +02:00
started with trust stuff
This commit is contained in:
parent
762d3d7197
commit
ed36092588
54 changed files with 1861 additions and 700 deletions
|
@ -23,6 +23,7 @@ cipher_SOURCES = blowfish.c \
|
|||
dsa.h \
|
||||
dsa.c \
|
||||
md.c \
|
||||
md.h \
|
||||
misc.c \
|
||||
smallprime.c
|
||||
|
||||
|
|
|
@ -60,6 +60,7 @@ cipher_SOURCES = blowfish.c \
|
|||
dsa.h \
|
||||
dsa.c \
|
||||
md.c \
|
||||
md.h \
|
||||
misc.c \
|
||||
smallprime.c
|
||||
|
||||
|
|
213
cipher/md.c
213
cipher/md.c
|
@ -27,146 +27,135 @@
|
|||
#include "errors.h"
|
||||
|
||||
|
||||
int
|
||||
md_okay( int algo )
|
||||
{
|
||||
return check_digest_algo( algo );
|
||||
}
|
||||
|
||||
|
||||
MD_HANDLE *
|
||||
/****************
|
||||
* Open a message digest handle for use with algorithm ALGO.
|
||||
* More algorithms may be added by md_enable(). The initial algorithm
|
||||
* may be 0.
|
||||
*/
|
||||
MD_HANDLE
|
||||
md_open( int algo, int secure )
|
||||
{
|
||||
MD_HANDLE *hd;
|
||||
MD_HANDLE hd;
|
||||
|
||||
hd = m_alloc( sizeof *hd + 19 );
|
||||
hd->algo = algo;
|
||||
hd->datalen = 0;
|
||||
if( algo == DIGEST_ALGO_MD5 )
|
||||
hd->u.md5 = md5_open( secure );
|
||||
else if( algo == DIGEST_ALGO_RMD160 )
|
||||
hd->u.rmd= rmd160_open( secure );
|
||||
else if( algo == DIGEST_ALGO_SHA1 )
|
||||
hd->u.sha1 = sha1_open( secure );
|
||||
else
|
||||
return NULL;
|
||||
|
||||
return hd;
|
||||
}
|
||||
|
||||
|
||||
MD_HANDLE *
|
||||
md_copy( MD_HANDLE *a )
|
||||
{
|
||||
MD_HANDLE *hd;
|
||||
|
||||
hd = m_alloc( sizeof *hd + 19 );
|
||||
hd->algo = a->algo;
|
||||
hd->datalen = 0;
|
||||
if( a->algo == DIGEST_ALGO_MD5 )
|
||||
hd->u.md5 = md5_copy( a->u.md5 );
|
||||
else if( a->algo == DIGEST_ALGO_RMD160 )
|
||||
hd->u.rmd= rmd160_copy( a->u.rmd );
|
||||
else if( a->algo == DIGEST_ALGO_SHA1 )
|
||||
hd->u.sha1= sha1_copy( a->u.sha1 );
|
||||
else
|
||||
log_bug(NULL);
|
||||
return hd;
|
||||
}
|
||||
|
||||
|
||||
/* used for a BAD Kludge in rmd160.c, md5.c */
|
||||
MD_HANDLE *
|
||||
md_makecontainer( int algo )
|
||||
{
|
||||
MD_HANDLE *hd;
|
||||
|
||||
hd = m_alloc( sizeof *hd + 19 );
|
||||
hd->algo = algo;
|
||||
hd->datalen = 0;
|
||||
if( algo == DIGEST_ALGO_MD5 )
|
||||
;
|
||||
else if( algo == DIGEST_ALGO_RMD160 )
|
||||
;
|
||||
else if( algo == DIGEST_ALGO_SHA1 )
|
||||
;
|
||||
else
|
||||
log_bug(NULL);
|
||||
hd = secure ? m_alloc_secure_clear( sizeof *hd )
|
||||
: m_alloc_clear( sizeof *hd );
|
||||
if( algo )
|
||||
md_enable( hd, algo );
|
||||
return hd;
|
||||
}
|
||||
|
||||
void
|
||||
md_close(MD_HANDLE *a)
|
||||
md_enable( MD_HANDLE h, int algo )
|
||||
{
|
||||
if( algo == DIGEST_ALGO_MD5 ) {
|
||||
md5_init( &h->md5 );
|
||||
h->use_md5 = 1;
|
||||
}
|
||||
else if( algo == DIGEST_ALGO_RMD160 ) {
|
||||
rmd160_init( &h->rmd160 );
|
||||
h->use_rmd160 = 1;
|
||||
}
|
||||
else if( algo == DIGEST_ALGO_SHA1 ) {
|
||||
sha1_init( &h->sha1 );
|
||||
h->use_sha1 = 1;
|
||||
}
|
||||
else
|
||||
log_bug("md_enable(%d)", algo );
|
||||
}
|
||||
|
||||
|
||||
MD_HANDLE
|
||||
md_copy( MD_HANDLE a )
|
||||
{
|
||||
MD_HANDLE b;
|
||||
|
||||
b = m_is_secure(a)? m_alloc_secure( sizeof *b )
|
||||
: m_alloc( sizeof *b );
|
||||
memcpy( b, a, sizeof *a );
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
md_close(MD_HANDLE a)
|
||||
{
|
||||
if( !a )
|
||||
return;
|
||||
if( a->algo == DIGEST_ALGO_MD5 )
|
||||
md5_close( a->u.md5 );
|
||||
else if( a->algo == DIGEST_ALGO_RMD160 )
|
||||
rmd160_close( a->u.rmd );
|
||||
else if( a->algo == DIGEST_ALGO_SHA1 )
|
||||
sha1_close( a->u.sha1 );
|
||||
else
|
||||
log_bug(NULL);
|
||||
m_free(a);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
md_write( MD_HANDLE *a, byte *inbuf, size_t inlen)
|
||||
md_write( MD_HANDLE a, byte *inbuf, size_t inlen)
|
||||
{
|
||||
if( a->algo == DIGEST_ALGO_MD5 )
|
||||
md5_write( a->u.md5, inbuf, inlen );
|
||||
else if( a->algo == DIGEST_ALGO_RMD160 )
|
||||
rmd160_write( a->u.rmd, inbuf, inlen );
|
||||
else if( a->algo == DIGEST_ALGO_SHA1 )
|
||||
sha1_write( a->u.sha1, inbuf, inlen );
|
||||
else
|
||||
log_bug(NULL);
|
||||
if( a->use_rmd160 ) {
|
||||
rmd160_write( &a->rmd160, a->buffer, a->bufcount );
|
||||
rmd160_write( &a->rmd160, inbuf, inlen );
|
||||
}
|
||||
if( a->use_sha1 ) {
|
||||
sha1_write( &a->sha1, a->buffer, a->bufcount );
|
||||
sha1_write( &a->sha1, inbuf, inlen );
|
||||
}
|
||||
if( a->use_md5 ) {
|
||||
md5_write( &a->md5, a->buffer, a->bufcount );
|
||||
md5_write( &a->md5, inbuf, inlen );
|
||||
}
|
||||
a->bufcount = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
md_putchar( MD_HANDLE *a, int c )
|
||||
md_final(MD_HANDLE a)
|
||||
{
|
||||
if( a->algo == DIGEST_ALGO_MD5 )
|
||||
md5_putchar( a->u.md5, c );
|
||||
else if( a->algo == DIGEST_ALGO_RMD160 )
|
||||
rmd160_putchar( a->u.rmd, c );
|
||||
else if( a->algo == DIGEST_ALGO_SHA1 )
|
||||
sha1_putchar( a->u.sha1, c );
|
||||
else
|
||||
log_bug(NULL);
|
||||
if( a->bufcount )
|
||||
md_write( a, NULL, 0 );
|
||||
if( a->use_rmd160 ) {
|
||||
byte *p;
|
||||
rmd160_final( &a->rmd160 );
|
||||
p = rmd160_read( &a->rmd160 );
|
||||
}
|
||||
if( a->use_sha1 )
|
||||
sha1_final( &a->sha1 );
|
||||
if( a->use_md5 )
|
||||
md5_final( &a->md5 );
|
||||
}
|
||||
|
||||
|
||||
/****************
|
||||
* if ALGO is null get the digest for the used algo (which should be only one)
|
||||
*/
|
||||
byte *
|
||||
md_final(MD_HANDLE *a)
|
||||
md_read( MD_HANDLE a, int algo )
|
||||
{
|
||||
if( a->algo == DIGEST_ALGO_MD5 ) {
|
||||
if( !a->datalen ) {
|
||||
md5_final( a->u.md5 );
|
||||
memcpy(a->data, md5_read( a->u.md5 ), 16);
|
||||
a->datalen = 16;
|
||||
}
|
||||
return a->data;
|
||||
if( !algo ) {
|
||||
if( a->use_rmd160 )
|
||||
return rmd160_read( &a->rmd160 );
|
||||
if( a->use_sha1 )
|
||||
return sha1_read( &a->sha1 );
|
||||
if( a->use_md5 )
|
||||
return md5_read( &a->md5 );
|
||||
}
|
||||
else if( a->algo == DIGEST_ALGO_RMD160 ) {
|
||||
if( !a->datalen ) {
|
||||
memcpy(a->data, rmd160_final( a->u.rmd ), 20 );
|
||||
a->datalen = 20;
|
||||
}
|
||||
return a->data;
|
||||
else {
|
||||
if( algo == DIGEST_ALGO_RMD160 )
|
||||
return rmd160_read( &a->rmd160 );
|
||||
if( algo == DIGEST_ALGO_SHA1 )
|
||||
return sha1_read( &a->sha1 );
|
||||
if( algo == DIGEST_ALGO_MD5 )
|
||||
return md5_read( &a->md5 );
|
||||
}
|
||||
else if( a->algo == DIGEST_ALGO_SHA1 ) {
|
||||
if( !a->datalen ) {
|
||||
memcpy(a->data, sha1_final( a->u.sha1 ), 20 );
|
||||
a->datalen = 20;
|
||||
}
|
||||
return a->data;
|
||||
}
|
||||
else
|
||||
log_bug(NULL);
|
||||
log_bug(NULL);
|
||||
}
|
||||
|
||||
int
|
||||
md_get_algo( MD_HANDLE a )
|
||||
{
|
||||
if( a->use_rmd160 )
|
||||
return DIGEST_ALGO_RMD160;
|
||||
if( a->use_sha1 )
|
||||
return DIGEST_ALGO_SHA1;
|
||||
if( a->use_md5 )
|
||||
return DIGEST_ALGO_MD5;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
60
cipher/md.h
Normal file
60
cipher/md.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/* md.h - digest functions
|
||||
* Copyright (c) 1997 by Werner Koch (dd9jn)
|
||||
*
|
||||
* This file is part of G10.
|
||||
*
|
||||
* G10 is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* G10 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
||||
*/
|
||||
#ifndef G10_MD_H
|
||||
#define G10_MD_H
|
||||
|
||||
#include "types.h"
|
||||
#include "rmd.h"
|
||||
#include "sha1.h"
|
||||
#include "md5.h"
|
||||
|
||||
#define MD_BUFFER_SIZE 512
|
||||
|
||||
typedef struct {
|
||||
int use_rmd160;
|
||||
RMD160_CONTEXT rmd160;
|
||||
int use_sha1;
|
||||
SHA1_CONTEXT sha1;
|
||||
int use_md5;
|
||||
MD5_CONTEXT md5;
|
||||
byte buffer[MD_BUFFER_SIZE]; /* primary buffer */
|
||||
int bufcount;
|
||||
} *MD_HANDLE;
|
||||
|
||||
|
||||
#define md_putc(h,c) \
|
||||
do { \
|
||||
if( (h)->bufcount == MD_BUFFER_SIZE ) \
|
||||
md_write( (h), NULL, 0 ); \
|
||||
(h)->buffer[(h)->bufcount++] = (c) & 0xff; \
|
||||
} while(0)
|
||||
|
||||
/*-- md.c --*/
|
||||
MD_HANDLE md_open( int algo, int secure );
|
||||
void md_enable( MD_HANDLE hd, int algo );
|
||||
MD_HANDLE md_copy( MD_HANDLE a );
|
||||
void md_close(MD_HANDLE a);
|
||||
void md_write( MD_HANDLE a, byte *inbuf, size_t inlen);
|
||||
void md_final(MD_HANDLE a);
|
||||
byte *md_read( MD_HANDLE a, int algo );
|
||||
int md_get_algo( MD_HANDLE a );
|
||||
|
||||
|
||||
#endif /*G10_MD_H*/
|
123
cipher/md5.c
123
cipher/md5.c
|
@ -61,7 +61,6 @@
|
|||
#include <assert.h>
|
||||
#include "util.h"
|
||||
#include "md5.h"
|
||||
#include "cipher.h" /* kludge for md5_copy2md() */
|
||||
#include "memory.h"
|
||||
|
||||
|
||||
|
@ -74,7 +73,7 @@
|
|||
#endif
|
||||
|
||||
|
||||
static void Init( MD5HANDLE mdContext);
|
||||
static void Init( MD5_CONTEXT *mdContext);
|
||||
static void Transform(u32 *buf,u32 *in);
|
||||
|
||||
static byte PADDING[64] = {
|
||||
|
@ -120,56 +119,9 @@ static byte PADDING[64] = {
|
|||
(a) += (b); \
|
||||
}
|
||||
|
||||
/* The routine Init initializes the message-digest context
|
||||
* mdContext. All fields are set to zero.
|
||||
* mode should be zero is reserved for extensions.
|
||||
*/
|
||||
|
||||
MD5HANDLE
|
||||
md5_open(int secure)
|
||||
{
|
||||
MD5HANDLE mdContext;
|
||||
|
||||
mdContext = secure? m_alloc_secure( sizeof *mdContext )
|
||||
: m_alloc( sizeof *mdContext );
|
||||
Init(mdContext);
|
||||
return mdContext;
|
||||
}
|
||||
|
||||
|
||||
MD5HANDLE
|
||||
md5_copy( MD5HANDLE a )
|
||||
{
|
||||
MD5HANDLE mdContext;
|
||||
|
||||
assert(a);
|
||||
mdContext = m_is_secure(a)? m_alloc_secure( sizeof *mdContext )
|
||||
: m_alloc( sizeof *mdContext );
|
||||
memcpy( mdContext, a, sizeof *a );
|
||||
return mdContext;
|
||||
}
|
||||
|
||||
|
||||
/* BAD Kludge!!! */
|
||||
MD_HANDLE *
|
||||
md5_copy2md( MD5HANDLE a )
|
||||
{
|
||||
MD_HANDLE *md = md_makecontainer( DIGEST_ALGO_MD5 );
|
||||
md->u.md5 = md5_copy( a );
|
||||
return md;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
md5_close(MD5HANDLE hd)
|
||||
{
|
||||
if( hd )
|
||||
m_free(hd);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
Init( MD5HANDLE mdContext)
|
||||
md5_init( MD5_CONTEXT *mdContext)
|
||||
{
|
||||
mdContext->i[0] = mdContext->i[1] = (u32)0;
|
||||
/* Load magic initialization constants.
|
||||
|
@ -178,7 +130,7 @@ Init( MD5HANDLE mdContext)
|
|||
mdContext->buf[1] = (u32)0xefcdab89L;
|
||||
mdContext->buf[2] = (u32)0x98badcfeL;
|
||||
mdContext->buf[3] = (u32)0x10325476L;
|
||||
mdContext->bufcount = 0;
|
||||
mdContext->count = 0;
|
||||
}
|
||||
|
||||
/* The routine Update updates the message-digest context to
|
||||
|
@ -186,15 +138,15 @@ Init( MD5HANDLE mdContext)
|
|||
* in the message whose digest is being computed.
|
||||
*/
|
||||
void
|
||||
md5_write( MD5HANDLE mdContext, byte *inBuf, size_t inLen)
|
||||
md5_write( MD5_CONTEXT *mdContext, byte *inBuf, size_t inLen)
|
||||
{
|
||||
register int i, ii;
|
||||
int mdi;
|
||||
u32 in[16];
|
||||
|
||||
if(mdContext->bufcount) { /* flush the buffer */
|
||||
i = mdContext->bufcount;
|
||||
mdContext->bufcount = 0;
|
||||
if(mdContext->count) { /* flush the buffer */
|
||||
i = mdContext->count;
|
||||
mdContext->count = 0;
|
||||
md5_write( mdContext, mdContext->digest, i);
|
||||
}
|
||||
if( !inBuf )
|
||||
|
@ -227,20 +179,6 @@ md5_write( MD5HANDLE mdContext, byte *inBuf, size_t inLen)
|
|||
}
|
||||
|
||||
|
||||
/****************
|
||||
* Process a single character, this character will be buffered to
|
||||
* increase performance. The digest-field is used as a buffer.
|
||||
*/
|
||||
|
||||
void
|
||||
md5_putchar( MD5HANDLE mdContext, int c )
|
||||
{
|
||||
if(mdContext->bufcount == 16)
|
||||
md5_write( mdContext, NULL, 0 );
|
||||
mdContext->digest[mdContext->bufcount++] = c & 0xff;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* The routine final terminates the message-digest computation and
|
||||
* ends with the desired message digest in mdContext->digest[0...15].
|
||||
|
@ -249,14 +187,14 @@ md5_putchar( MD5HANDLE mdContext, int c )
|
|||
*/
|
||||
|
||||
void
|
||||
md5_final(MD5HANDLE mdContext)
|
||||
md5_final( MD5_CONTEXT *mdContext )
|
||||
{
|
||||
u32 in[16];
|
||||
int mdi;
|
||||
unsigned int i, ii;
|
||||
unsigned int padLen;
|
||||
|
||||
if(mdContext->bufcount) /* flush buffer */
|
||||
if(mdContext->count) /* flush buffer */
|
||||
md5_write(mdContext, NULL, 0 );
|
||||
/* save number of bits */
|
||||
in[14] = mdContext->i[0];
|
||||
|
@ -284,49 +222,6 @@ md5_final(MD5HANDLE mdContext)
|
|||
mdContext->digest[ii+2] = (byte)((mdContext->buf[i] >> 16) & 0xFF);
|
||||
mdContext->digest[ii+3] = (byte)((mdContext->buf[i] >> 24) & 0xFF);
|
||||
}
|
||||
Init(mdContext);
|
||||
}
|
||||
|
||||
/**********
|
||||
* Returns 16 bytes representing the digest.
|
||||
*/
|
||||
byte *
|
||||
md5_read(MD5HANDLE mdContext)
|
||||
{
|
||||
return mdContext->digest;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/****************
|
||||
* Converts the result form Read into a printable representation.
|
||||
* This should only be used direct after a md5_read(), because it uses
|
||||
* In-Place conversion.
|
||||
* Returns digest.
|
||||
*/
|
||||
|
||||
char *
|
||||
md5_tostring( byte *digest )
|
||||
{
|
||||
static byte bintoasc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."
|
||||
"abcdefghijklmnopqrstuvwxyz_"
|
||||
"0123456789";
|
||||
int i;
|
||||
byte *d, *s;
|
||||
|
||||
memmove(digest+8,digest, 16); /* make some room */
|
||||
d = digest;
|
||||
s = digest+8;
|
||||
for(i=0; i < 5; i++, s += 3 ) {
|
||||
*d++ = bintoasc[(*s >> 2) & 077];
|
||||
*d++ = bintoasc[(((*s << 4) & 060) | ((s[1] >> 4) & 017)) & 077];
|
||||
*d++ = bintoasc[(((s[1] << 2) & 074) | ((s[2] >> 6) & 03)) & 077];
|
||||
*d++ = bintoasc[s[2] & 077];
|
||||
}
|
||||
*d++ = bintoasc[(*s >> 2) & 077];
|
||||
*d++ = bintoasc[((*s << 4) & 060) & 077];
|
||||
*d = 0;
|
||||
return (char*)digest;
|
||||
}
|
||||
|
||||
|
||||
|
|
17
cipher/md5.h
17
cipher/md5.h
|
@ -25,20 +25,15 @@
|
|||
typedef struct {
|
||||
u32 i[2]; /* number of _bits_ handled mod 2^64 */
|
||||
u32 buf[4]; /* scratch buffer */
|
||||
int count;
|
||||
byte in[64]; /* input buffer */
|
||||
byte digest[16+8+1]; /* actual digest after Final call */
|
||||
byte bufcount; /* extra room for bintoascii */
|
||||
} *MD5HANDLE;
|
||||
} MD5_CONTEXT;
|
||||
|
||||
/*-- md5.c --*/
|
||||
MD5HANDLE md5_open(int);
|
||||
MD5HANDLE md5_copy(MD5HANDLE a);
|
||||
void md5_write(MD5HANDLE hd, byte *inBuf, size_t inLen);
|
||||
void md5_putchar(MD5HANDLE hd, int c );
|
||||
void md5_final(MD5HANDLE hd);
|
||||
byte *md5_read(MD5HANDLE hd);
|
||||
char *md5_tostring( byte *digest );
|
||||
void md5_close(MD5HANDLE hd);
|
||||
|
||||
void md5_init( MD5_CONTEXT *c );
|
||||
void md5_write( MD5_CONTEXT *hd, byte *inbuf, size_t inlen);
|
||||
void md5_final( MD5_CONTEXT *hd);
|
||||
#define md5_read(h) ( (h)->digest )
|
||||
|
||||
#endif /*G10_MD5_H*/
|
||||
|
|
|
@ -82,13 +82,12 @@ open_device( const char *name, int minor )
|
|||
log_fatal("can't open %s: %s\n", name, strerror(errno) );
|
||||
if( fstat( fd, &sb ) )
|
||||
log_fatal("stat() off %s failed: %s\n", name, strerror(errno) );
|
||||
if( !S_ISCHR(sb.st_mode)
|
||||
#ifdef __linux__
|
||||
|| (sb.st_rdev >> 8) != 1
|
||||
|| (sb.st_rdev & 0xff) != minor
|
||||
#endif
|
||||
)
|
||||
#if defined(__sparc__) && defined(__linux__)
|
||||
#warning something is wrong with UltraPenguin /dev/random
|
||||
#else
|
||||
if( !S_ISCHR(sb.st_mode) )
|
||||
log_fatal("invalid random device!\n" );
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
@ -140,6 +139,10 @@ the OS a chance to collect more entropy! (Need %d more bytes)\n", length );
|
|||
assert( length < 200 );
|
||||
do {
|
||||
n = read(fd, buffer, length );
|
||||
if( n > length ) {
|
||||
log_error("bogus read from random device (n=%d)\n", n );
|
||||
n = length;
|
||||
}
|
||||
} while( n == -1 && errno == EINTR );
|
||||
if( n == -1 )
|
||||
log_fatal("read error on random device: %s\n", strerror(errno) );
|
||||
|
|
27
cipher/rmd.h
27
cipher/rmd.h
|
@ -25,27 +25,14 @@
|
|||
typedef struct {
|
||||
u32 h0,h1,h2,h3,h4;
|
||||
u32 nblocks;
|
||||
byte buffer[64];
|
||||
int bufcount;
|
||||
} *RMDHANDLE;
|
||||
byte buf[64];
|
||||
int count;
|
||||
} RMD160_CONTEXT;
|
||||
|
||||
|
||||
/****************
|
||||
* Process a single character, this character will be buffered to
|
||||
* increase performance.
|
||||
*/
|
||||
#define rmd160_putchar(h,c) \
|
||||
do { \
|
||||
if( (h)->bufcount == 64 ) \
|
||||
rmd160_write( (h), NULL, 0 ); \
|
||||
(h)->buffer[(h)->bufcount++] = (c) & 0xff; \
|
||||
} while(0)
|
||||
|
||||
RMDHANDLE rmd160_open( int secure );
|
||||
RMDHANDLE rmd160_copy( RMDHANDLE a );
|
||||
void rmd160_close(RMDHANDLE hd);
|
||||
void rmd160_write( RMDHANDLE hd, byte *inbuf, size_t inlen);
|
||||
byte * rmd160_final(RMDHANDLE hd);
|
||||
|
||||
void rmd160_init( RMD160_CONTEXT *c );
|
||||
void rmd160_write( RMD160_CONTEXT *hd, byte *inbuf, size_t inlen);
|
||||
void rmd160_final(RMD160_CONTEXT *hd);
|
||||
#define rmd160_read(h) ( (h)->buf )
|
||||
|
||||
#endif /*G10_RMD_H*/
|
||||
|
|
127
cipher/rmd160.c
127
cipher/rmd160.c
|
@ -25,7 +25,6 @@
|
|||
#include <assert.h>
|
||||
#include "util.h"
|
||||
#include "memory.h"
|
||||
#include "cipher.h" /* grrrr */
|
||||
#include "rmd.h"
|
||||
|
||||
/*********************************
|
||||
|
@ -139,16 +138,16 @@
|
|||
*/
|
||||
|
||||
|
||||
static void
|
||||
initialize( RMDHANDLE hd )
|
||||
void
|
||||
rmd160_init( RMD160_CONTEXT *hd )
|
||||
{
|
||||
hd->h0 = 0x67452301;
|
||||
hd->h1 = 0xEFCDAB89;
|
||||
hd->h2 = 0x98BADCFE;
|
||||
hd->h3 = 0x10325476;
|
||||
hd->h4 = 0xC3D2E1F0;
|
||||
hd->bufcount = 0;
|
||||
hd->nblocks = 0;
|
||||
hd->count = 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -156,7 +155,7 @@ initialize( RMDHANDLE hd )
|
|||
* Transform the message X which consists of 16 32-bit-words
|
||||
*/
|
||||
static void
|
||||
transform( RMDHANDLE hd, byte *data )
|
||||
transform( RMD160_CONTEXT *hd, byte *data )
|
||||
{
|
||||
static int r[80] = {
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
|
@ -257,68 +256,22 @@ transform( RMDHANDLE hd, byte *data )
|
|||
|
||||
|
||||
|
||||
|
||||
RMDHANDLE
|
||||
rmd160_open( int secure )
|
||||
{
|
||||
RMDHANDLE hd;
|
||||
|
||||
hd = secure? m_alloc_secure( sizeof *hd )
|
||||
: m_alloc( sizeof *hd );
|
||||
initialize(hd);
|
||||
return hd;
|
||||
}
|
||||
|
||||
|
||||
RMDHANDLE
|
||||
rmd160_copy( RMDHANDLE a )
|
||||
{
|
||||
RMDHANDLE b;
|
||||
|
||||
assert(a);
|
||||
b = m_is_secure(a)? m_alloc_secure( sizeof *b )
|
||||
: m_alloc( sizeof *b );
|
||||
memcpy( b, a, sizeof *a );
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
/* BAD Kludge!!! */
|
||||
MD_HANDLE *
|
||||
rmd160_copy2md( RMDHANDLE a )
|
||||
{
|
||||
MD_HANDLE *md = md_makecontainer( DIGEST_ALGO_RMD160 );
|
||||
md->u.rmd = rmd160_copy( a );
|
||||
return md;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
rmd160_close(RMDHANDLE hd)
|
||||
{
|
||||
if( hd )
|
||||
m_free(hd);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Update the message digest with the contents
|
||||
* of INBUF with length INLEN.
|
||||
*/
|
||||
void
|
||||
rmd160_write( RMDHANDLE hd, byte *inbuf, size_t inlen)
|
||||
rmd160_write( RMD160_CONTEXT *hd, byte *inbuf, size_t inlen)
|
||||
{
|
||||
if( hd->bufcount == 64 ) { /* flush the buffer */
|
||||
transform( hd, hd->buffer );
|
||||
hd->bufcount = 0;
|
||||
if( hd->count == 64 ) { /* flush the buffer */
|
||||
transform( hd, hd->buf );
|
||||
hd->count = 0;
|
||||
hd->nblocks++;
|
||||
}
|
||||
if( !inbuf )
|
||||
return;
|
||||
if( hd->bufcount ) {
|
||||
for( ; inlen && hd->bufcount < 64; inlen-- )
|
||||
hd->buffer[hd->bufcount++] = *inbuf++;
|
||||
if( hd->count ) {
|
||||
for( ; inlen && hd->count < 64; inlen-- )
|
||||
hd->buf[hd->count++] = *inbuf++;
|
||||
rmd160_write( hd, NULL, 0 );
|
||||
if( !inlen )
|
||||
return;
|
||||
|
@ -326,25 +279,21 @@ rmd160_write( RMDHANDLE hd, byte *inbuf, size_t inlen)
|
|||
|
||||
while( inlen >= 64 ) {
|
||||
transform( hd, inbuf );
|
||||
hd->bufcount = 0;
|
||||
hd->count = 0;
|
||||
hd->nblocks++;
|
||||
inlen -= 64;
|
||||
inbuf += 64;
|
||||
}
|
||||
for( ; inlen && hd->bufcount < 64; inlen-- )
|
||||
hd->buffer[hd->bufcount++] = *inbuf++;
|
||||
for( ; inlen && hd->count < 64; inlen-- )
|
||||
hd->buf[hd->count++] = *inbuf++;
|
||||
}
|
||||
|
||||
|
||||
/* The routine final terminates the computation and
|
||||
* returns the digest.
|
||||
* The handle is prepared for a new cycle, but adding bytes to the
|
||||
* handle will the destroy the returned buffer.
|
||||
* Returns: 20 bytes representing the digest.
|
||||
/* The routine terminates the computation
|
||||
*/
|
||||
|
||||
byte *
|
||||
rmd160_final(RMDHANDLE hd)
|
||||
void
|
||||
rmd160_final( RMD160_CONTEXT *hd )
|
||||
{
|
||||
u32 t, msb, lsb;
|
||||
byte *p;
|
||||
|
@ -357,37 +306,37 @@ rmd160_final(RMDHANDLE hd)
|
|||
msb++;
|
||||
msb += t >> 26;
|
||||
t = lsb;
|
||||
if( (lsb = t + hd->bufcount) < t ) /* add the bufcount */
|
||||
if( (lsb = t + hd->count) < t ) /* add the count */
|
||||
msb++;
|
||||
t = lsb;
|
||||
if( (lsb = t << 3) < t ) /* multiply by 8 to make a bit count */
|
||||
msb++;
|
||||
msb += t >> 29;
|
||||
|
||||
if( hd->bufcount < 56 ) { /* enough room */
|
||||
hd->buffer[hd->bufcount++] = 0x80; /* pad */
|
||||
while( hd->bufcount < 56 )
|
||||
hd->buffer[hd->bufcount++] = 0; /* pad */
|
||||
if( hd->count < 56 ) { /* enough room */
|
||||
hd->buf[hd->count++] = 0x80; /* pad */
|
||||
while( hd->count < 56 )
|
||||
hd->buf[hd->count++] = 0; /* pad */
|
||||
}
|
||||
else { /* need one extra block */
|
||||
hd->buffer[hd->bufcount++] = 0x80; /* pad character */
|
||||
while( hd->bufcount < 64 )
|
||||
hd->buffer[hd->bufcount++] = 0;
|
||||
hd->buf[hd->count++] = 0x80; /* pad character */
|
||||
while( hd->count < 64 )
|
||||
hd->buf[hd->count++] = 0;
|
||||
rmd160_write(hd, NULL, 0); /* flush */;
|
||||
memset(hd->buffer, 0, 56 ); /* fill next block with zeroes */
|
||||
memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
|
||||
}
|
||||
/* append the 64 bit count */
|
||||
hd->buffer[56] = lsb ;
|
||||
hd->buffer[57] = lsb >> 8;
|
||||
hd->buffer[58] = lsb >> 16;
|
||||
hd->buffer[59] = lsb >> 24;
|
||||
hd->buffer[60] = msb ;
|
||||
hd->buffer[61] = msb >> 8;
|
||||
hd->buffer[62] = msb >> 16;
|
||||
hd->buffer[63] = msb >> 24;
|
||||
transform( hd, hd->buffer );
|
||||
hd->buf[56] = lsb ;
|
||||
hd->buf[57] = lsb >> 8;
|
||||
hd->buf[58] = lsb >> 16;
|
||||
hd->buf[59] = lsb >> 24;
|
||||
hd->buf[60] = msb ;
|
||||
hd->buf[61] = msb >> 8;
|
||||
hd->buf[62] = msb >> 16;
|
||||
hd->buf[63] = msb >> 24;
|
||||
transform( hd, hd->buf );
|
||||
|
||||
p = hd->buffer;
|
||||
p = hd->buf;
|
||||
#ifdef BIG_ENDIAN_HOST
|
||||
#define X(a) do { *p++ = hd->h##a ; *p++ = hd->h##a >> 8; \
|
||||
*p++ = hd->h##a >> 16; *p++ = hd->h##a >> 24; } while(0)
|
||||
|
@ -400,10 +349,6 @@ rmd160_final(RMDHANDLE hd)
|
|||
X(3);
|
||||
X(4);
|
||||
#undef X
|
||||
|
||||
initialize( hd ); /* prepare for next cycle */
|
||||
return hd->buffer; /* now contains the digest */
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
120
cipher/sha1.c
120
cipher/sha1.c
|
@ -84,7 +84,6 @@
|
|||
#include <assert.h>
|
||||
#include "util.h"
|
||||
#include "memory.h"
|
||||
#include "cipher.h" /* grrrr */
|
||||
#include "sha1.h"
|
||||
|
||||
|
||||
|
@ -110,16 +109,16 @@
|
|||
( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) )
|
||||
|
||||
|
||||
static void
|
||||
initialize( SHA1HANDLE hd )
|
||||
void
|
||||
sha1_init( SHA1_CONTEXT *hd )
|
||||
{
|
||||
hd->h0 = 0x67452301;
|
||||
hd->h1 = 0xefcdab89;
|
||||
hd->h2 = 0x98badcfe;
|
||||
hd->h3 = 0x10325476;
|
||||
hd->h4 = 0xc3d2e1f0;
|
||||
hd->bufcount = 0;
|
||||
hd->nblocks = 0;
|
||||
hd->count = 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -127,7 +126,7 @@ initialize( SHA1HANDLE hd )
|
|||
* Transform the message X which consists of 16 32-bit-words
|
||||
*/
|
||||
static void
|
||||
transform( SHA1HANDLE hd, byte *data )
|
||||
transform( SHA1_CONTEXT *hd, byte *data )
|
||||
{
|
||||
u32 A, B, C, D, E; /* Local vars */
|
||||
u32 eData[ 16 ]; /* Expanded data */
|
||||
|
@ -247,69 +246,22 @@ transform( SHA1HANDLE hd, byte *data )
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
SHA1HANDLE
|
||||
sha1_open( int secure )
|
||||
{
|
||||
SHA1HANDLE hd;
|
||||
|
||||
hd = secure? m_alloc_secure( sizeof *hd )
|
||||
: m_alloc( sizeof *hd );
|
||||
initialize(hd);
|
||||
return hd;
|
||||
}
|
||||
|
||||
|
||||
SHA1HANDLE
|
||||
sha1_copy( SHA1HANDLE a )
|
||||
{
|
||||
SHA1HANDLE b;
|
||||
|
||||
assert(a);
|
||||
b = m_is_secure(a)? m_alloc_secure( sizeof *b )
|
||||
: m_alloc( sizeof *b );
|
||||
memcpy( b, a, sizeof *a );
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
/* BAD Kludge!!! */
|
||||
MD_HANDLE *
|
||||
sha1_copy2md( SHA1HANDLE a )
|
||||
{
|
||||
MD_HANDLE *md = md_makecontainer( DIGEST_ALGO_SHA1 );
|
||||
md->u.sha1 = sha1_copy( a );
|
||||
return md;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
sha1_close(SHA1HANDLE hd)
|
||||
{
|
||||
if( hd )
|
||||
m_free(hd);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Update the message digest with the contents
|
||||
* of INBUF with length INLEN.
|
||||
*/
|
||||
void
|
||||
sha1_write( SHA1HANDLE hd, byte *inbuf, size_t inlen)
|
||||
sha1_write( SHA1_CONTEXT *hd, byte *inbuf, size_t inlen)
|
||||
{
|
||||
if( hd->bufcount == 64 ) { /* flush the buffer */
|
||||
transform( hd, hd->buffer );
|
||||
hd->bufcount = 0;
|
||||
if( hd->count == 64 ) { /* flush the buffer */
|
||||
transform( hd, hd->buf );
|
||||
hd->count = 0;
|
||||
hd->nblocks++;
|
||||
}
|
||||
if( !inbuf )
|
||||
return;
|
||||
if( hd->bufcount ) {
|
||||
for( ; inlen && hd->bufcount < 64; inlen-- )
|
||||
hd->buffer[hd->bufcount++] = *inbuf++;
|
||||
if( hd->count ) {
|
||||
for( ; inlen && hd->count < 64; inlen-- )
|
||||
hd->buf[hd->count++] = *inbuf++;
|
||||
sha1_write( hd, NULL, 0 );
|
||||
if( !inlen )
|
||||
return;
|
||||
|
@ -317,13 +269,13 @@ sha1_write( SHA1HANDLE hd, byte *inbuf, size_t inlen)
|
|||
|
||||
while( inlen >= 64 ) {
|
||||
transform( hd, inbuf );
|
||||
hd->bufcount = 0;
|
||||
hd->count = 0;
|
||||
hd->nblocks++;
|
||||
inlen -= 64;
|
||||
inbuf += 64;
|
||||
}
|
||||
for( ; inlen && hd->bufcount < 64; inlen-- )
|
||||
hd->buffer[hd->bufcount++] = *inbuf++;
|
||||
for( ; inlen && hd->count < 64; inlen-- )
|
||||
hd->buf[hd->count++] = *inbuf++;
|
||||
}
|
||||
|
||||
|
||||
|
@ -334,8 +286,8 @@ sha1_write( SHA1HANDLE hd, byte *inbuf, size_t inlen)
|
|||
* Returns: 20 bytes representing the digest.
|
||||
*/
|
||||
|
||||
byte *
|
||||
sha1_final(SHA1HANDLE hd)
|
||||
void
|
||||
sha1_final(SHA1_CONTEXT *hd)
|
||||
{
|
||||
u32 t, msb, lsb;
|
||||
byte *p;
|
||||
|
@ -348,37 +300,37 @@ sha1_final(SHA1HANDLE hd)
|
|||
msb++;
|
||||
msb += t >> 26;
|
||||
t = lsb;
|
||||
if( (lsb = t + hd->bufcount) < t ) /* add the bufcount */
|
||||
if( (lsb = t + hd->count) < t ) /* add the count */
|
||||
msb++;
|
||||
t = lsb;
|
||||
if( (lsb = t << 3) < t ) /* multiply by 8 to make a bit count */
|
||||
msb++;
|
||||
msb += t >> 29;
|
||||
|
||||
if( hd->bufcount < 56 ) { /* enough room */
|
||||
hd->buffer[hd->bufcount++] = 0x80; /* pad */
|
||||
while( hd->bufcount < 56 )
|
||||
hd->buffer[hd->bufcount++] = 0; /* pad */
|
||||
if( hd->count < 56 ) { /* enough room */
|
||||
hd->buf[hd->count++] = 0x80; /* pad */
|
||||
while( hd->count < 56 )
|
||||
hd->buf[hd->count++] = 0; /* pad */
|
||||
}
|
||||
else { /* need one extra block */
|
||||
hd->buffer[hd->bufcount++] = 0x80; /* pad character */
|
||||
while( hd->bufcount < 64 )
|
||||
hd->buffer[hd->bufcount++] = 0;
|
||||
hd->buf[hd->count++] = 0x80; /* pad character */
|
||||
while( hd->count < 64 )
|
||||
hd->buf[hd->count++] = 0;
|
||||
sha1_write(hd, NULL, 0); /* flush */;
|
||||
memset(hd->buffer, 0, 56 ); /* fill next block with zeroes */
|
||||
memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
|
||||
}
|
||||
/* append the 64 bit count */
|
||||
hd->buffer[56] = msb >> 24;
|
||||
hd->buffer[57] = msb >> 16;
|
||||
hd->buffer[58] = msb >> 8;
|
||||
hd->buffer[59] = msb ;
|
||||
hd->buffer[60] = lsb >> 24;
|
||||
hd->buffer[61] = lsb >> 16;
|
||||
hd->buffer[62] = lsb >> 8;
|
||||
hd->buffer[63] = lsb ;
|
||||
transform( hd, hd->buffer );
|
||||
hd->buf[56] = msb >> 24;
|
||||
hd->buf[57] = msb >> 16;
|
||||
hd->buf[58] = msb >> 8;
|
||||
hd->buf[59] = msb ;
|
||||
hd->buf[60] = lsb >> 24;
|
||||
hd->buf[61] = lsb >> 16;
|
||||
hd->buf[62] = lsb >> 8;
|
||||
hd->buf[63] = lsb ;
|
||||
transform( hd, hd->buf );
|
||||
|
||||
p = hd->buffer;
|
||||
p = hd->buf;
|
||||
#ifdef BIG_ENDIAN_HOST
|
||||
#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
|
||||
#else /* little endian */
|
||||
|
@ -392,8 +344,6 @@ sha1_final(SHA1HANDLE hd)
|
|||
X(4);
|
||||
#undef X
|
||||
|
||||
initialize( hd ); /* prepare for next cycle */
|
||||
return hd->buffer; /* now contains the digest */
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -25,26 +25,14 @@
|
|||
typedef struct {
|
||||
u32 h0,h1,h2,h3,h4;
|
||||
u32 nblocks;
|
||||
byte buffer[64];
|
||||
int bufcount;
|
||||
} *SHA1HANDLE;
|
||||
byte buf[64];
|
||||
int count;
|
||||
} SHA1_CONTEXT;
|
||||
|
||||
|
||||
/****************
|
||||
* Process a single character, this character will be buffered to
|
||||
* increase performance.
|
||||
*/
|
||||
#define sha1_putchar(h,c) \
|
||||
do { \
|
||||
if( (h)->bufcount == 64 ) \
|
||||
sha1_write( (h), NULL, 0 ); \
|
||||
(h)->buffer[(h)->bufcount++] = (c) & 0xff; \
|
||||
} while(0)
|
||||
|
||||
SHA1HANDLE sha1_open( int secure );
|
||||
SHA1HANDLE sha1_copy( SHA1HANDLE a );
|
||||
void sha1_close( SHA1HANDLE hd );
|
||||
void sha1_write( SHA1HANDLE hd, byte *inbuf, size_t inlen );
|
||||
byte * sha1_final( SHA1HANDLE hd );
|
||||
void sha1_init( SHA1_CONTEXT *c );
|
||||
void sha1_write( SHA1_CONTEXT *hd, byte *inbuf, size_t inlen);
|
||||
void sha1_final( SHA1_CONTEXT *hd);
|
||||
#define sha1_read(h) ( (h)->buf )
|
||||
|
||||
#endif /*G10_SHA1_H*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue