iso9660 - type fixes
[libogc.git] / libtinysmb / des.c
blob1dcf8ee6e8c43c5c31426fbc5f62ec58e408ef7d
1 /****************************************************************************
2 * TinySMB
3 * Nintendo Wii/GameCube SMB implementation
5 * DES encryption/decryption Algorithm
6 ****************************************************************************/
8 #include <stdint.h>
9 #include <string.h>
11 typedef struct
13 uint32_t encrypt_subkeys[32];
14 uint32_t decrypt_subkeys[32];
15 } gl_des_ctx;
17 * The s-box values are permuted according to the 'primitive function P'
18 * and are rotated one bit to the left.
20 static const uint32_t sbox1[64] = {
21 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404,
22 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400,
23 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400,
24 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
25 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404,
26 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000,
27 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000,
28 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
29 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404,
30 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000,
31 0x00010004, 0x00010400, 0x00000000, 0x01010004
34 static const uint32_t sbox2[64] = {
35 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020,
36 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000,
37 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020,
38 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
39 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000,
40 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000,
41 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000,
42 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
43 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020,
44 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020,
45 0x80000000, 0x80100020, 0x80108020, 0x00108000
48 static const uint32_t sbox3[64] = {
49 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000,
50 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000,
51 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008,
52 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
53 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208,
54 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208,
55 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008,
56 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
57 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208,
58 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000,
59 0x00020208, 0x00000008, 0x08020008, 0x00020200
62 static const uint32_t sbox4[64] = {
63 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081,
64 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081,
65 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000,
66 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
67 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080,
68 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081,
69 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080,
70 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
71 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001,
72 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001,
73 0x00000080, 0x00800000, 0x00002000, 0x00802080
76 static const uint32_t sbox5[64] = {
77 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100,
78 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100,
79 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000,
80 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
81 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000,
82 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000,
83 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000,
84 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
85 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000,
86 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000,
87 0x00000000, 0x40080000, 0x02080100, 0x40000100
90 static const uint32_t sbox6[64] = {
91 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010,
92 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010,
93 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010,
94 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010,
95 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000,
96 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000,
97 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000,
98 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
99 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000,
100 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000,
101 0x20404000, 0x20000000, 0x00400010, 0x20004010
104 static const uint32_t sbox7[64] = {
105 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802,
106 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002,
107 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802,
108 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002,
109 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002,
110 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802,
111 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000,
112 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
113 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000,
114 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800,
115 0x04000002, 0x04000800, 0x00000800, 0x00200002
118 static const uint32_t sbox8[64] = {
119 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040,
120 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000,
121 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040,
122 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000,
123 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000,
124 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000,
125 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040,
126 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
127 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000,
128 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040,
129 0x00001040, 0x00040040, 0x10000000, 0x10041000
133 * These two tables are part of the 'permuted choice 1' function.
134 * In this implementation several speed improvements are done.
136 static const uint32_t leftkey_swap[16] = {
137 0x00000000, 0x00000001, 0x00000100, 0x00000101,
138 0x00010000, 0x00010001, 0x00010100, 0x00010101,
139 0x01000000, 0x01000001, 0x01000100, 0x01000101,
140 0x01010000, 0x01010001, 0x01010100, 0x01010101
143 static const uint32_t rightkey_swap[16] = {
144 0x00000000, 0x01000000, 0x00010000, 0x01010000,
145 0x00000100, 0x01000100, 0x00010100, 0x01010100,
146 0x00000001, 0x01000001, 0x00010001, 0x01010001,
147 0x00000101, 0x01000101, 0x00010101, 0x01010101,
151 * Numbers of left shifts per round for encryption subkeys. To
152 * calculate the decryption subkeys we just reverse the ordering of
153 * the calculated encryption subkeys, so there is no need for a
154 * decryption rotate tab.
156 static const unsigned char encrypt_rotate_tab[16] = {
157 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
161 * Table with weak DES keys sorted in ascending order. In DES there
162 * are 64 known keys which are weak. They are weak because they
163 * produce only one, two or four different subkeys in the subkey
164 * scheduling process. The keys in this table have all their parity
165 * bits cleared.
167 static const unsigned char weak_keys[64][8] = {
168 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*w */
169 {0x00, 0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e},
170 {0x00, 0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0},
171 {0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe},
172 {0x00, 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e}, /*sw */
173 {0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00},
174 {0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe},
175 {0x00, 0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0},
176 {0x00, 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0}, /*sw */
177 {0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe},
178 {0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00},
179 {0x00, 0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e},
180 {0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe}, /*sw */
181 {0x00, 0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0},
182 {0x00, 0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e},
183 {0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00},
184 {0x1e, 0x00, 0x00, 0x1e, 0x0e, 0x00, 0x00, 0x0e},
185 {0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e, 0x00}, /*sw */
186 {0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0, 0xfe},
187 {0x1e, 0x00, 0xfe, 0xe0, 0x0e, 0x00, 0xfe, 0xf0},
188 {0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00, 0x00},
189 {0x1e, 0x1e, 0x1e, 0x1e, 0x0e, 0x0e, 0x0e, 0x0e}, /*w */
190 {0x1e, 0x1e, 0xe0, 0xe0, 0x0e, 0x0e, 0xf0, 0xf0},
191 {0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe, 0xfe},
192 {0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00, 0xfe},
193 {0x1e, 0xe0, 0x1e, 0xe0, 0x0e, 0xf0, 0x0e, 0xf0}, /*sw */
194 {0x1e, 0xe0, 0xe0, 0x1e, 0x0e, 0xf0, 0xf0, 0x0e},
195 {0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe, 0x00},
196 {0x1e, 0xfe, 0x00, 0xe0, 0x0e, 0xfe, 0x00, 0xf0},
197 {0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe}, /*sw */
198 {0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0, 0x00},
199 {0x1e, 0xfe, 0xfe, 0x1e, 0x0e, 0xfe, 0xfe, 0x0e},
200 {0xe0, 0x00, 0x00, 0xe0, 0xf0, 0x00, 0x00, 0xf0},
201 {0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e, 0xfe},
202 {0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0, 0x00}, /*sw */
203 {0xe0, 0x00, 0xfe, 0x1e, 0xf0, 0x00, 0xfe, 0x0e},
204 {0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00, 0xfe},
205 {0xe0, 0x1e, 0x1e, 0xe0, 0xf0, 0x0e, 0x0e, 0xf0},
206 {0xe0, 0x1e, 0xe0, 0x1e, 0xf0, 0x0e, 0xf0, 0x0e}, /*sw */
207 {0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe, 0x00},
208 {0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00, 0x00},
209 {0xe0, 0xe0, 0x1e, 0x1e, 0xf0, 0xf0, 0x0e, 0x0e},
210 {0xe0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf0, 0xf0}, /*w */
211 {0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe, 0xfe},
212 {0xe0, 0xfe, 0x00, 0x1e, 0xf0, 0xfe, 0x00, 0x0e},
213 {0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e, 0x00},
214 {0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0, 0xfe}, /*sw */
215 {0xe0, 0xfe, 0xfe, 0xe0, 0xf0, 0xfe, 0xfe, 0xf0},
216 {0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe},
217 {0xfe, 0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0},
218 {0xfe, 0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e},
219 {0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00}, /*sw */
220 {0xfe, 0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0},
221 {0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe},
222 {0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00},
223 {0xfe, 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e}, /*sw */
224 {0xfe, 0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e},
225 {0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00},
226 {0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe},
227 {0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0}, /*sw */
228 {0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00},
229 {0xfe, 0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e},
230 {0xfe, 0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0},
231 {0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe} /*w */
235 * Macro to swap bits across two words.
237 #define DO_PERMUTATION(a, temp, b, offset, mask) \
238 temp = ((a>>offset) ^ b) & mask; \
239 b ^= temp; \
240 a ^= temp<<offset;
244 * This performs the 'initial permutation' of the data to be encrypted
245 * or decrypted. Additionally the resulting two words are rotated one bit
246 * to the left.
248 #define INITIAL_PERMUTATION(left, temp, right) \
249 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f) \
250 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
251 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
252 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
253 right = (right << 1) | (right >> 31); \
254 temp = (left ^ right) & 0xaaaaaaaa; \
255 right ^= temp; \
256 left ^= temp; \
257 left = (left << 1) | (left >> 31);
260 * The 'inverse initial permutation'.
262 #define FINAL_PERMUTATION(left, temp, right) \
263 left = (left << 31) | (left >> 1); \
264 temp = (left ^ right) & 0xaaaaaaaa; \
265 left ^= temp; \
266 right ^= temp; \
267 right = (right << 31) | (right >> 1); \
268 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
269 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
270 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
271 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f)
275 * A full DES round including 'expansion function', 'sbox substitution'
276 * and 'primitive function P' but without swapping the left and right word.
277 * Please note: The data in 'from' and 'to' is already rotated one bit to
278 * the left, done in the initial permutation.
280 #define DES_ROUND(from, to, work, subkey) \
281 work = from ^ *subkey++; \
282 to ^= sbox8[ work & 0x3f ]; \
283 to ^= sbox6[ (work>>8) & 0x3f ]; \
284 to ^= sbox4[ (work>>16) & 0x3f ]; \
285 to ^= sbox2[ (work>>24) & 0x3f ]; \
286 work = ((from << 28) | (from >> 4)) ^ *subkey++; \
287 to ^= sbox7[ work & 0x3f ]; \
288 to ^= sbox5[ (work>>8) & 0x3f ]; \
289 to ^= sbox3[ (work>>16) & 0x3f ]; \
290 to ^= sbox1[ (work>>24) & 0x3f ];
293 * Macros to convert 8 bytes from/to 32bit words.
295 #define READ_64BIT_DATA(data, left, right) \
296 left = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \
297 right = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
299 #define WRITE_64BIT_DATA(data, left, right) \
300 data[0] = (left >> 24) &0xff; data[1] = (left >> 16) &0xff; \
301 data[2] = (left >> 8) &0xff; data[3] = left &0xff; \
302 data[4] = (right >> 24) &0xff; data[5] = (right >> 16) &0xff; \
303 data[6] = (right >> 8) &0xff; data[7] = right &0xff;
306 * des_key_schedule(): Calculate 16 subkeys pairs (even/odd) for
307 * 16 encryption rounds.
308 * To calculate subkeys for decryption the caller
309 * have to reorder the generated subkeys.
311 * rawkey: 8 Bytes of key data
312 * subkey: Array of at least 32 uint32_ts. Will be filled
313 * with calculated subkeys.
316 static void
317 des_key_schedule (const char * _rawkey, uint32_t * subkey)
319 const unsigned char *rawkey = (const unsigned char *) _rawkey;
320 uint32_t left, right, work;
321 int round;
323 READ_64BIT_DATA (rawkey, left, right)
324 DO_PERMUTATION (right, work, left, 4, 0x0f0f0f0f)
325 DO_PERMUTATION (right, work, left, 0, 0x10101010)
326 left = ((leftkey_swap[(left >> 0) & 0xf] << 3)
327 | (leftkey_swap[(left >> 8) & 0xf] << 2)
328 | (leftkey_swap[(left >> 16) & 0xf] << 1)
329 | (leftkey_swap[(left >> 24) & 0xf])
330 | (leftkey_swap[(left >> 5) & 0xf] << 7)
331 | (leftkey_swap[(left >> 13) & 0xf] << 6)
332 | (leftkey_swap[(left >> 21) & 0xf] << 5)
333 | (leftkey_swap[(left >> 29) & 0xf] << 4));
335 left &= 0x0fffffff;
337 right = ((rightkey_swap[(right >> 1) & 0xf] << 3)
338 | (rightkey_swap[(right >> 9) & 0xf] << 2)
339 | (rightkey_swap[(right >> 17) & 0xf] << 1)
340 | (rightkey_swap[(right >> 25) & 0xf])
341 | (rightkey_swap[(right >> 4) & 0xf] << 7)
342 | (rightkey_swap[(right >> 12) & 0xf] << 6)
343 | (rightkey_swap[(right >> 20) & 0xf] << 5)
344 | (rightkey_swap[(right >> 28) & 0xf] << 4));
346 right &= 0x0fffffff;
348 for (round = 0; round < 16; ++round)
350 left = ((left << encrypt_rotate_tab[round])
351 | (left >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
352 right = ((right << encrypt_rotate_tab[round])
353 | (right >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
355 *subkey++ = (((left << 4) & 0x24000000)
356 | ((left << 28) & 0x10000000)
357 | ((left << 14) & 0x08000000)
358 | ((left << 18) & 0x02080000)
359 | ((left << 6) & 0x01000000)
360 | ((left << 9) & 0x00200000)
361 | ((left >> 1) & 0x00100000)
362 | ((left << 10) & 0x00040000)
363 | ((left << 2) & 0x00020000)
364 | ((left >> 10) & 0x00010000)
365 | ((right >> 13) & 0x00002000)
366 | ((right >> 4) & 0x00001000)
367 | ((right << 6) & 0x00000800)
368 | ((right >> 1) & 0x00000400)
369 | ((right >> 14) & 0x00000200)
370 | (right & 0x00000100)
371 | ((right >> 5) & 0x00000020)
372 | ((right >> 10) & 0x00000010)
373 | ((right >> 3) & 0x00000008)
374 | ((right >> 18) & 0x00000004)
375 | ((right >> 26) & 0x00000002)
376 | ((right >> 24) & 0x00000001));
378 *subkey++ = (((left << 15) & 0x20000000)
379 | ((left << 17) & 0x10000000)
380 | ((left << 10) & 0x08000000)
381 | ((left << 22) & 0x04000000)
382 | ((left >> 2) & 0x02000000)
383 | ((left << 1) & 0x01000000)
384 | ((left << 16) & 0x00200000)
385 | ((left << 11) & 0x00100000)
386 | ((left << 3) & 0x00080000)
387 | ((left >> 6) & 0x00040000)
388 | ((left << 15) & 0x00020000)
389 | ((left >> 4) & 0x00010000)
390 | ((right >> 2) & 0x00002000)
391 | ((right << 8) & 0x00001000)
392 | ((right >> 14) & 0x00000808)
393 | ((right >> 9) & 0x00000400)
394 | ((right) & 0x00000200)
395 | ((right << 7) & 0x00000100)
396 | ((right >> 7) & 0x00000020)
397 | ((right >> 3) & 0x00000011)
398 | ((right << 2) & 0x00000004)
399 | ((right >> 21) & 0x00000002));
403 void
404 gl_des_setkey (gl_des_ctx *ctx, const char * key)
406 int i;
408 des_key_schedule (key, ctx->encrypt_subkeys);
410 for (i = 0; i < 32; i += 2)
412 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30 - i];
413 ctx->decrypt_subkeys[i + 1] = ctx->encrypt_subkeys[31 - i];
417 void
418 gl_des_ecb_encrypt (gl_des_ctx *ctx, const char * _from, char * _to)
420 const unsigned char *from = (const unsigned char *) _from;
421 unsigned char *to = (unsigned char *) _to;
422 uint32_t left, right, work;
423 uint32_t *keys;
425 keys = ctx->encrypt_subkeys;
427 READ_64BIT_DATA (from, left, right)
428 INITIAL_PERMUTATION (left, work, right)
429 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
430 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
431 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
432 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
433 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
434 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
435 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
436 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
437 FINAL_PERMUTATION (right, work, left)
438 WRITE_64BIT_DATA (to, right, left)