afsconfig-and-rcsid-all-around-20010705
[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 <afs/param.h>
26 #include <afsconfig.h>
27
28 RCSID("$Header$");
29
30 #include <des.h>
31 #include "des_internal.h"
32 #if defined(HAVE_STRINGS_H)
33 #include <strings.h>
34 #endif
35 #if defined(HAVE_STRING_H)
36 #include <string.h>
37 #endif
38
39 extern int des_debug;
40 extern int des_debug_print();
41 extern void des_fixup_key_parity();
42 extern afs_uint32 des_cbc_cksum();
43
44 /* prototypes */
45 int des_key_sched(register des_cblock *k, des_key_schedule schedule);
46
47 /*
48  * convert an arbitrary length string to a DES key
49  */
50 void
51 des_string_to_key(str,key)
52     char *str;
53     register des_cblock *key;
54 {
55     register char *in_str;
56     register unsigned temp,i,j;
57     register afs_int32 length;
58     unsigned char *k_p;
59     int forward;
60     register char *p_char;
61     char k_char[64];
62     des_key_schedule key_sked;
63
64     in_str = str;
65     forward = 1;
66     p_char = k_char;
67     length = strlen(str);
68
69     /* init key array for bits */
70     bzero(k_char,sizeof(k_char));
71
72 #ifdef DEBUG
73     if (des_debug)
74         fprintf(stdout,
75                 "\n\ninput str length = %d  string = %s\nstring = 0x ",
76                 length,str);
77 #endif
78
79     /* get next 8 bytes, strip parity, xor */
80     for (i = 1; i <= length; i++) {
81         /* get next input key byte */
82         temp = (unsigned int) *str++;
83 #ifdef DEBUG
84         if (des_debug)
85             fprintf(stdout,"%02x ",temp & 0xff);
86 #endif
87         /* loop through bits within byte, ignore parity */
88         for (j = 0; j <= 6; j++) {
89             if (forward)
90                 *p_char++ ^= (int) temp & 01;
91             else
92                 *--p_char ^= (int) temp & 01;
93             temp = temp >> 1;
94         } while (--j > 0);
95
96         /* check and flip direction */
97         if ((i%8) == 0)
98             forward = !forward;
99     }
100
101     /* now stuff into the key des_cblock, and force odd parity */
102     p_char = k_char;
103     k_p = (unsigned char *) key;
104
105     for (i = 0; i <= 7; i++) {
106         temp = 0;
107         for (j = 0; j <= 6; j++)
108             temp |= *p_char++ << (1+j);
109         *k_p++ = (unsigned char) temp;
110     }
111
112     /* fix key parity */
113     des_fixup_key_parity(key);
114
115     /* Now one-way encrypt it with the folded key */
116     (void) des_key_sched(key,key_sked);
117     (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
118     /* erase key_sked */
119     bzero((char *)key_sked,sizeof(key_sked));
120
121     /* now fix up key parity again */
122     des_fixup_key_parity(key);
123
124     if (des_debug)
125         fprintf(stdout,
126                 "\nResulting string_to_key = 0x%lx 0x%lx\n",
127                 *((afs_uint32 *) key),
128                 *((afs_uint32 *) key+1));
129 }