diff options
Diffstat (limited to 'g10')
-rw-r--r-- | g10/ChangeLog | 10 | ||||
-rw-r--r-- | g10/cipher.c | 59 | ||||
-rw-r--r-- | g10/encr-data.c | 28 | ||||
-rw-r--r-- | g10/filter.h | 15 | ||||
-rw-r--r-- | g10/misc.c | 12 |
5 files changed, 85 insertions, 39 deletions
diff --git a/g10/ChangeLog b/g10/ChangeLog index a614d0f25..32d5f36a4 100644 --- a/g10/ChangeLog +++ b/g10/ChangeLog @@ -1,3 +1,13 @@ +Sat Sep 18 12:16:08 CEST 1999 Werner Koch <[email protected]> + + + * filter.h: Changed cipher handle types to the the GCRY_xxx ones. + replaces include cipher by system header include gcrypt.h. + * cipher.c: replaced the cipher functions by the gcry_ ones. + Ditto for the md functions. + + * misc.c (map_gcry_rc): New. + Fri Sep 17 12:56:42 CEST 1999 Werner Koch <[email protected]> diff --git a/g10/cipher.c b/g10/cipher.c index 0de2a9d90..e458a807c 100644 --- a/g10/cipher.c +++ b/g10/cipher.c @@ -44,40 +44,58 @@ write_header( cipher_filter_context_t *cfx, IOBUF a ) PACKET pkt; PKT_encrypted ed; byte temp[18]; - unsigned blocksize; + int blocksize; unsigned nprefix; int use_mdc = opt.force_mdc; + int rc; memset( &ed, 0, sizeof ed ); ed.len = cfx->datalen; ed.new_ctb = !ed.len && !opt.rfc1991; if( use_mdc ) { ed.mdc_method = DIGEST_ALGO_SHA1; - cfx->mdc_hash = md_open( DIGEST_ALGO_SHA1, 0 ); - md_start_debug( cfx->mdc_hash, "mdccreat" ); + cfx->mdc_hash = gcry_md_open( DIGEST_ALGO_SHA1, 0 ); + /*md_start_debug( cfx->mdc_hash, "mdccreat" );*/ } init_packet( &pkt ); pkt.pkttype = use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED; pkt.pkt.encrypted = &ed; if( build_packet( a, &pkt )) log_bug("build_packet(ENCR_DATA) failed\n"); - blocksize = cipher_get_blocksize( cfx->dek->algo ); + blocksize = gcry_cipher_get_algo_blklen( cfx->dek->algo ); if( blocksize < 8 || blocksize > 16 ) - log_fatal("unsupported blocksize %u\n", blocksize ); + log_fatal("unsupported blocksize %d\n", blocksize ); nprefix = blocksize; randomize_buffer( temp, nprefix, 1 ); temp[nprefix] = temp[nprefix-2]; temp[nprefix+1] = temp[nprefix-1]; print_cipher_algo_note( cfx->dek->algo ); - cfx->cipher_hd = cipher_open( cfx->dek->algo, CIPHER_MODE_AUTO_CFB, 1 ); + if( gcry_cipher_open( &cfx->cipher_hd, + cfx->dek->algo, + CIPHER_MODE_CFB, + GCRY_CIPHER_SECURE + | (cfy->dek->algo >= 100 ? + 0 : GCRY_CIPHER_ENABLE_SYNC) ) + ) { + /* we should never get an error here cause we already checked, that + * the algorithm is available. */ + BUG(); + } + /* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/ - cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen ); - cipher_setiv( cfx->cipher_hd, NULL, 0 ); + rc = gcry_cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen ); + if( !rc ) + rc = gcry_cipher_setiv( cfx->cipher_hd, NULL, 0 ); + if( rc ) + log_fatal("set key or IV failed: %s\n", gcry_strerror(rc) ); /* log_hexdump( "prefix", temp, nprefix+2 ); */ if( cfx->mdc_hash ) - md_write( cfx->mdc_hash, temp, nprefix+2 ); - cipher_encrypt( cfx->cipher_hd, temp, temp, nprefix+2); - cipher_sync( cfx->cipher_hd ); + gcry_md_write( cfx->mdc_hash, temp, nprefix+2 ); + rc = cipher_encrypt( cfx->cipher_hd, temp, nprefix+2, NULL, 0 ); + if( !rc ) + rc = gcry_cipher_sync( cfx->cipher_hd ); + if( rc ) + log_fatal("encrypt failed: %s\n", gcry_strerror(rc) ); iobuf_write(a, temp, nprefix+2); cfx->header=1; } @@ -104,23 +122,26 @@ cipher_filter( void *opaque, int control, write_header( cfx, a ); } if( cfx->mdc_hash ) - md_write( cfx->mdc_hash, buf, size ); - cipher_encrypt( cfx->cipher_hd, buf, buf, size); + gcry_md_write( cfx->mdc_hash, buf, size ); + rc = gcry_cipher_encrypt( cfx->cipher_hd, buf, size, NULL, 0); + if( rc ) + log_fatal("encrypt failed: %s\n", gcry_strerror(rc) ); if( iobuf_write( a, buf, size ) ) rc = G10ERR_WRITE_FILE; } else if( control == IOBUFCTRL_FREE ) { if( cfx->mdc_hash ) { byte *hash; - int hashlen = md_digest_length( md_get_algo( cfx->mdc_hash ) ); - md_final( cfx->mdc_hash ); - hash = md_read( cfx->mdc_hash, 0 ); - cipher_encrypt( cfx->cipher_hd, hash, hash, hashlen ); + int hashlen = gcry_md_get_algo_dlen( gcry_md_get_algo( cfx->mdc_hash ) ); + hash = gcry_md_read( cfx->mdc_hash, 0 ); + rc = gcry_cipher_encrypt( cfx->cipher_hd, hash, hashlen, NULL, 0 ); + if( rc ) + log_fatal("encrypt failed: %s\n", gcry_strerror(rc) ); if( iobuf_write( a, hash, hashlen ) ) rc = G10ERR_WRITE_FILE; - md_close( cfx->mdc_hash ); cfx->mdc_hash = NULL; + gcry_md_close( cfx->mdc_hash ); cfx->mdc_hash = NULL; } - cipher_close(cfx->cipher_hd); + gcry_cipher_close(cfx->cipher_hd); } else if( control == IOBUFCTRL_DESC ) { *(char**)buf = "cipher_filter"; diff --git a/g10/encr-data.c b/g10/encr-data.c index 0593efe97..0b3accf56 100644 --- a/g10/encr-data.c +++ b/g10/encr-data.c @@ -23,6 +23,7 @@ #include <stdlib.h> #include <string.h> #include <assert.h> +#include <gcrypt.h> #include "util.h" #include "memory.h" #include "packet.h" @@ -38,8 +39,8 @@ static int mdc_decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len); typedef struct { - CIPHER_HANDLE cipher_hd; - MD_HANDLE mdc_hash; + GCRY_CIPHER_HD cipher_hd; + GCRY_MD_HD mdc_hash; char defer[20]; int defer_filled; int eof_seen; @@ -55,27 +56,28 @@ decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek ) decode_filter_ctx_t dfx; byte *p; int rc=0, c, i; + int algo_okay; byte temp[32]; - unsigned blocksize; + int blocksize; unsigned nprefix; memset( &dfx, 0, sizeof dfx ); - if( opt.verbose ) { - const char *s = cipher_algo_to_string( dek->algo ); - if( s ) - log_info(_("%s encrypted data\n"), s ); - else + if( gcry_cipher_test_algo( dek->algo ) ) { + if( opt.verbose ) log_info(_("encrypted with unknown algorithm %d\n"), dek->algo ); - } - if( (rc=check_cipher_algo(dek->algo)) ) + rc = G10ERR_CIPHER_ALGO; goto leave; - blocksize = cipher_get_blocksize(dek->algo); - if( !blocksize || blocksize > 16 ) + } + if( opt.verbose ) + log_info(_("%s encrypted data\n"), gcry_cipher_algo_name( dek->algo ) ); + + blocksize = gcry_cipher_get_blklen( dek->algo ); + if( blocksize < 1 || blocksize > 16 ) log_fatal("unsupported blocksize %u\n", blocksize ); nprefix = blocksize; if( ed->len && ed->len < (nprefix+2) ) BUG(); - +--> We are currently working HERE!!!! if( ed->mdc_method ) dfx.mdc_hash = md_open( ed->mdc_method, 0 ); dfx.cipher_hd = cipher_open( dek->algo, CIPHER_MODE_AUTO_CFB, 1 ); diff --git a/g10/filter.h b/g10/filter.h index 86a8e45b8..02e4e2ed6 100644 --- a/g10/filter.h +++ b/g10/filter.h @@ -20,12 +20,13 @@ #ifndef G10_FILTER_H #define G10_FILTER_H +#include <gcrypt.h> + #include "types.h" -#include "cipher.h" typedef struct { - MD_HANDLE md; /* catch all */ - MD_HANDLE md2; /* if we want to calculate an alternate hash */ + GCRY_MD_HD md; /* catch all */ + GCRY_MD_HD md2; /* if we want to calculate an alternate hash */ size_t maxbuf_size; } md_filter_context_t; @@ -76,9 +77,9 @@ typedef struct { typedef struct { DEK *dek; u32 datalen; - CIPHER_HANDLE cipher_hd; + GCRY_CIPHER_HD cipher_hd; int header; - MD_HANDLE mdc_hash; + GCRY_MD_HD mdc_hash; } cipher_filter_context_t; @@ -91,7 +92,7 @@ typedef struct { int truncated; /* number of truncated lines */ int not_dash_escaped; int escape_from; - MD_HANDLE md; + GCRY_MD_HD md; int pending_lf; int pending_esc; } text_filter_context_t; @@ -119,7 +120,7 @@ int cipher_filter( void *opaque, int control, /*-- textfilter.c --*/ int text_filter( void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len); -int copy_clearsig_text( IOBUF out, IOBUF inp, MD_HANDLE md, +int copy_clearsig_text( IOBUF out, IOBUF inp, GCRY_MD_HD md, int escape_dash, int escape_from, int pgp2mode ); diff --git a/g10/misc.c b/g10/misc.c index 3f18183b5..61f104a01 100644 --- a/g10/misc.c +++ b/g10/misc.c @@ -253,3 +253,15 @@ print_digest_algo_note( int algo ) +/**************** + * Map errors retuned by libgcrypt to those used by GnuPG. + */ +int +map_gcry_rc( int rc ) +{ + switch( rc ) { + case 0: return 0; + default: return G10ERR_GENERAL; + } +} + |