summaryrefslogtreecommitdiff
path: root/cipher
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>1999-01-17 10:06:02 +0000
committerWerner Koch <wk@gnupg.org>1999-01-17 10:06:02 +0000
commit7b66b8025a8487ab5e2c13263c2add0e237f414a (patch)
tree75ce898bf4235b38c01f1fcb06969f80db7fa5e4 /cipher
parentb725cf8008d74702b171862bea8052e7168561b6 (diff)
downloadlibgcrypt-7b66b8025a8487ab5e2c13263c2add0e237f414a.tar.gz
See ChangeLog: Sun Jan 17 11:04:33 CET 1999 Werner Koch
Diffstat (limited to 'cipher')
-rw-r--r--cipher/ChangeLog10
-rw-r--r--cipher/blowfish.c22
-rw-r--r--cipher/cast5.c23
-rw-r--r--cipher/des.c52
-rw-r--r--cipher/twofish.c25
5 files changed, 90 insertions, 42 deletions
diff --git a/cipher/ChangeLog b/cipher/ChangeLog
index a17ed34b..c46ca5d8 100644
--- a/cipher/ChangeLog
+++ b/cipher/ChangeLog
@@ -1,3 +1,13 @@
+Sun Jan 17 11:04:33 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
+
+ * des.c (is_weak_key): Replace system memcmp due to bugs
+ in SunOS's memcmp.
+ (des_get_info): Return error on failed selftest.
+ * twofish.c (twofish_setkey): Return error on failed selftest or
+ invalid keylength.
+ * cast5.c (cast_setkey): Ditto.
+ * blowfish.c (bf_setkey): Return error on failed selftest.
+
Tue Jan 12 11:17:18 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
* random.c (random_is_faked): New.
diff --git a/cipher/blowfish.c b/cipher/blowfish.c
index fdc4c4bf..3bbc0383 100644
--- a/cipher/blowfish.c
+++ b/cipher/blowfish.c
@@ -34,10 +34,12 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
-#include "util.h"
#include "types.h"
+#include "errors.h"
#include "blowfish.h"
+
+
#define CIPHER_ALGO_BLOWFISH 4 /* blowfish 128 bit key */
#define CIPHER_ALGO_BLOWFISH160 42 /* blowfish 160 bit key (not in OpenPGP)*/
@@ -451,7 +453,7 @@ decrypt_block( BLOWFISH_context *bc, byte *outbuf, byte *inbuf )
}
-static void
+static const char*
selftest()
{
BLOWFISH_context c;
@@ -464,18 +466,19 @@ selftest()
bf_setkey( &c, "abcdefghijklmnopqrstuvwxyz", 26 );
encrypt_block( &c, buffer, plain );
if( memcmp( buffer, "\x32\x4E\xD0\xFE\xF4\x13\xA2\x03", 8 ) )
- log_error("wrong blowfish encryption\n");
+ return "Blowfish selftest failed (1).";
decrypt_block( &c, buffer, buffer );
if( memcmp( buffer, plain, 8 ) )
- log_bug("blowfish failed\n");
+ return "Blowfish selftest failed (2).";
bf_setkey( &c, key3, 8 );
encrypt_block( &c, buffer, plain3 );
if( memcmp( buffer, cipher3, 8 ) )
- log_error("wrong blowfish encryption (3)\n");
+ return "Blowfish selftest failed (3).";
decrypt_block( &c, buffer, buffer );
if( memcmp( buffer, plain3, 8 ) )
- log_bug("blowfish failed (3)\n");
+ return "Blowfish selftest failed (4).";
+ return NULL;
}
@@ -486,11 +489,16 @@ bf_setkey( BLOWFISH_context *c, byte *key, unsigned keylen )
int i, j;
u32 data, datal, datar;
static int initialized;
+ static const char *selftest_failed;
if( !initialized ) {
initialized = 1;
- selftest();
+ selftest_failed = selftest();
+ if( selftest_failed )
+ fprintf(stderr,"%s\n", selftest_failed );
}
+ if( selftest_failed )
+ return G10ERR_SELFTEST_FAILED;
for(i=0; i < BLOWFISH_ROUNDS+2; i++ )
c->p[i] = ps[i];
diff --git a/cipher/cast5.c b/cipher/cast5.c
index 279838fa..6f131ca2 100644
--- a/cipher/cast5.c
+++ b/cipher/cast5.c
@@ -39,9 +39,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <assert.h>
-#include "util.h"
#include "types.h"
+#include "errors.h"
#include "cast5.h"
@@ -455,7 +454,7 @@ decrypt_block( CAST5_context *c, byte *outbuf, byte *inbuf )
-static void
+static const char*
selftest()
{
CAST5_context c;
@@ -468,10 +467,10 @@ selftest()
cast_setkey( &c, key, 16 );
encrypt_block( &c, buffer, plain );
if( memcmp( buffer, cipher, 8 ) )
- log_error("wrong cast5-128 encryption\n");
+ return "1";
decrypt_block( &c, buffer, buffer );
if( memcmp( buffer, plain, 8 ) )
- log_bug("cast5-128 failed\n");
+ return "2";
#if 0 /* full maintenance test */
{
@@ -494,10 +493,11 @@ selftest()
encrypt_block( &c, b0+8, b0+8 );
}
if( memcmp( a0, a1, 16 ) || memcmp( b0, b1, 16 ) )
- log_bug("cast5-128 maintenance test failed\n");
+ return "3";
}
#endif
+ return NULL;
}
@@ -553,6 +553,7 @@ static int
cast_setkey( CAST5_context *c, byte *key, unsigned keylen )
{
static int initialized;
+ static const char* selftest_failed;
int i;
u32 x[4];
u32 z[4];
@@ -560,10 +561,16 @@ cast_setkey( CAST5_context *c, byte *key, unsigned keylen )
if( !initialized ) {
initialized = 1;
- selftest();
+ selftest_failed = selftest();
+ if( selftest_failed )
+ fprintf(stderr,"CAST5 selftest failed (%s).\n", selftest_failed );
}
+ if( selftest_failed )
+ return G10ERR_SELFTEST_FAILED;
+
+ if( keylen != 16 )
+ return G10ERR_WRONG_KEYLEN;
- assert(keylen==16);
x[0] = key[0] << 24 | key[1] << 16 | key[2] << 8 | key[3];
x[1] = key[4] << 24 | key[5] << 16 | key[6] << 8 | key[7];
x[2] = key[8] << 24 | key[9] << 16 | key[10] << 8 | key[11];
diff --git a/cipher/des.c b/cipher/des.c
index 41c76889..088f5e14 100644
--- a/cipher/des.c
+++ b/cipher/des.c
@@ -113,12 +113,30 @@
#include <config.h>
+#include <stdio.h>
#include <string.h> /* memcpy, memcmp */
-#include <assert.h>
#include "types.h" /* for byte and u32 typedefs */
-#include "util.h" /* for log_fatal() */
+#include "errors.h"
#include "des.h"
+#if defined(__GNUC__) && defined(__GNU_LIBRARY__)
+#define working_memcmp memcmp
+#else
+/*
+ * According to the SunOS man page, memcmp returns indeterminate sign
+ * depending on whether characters are signed or not.
+ */
+int
+working_memcmp( const char *a, const char *b, size_t n )
+{
+ for( ; n; n--, a++, b++ )
+ if( *a != *b )
+ return (int)(*(byte*)a) - (int)(*(byte*)b);
+ return 0;
+}
+#endif
+
+
/* Some defines/checks to support standalone modules */
@@ -128,19 +146,6 @@
#error CIPHER_ALGO_3DES is defined to a wrong value.
#endif
-#ifndef G10ERR_WEAK_KEY
- #define G10ERR_WEAK_KEY 43
-#elif G10ERR_WEAK_KEY != 43
- #error G10ERR_WEAK_KEY is defined to a wrong value.
-#endif
-
-#ifndef G10ERR_WRONG_KEYLEN
- #define G10ERR_WRONG_KEYLEN 44
-#elif G10ERR_WRONG_KEYLEN != 44
- #error G10ERR_WRONG_KEYLEN is defined to a wrong value.
-#endif
-
-
/* Macros used by the info function. */
#define FNCCAST_SETKEY(f) ((int(*)(void*, byte*, unsigned))(f))
@@ -167,6 +172,7 @@ typedef struct _tripledes_ctx
}
tripledes_ctx[1];
+static const char *selftest_failed;
static void des_key_schedule (const byte *, u32 *);
static int des_setkey (struct _des_ctx *, const byte *);
@@ -542,6 +548,9 @@ des_setkey (struct _des_ctx *ctx, const byte * key)
{
int i;
+ if( selftest_failed )
+ return G10ERR_SELFTEST_FAILED;
+
des_key_schedule (key, ctx->encrypt_subkeys);
for(i=0; i<32; i+=2)
@@ -706,6 +715,8 @@ tripledes_ecb_crypt (struct _tripledes_ctx *ctx, const byte * from, byte * to, i
+
+
/*
* Check whether the 8 byte key is weak.
* Dose not check the parity bits of the key but simple ignore them.
@@ -727,7 +738,7 @@ is_weak_key ( const byte *key )
{
middle = (left + right) / 2;
- if ( !(cmp_result=memcmp(work, weak_keys[middle], 8)) )
+ if ( !(cmp_result=working_memcmp(work, weak_keys[middle], 8)) )
return -1;
if ( cmp_result > 0 )
@@ -836,6 +847,8 @@ selftest (void)
static int
do_tripledes_setkey ( struct _tripledes_ctx *ctx, byte *key, unsigned keylen )
{
+ if( selftest_failed )
+ return G10ERR_SELFTEST_FAILED;
if( keylen != 24 )
return G10ERR_WRONG_KEYLEN;
@@ -879,9 +892,12 @@ des_get_info( int algo, size_t *keylen,
if( !did_selftest ) {
const char *s = selftest();
- if( s )
- log_fatal("selftest failed: %s\n", s );
did_selftest = 1;
+ if( s ) {
+ fprintf(stderr,"%s\n", s );
+ selftest_failed = s;
+ return NULL;
+ }
}
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 <config.h>
#include <stdio.h>
#include <stdlib.h>
-#include <assert.h> /* for assert() */
#include <string.h> /* 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