From 7b66b8025a8487ab5e2c13263c2add0e237f414a Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Sun, 17 Jan 1999 10:06:02 +0000 Subject: See ChangeLog: Sun Jan 17 11:04:33 CET 1999 Werner Koch --- cipher/twofish.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'cipher/twofish.c') diff --git a/cipher/twofish.c b/cipher/twofish.c index d93c145e..d52377f7 100644 --- a/cipher/twofish.c +++ b/cipher/twofish.c @@ -19,14 +19,14 @@ #include #include #include -#include /* for assert() */ #include /* for memcmp() */ #include "types.h" /* for byte and u32 typedefs */ -#include "util.h" /* for log_fatal() */ +#include "errors.h" + /* Prototype for the self-test function. */ -static void selftest(void); +static const char *selftest(void); /* Macros used by the info function. */ #define FNCCAST_SETKEY(f) ((int(*)(void*, byte*, unsigned))(f)) @@ -456,17 +456,23 @@ twofish_setkey (TWOFISH_context *ctx, const byte *key, const unsigned keylen) /* Temporary for CALC_S. */ byte tmp; - /* Flag for self-test. */ + /* Flags for self-test. */ static int initialized = 0; + static const char *selftest_failed=0; /* Check key length. */ - assert (keylen == 16); + if( keylen != 16 ) + return G10ERR_WRONG_KEYLEN; /* Do self-test if necessary. */ if (!initialized) { initialized = 1; - selftest (); + selftest_failed = selftest (); + if( selftest_failed ) + fprintf(stderr, "%s\n", selftest_failed ); } + if( selftest_failed ) + return G10ERR_SELFTEST_FAILED; /* Compute the S vector. The magic numbers are the entries of the RS * matrix, preprocessed through poly_to_exp. The numbers in the comments @@ -709,7 +715,7 @@ twofish_decrypt (const TWOFISH_context *ctx, byte *out, const byte *in) /* Test a single encryption and decryption, as a sanity check. */ -static void +static const char* selftest (void) { TWOFISH_context ctx; /* Expanded key. */ @@ -736,10 +742,11 @@ selftest (void) twofish_setkey (&ctx, key, sizeof(key)); twofish_encrypt (&ctx, scratch, plaintext); if (memcmp (scratch, ciphertext, sizeof (ciphertext))) - log_fatal ("Twofish test encryption failed\n"); + return "Twofish test encryption failed."; twofish_decrypt (&ctx, scratch, scratch); if (memcmp (scratch, plaintext, sizeof (plaintext))) - log_fatal ("Twofish test decryption failed\n"); + return "Twofish test decryption failed."; + return NULL; } /* More complete test program. This does a thousand encryptions and -- cgit v1.2.1