diff options
author | Ulrich Müller <ulm@gentoo.org> | 2012-02-16 21:58:52 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2013-03-20 12:55:47 +0100 |
commit | 4f57ad0f8744263c917b3f16d84c2e5204893422 (patch) | |
tree | 69af4d1cbfc6c7a23d357fe754693b25d7f00143 | |
parent | 0fb5e52d417d5430ce8abf9af2ce1cd6b2bfa8b8 (diff) | |
download | libgcrypt-4f57ad0f8744263c917b3f16d84c2e5204893422.tar.gz |
Rework selftest in idea.c.
* cipher/idea.c (do_setkey): Execute selftest when first called.
(decrypt_block): Remove commented-out code.
(selftest): Execute all selftests. Return NULL on success, or
string in case of error.
-rw-r--r-- | cipher/idea.c | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/cipher/idea.c b/cipher/idea.c index fe14b219..39c97202 100644 --- a/cipher/idea.c +++ b/cipher/idea.c @@ -63,6 +63,8 @@ typedef struct { int have_dk; } IDEA_context; +static const char *selftest(void); + static u16 mul_inv( u16 x ) @@ -236,14 +238,18 @@ cipher( byte *outbuf, const byte *inbuf, u16 *key ) static int do_setkey( IDEA_context *c, const byte *key, unsigned int keylen ) { -#if 0 static int initialized = 0; + static const char *selftest_failed = 0; if( !initialized ) { initialized = 1; - selftest(0); + selftest_failed = selftest(); + if( selftest_failed ) + log_error( "%s\n", selftest_failed ); } -#endif + if( selftest_failed ) + return GPG_ERR_SELFTEST_FAILED; + assert(keylen == 16); c->have_dk = 0; expand_key( key, c->ek ); @@ -277,14 +283,6 @@ idea_encrypt (void *context, byte *out, const byte *in) static void decrypt_block( IDEA_context *c, byte *outbuf, const byte *inbuf ) { -#if 0 - static int initialized; - - if( !initialized ) { - initialized = 1; - selftest(1); - } -#endif if( !c->have_dk ) { c->have_dk = 1; invert_key( c->ek, c->dk ); @@ -301,9 +299,8 @@ idea_decrypt (void *context, byte *out, const byte *in) } -#if 0 -static void -selftest( int check_decrypt ) +static const char * +selftest( void ) { static struct { byte key[16]; @@ -361,19 +358,16 @@ static struct { for(i=0; i < DIM(test_vectors); i++ ) { do_setkey( &c, test_vectors[i].key, 16 ); - if( !check_decrypt ) { - encrypt_block( &c, buffer, test_vectors[i].plain ); - if( memcmp( buffer, test_vectors[i].cipher, 8 ) ) - g10_log_fatal("idea encryption (%d) failed\n", i); - } - else { - decrypt_block( &c, buffer, test_vectors[i].cipher ); - if( memcmp( buffer, test_vectors[i].plain, 8 ) ) - g10_log_fatal("idea decryption (%d) failed\n", i); - } + encrypt_block( &c, buffer, test_vectors[i].plain ); + if( memcmp( buffer, test_vectors[i].cipher, 8 ) ) + return "IDEA test encryption failed."; + decrypt_block( &c, buffer, test_vectors[i].cipher ); + if( memcmp( buffer, test_vectors[i].plain, 8 ) ) + return "IDEA test decryption failed."; } + + return NULL; } -#endif gcry_cipher_spec_t _gcry_cipher_spec_idea = |