3 * The Data Encryption Standard
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
42 /*----- Global variables --------------------------------------------------*/
44 const octet des_keysz[] = { KSZ_SET, 7, 8, 0 };
46 /*----- Main code ---------------------------------------------------------*/
48 /* --- @permute@ --- *
50 * Arguments: @const char *p@ = pointer to permutation table
51 * @uint32 a, b@ = source value to permute
52 * @uint32 *d@ = destination for value
56 * Use: Performs a 64-bit permutation. The table is given in the
57 * normal (but bizarre) DES bit numbering system. That's not to
58 * say that the tables in this source file are like the normal
59 * DES tables, because they're not.
62 static void permute(const char *p, uint32 a, uint32 b, uint32 *d)
67 for (i = 0; i < 32; i++) {
78 if (t & (1 << (32 - q)))
84 for (i = 0; i < 32; i++) {
95 if (t & (1 << (32 - q)))
103 /* --- @des_expand@ --- *
105 * Arguments: @const octet *k@ = pointer to key material
106 * @size_t n@ = number of octets of key material (7 or 8)
107 * @uint32 *xx, *yy@ = where to put the results
111 * Use: Extracts 64 bits of key material from the given buffer,
112 * possibly expanding it from 56 to 64 bits on the way.
113 * Parity is set correctly if the key is expanded.
116 void des_expand(const octet *k, size_t n, uint32 *xx, uint32 *yy)
125 x = (x & 0xfe000000) | ((x & 0x01fffff0) >> 1);
126 x = (x & 0xfffe0000) | ((x & 0x0001fff8) >> 1);
127 x = (x & 0xfffffe00) | ((x & 0x000001fc) >> 1);
128 z = x; z ^= z >> 4; z ^= z >> 2; z ^= z >> 1;
129 x |= (z & 0x01010101) ^ 0x01010101;
130 y = LOAD32(k + 3) << 1; /* Note: misaligned */
131 y = (y & 0x000000fe) | ((y & 0x1fffff00) << 1);
132 y = (y & 0x0000fefe) | ((y & 0x3fff0000) << 1);
133 y = (y & 0x00fefefe) | ((y & 0x7f000000) << 1);
134 z = y; z ^= z >> 4; z ^= z >> 2; z ^= z >> 1;
135 y |= (z & 0x01010101) ^ 0x01010101;
140 /* --- @des_init@ --- *
142 * Arguments: @des_ctx *k@ = pointer to key block
143 * @const void *buf@ = pointer to key buffer
144 * @size_t sz@ = size of key material
148 * Use: Initializes a DES key buffer. The key buffer may be either 7
149 * or 8 bytes long. If it's 8 bytes, the key is assumed to be
150 * padded with parity bits in the low order bit of each octet.
151 * These are stripped out without checking prior to the actual
155 void des_init(des_ctx *k, const void *buf, size_t sz)
164 * This cryptographically useless permutation is used to mangle the key
165 * before it's subjected to the key schedule proper. I've not actually
166 * messed it about much except for inserting padding at the beginning of
167 * the two halves of the key.
170 static const char pc1[] = {
172 57, 49, 41, 33, 25, 17, 9,
173 1, 58, 50, 42, 34, 26, 18,
174 10, 2, 59, 51, 43, 35, 27,
175 19, 11, 3, 60, 52, 44, 36,
177 63, 55, 47, 39, 31, 23, 15,
178 7, 62, 54, 46, 38, 30, 22,
179 14, 6, 61, 53, 45, 37, 29,
180 21, 13, 5, 28, 20, 12, 4
185 * This irritating but necessary permutation mangles the key between the
186 * simple rotation-based schedule and the actual XOR with which it modifies
187 * the behaviour of the cipher.
189 * This version of the table doesn't look much like the original. This is
190 * because some parts of the world have been permuted in order to make
191 * things simpler for the round function. In particular, everything is
192 * rotated left one place to avoid problems with the wraparound of the
193 * expansion permutation, and the key is split between odd and even S-boxes
194 * rather than high and low ones. That's without the complication of the
195 * padding bits in the representation of the 56-bit proto-key.
198 static const char pc2[] = {
199 0, 0, 3 + 4, 28 + 4, 15 + 4, 6 + 4, 21 + 4, 10 + 4, /* S-box 2 */
200 0, 0, 16 + 4, 7 + 4, 27 + 4, 20 + 4, 13 + 4, 2 + 4, /* S-box 4 */
201 0, 0, 30 + 8, 40 + 8, 51 + 8, 45 + 8, 33 + 8, 48 + 8, /* S-box 6 */
202 0, 0, 46 + 8, 42 + 8, 50 + 8, 36 + 8, 29 + 8, 32 + 8, /* S-box 8 */
203 0, 0, 14 + 4, 17 + 4, 11 + 4, 24 + 4, 1 + 4, 5 + 4, /* S-box 1 */
204 0, 0, 23 + 4, 19 + 4, 12 + 4, 4 + 4, 26 + 4, 8 + 4, /* S-box 3 */
205 0, 0, 41 + 8, 52 + 8, 31 + 8, 37 + 8, 47 + 8, 55 + 8, /* S-box 5 */
206 0, 0, 44 + 8, 49 + 8, 39 + 8, 56 + 8, 34 + 8, 53 + 8 /* S-box 7 */
211 * Contains the rotation amounts for the key halves.
214 static const char v[] = {
215 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
218 /* --- Extract the key into my registers --- *
220 * The 7 byte case is rather horrible. It expands the key to the 8 byte
221 * case before going any further. It could probably do with its own @pc1@
226 des_expand(buf, sz, &x, &y);
228 /* --- Permute using the pointless PC1 --- */
230 permute(pc1, x, y, ka);
231 x = ka[0]; y = ka[1];
233 /* --- Now for the key schedule proper --- */
235 for (i = 0; i < 16; i++) {
237 x = ((x << 1) | (x >> 27)) & 0x0fffffff;
238 y = ((y << 1) | (y >> 27)) & 0x0fffffff;
240 x = ((x << 2) | (x >> 26)) & 0x0fffffff;
241 y = ((y << 2) | (y >> 26)) & 0x0fffffff;
243 permute(pc2, x, y, kp);
248 /* --- @des_eblk@, @des_dblk@ --- *
250 * Arguments: @const des_ctx *k@ = pointer to key block
251 * @const uint32 s[2]@ = pointer to source block
252 * @uint32 d[2]@ = pointer to destination block
256 * Use: Low-level block encryption and decryption.
259 void des_eblk(const des_ctx *k, const uint32 *s, uint32 *d)
261 uint32 x = s[0], y = s[1];
263 DES_EBLK(k->k, x, y, x, y);
268 void des_dblk(const des_ctx *k, const uint32 *s, uint32 *d)
270 uint32 x = s[0], y = s[1];
272 DES_DBLK(k->k, x, y, x, y);
279 /*----- That's all, folks -------------------------------------------------*/