1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * These routines check and fix parity of encryption keys for the DES
4 * algorithm.
5 *
6 * They are a replacement for routines in key_parity.c, that don't require
7 * the table building that they do.
8 *
9 * Mark Eichin -- Cygnus Support
10 */
11
12 #include "crypto_int.h"
13 #include "des_int.h"
14
15 #ifdef K5_BUILTIN_DES_KEY_PARITY
16
17 /*
18 * des_fixup_key_parity: Forces odd parity per byte; parity is bits
19 * 8,16,...64 in des order, implies 0, 8, 16, ...
20 * vax order.
21 */
22 #define smask(step) ((1<<step)-1)
23 #define pstep(x,step) (((x)&smask(step))^(((x)>>step)&smask(step)))
24 #define parity_char(x) pstep(pstep(pstep((x),4),2),1)
25
26 void
mit_des_fixup_key_parity(mit_des_cblock key)27 mit_des_fixup_key_parity(mit_des_cblock key)
28 {
29 unsigned int i;
30 for (i=0; i<sizeof(mit_des_cblock); i++)
31 {
32 key[i] &= 0xfe;
33 key[i] |= 1^parity_char(key[i]);
34 }
35
36 return;
37 }
38
39 #endif /* K5_BUILTIN_DES_KEY_PARITY */
40
41 #ifdef K5_BUILTIN_DES
42
43 /*
44 * des_check_key_parity: returns true iff key has the correct des parity.
45 * See des_fix_key_parity for the definition of
46 * correct des parity.
47 */
48 int
mit_des_check_key_parity(mit_des_cblock key)49 mit_des_check_key_parity(mit_des_cblock key)
50 {
51 unsigned int i;
52
53 for (i=0; i<sizeof(mit_des_cblock); i++)
54 {
55 if((key[i] & 1) == parity_char(0xfe&key[i]))
56 {
57 return 0;
58 }
59 }
60
61 return(1);
62 }
63
64 #endif /* K5_BUILTIN_DES */
65