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