X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/89ae755da614975a84c65da6ffcf351dec60a690..025c5f4aa5ffbf8948482a4233318db81c2df5d2:/des.c diff --git a/des.c b/des.c index d48637b..a056c11 100644 --- a/des.c +++ b/des.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: des.c,v 1.2 2000/06/17 10:52:32 mdw Exp $ + * $Id$ * * The Data Encryption Standard * @@ -27,17 +27,6 @@ * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: des.c,v $ - * Revision 1.2 2000/06/17 10:52:32 mdw - * Support new key size interface. - * - * Revision 1.1 1999/09/03 08:41:11 mdw - * Initial import. - * - */ - /*----- Header files ------------------------------------------------------*/ #include @@ -113,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 @@ -132,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@ --- * @@ -198,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 --- */