Fix strange formatting by indent
[openssl.git] / crypto / des / set_key.c
index 0dc79c946110caacd3f940c96c8fe337bbe16097..7aa2c80482b9ae77505cb6bb2e144cb6bf53d8fa 100644 (file)
  * [including the GNU Public Licence.]
  */
 
-/* set_key.c v 1.4 eay 24/9/91
+/*-
+ * set_key.c v 1.4 eay 24/9/91
  * 1.4 Speed up by 400% :-)
  * 1.3 added register declarations.
  * 1.2 unrolled make_key_sched a bit more
  * 1.1 added norm_expand_bits
  * 1.0 First working version
  */
+#include <openssl/crypto.h>
 #include "des_locl.h"
 
-OPENSSL_IMPLEMENT_GLOBAL(int,des_check_key);   /* defaults to false */
+OPENSSL_IMPLEMENT_GLOBAL(int,DES_check_key,0)  /* defaults to false */
 
 static const unsigned char odd_parity[256]={
   1,  1,  2,  2,  4,  4,  7,  7,  8,  8, 11, 11, 13, 13, 14, 14,
@@ -85,17 +87,17 @@ static const unsigned char odd_parity[256]={
 224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
 241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254};
 
-void des_set_odd_parity(des_cblock *key)
+void DES_set_odd_parity(DES_cblock *key)
        {
-       int i;
+       unsigned int i;
 
        for (i=0; i<DES_KEY_SZ; i++)
                (*key)[i]=odd_parity[(*key)[i]];
        }
 
-int des_check_key_parity(const_des_cblock *key)
+int DES_check_key_parity(const_DES_cblock *key)
        {
-       int i;
+       unsigned int i;
 
        for (i=0; i<DES_KEY_SZ; i++)
                {
@@ -105,7 +107,8 @@ int des_check_key_parity(const_des_cblock *key)
        return(1);
        }
 
-/* Weak and semi week keys as take from
+/*-
+ * Weak and semi week keys as take from
  * %A D.W. Davies
  * %A W.L. Price
  * %T Security for Computer Networks
@@ -115,7 +118,7 @@ int des_check_key_parity(const_des_cblock *key)
  * (and actual cblock values).
  */
 #define NUM_WEAK_KEY   16
-static des_cblock weak_keys[NUM_WEAK_KEY]={
+static const DES_cblock weak_keys[NUM_WEAK_KEY]={
        /* weak keys */
        {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
        {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
@@ -135,7 +138,7 @@ static des_cblock weak_keys[NUM_WEAK_KEY]={
        {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
        {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
 
-int des_is_weak_key(const_des_cblock *key)
+int DES_is_weak_key(const_DES_cblock *key)
        {
        int i;
 
@@ -146,11 +149,12 @@ int des_is_weak_key(const_des_cblock *key)
                 * eay 93/06/29
                 * Another problem, I was comparing only the first 4
                 * bytes, 97/03/18 */
-               if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
+               if (memcmp(weak_keys[i],key,sizeof(DES_cblock)) == 0) return(1);
        return(0);
        }
 
-/* NOW DEFINED IN des_local.h
+/*-
+ * NOW DEFINED IN des_local.h
  * See ecb_encrypt.c for a pseudo description of these macros. 
  * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
  *     (b)^=(t),\
@@ -179,7 +183,8 @@ static const DES_LONG des_skb[8][64]={
        0x00090020L,0x00090030L,0x20090020L,0x20090030L,
        0x00080820L,0x00080830L,0x20080820L,0x20080830L,
        0x00090820L,0x00090830L,0x20090820L,0x20090830L,
-       },{
+       },
+       {
        /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */
        0x00000000L,0x02000000L,0x00002000L,0x02002000L,
        0x00200000L,0x02200000L,0x00202000L,0x02202000L,
@@ -197,7 +202,8 @@ static const DES_LONG des_skb[8][64]={
        0x10200400L,0x12200400L,0x10202400L,0x12202400L,
        0x10000404L,0x12000404L,0x10002404L,0x12002404L,
        0x10200404L,0x12200404L,0x10202404L,0x12202404L,
-       },{
+       },
+       {
        /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */
        0x00000000L,0x00000001L,0x00040000L,0x00040001L,
        0x01000000L,0x01000001L,0x01040000L,0x01040001L,
@@ -215,7 +221,8 @@ static const DES_LONG des_skb[8][64]={
        0x09000200L,0x09000201L,0x09040200L,0x09040201L,
        0x08000202L,0x08000203L,0x08040202L,0x08040203L,
        0x09000202L,0x09000203L,0x09040202L,0x09040203L,
-       },{
+       },
+       {
        /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */
        0x00000000L,0x00100000L,0x00000100L,0x00100100L,
        0x00000008L,0x00100008L,0x00000108L,0x00100108L,
@@ -233,7 +240,8 @@ static const DES_LONG des_skb[8][64]={
        0x04020008L,0x04120008L,0x04020108L,0x04120108L,
        0x04021000L,0x04121000L,0x04021100L,0x04121100L,
        0x04021008L,0x04121008L,0x04021108L,0x04121108L,
-       },{
+       },
+       {
        /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
        0x00000000L,0x10000000L,0x00010000L,0x10010000L,
        0x00000004L,0x10000004L,0x00010004L,0x10010004L,
@@ -251,7 +259,8 @@ static const DES_LONG des_skb[8][64]={
        0x00101004L,0x10101004L,0x00111004L,0x10111004L,
        0x20101000L,0x30101000L,0x20111000L,0x30111000L,
        0x20101004L,0x30101004L,0x20111004L,0x30111004L,
-       },{
+       },
+       {
        /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */
        0x00000000L,0x08000000L,0x00000008L,0x08000008L,
        0x00000400L,0x08000400L,0x00000408L,0x08000408L,
@@ -269,7 +278,8 @@ static const DES_LONG des_skb[8][64]={
        0x02000401L,0x0A000401L,0x02000409L,0x0A000409L,
        0x02020001L,0x0A020001L,0x02020009L,0x0A020009L,
        0x02020401L,0x0A020401L,0x02020409L,0x0A020409L,
-       },{
+       },
+       {
        /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */
        0x00000000L,0x00000100L,0x00080000L,0x00080100L,
        0x01000000L,0x01000100L,0x01080000L,0x01080100L,
@@ -287,7 +297,8 @@ static const DES_LONG des_skb[8][64]={
        0x01200200L,0x01200300L,0x01280200L,0x01280300L,
        0x00200210L,0x00200310L,0x00280210L,0x00280310L,
        0x01200210L,0x01200310L,0x01280210L,0x01280310L,
-       },{
+       },
+       {
        /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
        0x00000000L,0x04000000L,0x00040000L,0x04040000L,
        0x00000002L,0x04000002L,0x00040002L,0x04040002L,
@@ -305,44 +316,50 @@ static const DES_LONG des_skb[8][64]={
        0x00000822L,0x04000822L,0x00040822L,0x04040822L,
        0x00002820L,0x04002820L,0x00042820L,0x04042820L,
        0x00002822L,0x04002822L,0x00042822L,0x04042822L,
-       }};
+       }
+};
 
-int des_set_key(const_des_cblock *key, des_key_schedule schedule)
+int DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule)
        {
-       if (des_check_key)
+       if (DES_check_key)
                {
-               return des_set_key_checked(key, schedule);
+               return DES_set_key_checked(key, schedule);
                }
        else
                {
-               des_set_key_unchecked(key, schedule);
+               DES_set_key_unchecked(key, schedule);
                return 0;
                }
        }
 
-/* return 0 if key parity is odd (correct),
+/*-
+ * return 0 if key parity is odd (correct),
  * return -1 if key parity error,
  * return -2 if illegal weak key.
  */
-int des_set_key_checked(const_des_cblock *key, des_key_schedule schedule)
+int DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule)
        {
-       if (!des_check_key_parity(key))
+       if (!DES_check_key_parity(key))
                return(-1);
-       if (des_is_weak_key(key))
+       if (DES_is_weak_key(key))
                return(-2);
-       des_set_key_unchecked(key, schedule);
+       DES_set_key_unchecked(key, schedule);
        return 0;
        }
 
-void des_set_key_unchecked(const_des_cblock *key, des_key_schedule schedule)
+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};
+       static const 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 const unsigned char *in;
        register DES_LONG *k;
        register int i;
 
-       k = &schedule->ks.deslong[0];
+#ifdef OPENBSD_DEV_CRYPTO
+       memcpy(schedule->key,key,sizeof schedule->key);
+       schedule->session=NULL;
+#endif
+       k = &schedule->ks->deslong[0];
        in = &(*key)[0];
 
        c2l(in,c);
@@ -390,11 +407,11 @@ void des_set_key_unchecked(const_des_cblock *key, des_key_schedule schedule)
                }
        }
 
-int des_key_sched(const_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));
+       return(DES_set_key(key,schedule));
        }
-/*
+/*-
 #undef des_fixup_key_parity
 void des_fixup_key_parity(des_cblock *key)
        {