/* crypto/des/set_key.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
#include "podd.h"
#include "sk.h"
-#ifndef NOPROTO
-static int check_parity(des_cblock (*key));
-#else
-static int check_parity();
-#endif
+static int check_parity(const_des_cblock *key);
+OPENSSL_GLOBAL int des_check_key=0;
-int des_check_key=0;
-
-void des_set_odd_parity(key)
-des_cblock (*key);
+void des_set_odd_parity(des_cblock *key)
{
int i;
(*key)[i]=odd_parity[(*key)[i]];
}
-static int check_parity(key)
-des_cblock (*key);
+static int check_parity(const_des_cblock *key)
{
int i;
/* weak keys */
{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
- {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
- {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
+ {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
+ {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
/* semi-weak keys */
{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
{0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
{0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
-int des_is_weak_key(key)
-des_cblock (*key);
+int des_is_weak_key(const_des_cblock *key)
{
int i;
for (i=0; i<NUM_WEAK_KEY; i++)
- /* Added == 0 to comparision, I obviously don't run
+ /* Added == 0 to comparison, I obviously don't run
* this section very often :-(, thanks to
* engineering@MorningStar.Com for the fix
* eay 93/06/29
#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(key, schedule)
-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 unsigned char *in;
+ register const unsigned char *in;
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=(DES_LONG *)schedule;
- in=(unsigned char *)key;
+ k = &schedule->ks.deslong[0];
+ in = &(*key)[0];
c2l(in,c);
c2l(in,d);
/* could be a few less shifts but I am to lazy at this
* point in time to investigate */
s= des_skb[0][ (c )&0x3f ]|
- des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
- des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
- des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
+ des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|
+ des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
+ des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
((c>>22L)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
t2=((s>>16L)|(t&0xffff0000L));
*(k++)=ROTATE(t2,26)&0xffffffffL;
}
- return(0);
}
-int des_key_sched(key, schedule)
-des_cblock (*key);
-des_key_schedule schedule;
+int des_key_sched(const_des_cblock *key, des_key_schedule schedule)
{
return(des_set_key(key,schedule));
}