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