/* -*-c-*-
*
- * $Id: des.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
+ * $Id$
*
* The Data Encryption Standard
*
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
{
uint32 x, y;
uint32 *kp = k->k;
+ uint32 ka[2];
int i;
/* --- @pc1@ --- *
*/
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 --- */
/* -*-c-*-
*
- * $Id: des.h,v 1.4 2004/04/08 01:36:15 mdw Exp $
+ * $Id$
*
* The Data Encryption Standard
*
/*----- 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
/* -*-c-*-
*
- * $Id: desx.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
+ * $Id$
*
* Implementation of DESX
*
/*----- 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 ---------------------------------------------------------*/
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);