aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/lgl/des.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/https/lgl/des.c')
-rw-r--r--src/daemon/https/lgl/des.c559
1 files changed, 0 insertions, 559 deletions
diff --git a/src/daemon/https/lgl/des.c b/src/daemon/https/lgl/des.c
deleted file mode 100644
index ecb09e41..00000000
--- a/src/daemon/https/lgl/des.c
+++ /dev/null
@@ -1,559 +0,0 @@
1/* des.c --- DES and Triple-DES encryption/decryption Algorithm
2 * Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 * Free Software Foundation, Inc.
4 *
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2.1, or (at your
8 * option) any later version.
9 *
10 * This file is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this file; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA.
19 *
20 */
21
22/* Adapted for gnulib by Simon Josefsson, based on Libgcrypt. */
23
24/*
25 * For a description of triple encryption, see:
26 * Bruce Schneier: Applied Cryptography. Second Edition.
27 * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
28 * This implementation is according to the definition of DES in FIPS
29 * PUB 46-2 from December 1993.
30 *
31 * Written by Michael Roth <mroth@nessie.de>, September 1998
32 */
33
34/*
35 * U S A G E
36 * ===========
37 *
38 * For DES or Triple-DES encryption/decryption you must initialize a proper
39 * encryption context with a key.
40 *
41 * A DES key is 64bit wide but only 56bits of the key are used. The remaining
42 * bits are parity bits and they will _not_ checked in this implementation, but
43 * simply ignored.
44 *
45 * For Triple-DES you could use either two 64bit keys or three 64bit keys.
46 * The parity bits will _not_ checked, too.
47 *
48 * After initializing a context with a key you could use this context to
49 * encrypt or decrypt data in 64bit blocks in Electronic Codebook Mode.
50 *
51 * DES Example
52 * -----------
53 * unsigned char key[8];
54 * unsigned char plaintext[8];
55 * unsigned char ciphertext[8];
56 * unsigned char recoverd[8];
57 * MHD_gl_des_ctx context;
58 *
59 * // Fill 'key' and 'plaintext' with some data
60 * ....
61 *
62 * // Set up the DES encryption context
63 * MHD_gl_des_setkey(&context, key);
64 *
65 * // Encrypt the plaintext
66 * des_ecb_encrypt(&context, plaintext, ciphertext);
67 *
68 * // To recover the orginal plaintext from ciphertext use:
69 * des_ecb_decrypt(&context, ciphertext, recoverd);
70 *
71 *
72 * Triple-DES Example
73 * ------------------
74 * unsigned char key1[8];
75 * unsigned char key2[8];
76 * unsigned char key3[8];
77 * unsigned char plaintext[8];
78 * unsigned char ciphertext[8];
79 * unsigned char recoverd[8];
80 * MHD_gl_3des_ctx context;
81 *
82 * // Encrypting plaintext with Triple-DES
83 * MHD_gl_3des_ecb_encrypt(&context, plaintext, ciphertext);
84 *
85 * // Decrypting ciphertext to recover the plaintext with Triple-DES
86 * MHD_gl_3des_ecb_decrypt(&context, ciphertext, recoverd);
87 */
88
89
90#include "MHD_config.h"
91
92#include "des.h"
93
94#include <stdio.h>
95#include <string.h> /* memcpy, memcmp */
96
97/*
98 * The s-box values are permuted according to the 'primitive function P'
99 * and are rotated one bit to the left.
100 */
101static const uint32_t sbox1[64] = {
102 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404,
103 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400,
104 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400,
105 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
106 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404,
107 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000,
108 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000,
109 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
110 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404,
111 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000,
112 0x00010004, 0x00010400, 0x00000000, 0x01010004
113};
114
115static const uint32_t sbox2[64] = {
116 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020,
117 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000,
118 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020,
119 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
120 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000,
121 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000,
122 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000,
123 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
124 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020,
125 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020,
126 0x80000000, 0x80100020, 0x80108020, 0x00108000
127};
128
129static const uint32_t sbox3[64] = {
130 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000,
131 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000,
132 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008,
133 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
134 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208,
135 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208,
136 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008,
137 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
138 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208,
139 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000,
140 0x00020208, 0x00000008, 0x08020008, 0x00020200
141};
142
143static const uint32_t sbox4[64] = {
144 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081,
145 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081,
146 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000,
147 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
148 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080,
149 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081,
150 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080,
151 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
152 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001,
153 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001,
154 0x00000080, 0x00800000, 0x00002000, 0x00802080
155};
156
157static const uint32_t sbox5[64] = {
158 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100,
159 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100,
160 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000,
161 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
162 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000,
163 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000,
164 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000,
165 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
166 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000,
167 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000,
168 0x00000000, 0x40080000, 0x02080100, 0x40000100
169};
170
171static const uint32_t sbox6[64] = {
172 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010,
173 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010,
174 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010,
175 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010,
176 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000,
177 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000,
178 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000,
179 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
180 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000,
181 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000,
182 0x20404000, 0x20000000, 0x00400010, 0x20004010
183};
184
185static const uint32_t sbox7[64] = {
186 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802,
187 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002,
188 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802,
189 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002,
190 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002,
191 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802,
192 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000,
193 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
194 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000,
195 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800,
196 0x04000002, 0x04000800, 0x00000800, 0x00200002
197};
198
199static const uint32_t sbox8[64] = {
200 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040,
201 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000,
202 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040,
203 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000,
204 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000,
205 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000,
206 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040,
207 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
208 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000,
209 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040,
210 0x00001040, 0x00040040, 0x10000000, 0x10041000
211};
212
213/*
214 * These two tables are part of the 'permuted choice 1' function.
215 * In this implementation several speed improvements are done.
216 */
217static const uint32_t leftkey_swap[16] = {
218 0x00000000, 0x00000001, 0x00000100, 0x00000101,
219 0x00010000, 0x00010001, 0x00010100, 0x00010101,
220 0x01000000, 0x01000001, 0x01000100, 0x01000101,
221 0x01010000, 0x01010001, 0x01010100, 0x01010101
222};
223
224static const uint32_t rightkey_swap[16] = {
225 0x00000000, 0x01000000, 0x00010000, 0x01010000,
226 0x00000100, 0x01000100, 0x00010100, 0x01010100,
227 0x00000001, 0x01000001, 0x00010001, 0x01010001,
228 0x00000101, 0x01000101, 0x00010101, 0x01010101,
229};
230
231/*
232 * Numbers of left shifts per round for encryption subkeys. To
233 * calculate the decryption subkeys we just reverse the ordering of
234 * the calculated encryption subkeys, so there is no need for a
235 * decryption rotate tab.
236 */
237static const unsigned char encrypt_rotate_tab[16] = {
238 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
239};
240
241/*
242 * Table with weak DES keys sorted in ascending order. In DES there
243 * are 64 known keys which are weak. They are weak because they
244 * produce only one, two or four different subkeys in the subkey
245 * scheduling process. The keys in this table have all their parity
246 * bits cleared.
247 */
248static const unsigned char weak_keys[64][8] = {
249 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*w */
250 {0x00, 0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e},
251 {0x00, 0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0},
252 {0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe},
253 {0x00, 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e}, /*sw */
254 {0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00},
255 {0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe},
256 {0x00, 0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0},
257 {0x00, 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0}, /*sw */
258 {0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe},
259 {0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00},
260 {0x00, 0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e},
261 {0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe}, /*sw */
262 {0x00, 0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0},
263 {0x00, 0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e},
264 {0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00},
265 {0x1e, 0x00, 0x00, 0x1e, 0x0e, 0x00, 0x00, 0x0e},
266 {0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e, 0x00}, /*sw */
267 {0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0, 0xfe},
268 {0x1e, 0x00, 0xfe, 0xe0, 0x0e, 0x00, 0xfe, 0xf0},
269 {0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00, 0x00},
270 {0x1e, 0x1e, 0x1e, 0x1e, 0x0e, 0x0e, 0x0e, 0x0e}, /*w */
271 {0x1e, 0x1e, 0xe0, 0xe0, 0x0e, 0x0e, 0xf0, 0xf0},
272 {0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe, 0xfe},
273 {0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00, 0xfe},
274 {0x1e, 0xe0, 0x1e, 0xe0, 0x0e, 0xf0, 0x0e, 0xf0}, /*sw */
275 {0x1e, 0xe0, 0xe0, 0x1e, 0x0e, 0xf0, 0xf0, 0x0e},
276 {0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe, 0x00},
277 {0x1e, 0xfe, 0x00, 0xe0, 0x0e, 0xfe, 0x00, 0xf0},
278 {0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe}, /*sw */
279 {0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0, 0x00},
280 {0x1e, 0xfe, 0xfe, 0x1e, 0x0e, 0xfe, 0xfe, 0x0e},
281 {0xe0, 0x00, 0x00, 0xe0, 0xf0, 0x00, 0x00, 0xf0},
282 {0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e, 0xfe},
283 {0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0, 0x00}, /*sw */
284 {0xe0, 0x00, 0xfe, 0x1e, 0xf0, 0x00, 0xfe, 0x0e},
285 {0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00, 0xfe},
286 {0xe0, 0x1e, 0x1e, 0xe0, 0xf0, 0x0e, 0x0e, 0xf0},
287 {0xe0, 0x1e, 0xe0, 0x1e, 0xf0, 0x0e, 0xf0, 0x0e}, /*sw */
288 {0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe, 0x00},
289 {0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00, 0x00},
290 {0xe0, 0xe0, 0x1e, 0x1e, 0xf0, 0xf0, 0x0e, 0x0e},
291 {0xe0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf0, 0xf0}, /*w */
292 {0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe, 0xfe},
293 {0xe0, 0xfe, 0x00, 0x1e, 0xf0, 0xfe, 0x00, 0x0e},
294 {0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e, 0x00},
295 {0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0, 0xfe}, /*sw */
296 {0xe0, 0xfe, 0xfe, 0xe0, 0xf0, 0xfe, 0xfe, 0xf0},
297 {0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe},
298 {0xfe, 0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0},
299 {0xfe, 0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e},
300 {0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00}, /*sw */
301 {0xfe, 0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0},
302 {0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe},
303 {0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00},
304 {0xfe, 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e}, /*sw */
305 {0xfe, 0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e},
306 {0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00},
307 {0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe},
308 {0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0}, /*sw */
309 {0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00},
310 {0xfe, 0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e},
311 {0xfe, 0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0},
312 {0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe} /*w */
313};
314
315/*
316 * Macro to swap bits across two words.
317 */
318#define DO_PERMUTATION(a, temp, b, offset, mask) \
319 temp = ((a>>offset) ^ b) & mask; \
320 b ^= temp; \
321 a ^= temp<<offset;
322
323
324/*
325 * This performs the 'initial permutation' of the data to be encrypted
326 * or decrypted. Additionally the resulting two words are rotated one bit
327 * to the left.
328 */
329#define INITIAL_PERMUTATION(left, temp, right) \
330 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f) \
331 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
332 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
333 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
334 right = (right << 1) | (right >> 31); \
335 temp = (left ^ right) & 0xaaaaaaaa; \
336 right ^= temp; \
337 left ^= temp; \
338 left = (left << 1) | (left >> 31);
339
340/*
341 * The 'inverse initial permutation'.
342 */
343#define FINAL_PERMUTATION(left, temp, right) \
344 left = (left << 31) | (left >> 1); \
345 temp = (left ^ right) & 0xaaaaaaaa; \
346 left ^= temp; \
347 right ^= temp; \
348 right = (right << 31) | (right >> 1); \
349 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
350 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
351 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
352 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f)
353
354
355/*
356 * A full DES round including 'expansion function', 'sbox substitution'
357 * and 'primitive function P' but without swapping the left and right word.
358 * Please note: The data in 'from' and 'to' is already rotated one bit to
359 * the left, done in the initial permutation.
360 */
361#define DES_ROUND(from, to, work, subkey) \
362 work = from ^ *subkey++; \
363 to ^= sbox8[ work & 0x3f ]; \
364 to ^= sbox6[ (work>>8) & 0x3f ]; \
365 to ^= sbox4[ (work>>16) & 0x3f ]; \
366 to ^= sbox2[ (work>>24) & 0x3f ]; \
367 work = ((from << 28) | (from >> 4)) ^ *subkey++; \
368 to ^= sbox7[ work & 0x3f ]; \
369 to ^= sbox5[ (work>>8) & 0x3f ]; \
370 to ^= sbox3[ (work>>16) & 0x3f ]; \
371 to ^= sbox1[ (work>>24) & 0x3f ];
372
373/*
374 * Macros to convert 8 bytes from/to 32bit words.
375 */
376#define READ_64BIT_DATA(data, left, right) \
377 left = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \
378 right = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
379
380#define WRITE_64BIT_DATA(data, left, right) \
381 data[0] = (left >> 24) &0xff; data[1] = (left >> 16) &0xff; \
382 data[2] = (left >> 8) &0xff; data[3] = left &0xff; \
383 data[4] = (right >> 24) &0xff; data[5] = (right >> 16) &0xff; \
384 data[6] = (right >> 8) &0xff; data[7] = right &0xff;
385
386/*
387 * des_key_schedule(): Calculate 16 subkeys pairs (even/odd) for
388 * 16 encryption rounds.
389 * To calculate subkeys for decryption the caller
390 * have to reorder the generated subkeys.
391 *
392 * rawkey: 8 Bytes of key data
393 * subkey: Array of at least 32 uint32_ts. Will be filled
394 * with calculated subkeys.
395 *
396 */
397static void
398des_key_schedule (const char *_rawkey, uint32_t * subkey)
399{
400 const unsigned char *rawkey = (const unsigned char *) _rawkey;
401 uint32_t left, right, work;
402 int round;
403
404 READ_64BIT_DATA (rawkey, left, right)
405 DO_PERMUTATION (right, work, left, 4, 0x0f0f0f0f)
406 DO_PERMUTATION (right, work, left, 0, 0x10101010)
407 left = ((leftkey_swap[(left >> 0) & 0xf] << 3)
408 | (leftkey_swap[(left >> 8) & 0xf] << 2)
409 | (leftkey_swap[(left >> 16) & 0xf] << 1)
410 | (leftkey_swap[(left >> 24) & 0xf])
411 | (leftkey_swap[(left >> 5) & 0xf] << 7)
412 | (leftkey_swap[(left >> 13) & 0xf] << 6)
413 | (leftkey_swap[(left >> 21) & 0xf] << 5)
414 | (leftkey_swap[(left >> 29) & 0xf] << 4));
415
416 left &= 0x0fffffff;
417
418 right = ((rightkey_swap[(right >> 1) & 0xf] << 3)
419 | (rightkey_swap[(right >> 9) & 0xf] << 2)
420 | (rightkey_swap[(right >> 17) & 0xf] << 1)
421 | (rightkey_swap[(right >> 25) & 0xf])
422 | (rightkey_swap[(right >> 4) & 0xf] << 7)
423 | (rightkey_swap[(right >> 12) & 0xf] << 6)
424 | (rightkey_swap[(right >> 20) & 0xf] << 5)
425 | (rightkey_swap[(right >> 28) & 0xf] << 4));
426
427 right &= 0x0fffffff;
428
429 for (round = 0; round < 16; ++round)
430 {
431 left = ((left << encrypt_rotate_tab[round])
432 | (left >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
433 right = ((right << encrypt_rotate_tab[round])
434 | (right >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
435
436 *subkey++ = (((left << 4) & 0x24000000)
437 | ((left << 28) & 0x10000000)
438 | ((left << 14) & 0x08000000)
439 | ((left << 18) & 0x02080000)
440 | ((left << 6) & 0x01000000)
441 | ((left << 9) & 0x00200000)
442 | ((left >> 1) & 0x00100000)
443 | ((left << 10) & 0x00040000)
444 | ((left << 2) & 0x00020000)
445 | ((left >> 10) & 0x00010000)
446 | ((right >> 13) & 0x00002000)
447 | ((right >> 4) & 0x00001000)
448 | ((right << 6) & 0x00000800)
449 | ((right >> 1) & 0x00000400)
450 | ((right >> 14) & 0x00000200)
451 | (right & 0x00000100)
452 | ((right >> 5) & 0x00000020)
453 | ((right >> 10) & 0x00000010)
454 | ((right >> 3) & 0x00000008)
455 | ((right >> 18) & 0x00000004)
456 | ((right >> 26) & 0x00000002)
457 | ((right >> 24) & 0x00000001));
458
459 *subkey++ = (((left << 15) & 0x20000000)
460 | ((left << 17) & 0x10000000)
461 | ((left << 10) & 0x08000000)
462 | ((left << 22) & 0x04000000)
463 | ((left >> 2) & 0x02000000)
464 | ((left << 1) & 0x01000000)
465 | ((left << 16) & 0x00200000)
466 | ((left << 11) & 0x00100000)
467 | ((left << 3) & 0x00080000)
468 | ((left >> 6) & 0x00040000)
469 | ((left << 15) & 0x00020000)
470 | ((left >> 4) & 0x00010000)
471 | ((right >> 2) & 0x00002000)
472 | ((right << 8) & 0x00001000)
473 | ((right >> 14) & 0x00000808)
474 | ((right >> 9) & 0x00000400)
475 | ((right) & 0x00000200)
476 | ((right << 7) & 0x00000100)
477 | ((right >> 7) & 0x00000020)
478 | ((right >> 3) & 0x00000011)
479 | ((right << 2) & 0x00000004)
480 | ((right >> 21) & 0x00000002));
481 }
482}
483
484void
485MHD_gl_des_setkey (MHD_gl_des_ctx * ctx, const char *key)
486{
487 int i;
488
489 des_key_schedule (key, ctx->encrypt_subkeys);
490
491 for (i = 0; i < 32; i += 2)
492 {
493 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30 - i];
494 ctx->decrypt_subkeys[i + 1] = ctx->encrypt_subkeys[31 - i];
495 }
496}
497
498void
499MHD_gl_des_ecb_crypt (MHD_gl_des_ctx * ctx, const char *_from, char *_to,
500 int mode)
501{
502 const unsigned char *from = (const unsigned char *) _from;
503 unsigned char *to = (unsigned char *) _to;
504 uint32_t left, right, work;
505 uint32_t *keys;
506
507 keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
508
509READ_64BIT_DATA (from, left, right)
510 INITIAL_PERMUTATION (left, work, right)
511 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
512 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
513 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
514 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
515 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
516 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
517 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
518 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
519 FINAL_PERMUTATION (right, work, left) WRITE_64BIT_DATA (to, right, left)}
520
521void
522MHD_gl_3des_ecb_crypt (MHD_gl_3des_ctx * ctx, const char *_from, char *_to,
523 int mode)
524{
525 const unsigned char *from = (const unsigned char *) _from;
526 unsigned char *to = (unsigned char *) _to;
527 uint32_t left, right, work;
528 uint32_t *keys;
529
530 keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
531
532READ_64BIT_DATA (from, left, right)
533 INITIAL_PERMUTATION (left, work, right)
534 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
535 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
536 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
537 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
538 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
539 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
540 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
541 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
542 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
543 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
544 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
545 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
546 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
547 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
548 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
549 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
550 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
551 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
552 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
553 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
554 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
555 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
556 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
557 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
558 FINAL_PERMUTATION (right, work, left) WRITE_64BIT_DATA (to, right, left)}
559