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]
#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);
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)
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
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 :-( */
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,
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;
{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
#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;
printf("Doing ecb\n");
for (i=0; i<NUM_TESTS; i++)
{
- if ((j=des_key_sched(&key_data[i],ks)) != 0)
- {
- printf("Key error %2d:%d\n",i+1,j);
- err=1;
- }
+ des_set_key_unchecked(&key_data[i],ks);
memcpy(in,plain_data[i],8);
memset(out,0,8);
memset(outin,0,8);
printf("Doing ede ecb\n");
for (i=0; i<(NUM_TESTS-1); i++)
{
- if ((j=des_key_sched(&key_data[i],ks)) != 0)
- {
- err=1;
- printf("Key error %2d:%d\n",i+1,j);
- }
- if ((j=des_key_sched(&key_data[i+1],ks2)) != 0)
- {
- printf("Key error %2d:%d\n",i+2,j);
- err=1;
- }
- if ((j=des_key_sched(&key_data[i+2],ks3)) != 0)
- {
- printf("Key error %2d:%d\n",i+3,j);
- err=1;
- }
+ des_set_key_unchecked(&key_data[i],ks);
+ des_set_key_unchecked(&key_data[i+1],ks2);
+ des_set_key_unchecked(&key_data[i+2],ks3);
memcpy(in,plain_data[i],8);
memset(out,0,8);
memset(outin,0,8);
#endif
printf("Doing cbc\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;
#ifndef LIBDES_LIT
printf("Doing desx cbc\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;
#endif
printf("Doing ede cbc\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;
#ifndef LIBDES_LIT
printf("Doing pcbc\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;
printf("done\n");
printf("Doing ofb\n");
- des_key_sched(&ofb_key,ks);
+ des_set_key_checked(&ofb_key,ks);
memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
des_ofb_encrypt(plain,ofb_buf1,64,sizeof(plain)/8,ks,&ofb_tmp);
if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
}
printf("Doing ofb64\n");
- des_key_sched(&ofb_key,ks);
+ des_set_key_checked(&ofb_key,ks);
memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
memset(ofb_buf1,0,sizeof(ofb_buf1));
memset(ofb_buf2,0,sizeof(ofb_buf1));
}
printf("Doing ede_ofb64\n");
- des_key_sched(&ofb_key,ks);
+ des_set_key_checked(&ofb_key,ks);
memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
memset(ofb_buf1,0,sizeof(ofb_buf1));
memset(ofb_buf2,0,sizeof(ofb_buf1));
}
printf("Doing cbc_cksum\n");
- des_key_sched(&cbc_key,ks);
+ des_set_key_checked(&cbc_key,ks);
cs=des_cbc_cksum(cbc_data,&cret,strlen((char *)cbc_data),ks,&cbc_iv);
if (cs != cbc_cksum_ret)
{
des_key_schedule ks;
int i,err=0;
- des_key_sched(&cfb_key,ks);
+ des_set_key_checked(&cfb_key,ks);
memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
des_cfb_encrypt(plain,cfb_buf1,bits,sizeof(plain),ks,&cfb_tmp,
DES_ENCRYPT);
des_key_schedule ks;
int err=0,i,n;
- des_key_sched(&cfb_key,ks);
+ des_set_key_checked(&cfb_key,ks);
memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
n=0;
des_cfb64_encrypt(plain,cfb_buf1,12,ks,&cfb_tmp,&n,DES_ENCRYPT);
des_key_schedule ks;
int err=0,i,n;
- des_key_sched(&cfb_key,ks);
+ des_set_key_checked(&cfb_key,ks);
memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
n=0;
des_ede3_cfb64_encrypt(plain,cfb_buf1,12,ks,ks,ks,&cfb_tmp,&n,
for (; i<8; i++)
key[i]=0;
- des_set_key(&key,ks);
+ des_set_key_unchecked(&key,ks);
fcrypt_body(&(out[0]),ks,Eswap0,Eswap1);
ll=out[0]; l2c(ll,b);
l2c(t,p);
des_set_odd_parity(&data);
- des_set_key(&data,ks);
+ des_set_key_unchecked(&data,ks);
des_cbc_cksum(key,&key,sizeof(key),ks,&data);
des_set_odd_parity(&key);
- des_set_key(&key,ks);
+ des_set_key_unchecked(&key,ks);
des_cbc_cksum(key,&data,sizeof(key),ks,&key);
memcpy(ret,data,sizeof(key));
des_key_schedule ks;
int enc;
- des_set_key(&desp->des_key,ks);
+ des_set_key_unchecked(&desp->des_key,ks);
enc=(desp->des_dir == ENCRYPT)?DES_ENCRYPT:DES_DECRYPT;
if (desp->des_mode == CBC)
#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;
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];
t2=((s>>16L)|(t&0xffff0000L));
*(k++)=ROTATE(t2,26)&0xffffffffL;
}
- return(0);
}
int des_key_sched(const_des_cblock *key, des_key_schedule schedule)
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;
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;
#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;
}
#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);
#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);
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));
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);
}
}
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));
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);
}
}
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,
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));
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);
}
}
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,
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));
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);
}
}
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,
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);
}
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];