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