diff options
Diffstat (limited to 'cipher')
-rw-r--r-- | cipher/Makefile.am | 1 | ||||
-rw-r--r-- | cipher/Makefile.in | 9 | ||||
-rw-r--r-- | cipher/blowfish.c | 36 | ||||
-rw-r--r-- | cipher/elgamal.c | 32 | ||||
-rw-r--r-- | cipher/elgamal.h | 4 | ||||
-rw-r--r-- | cipher/md.c | 158 | ||||
-rw-r--r-- | cipher/md5.c | 12 | ||||
-rw-r--r-- | cipher/rmd160.c | 13 |
8 files changed, 225 insertions, 40 deletions
diff --git a/cipher/Makefile.am b/cipher/Makefile.am index b994cbe0e..939a48303 100644 --- a/cipher/Makefile.am +++ b/cipher/Makefile.am @@ -18,6 +18,7 @@ cipher_SOURCES = blowfish.c \ random.c \ rmd.h \ rmd160.c \ + md.c \ smallprime.c cipher_LIBADD = rsa.o diff --git a/cipher/Makefile.in b/cipher/Makefile.in index 0daff112c..96081b73f 100644 --- a/cipher/Makefile.in +++ b/cipher/Makefile.in @@ -55,6 +55,7 @@ cipher_SOURCES = blowfish.c \ random.c \ rmd.h \ rmd160.c \ + md.c \ smallprime.c cipher_LIBADD = rsa.o @@ -77,7 +78,7 @@ LIBS = @LIBS@ COMPILE = $(CC) -c $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) LINK = $(CC) $(LDFLAGS) -o $@ cipher_OBJECTS = blowfish.o elgamal.o gost.o md5.o primegen.o random.o \ -rmd160.o smallprime.o +rmd160.o md.o smallprime.o EXTRA_cipher_SOURCES = LIBFILES = libcipher.a AR = ar @@ -95,9 +96,9 @@ DEP_DISTFILES = $(DIST_COMMON) $(SOURCES) $(BUILT_SOURCES) $(HEADERS) \ TAR = tar DEP_FILES = $(srcdir)/.deps/blowfish.P $(srcdir)/.deps/elgamal.P \ -$(srcdir)/.deps/gost.P $(srcdir)/.deps/md5.P $(srcdir)/.deps/primegen.P \ -$(srcdir)/.deps/random.P $(srcdir)/.deps/rmd160.P \ -$(srcdir)/.deps/smallprime.P +$(srcdir)/.deps/gost.P $(srcdir)/.deps/md.P $(srcdir)/.deps/md5.P \ +$(srcdir)/.deps/primegen.P $(srcdir)/.deps/random.P \ +$(srcdir)/.deps/rmd160.P $(srcdir)/.deps/smallprime.P SOURCES = $(cipher_SOURCES) OBJECTS = $(cipher_OBJECTS) diff --git a/cipher/blowfish.c b/cipher/blowfish.c index 59f1afa8c..97817e33d 100644 --- a/cipher/blowfish.c +++ b/cipher/blowfish.c @@ -242,7 +242,7 @@ function_F( BLOWFISH_context *bc, u32 x ) static void -encipher( BLOWFISH_context *bc, u32 *ret_xl, u32 *ret_xr ) +encrypted( BLOWFISH_context *bc, u32 *ret_xl, u32 *ret_xr ) { u32 xl, xr, temp; int i; @@ -270,7 +270,7 @@ encipher( BLOWFISH_context *bc, u32 *ret_xl, u32 *ret_xr ) } static void -decipher( BLOWFISH_context *bc, u32 *ret_xl, u32 *ret_xr ) +decrypted( BLOWFISH_context *bc, u32 *ret_xl, u32 *ret_xr ) { u32 xl, xr, temp; int i; @@ -298,25 +298,25 @@ decipher( BLOWFISH_context *bc, u32 *ret_xl, u32 *ret_xr ) } static void -encipher_block( BLOWFISH_context *bc, byte *outbuf, byte *inbuf ) +encrypted_block( BLOWFISH_context *bc, byte *outbuf, byte *inbuf ) { u32 d1, d2; d1 = ((u32*)inbuf)[0]; d2 = ((u32*)inbuf)[1]; - encipher( bc, &d1, &d2 ); + encrypted( bc, &d1, &d2 ); ((u32*)outbuf)[0] = d1; ((u32*)outbuf)[1] = d2; } static void -decipher_block( BLOWFISH_context *bc, byte *outbuf, byte *inbuf ) +decrypted_block( BLOWFISH_context *bc, byte *outbuf, byte *inbuf ) { u32 d1, d2; d1 = ((u32*)inbuf)[0]; d2 = ((u32*)inbuf)[1]; - decipher( bc, &d1, &d2 ); + decrypted( bc, &d1, &d2 ); ((u32*)outbuf)[0] = d1; ((u32*)outbuf)[1] = d2; } @@ -349,27 +349,27 @@ blowfish_setkey( BLOWFISH_context *c, byte *key, unsigned keylen ) datal = datar = 0; for(i=0; i < BLOWFISH_ROUNDS+2; i += 2 ) { - encipher( c, &datal, &datar ); + encrypted( c, &datal, &datar ); c->p[i] = datal; c->p[i+1] = datar; } for(i=0; i < 256; i += 2 ) { - encipher( c, &datal, &datar ); + encrypted( c, &datal, &datar ); c->s0[i] = datal; c->s0[i+1] = datar; } for(i=0; i < 256; i += 2 ) { - encipher( c, &datal, &datar ); + encrypted( c, &datal, &datar ); c->s1[i] = datal; c->s1[i+1] = datar; } for(i=0; i < 256; i += 2 ) { - encipher( c, &datal, &datar ); + encrypted( c, &datal, &datar ); c->s2[i] = datal; c->s2[i+1] = datar; } for(i=0; i < 256; i += 2 ) { - encipher( c, &datal, &datar ); + encrypted( c, &datal, &datar ); c->s3[i] = datal; c->s3[i+1] = datar; } @@ -384,7 +384,7 @@ blowfish_setiv( BLOWFISH_context *c, byte *iv ) else memset( c->iv, 0, BLOWFISH_BLOCKSIZE ); c->count = 0; - encipher_block( c, c->eniv, c->iv ); + encrypted_block( c, c->eniv, c->iv ); } @@ -395,7 +395,7 @@ blowfish_encode( BLOWFISH_context *c, byte *outbuf, byte *inbuf, unsigned n; for(n=0; n < nblocks; n++ ) { - encipher_block( c, outbuf, inbuf ); + encrypted_block( c, outbuf, inbuf ); inbuf += BLOWFISH_BLOCKSIZE;; outbuf += BLOWFISH_BLOCKSIZE; } @@ -408,7 +408,7 @@ blowfish_decode( BLOWFISH_context *c, byte *outbuf, byte *inbuf, unsigned n; for(n=0; n < nblocks; n++ ) { - decipher_block( c, outbuf, inbuf ); + decrypted_block( c, outbuf, inbuf ); inbuf += BLOWFISH_BLOCKSIZE;; outbuf += BLOWFISH_BLOCKSIZE; } @@ -451,7 +451,7 @@ blowfish_encode_cfb( BLOWFISH_context *c, byte *outbuf, outbuf += n; assert( c->count <= BLOWFISH_BLOCKSIZE); if( c->count == BLOWFISH_BLOCKSIZE ) { - encipher_block( c, c->eniv, c->iv ); + encrypted_block( c, c->eniv, c->iv ); c->count = 0; } else @@ -461,7 +461,7 @@ blowfish_encode_cfb( BLOWFISH_context *c, byte *outbuf, while( nbytes >= BLOWFISH_BLOCKSIZE ) { xorblock( outbuf, c->eniv, inbuf, BLOWFISH_BLOCKSIZE); memcpy( c->iv, outbuf, BLOWFISH_BLOCKSIZE); - encipher_block( c, c->eniv, c->iv ); + encrypted_block( c, c->eniv, c->iv ); nbytes -= BLOWFISH_BLOCKSIZE; inbuf += BLOWFISH_BLOCKSIZE; outbuf += BLOWFISH_BLOCKSIZE; @@ -495,7 +495,7 @@ blowfish_decode_cfb( BLOWFISH_context *c, byte *outbuf, outbuf += n; assert( c->count <= BLOWFISH_BLOCKSIZE); if( c->count == BLOWFISH_BLOCKSIZE ) { - encipher_block( c, c->eniv, c->iv ); + encrypted_block( c, c->eniv, c->iv ); c->count = 0; } else @@ -506,7 +506,7 @@ blowfish_decode_cfb( BLOWFISH_context *c, byte *outbuf, while( nbytes >= BLOWFISH_BLOCKSIZE ) { memcpy( c->iv, inbuf, BLOWFISH_BLOCKSIZE); xorblock( outbuf, c->eniv, inbuf, BLOWFISH_BLOCKSIZE); - encipher_block( c, c->eniv, c->iv ); + encrypted_block( c, c->eniv, c->iv ); nbytes -= BLOWFISH_BLOCKSIZE; inbuf += BLOWFISH_BLOCKSIZE; outbuf += BLOWFISH_BLOCKSIZE; diff --git a/cipher/elgamal.c b/cipher/elgamal.c index b1239732d..a7450e068 100644 --- a/cipher/elgamal.c +++ b/cipher/elgamal.c @@ -60,10 +60,10 @@ test_keys( ELG_public_key *pk, ELG_secret_key *sk, unsigned nbits ) mpi_set_bytes( test, nbits, get_random_byte, 0 ); - elg_encipher( out1_a, out1_b, test, pk ); - elg_decipher( out2, out1_a, out1_b, sk ); + elg_encrypted( out1_a, out1_b, test, pk ); + elg_decrypted( out2, out1_a, out1_b, sk ); if( mpi_cmp( test, out2 ) ) - log_fatal("ElGamal operation: encipher, decipher failed\n"); + log_fatal("ElGamal operation: encrypted, decrypted failed\n"); elg_sign( out1_a, out1_b, test, sk ); if( !elg_verify( out1_a, out1_b, test, pk ) ) @@ -182,7 +182,7 @@ elg_check_secret_key( ELG_secret_key *sk ) void -elg_encipher(MPI a, MPI b, MPI input, ELG_public_key *pkey ) +elg_encrypted(MPI a, MPI b, MPI input, ELG_public_key *pkey ) { MPI k; @@ -197,12 +197,12 @@ elg_encipher(MPI a, MPI b, MPI input, ELG_public_key *pkey ) mpi_mulm( b, b, input, pkey->p ); #if 0 if( DBG_CIPHER ) { - log_mpidump("elg encipher y= ", pkey->y); - log_mpidump("elg encipher p= ", pkey->p); - log_mpidump("elg encipher k= ", k); - log_mpidump("elg encipher M= ", input); - log_mpidump("elg encipher a= ", a); - log_mpidump("elg encipher b= ", b); + log_mpidump("elg encrypted y= ", pkey->y); + log_mpidump("elg encrypted p= ", pkey->p); + log_mpidump("elg encrypted k= ", k); + log_mpidump("elg encrypted M= ", input); + log_mpidump("elg encrypted a= ", a); + log_mpidump("elg encrypted b= ", b); } #endif mpi_free(k); @@ -212,7 +212,7 @@ elg_encipher(MPI a, MPI b, MPI input, ELG_public_key *pkey ) void -elg_decipher(MPI output, MPI a, MPI b, ELG_secret_key *skey ) +elg_decrypted(MPI output, MPI a, MPI b, ELG_secret_key *skey ) { MPI t1 = mpi_alloc_secure( mpi_get_nlimbs( skey->p ) ); @@ -223,11 +223,11 @@ elg_decipher(MPI output, MPI a, MPI b, ELG_secret_key *skey ) mpi_mulm( output, b, t1, skey->p ); #if 0 if( DBG_CIPHER ) { - log_mpidump("elg decipher x= ", skey->x); - log_mpidump("elg decipher p= ", skey->p); - log_mpidump("elg decipher a= ", a); - log_mpidump("elg decipher b= ", b); - log_mpidump("elg decipher M= ", output); + log_mpidump("elg decrypted x= ", skey->x); + log_mpidump("elg decrypted p= ", skey->p); + log_mpidump("elg decrypted a= ", a); + log_mpidump("elg decrypted b= ", b); + log_mpidump("elg decrypted M= ", output); } #endif mpi_free(t1); diff --git a/cipher/elgamal.h b/cipher/elgamal.h index e93b49e59..4086a8fc5 100644 --- a/cipher/elgamal.h +++ b/cipher/elgamal.h @@ -41,8 +41,8 @@ void elg_free_public_key( ELG_public_key *pk ); void elg_free_secret_key( ELG_secret_key *sk ); void elg_generate( ELG_public_key *pk, ELG_secret_key *sk, unsigned nbits ); int elg_check_secret_key( ELG_secret_key *sk ); -void elg_encipher(MPI a, MPI b, MPI input, ELG_public_key *pkey ); -void elg_decipher(MPI output, MPI a, MPI b, ELG_secret_key *skey ); +void elg_encrypted(MPI a, MPI b, MPI input, ELG_public_key *pkey ); +void elg_decrypted(MPI output, MPI a, MPI b, ELG_secret_key *skey ); void elg_sign(MPI a, MPI b, MPI input, ELG_secret_key *skey); int elg_verify(MPI a, MPI b, MPI input, ELG_public_key *pkey); diff --git a/cipher/md.c b/cipher/md.c new file mode 100644 index 000000000..a14d62f32 --- /dev/null +++ b/cipher/md.c @@ -0,0 +1,158 @@ +/* md.c - message digest dispatcher + * 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 + */ + +#include <config.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include "util.h" +#include "cipher.h" +#include "errors.h" + +int +md_okay( int algo ) +{ + switch( algo ) { + case DIGEST_ALGO_MD5: + case DIGEST_ALGO_RMD160: + return 0; + default: + return G10ERR_DIGEST_ALGO; + } +} + + +MD_HANDLE * +md_open( int algo, int secure ) +{ + 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 + 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 + 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 + log_bug(NULL); + return hd; +} + +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 + log_bug(NULL); + m_free(a); +} + + +void +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 + log_bug(NULL); +} + + +void +md_putchar( MD_HANDLE *a, int c ) +{ + 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 + log_bug(NULL); +} + + +byte * +md_final(MD_HANDLE *a) +{ + 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; + } + 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 + log_bug(NULL); +} + + diff --git a/cipher/md5.c b/cipher/md5.c index 98429ab77..06e3a88f4 100644 --- a/cipher/md5.c +++ b/cipher/md5.c @@ -55,6 +55,7 @@ #include <assert.h> #include "util.h" #include "md5.h" +#include "cipher.h" /* kludge for md5_copy2md() */ #include "memory.h" @@ -142,6 +143,17 @@ md5_copy( MD5HANDLE 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) { diff --git a/cipher/rmd160.c b/cipher/rmd160.c index 9a882fc5e..2c583d576 100644 --- a/cipher/rmd160.c +++ b/cipher/rmd160.c @@ -25,6 +25,7 @@ #include <assert.h> #include "util.h" #include "memory.h" +#include "cipher.h" /* grrrr */ #include "rmd.h" /********************************* @@ -261,6 +262,18 @@ rmd160_copy( RMDHANDLE 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) { |