diff options
author | Werner Koch <wk@gnupg.org> | 2002-09-17 12:38:22 +0000 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2002-09-17 12:38:22 +0000 |
commit | e2952f48b02182a55cc00ce2fecbe0fa619c762d (patch) | |
tree | 3b4801ed13ff40ab3a011e063a66f3e6edc5a254 /cipher | |
parent | 04bf238ac21fcc09d48940d8871832216c1b327f (diff) | |
download | libgcrypt-e2952f48b02182a55cc00ce2fecbe0fa619c762d.tar.gz |
* random.c: Replaced mutex.h by the new ath.h. Changed all calls.
Diffstat (limited to 'cipher')
-rw-r--r-- | cipher/ChangeLog | 25 | ||||
-rw-r--r-- | cipher/arcfour.c | 30 | ||||
-rw-r--r-- | cipher/blowfish.c | 2 | ||||
-rw-r--r-- | cipher/cast5.c | 2 | ||||
-rw-r--r-- | cipher/des.c | 2 | ||||
-rw-r--r-- | cipher/md5.c | 14 | ||||
-rw-r--r-- | cipher/primegen.c | 4 | ||||
-rw-r--r-- | cipher/random.c | 94 | ||||
-rw-r--r-- | cipher/rijndael.c | 2 | ||||
-rw-r--r-- | cipher/rmd160.c | 51 | ||||
-rw-r--r-- | cipher/rndegd.c | 2 | ||||
-rw-r--r-- | cipher/rndlinux.c | 17 | ||||
-rw-r--r-- | cipher/sha1.c | 2 | ||||
-rw-r--r-- | cipher/twofish.c | 2 |
14 files changed, 151 insertions, 98 deletions
diff --git a/cipher/ChangeLog b/cipher/ChangeLog index ec355ac0..e54d94ed 100644 --- a/cipher/ChangeLog +++ b/cipher/ChangeLog @@ -1,3 +1,28 @@ +2002-09-17 Werner Koch <wk@gnupg.org> + + * random.c: Replaced mutex.h by the new ath.h. Changed all calls. + +2002-09-16 Werner Koch <wk@gnupg.org> + + * arcfour.c (do_encrypt_stream): Use register modifier and modulo. + According to Nikos Mavroyanopoulos this increases perfromace on + i386 system noticable. And I always tought gcc is clever enough. + * md5.c (transform): Use register modifier. + * rmd160.c (transform): Ditto. + * sha1.c (transform): Ditto. We hope that there are 6 free registers. + * random.c (gcry_randomize): Rewrote to avoid malloc calls. + + * rndlinux.c (gather_random): Replaced remaining fprintfs by log_*. + * arcfour.c (do_arcfour_setkey): Ditto. + * twofish.c (do_twofish_setkey): Ditto. + * rndegd.c (gather_random): Ditto. + * rijndael.c (do_setkey): Ditto. + * random.c (_gcry_random_dump_stats): Ditto. + * primegen.c (_gcry_generate_elg_prime): Ditto. + * des.c (_gcry_des_get_info): Ditto. + * cast5.c (do_cast_setkey): Ditto. + * blowfish.c (do_bf_setkey): Ditto. + 2002-08-26 Werner Koch <wk@gnupg.org> * des.c (weak_keys): Fixed one entry in the table and compared diff --git a/cipher/arcfour.c b/cipher/arcfour.c index 09bdd50d..dec465eb 100644 --- a/cipher/arcfour.c +++ b/cipher/arcfour.c @@ -56,20 +56,22 @@ static void do_encrypt_stream( ARCFOUR_context *ctx, byte *outbuf, const byte *inbuf, unsigned int length ) { - int t; - int i = ctx->idx_i; - int j = ctx->idx_j; - byte *sbox = ctx->sbox; - - while ( length-- ) { - i = (i+1) % 256; - j = (j + sbox[i]) % 256; - t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t; - *outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) % 256]; + register int i = ctx->idx_i; + register int j = ctx->idx_j; + register byte *sbox = ctx->sbox; + register int t; + + while ( length-- ) + { + i = ++i & 255; /* and seems to faster than mod */ + j += sbox[i]; + j &= 255; + t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t; + *outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) & 255]; } - - ctx->idx_i = i; - ctx->idx_j = j; + + ctx->idx_i = i; + ctx->idx_j = j; } static void @@ -94,7 +96,7 @@ do_arcfour_setkey( ARCFOUR_context *ctx, const byte *key, unsigned int keylen ) initialized = 1; selftest_failed = selftest(); if( selftest_failed ) - fprintf(stderr,"ARCFOUR selftest failed (%s)\n", selftest_failed ); + log_error ("ARCFOUR selftest failed (%s)\n", selftest_failed ); } if( selftest_failed ) return GCRYERR_SELFTEST; diff --git a/cipher/blowfish.c b/cipher/blowfish.c index 57c8ab9d..af4f4978 100644 --- a/cipher/blowfish.c +++ b/cipher/blowfish.c @@ -520,7 +520,7 @@ do_bf_setkey( BLOWFISH_context *c, byte *key, unsigned keylen ) initialized = 1; selftest_failed = selftest(); if( selftest_failed ) - fprintf(stderr,"%s\n", selftest_failed ); + log_error ("%s\n", selftest_failed ); } if( selftest_failed ) return GCRYERR_SELFTEST; diff --git a/cipher/cast5.c b/cipher/cast5.c index 35829d61..fbed819e 100644 --- a/cipher/cast5.c +++ b/cipher/cast5.c @@ -591,7 +591,7 @@ do_cast_setkey( CAST5_context *c, byte *key, unsigned keylen ) initialized = 1; selftest_failed = selftest(); if( selftest_failed ) - fprintf(stderr,"CAST5 selftest failed (%s).\n", selftest_failed ); + log_error ("CAST5 selftest failed (%s).\n", selftest_failed ); } if( selftest_failed ) return GCRYERR_SELFTEST; diff --git a/cipher/des.c b/cipher/des.c index 9d05bfac..bb196f4a 100644 --- a/cipher/des.c +++ b/cipher/des.c @@ -1097,7 +1097,7 @@ _gcry_des_get_info( int algo, size_t *keylen, const char *s = selftest(); did_selftest = 1; if( s ) { - fprintf(stderr,"%s\n", s ); + log_error ("%s\n", s ); selftest_failed = s; return NULL; } diff --git a/cipher/md5.c b/cipher/md5.c index 34713a28..f03a301d 100644 --- a/cipher/md5.c +++ b/cipher/md5.c @@ -93,13 +93,13 @@ static void transform( MD5_CONTEXT *ctx, byte *data ) { u32 correct_words[16]; - u32 A = ctx->A; - u32 B = ctx->B; - u32 C = ctx->C; - u32 D = ctx->D; + register u32 A = ctx->A; + register u32 B = ctx->B; + register u32 C = ctx->C; + register u32 D = ctx->D; u32 *cwp = correct_words; - #ifdef BIG_ENDIAN_HOST +#ifdef BIG_ENDIAN_HOST { int i; byte *p2, *p1; for(i=0, p1=data, p2=(byte*)correct_words; i < 16; i++, p2 += 4 ) { @@ -109,9 +109,9 @@ transform( MD5_CONTEXT *ctx, byte *data ) p2[0] = *p1++; } } - #else +#else memcpy( correct_words, data, 64 ); - #endif +#endif #define OP(a, b, c, d, s, T) \ diff --git a/cipher/primegen.c b/cipher/primegen.c index 93c4e316..73f678d4 100644 --- a/cipher/primegen.c +++ b/cipher/primegen.c @@ -305,9 +305,9 @@ _gcry_generate_elg_prime( int mode, unsigned pbits, unsigned qbits, log_mpidump( "factor pi: ", factors[i] ); log_debug("bit sizes: prime=%u, q=%u", mpi_get_nbits(prime), mpi_get_nbits(q) ); if( mode == 1 ) - fprintf(stderr, ", q0=%u", mpi_get_nbits(q_factor) ); + log_debug (", q0=%u", mpi_get_nbits(q_factor) ); for(i=0; i < n; i++ ) - fprintf(stderr, ", p%d=%u", i, mpi_get_nbits(factors[i]) ); + log_debug (", p%d=%u", i, mpi_get_nbits(factors[i]) ); progress('\n'); } diff --git a/cipher/random.c b/cipher/random.c index b4bc2d9d..0659cc3d 100644 --- a/cipher/random.c +++ b/cipher/random.c @@ -55,7 +55,7 @@ #include "rand-internal.h" #include "dynload.h" #include "cipher.h" /* only used for the rmd160_hash_buffer() prototype */ -#include "mutex.h" +#include "ath.h" #ifndef RAND_MAX /* for SunOS */ #define RAND_MAX 32767 @@ -105,8 +105,8 @@ static int secure_alloc; static int quick_test; static int faked_rng; -DEFINE_LOCAL_MUTEX(pool_lock) -static int pool_is_locked; /* only for assertion */ +static ath_mutex_t pool_lock = ATH_MUTEX_INITIALIZER; +static int pool_is_locked; /* only used for assertion */ static byte *get_random_bytes( size_t nbytes, int level, int secure ); static void read_pool( byte *buffer, size_t length, int level ); @@ -138,7 +138,7 @@ initialize(void) { int err; - err = mutex_init (pool_lock); + err = ath_mutex_init (&pool_lock); if (err) log_fatal ("failed to create the pool lock: %s\n", strerror (err) ); @@ -168,7 +168,7 @@ burn_stack (int bytes) void _gcry_random_dump_stats() { - fprintf(stderr, + log_info ( "random usage: poolsize=%d mixed=%lu polls=%lu/%lu added=%lu/%lu\n" " outmix=%lu getlvl1=%lu/%lu getlvl2=%lu/%lu\n", POOLSIZE, rndstats.mixrnd, rndstats.slowpolls, rndstats.fastpolls, @@ -199,25 +199,6 @@ _gcry_quick_random_gen( int onoff ) return faked_rng? 1 : last; } - -/**************** - * Fill the buffer with LENGTH bytes of cryptographically strong - * random bytes. level 0 is not very strong, 1 is strong enough - * for most usage, 2 is good for key generation stuff but may be very slow. - */ -void -gcry_randomize( byte *buffer, size_t length, enum gcry_random_level level ) -{ - char *p; - - if (!is_initialized) - initialize (); - p = get_random_bytes( length, level, 1 ); - memcpy( buffer, p, length ); - gcry_free(p); -} - - int _gcry_random_is_faked() { @@ -241,7 +222,7 @@ get_random_bytes( size_t nbytes, int level, int secure ) level = 1; MASK_LEVEL(level); - err = mutex_lock (pool_lock); + err = ath_mutex_lock (&pool_lock); if (err) log_fatal ("failed to acquire the pool lock: %s\n", strerror (err)); pool_is_locked = 1; @@ -264,7 +245,7 @@ get_random_bytes( size_t nbytes, int level, int secure ) } pool_is_locked = 0; - err = mutex_unlock (pool_lock); + err = ath_mutex_unlock (&pool_lock); if (err) log_fatal ("failed to release the pool lock: %s\n", strerror (err)); return buf; @@ -287,6 +268,55 @@ gcry_random_bytes_secure( size_t nbytes, enum gcry_random_level level ) } +/* Fill the buffer with LENGTH bytes of cryptographically strong + random bytes. level 0 is not very strong, 1 is strong enough for + most usage, 2 is good for key generation stuff but may be very + slow. */ +void +gcry_randomize (byte *buffer, size_t length, enum gcry_random_level level) +{ + byte *p; + int err; + + if (!is_initialized) + initialize (); + + if( quick_test && level > 1 ) + level = 1; + MASK_LEVEL(level); + + err = ath_mutex_lock (&pool_lock); + if (err) + log_fatal ("failed to acquire the pool lock: %s\n", strerror (err)); + pool_is_locked = 1; + if (level == 1) + { + rndstats.getbytes1 += length; + rndstats.ngetbytes1++; + } + else if (level >= 2) + { + rndstats.getbytes2 += length; + rndstats.ngetbytes2++; + } + + for (p = buffer; length > 0;) + { + size_t n = length > POOLSIZE? POOLSIZE : length; + read_pool (p, n, level); + length -= n; + p += n; + } + + pool_is_locked = 0; + err = ath_mutex_unlock (&pool_lock); + if (err) + log_fatal ("failed to release the pool lock: %s\n", strerror (err)); +} + + + + /* Mix the pool: @@ -483,7 +513,7 @@ _gcry_update_random_seed_file() return; } - err = mutex_lock (pool_lock); + err = ath_mutex_lock (&pool_lock); if (err) log_fatal ("failed to acquire the pool lock: %s\n", strerror (err)); pool_is_locked = 1; @@ -518,7 +548,7 @@ _gcry_update_random_seed_file() } pool_is_locked = 0; - err = mutex_unlock (pool_lock); + err = ath_mutex_unlock (&pool_lock); if (err) log_fatal ("failed to release the pool lock: %s\n", strerror (err)); } @@ -745,18 +775,18 @@ _gcry_fast_random_poll() /* We have to make sure that the intialization is done because this gatherer might be called before any other functions and it is not sufficient to initialize it within do_fast_random_pool becuase we - want to use the mutex here. FIXME: Weh should initialie the mutex - using a global constructore independent from the initialization + want to use the mutex here. FIXME: Whe should initialize the mutex + using a global constructor independent from the initialization of the pool. */ if (!is_initialized) initialize (); - err = mutex_lock (pool_lock); + err = ath_mutex_lock (&pool_lock); if (err) log_fatal ("failed to acquire the pool lock: %s\n", strerror (err)); pool_is_locked = 1; do_fast_random_poll (); pool_is_locked = 0; - err = mutex_unlock (pool_lock); + err = ath_mutex_unlock (&pool_lock); if (err) log_fatal ("failed to acquire the pool lock: %s\n", strerror (err)); diff --git a/cipher/rijndael.c b/cipher/rijndael.c index 98efa210..f4b1fdd9 100644 --- a/cipher/rijndael.c +++ b/cipher/rijndael.c @@ -1736,7 +1736,7 @@ do_setkey (RIJNDAEL_context *ctx, const byte *key, const unsigned keylen) initialized = 1; selftest_failed = selftest (); if( selftest_failed ) - fprintf(stderr, "%s\n", selftest_failed ); + log_error ("%s\n", selftest_failed ); } if( selftest_failed ) return GCRYERR_SELFTEST; diff --git a/cipher/rmd160.c b/cipher/rmd160.c index 485683f6..3680f0ad 100644 --- a/cipher/rmd160.c +++ b/cipher/rmd160.c @@ -174,33 +174,30 @@ _gcry_rmd160_init( RMD160_CONTEXT *hd ) static void transform( RMD160_CONTEXT *hd, byte *data ) { - u32 a,b,c,d,e,aa,bb,cc,dd,ee,t; - #ifdef BIG_ENDIAN_HOST - u32 x[16]; - { int i; - byte *p2, *p1; - for(i=0, p1=data, p2=(byte*)x; i < 16; i++, p2 += 4 ) { - p2[3] = *p1++; - p2[2] = *p1++; - p2[1] = *p1++; - p2[0] = *p1++; - } - } - #else - #if 0 - u32 *x =(u32*)data; - #else - /* this version is better because it is always aligned; - * The performance penalty on a 586-100 is about 6% which - * is acceptable - because the data is more local it might - * also be possible that this is faster on some machines. - * This function (when compiled with -02 on gcc 2.7.2) - * executes on a 586-100 (39.73 bogomips) at about 1900kb/sec; - * [measured with a 4MB data and "gpgm --print-md rmd160"] */ - u32 x[16]; - memcpy( x, data, 64 ); - #endif - #endif + register u32 a,b,c,d,e; + u32 aa,bb,cc,dd,ee,t; +#ifdef BIG_ENDIAN_HOST + u32 x[16]; + { int i; + byte *p2, *p1; + for(i=0, p1=data, p2=(byte*)x; i < 16; i++, p2 += 4 ) { + p2[3] = *p1++; + p2[2] = *p1++; + p2[1] = *p1++; + p2[0] = *p1++; + } + } +#else + /* this version is better because it is always aligned; + * The performance penalty on a 586-100 is about 6% which + * is acceptable - because the data is more local it might + * also be possible that this is faster on some machines. + * This function (when compiled with -02 on gcc 2.7.2) + * executes on a 586-100 (39.73 bogomips) at about 1900kb/sec; + * [measured with a 4MB data and "gpgm --print-md rmd160"] */ + u32 x[16]; + memcpy( x, data, 64 ); +#endif #define K0 0x00000000 diff --git a/cipher/rndegd.c b/cipher/rndegd.c index 8dd705dd..f2a3d3fe 100644 --- a/cipher/rndegd.c +++ b/cipher/rndegd.c @@ -194,7 +194,7 @@ gather_random( void (*add)(const void*, size_t, int), int requester, } if( length ) { - fprintf( stderr, + log_info ( _("Please wait, entropy is being gathered. Do some work if it would\n" "keep you from getting bored, because it will improve the quality\n" "of the entropy.\n") ); diff --git a/cipher/rndlinux.c b/cipher/rndlinux.c index 27448784..143c58a2 100644 --- a/cipher/rndlinux.c +++ b/cipher/rndlinux.c @@ -120,18 +120,17 @@ gather_random( void (*add)(const void*, size_t, int), int requester, tv.tv_sec = 3; tv.tv_usec = 0; if( !(rc=select(fd+1, &rfds, NULL, NULL, &tv)) ) { - #warning FIXME: Replace fprintf by a callback - if( !warn ) - fprintf(stderr, -_("\n" -"Not enough random bytes available. Please do some other work to give\n" -"the OS a chance to collect more entropy! (Need %d more bytes)\n"), - (int)length ); - warn = 1; + if( !warn ) + { + log_info (_("not enough random bytes available (need %d bytes)\n"), + (int)length); + log_info (_("please do some other work to give the OS a chance to collect more entropy\n")); + } + warn = 1; continue; } else if( rc == -1 ) { - fprintf(stderr, "select() error: %s\n", strerror(errno)); + log_error ("select() error: %s\n", strerror(errno)); continue; } diff --git a/cipher/sha1.c b/cipher/sha1.c index 0451b2e7..b1caf218 100644 --- a/cipher/sha1.c +++ b/cipher/sha1.c @@ -79,7 +79,7 @@ sha1_init( SHA1_CONTEXT *hd ) static void transform( SHA1_CONTEXT *hd, byte *data ) { - u32 a,b,c,d,e,tm; + register u32 a,b,c,d,e,tm; u32 x[16]; /* get values from the chaining vars */ diff --git a/cipher/twofish.c b/cipher/twofish.c index 8cb8c8cf..4898b4ba 100644 --- a/cipher/twofish.c +++ b/cipher/twofish.c @@ -604,7 +604,7 @@ do_twofish_setkey (TWOFISH_context *ctx, const byte *key, const unsigned keylen) initialized = 1; selftest_failed = selftest (); if( selftest_failed ) - fprintf(stderr, "%s\n", selftest_failed ); + log_error("%s\n", selftest_failed ); } if( selftest_failed ) return GCRYERR_SELFTEST; |