Uprating of the passphrase pixie.
[u/mdw/catacomb] / des.c
diff --git a/des.c b/des.c
index cb73a8e..a056c11 100644 (file)
--- a/des.c
+++ b/des.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: des.c,v 1.1 1999/09/03 08:41:11 mdw Exp $
+ * $Id$
  *
  * The Data Encryption Standard
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: des.c,v $
- * Revision 1.1  1999/09/03 08:41:11  mdw
- * Initial import.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <assert.h>
 #include "blkc.h"
 #include "des-base.h"
 #include "des.h"
+#include "gcipher.h"
+
+/*----- Global variables --------------------------------------------------*/
+
+const octet des_keysz[] = { KSZ_SET, 7, 8, 0 };
 
 /*----- Main code ---------------------------------------------------------*/
 
@@ -105,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
@@ -124,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@ --- *
@@ -189,30 +224,13 @@ void des_init(des_ctx *k, const void *buf, size_t sz)
    * table.
    */
 
-  assert(((void)"DES key must be 56 or 64 bits", sz == 7 || sz == 8));
-
-  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);
-  }
-
+  KSZ_ASSERT(des, sz);
+  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 --- */