warning-cleanup-20010414
[openafs.git] / src / des / strng_to_key.c
1 /*
2  * Copyright 1985, 1986, 1987, 1988, 1989 by the Massachusetts Institute
3  * of Technology.
4  *
5  * For copying and distribution information, please see the file
6  * <mit-cpyright.h>.
7  *
8  * These routines perform encryption and decryption using the DES
9  * private key algorithm, or else a subset of it-- fewer inner loops.
10  * (AUTH_DES_ITER defaults to 16, may be less.)
11  *
12  * Under U.S. law, this software may not be exported outside the US
13  * without license from the U.S. Commerce department.
14  *
15  * The key schedule is passed as an arg, as well as the cleartext or
16  * ciphertext.  The cleartext and ciphertext should be in host order.
17  *
18  * These routines form the library interface to the DES facilities.
19  *
20  *      spm     8/85    MIT project athena
21  */
22
23 #include <mit-cpyright.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <afs/param.h>
27 #include <des.h>
28 #include "des_internal.h"
29
30 extern int des_debug;
31 extern int des_debug_print();
32 extern void des_fixup_key_parity();
33 extern afs_uint32 des_cbc_cksum();
34
35 /* prototypes */
36 int des_key_sched(register des_cblock *k, des_key_schedule schedule);
37
38 /*
39  * convert an arbitrary length string to a DES key
40  */
41 void
42 des_string_to_key(str,key)
43     char *str;
44     register des_cblock *key;
45 {
46     register char *in_str;
47     register unsigned temp,i,j;
48     register afs_int32 length;
49     unsigned char *k_p;
50     int forward;
51     register char *p_char;
52     char k_char[64];
53     des_key_schedule key_sked;
54
55     in_str = str;
56     forward = 1;
57     p_char = k_char;
58     length = strlen(str);
59
60     /* init key array for bits */
61     bzero(k_char,sizeof(k_char));
62
63 #ifdef DEBUG
64     if (des_debug)
65         fprintf(stdout,
66                 "\n\ninput str length = %d  string = %s\nstring = 0x ",
67                 length,str);
68 #endif
69
70     /* get next 8 bytes, strip parity, xor */
71     for (i = 1; i <= length; i++) {
72         /* get next input key byte */
73         temp = (unsigned int) *str++;
74 #ifdef DEBUG
75         if (des_debug)
76             fprintf(stdout,"%02x ",temp & 0xff);
77 #endif
78         /* loop through bits within byte, ignore parity */
79         for (j = 0; j <= 6; j++) {
80             if (forward)
81                 *p_char++ ^= (int) temp & 01;
82             else
83                 *--p_char ^= (int) temp & 01;
84             temp = temp >> 1;
85         } while (--j > 0);
86
87         /* check and flip direction */
88         if ((i%8) == 0)
89             forward = !forward;
90     }
91
92     /* now stuff into the key des_cblock, and force odd parity */
93     p_char = k_char;
94     k_p = (unsigned char *) key;
95
96     for (i = 0; i <= 7; i++) {
97         temp = 0;
98         for (j = 0; j <= 6; j++)
99             temp |= *p_char++ << (1+j);
100         *k_p++ = (unsigned char) temp;
101     }
102
103     /* fix key parity */
104     des_fixup_key_parity(key);
105
106     /* Now one-way encrypt it with the folded key */
107     (void) des_key_sched(key,key_sked);
108     (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
109     /* erase key_sked */
110     bzero((char *)key_sked,sizeof(key_sked));
111
112     /* now fix up key parity again */
113     des_fixup_key_parity(key);
114
115     if (des_debug)
116         fprintf(stdout,
117                 "\nResulting string_to_key = 0x%x 0x%x\n",
118                 *((afs_uint32 *) key),
119                 *((afs_uint32 *) key+1));
120 }