aboutsummaryrefslogtreecommitdiff
path: root/libbb/pw_encrypt_des.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-06-15 09:48:18 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-06-15 09:48:18 +0000
commit5cdc247ba4e813db8c2d0a05ed7ed9624ed422fa (patch)
treee1dd3f2508d486c23941a11175b689a2bdef24d8 /libbb/pw_encrypt_des.c
parent04087c6bbd247ef5802de3f8bd625fa3643e23e2 (diff)
downloadbusybox-w32-5cdc247ba4e813db8c2d0a05ed7ed9624ed422fa.tar.gz
busybox-w32-5cdc247ba4e813db8c2d0a05ed7ed9624ed422fa.tar.bz2
busybox-w32-5cdc247ba4e813db8c2d0a05ed7ed9624ed422fa.zip
further encrypt_des optimizations
function old new delta pw_encrypt 943 964 +21 des_crypt 1512 1509 -3 u_sbox 512 256 -256 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/2 up/down: 21/-259) Total: -238 bytes
Diffstat (limited to 'libbb/pw_encrypt_des.c')
-rw-r--r--libbb/pw_encrypt_des.c192
1 files changed, 141 insertions, 51 deletions
diff --git a/libbb/pw_encrypt_des.c b/libbb/pw_encrypt_des.c
index 956a3e679..a13e1a5df 100644
--- a/libbb/pw_encrypt_des.c
+++ b/libbb/pw_encrypt_des.c
@@ -56,6 +56,14 @@
56 * alignment). 56 * alignment).
57 */ 57 */
58 58
59
60/* Parts busybox doesn't need or had optimized */
61#define USE_PRECOMPUTED_u_sbox 1
62#define USE_REPETITIVE_SPEEDUP 0
63#define USE_ip_mask 0
64#define USE_de_keys 0
65
66
59/* A pile of data */ 67/* A pile of data */
60static const uint8_t IP[64] = { 68static const uint8_t IP[64] = {
61 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 69 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
@@ -85,57 +93,93 @@ static const uint8_t comp_perm[48] = {
85/* 93/*
86 * No E box is used, as it's replaced by some ANDs, shifts, and ORs. 94 * No E box is used, as it's replaced by some ANDs, shifts, and ORs.
87 */ 95 */
88 96#if !USE_PRECOMPUTED_u_sbox
89static const uint8_t sbox[8][64] = { 97static const uint8_t sbox[8][64] = {
90 { 98 { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
91 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
92 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 99 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
93 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 100 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
94 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 101 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
95 }, 102 },
96 { 103 { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
97 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
98 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 104 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
99 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 105 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
100 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 106 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
101 }, 107 },
102 { 108 { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
103 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
104 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 109 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
105 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 110 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
106 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 111 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
107 }, 112 },
108 { 113 { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
109 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
110 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 114 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
111 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 115 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
112 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 116 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
113 }, 117 },
114 { 118 { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
115 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
116 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 119 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
117 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 120 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
118 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 121 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
119 }, 122 },
120 { 123 { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
121 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
122 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 124 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
123 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 125 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
124 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 126 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
125 }, 127 },
126 { 128 { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
127 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
128 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 129 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
129 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 130 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
130 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 131 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
131 }, 132 },
132 { 133 { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
133 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
134 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 134 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
135 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 135 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
136 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 136 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
137 } 137 }
138}; 138};
139#else /* precomputed, with half-bytes packed into one byte */
140static const uint8_t u_sbox[8][32] = {
141 { 0x0e, 0xf4, 0x7d, 0x41, 0xe2, 0x2f, 0xdb, 0x18,
142 0xa3, 0x6a, 0xc6, 0xbc, 0x95, 0x59, 0x30, 0x87,
143 0xf4, 0xc1, 0x8e, 0x28, 0x4d, 0x96, 0x12, 0x7b,
144 0x5f, 0xbc, 0x39, 0xe7, 0xa3, 0x0a, 0x65, 0xd0,
145 },
146 { 0x3f, 0xd1, 0x48, 0x7e, 0xf6, 0x2b, 0x83, 0xe4,
147 0xc9, 0x07, 0x12, 0xad, 0x6c, 0x90, 0xb5, 0x5a,
148 0xd0, 0x8e, 0xa7, 0x1b, 0x3a, 0xf4, 0x4d, 0x21,
149 0xb5, 0x68, 0x7c, 0xc6, 0x09, 0x53, 0xe2, 0x9f,
150 },
151 { 0xda, 0x70, 0x09, 0x9e, 0x36, 0x43, 0x6f, 0xa5,
152 0x21, 0x8d, 0x5c, 0xe7, 0xcb, 0xb4, 0xf2, 0x18,
153 0x1d, 0xa6, 0xd4, 0x09, 0x68, 0x9f, 0x83, 0x70,
154 0x4b, 0xf1, 0xe2, 0x3c, 0xb5, 0x5a, 0x2e, 0xc7,
155 },
156 { 0xd7, 0x8d, 0xbe, 0x53, 0x60, 0xf6, 0x09, 0x3a,
157 0x41, 0x72, 0x28, 0xc5, 0x1b, 0xac, 0xe4, 0x9f,
158 0x3a, 0xf6, 0x09, 0x60, 0xac, 0x1b, 0xd7, 0x8d,
159 0x9f, 0x41, 0x53, 0xbe, 0xc5, 0x72, 0x28, 0xe4,
160 },
161 { 0xe2, 0xbc, 0x24, 0xc1, 0x47, 0x7a, 0xdb, 0x16,
162 0x58, 0x05, 0xf3, 0xaf, 0x3d, 0x90, 0x8e, 0x69,
163 0xb4, 0x82, 0xc1, 0x7b, 0x1a, 0xed, 0x27, 0xd8,
164 0x6f, 0xf9, 0x0c, 0x95, 0xa6, 0x43, 0x50, 0x3e,
165 },
166 { 0xac, 0xf1, 0x4a, 0x2f, 0x79, 0xc2, 0x96, 0x58,
167 0x60, 0x1d, 0xd3, 0xe4, 0x0e, 0xb7, 0x35, 0x8b,
168 0x49, 0x3e, 0x2f, 0xc5, 0x92, 0x58, 0xfc, 0xa3,
169 0xb7, 0xe0, 0x14, 0x7a, 0x61, 0x0d, 0x8b, 0xd6,
170 },
171 { 0xd4, 0x0b, 0xb2, 0x7e, 0x4f, 0x90, 0x18, 0xad,
172 0xe3, 0x3c, 0x59, 0xc7, 0x25, 0xfa, 0x86, 0x61,
173 0x61, 0xb4, 0xdb, 0x8d, 0x1c, 0x43, 0xa7, 0x7e,
174 0x9a, 0x5f, 0x06, 0xf8, 0xe0, 0x25, 0x39, 0xc2,
175 },
176 { 0x1d, 0xf2, 0xd8, 0x84, 0xa6, 0x3f, 0x7b, 0x41,
177 0xca, 0x59, 0x63, 0xbe, 0x05, 0xe0, 0x9c, 0x27,
178 0x27, 0x1b, 0xe4, 0x71, 0x49, 0xac, 0x8e, 0xd2,
179 0xf0, 0xc6, 0x9a, 0x0d, 0x3f, 0x53, 0x65, 0xb8,
180 },
181};
182#endif
139 183
140static const uint8_t pbox[32] = { 184static const uint8_t pbox[32] = {
141 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 185 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
@@ -180,7 +224,10 @@ ascii_to_bin(char ch)
180 * being initialized, and therefore doesn't need to be made 224 * being initialized, and therefore doesn't need to be made
181 * reentrant. */ 225 * reentrant. */
182struct const_des_ctx { 226struct const_des_ctx {
183 uint8_t init_perm[64], final_perm[64]; /* referenced 2 times each */ 227#if USE_ip_mask
228 uint8_t init_perm[64]; /* referenced 2 times */
229#endif
230 uint8_t final_perm[64]; /* 2 times */
184 uint8_t m_sbox[4][4096]; /* 5 times */ 231 uint8_t m_sbox[4][4096]; /* 5 times */
185}; 232};
186#define C (*cctx) 233#define C (*cctx)
@@ -191,22 +238,27 @@ struct const_des_ctx {
191static struct const_des_ctx* 238static struct const_des_ctx*
192const_des_init(void) 239const_des_init(void)
193{ 240{
194 int i, j, b; 241 unsigned i, j, b;
195 uint8_t u_sbox[8][64];
196 struct const_des_ctx *cctx; 242 struct const_des_ctx *cctx;
197 243
244#if !USE_PRECOMPUTED_u_sbox
245 uint8_t u_sbox[8][64];
246
198 cctx = xmalloc(sizeof(*cctx)); 247 cctx = xmalloc(sizeof(*cctx));
199 248
200 /* 249 /* Invert the S-boxes, reordering the input bits. */
201 * Invert the S-boxes, reordering the input bits.
202 */
203 for (i = 0; i < 8; i++) { 250 for (i = 0; i < 8; i++) {
204 for (j = 0; j < 64; j++) { 251 for (j = 0; j < 64; j++) {
205 b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf); 252 b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf);
206 u_sbox[i][j] = sbox[i][b]; 253 u_sbox[i][j] = sbox[i][b];
207 } 254 }
208 } 255 }
209 256 for (i = 0; i < 8; i++) {
257 fprintf(stderr, "\t{\t");
258 for (j = 0; j < 64; j+=2)
259 fprintf(stderr, " 0x%02x,", u_sbox[i][j] + u_sbox[i][j+1]*16);
260 fprintf(stderr, "\n\t},\n");
261 }
210 /* 262 /*
211 * Convert the inverted S-boxes into 4 arrays of 8 bits. 263 * Convert the inverted S-boxes into 4 arrays of 8 bits.
212 * Each will handle 12 bits of the S-box input. 264 * Each will handle 12 bits of the S-box input.
@@ -217,24 +269,45 @@ const_des_init(void)
217 m_sbox[b][(i << 6) | j] = 269 m_sbox[b][(i << 6) | j] =
218 (uint8_t)((u_sbox[(b << 1)][i] << 4) | 270 (uint8_t)((u_sbox[(b << 1)][i] << 4) |
219 u_sbox[(b << 1) + 1][j]); 271 u_sbox[(b << 1) + 1][j]);
272#else
273 cctx = xmalloc(sizeof(*cctx));
274
275 /*
276 * Convert the inverted S-boxes into 4 arrays of 8 bits.
277 * Each will handle 12 bits of the S-box input.
278 */
279 for (b = 0; b < 4; b++)
280 for (i = 0; i < 64; i++)
281 for (j = 0; j < 64; j++) {
282 uint8_t lo, hi;
283 hi = u_sbox[(b << 1)][i / 2];
284 if (!(i & 1))
285 hi <<= 4;
286 lo = u_sbox[(b << 1) + 1][j / 2];
287 if (j & 1)
288 lo >>= 4;
289 m_sbox[b][(i << 6) | j] = (hi & 0xf0) | (lo & 0x0f);
290 }
291#endif
220 292
221 /* 293 /*
222 * Set up the initial & final permutations into a useful form. 294 * Set up the initial & final permutations into a useful form.
223 */ 295 */
224 for (i = 0; i < 64; i++) { 296 for (i = 0; i < 64; i++) {
225 final_perm[i] = IP[i] - 1; 297 final_perm[i] = IP[i] - 1;
298#if USE_ip_mask
226 init_perm[final_perm[i]] = (uint8_t)i; 299 init_perm[final_perm[i]] = (uint8_t)i;
300#endif
227 } 301 }
228 302
229 return cctx; 303 return cctx;
230} 304}
231 305
232#define WANT_REPETITIVE_SPEEDUP 0
233 306
234struct des_ctx { 307struct des_ctx {
235 const struct const_des_ctx *const_ctx; 308 const struct const_des_ctx *const_ctx;
236 uint32_t saltbits; /* referenced 5 times */ 309 uint32_t saltbits; /* referenced 5 times */
237#if WANT_REPETITIVE_SPEEDUP 310#if USE_REPETITIVE_SPEEDUP
238 uint32_t old_salt; /* 3 times */ 311 uint32_t old_salt; /* 3 times */
239 uint32_t old_rawkey0, old_rawkey1; /* 3 times each */ 312 uint32_t old_rawkey0, old_rawkey1; /* 3 times each */
240#endif 313#endif
@@ -242,8 +315,12 @@ struct des_ctx {
242 uint8_t inv_comp_perm[56]; /* 3 times */ 315 uint8_t inv_comp_perm[56]; /* 3 times */
243 uint8_t inv_key_perm[64]; /* 3 times */ 316 uint8_t inv_key_perm[64]; /* 3 times */
244 uint32_t en_keysl[16], en_keysr[16]; /* 2 times each */ 317 uint32_t en_keysl[16], en_keysr[16]; /* 2 times each */
245// uint32_t de_keysl[16], de_keysr[16]; /* 2 times each */ 318#if USE_de_keys
319 uint32_t de_keysl[16], de_keysr[16]; /* 2 times each */
320#endif
321#if USE_ip_mask
246 uint32_t ip_maskl[8][256], ip_maskr[8][256]; /* 9 times each */ 322 uint32_t ip_maskl[8][256], ip_maskr[8][256]; /* 9 times each */
323#endif
247 uint32_t fp_maskl[8][256], fp_maskr[8][256]; /* 9 times each */ 324 uint32_t fp_maskl[8][256], fp_maskr[8][256]; /* 9 times each */
248 uint32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; /* 9 times */ 325 uint32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; /* 9 times */
249 uint32_t comp_maskl[8][128], comp_maskr[8][128]; /* 9 times each */ 326 uint32_t comp_maskl[8][128], comp_maskr[8][128]; /* 9 times each */
@@ -260,8 +337,8 @@ struct des_ctx {
260#define inv_key_perm (D.inv_key_perm ) 337#define inv_key_perm (D.inv_key_perm )
261#define en_keysl (D.en_keysl ) 338#define en_keysl (D.en_keysl )
262#define en_keysr (D.en_keysr ) 339#define en_keysr (D.en_keysr )
263//#define de_keysl (D.de_keysl ) 340#define de_keysl (D.de_keysl )
264//#define de_keysr (D.de_keysr ) 341#define de_keysr (D.de_keysr )
265#define ip_maskl (D.ip_maskl ) 342#define ip_maskl (D.ip_maskl )
266#define ip_maskr (D.ip_maskr ) 343#define ip_maskr (D.ip_maskr )
267#define fp_maskl (D.fp_maskl ) 344#define fp_maskl (D.fp_maskl )
@@ -277,14 +354,13 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
277{ 354{
278 int i, j, b, k, inbit, obit; 355 int i, j, b, k, inbit, obit;
279 uint32_t p; 356 uint32_t p;
280 uint32_t il, ir, fl, fr;
281 const uint32_t *bits28, *bits24; 357 const uint32_t *bits28, *bits24;
282 358
283 if (!ctx) 359 if (!ctx)
284 ctx = xmalloc(sizeof(*ctx)); 360 ctx = xmalloc(sizeof(*ctx));
285 const_ctx = cctx; 361 const_ctx = cctx;
286 362
287#if WANT_REPETITIVE_SPEEDUP 363#if USE_REPETITIVE_SPEEDUP
288 old_rawkey0 = old_rawkey1 = 0; 364 old_rawkey0 = old_rawkey1 = 0;
289 old_salt = 0; 365 old_salt = 0;
290#endif 366#endif
@@ -292,9 +368,7 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
292 bits28 = bits32 + 4; 368 bits28 = bits32 + 4;
293 bits24 = bits28 + 4; 369 bits24 = bits28 + 4;
294 370
295 /* 371 /* Initialise the inverted key permutation. */
296 * Initialise the inverted key permutation.
297 */
298 for (i = 0; i < 64; i++) { 372 for (i = 0; i < 64; i++) {
299 inv_key_perm[i] = 255; 373 inv_key_perm[i] = 255;
300 } 374 }
@@ -308,9 +382,7 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
308 inv_comp_perm[i] = 255; 382 inv_comp_perm[i] = 255;
309 } 383 }
310 384
311 /* 385 /* Invert the key compression permutation. */
312 * Invert the key compression permutation.
313 */
314 for (i = 0; i < 48; i++) { 386 for (i = 0; i < 48; i++) {
315 inv_comp_perm[comp_perm[i] - 1] = (uint8_t)i; 387 inv_comp_perm[comp_perm[i] - 1] = (uint8_t)i;
316 } 388 }
@@ -320,19 +392,25 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
320 * and for the key initial and compression permutations. 392 * and for the key initial and compression permutations.
321 */ 393 */
322 for (k = 0; k < 8; k++) { 394 for (k = 0; k < 8; k++) {
395 uint32_t il, ir;
396 uint32_t fl, fr;
323 for (i = 0; i < 256; i++) { 397 for (i = 0; i < 256; i++) {
398#if USE_ip_mask
324 il = 0; 399 il = 0;
325 ir = 0; 400 ir = 0;
401#endif
326 fl = 0; 402 fl = 0;
327 fr = 0; 403 fr = 0;
328 for (j = 0; j < 8; j++) { 404 for (j = 0; j < 8; j++) {
329 inbit = 8 * k + j; 405 inbit = 8 * k + j;
330 if (i & bits8[j]) { 406 if (i & bits8[j]) {
407#if USE_ip_mask
331 obit = init_perm[inbit]; 408 obit = init_perm[inbit];
332 if (obit < 32) 409 if (obit < 32)
333 il |= bits32[obit]; 410 il |= bits32[obit];
334 else 411 else
335 ir |= bits32[obit - 32]; 412 ir |= bits32[obit - 32];
413#endif
336 obit = final_perm[inbit]; 414 obit = final_perm[inbit];
337 if (obit < 32) 415 if (obit < 32)
338 fl |= bits32[obit]; 416 fl |= bits32[obit];
@@ -340,8 +418,10 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
340 fr |= bits32[obit - 32]; 418 fr |= bits32[obit - 32];
341 } 419 }
342 } 420 }
421#if USE_ip_mask
343 ip_maskl[k][i] = il; 422 ip_maskl[k][i] = il;
344 ip_maskr[k][i] = ir; 423 ip_maskr[k][i] = ir;
424#endif
345 fp_maskl[k][i] = fl; 425 fp_maskl[k][i] = fl;
346 fp_maskr[k][i] = fr; 426 fp_maskr[k][i] = fr;
347 } 427 }
@@ -409,7 +489,7 @@ setup_salt(struct des_ctx *ctx, uint32_t salt)
409 uint32_t obit, saltbit; 489 uint32_t obit, saltbit;
410 int i; 490 int i;
411 491
412#if WANT_REPETITIVE_SPEEDUP 492#if USE_REPETITIVE_SPEEDUP
413 if (salt == old_salt) 493 if (salt == old_salt)
414 return; 494 return;
415 old_salt = salt; 495 old_salt = salt;
@@ -435,7 +515,7 @@ des_setkey(struct des_ctx *ctx, const char *key)
435 rawkey0 = ntohl(*(const uint32_t *) key); 515 rawkey0 = ntohl(*(const uint32_t *) key);
436 rawkey1 = ntohl(*(const uint32_t *) (key + 4)); 516 rawkey1 = ntohl(*(const uint32_t *) (key + 4));
437 517
438#if WANT_REPETITIVE_SPEEDUP 518#if USE_REPETITIVE_SPEEDUP
439 if ((rawkey0 | rawkey1) 519 if ((rawkey0 | rawkey1)
440 && rawkey0 == old_rawkey0 520 && rawkey0 == old_rawkey0
441 && rawkey1 == old_rawkey1 521 && rawkey1 == old_rawkey1
@@ -453,7 +533,7 @@ des_setkey(struct des_ctx *ctx, const char *key)
453#endif 533#endif
454 534
455 /* 535 /*
456 * Do key permutation and split into two 28-bit subkeys. 536 * Do key permutation and split into two 28-bit subkeys.
457 */ 537 */
458 k0 = key_perm_maskl[0][rawkey0 >> 25] 538 k0 = key_perm_maskl[0][rawkey0 >> 25]
459 | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f] 539 | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f]
@@ -472,7 +552,7 @@ des_setkey(struct des_ctx *ctx, const char *key)
472 | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f] 552 | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f]
473 | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f]; 553 | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f];
474 /* 554 /*
475 * Rotate subkeys and do compression permutation. 555 * Rotate subkeys and do compression permutation.
476 */ 556 */
477 shifts = 0; 557 shifts = 0;
478 for (round = 0; round < 16; round++) { 558 for (round = 0; round < 16; round++) {
@@ -483,7 +563,9 @@ des_setkey(struct des_ctx *ctx, const char *key)
483 t0 = (k0 << shifts) | (k0 >> (28 - shifts)); 563 t0 = (k0 << shifts) | (k0 >> (28 - shifts));
484 t1 = (k1 << shifts) | (k1 >> (28 - shifts)); 564 t1 = (k1 << shifts) | (k1 >> (28 - shifts));
485 565
486// de_keysl[15 - round] = 566#if USE_de_keys
567 de_keysl[15 - round] =
568#endif
487 en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] 569 en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f]
488 | comp_maskl[1][(t0 >> 14) & 0x7f] 570 | comp_maskl[1][(t0 >> 14) & 0x7f]
489 | comp_maskl[2][(t0 >> 7) & 0x7f] 571 | comp_maskl[2][(t0 >> 7) & 0x7f]
@@ -493,7 +575,9 @@ des_setkey(struct des_ctx *ctx, const char *key)
493 | comp_maskl[6][(t1 >> 7) & 0x7f] 575 | comp_maskl[6][(t1 >> 7) & 0x7f]
494 | comp_maskl[7][t1 & 0x7f]; 576 | comp_maskl[7][t1 & 0x7f];
495 577
496// de_keysr[15 - round] = 578#if USE_de_keys
579 de_keysr[15 - round] =
580#endif
497 en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] 581 en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f]
498 | comp_maskr[1][(t0 >> 14) & 0x7f] 582 | comp_maskr[1][(t0 >> 14) & 0x7f]
499 | comp_maskr[2][(t0 >> 7) & 0x7f] 583 | comp_maskr[2][(t0 >> 7) & 0x7f]
@@ -519,7 +603,9 @@ do_des(struct des_ctx *ctx, /*uint32_t l_in, uint32_t r_in,*/ uint32_t *l_out, u
519 int round; 603 int round;
520 604
521 /* Do initial permutation (IP). */ 605 /* Do initial permutation (IP). */
522#if 0 606#if USE_ip_mask
607 uint32_t l_in = 0;
608 uint32_t r_in = 0;
523 l = ip_maskl[0][l_in >> 24] 609 l = ip_maskl[0][l_in >> 24]
524 | ip_maskl[1][(l_in >> 16) & 0xff] 610 | ip_maskl[1][(l_in >> 16) & 0xff]
525 | ip_maskl[2][(l_in >> 8) & 0xff] 611 | ip_maskl[2][(l_in >> 8) & 0xff]
@@ -588,9 +674,8 @@ do_des(struct des_ctx *ctx, /*uint32_t l_in, uint32_t r_in,*/ uint32_t *l_out, u
588 r = l; 674 r = l;
589 l = f; 675 l = f;
590 } while (--count); 676 } while (--count);
591 /* 677
592 * Do final permutation (inverse of IP). 678 /* Do final permutation (inverse of IP). */
593 */
594 *l_out = fp_maskl[0][l >> 24] 679 *l_out = fp_maskl[0][l >> 24]
595 | fp_maskl[1][(l >> 16) & 0xff] 680 | fp_maskl[1][(l >> 16) & 0xff]
596 | fp_maskl[2][(l >> 8) & 0xff] 681 | fp_maskl[2][(l >> 8) & 0xff]
@@ -613,7 +698,8 @@ do_des(struct des_ctx *ctx, /*uint32_t l_in, uint32_t r_in,*/ uint32_t *l_out, u
613 698
614static char * 699static char *
615NOINLINE 700NOINLINE
616des_crypt(struct des_ctx *ctx, char output[21], const unsigned char *key, const unsigned char *setting) 701des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
702 const unsigned char *key, const unsigned char *setting)
617{ 703{
618 uint32_t salt, l, r0, r1, keybuf[2]; 704 uint32_t salt, l, r0, r1, keybuf[2];
619 uint8_t *p, *q; 705 uint8_t *p, *q;
@@ -679,7 +765,11 @@ des_crypt(struct des_ctx *ctx, char output[21], const unsigned char *key, const
679 return output; 765 return output;
680} 766}
681 767
682#undef WANT_REPETITIVE_SPEEDUP 768#undef USE_PRECOMPUTED_u_sbox
769#undef USE_REPETITIVE_SPEEDUP
770#undef USE_ip_mask
771#undef USE_de_keys
772
683#undef C 773#undef C
684#undef init_perm 774#undef init_perm
685#undef final_perm 775#undef final_perm
@@ -695,8 +785,8 @@ des_crypt(struct des_ctx *ctx, char output[21], const unsigned char *key, const
695#undef inv_key_perm 785#undef inv_key_perm
696#undef en_keysl 786#undef en_keysl
697#undef en_keysr 787#undef en_keysr
698//#undef de_keysl 788#undef de_keysl
699//#undef de_keysr 789#undef de_keysr
700#undef ip_maskl 790#undef ip_maskl
701#undef ip_maskr 791#undef ip_maskr
702#undef fp_maskl 792#undef fp_maskl