Debian Codegraph @

lib/hcrypto/des.c

raw
1 /*
2 * Copyright (c) 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 /**
35 * @page page_des DES - Data Encryption Standard crypto interface
36 *
37 * See the library functions here: @ref hcrypto_des
38 *
39 * DES was created by IBM, modififed by NSA and then adopted by NBS
40 * (now NIST) and published ad FIPS PUB 46 (updated by FIPS 46-1).
41 *
42 * Since the 19th May 2005 DES was withdrawn by NIST and should no
43 * longer be used. See @ref page_evp for replacement encryption
44 * algorithms and interfaces.
45 *
46 * Read more the iteresting history of DES on Wikipedia
47 * http://www.wikipedia.org/wiki/Data_Encryption_Standard .
48 *
49 * @section des_keygen DES key generation
50 *
51 * To generate a DES key safely you have to use the code-snippet
52 * below. This is because the DES_random_key() can fail with an
53 * abort() in case of and failure to start the random generator.
54 *
55 * There is a replacement function DES_new_random_key(), however that
56 * function does not exists in OpenSSL.
57 *
58 * @code
59 * DES_cblock key;
60 * do {
61 * if (RAND_rand(&key, sizeof(key)) != 1)
62 * goto failure;
63 * DES_set_odd_parity(key);
64 * } while (DES_is_weak_key(&key));
65 * @endcode
66 *
67 * @section des_impl DES implementation history
68 *
69 * There was no complete BSD licensed, fast, GPL compatible
70 * implementation of DES, so Love wrote the part that was missing,
71 * fast key schedule setup and adapted the interface to the orignal
72 * libdes.
73 *
74 * The document that got me started for real was "Efficient
75 * Implementation of the Data Encryption Standard" by Dag Arne Osvik.
76 * I never got to the PC1 transformation was working, instead I used
77 * table-lookup was used for all key schedule setup. The document was
78 * very useful since it de-mystified other implementations for me.
79 *
80 * The core DES function (SBOX + P transformation) is from Richard
81 * Outerbridge public domain DES implementation. My sanity is saved
82 * thanks to his work. Thank you Richard.
83 */
84
85 #include <config.h>
86 #include <roken.h>
87
88 #define HC_DEPRECATED
89 #include <krb5-types.h>
90 #include <assert.h>
91
92 #include "des.h"
93 #include "ui.h"
94
95 static void desx(uint32_t [2], DES_key_schedule *, int);
96 static void IP(uint32_t [2]);
97 static void FP(uint32_t [2]);
98
99 #include "des-tables.h"
100
101 #define ROTATE_LEFT28(x,one) \
102 if (one) { \
103 x = ( ((x)<<(1)) & 0xffffffe) | ((x) >> 27); \
104 } else { \
105 x = ( ((x)<<(2)) & 0xffffffc) | ((x) >> 26); \
106 }
107
108 /**
109 * Set the parity of the key block, used to generate a des key from a
110 * random key. See @ref des_keygen.
111 *
112 * @param key key to fixup the parity for.
113 * @ingroup hcrypto_des
114 */
115
116 void
117 DES_set_odd_parity(DES_cblock *key)
118 {
119 unsigned int i;
120 for (i = 0; i < DES_CBLOCK_LEN; i++)
121 (*key)[i] = odd_parity[(*key)[i]];
122 }
123
124 /**
125 * Check if the key have correct parity.
126 *
127 * @param key key to check the parity.
128 * @return 1 on success, 0 on failure.
129 * @ingroup hcrypto_des
130 */
131
132 int HC_DEPRECATED
133 DES_check_key_parity(DES_cblock *key)
134 {
135 unsigned int i;
136
137 for (i = 0; i < DES_CBLOCK_LEN; i++)
138 if ((*key)[i] != odd_parity[(*key)[i]])
139 return 0;
140 return 1;
141 }
142
143 /*
144 *
145 */
146
147 /* FIPS 74 */
148 static DES_cblock weak_keys[] = {
149 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}, /* weak keys */
150 {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
151 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
152 {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
153 {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE}, /* semi-weak keys */
154 {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
155 {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
156 {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
157 {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
158 {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
159 {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
160 {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
161 {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
162 {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
163 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
164 {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}
165 };
166
167 /**
168 * Checks if the key is any of the weaks keys that makes DES attacks
169 * trival.
170 *
171 * @param key key to check.
172 *
173 * @return 1 if the key is weak, 0 otherwise.
174 * @ingroup hcrypto_des
175 */
176
177 int
178 DES_is_weak_key(DES_cblock *key)
179 {
180 int weak = 0;
181 int i;
182
183 for (i = 0; i < sizeof(weak_keys)/sizeof(weak_keys[0]); i++)
184 weak ^= (ct_memcmp(weak_keys[i], key, DES_CBLOCK_LEN) == 0);
185
186 return !!weak;
187 }
188
189 /**
190 * Setup a des key schedule from a key. Deprecated function, use
191 * DES_set_key_unchecked() or DES_set_key_checked() instead.
192 *
193 * @param key a key to initialize the key schedule with.
194 * @param ks a key schedule to initialize.
195 *
196 * @return 0 on success
197 * @ingroup hcrypto_des
198 */
199
200 int HC_DEPRECATED
201 DES_set_key(DES_cblock *key, DES_key_schedule *ks)
202 {
203 return DES_set_key_checked(key, ks);
204 }
205
206 /**
207 * Setup a des key schedule from a key. The key is no longer needed
208 * after this transaction and can cleared.
209 *
210 * Does NOT check that the key is weak for or have wrong parity.
211 *
212 * @param key a key to initialize the key schedule with.
213 * @param ks a key schedule to initialize.
214 *
215 * @return 0 on success
216 * @ingroup hcrypto_des
217 */
218
219 int
220 DES_set_key_unchecked(DES_cblock *key, DES_key_schedule *ks)
221 {
222 uint32_t t1, t2;
223 uint32_t c, d;
224 int shifts[16] = { 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
225 uint32_t *k = &ks->ks[0];
226 int i;
227
228 t1 = (uint32_t)(*key)[0] << 24 | (*key)[1] << 16 | (*key)[2] << 8 | (*key)[3];
229 t2 = (uint32_t)(*key)[4] << 24 | (*key)[5] << 16 | (*key)[6] << 8 | (*key)[7];
230
231 c = (pc1_c_3[(t1 >> (5 )) & 0x7] << 3)
232 | (pc1_c_3[(t1 >> (5 + 8 )) & 0x7] << 2)
233 | (pc1_c_3[(t1 >> (5 + 8 + 8 )) & 0x7] << 1)
234 | (pc1_c_3[(t1 >> (5 + 8 + 8 + 8)) & 0x7] << 0)
235 | (pc1_c_4[(t2 >> (4 )) & 0xf] << 3)
236 | (pc1_c_4[(t2 >> (4 + 8 )) & 0xf] << 2)
237 | (pc1_c_4[(t2 >> (4 + 8 + 8 )) & 0xf] << 1)
238 | (pc1_c_4[(t2 >> (4 + 8 + 8 + 8)) & 0xf] << 0);
239
240
241 d = (pc1_d_3[(t2 >> (1 )) & 0x7] << 3)
242 | (pc1_d_3[(t2 >> (1 + 8 )) & 0x7] << 2)
243 | (pc1_d_3[(t2 >> (1 + 8 + 8 )) & 0x7] << 1)
244 | (pc1_d_3[(t2 >> (1 + 8 + 8 + 8)) & 0x7] << 0)
245 | (pc1_d_4[(t1 >> (1 )) & 0xf] << 3)
246 | (pc1_d_4[(t1 >> (1 + 8 )) & 0xf] << 2)
247 | (pc1_d_4[(t1 >> (1 + 8 + 8 )) & 0xf] << 1)
248 | (pc1_d_4[(t1 >> (1 + 8 + 8 + 8)) & 0xf] << 0);
249
250 for (i = 0; i < 16; i++) {
251 uint32_t kc, kd;
252
253 ROTATE_LEFT28(c, shifts[i]);
254 ROTATE_LEFT28(d, shifts[i]);
255
256 kc = pc2_c_1[(c >> 22) & 0x3f] |
257 pc2_c_2[((c >> 16) & 0x30) | ((c >> 15) & 0xf)] |
258 pc2_c_3[((c >> 9 ) & 0x3c) | ((c >> 8 ) & 0x3)] |
259 pc2_c_4[((c >> 2 ) & 0x20) | ((c >> 1) & 0x18) | (c & 0x7)];
260 kd = pc2_d_1[(d >> 22) & 0x3f] |
261 pc2_d_2[((d >> 15) & 0x30) | ((d >> 14) & 0xf)] |
262 pc2_d_3[ (d >> 7 ) & 0x3f] |
263 pc2_d_4[((d >> 1 ) & 0x3c) | ((d ) & 0x3)];
264
265 /* Change to byte order used by the S boxes */
266 *k = (kc & 0x00fc0000L) << 6;
267 *k |= (kc & 0x00000fc0L) << 10;
268 *k |= (kd & 0x00fc0000L) >> 10;
269 *k++ |= (kd & 0x00000fc0L) >> 6;
270 *k = (kc & 0x0003f000L) << 12;
271 *k |= (kc & 0x0000003fL) << 16;
272 *k |= (kd & 0x0003f000L) >> 4;
273 *k++ |= (kd & 0x0000003fL);
274 }
275
276 return 0;
277 }
278
279 /**
280 * Just like DES_set_key_unchecked() except checking that the key is
281 * not weak for or have correct parity.
282 *
283 * @param key a key to initialize the key schedule with.
284 * @param ks a key schedule to initialize.
285 *
286 * @return 0 on success, -1 on invalid parity, -2 on weak key.
287 * @ingroup hcrypto_des
288 */
289
290 int
291 DES_set_key_checked(DES_cblock *key, DES_key_schedule *ks)
292 {
293 if (!DES_check_key_parity(key)) {
294 memset(ks, 0, sizeof(*ks));
295 return -1;
296 }
297 if (DES_is_weak_key(key)) {
298 memset(ks, 0, sizeof(*ks));
299 return -2;
300 }
301 return DES_set_key_unchecked(key, ks);
302 }
303
304 /**
305 * Compatibility function for eay libdes, works just like
306 * DES_set_key_checked().
307 *
308 * @param key a key to initialize the key schedule with.
309 * @param ks a key schedule to initialize.
310 *
311 * @return 0 on success, -1 on invalid parity, -2 on weak key.
312 * @ingroup hcrypto_des
313 */
314
315 int
316 DES_key_sched(DES_cblock *key, DES_key_schedule *ks)
317 {
318 return DES_set_key_checked(key, ks);
319 }
320
321 /*
322 *
323 */
324
325 static void
326 load(const unsigned char *b, uint32_t v[2])
327 {
328 v[0] = (uint32_t)b[0] << 24U;
329 v[0] |= b[1] << 16U;
330 v[0] |= b[2] << 8U;
331 v[0] |= b[3] << 0U;
332 v[1] = (uint32_t)b[4] << 24U;
333 v[1] |= b[5] << 16U;
334 v[1] |= b[6] << 8U;
335 v[1] |= b[7] << 0U;
336 }
337
338 static void
339 store(const uint32_t v[2], unsigned char *b)
340 {
341 b[0] = (v[0] >> 24) & 0xffU;
342 b[1] = (v[0] >> 16) & 0xffU;
343 b[2] = (v[0] >> 8) & 0xffU;
344 b[3] = (v[0] >> 0) & 0xffU;
345 b[4] = (v[1] >> 24) & 0xffU;
346 b[5] = (v[1] >> 16) & 0xffU;
347 b[6] = (v[1] >> 8) & 0xffU;
348 b[7] = (v[1] >> 0) & 0xffU;
349 }
350
351 /**
352 * Encrypt/decrypt a block using DES. Also called ECB mode
353 *
354 * @param u data to encrypt
355 * @param ks key schedule to use
356 * @param encp if non zero, encrypt. if zero, decrypt.
357 *
358 * @ingroup hcrypto_des
359 */
360
361 void
362 DES_encrypt(uint32_t u[2], DES_key_schedule *ks, int encp)
363 {
364 IP(u);
365 desx(u, ks, encp);
366 FP(u);
367 }
368
369 /**
370 * Encrypt/decrypt a block using DES.
371 *
372 * @param input data to encrypt
373 * @param output data to encrypt
374 * @param ks key schedule to use
375 * @param encp if non zero, encrypt. if zero, decrypt.
376 *
377 * @ingroup hcrypto_des
378 */
379
380 void
381 DES_ecb_encrypt(DES_cblock *input, DES_cblock *output,
382 DES_key_schedule *ks, int encp)
383 {
384 uint32_t u[2];
385 load(*input, u);
386 DES_encrypt(u, ks, encp);
387 store(u, *output);
388 }
389
390 /**
391 * Encrypt/decrypt a block using DES in Chain Block Cipher mode (cbc).
392 *
393 * The IV must always be diffrent for diffrent input data blocks.
394 *
395 * @param in data to encrypt
396 * @param out data to encrypt
397 * @param length length of data
398 * @param ks key schedule to use
399 * @param iv initial vector to use
400 * @param encp if non zero, encrypt. if zero, decrypt.
401 *
402 * @ingroup hcrypto_des
403 */
404
405 void
406 DES_cbc_encrypt(const void *in, void *out, long length,
407 DES_key_schedule *ks, DES_cblock *iv, int encp)
408 {
409 const unsigned char *input = in;
410 unsigned char *output = out;
411 uint32_t u[2];
412 uint32_t uiv[2];
413
414 load(*iv, uiv);
415
416 if (encp) {
417 while (length >= DES_CBLOCK_LEN) {
418 load(input, u);
419 u[0] ^= uiv[0]; u[1] ^= uiv[1];
420 DES_encrypt(u, ks, 1);
421 uiv[0] = u[0]; uiv[1] = u[1];
422 store(u, output);
423
424 length -= DES_CBLOCK_LEN;
425 input += DES_CBLOCK_LEN;
426 output += DES_CBLOCK_LEN;
427 }
428 if (length) {
429 unsigned char tmp[DES_CBLOCK_LEN];
430 memcpy(tmp, input, length);
431 memset(tmp + length, 0, DES_CBLOCK_LEN - length);
432 load(tmp, u);
433 u[0] ^= uiv[0]; u[1] ^= uiv[1];
434 DES_encrypt(u, ks, 1);
435 store(u, output);
436 }
437 } else {
438 uint32_t t[2];
439 while (length >= DES_CBLOCK_LEN) {
440 load(input, u);
441 t[0] = u[0]; t[1] = u[1];
442 DES_encrypt(u, ks, 0);
443 u[0] ^= uiv[0]; u[1] ^= uiv[1];
444 store(u, output);
445 uiv[0] = t[0]; uiv[1] = t[1];
446
447 length -= DES_CBLOCK_LEN;
448 input += DES_CBLOCK_LEN;
449 output += DES_CBLOCK_LEN;
450 }
451 if (length) {
452 unsigned char tmp[DES_CBLOCK_LEN];
453 memcpy(tmp, input, length);
454 memset(tmp + length, 0, DES_CBLOCK_LEN - length);
455 load(tmp, u);
456 DES_encrypt(u, ks, 0);
457 u[0] ^= uiv[0]; u[1] ^= uiv[1];
458 store(u, output);
459 }
460 }
461 uiv[0] = 0; u[0] = 0; uiv[1] = 0; u[1] = 0;
462 }
463
464 /**
465 * Encrypt/decrypt a block using DES in Propagating Cipher Block
466 * Chaining mode. This mode is only used for Kerberos 4, and it should
467 * stay that way.
468 *
469 * The IV must always be diffrent for diffrent input data blocks.
470 *
471 * @param in data to encrypt
472 * @param out data to encrypt
473 * @param length length of data
474 * @param ks key schedule to use
475 * @param iv initial vector to use
476 * @param encp if non zero, encrypt. if zero, decrypt.
477 *
478 * @ingroup hcrypto_des
479 */
480
481 void
482 DES_pcbc_encrypt(const void *in, void *out, long length,
483 DES_key_schedule *ks, DES_cblock *iv, int encp)
484 {
485 const unsigned char *input = in;
486 unsigned char *output = out;
487 uint32_t u[2];
488 uint32_t uiv[2];
489
490 load(*iv, uiv);
491
492 if (encp) {
493 uint32_t t[2];
494 while (length >= DES_CBLOCK_LEN) {
495 load(input, u);
496 t[0] = u[0]; t[1] = u[1];
497 u[0] ^= uiv[0]; u[1] ^= uiv[1];
498 DES_encrypt(u, ks, 1);
499 uiv[0] = u[0] ^ t[0]; uiv[1] = u[1] ^ t[1];
500 store(u, output);
501
502 length -= DES_CBLOCK_LEN;
503 input += DES_CBLOCK_LEN;
504 output += DES_CBLOCK_LEN;
505 }
506 if (length) {
507 unsigned char tmp[DES_CBLOCK_LEN];
508 memcpy(tmp, input, length);
509 memset(tmp + length, 0, DES_CBLOCK_LEN - length);
510 load(tmp, u);
511 u[0] ^= uiv[0]; u[1] ^= uiv[1];
512 DES_encrypt(u, ks, 1);
513 store(u, output);
514 }
515 } else {
516 uint32_t t[2];
517 while (length >= DES_CBLOCK_LEN) {
518 load(input, u);
519 t[0] = u[0]; t[1] = u[1];
520 DES_encrypt(u, ks, 0);
521 u[0] ^= uiv[0]; u[1] ^= uiv[1];
522 store(u, output);
523 uiv[0] = t[0] ^ u[0]; uiv[1] = t[1] ^ u[1];
524
525 length -= DES_CBLOCK_LEN;
526 input += DES_CBLOCK_LEN;
527 output += DES_CBLOCK_LEN;
528 }
529 if (length) {
530 unsigned char tmp[DES_CBLOCK_LEN];
531 memcpy(tmp, input, length);
532 memset(tmp + length, 0, DES_CBLOCK_LEN - length);
533 load(tmp, u);
534 DES_encrypt(u, ks, 0);
535 u[0] ^= uiv[0]; u[1] ^= uiv[1];
536 }
537 }
538 uiv[0] = 0; u[0] = 0; uiv[1] = 0; u[1] = 0;
539 }
540
541 /*
542 *
543 */
544
545 static void
546 _des3_encrypt(uint32_t u[2], DES_key_schedule *ks1, DES_key_schedule *ks2,
547 DES_key_schedule *ks3, int encp)
548 {
549 IP(u);
550 if (encp) {
551 desx(u, ks1, 1); /* IP + FP cancel out each other */
552 desx(u, ks2, 0);
553 desx(u, ks3, 1);
554 } else {
555 desx(u, ks3, 0);
556 desx(u, ks2, 1);
557 desx(u, ks1, 0);
558 }
559 FP(u);
560 }
561
562 /**
563 * Encrypt/decrypt a block using triple DES using EDE mode,
564 * encrypt/decrypt/encrypt.
565 *
566 * @param input data to encrypt
567 * @param output data to encrypt
568 * @param ks1 key schedule to use
569 * @param ks2 key schedule to use
570 * @param ks3 key schedule to use
571 * @param encp if non zero, encrypt. if zero, decrypt.
572 *
573 * @ingroup hcrypto_des
574 */
575
576 void
577 DES_ecb3_encrypt(DES_cblock *input,
578 DES_cblock *output,
579 DES_key_schedule *ks1,
580 DES_key_schedule *ks2,
581 DES_key_schedule *ks3,
582 int encp)
583 {
584 uint32_t u[2];
585 load(*input, u);
586 _des3_encrypt(u, ks1, ks2, ks3, encp);
587 store(u, *output);
588 return;
589 }
590
591 /**
592 * Encrypt/decrypt using Triple DES in Chain Block Cipher mode (cbc).
593 *
594 * The IV must always be diffrent for diffrent input data blocks.
595 *
596 * @param in data to encrypt
597 * @param out data to encrypt
598 * @param length length of data
599 * @param ks1 key schedule to use
600 * @param ks2 key schedule to use
601 * @param ks3 key schedule to use
602 * @param iv initial vector to use
603 * @param encp if non zero, encrypt. if zero, decrypt.
604 *
605 * @ingroup hcrypto_des
606 */
607
608 void
609 DES_ede3_cbc_encrypt(const void *in, void *out,
610 long length, DES_key_schedule *ks1,
611 DES_key_schedule *ks2, DES_key_schedule *ks3,
612 DES_cblock *iv, int encp)
613 {
614 const unsigned char *input = in;
615 unsigned char *output = out;
616 uint32_t u[2];
617 uint32_t uiv[2];
618
619 load(*iv, uiv);
620
621 if (encp) {
622 while (length >= DES_CBLOCK_LEN) {
623 load(input, u);
624 u[0] ^= uiv[0]; u[1] ^= uiv[1];
625 _des3_encrypt(u, ks1, ks2, ks3, 1);
626 uiv[0] = u[0]; uiv[1] = u[1];
627 store(u, output);
628
629 length -= DES_CBLOCK_LEN;
630 input += DES_CBLOCK_LEN;
631 output += DES_CBLOCK_LEN;
632 }
633 if (length) {
634 unsigned char tmp[DES_CBLOCK_LEN];
635 memcpy(tmp, input, length);
636 memset(tmp + length, 0, DES_CBLOCK_LEN - length);
637 load(tmp, u);
638 u[0] ^= uiv[0]; u[1] ^= uiv[1];
639 _des3_encrypt(u, ks1, ks2, ks3, 1);
640 store(u, output);
641 }
642 } else {
643 uint32_t t[2];
644 while (length >= DES_CBLOCK_LEN) {
645 load(input, u);
646 t[0] = u[0]; t[1] = u[1];
647 _des3_encrypt(u, ks1, ks2, ks3, 0);
648 u[0] ^= uiv[0]; u[1] ^= uiv[1];
649 store(u, output);
650 uiv[0] = t[0]; uiv[1] = t[1];
651
652 length -= DES_CBLOCK_LEN;
653 input += DES_CBLOCK_LEN;
654 output += DES_CBLOCK_LEN;
655 }
656 if (length) {
657 unsigned char tmp[DES_CBLOCK_LEN];
658 memcpy(tmp, input, length);
659 memset(tmp + length, 0, DES_CBLOCK_LEN - length);
660 load(tmp, u);
661 _des3_encrypt(u, ks1, ks2, ks3, 0);
662 u[0] ^= uiv[0]; u[1] ^= uiv[1];
663 store(u, output);
664 }
665 }
666 store(uiv, *iv);
667 uiv[0] = 0; u[0] = 0; uiv[1] = 0; u[1] = 0;
668 }
669
670 /**
671 * Encrypt/decrypt using DES in cipher feedback mode with 64 bit
672 * feedback.
673 *
674 * The IV must always be diffrent for diffrent input data blocks.
675 *
676 * @param in data to encrypt
677 * @param out data to encrypt
678 * @param length length of data
679 * @param ks key schedule to use
680 * @param iv initial vector to use
681 * @param num offset into in cipher block encryption/decryption stop last time.
682 * @param encp if non zero, encrypt. if zero, decrypt.
683 *
684 * @ingroup hcrypto_des
685 */
686
687 void
688 DES_cfb64_encrypt(const void *in, void *out,
689 long length, DES_key_schedule *ks, DES_cblock *iv,
690 int *num, int encp)
691 {
692 const unsigned char *input = in;
693 unsigned char *output = out;
694 unsigned char tmp[DES_CBLOCK_LEN];
695 uint32_t uiv[2];
696
697 load(*iv, uiv);
698
699 assert(*num >= 0 && *num < DES_CBLOCK_LEN);
700
701 if (encp) {
702 int i = *num;
703
704 while (length > 0) {
705 if (i == 0)
706 DES_encrypt(uiv, ks, 1);
707 store(uiv, tmp);
708 for (; i < DES_CBLOCK_LEN && i < length; i++) {
709 output[i] = tmp[i] ^ input[i];
710 }
711 if (i == DES_CBLOCK_LEN)
712 load(output, uiv);
713 output += i;
714 input += i;
715 length -= i;
716 if (i == DES_CBLOCK_LEN)
717 i = 0;
718 }
719 store(uiv, *iv);
720 *num = i;
721 } else {
722 int i = *num;
723 unsigned char c;
724
725 while (length > 0) {
726 if (i == 0) {
727 DES_encrypt(uiv, ks, 1);
728 store(uiv, tmp);
729 }
730 for (; i < DES_CBLOCK_LEN && i < length; i++) {
731 c = input[i];
732 output[i] = tmp[i] ^ input[i];
733 (*iv)[i] = c;
734 }
735 output += i;
736 input += i;
737 length -= i;
738 if (i == DES_CBLOCK_LEN) {
739 i = 0;
740 load(*iv, uiv);
741 }
742 }
743 store(uiv, *iv);
744 *num = i;
745 }
746 }
747
748 /**
749 * Crete a checksum using DES in CBC encryption mode. This mode is
750 * only used for Kerberos 4, and it should stay that way.
751 *
752 * The IV must always be diffrent for diffrent input data blocks.
753 *
754 * @param in data to checksum
755 * @param output the checksum
756 * @param length length of data
757 * @param ks key schedule to use
758 * @param iv initial vector to use
759 *
760 * @ingroup hcrypto_des
761 */
762
763 uint32_t
764 DES_cbc_cksum(const void *in, DES_cblock *output,
765 long length, DES_key_schedule *ks, DES_cblock *iv)
766 {
767 const unsigned char *input = in;
768 uint32_t uiv[2];
769 uint32_t u[2] = { 0, 0 };
770
771 load(*iv, uiv);
772
773 while (length >= DES_CBLOCK_LEN) {
774 load(input, u);
775 u[0] ^= uiv[0]; u[1] ^= uiv[1];
776 DES_encrypt(u, ks, 1);
777 uiv[0] = u[0]; uiv[1] = u[1];
778
779 length -= DES_CBLOCK_LEN;
780 input += DES_CBLOCK_LEN;
781 }
782 if (length) {
783 unsigned char tmp[DES_CBLOCK_LEN];
784 memcpy(tmp, input, length);
785 memset(tmp + length, 0, DES_CBLOCK_LEN - length);
786 load(tmp, u);
787 u[0] ^= uiv[0]; u[1] ^= uiv[1];
788 DES_encrypt(u, ks, 1);
789 }
790 if (output)
791 store(u, *output);
792
793 uiv[0] = 0; u[0] = 0; uiv[1] = 0;
794 return u[1];
795 }
796
797 /*
798 *
799 */
800
801 static unsigned char
802 bitswap8(unsigned char b)
803 {
804 unsigned char r = 0;
805 int i;
806 for (i = 0; i < 8; i++) {
807 r = r << 1 | (b & 1);
808 b = b >> 1;
809 }
810 return r;
811 }
812
813 /**
814 * Convert a string to a DES key. Use something like
815 * PKCS5_PBKDF2_HMAC_SHA1() to create key from passwords.
816 *
817 * @param str The string to convert to a key
818 * @param key the resulting key
819 *
820 * @ingroup hcrypto_des
821 */
822
823 void
824 DES_string_to_key(const char *str, DES_cblock *key)
825 {
826 const unsigned char *s;
827 unsigned char *k;
828 DES_key_schedule ks;
829 size_t i, len;
830
831 memset(key, 0, sizeof(*key));
832 k = *key;
833 s = (const unsigned char *)str;
834
835 len = strlen(str);
836 for (i = 0; i < len; i++) {
837 if ((i % 16) < 8)
838 k[i % 8] ^= s[i] << 1;
839 else
840 k[7 - (i % 8)] ^= bitswap8(s[i]);
841 }
842 DES_set_odd_parity(key);
843 if (DES_is_weak_key(key))
844 k[7] ^= 0xF0;
845 DES_set_key(key, &ks);
846 DES_cbc_cksum(s, key, len, &ks, key);
847 memset(&ks, 0, sizeof(ks));
848 DES_set_odd_parity(key);
849 if (DES_is_weak_key(key))
850 k[7] ^= 0xF0;
851 }
852
853 /**
854 * Read password from prompt and create a DES key. Internal uses
855 * DES_string_to_key(). Really, go use a really string2key function
856 * like PKCS5_PBKDF2_HMAC_SHA1().
857 *
858 * @param key key to convert to
859 * @param prompt prompt to display user
860 * @param verify prompt twice.
861 *
862 * @return 1 on success, non 1 on failure.
863 */
864
865 int
866 DES_read_password(DES_cblock *key, char *prompt, int verify)
867 {
868 char buf[512];
869 int ret;
870
871 ret = UI_UTIL_read_pw_string(buf, sizeof(buf) - 1, prompt, verify);
872 if (ret == 1)
873 DES_string_to_key(buf, key);
874 return ret;
875 }
876
877 /*
878 *
879 */
880
881
882 void
883 _DES_ipfp_test(void)
884 {
885 DES_cblock k = "\x01\x02\x04\x08\x10\x20\x40\x80", k2;
886 uint32_t u[2] = { 1, 0 };
887 IP(u);
888 FP(u);
889 IP(u);
890 FP(u);
891 if (u[0] != 1 || u[1] != 0)
892 abort();
893
894 load(k, u);
895 store(u, k2);
896 if (memcmp(k, k2, 8) != 0)
897 abort();
898 }
899
900 /* D3DES (V5.09) -
901 *
902 * A portable, public domain, version of the Data Encryption Standard.
903 *
904 * Written with Symantec's THINK (Lightspeed) C by Richard Outerbridge.
905 * Thanks to: Dan Hoey for his excellent Initial and Inverse permutation
906 * code; Jim Gillogly & Phil Karn for the DES key schedule code; Dennis
907 * Ferguson, Eric Young and Dana How for comparing notes; and Ray Lau,
908 * for humouring me on.
909 *
910 * Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge.
911 * (GEnie : OUTER; CIS : [71755,204]) Graven Imagery, 1992.
912 */
913
914 static uint32_t SP1[64] = {
915 0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
916 0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
917 0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
918 0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
919 0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
920 0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
921 0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
922 0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
923 0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
924 0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
925 0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
926 0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
927 0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
928 0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
929 0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
930 0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };
931
932 static uint32_t SP2[64] = {
933 0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
934 0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
935 0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
936 0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
937 0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
938 0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
939 0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
940 0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
941 0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
942 0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
943 0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
944 0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
945 0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
946 0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
947 0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
948 0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };
949
950 static uint32_t SP3[64] = {
951 0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
952 0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
953 0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
954 0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
955 0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
956 0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
957 0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
958 0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
959 0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
960 0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
961 0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
962 0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
963 0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
964 0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
965 0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
966 0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };
967
968 static uint32_t SP4[64] = {
969 0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
970 0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
971 0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
972 0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
973 0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
974 0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
975 0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
976 0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
977 0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
978 0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
979 0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
980 0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
981 0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
982 0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
983 0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
984 0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };
985
986 static uint32_t SP5[64] = {
987 0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
988 0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
989 0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
990 0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
991 0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
992 0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
993 0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
994 0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
995 0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
996 0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,
997 0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,
998 0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,
999 0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,
1000 0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,
1001 0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,
1002 0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L };
1003
1004 static uint32_t SP6[64] = {
1005 0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,
1006 0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,
1007 0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,
1008 0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,
1009 0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,
1010 0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,
1011 0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,
1012 0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,
1013 0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,
1014 0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,
1015 0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,
1016 0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,
1017 0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,
1018 0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,
1019 0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,
1020 0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L };
1021
1022 static uint32_t SP7[64] = {
1023 0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,
1024 0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,
1025 0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,
1026 0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,
1027 0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,
1028 0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,
1029 0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,
1030 0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,
1031 0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,
1032 0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,
1033 0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,
1034 0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,
1035 0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,
1036 0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,
1037 0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,
1038 0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };
1039
1040 static uint32_t SP8[64] = {
1041 0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,
1042 0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,
1043 0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,
1044 0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,
1045 0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,
1046 0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,
1047 0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,
1048 0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,
1049 0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,
1050 0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,
1051 0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,
1052 0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,
1053 0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,
1054 0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,
1055 0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,
1056 0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };
1057
1058 static void
1059 IP(uint32_t v[2])
1060 {
1061 uint32_t work;
1062
1063 work = ((v[0] >> 4) ^ v[1]) & 0x0f0f0f0fL;
1064 v[1] ^= work;
1065 v[0] ^= (work << 4);
1066 work = ((v[0] >> 16) ^ v[1]) & 0x0000ffffL;
1067 v[1] ^= work;
1068 v[0] ^= (work << 16);
1069 work = ((v[1] >> 2) ^ v[0]) & 0x33333333L;
1070 v[0] ^= work;
1071 v[1] ^= (work << 2);
1072 work = ((v[1] >> 8) ^ v[0]) & 0x00ff00ffL;
1073 v[0] ^= work;
1074 v[1] ^= (work << 8);
1075 v[1] = ((v[1] << 1) | ((v[1] >> 31) & 1L)) & 0xffffffffL;
1076 work = (v[0] ^ v[1]) & 0xaaaaaaaaL;
1077 v[0] ^= work;
1078 v[1] ^= work;
1079 v[0] = ((v[0] << 1) | ((v[0] >> 31) & 1L)) & 0xffffffffL;
1080 }
1081
1082 static void
1083 FP(uint32_t v[2])
1084 {
1085 uint32_t work;
1086
1087 v[0] = (v[0] << 31) | (v[0] >> 1);
1088 work = (v[1] ^ v[0]) & 0xaaaaaaaaL;
1089 v[1] ^= work;
1090 v[0] ^= work;
1091 v[1] = (v[1] << 31) | (v[1] >> 1);
1092 work = ((v[1] >> 8) ^ v[0]) & 0x00ff00ffL;
1093 v[0] ^= work;
1094 v[1] ^= (work << 8);
1095 work = ((v[1] >> 2) ^ v[0]) & 0x33333333L;
1096 v[0] ^= work;
1097 v[1] ^= (work << 2);
1098 work = ((v[0] >> 16) ^ v[1]) & 0x0000ffffL;
1099 v[1] ^= work;
1100 v[0] ^= (work << 16);
1101 work = ((v[0] >> 4) ^ v[1]) & 0x0f0f0f0fL;
1102 v[1] ^= work;
1103 v[0] ^= (work << 4);
1104 }
1105
1106 static void
1107 desx(uint32_t block[2], DES_key_schedule *ks, int encp)
1108 {
1109 uint32_t *keys;
1110 uint32_t fval, work, right, left;
1111 int round;
1112
1113 left = block[0];
1114 right = block[1];
1115
1116 if (encp) {
1117 keys = &ks->ks[0];
1118
1119 for( round = 0; round < 8; round++ ) {
1120 work = (right << 28) | (right >> 4);
1121 work ^= *keys++;
1122 fval = SP7[ work & 0x3fL];
1123 fval |= SP5[(work >> 8) & 0x3fL];
1124 fval |= SP3[(work >> 16) & 0x3fL];
1125 fval |= SP1[(work >> 24) & 0x3fL];
1126 work = right ^ *keys++;
1127 fval |= SP8[ work & 0x3fL];
1128 fval |= SP6[(work >> 8) & 0x3fL];
1129 fval |= SP4[(work >> 16) & 0x3fL];
1130 fval |= SP2[(work >> 24) & 0x3fL];
1131 left ^= fval;
1132 work = (left << 28) | (left >> 4);
1133 work ^= *keys++;
1134 fval = SP7[ work & 0x3fL];
1135 fval |= SP5[(work >> 8) & 0x3fL];
1136 fval |= SP3[(work >> 16) & 0x3fL];
1137 fval |= SP1[(work >> 24) & 0x3fL];
1138 work = left ^ *keys++;
1139 fval |= SP8[ work & 0x3fL];
1140 fval |= SP6[(work >> 8) & 0x3fL];
1141 fval |= SP4[(work >> 16) & 0x3fL];
1142 fval |= SP2[(work >> 24) & 0x3fL];
1143 right ^= fval;
1144 }
1145 } else {
1146 keys = &ks->ks[30];
1147
1148 for( round = 0; round < 8; round++ ) {
1149 work = (right << 28) | (right >> 4);
1150 work ^= *keys++;
1151 fval = SP7[ work & 0x3fL];
1152 fval |= SP5[(work >> 8) & 0x3fL];
1153 fval |= SP3[(work >> 16) & 0x3fL];
1154 fval |= SP1[(work >> 24) & 0x3fL];
1155 work = right ^ *keys++;
1156 fval |= SP8[ work & 0x3fL];
1157 fval |= SP6[(work >> 8) & 0x3fL];
1158 fval |= SP4[(work >> 16) & 0x3fL];
1159 fval |= SP2[(work >> 24) & 0x3fL];
1160 left ^= fval;
1161 work = (left << 28) | (left >> 4);
1162 keys -= 4;
1163 work ^= *keys++;
1164 fval = SP7[ work & 0x3fL];
1165 fval |= SP5[(work >> 8) & 0x3fL];
1166 fval |= SP3[(work >> 16) & 0x3fL];
1167 fval |= SP1[(work >> 24) & 0x3fL];
1168 work = left ^ *keys++;
1169 fval |= SP8[ work & 0x3fL];
1170 fval |= SP6[(work >> 8) & 0x3fL];
1171 fval |= SP4[(work >> 16) & 0x3fL];
1172 fval |= SP2[(work >> 24) & 0x3fL];
1173 right ^= fval;
1174 keys -= 4;
1175 }
1176 }
1177 block[0] = right;
1178 block[1] = left;
1179 }