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