xref: /illumos-gate/usr/src/uts/common/gssapi/mechs/krb5/crypto/des/f_parity.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 
13 #include <des_int.h>
14 
15 /*
16  * des_fixup_key_parity: Forces odd parity per byte; parity is bits
17  *                       8,16,...64 in des order, implies 0, 8, 16, ...
18  *                       vax order.
19  */
20 #define smask(step) ((1<<step)-1)
21 #define pstep(x,step) (((x)&smask(step))^(((x)>>step)&smask(step)))
22 #define parity_char(x) pstep(pstep(pstep((x),4),2),1)
23 
24 void
25 mit_des_fixup_key_parity(key)
26      register mit_des_cblock key;
27 {
28     int i;
29     for (i=0; i<sizeof(mit_des_cblock); i++)
30       {
31 	key[i] &= 0xfe;
32 	key[i] |= 1^parity_char(key[i]);
33       }
34 
35     return;
36 }
37 
38 /*
39  * des_check_key_parity: returns true iff key has the correct des parity.
40  *                       See des_fix_key_parity for the definition of
41  *                       correct des parity.
42  */
43 int
44 mit_des_check_key_parity(key)
45      register mit_des_cblock key;
46 {
47     int i;
48 
49     for (i=0; i<sizeof(mit_des_cblock); i++)
50       {
51 	if((key[i] & 1) == parity_char(0xfe&key[i]))
52 	  {
53 	    return 0;
54 	  }
55       }
56 
57     return(1);
58 }
59 
60