From cddfe788fbbc4726fcf9892963df3f3e823eb233 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bodo=20M=C3=B6ller?= Date: Fri, 3 Dec 1999 20:24:21 +0000 Subject: [PATCH] Add functions des_set_key_checked, des_set_key_unchecked. Never use des_set_key (it depends on the global variable des_check_key), but usually des_set_key_unchecked. Only destest.c bothered to look at the return values of des_set_key, but it did not set des_check_key -- if it had done so, most checks would have failed because of wrong parity and because of weak keys. --- CHANGES | 8 ++++++ apps/speed.c | 6 ++--- crypto/des/des.c | 6 ++--- crypto/des/des.h | 5 ++++ crypto/des/des_opts.c | 8 +++--- crypto/des/destest.c | 59 ++++++++++++++++-------------------------- crypto/des/fcrypt.c | 2 +- crypto/des/rand_key.c | 4 +-- crypto/des/rpc_enc.c | 2 +- crypto/des/set_key.c | 35 +++++++++++++++++-------- crypto/des/speed.c | 8 +++--- crypto/des/str2key.c | 14 +++------- crypto/evp/e_cbc_3d.c | 10 +++---- crypto/evp/e_cfb_3d.c | 10 +++---- crypto/evp/e_cfb_d.c | 2 +- crypto/evp/e_ecb_3d.c | 10 +++---- crypto/evp/e_ecb_d.c | 2 +- crypto/evp/e_ofb_3d.c | 10 +++---- crypto/evp/e_ofb_d.c | 2 +- crypto/evp/e_xcbc_d.c | 2 +- crypto/mdc2/mdc2dgst.c | 4 +-- 21 files changed, 106 insertions(+), 103 deletions(-) diff --git a/CHANGES b/CHANGES index 5dcd6d8046..247d65a001 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,14 @@ Changes between 0.9.4 and 0.9.5 [xx XXX 1999] + *) Add variants des_set_key_checked and des_set_key_unchecked of + des_set_key (aka des_key_sched). Global variable des_check_key + decides which of these is called by des_set_key; this way + des_check_key behaves as it always did, but applications and + the library itself, which was buggy for des_check_key == 1, + have a cleaner way to pick the version they need. + [Bodo Moeller] + *) New function PKCS12_newpass() which changes the password of a PKCS12 structure. [Steve Henson] diff --git a/apps/speed.c b/apps/speed.c index 779ee03a19..caf47e2277 100644 --- a/apps/speed.c +++ b/apps/speed.c @@ -590,9 +590,9 @@ int MAIN(int argc, char **argv) #endif #ifndef NO_DES - des_set_key(&key,sch); - des_set_key(&key2,sch2); - des_set_key(&key3,sch3); + des_set_key_unchecked(&key,sch); + des_set_key_unchecked(&key2,sch2); + des_set_key_unchecked(&key3,sch3); #endif #ifndef NO_IDEA idea_set_encrypt_key(key16,&idea_ks); diff --git a/crypto/des/des.c b/crypto/des/des.c index 206e3777a7..5cd337301a 100644 --- a/crypto/des/des.c +++ b/crypto/des/des.c @@ -425,7 +425,7 @@ void doencryption(void) else k2[i-8]=k; } - des_set_key(&k2,ks2); + des_set_key_unchecked(&k2,ks2); memset(k2,0,sizeof(k2)); } else if (longk || flag3) @@ -433,7 +433,7 @@ void doencryption(void) if (flag3) { des_string_to_2keys(key,&kk,&k2); - des_set_key(&k2,ks2); + des_set_key_unchecked(&k2,ks2); memset(k2,0,sizeof(k2)); } else @@ -455,7 +455,7 @@ void doencryption(void) kk[i]=key[i]|0x80; } - des_set_key(&kk,ks); + des_set_key_unchecked(&kk,ks); memset(key,0,sizeof(key)); memset(kk,0,sizeof(kk)); /* woops - A bug that does not showup under unix :-( */ diff --git a/crypto/des/des.h b/crypto/des/des.h index 67f90aaf17..b492002435 100644 --- a/crypto/des/des.h +++ b/crypto/des/des.h @@ -193,8 +193,13 @@ int des_read_2passwords(des_cblock *key1,des_cblock *key2, int des_read_pw_string(char *buf,int length,const char *prompt,int verify); void des_set_odd_parity(des_cblock *key); int des_is_weak_key(const_des_cblock *key); +/* des_set_key (= set_key = des_key_sched = key_sched) calls + * des_set_key_checked if global variable des_check_key is set, + * des_set_key_unchecked otherwise. */ int des_set_key(const_des_cblock *key,des_key_schedule schedule); int des_key_sched(const_des_cblock *key,des_key_schedule schedule); +int des_set_key_checked(const_des_cblock *key,des_key_schedule schedule); +void des_set_key_unchecked(const_des_cblock *key,des_key_schedule schedule); void des_string_to_key(const char *str,des_cblock *key); void des_string_to_2keys(const char *str,des_cblock *key1,des_cblock *key2); void des_cfb64_encrypt(const unsigned char *in,unsigned char *out,long length, diff --git a/crypto/des/des_opts.c b/crypto/des/des_opts.c index 746c456f8f..545075ce49 100644 --- a/crypto/des/des_opts.c +++ b/crypto/des/des_opts.c @@ -438,13 +438,13 @@ int main(int argc, char **argv) fprintf(stderr,"program when this computer is idle.\n"); #endif - des_set_key(&key,sch); - des_set_key(&key2,sch2); - des_set_key(&key3,sch3); + des_set_key_unchecked(&key,sch); + des_set_key_unchecked(&key2,sch2); + des_set_key_unchecked(&key3,sch3); #ifndef SIGALRM fprintf(stderr,"First we calculate the approximate speed ...\n"); - des_set_key(&key,sch); + des_set_key_unchecked(&key,sch); count=10; do { long i; diff --git a/crypto/des/destest.c b/crypto/des/destest.c index 69155eda12..7efd83dc30 100644 --- a/crypto/des/destest.c +++ b/crypto/des/destest.c @@ -234,7 +234,8 @@ static unsigned char cipher_ecb2[NUM_TESTS-1][8]={ {0x08,0xD7,0xB4,0xFB,0x62,0x9D,0x08,0x85}}; static unsigned char cbc_key [8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef}; -static unsigned char cbc2_key[8]={0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87}; +/* static unsigned char cbc2_key[8]={0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87}; */ +static unsigned char cbc2_key[8]={0xf1,0xe0,0xd3,0xc2,0xb5,0xa4,0x97,0x86}; static unsigned char cbc3_key[8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}; static unsigned char cbc_iv [8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}; /* Changed the following text constant to binary so it will work on ebcdic @@ -331,17 +332,17 @@ int main(int argc, char *argv[]) #ifndef NO_DESCBCM printf("Doing cbcm\n"); - if ((j=des_key_sched(&cbc_key,ks)) != 0) + if ((j=des_set_key_checked(&cbc_key,ks)) != 0) { printf("Key error %d\n",j); err=1; } - if ((j=des_key_sched(&cbc2_key,ks2)) != 0) + if ((j=des_set_key_checked(&cbc2_key,ks2)) != 0) { printf("Key error %d\n",j); err=1; } - if ((j=des_key_sched(&cbc3_key,ks3)) != 0) + if ((j=des_set_key_checked(&cbc3_key,ks3)) != 0) { printf("Key error %d\n",j); err=1; @@ -385,11 +386,7 @@ int main(int argc, char *argv[]) printf("Doing ecb\n"); for (i=0; ides_key,ks); + des_set_key_unchecked(&desp->des_key,ks); enc=(desp->des_dir == ENCRYPT)?DES_ENCRYPT:DES_DECRYPT; if (desp->des_mode == CBC) diff --git a/crypto/des/set_key.c b/crypto/des/set_key.c index 52553a4c16..eb64bd5292 100644 --- a/crypto/des/set_key.c +++ b/crypto/des/set_key.c @@ -145,11 +145,34 @@ int des_is_weak_key(const_des_cblock *key) #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\ (a)=(a)^(t)^(t>>(16-(n)))) +int des_set_key(const_des_cblock *key, des_key_schedule schedule) + { + if (des_check_key) + { + return des_set_key_checked(key, schedule); + } + else + { + des_set_key_unchecked(key, schedule); + return 0; + } + } + /* return 0 if key parity is odd (correct), * return -1 if key parity error, * return -2 if illegal weak key. */ -int des_set_key(const_des_cblock *key, des_key_schedule schedule) +int des_set_key_checked(const_des_cblock *key, des_key_schedule schedule) + { + if (!check_parity(key)) + return(-1); + if (des_is_weak_key(key)) + return(-2); + des_set_key_unchecked(key, schedule); + return 0; + } + +void des_set_key_unchecked(const_des_cblock *key, des_key_schedule schedule) { static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0}; register DES_LONG c,d,t,s,t2; @@ -157,15 +180,6 @@ int des_set_key(const_des_cblock *key, des_key_schedule schedule) register DES_LONG *k; register int i; - if (des_check_key) - { - if (!check_parity(key)) - return(-1); - - if (des_is_weak_key(key)) - return(-2); - } - k = &schedule->ks.deslong[0]; in = &(*key)[0]; @@ -225,7 +239,6 @@ int des_set_key(const_des_cblock *key, des_key_schedule schedule) t2=((s>>16L)|(t&0xffff0000L)); *(k++)=ROTATE(t2,26)&0xffffffffL; } - return(0); } int des_key_sched(const_des_cblock *key, des_key_schedule schedule) diff --git a/crypto/des/speed.c b/crypto/des/speed.c index da41abcb03..652cf5b03c 100644 --- a/crypto/des/speed.c +++ b/crypto/des/speed.c @@ -190,12 +190,12 @@ int main(int argc, char **argv) printf("program when this computer is idle.\n"); #endif - des_set_key(&key2,sch2); - des_set_key(&key3,sch3); + des_set_key_unchecked(&key2,sch2); + des_set_key_unchecked(&key3,sch3); #ifndef SIGALRM printf("First we calculate the approximate speed ...\n"); - des_set_key(&key,sch); + des_set_key_unchecked(&key,sch); count=10; do { long i; @@ -225,7 +225,7 @@ int main(int argc, char **argv) Time_F(START); for (count=0,run=1; COND(ca); count++) - des_set_key(&key,sch); + des_set_key_unchecked(&key,sch); d=Time_F(STOP); printf("%ld set_key's in %.2f seconds\n",count,d); a=((double)COUNT(ca))/d; diff --git a/crypto/des/str2key.c b/crypto/des/str2key.c index 24841452f1..5522824952 100644 --- a/crypto/des/str2key.c +++ b/crypto/des/str2key.c @@ -58,8 +58,6 @@ #include "des_locl.h" -OPENSSL_EXTERN int des_check_key; - void des_string_to_key(const char *str, des_cblock *key) { des_key_schedule ks; @@ -88,10 +86,7 @@ void des_string_to_key(const char *str, des_cblock *key) } #endif des_set_odd_parity(key); - i=des_check_key; - des_check_key=0; - des_set_key(key,ks); - des_check_key=i; + des_set_key_unchecked(key,ks); des_cbc_cksum((unsigned char*)str,key,length,ks,key); memset(ks,0,sizeof(ks)); des_set_odd_parity(key); @@ -150,13 +145,10 @@ void des_string_to_2keys(const char *str, des_cblock *key1, des_cblock *key2) #endif des_set_odd_parity(key1); des_set_odd_parity(key2); - i=des_check_key; - des_check_key=0; - des_set_key(key1,ks); + des_set_key_unchecked(key1,ks); des_cbc_cksum((unsigned char*)str,key1,length,ks,key1); - des_set_key(key2,ks); + des_set_key_unchecked(key2,ks); des_cbc_cksum((unsigned char*)str,key2,length,ks,key2); - des_check_key=i; memset(ks,0,sizeof(ks)); des_set_odd_parity(key1); des_set_odd_parity(key2); diff --git a/crypto/evp/e_cbc_3d.c b/crypto/evp/e_cbc_3d.c index 02ccc6dc90..5d16b865c5 100644 --- a/crypto/evp/e_cbc_3d.c +++ b/crypto/evp/e_cbc_3d.c @@ -115,8 +115,8 @@ static void des_cbc_ede_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, if (deskey != NULL) { - des_set_key(&deskey[0],ctx->c.des_ede.ks1); - des_set_key(&deskey[1],ctx->c.des_ede.ks2); + des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); + des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); memcpy( (char *)ctx->c.des_ede.ks3, (char *)ctx->c.des_ede.ks1, sizeof(ctx->c.des_ede.ks1)); @@ -134,9 +134,9 @@ static void des_cbc_ede3_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, if (deskey != NULL) { - des_set_key(&deskey[0],ctx->c.des_ede.ks1); - des_set_key(&deskey[1],ctx->c.des_ede.ks2); - des_set_key(&deskey[2],ctx->c.des_ede.ks3); + des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); + des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); + des_set_key_unchecked(&deskey[2],ctx->c.des_ede.ks3); } } diff --git a/crypto/evp/e_cfb_3d.c b/crypto/evp/e_cfb_3d.c index bd32b072e2..b364bd4e31 100644 --- a/crypto/evp/e_cfb_3d.c +++ b/crypto/evp/e_cfb_3d.c @@ -116,8 +116,8 @@ static void des_ede_cfb_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, memcpy(&(ctx->iv[0]),&(ctx->oiv[0]),8); if (deskey != NULL) { - des_set_key(&deskey[0],ctx->c.des_ede.ks1); - des_set_key(&deskey[1],ctx->c.des_ede.ks2); + des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); + des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); memcpy( (char *)ctx->c.des_ede.ks3, (char *)ctx->c.des_ede.ks1, sizeof(ctx->c.des_ede.ks1)); @@ -136,9 +136,9 @@ static void des_ede3_cfb_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, memcpy(&(ctx->iv[0]),&(ctx->oiv[0]),8); if (deskey != NULL) { - des_set_key(&deskey[0],ctx->c.des_ede.ks1); - des_set_key(&deskey[1],ctx->c.des_ede.ks2); - des_set_key(&deskey[2],ctx->c.des_ede.ks3); + des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); + des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); + des_set_key_unchecked(&deskey[2],ctx->c.des_ede.ks3); } } diff --git a/crypto/evp/e_cfb_d.c b/crypto/evp/e_cfb_d.c index 6bdf20b646..9e1714bd15 100644 --- a/crypto/evp/e_cfb_d.c +++ b/crypto/evp/e_cfb_d.c @@ -95,7 +95,7 @@ static void des_cfb_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, memcpy(&(ctx->oiv[0]),iv,8); memcpy(&(ctx->iv[0]),&(ctx->oiv[0]),8); if (deskey != NULL) - des_set_key(deskey,ctx->c.des_ks); + des_set_key_unchecked(deskey,ctx->c.des_ks); } static void des_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, diff --git a/crypto/evp/e_ecb_3d.c b/crypto/evp/e_ecb_3d.c index 354a8b79a7..806e971d36 100644 --- a/crypto/evp/e_ecb_3d.c +++ b/crypto/evp/e_ecb_3d.c @@ -110,8 +110,8 @@ static void des_ede_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, if (deskey != NULL) { - des_set_key(&deskey[0],ctx->c.des_ede.ks1); - des_set_key(&deskey[1],ctx->c.des_ede.ks2); + des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); + des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); memcpy( (char *)ctx->c.des_ede.ks3, (char *)ctx->c.des_ede.ks1, sizeof(ctx->c.des_ede.ks1)); @@ -125,9 +125,9 @@ static void des_ede3_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, if (deskey != NULL) { - des_set_key(&deskey[0],ctx->c.des_ede.ks1); - des_set_key(&deskey[1],ctx->c.des_ede.ks2); - des_set_key(&deskey[2],ctx->c.des_ede.ks3); + des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); + des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); + des_set_key_unchecked(&deskey[2],ctx->c.des_ede.ks3); } } diff --git a/crypto/evp/e_ecb_d.c b/crypto/evp/e_ecb_d.c index 5fb4e64b1c..c11bef55ef 100644 --- a/crypto/evp/e_ecb_d.c +++ b/crypto/evp/e_ecb_d.c @@ -90,7 +90,7 @@ static void des_ecb_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, des_cblock *deskey = (des_cblock *)key; if (deskey != NULL) - des_set_key(deskey,ctx->c.des_ks); + des_set_key_unchecked(deskey,ctx->c.des_ks); } static void des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, diff --git a/crypto/evp/e_ofb_3d.c b/crypto/evp/e_ofb_3d.c index 5233567c0c..d1a33e2ecd 100644 --- a/crypto/evp/e_ofb_3d.c +++ b/crypto/evp/e_ofb_3d.c @@ -116,8 +116,8 @@ static void des_ede_ofb_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, memcpy(&(ctx->iv[0]),&(ctx->oiv[0]),8); if (deskey != NULL) { - des_set_key(&deskey[0],ctx->c.des_ede.ks1); - des_set_key(&deskey[1],ctx->c.des_ede.ks2); + des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); + des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); memcpy( (char *)ctx->c.des_ede.ks3, (char *)ctx->c.des_ede.ks1, sizeof(ctx->c.des_ede.ks1)); @@ -136,9 +136,9 @@ static void des_ede3_ofb_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, memcpy(&(ctx->iv[0]),&(ctx->oiv[0]),8); if (deskey != NULL) { - des_set_key(&deskey[0],ctx->c.des_ede.ks1); - des_set_key(&deskey[1],ctx->c.des_ede.ks2); - des_set_key(&deskey[2],ctx->c.des_ede.ks3); + des_set_key_unchecked(&deskey[0],ctx->c.des_ede.ks1); + des_set_key_unchecked(&deskey[1],ctx->c.des_ede.ks2); + des_set_key_unchecked(&deskey[2],ctx->c.des_ede.ks3); } } diff --git a/crypto/evp/e_ofb_d.c b/crypto/evp/e_ofb_d.c index 398b3a002e..d51ce230f4 100644 --- a/crypto/evp/e_ofb_d.c +++ b/crypto/evp/e_ofb_d.c @@ -95,7 +95,7 @@ static void des_ofb_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, memcpy(&(ctx->oiv[0]),iv,8); memcpy(&(ctx->iv[0]),&(ctx->oiv[0]),8); if (deskey != NULL) - des_set_key(deskey,ctx->c.des_ks); + des_set_key_unchecked(deskey,ctx->c.des_ks); } static void des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, diff --git a/crypto/evp/e_xcbc_d.c b/crypto/evp/e_xcbc_d.c index 3a6628a75c..7568fad4ff 100644 --- a/crypto/evp/e_xcbc_d.c +++ b/crypto/evp/e_xcbc_d.c @@ -94,7 +94,7 @@ static void desx_cbc_init_key(EVP_CIPHER_CTX *ctx, unsigned char *key, memcpy(&(ctx->iv[0]),&(ctx->oiv[0]),8); if (deskey != NULL) { - des_set_key(deskey,ctx->c.desx_cbc.ks); + des_set_key_unchecked(deskey,ctx->c.desx_cbc.ks); memcpy(&(ctx->c.desx_cbc.inw[0]),&(key[8]),8); memcpy(&(ctx->c.desx_cbc.outw[0]),&(key[16]),8); } diff --git a/crypto/mdc2/mdc2dgst.c b/crypto/mdc2/mdc2dgst.c index 4fc39ed86c..aed715c69d 100644 --- a/crypto/mdc2/mdc2dgst.c +++ b/crypto/mdc2/mdc2dgst.c @@ -135,11 +135,11 @@ static void mdc2_body(MDC2_CTX *c, unsigned char *in, unsigned int len) c->hh[0]=(c->hh[0]&0x9f)|0x20; des_set_odd_parity(&c->h); - des_set_key(&c->h,k); + des_set_key_unchecked(&c->h,k); des_encrypt(d,k,1); des_set_odd_parity(&c->hh); - des_set_key(&c->hh,k); + des_set_key_unchecked(&c->hh,k); des_encrypt(dd,k,1); ttin0=tin0^dd[0]; -- 2.25.1