changed `struct fd_set' to `fd_set'
[heimdal.git] / lib / krb5 / str2key.c
blob8958c5678fa02df84d924c128daaaa531572181c
1 /*
2 * Copyright (c) 1997 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #include <krb5_locl.h>
41 RCSID("$Id$");
44 * Reverse 8 bytes
47 static void
48 reverse (unsigned char *s)
50 static const unsigned char tbl[] = {
51 0x0,
52 0x8,
53 0x4,
54 0xC,
55 0x2,
56 0xA,
57 0x6,
58 0xE,
59 0x1,
60 0x9,
61 0x5,
62 0xD,
63 0x3,
64 0xB,
65 0x7,
66 0xF
69 char tmp;
71 #define REVONE(str, i, j) \
72 do { tmp = str[i]; str[i] = str[j]; str[j] = tmp;} while(0)
74 REVONE(s,0,7);
75 REVONE(s,1,6);
76 REVONE(s,2,5);
77 REVONE(s,3,4);
78 #undef REVONE
80 #define REVTWO(q) \
81 q = (tbl[q & 0x0F] << 4) | (tbl[q >> 4])
83 REVTWO(s[0]);
84 REVTWO(s[1]);
85 REVTWO(s[2]);
86 REVTWO(s[3]);
87 REVTWO(s[4]);
88 REVTWO(s[5]);
89 REVTWO(s[6]);
90 REVTWO(s[7]);
92 #undef REVTWO
96 * A = A xor B. A & B is 8 bytes.
99 static void
100 xor (des_cblock *key, unsigned char *b)
102 unsigned char *a = (unsigned char*)key;
103 a[0] ^= b[0];
104 a[1] ^= b[1];
105 a[2] ^= b[2];
106 a[3] ^= b[3];
107 a[4] ^= b[4];
108 a[5] ^= b[5];
109 a[6] ^= b[6];
110 a[7] ^= b[7];
114 * Init a from b
117 static void
118 init (unsigned char *a, unsigned char *b)
120 a[0] = b[0] << 1;
121 a[1] = b[1] << 1;
122 a[2] = b[2] << 1;
123 a[3] = b[3] << 1;
124 a[4] = b[4] << 1;
125 a[5] = b[5] << 1;
126 a[6] = b[6] << 1;
127 a[7] = b[7] << 1;
130 static void
131 DES_string_to_key(const unsigned char *str, size_t len, des_cblock *key)
133 int odd, i;
134 des_key_schedule sched;
136 memset (key, 0, sizeof(des_cblock));
138 odd = 1;
139 for (i = 0; i < len; i += 8) {
140 unsigned char tmp[8];
142 init (tmp, (unsigned char*)&str[i]);
144 if (odd == 0) {
145 odd = 1;
146 reverse (tmp);
147 init (tmp, tmp);
148 } else
149 odd = 0;
150 xor (key, tmp);
152 des_set_odd_parity (key);
153 des_set_key (key, sched);
154 des_cbc_cksum ((des_cblock *)str, key, len, sched, key);
155 des_set_odd_parity (key);
156 if (des_is_weak_key (key))
157 xor (key, (unsigned char*)"\0\0\0\0\0\0\0\xf0");
160 static int
161 gcd(int a, int b)
164 int r = a % b;
165 a = b;
166 b = r;
167 }while(b);
168 return a;
172 static void
173 rr13(unsigned char *buf, size_t len)
175 unsigned char *tmp = malloc(len);
176 int i;
177 for(i = 0; i < len; i++){
178 int x = (buf[i] << 8) | buf[(i + 1) % len];
179 x >>= 5;
180 tmp[(i + 2) % len] = x & 0xff;
182 memcpy(buf, tmp, len);
183 free(tmp);
186 static void
187 add1(unsigned char *a, unsigned char *b, size_t len)
189 int i;
190 int carry = 0;
191 for(i = len - 1; i >= 0; i--){
192 int x = a[i] + b[i];
193 carry = x > 0xff;
194 a[i] = x & 0xff;
196 for(i = len - 1; carry && i >= 0; i--){
197 int x = a[i] + carry;
198 carry = x > 0xff;
199 a[i] = carry & 0xff;
203 static void
204 fold(const unsigned char *str, size_t len, unsigned char *out)
206 const int size = 24;
207 unsigned char key[24];
208 int num = 0;
209 int i;
210 int lcm = size * len / gcd(size, len);
211 unsigned char *tmp = malloc(lcm);
212 unsigned char *buf = malloc(len);
213 memcpy(buf, str, len);
214 for(; num < lcm; num += len){
215 memcpy(tmp + num, buf, len);
216 rr13(buf, len);
218 free(buf);
219 memset(key, 0, sizeof(key));
220 for(i = 0; i < lcm; i += size)
221 add1(key, tmp + i, size);
223 memcpy(out, key, size);
226 static void
227 DES3_string_to_key(const unsigned char *str, size_t len, des_cblock *keys)
229 unsigned char tmp[24];
230 des_cblock ivec;
231 des_key_schedule s[3];
232 int i;
234 fold(str, len, tmp);
235 for(i = 0; i < 3; i++){
236 memcpy(keys + i, tmp + 8 * i, 8);
237 des_set_odd_parity(keys + i);
238 if(des_is_weak_key(keys + i))
239 xor(keys + i, (unsigned char*)"\0\0\0\0\0\0\0\xf0");
240 des_set_key(keys + i, s[i]);
242 memset(&ivec, 0, sizeof(ivec));
243 des_ede3_cbc_encrypt((void*)tmp, (void*)tmp, sizeof(tmp),
244 s[0], s[1], s[2], &ivec, 1);
245 memset(s, 0, sizeof(s));
246 for(i = 0; i < 3; i++){
247 memcpy(keys + i, tmp + 8 * i, 8);
248 des_set_odd_parity(keys + i);
249 if(des_is_weak_key(keys + i))
250 xor(keys + i, (unsigned char*)"\0\0\0\0\0\0\0\xf0");
252 memset(tmp, 0, sizeof(tmp));
256 static krb5_error_code
257 string_to_key_internal (const unsigned char *str,
258 size_t str_len,
259 krb5_data *salt,
260 krb5_keytype ktype,
261 krb5_keyblock *key)
263 size_t len;
264 unsigned char *s, *p;
265 krb5_error_code ret;
267 len = str_len + salt->length;
268 #if 1
269 len = (len + 7) / 8 * 8;
270 #endif
271 p = s = malloc (len);
272 if (p == NULL)
273 return ENOMEM;
274 memset (s, 0, len);
275 strncpy ((char *)p, (char *)str, str_len);
276 p += str_len;
277 memcpy (p, salt->data, salt->length);
279 switch(ktype){
280 case KEYTYPE_DES:{
281 des_cblock tmpkey;
282 DES_string_to_key(s, len, &tmpkey);
283 ret = krb5_data_copy(&key->keyvalue, tmpkey, sizeof(des_cblock));
284 memset(&tmpkey, 0, sizeof(tmpkey));
285 break;
287 case KEYTYPE_DES3:{
288 des_cblock keys[3];
289 DES3_string_to_key(s, len, keys);
290 ret = krb5_data_copy(&key->keyvalue, keys, sizeof(keys));
291 memset(keys, 0, sizeof(keys));
292 break;
294 default:
295 ret = KRB5_PROG_KEYTYPE_NOSUPP;
296 break;
298 memset(s, 0, len);
299 free(s);
300 if(ret)
301 return ret;
302 key->keytype = ktype;
303 return 0;
306 krb5_error_code
307 krb5_string_to_key (const char *str,
308 krb5_data *salt,
309 krb5_keytype ktype,
310 krb5_keyblock *key)
312 return string_to_key_internal ((const unsigned char *)str,
313 strlen(str), salt, ktype, key);
316 krb5_error_code
317 krb5_string_to_key_data (krb5_data *str,
318 krb5_data *salt,
319 krb5_keytype ktype,
320 krb5_keyblock *key)
322 return string_to_key_internal (str->data, str->length, salt, ktype, key);
325 krb5_error_code
326 krb5_get_salt (krb5_principal princ,
327 krb5_data *salt)
329 size_t len;
330 int i;
331 krb5_error_code err;
332 char *p;
334 len = strlen(princ->realm);
335 for (i = 0; i < princ->name.name_string.len; ++i)
336 len += strlen(princ->name.name_string.val[i]);
337 err = krb5_data_alloc (salt, len);
338 if (err)
339 return err;
340 p = salt->data;
341 strncpy (p, princ->realm, strlen(princ->realm));
342 p += strlen(princ->realm);
343 for (i = 0; i < princ->name.name_string.len; ++i) {
344 strncpy (p,
345 princ->name.name_string.val[i],
346 strlen(princ->name.name_string.val[i]));
347 p += strlen(princ->name.name_string.val[i]);
349 return 0;