Eliminate clone-and-hack of DES key expansion and parity setting.
authormdw <mdw>
Sun, 17 Oct 2004 15:00:28 +0000 (15:00 +0000)
committermdw <mdw>
Sun, 17 Oct 2004 15:00:28 +0000 (15:00 +0000)
des.c
des.h
desx.c

diff --git a/des.c b/des.c
index 822a12f..a056c11 100644 (file)
--- a/des.c
+++ b/des.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: des.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
+ * $Id$
  *
  * The Data Encryption Standard
  *
@@ -102,6 +102,43 @@ static void permute(const char *p, uint32 a, uint32 b, uint32 *d)
   d[1] = y;
 }
 
+/* --- @des_expand@ --- *
+ *
+ * Arguments:  @const octet *k@ = pointer to key material
+ *             @size_t n@ = number of octets of key material (7 or 8)
+ *             @uint32 *xx, *yy@ = where to put the results
+ *
+ * Returns:    ---
+ *
+ * Use:                Extracts 64 bits of key material from the given buffer,
+ *             possibly expanding it from 56 to 64 bits on the way.
+ *             Parity is set correctly if the key is expanded.
+ */
+
+void des_expand(const octet *k, size_t n, uint32 *xx, uint32 *yy)
+{
+  uint32 x, y, z;
+
+  if (n == 8) {
+    x = LOAD32(k + 0);
+    y = LOAD32(k + 4);
+  } else {
+    x = LOAD32(k + 0);
+    x = (x & 0xfe000000) | ((x & 0x01fffff0) >> 1);
+    x = (x & 0xfffe0000) | ((x & 0x0001fff8) >> 1);
+    x = (x & 0xfffffe00) | ((x & 0x000001fc) >> 1);
+    z = x; z ^= z >> 4; z ^= z >> 2; z ^= z >> 1;
+    x |= (z & 0x01010101) ^ 0x01010101;
+    y = LOAD32(k + 3) << 1; /* Note: misaligned */
+    y = (y & 0x000000fe) | ((y & 0x1fffff00) << 1);
+    y = (y & 0x0000fefe) | ((y & 0x3fff0000) << 1);
+    y = (y & 0x00fefefe) | ((y & 0x7f000000) << 1);
+    z = y; z ^= z >> 4; z ^= z >> 2; z ^= z >> 1;
+    y |= (z & 0x01010101) ^ 0x01010101;
+  }
+  *xx = x; *yy = y;
+}
+
 /* --- @des_init@ --- *
  *
  * Arguments:  @des_ctx *k@ = pointer to key block
@@ -121,6 +158,7 @@ void des_init(des_ctx *k, const void *buf, size_t sz)
 {
   uint32 x, y;
   uint32 *kp = k->k;
+  uint32 ka[2];
   int i;
 
   /* --- @pc1@ --- *
@@ -187,29 +225,12 @@ void des_init(des_ctx *k, const void *buf, size_t sz)
    */
 
   KSZ_ASSERT(des, sz);
-
-  if (sz == 8) {
-    const octet *p = buf;
-    x = LOAD32(p); y = LOAD32(p + 4);
-  } else {
-    const octet *p = buf;
-    x = LOAD32(p);
-    x = (x & 0xfe000000) | ((x & 0x01fffff0) >> 1);
-    x = (x & 0xfffe0000) | ((x & 0x0001fff8) >> 1);
-    x = (x & 0xfffffe00) | ((x & 0x000001fc) >> 1);
-    y = LOAD32(p + 3) << 1; /* Note: misaligned */
-    y = (y & 0x000000fe) | ((y & 0x1fffff00) << 1);
-    y = (y & 0x0000fefe) | ((y & 0x3fff0000) << 1);
-    y = (y & 0x00fefefe) | ((y & 0x7f000000) << 1);
-  }
-
+  des_expand(buf, sz, &x, &y);
+  
   /* --- Permute using the pointless PC1 --- */
 
-  {
-    uint32 ka[2];
-    permute(pc1, x, y, ka);
-    x = ka[0]; y = ka[1];
-  }
+  permute(pc1, x, y, ka);
+  x = ka[0]; y = ka[1];
 
   /* --- Now for the key schedule proper --- */
 
diff --git a/des.h b/des.h
index b24037b..489d5e0 100644 (file)
--- a/des.h
+++ b/des.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: des.h,v 1.4 2004/04/08 01:36:15 mdw Exp $
+ * $Id$
  *
  * The Data Encryption Standard
  *
@@ -68,6 +68,22 @@ typedef struct des_ctx {
 
 /*----- Functions provided ------------------------------------------------*/
 
+/* --- @des_expand@ --- *
+ *
+ * Arguments:  @const octet *k@ = pointer to key material
+ *             @size_t n@ = number of octets of key material (7 or 8)
+ *             @uint32 *xx, *yy@ = where to put the results
+ *
+ * Returns:    ---
+ *
+ * Use:                Extracts 64 bits of key material from the given buffer,
+ *             possibly expanding it from 56 to 64 bits on the way.
+ *             Parity is set correctly if the key is expanded.
+ */
+
+extern void des_expand(const octet */*k*/, size_t /*n*/,
+                      uint32 */*xx*/, uint32 */*yy*/);
+
 /* --- @des_init@ --- *
  *
  * Arguments:  @des_ctx *k@ = pointer to key block
diff --git a/desx.c b/desx.c
index 6c513f3..9c7f8eb 100644 (file)
--- a/desx.c
+++ b/desx.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: desx.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
+ * $Id$
  *
  * Implementation of DESX
  *
@@ -49,7 +49,7 @@ static const octet s[256] = DESX_S;
 
 /*----- Global variables --------------------------------------------------*/
 
-const octet desx_keysz[] = { KSZ_SET, 7, 8, 15, 16, 23, 24, 0 };
+const octet desx_keysz[] = { KSZ_SET, 23, 7, 8, 15, 16, 24, 0 };
 
 /*----- Main code ---------------------------------------------------------*/
 
@@ -100,35 +100,12 @@ void desx_init(desx_ctx *k, const void *buf, size_t sz)
       k->postb = LOAD32(p + 4);
     } else {
       octet b[16];
+      uint32 x, y;
 
-      if (n == 7) {
-
-       /* --- Expand 7 bits to 8 bits --- *
-        *
-        * Cloned and hacked from @des_init@ to set parity.
-        */
-
-       uint32 x, y, z;
-       x = LOAD32(kk + 0);
-       x = (x & 0xfe000000) | ((x & 0x01fffff0) >> 1);
-       x = (x & 0xfffe0000) | ((x & 0x0001fff8) >> 1);
-       x = (x & 0xfffffe00) | ((x & 0x000001fc) >> 1);
-       z = x; z ^= z >> 4; z ^= z >> 2; z ^= z >> 1;
-       x |= (z & 0x01010101) ^ 0x01010101;
-
-       y = LOAD32(kk + 3) << 1;
-       y = (y & 0x000000fe) | ((y & 0x1fffff00) << 1);
-       y = (y & 0x0000fefe) | ((y & 0x3fff0000) << 1);
-       y = (y & 0x00fefefe) | ((y & 0x7f000000) << 1);
-       z = y; z ^= z >> 4; z ^= z >> 2; z ^= z >> 1;
-       y |= (z & 0x01010101) ^ 0x01010101;
-
-       kk = b + 8;
-       STORE32(kk + 0, x); STORE32(kk + 4, y);
-      }
-
+      des_expand(kk, n, &x, &y);
+      STORE32(b + 8, x); STORE32(b + 12, y);
       memset(b, 0, 8);
-      mangle(b, kk);
+      mangle(b, b + 8);
       mangle(b, q);
       k->posta = LOAD32(b + 0);
       k->postb = LOAD32(b + 4);