]> git.sur5r.net Git - openldap/blob - clients/ud/string_to_key.c
Fix format %x -> %lx in des_string_to_key
[openldap] / clients / ud / string_to_key.c
1 #include "portable.h"
2
3 #if defined(HAVE_KERBEROS) && !defined(openbsd)
4 /*
5  * $Source: /repo/OpenLDAP/pkg/ldap/clients/ud/string_to_key.c,v $
6  * $Author: kdz $
7  *
8  * Copyright 1985, 1986, 1987, 1988, 1989 by the Massachusetts Institute
9  * of Technology.
10  *
11  * For copying and distribution information, please see the file
12  * <mit-copyright.h>.
13  *
14  * These routines perform encryption and decryption using the DES
15  * private key algorithm, or else a subset of it-- fewer inner loops.
16  * (AUTH_DES_ITER defaults to 16, may be less.)
17  *
18  * Under U.S. law, this software may not be exported outside the US
19  * without license from the U.S. Commerce department.
20  *
21  * The key schedule is passed as an arg, as well as the cleartext or
22  * ciphertext.  The cleartext and ciphertext should be in host order.
23  *
24  * These routines form the library interface to the DES facilities.
25  *
26  *      spm     8/85    MIT project athena
27  */
28
29 #include <stdio.h>
30 #include <ac/krb.h>
31
32 #if defined( DEBUG ) && defined( HAVE_DES_DEBUG )
33 #define USE_DES_DEBUG
34 extern int des_debug;
35 #endif
36
37 extern void des_fixup_key_parity();
38
39 #ifndef HAVE_AFS_KERBEROS
40 #define WORLDPEACEINOURTIME
41 #endif
42
43 #if defined(WORLDPEACEINOURTIME) /* Use original, not ifs version */
44 #ifndef HAVE_KERBEROS_V
45 /*
46  * convert an arbitrary length string to a DES key
47  */
48 void
49 des_string_to_key( char *str, register des_cblock *key )
50 {
51     register char *in_str;
52     register unsigned temp,i;
53     register int j;
54     register long length;
55     static unsigned char *k_p;
56     static int forward;
57     register char *p_char;
58     static char k_char[64];
59     static des_key_schedule key_sked;
60     extern unsigned long des_cbc_cksum();
61
62     in_str = str;
63     forward = 1;
64     p_char = k_char;
65     length = strlen(str);
66
67     /* init key array for bits */
68     memset(k_char, 0, sizeof(k_char));
69
70 #ifdef USE_DES_DEBUG
71     if (des_debug)
72         fprintf(stdout,
73                 "\n\ninput str length = %d  string = %s\nstring = 0x ",
74                 length,str);
75 #endif
76
77     /* get next 8 bytes, strip parity, xor */
78     for (i = 1; i <= length; i++) {
79         /* get next input key byte */
80         temp = (unsigned int) *str++;
81 #ifdef USE_DES_DEBUG
82         if (des_debug)
83             fprintf(stdout,"%02x ",temp & 0xff);
84 #endif
85         /* loop through bits within byte, ignore parity */
86         for (j = 0; j <= 6; j++) {
87             if (forward)
88                 *p_char++ ^= (int) temp & 01;
89             else
90                 *--p_char ^= (int) temp & 01;
91             temp = temp >> 1;
92         } while (--j > 0);
93
94         /* check and flip direction */
95         if ((i%8) == 0)
96             forward = !forward;
97     }
98
99     /* now stuff into the key des_cblock, and force odd parity */
100     p_char = k_char;
101     k_p = (unsigned char *) key;
102
103     for (i = 0; i <= 7; i++) {
104         temp = 0;
105         for (j = 0; j <= 6; j++)
106             temp |= *p_char++ << (1+j);
107         *k_p++ = (unsigned char) temp;
108     }
109
110     /* fix key parity */
111     des_fixup_key_parity(key);
112
113     /* Now one-way encrypt it with the folded key */
114     (void) des_key_sched(key,key_sked);
115     (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
116     /* erase key_sked */
117     memset((char *)key_sked, 0, sizeof(key_sked));
118
119     /* now fix up key parity again */
120     des_fixup_key_parity(key);
121
122 #ifdef USE_DES_DEBUG
123     if (des_debug)
124         fprintf(stdout,
125                 "\nResulting string_to_key = 0x%lx 0x%lx\n",
126                 *((unsigned long *) key),
127                 *((unsigned long *) key+1));
128 #endif
129 }
130
131 #endif /* HAVE_KERBEROS_V */
132 #else /* Use ifs version */
133
134 #if 0
135 #include <stdio.h>
136     /* These two needed for rxgen output to work */
137 #include <sys/types.h>
138 #include <rx/xdr.h>
139 #include <afs/cellconfig.h>
140 #include <afs/auth.h>
141
142 #include "/usr/andy/kauth/kauth.h"
143 #include "/usr/andy/kauth/kautils.h"
144 #endif
145
146 /* This defines the Andrew string_to_key function.  It accepts a password
147    string as input and converts its via a one-way encryption algorithm to a DES
148    encryption key.  It is compatible with the original Andrew authentication
149    service password database. */
150
151 static void
152 Andrew_StringToKey(
153   char          *str,
154   char          *cell,                  /* cell for password */
155   des_cblock *key
156 )
157 {   char  password[8+1];                /* crypt is limited to 8 chars anyway */
158     int   i;
159     int   passlen;
160
161     memset(key, 0, sizeof(des_cblock));
162     memset(password, 0, sizeof(password));
163
164     strncpy (password, cell, 8);
165     passlen = strlen (str);
166     if (passlen > 8) passlen = 8;
167
168     for (i=0; i<passlen; i++)
169         password[i] = str[i] ^ cell[i];
170
171     for (i=0;i<8;i++)
172         if (password[i] == '\0') password[i] = 'X';
173
174     /* crypt only considers the first 8 characters of password but for some
175        reason returns eleven characters of result (plus the two salt chars). */
176     strncpy(key, crypt(password, "#~") + 2, sizeof(des_cblock));
177
178     /* parity is inserted into the LSB so leftshift each byte up one bit.  This
179        allows ascii characters with a zero MSB to retain as much significance
180        as possible. */
181     {   char *keybytes = (char *)key;
182         unsigned int temp;
183
184         for (i = 0; i < 8; i++) {
185             temp = (unsigned int) keybytes[i];
186             keybytes[i] = (unsigned char) (temp << 1);
187         }
188     }
189     des_fixup_key_parity (key);
190 }
191
192 static void
193 StringToKey(
194   char          *str,
195   char          *cell,                  /* cell for password */
196   des_cblock    *key
197 )
198 {   des_key_schedule schedule;
199     char temp_key[8];
200     char ivec[8];
201     char password[BUFSIZ];
202     int  passlen;
203
204     strncpy (password, str, sizeof(password));
205     if ((passlen = strlen (password)) < sizeof(password)-1)
206         strncat (password, cell, sizeof(password)-passlen);
207     if ((passlen = strlen(password)) > sizeof(password)) passlen = sizeof(password);
208
209     memcpy(ivec, "kerberos", 8);
210     memcpy(temp_key, "kerberos", 8);
211     des_fixup_key_parity (temp_key);
212     des_key_sched (temp_key, schedule);
213     des_cbc_cksum (password, ivec, passlen, schedule, ivec);
214
215     memcpy(temp_key, ivec, 8);
216     des_fixup_key_parity (temp_key);
217     des_key_sched (temp_key, schedule);
218     des_cbc_cksum (password, key, passlen, schedule, ivec);
219
220     des_fixup_key_parity (key);
221 }
222
223 void
224 ka_StringToKey (
225   char          *str,
226   char          *cell,                  /* cell for password */
227   des_cblock    *key
228 )
229 {   char  realm[REALM_SZ];
230
231 #if NOWAYOUTTODAY
232     long  code;
233 #if 0
234     code = ka_CellToRealm (cell, realm, 0/*local*/);
235 #endif
236     if (code) strcpy (realm, "");
237     else lcstring (realm, realm, sizeof(realm)); /* for backward compatibility */
238 #else
239         (void)strcpy(realm, cell);
240 #endif
241
242     if (strlen(str) > 8) StringToKey (str, realm, key);
243     else Andrew_StringToKey (str, realm, key);
244 }
245
246 /*
247  * convert an arbitrary length string to a DES key
248  */
249 int
250 des_string_to_key( char *str, register des_cblock *key )
251 {
252         /* NB: i should probably call routine to get local cell here */
253         ka_StringToKey(str, "umich.edu", key);
254         return 0;
255 }
256
257 #endif /* Use IFS Version */
258
259 #endif /* kerberos */