No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / crypto.c
blob6bc50b9bf07328c6bf241f04127f6dcf2cb1e286
1 /*
2 * Copyright (c) 1997 - 2005 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. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
35 __RCSID("$Heimdal: crypto.c 22200 2007-12-07 13:48:01Z lha $"
36 "$NetBSD: crypto.c,v 1.14 2008/03/22 08:37:13 mlelstv Exp $");
38 #undef CRYPTO_DEBUG
39 #ifdef CRYPTO_DEBUG
40 static void krb5_crypto_debug(krb5_context, int, size_t, krb5_keyblock*);
41 #endif
44 struct key_data {
45 krb5_keyblock *key;
46 krb5_data *schedule;
49 struct key_usage {
50 unsigned usage;
51 struct key_data key;
54 struct krb5_crypto_data {
55 struct encryption_type *et;
56 struct key_data key;
57 int num_key_usage;
58 struct key_usage *key_usage;
61 #define CRYPTO_ETYPE(C) ((C)->et->type)
63 /* bits for `flags' below */
64 #define F_KEYED 1 /* checksum is keyed */
65 #define F_CPROOF 2 /* checksum is collision proof */
66 #define F_DERIVED 4 /* uses derived keys */
67 #define F_VARIANT 8 /* uses `variant' keys (6.4.3) */
68 #define F_PSEUDO 16 /* not a real protocol type */
69 #define F_SPECIAL 32 /* backwards */
70 #define F_DISABLED 64 /* enctype/checksum disabled */
72 struct salt_type {
73 krb5_salttype type;
74 const char *name;
75 krb5_error_code (*string_to_key)(krb5_context, krb5_enctype, krb5_data,
76 krb5_salt, krb5_data, krb5_keyblock*);
79 struct key_type {
80 krb5_keytype type; /* XXX */
81 const char *name;
82 size_t bits;
83 size_t size;
84 size_t schedule_size;
85 #if 0
86 krb5_enctype best_etype;
87 #endif
88 void (*random_key)(krb5_context, krb5_keyblock*);
89 void (*schedule)(krb5_context, struct key_data *);
90 struct salt_type *string_to_key;
91 void (*random_to_key)(krb5_context, krb5_keyblock*, const void*, size_t);
94 struct checksum_type {
95 krb5_cksumtype type;
96 const char *name;
97 size_t blocksize;
98 size_t checksumsize;
99 unsigned flags;
100 void (*checksum)(krb5_context context,
101 struct key_data *key,
102 const void *buf, size_t len,
103 unsigned usage,
104 Checksum *csum);
105 krb5_error_code (*verify)(krb5_context context,
106 struct key_data *key,
107 const void *buf, size_t len,
108 unsigned usage,
109 Checksum *csum);
112 struct encryption_type {
113 krb5_enctype type;
114 const char *name;
115 heim_oid *oid;
116 size_t blocksize;
117 size_t padsize;
118 size_t confoundersize;
119 struct key_type *keytype;
120 struct checksum_type *checksum;
121 struct checksum_type *keyed_checksum;
122 unsigned flags;
123 krb5_error_code (*encrypt)(krb5_context context,
124 struct key_data *key,
125 void *data, size_t len,
126 krb5_boolean encryptp,
127 int usage,
128 void *ivec);
129 size_t prf_length;
130 krb5_error_code (*prf)(krb5_context,
131 krb5_crypto, const krb5_data *, krb5_data *);
134 #define ENCRYPTION_USAGE(U) (((U) << 8) | 0xAA)
135 #define INTEGRITY_USAGE(U) (((U) << 8) | 0x55)
136 #define CHECKSUM_USAGE(U) (((U) << 8) | 0x99)
138 static struct checksum_type *_find_checksum(krb5_cksumtype type);
139 static struct encryption_type *_find_enctype(krb5_enctype type);
140 static struct key_type *_find_keytype(krb5_keytype type);
141 static krb5_error_code _get_derived_key(krb5_context, krb5_crypto,
142 unsigned, struct key_data**);
143 static struct key_data *_new_derived_key(krb5_crypto crypto, unsigned usage);
144 static krb5_error_code derive_key(krb5_context context,
145 struct encryption_type *et,
146 struct key_data *key,
147 const void *constant,
148 size_t len);
149 static krb5_error_code hmac(krb5_context context,
150 struct checksum_type *cm,
151 const void *data,
152 size_t len,
153 unsigned usage,
154 struct key_data *keyblock,
155 Checksum *result);
156 static void free_key_data(krb5_context context, struct key_data *key);
157 static krb5_error_code usage2arcfour (krb5_context, unsigned *);
158 static void xor (DES_cblock *, const unsigned char *);
160 /************************************************************
162 ************************************************************/
164 static HEIMDAL_MUTEX crypto_mutex = HEIMDAL_MUTEX_INITIALIZER;
167 static void
168 krb5_DES_random_key(krb5_context context,
169 krb5_keyblock *key)
171 DES_cblock *k = key->keyvalue.data;
172 do {
173 krb5_generate_random_block(k, sizeof(DES_cblock));
174 DES_set_odd_parity(k);
175 } while(DES_is_weak_key(k));
178 static void
179 krb5_DES_schedule(krb5_context context,
180 struct key_data *key)
182 DES_set_key(key->key->keyvalue.data, key->schedule->data);
185 #ifdef ENABLE_AFS_STRING_TO_KEY
187 /* This defines the Andrew string_to_key function. It accepts a password
188 * string as input and converts it via a one-way encryption algorithm to a DES
189 * encryption key. It is compatible with the original Andrew authentication
190 * service password database.
194 * Short passwords, i.e 8 characters or less.
196 static void
197 krb5_DES_AFS3_CMU_string_to_key (krb5_data pw,
198 krb5_data cell,
199 DES_cblock *key)
201 char password[8+1]; /* crypt is limited to 8 chars anyway */
202 int i;
204 for(i = 0; i < 8; i++) {
205 char c = ((i < pw.length) ? ((char*)pw.data)[i] : 0) ^
206 ((i < cell.length) ?
207 tolower(((unsigned char*)cell.data)[i]) : 0);
208 password[i] = c ? c : 'X';
210 password[8] = '\0';
212 memcpy(key, crypt(password, "p1") + 2, sizeof(DES_cblock));
214 /* parity is inserted into the LSB so left shift each byte up one
215 bit. This allows ascii characters with a zero MSB to retain as
216 much significance as possible. */
217 for (i = 0; i < sizeof(DES_cblock); i++)
218 ((unsigned char*)key)[i] <<= 1;
219 DES_set_odd_parity (key);
223 * Long passwords, i.e 9 characters or more.
225 static void
226 krb5_DES_AFS3_Transarc_string_to_key (krb5_data pw,
227 krb5_data cell,
228 DES_cblock *key)
230 DES_key_schedule schedule;
231 DES_cblock temp_key;
232 DES_cblock ivec;
233 char password[512];
234 size_t passlen;
236 memcpy(password, pw.data, min(pw.length, sizeof(password)));
237 if(pw.length < sizeof(password)) {
238 int len = min(cell.length, sizeof(password) - pw.length);
239 int i;
241 memcpy(password + pw.length, cell.data, len);
242 for (i = pw.length; i < pw.length + len; ++i)
243 password[i] = tolower((unsigned char)password[i]);
245 passlen = min(sizeof(password), pw.length + cell.length);
246 memcpy(&ivec, "kerberos", 8);
247 memcpy(&temp_key, "kerberos", 8);
248 DES_set_odd_parity (&temp_key);
249 DES_set_key (&temp_key, &schedule);
250 DES_cbc_cksum ((void*)password, &ivec, passlen, &schedule, &ivec);
252 memcpy(&temp_key, &ivec, 8);
253 DES_set_odd_parity (&temp_key);
254 DES_set_key (&temp_key, &schedule);
255 DES_cbc_cksum ((void*)password, key, passlen, &schedule, &ivec);
256 memset(&schedule, 0, sizeof(schedule));
257 memset(&temp_key, 0, sizeof(temp_key));
258 memset(&ivec, 0, sizeof(ivec));
259 memset(password, 0, sizeof(password));
261 DES_set_odd_parity (key);
264 static krb5_error_code
265 DES_AFS3_string_to_key(krb5_context context,
266 krb5_enctype enctype,
267 krb5_data password,
268 krb5_salt salt,
269 krb5_data opaque,
270 krb5_keyblock *key)
272 DES_cblock tmp;
273 if(password.length > 8)
274 krb5_DES_AFS3_Transarc_string_to_key(password, salt.saltvalue, &tmp);
275 else
276 krb5_DES_AFS3_CMU_string_to_key(password, salt.saltvalue, &tmp);
277 key->keytype = enctype;
278 krb5_data_copy(&key->keyvalue, tmp, sizeof(tmp));
279 memset(&key, 0, sizeof(key));
280 return 0;
282 #endif /* ENABLE_AFS_STRING_TO_KEY */
284 static void
285 DES_string_to_key_int(unsigned char *data, size_t length, DES_cblock *key)
287 DES_key_schedule schedule;
288 int i;
289 int reverse = 0;
290 unsigned char *p;
292 unsigned char swap[] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
293 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf };
294 memset(key, 0, 8);
296 p = (unsigned char*)key;
297 for (i = 0; i < length; i++) {
298 unsigned char tmp = data[i];
299 if (!reverse)
300 *p++ ^= (tmp << 1);
301 else
302 *--p ^= (swap[tmp & 0xf] << 4) | swap[(tmp & 0xf0) >> 4];
303 if((i % 8) == 7)
304 reverse = !reverse;
306 DES_set_odd_parity(key);
307 if(DES_is_weak_key(key))
308 (*key)[7] ^= 0xF0;
309 DES_set_key(key, &schedule);
310 DES_cbc_cksum((void*)data, key, length, &schedule, key);
311 memset(&schedule, 0, sizeof(schedule));
312 DES_set_odd_parity(key);
313 if(DES_is_weak_key(key))
314 (*key)[7] ^= 0xF0;
317 static krb5_error_code
318 krb5_DES_string_to_key(krb5_context context,
319 krb5_enctype enctype,
320 krb5_data password,
321 krb5_salt salt,
322 krb5_data opaque,
323 krb5_keyblock *key)
325 unsigned char *s;
326 size_t len;
327 DES_cblock tmp;
329 #ifdef ENABLE_AFS_STRING_TO_KEY
330 if (opaque.length == 1) {
331 unsigned long v;
332 _krb5_get_int(opaque.data, &v, 1);
333 if (v == 1)
334 return DES_AFS3_string_to_key(context, enctype, password,
335 salt, opaque, key);
337 #endif
339 len = password.length + salt.saltvalue.length;
340 s = malloc(len);
341 if(len > 0 && s == NULL) {
342 krb5_set_error_string(context, "malloc: out of memory");
343 return ENOMEM;
345 memcpy(s, password.data, password.length);
346 memcpy(s + password.length, salt.saltvalue.data, salt.saltvalue.length);
347 DES_string_to_key_int(s, len, &tmp);
348 key->keytype = enctype;
349 krb5_data_copy(&key->keyvalue, tmp, sizeof(tmp));
350 memset(&tmp, 0, sizeof(tmp));
351 memset(s, 0, len);
352 free(s);
353 return 0;
356 static void
357 krb5_DES_random_to_key(krb5_context context,
358 krb5_keyblock *key,
359 const void *data,
360 size_t size)
362 DES_cblock *k = key->keyvalue.data;
363 memcpy(k, data, key->keyvalue.length);
364 DES_set_odd_parity(k);
365 if(DES_is_weak_key(k))
366 xor(k, (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
373 static void
374 DES3_random_key(krb5_context context,
375 krb5_keyblock *key)
377 DES_cblock *k = key->keyvalue.data;
378 do {
379 krb5_generate_random_block(k, 3 * sizeof(DES_cblock));
380 DES_set_odd_parity(&k[0]);
381 DES_set_odd_parity(&k[1]);
382 DES_set_odd_parity(&k[2]);
383 } while(DES_is_weak_key(&k[0]) ||
384 DES_is_weak_key(&k[1]) ||
385 DES_is_weak_key(&k[2]));
388 static void
389 DES3_schedule(krb5_context context,
390 struct key_data *key)
392 DES_cblock *k = key->key->keyvalue.data;
393 DES_key_schedule *s = key->schedule->data;
394 DES_set_key(&k[0], &s[0]);
395 DES_set_key(&k[1], &s[1]);
396 DES_set_key(&k[2], &s[2]);
400 * A = A xor B. A & B are 8 bytes.
403 static void
404 xor (DES_cblock *key, const unsigned char *b)
406 unsigned char *a = (unsigned char*)key;
407 a[0] ^= b[0];
408 a[1] ^= b[1];
409 a[2] ^= b[2];
410 a[3] ^= b[3];
411 a[4] ^= b[4];
412 a[5] ^= b[5];
413 a[6] ^= b[6];
414 a[7] ^= b[7];
417 static krb5_error_code
418 DES3_string_to_key(krb5_context context,
419 krb5_enctype enctype,
420 krb5_data password,
421 krb5_salt salt,
422 krb5_data opaque,
423 krb5_keyblock *key)
425 char *str;
426 size_t len;
427 unsigned char tmp[24];
428 DES_cblock keys[3];
429 krb5_error_code ret;
431 len = password.length + salt.saltvalue.length;
432 str = malloc(len);
433 if(len != 0 && str == NULL) {
434 krb5_set_error_string(context, "malloc: out of memory");
435 return ENOMEM;
437 memcpy(str, password.data, password.length);
438 memcpy(str + password.length, salt.saltvalue.data, salt.saltvalue.length);
440 DES_cblock ivec;
441 DES_key_schedule s[3];
442 int i;
444 ret = _krb5_n_fold(str, len, tmp, 24);
445 if (ret) {
446 memset(str, 0, len);
447 free(str);
448 krb5_set_error_string(context, "out of memory");
449 return ret;
452 for(i = 0; i < 3; i++){
453 memcpy(keys + i, tmp + i * 8, sizeof(keys[i]));
454 DES_set_odd_parity(keys + i);
455 if(DES_is_weak_key(keys + i))
456 xor(keys + i, (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
457 DES_set_key(keys + i, &s[i]);
459 memset(&ivec, 0, sizeof(ivec));
460 DES_ede3_cbc_encrypt(tmp,
461 tmp, sizeof(tmp),
462 &s[0], &s[1], &s[2], &ivec, DES_ENCRYPT);
463 memset(s, 0, sizeof(s));
464 memset(&ivec, 0, sizeof(ivec));
465 for(i = 0; i < 3; i++){
466 memcpy(keys + i, tmp + i * 8, sizeof(keys[i]));
467 DES_set_odd_parity(keys + i);
468 if(DES_is_weak_key(keys + i))
469 xor(keys + i, (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
471 memset(tmp, 0, sizeof(tmp));
473 key->keytype = enctype;
474 krb5_data_copy(&key->keyvalue, keys, sizeof(keys));
475 memset(keys, 0, sizeof(keys));
476 memset(str, 0, len);
477 free(str);
478 return 0;
481 static krb5_error_code
482 DES3_string_to_key_derived(krb5_context context,
483 krb5_enctype enctype,
484 krb5_data password,
485 krb5_salt salt,
486 krb5_data opaque,
487 krb5_keyblock *key)
489 krb5_error_code ret;
490 size_t len = password.length + salt.saltvalue.length;
491 char *s;
493 s = malloc(len);
494 if(len != 0 && s == NULL) {
495 krb5_set_error_string(context, "malloc: out of memory");
496 return ENOMEM;
498 memcpy(s, password.data, password.length);
499 memcpy(s + password.length, salt.saltvalue.data, salt.saltvalue.length);
500 ret = krb5_string_to_key_derived(context,
502 len,
503 enctype,
504 key);
505 memset(s, 0, len);
506 free(s);
507 return ret;
510 static void
511 DES3_random_to_key(krb5_context context,
512 krb5_keyblock *key,
513 const void *data,
514 size_t size)
516 unsigned char *x = key->keyvalue.data;
517 const u_char *q = data;
518 DES_cblock *k;
519 int i, j;
521 memset(x, 0, sizeof(x));
522 for (i = 0; i < 3; ++i) {
523 unsigned char foo;
524 for (j = 0; j < 7; ++j) {
525 unsigned char b = q[7 * i + j];
527 x[8 * i + j] = b;
529 foo = 0;
530 for (j = 6; j >= 0; --j) {
531 foo |= q[7 * i + j] & 1;
532 foo <<= 1;
534 x[8 * i + 7] = foo;
536 k = key->keyvalue.data;
537 for (i = 0; i < 3; i++) {
538 DES_set_odd_parity(&k[i]);
539 if(DES_is_weak_key(&k[i]))
540 xor(&k[i], (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
545 * ARCFOUR
548 static void
549 ARCFOUR_schedule(krb5_context context,
550 struct key_data *kd)
552 RC4_set_key (kd->schedule->data,
553 kd->key->keyvalue.length, kd->key->keyvalue.data);
556 static krb5_error_code
557 ARCFOUR_string_to_key(krb5_context context,
558 krb5_enctype enctype,
559 krb5_data password,
560 krb5_salt salt,
561 krb5_data opaque,
562 krb5_keyblock *key)
564 char *s, *p;
565 size_t len;
566 int i;
567 MD4_CTX m;
568 krb5_error_code ret;
570 len = 2 * password.length;
571 s = malloc (len);
572 if (len != 0 && s == NULL) {
573 krb5_set_error_string(context, "malloc: out of memory");
574 ret = ENOMEM;
575 goto out;
577 for (p = s, i = 0; i < password.length; ++i) {
578 *p++ = ((char *)password.data)[i];
579 *p++ = 0;
581 MD4_Init (&m);
582 MD4_Update (&m, s, len);
583 key->keytype = enctype;
584 ret = krb5_data_alloc (&key->keyvalue, 16);
585 if (ret) {
586 krb5_set_error_string(context, "malloc: out of memory");
587 goto out;
589 MD4_Final (key->keyvalue.data, &m);
590 memset (s, 0, len);
591 ret = 0;
592 out:
593 free (s);
594 return ret;
598 * AES
601 int _krb5_AES_string_to_default_iterator = 4096;
603 static krb5_error_code
604 AES_string_to_key(krb5_context context,
605 krb5_enctype enctype,
606 krb5_data password,
607 krb5_salt salt,
608 krb5_data opaque,
609 krb5_keyblock *key)
611 krb5_error_code ret;
612 uint32_t iter;
613 struct encryption_type *et;
614 struct key_data kd;
616 if (opaque.length == 0)
617 iter = _krb5_AES_string_to_default_iterator;
618 else if (opaque.length == 4) {
619 unsigned long v;
620 _krb5_get_int(opaque.data, &v, 4);
621 iter = ((uint32_t)v);
622 } else
623 return KRB5_PROG_KEYTYPE_NOSUPP; /* XXX */
625 et = _find_enctype(enctype);
626 if (et == NULL)
627 return KRB5_PROG_KEYTYPE_NOSUPP;
629 kd.schedule = NULL;
630 ALLOC(kd.key, 1);
631 if(kd.key == NULL) {
632 krb5_set_error_string (context, "malloc: out of memory");
633 return ENOMEM;
635 kd.key->keytype = enctype;
636 ret = krb5_data_alloc(&kd.key->keyvalue, et->keytype->size);
637 if (ret) {
638 krb5_set_error_string(context, "Failed to allocate pkcs5 key");
639 return ret;
642 ret = PKCS5_PBKDF2_HMAC_SHA1(password.data, password.length,
643 salt.saltvalue.data, salt.saltvalue.length,
644 iter,
645 et->keytype->size, kd.key->keyvalue.data);
646 if (ret != 1) {
647 free_key_data(context, &kd);
648 krb5_set_error_string(context, "Error calculating s2k");
649 return KRB5_PROG_KEYTYPE_NOSUPP;
652 ret = derive_key(context, et, &kd, "kerberos", strlen("kerberos"));
653 if (ret == 0)
654 ret = krb5_copy_keyblock_contents(context, kd.key, key);
655 free_key_data(context, &kd);
657 return ret;
660 struct krb5_aes_schedule {
661 AES_KEY ekey;
662 AES_KEY dkey;
665 static void
666 AES_schedule(krb5_context context,
667 struct key_data *kd)
669 struct krb5_aes_schedule *key = kd->schedule->data;
670 int bits = kd->key->keyvalue.length * 8;
672 memset(key, 0, sizeof(*key));
673 AES_set_encrypt_key(kd->key->keyvalue.data, bits, &key->ekey);
674 AES_set_decrypt_key(kd->key->keyvalue.data, bits, &key->dkey);
681 static struct salt_type des_salt[] = {
683 KRB5_PW_SALT,
684 "pw-salt",
685 krb5_DES_string_to_key
687 #ifdef ENABLE_AFS_STRING_TO_KEY
689 KRB5_AFS3_SALT,
690 "afs3-salt",
691 DES_AFS3_string_to_key
693 #endif
694 { 0 }
697 static struct salt_type des3_salt[] = {
699 KRB5_PW_SALT,
700 "pw-salt",
701 DES3_string_to_key
703 { 0 }
706 static struct salt_type des3_salt_derived[] = {
708 KRB5_PW_SALT,
709 "pw-salt",
710 DES3_string_to_key_derived
712 { 0 }
715 static struct salt_type AES_salt[] = {
717 KRB5_PW_SALT,
718 "pw-salt",
719 AES_string_to_key
721 { 0 }
724 static struct salt_type arcfour_salt[] = {
726 KRB5_PW_SALT,
727 "pw-salt",
728 ARCFOUR_string_to_key
730 { 0 }
737 static struct key_type keytype_null = {
738 KEYTYPE_NULL,
739 "null",
743 NULL,
744 NULL,
745 NULL
748 static struct key_type keytype_des = {
749 KEYTYPE_DES,
750 "des",
752 sizeof(DES_cblock),
753 sizeof(DES_key_schedule),
754 krb5_DES_random_key,
755 krb5_DES_schedule,
756 des_salt,
757 krb5_DES_random_to_key
760 static struct key_type keytype_des3 = {
761 KEYTYPE_DES3,
762 "des3",
763 168,
764 3 * sizeof(DES_cblock),
765 3 * sizeof(DES_key_schedule),
766 DES3_random_key,
767 DES3_schedule,
768 des3_salt,
769 DES3_random_to_key
772 static struct key_type keytype_des3_derived = {
773 KEYTYPE_DES3,
774 "des3",
775 168,
776 3 * sizeof(DES_cblock),
777 3 * sizeof(DES_key_schedule),
778 DES3_random_key,
779 DES3_schedule,
780 des3_salt_derived,
781 DES3_random_to_key
784 static struct key_type keytype_aes128 = {
785 KEYTYPE_AES128,
786 "aes-128",
787 128,
789 sizeof(struct krb5_aes_schedule),
790 NULL,
791 AES_schedule,
792 AES_salt
795 static struct key_type keytype_aes256 = {
796 KEYTYPE_AES256,
797 "aes-256",
798 256,
800 sizeof(struct krb5_aes_schedule),
801 NULL,
802 AES_schedule,
803 AES_salt
806 static struct key_type keytype_arcfour = {
807 KEYTYPE_ARCFOUR,
808 "arcfour",
809 128,
811 sizeof(RC4_KEY),
812 NULL,
813 ARCFOUR_schedule,
814 arcfour_salt
817 static struct key_type *keytypes[] = {
818 &keytype_null,
819 &keytype_des,
820 &keytype_des3_derived,
821 &keytype_des3,
822 &keytype_aes128,
823 &keytype_aes256,
824 &keytype_arcfour
827 static int num_keytypes = sizeof(keytypes) / sizeof(keytypes[0]);
829 static struct key_type *
830 _find_keytype(krb5_keytype type)
832 int i;
833 for(i = 0; i < num_keytypes; i++)
834 if(keytypes[i]->type == type)
835 return keytypes[i];
836 return NULL;
840 /* coverity[+alloc : arg-*3] */
841 krb5_error_code KRB5_LIB_FUNCTION
842 krb5_salttype_to_string (krb5_context context,
843 krb5_enctype etype,
844 krb5_salttype stype,
845 char **string)
847 struct encryption_type *e;
848 struct salt_type *st;
850 e = _find_enctype (etype);
851 if (e == NULL) {
852 krb5_set_error_string(context, "encryption type %d not supported",
853 etype);
854 return KRB5_PROG_ETYPE_NOSUPP;
856 for (st = e->keytype->string_to_key; st && st->type; st++) {
857 if (st->type == stype) {
858 *string = strdup (st->name);
859 if (*string == NULL) {
860 krb5_set_error_string(context, "malloc: out of memory");
861 return ENOMEM;
863 return 0;
866 krb5_set_error_string(context, "salttype %d not supported", stype);
867 return HEIM_ERR_SALTTYPE_NOSUPP;
870 krb5_error_code KRB5_LIB_FUNCTION
871 krb5_string_to_salttype (krb5_context context,
872 krb5_enctype etype,
873 const char *string,
874 krb5_salttype *salttype)
876 struct encryption_type *e;
877 struct salt_type *st;
879 e = _find_enctype (etype);
880 if (e == NULL) {
881 krb5_set_error_string(context, "encryption type %d not supported",
882 etype);
883 return KRB5_PROG_ETYPE_NOSUPP;
885 for (st = e->keytype->string_to_key; st && st->type; st++) {
886 if (strcasecmp (st->name, string) == 0) {
887 *salttype = st->type;
888 return 0;
891 krb5_set_error_string(context, "salttype %s not supported", string);
892 return HEIM_ERR_SALTTYPE_NOSUPP;
895 krb5_error_code KRB5_LIB_FUNCTION
896 krb5_get_pw_salt(krb5_context context,
897 krb5_const_principal principal,
898 krb5_salt *salt)
900 size_t len;
901 int i;
902 krb5_error_code ret;
903 char *p;
905 salt->salttype = KRB5_PW_SALT;
906 len = strlen(principal->realm);
907 for (i = 0; i < principal->name.name_string.len; ++i)
908 len += strlen(principal->name.name_string.val[i]);
909 ret = krb5_data_alloc (&salt->saltvalue, len);
910 if (ret)
911 return ret;
912 p = salt->saltvalue.data;
913 memcpy (p, principal->realm, strlen(principal->realm));
914 p += strlen(principal->realm);
915 for (i = 0; i < principal->name.name_string.len; ++i) {
916 memcpy (p,
917 principal->name.name_string.val[i],
918 strlen(principal->name.name_string.val[i]));
919 p += strlen(principal->name.name_string.val[i]);
921 return 0;
924 krb5_error_code KRB5_LIB_FUNCTION
925 krb5_free_salt(krb5_context context,
926 krb5_salt salt)
928 krb5_data_free(&salt.saltvalue);
929 return 0;
932 krb5_error_code KRB5_LIB_FUNCTION
933 krb5_string_to_key_data (krb5_context context,
934 krb5_enctype enctype,
935 krb5_data password,
936 krb5_principal principal,
937 krb5_keyblock *key)
939 krb5_error_code ret;
940 krb5_salt salt;
942 ret = krb5_get_pw_salt(context, principal, &salt);
943 if(ret)
944 return ret;
945 ret = krb5_string_to_key_data_salt(context, enctype, password, salt, key);
946 krb5_free_salt(context, salt);
947 return ret;
950 krb5_error_code KRB5_LIB_FUNCTION
951 krb5_string_to_key (krb5_context context,
952 krb5_enctype enctype,
953 const char *password,
954 krb5_principal principal,
955 krb5_keyblock *key)
957 krb5_data pw;
958 pw.data = rk_UNCONST(password);
959 pw.length = strlen(password);
960 return krb5_string_to_key_data(context, enctype, pw, principal, key);
963 krb5_error_code KRB5_LIB_FUNCTION
964 krb5_string_to_key_data_salt (krb5_context context,
965 krb5_enctype enctype,
966 krb5_data password,
967 krb5_salt salt,
968 krb5_keyblock *key)
970 krb5_data opaque;
971 krb5_data_zero(&opaque);
972 return krb5_string_to_key_data_salt_opaque(context, enctype, password,
973 salt, opaque, key);
977 * Do a string -> key for encryption type `enctype' operation on
978 * `password' (with salt `salt' and the enctype specific data string
979 * `opaque'), returning the resulting key in `key'
982 krb5_error_code KRB5_LIB_FUNCTION
983 krb5_string_to_key_data_salt_opaque (krb5_context context,
984 krb5_enctype enctype,
985 krb5_data password,
986 krb5_salt salt,
987 krb5_data opaque,
988 krb5_keyblock *key)
990 struct encryption_type *et =_find_enctype(enctype);
991 struct salt_type *st;
992 if(et == NULL) {
993 krb5_set_error_string(context, "encryption type %d not supported",
994 enctype);
995 return KRB5_PROG_ETYPE_NOSUPP;
997 for(st = et->keytype->string_to_key; st && st->type; st++)
998 if(st->type == salt.salttype)
999 return (*st->string_to_key)(context, enctype, password,
1000 salt, opaque, key);
1001 krb5_set_error_string(context, "salt type %d not supported",
1002 salt.salttype);
1003 return HEIM_ERR_SALTTYPE_NOSUPP;
1007 * Do a string -> key for encryption type `enctype' operation on the
1008 * string `password' (with salt `salt'), returning the resulting key
1009 * in `key'
1012 krb5_error_code KRB5_LIB_FUNCTION
1013 krb5_string_to_key_salt (krb5_context context,
1014 krb5_enctype enctype,
1015 const char *password,
1016 krb5_salt salt,
1017 krb5_keyblock *key)
1019 krb5_data pw;
1020 pw.data = rk_UNCONST(password);
1021 pw.length = strlen(password);
1022 return krb5_string_to_key_data_salt(context, enctype, pw, salt, key);
1025 krb5_error_code KRB5_LIB_FUNCTION
1026 krb5_string_to_key_salt_opaque (krb5_context context,
1027 krb5_enctype enctype,
1028 const char *password,
1029 krb5_salt salt,
1030 krb5_data opaque,
1031 krb5_keyblock *key)
1033 krb5_data pw;
1034 pw.data = rk_UNCONST(password);
1035 pw.length = strlen(password);
1036 return krb5_string_to_key_data_salt_opaque(context, enctype,
1037 pw, salt, opaque, key);
1040 krb5_error_code KRB5_LIB_FUNCTION
1041 krb5_keytype_to_string(krb5_context context,
1042 krb5_keytype keytype,
1043 char **string)
1045 struct key_type *kt = _find_keytype(keytype);
1046 if(kt == NULL) {
1047 krb5_set_error_string(context, "key type %d not supported", keytype);
1048 return KRB5_PROG_KEYTYPE_NOSUPP;
1050 *string = strdup(kt->name);
1051 if(*string == NULL) {
1052 krb5_set_error_string(context, "malloc: out of memory");
1053 return ENOMEM;
1055 return 0;
1058 krb5_error_code KRB5_LIB_FUNCTION
1059 krb5_string_to_keytype(krb5_context context,
1060 const char *string,
1061 krb5_keytype *keytype)
1063 int i;
1064 for(i = 0; i < num_keytypes; i++)
1065 if(strcasecmp(keytypes[i]->name, string) == 0){
1066 *keytype = keytypes[i]->type;
1067 return 0;
1069 krb5_set_error_string(context, "key type %s not supported", string);
1070 return KRB5_PROG_KEYTYPE_NOSUPP;
1073 krb5_error_code KRB5_LIB_FUNCTION
1074 krb5_enctype_keysize(krb5_context context,
1075 krb5_enctype type,
1076 size_t *keysize)
1078 struct encryption_type *et = _find_enctype(type);
1079 if(et == NULL) {
1080 krb5_set_error_string(context, "encryption type %d not supported",
1081 type);
1082 return KRB5_PROG_ETYPE_NOSUPP;
1084 *keysize = et->keytype->size;
1085 return 0;
1088 krb5_error_code KRB5_LIB_FUNCTION
1089 krb5_enctype_keybits(krb5_context context,
1090 krb5_enctype type,
1091 size_t *keybits)
1093 struct encryption_type *et = _find_enctype(type);
1094 if(et == NULL) {
1095 krb5_set_error_string(context, "encryption type %d not supported",
1096 type);
1097 return KRB5_PROG_ETYPE_NOSUPP;
1099 *keybits = et->keytype->bits;
1100 return 0;
1103 krb5_error_code KRB5_LIB_FUNCTION
1104 krb5_generate_random_keyblock(krb5_context context,
1105 krb5_enctype type,
1106 krb5_keyblock *key)
1108 krb5_error_code ret;
1109 struct encryption_type *et = _find_enctype(type);
1110 if(et == NULL) {
1111 krb5_set_error_string(context, "encryption type %d not supported",
1112 type);
1113 return KRB5_PROG_ETYPE_NOSUPP;
1115 ret = krb5_data_alloc(&key->keyvalue, et->keytype->size);
1116 if(ret)
1117 return ret;
1118 key->keytype = type;
1119 if(et->keytype->random_key)
1120 (*et->keytype->random_key)(context, key);
1121 else
1122 krb5_generate_random_block(key->keyvalue.data,
1123 key->keyvalue.length);
1124 return 0;
1127 static krb5_error_code
1128 _key_schedule(krb5_context context,
1129 struct key_data *key)
1131 krb5_error_code ret;
1132 struct encryption_type *et = _find_enctype(key->key->keytype);
1133 struct key_type *kt = et->keytype;
1135 if(kt->schedule == NULL)
1136 return 0;
1137 if (key->schedule != NULL)
1138 return 0;
1139 ALLOC(key->schedule, 1);
1140 if(key->schedule == NULL) {
1141 krb5_set_error_string(context, "malloc: out of memory");
1142 return ENOMEM;
1144 ret = krb5_data_alloc(key->schedule, kt->schedule_size);
1145 if(ret) {
1146 free(key->schedule);
1147 key->schedule = NULL;
1148 return ret;
1150 (*kt->schedule)(context, key);
1151 return 0;
1154 /************************************************************
1156 ************************************************************/
1158 static void
1159 NONE_checksum(krb5_context context,
1160 struct key_data *key,
1161 const void *data,
1162 size_t len,
1163 unsigned usage,
1164 Checksum *C)
1168 static void
1169 CRC32_checksum(krb5_context context,
1170 struct key_data *key,
1171 const void *data,
1172 size_t len,
1173 unsigned usage,
1174 Checksum *C)
1176 uint32_t crc;
1177 unsigned char *r = C->checksum.data;
1178 _krb5_crc_init_table ();
1179 crc = _krb5_crc_update (data, len, 0);
1180 r[0] = crc & 0xff;
1181 r[1] = (crc >> 8) & 0xff;
1182 r[2] = (crc >> 16) & 0xff;
1183 r[3] = (crc >> 24) & 0xff;
1186 static void
1187 RSA_MD4_checksum(krb5_context context,
1188 struct key_data *key,
1189 const void *data,
1190 size_t len,
1191 unsigned usage,
1192 Checksum *C)
1194 MD4_CTX m;
1196 MD4_Init (&m);
1197 MD4_Update (&m, data, len);
1198 MD4_Final (C->checksum.data, &m);
1201 static void
1202 RSA_MD4_DES_checksum(krb5_context context,
1203 struct key_data *key,
1204 const void *data,
1205 size_t len,
1206 unsigned usage,
1207 Checksum *cksum)
1209 MD4_CTX md4;
1210 DES_cblock ivec;
1211 unsigned char *p = cksum->checksum.data;
1213 krb5_generate_random_block(p, 8);
1214 MD4_Init (&md4);
1215 MD4_Update (&md4, p, 8);
1216 MD4_Update (&md4, data, len);
1217 MD4_Final (p + 8, &md4);
1218 memset (&ivec, 0, sizeof(ivec));
1219 DES_cbc_encrypt(p,
1221 24,
1222 key->schedule->data,
1223 &ivec,
1224 DES_ENCRYPT);
1227 static krb5_error_code
1228 RSA_MD4_DES_verify(krb5_context context,
1229 struct key_data *key,
1230 const void *data,
1231 size_t len,
1232 unsigned usage,
1233 Checksum *C)
1235 MD4_CTX md4;
1236 unsigned char tmp[24];
1237 unsigned char res[16];
1238 DES_cblock ivec;
1239 krb5_error_code ret = 0;
1241 memset(&ivec, 0, sizeof(ivec));
1242 DES_cbc_encrypt(C->checksum.data,
1243 (void*)tmp,
1244 C->checksum.length,
1245 key->schedule->data,
1246 &ivec,
1247 DES_DECRYPT);
1248 MD4_Init (&md4);
1249 MD4_Update (&md4, tmp, 8); /* confounder */
1250 MD4_Update (&md4, data, len);
1251 MD4_Final (res, &md4);
1252 if(memcmp(res, tmp + 8, sizeof(res)) != 0) {
1253 krb5_clear_error_string (context);
1254 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1256 memset(tmp, 0, sizeof(tmp));
1257 memset(res, 0, sizeof(res));
1258 return ret;
1261 static void
1262 RSA_MD5_checksum(krb5_context context,
1263 struct key_data *key,
1264 const void *data,
1265 size_t len,
1266 unsigned usage,
1267 Checksum *C)
1269 MD5_CTX m;
1271 MD5_Init (&m);
1272 MD5_Update(&m, data, len);
1273 MD5_Final (C->checksum.data, &m);
1276 static void
1277 RSA_MD5_DES_checksum(krb5_context context,
1278 struct key_data *key,
1279 const void *data,
1280 size_t len,
1281 unsigned usage,
1282 Checksum *C)
1284 MD5_CTX md5;
1285 DES_cblock ivec;
1286 unsigned char *p = C->checksum.data;
1288 krb5_generate_random_block(p, 8);
1289 MD5_Init (&md5);
1290 MD5_Update (&md5, p, 8);
1291 MD5_Update (&md5, data, len);
1292 MD5_Final (p + 8, &md5);
1293 memset (&ivec, 0, sizeof(ivec));
1294 DES_cbc_encrypt(p,
1296 24,
1297 key->schedule->data,
1298 &ivec,
1299 DES_ENCRYPT);
1302 static krb5_error_code
1303 RSA_MD5_DES_verify(krb5_context context,
1304 struct key_data *key,
1305 const void *data,
1306 size_t len,
1307 unsigned usage,
1308 Checksum *C)
1310 MD5_CTX md5;
1311 unsigned char tmp[24];
1312 unsigned char res[16];
1313 DES_cblock ivec;
1314 DES_key_schedule *sched = key->schedule->data;
1315 krb5_error_code ret = 0;
1317 memset(&ivec, 0, sizeof(ivec));
1318 DES_cbc_encrypt(C->checksum.data,
1319 (void*)tmp,
1320 C->checksum.length,
1321 &sched[0],
1322 &ivec,
1323 DES_DECRYPT);
1324 MD5_Init (&md5);
1325 MD5_Update (&md5, tmp, 8); /* confounder */
1326 MD5_Update (&md5, data, len);
1327 MD5_Final (res, &md5);
1328 if(memcmp(res, tmp + 8, sizeof(res)) != 0) {
1329 krb5_clear_error_string (context);
1330 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1332 memset(tmp, 0, sizeof(tmp));
1333 memset(res, 0, sizeof(res));
1334 return ret;
1337 static void
1338 RSA_MD5_DES3_checksum(krb5_context context,
1339 struct key_data *key,
1340 const void *data,
1341 size_t len,
1342 unsigned usage,
1343 Checksum *C)
1345 MD5_CTX md5;
1346 DES_cblock ivec;
1347 unsigned char *p = C->checksum.data;
1348 DES_key_schedule *sched = key->schedule->data;
1350 krb5_generate_random_block(p, 8);
1351 MD5_Init (&md5);
1352 MD5_Update (&md5, p, 8);
1353 MD5_Update (&md5, data, len);
1354 MD5_Final (p + 8, &md5);
1355 memset (&ivec, 0, sizeof(ivec));
1356 DES_ede3_cbc_encrypt(p,
1358 24,
1359 &sched[0], &sched[1], &sched[2],
1360 &ivec,
1361 DES_ENCRYPT);
1364 static krb5_error_code
1365 RSA_MD5_DES3_verify(krb5_context context,
1366 struct key_data *key,
1367 const void *data,
1368 size_t len,
1369 unsigned usage,
1370 Checksum *C)
1372 MD5_CTX md5;
1373 unsigned char tmp[24];
1374 unsigned char res[16];
1375 DES_cblock ivec;
1376 DES_key_schedule *sched = key->schedule->data;
1377 krb5_error_code ret = 0;
1379 memset(&ivec, 0, sizeof(ivec));
1380 DES_ede3_cbc_encrypt(C->checksum.data,
1381 (void*)tmp,
1382 C->checksum.length,
1383 &sched[0], &sched[1], &sched[2],
1384 &ivec,
1385 DES_DECRYPT);
1386 MD5_Init (&md5);
1387 MD5_Update (&md5, tmp, 8); /* confounder */
1388 MD5_Update (&md5, data, len);
1389 MD5_Final (res, &md5);
1390 if(memcmp(res, tmp + 8, sizeof(res)) != 0) {
1391 krb5_clear_error_string (context);
1392 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1394 memset(tmp, 0, sizeof(tmp));
1395 memset(res, 0, sizeof(res));
1396 return ret;
1399 static void
1400 SHA1_checksum(krb5_context context,
1401 struct key_data *key,
1402 const void *data,
1403 size_t len,
1404 unsigned usage,
1405 Checksum *C)
1407 SHA_CTX m;
1409 SHA1_Init(&m);
1410 SHA1_Update(&m, data, len);
1411 SHA1_Final(C->checksum.data, &m);
1414 /* HMAC according to RFC2104 */
1415 static krb5_error_code
1416 hmac(krb5_context context,
1417 struct checksum_type *cm,
1418 const void *data,
1419 size_t len,
1420 unsigned usage,
1421 struct key_data *keyblock,
1422 Checksum *result)
1424 unsigned char *ipad, *opad;
1425 unsigned char *key;
1426 size_t key_len;
1427 int i;
1429 ipad = malloc(cm->blocksize + len);
1430 if (ipad == NULL)
1431 return ENOMEM;
1432 opad = malloc(cm->blocksize + cm->checksumsize);
1433 if (opad == NULL) {
1434 free(ipad);
1435 return ENOMEM;
1437 memset(ipad, 0x36, cm->blocksize);
1438 memset(opad, 0x5c, cm->blocksize);
1440 if(keyblock->key->keyvalue.length > cm->blocksize){
1441 (*cm->checksum)(context,
1442 keyblock,
1443 keyblock->key->keyvalue.data,
1444 keyblock->key->keyvalue.length,
1445 usage,
1446 result);
1447 key = result->checksum.data;
1448 key_len = result->checksum.length;
1449 } else {
1450 key = keyblock->key->keyvalue.data;
1451 key_len = keyblock->key->keyvalue.length;
1453 for(i = 0; i < key_len; i++){
1454 ipad[i] ^= key[i];
1455 opad[i] ^= key[i];
1457 memcpy(ipad + cm->blocksize, data, len);
1458 (*cm->checksum)(context, keyblock, ipad, cm->blocksize + len,
1459 usage, result);
1460 memcpy(opad + cm->blocksize, result->checksum.data,
1461 result->checksum.length);
1462 (*cm->checksum)(context, keyblock, opad,
1463 cm->blocksize + cm->checksumsize, usage, result);
1464 memset(ipad, 0, cm->blocksize + len);
1465 free(ipad);
1466 memset(opad, 0, cm->blocksize + cm->checksumsize);
1467 free(opad);
1469 return 0;
1472 krb5_error_code KRB5_LIB_FUNCTION
1473 krb5_hmac(krb5_context context,
1474 krb5_cksumtype cktype,
1475 const void *data,
1476 size_t len,
1477 unsigned usage,
1478 krb5_keyblock *key,
1479 Checksum *result)
1481 struct checksum_type *c = _find_checksum(cktype);
1482 struct key_data kd;
1483 krb5_error_code ret;
1485 if (c == NULL) {
1486 krb5_set_error_string (context, "checksum type %d not supported",
1487 cktype);
1488 return KRB5_PROG_SUMTYPE_NOSUPP;
1491 kd.key = key;
1492 kd.schedule = NULL;
1494 ret = hmac(context, c, data, len, usage, &kd, result);
1496 if (kd.schedule)
1497 krb5_free_data(context, kd.schedule);
1499 return ret;
1502 static void
1503 SP_HMAC_SHA1_checksum(krb5_context context,
1504 struct key_data *key,
1505 const void *data,
1506 size_t len,
1507 unsigned usage,
1508 Checksum *result)
1510 struct checksum_type *c = _find_checksum(CKSUMTYPE_SHA1);
1511 Checksum res;
1512 char sha1_data[20];
1513 krb5_error_code ret;
1515 res.checksum.data = sha1_data;
1516 res.checksum.length = sizeof(sha1_data);
1518 ret = hmac(context, c, data, len, usage, key, &res);
1519 if (ret)
1520 krb5_abortx(context, "hmac failed");
1521 memcpy(result->checksum.data, res.checksum.data, result->checksum.length);
1525 * checksum according to section 5. of draft-brezak-win2k-krb-rc4-hmac-03.txt
1528 static void
1529 HMAC_MD5_checksum(krb5_context context,
1530 struct key_data *key,
1531 const void *data,
1532 size_t len,
1533 unsigned usage,
1534 Checksum *result)
1536 MD5_CTX md5;
1537 struct checksum_type *c = _find_checksum (CKSUMTYPE_RSA_MD5);
1538 const char signature[] = "signaturekey";
1539 Checksum ksign_c;
1540 struct key_data ksign;
1541 krb5_keyblock kb;
1542 unsigned char t[4];
1543 unsigned char tmp[16];
1544 unsigned char ksign_c_data[16];
1545 krb5_error_code ret;
1547 ksign_c.checksum.length = sizeof(ksign_c_data);
1548 ksign_c.checksum.data = ksign_c_data;
1549 ret = hmac(context, c, signature, sizeof(signature), 0, key, &ksign_c);
1550 if (ret)
1551 krb5_abortx(context, "hmac failed");
1552 ksign.key = &kb;
1553 kb.keyvalue = ksign_c.checksum;
1554 MD5_Init (&md5);
1555 t[0] = (usage >> 0) & 0xFF;
1556 t[1] = (usage >> 8) & 0xFF;
1557 t[2] = (usage >> 16) & 0xFF;
1558 t[3] = (usage >> 24) & 0xFF;
1559 MD5_Update (&md5, t, 4);
1560 MD5_Update (&md5, data, len);
1561 MD5_Final (tmp, &md5);
1562 ret = hmac(context, c, tmp, sizeof(tmp), 0, &ksign, result);
1563 if (ret)
1564 krb5_abortx(context, "hmac failed");
1568 * same as previous but being used while encrypting.
1571 static void
1572 HMAC_MD5_checksum_enc(krb5_context context,
1573 struct key_data *key,
1574 const void *data,
1575 size_t len,
1576 unsigned usage,
1577 Checksum *result)
1579 struct checksum_type *c = _find_checksum (CKSUMTYPE_RSA_MD5);
1580 Checksum ksign_c;
1581 struct key_data ksign;
1582 krb5_keyblock kb;
1583 unsigned char t[4];
1584 unsigned char ksign_c_data[16];
1585 krb5_error_code ret;
1587 t[0] = (usage >> 0) & 0xFF;
1588 t[1] = (usage >> 8) & 0xFF;
1589 t[2] = (usage >> 16) & 0xFF;
1590 t[3] = (usage >> 24) & 0xFF;
1592 ksign_c.checksum.length = sizeof(ksign_c_data);
1593 ksign_c.checksum.data = ksign_c_data;
1594 ret = hmac(context, c, t, sizeof(t), 0, key, &ksign_c);
1595 if (ret)
1596 krb5_abortx(context, "hmac failed");
1597 ksign.key = &kb;
1598 kb.keyvalue = ksign_c.checksum;
1599 ret = hmac(context, c, data, len, 0, &ksign, result);
1600 if (ret)
1601 krb5_abortx(context, "hmac failed");
1604 static struct checksum_type checksum_none = {
1605 CKSUMTYPE_NONE,
1606 "none",
1610 NONE_checksum,
1611 NULL
1613 static struct checksum_type checksum_crc32 = {
1614 CKSUMTYPE_CRC32,
1615 "crc32",
1619 CRC32_checksum,
1620 NULL
1622 static struct checksum_type checksum_rsa_md4 = {
1623 CKSUMTYPE_RSA_MD4,
1624 "rsa-md4",
1627 F_CPROOF,
1628 RSA_MD4_checksum,
1629 NULL
1631 static struct checksum_type checksum_rsa_md4_des = {
1632 CKSUMTYPE_RSA_MD4_DES,
1633 "rsa-md4-des",
1636 F_KEYED | F_CPROOF | F_VARIANT,
1637 RSA_MD4_DES_checksum,
1638 RSA_MD4_DES_verify
1640 #if 0
1641 static struct checksum_type checksum_des_mac = {
1642 CKSUMTYPE_DES_MAC,
1643 "des-mac",
1647 DES_MAC_checksum
1649 static struct checksum_type checksum_des_mac_k = {
1650 CKSUMTYPE_DES_MAC_K,
1651 "des-mac-k",
1655 DES_MAC_K_checksum
1657 static struct checksum_type checksum_rsa_md4_des_k = {
1658 CKSUMTYPE_RSA_MD4_DES_K,
1659 "rsa-md4-des-k",
1663 RSA_MD4_DES_K_checksum,
1664 RSA_MD4_DES_K_verify
1666 #endif
1667 static struct checksum_type checksum_rsa_md5 = {
1668 CKSUMTYPE_RSA_MD5,
1669 "rsa-md5",
1672 F_CPROOF,
1673 RSA_MD5_checksum,
1674 NULL
1676 static struct checksum_type checksum_rsa_md5_des = {
1677 CKSUMTYPE_RSA_MD5_DES,
1678 "rsa-md5-des",
1681 F_KEYED | F_CPROOF | F_VARIANT,
1682 RSA_MD5_DES_checksum,
1683 RSA_MD5_DES_verify
1685 static struct checksum_type checksum_rsa_md5_des3 = {
1686 CKSUMTYPE_RSA_MD5_DES3,
1687 "rsa-md5-des3",
1690 F_KEYED | F_CPROOF | F_VARIANT,
1691 RSA_MD5_DES3_checksum,
1692 RSA_MD5_DES3_verify
1694 static struct checksum_type checksum_sha1 = {
1695 CKSUMTYPE_SHA1,
1696 "sha1",
1699 F_CPROOF,
1700 SHA1_checksum,
1701 NULL
1703 static struct checksum_type checksum_hmac_sha1_des3 = {
1704 CKSUMTYPE_HMAC_SHA1_DES3,
1705 "hmac-sha1-des3",
1708 F_KEYED | F_CPROOF | F_DERIVED,
1709 SP_HMAC_SHA1_checksum,
1710 NULL
1713 static struct checksum_type checksum_hmac_sha1_aes128 = {
1714 CKSUMTYPE_HMAC_SHA1_96_AES_128,
1715 "hmac-sha1-96-aes128",
1718 F_KEYED | F_CPROOF | F_DERIVED,
1719 SP_HMAC_SHA1_checksum,
1720 NULL
1723 static struct checksum_type checksum_hmac_sha1_aes256 = {
1724 CKSUMTYPE_HMAC_SHA1_96_AES_256,
1725 "hmac-sha1-96-aes256",
1728 F_KEYED | F_CPROOF | F_DERIVED,
1729 SP_HMAC_SHA1_checksum,
1730 NULL
1733 static struct checksum_type checksum_hmac_md5 = {
1734 CKSUMTYPE_HMAC_MD5,
1735 "hmac-md5",
1738 F_KEYED | F_CPROOF,
1739 HMAC_MD5_checksum,
1740 NULL
1743 static struct checksum_type checksum_hmac_md5_enc = {
1744 CKSUMTYPE_HMAC_MD5_ENC,
1745 "hmac-md5-enc",
1748 F_KEYED | F_CPROOF | F_PSEUDO,
1749 HMAC_MD5_checksum_enc,
1750 NULL
1753 static struct checksum_type *checksum_types[] = {
1754 &checksum_none,
1755 &checksum_crc32,
1756 &checksum_rsa_md4,
1757 &checksum_rsa_md4_des,
1758 #if 0
1759 &checksum_des_mac,
1760 &checksum_des_mac_k,
1761 &checksum_rsa_md4_des_k,
1762 #endif
1763 &checksum_rsa_md5,
1764 &checksum_rsa_md5_des,
1765 &checksum_rsa_md5_des3,
1766 &checksum_sha1,
1767 &checksum_hmac_sha1_des3,
1768 &checksum_hmac_sha1_aes128,
1769 &checksum_hmac_sha1_aes256,
1770 &checksum_hmac_md5,
1771 &checksum_hmac_md5_enc
1774 static int num_checksums = sizeof(checksum_types) / sizeof(checksum_types[0]);
1776 static struct checksum_type *
1777 _find_checksum(krb5_cksumtype type)
1779 int i;
1780 for(i = 0; i < num_checksums; i++)
1781 if(checksum_types[i]->type == type)
1782 return checksum_types[i];
1783 return NULL;
1786 static krb5_error_code
1787 get_checksum_key(krb5_context context,
1788 krb5_crypto crypto,
1789 unsigned usage, /* not krb5_key_usage */
1790 struct checksum_type *ct,
1791 struct key_data **key)
1793 krb5_error_code ret = 0;
1795 if(ct->flags & F_DERIVED)
1796 ret = _get_derived_key(context, crypto, usage, key);
1797 else if(ct->flags & F_VARIANT) {
1798 int i;
1800 *key = _new_derived_key(crypto, 0xff/* KRB5_KU_RFC1510_VARIANT */);
1801 if(*key == NULL) {
1802 krb5_set_error_string(context, "malloc: out of memory");
1803 return ENOMEM;
1805 ret = krb5_copy_keyblock(context, crypto->key.key, &(*key)->key);
1806 if(ret)
1807 return ret;
1808 for(i = 0; i < (*key)->key->keyvalue.length; i++)
1809 ((unsigned char*)(*key)->key->keyvalue.data)[i] ^= 0xF0;
1810 } else {
1811 *key = &crypto->key;
1813 if(ret == 0)
1814 ret = _key_schedule(context, *key);
1815 return ret;
1818 static krb5_error_code
1819 create_checksum (krb5_context context,
1820 struct checksum_type *ct,
1821 krb5_crypto crypto,
1822 unsigned usage,
1823 void *data,
1824 size_t len,
1825 Checksum *result)
1827 krb5_error_code ret;
1828 struct key_data *dkey;
1829 int keyed_checksum;
1831 if (ct->flags & F_DISABLED) {
1832 krb5_clear_error_string (context);
1833 return KRB5_PROG_SUMTYPE_NOSUPP;
1835 keyed_checksum = (ct->flags & F_KEYED) != 0;
1836 if(keyed_checksum && crypto == NULL) {
1837 krb5_set_error_string (context, "Checksum type %s is keyed "
1838 "but no crypto context (key) was passed in",
1839 ct->name);
1840 return KRB5_PROG_SUMTYPE_NOSUPP; /* XXX */
1842 if(keyed_checksum) {
1843 ret = get_checksum_key(context, crypto, usage, ct, &dkey);
1844 if (ret)
1845 return ret;
1846 } else
1847 dkey = NULL;
1848 result->cksumtype = ct->type;
1849 ret = krb5_data_alloc(&result->checksum, ct->checksumsize);
1850 if (ret)
1851 return (ret);
1852 (*ct->checksum)(context, dkey, data, len, usage, result);
1853 return 0;
1856 static int
1857 arcfour_checksum_p(struct checksum_type *ct, krb5_crypto crypto)
1859 return (ct->type == CKSUMTYPE_HMAC_MD5) &&
1860 (crypto->key.key->keytype == KEYTYPE_ARCFOUR);
1863 krb5_error_code KRB5_LIB_FUNCTION
1864 krb5_create_checksum(krb5_context context,
1865 krb5_crypto crypto,
1866 krb5_key_usage usage,
1867 int type,
1868 void *data,
1869 size_t len,
1870 Checksum *result)
1872 struct checksum_type *ct = NULL;
1873 unsigned keyusage;
1875 /* type 0 -> pick from crypto */
1876 if (type) {
1877 ct = _find_checksum(type);
1878 } else if (crypto) {
1879 ct = crypto->et->keyed_checksum;
1880 if (ct == NULL)
1881 ct = crypto->et->checksum;
1884 if(ct == NULL) {
1885 krb5_set_error_string (context, "checksum type %d not supported",
1886 type);
1887 return KRB5_PROG_SUMTYPE_NOSUPP;
1890 if (arcfour_checksum_p(ct, crypto)) {
1891 keyusage = usage;
1892 usage2arcfour(context, &keyusage);
1893 } else
1894 keyusage = CHECKSUM_USAGE(usage);
1896 return create_checksum(context, ct, crypto, keyusage,
1897 data, len, result);
1900 static krb5_error_code
1901 verify_checksum(krb5_context context,
1902 krb5_crypto crypto,
1903 unsigned usage, /* not krb5_key_usage */
1904 void *data,
1905 size_t len,
1906 Checksum *cksum)
1908 krb5_error_code ret;
1909 struct key_data *dkey;
1910 int keyed_checksum;
1911 Checksum c;
1912 struct checksum_type *ct;
1914 ct = _find_checksum(cksum->cksumtype);
1915 if (ct == NULL || (ct->flags & F_DISABLED)) {
1916 krb5_set_error_string (context, "checksum type %d not supported",
1917 cksum->cksumtype);
1918 return KRB5_PROG_SUMTYPE_NOSUPP;
1920 if(ct->checksumsize != cksum->checksum.length) {
1921 krb5_clear_error_string (context);
1922 return KRB5KRB_AP_ERR_BAD_INTEGRITY; /* XXX */
1924 keyed_checksum = (ct->flags & F_KEYED) != 0;
1925 if(keyed_checksum && crypto == NULL) {
1926 krb5_set_error_string (context, "Checksum type %s is keyed "
1927 "but no crypto context (key) was passed in",
1928 ct->name);
1929 return KRB5_PROG_SUMTYPE_NOSUPP; /* XXX */
1931 if(keyed_checksum)
1932 ret = get_checksum_key(context, crypto, usage, ct, &dkey);
1933 else
1934 dkey = NULL;
1935 if(ct->verify)
1936 return (*ct->verify)(context, dkey, data, len, usage, cksum);
1938 ret = krb5_data_alloc (&c.checksum, ct->checksumsize);
1939 if (ret)
1940 return ret;
1942 (*ct->checksum)(context, dkey, data, len, usage, &c);
1944 if(c.checksum.length != cksum->checksum.length ||
1945 memcmp(c.checksum.data, cksum->checksum.data, c.checksum.length)) {
1946 krb5_clear_error_string (context);
1947 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1948 } else {
1949 ret = 0;
1951 krb5_data_free (&c.checksum);
1952 return ret;
1955 krb5_error_code KRB5_LIB_FUNCTION
1956 krb5_verify_checksum(krb5_context context,
1957 krb5_crypto crypto,
1958 krb5_key_usage usage,
1959 void *data,
1960 size_t len,
1961 Checksum *cksum)
1963 struct checksum_type *ct;
1964 unsigned keyusage;
1966 ct = _find_checksum(cksum->cksumtype);
1967 if(ct == NULL) {
1968 krb5_set_error_string (context, "checksum type %d not supported",
1969 cksum->cksumtype);
1970 return KRB5_PROG_SUMTYPE_NOSUPP;
1973 if (arcfour_checksum_p(ct, crypto)) {
1974 keyusage = usage;
1975 usage2arcfour(context, &keyusage);
1976 } else
1977 keyusage = CHECKSUM_USAGE(usage);
1979 return verify_checksum(context, crypto, keyusage,
1980 data, len, cksum);
1983 krb5_error_code KRB5_LIB_FUNCTION
1984 krb5_crypto_get_checksum_type(krb5_context context,
1985 krb5_crypto crypto,
1986 krb5_cksumtype *type)
1988 struct checksum_type *ct = NULL;
1990 if (crypto != NULL) {
1991 ct = crypto->et->keyed_checksum;
1992 if (ct == NULL)
1993 ct = crypto->et->checksum;
1996 if (ct == NULL) {
1997 krb5_set_error_string (context, "checksum type not found");
1998 return KRB5_PROG_SUMTYPE_NOSUPP;
2001 *type = ct->type;
2003 return 0;
2007 krb5_error_code KRB5_LIB_FUNCTION
2008 krb5_checksumsize(krb5_context context,
2009 krb5_cksumtype type,
2010 size_t *size)
2012 struct checksum_type *ct = _find_checksum(type);
2013 if(ct == NULL) {
2014 krb5_set_error_string (context, "checksum type %d not supported",
2015 type);
2016 return KRB5_PROG_SUMTYPE_NOSUPP;
2018 *size = ct->checksumsize;
2019 return 0;
2022 krb5_boolean KRB5_LIB_FUNCTION
2023 krb5_checksum_is_keyed(krb5_context context,
2024 krb5_cksumtype type)
2026 struct checksum_type *ct = _find_checksum(type);
2027 if(ct == NULL) {
2028 if (context)
2029 krb5_set_error_string (context, "checksum type %d not supported",
2030 type);
2031 return KRB5_PROG_SUMTYPE_NOSUPP;
2033 return ct->flags & F_KEYED;
2036 krb5_boolean KRB5_LIB_FUNCTION
2037 krb5_checksum_is_collision_proof(krb5_context context,
2038 krb5_cksumtype type)
2040 struct checksum_type *ct = _find_checksum(type);
2041 if(ct == NULL) {
2042 if (context)
2043 krb5_set_error_string (context, "checksum type %d not supported",
2044 type);
2045 return KRB5_PROG_SUMTYPE_NOSUPP;
2047 return ct->flags & F_CPROOF;
2050 krb5_error_code KRB5_LIB_FUNCTION
2051 krb5_checksum_disable(krb5_context context,
2052 krb5_cksumtype type)
2054 struct checksum_type *ct = _find_checksum(type);
2055 if(ct == NULL) {
2056 if (context)
2057 krb5_set_error_string (context, "checksum type %d not supported",
2058 type);
2059 return KRB5_PROG_SUMTYPE_NOSUPP;
2061 ct->flags |= F_DISABLED;
2062 return 0;
2065 /************************************************************
2067 ************************************************************/
2069 static krb5_error_code
2070 NULL_encrypt(krb5_context context,
2071 struct key_data *key,
2072 void *data,
2073 size_t len,
2074 krb5_boolean encryptp,
2075 int usage,
2076 void *ivec)
2078 return 0;
2081 static krb5_error_code
2082 DES_CBC_encrypt_null_ivec(krb5_context context,
2083 struct key_data *key,
2084 void *data,
2085 size_t len,
2086 krb5_boolean encryptp,
2087 int usage,
2088 void *ignore_ivec)
2090 DES_cblock ivec;
2091 DES_key_schedule *s = key->schedule->data;
2092 memset(&ivec, 0, sizeof(ivec));
2093 DES_cbc_encrypt(data, data, len, s, &ivec, encryptp);
2094 return 0;
2097 static krb5_error_code
2098 DES_CBC_encrypt_key_ivec(krb5_context context,
2099 struct key_data *key,
2100 void *data,
2101 size_t len,
2102 krb5_boolean encryptp,
2103 int usage,
2104 void *ignore_ivec)
2106 DES_cblock ivec;
2107 DES_key_schedule *s = key->schedule->data;
2108 memcpy(&ivec, key->key->keyvalue.data, sizeof(ivec));
2109 DES_cbc_encrypt(data, data, len, s, &ivec, encryptp);
2110 return 0;
2113 static krb5_error_code
2114 DES3_CBC_encrypt(krb5_context context,
2115 struct key_data *key,
2116 void *data,
2117 size_t len,
2118 krb5_boolean encryptp,
2119 int usage,
2120 void *ivec)
2122 DES_cblock local_ivec;
2123 DES_key_schedule *s = key->schedule->data;
2124 if(ivec == NULL) {
2125 ivec = &local_ivec;
2126 memset(local_ivec, 0, sizeof(local_ivec));
2128 DES_ede3_cbc_encrypt(data, data, len, &s[0], &s[1], &s[2], ivec, encryptp);
2129 return 0;
2132 static krb5_error_code
2133 DES_CFB64_encrypt_null_ivec(krb5_context context,
2134 struct key_data *key,
2135 void *data,
2136 size_t len,
2137 krb5_boolean encryptp,
2138 int usage,
2139 void *ignore_ivec)
2141 DES_cblock ivec;
2142 int num = 0;
2143 DES_key_schedule *s = key->schedule->data;
2144 memset(&ivec, 0, sizeof(ivec));
2146 DES_cfb64_encrypt(data, data, len, s, &ivec, &num, encryptp);
2147 return 0;
2150 static krb5_error_code
2151 DES_PCBC_encrypt_key_ivec(krb5_context context,
2152 struct key_data *key,
2153 void *data,
2154 size_t len,
2155 krb5_boolean encryptp,
2156 int usage,
2157 void *ignore_ivec)
2159 DES_cblock ivec;
2160 DES_key_schedule *s = key->schedule->data;
2161 memcpy(&ivec, key->key->keyvalue.data, sizeof(ivec));
2163 DES_pcbc_encrypt(data, data, len, s, &ivec, encryptp);
2164 return 0;
2168 * AES draft-raeburn-krb-rijndael-krb-02
2171 void KRB5_LIB_FUNCTION
2172 _krb5_aes_cts_encrypt(const unsigned char *in, unsigned char *out,
2173 size_t len, const AES_KEY *key,
2174 unsigned char *ivec, const int encryptp)
2176 unsigned char tmp[AES_BLOCK_SIZE];
2177 int i;
2180 * In the framework of kerberos, the length can never be shorter
2181 * then at least one blocksize.
2184 if (encryptp) {
2186 while(len > AES_BLOCK_SIZE) {
2187 for (i = 0; i < AES_BLOCK_SIZE; i++)
2188 tmp[i] = in[i] ^ ivec[i];
2189 AES_encrypt(tmp, out, key);
2190 memcpy(ivec, out, AES_BLOCK_SIZE);
2191 len -= AES_BLOCK_SIZE;
2192 in += AES_BLOCK_SIZE;
2193 out += AES_BLOCK_SIZE;
2196 for (i = 0; i < len; i++)
2197 tmp[i] = in[i] ^ ivec[i];
2198 for (; i < AES_BLOCK_SIZE; i++)
2199 tmp[i] = 0 ^ ivec[i];
2201 AES_encrypt(tmp, out - AES_BLOCK_SIZE, key);
2203 memcpy(out, ivec, len);
2204 memcpy(ivec, out - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
2206 } else {
2207 unsigned char tmp2[AES_BLOCK_SIZE];
2208 unsigned char tmp3[AES_BLOCK_SIZE];
2210 while(len > AES_BLOCK_SIZE * 2) {
2211 memcpy(tmp, in, AES_BLOCK_SIZE);
2212 AES_decrypt(in, out, key);
2213 for (i = 0; i < AES_BLOCK_SIZE; i++)
2214 out[i] ^= ivec[i];
2215 memcpy(ivec, tmp, AES_BLOCK_SIZE);
2216 len -= AES_BLOCK_SIZE;
2217 in += AES_BLOCK_SIZE;
2218 out += AES_BLOCK_SIZE;
2221 len -= AES_BLOCK_SIZE;
2223 memcpy(tmp, in, AES_BLOCK_SIZE); /* save last iv */
2224 AES_decrypt(in, tmp2, key);
2226 memcpy(tmp3, in + AES_BLOCK_SIZE, len);
2227 memcpy(tmp3 + len, tmp2 + len, AES_BLOCK_SIZE - len); /* xor 0 */
2229 for (i = 0; i < len; i++)
2230 out[i + AES_BLOCK_SIZE] = tmp2[i] ^ tmp3[i];
2232 AES_decrypt(tmp3, out, key);
2233 for (i = 0; i < AES_BLOCK_SIZE; i++)
2234 out[i] ^= ivec[i];
2235 memcpy(ivec, tmp, AES_BLOCK_SIZE);
2239 static krb5_error_code
2240 AES_CTS_encrypt(krb5_context context,
2241 struct key_data *key,
2242 void *data,
2243 size_t len,
2244 krb5_boolean encryptp,
2245 int usage,
2246 void *ivec)
2248 struct krb5_aes_schedule *aeskey = key->schedule->data;
2249 char local_ivec[AES_BLOCK_SIZE];
2250 AES_KEY *k;
2252 if (encryptp)
2253 k = &aeskey->ekey;
2254 else
2255 k = &aeskey->dkey;
2257 if (len < AES_BLOCK_SIZE)
2258 krb5_abortx(context, "invalid use of AES_CTS_encrypt");
2259 if (len == AES_BLOCK_SIZE) {
2260 if (encryptp)
2261 AES_encrypt(data, data, k);
2262 else
2263 AES_decrypt(data, data, k);
2264 } else {
2265 if(ivec == NULL) {
2266 memset(local_ivec, 0, sizeof(local_ivec));
2267 ivec = local_ivec;
2269 _krb5_aes_cts_encrypt(data, data, len, k, ivec, encryptp);
2272 return 0;
2276 * section 6 of draft-brezak-win2k-krb-rc4-hmac-03
2278 * warning: not for small children
2281 static krb5_error_code
2282 ARCFOUR_subencrypt(krb5_context context,
2283 struct key_data *key,
2284 void *data,
2285 size_t len,
2286 unsigned usage,
2287 void *ivec)
2289 struct checksum_type *c = _find_checksum (CKSUMTYPE_RSA_MD5);
2290 Checksum k1_c, k2_c, k3_c, cksum;
2291 struct key_data ke;
2292 krb5_keyblock kb;
2293 unsigned char t[4];
2294 RC4_KEY rc4_key;
2295 unsigned char *cdata = data;
2296 unsigned char k1_c_data[16], k2_c_data[16], k3_c_data[16];
2297 krb5_error_code ret;
2299 t[0] = (usage >> 0) & 0xFF;
2300 t[1] = (usage >> 8) & 0xFF;
2301 t[2] = (usage >> 16) & 0xFF;
2302 t[3] = (usage >> 24) & 0xFF;
2304 k1_c.checksum.length = sizeof(k1_c_data);
2305 k1_c.checksum.data = k1_c_data;
2307 ret = hmac(NULL, c, t, sizeof(t), 0, key, &k1_c);
2308 if (ret)
2309 krb5_abortx(context, "hmac failed");
2311 memcpy (k2_c_data, k1_c_data, sizeof(k1_c_data));
2313 k2_c.checksum.length = sizeof(k2_c_data);
2314 k2_c.checksum.data = k2_c_data;
2316 ke.key = &kb;
2317 kb.keyvalue = k2_c.checksum;
2319 cksum.checksum.length = 16;
2320 cksum.checksum.data = data;
2322 ret = hmac(NULL, c, cdata + 16, len - 16, 0, &ke, &cksum);
2323 if (ret)
2324 krb5_abortx(context, "hmac failed");
2326 ke.key = &kb;
2327 kb.keyvalue = k1_c.checksum;
2329 k3_c.checksum.length = sizeof(k3_c_data);
2330 k3_c.checksum.data = k3_c_data;
2332 ret = hmac(NULL, c, data, 16, 0, &ke, &k3_c);
2333 if (ret)
2334 krb5_abortx(context, "hmac failed");
2336 RC4_set_key (&rc4_key, k3_c.checksum.length, k3_c.checksum.data);
2337 RC4 (&rc4_key, len - 16, cdata + 16, cdata + 16);
2338 memset (k1_c_data, 0, sizeof(k1_c_data));
2339 memset (k2_c_data, 0, sizeof(k2_c_data));
2340 memset (k3_c_data, 0, sizeof(k3_c_data));
2341 return 0;
2344 static krb5_error_code
2345 ARCFOUR_subdecrypt(krb5_context context,
2346 struct key_data *key,
2347 void *data,
2348 size_t len,
2349 unsigned usage,
2350 void *ivec)
2352 struct checksum_type *c = _find_checksum (CKSUMTYPE_RSA_MD5);
2353 Checksum k1_c, k2_c, k3_c, cksum;
2354 struct key_data ke;
2355 krb5_keyblock kb;
2356 unsigned char t[4];
2357 RC4_KEY rc4_key;
2358 unsigned char *cdata = data;
2359 unsigned char k1_c_data[16], k2_c_data[16], k3_c_data[16];
2360 unsigned char cksum_data[16];
2361 krb5_error_code ret;
2363 t[0] = (usage >> 0) & 0xFF;
2364 t[1] = (usage >> 8) & 0xFF;
2365 t[2] = (usage >> 16) & 0xFF;
2366 t[3] = (usage >> 24) & 0xFF;
2368 k1_c.checksum.length = sizeof(k1_c_data);
2369 k1_c.checksum.data = k1_c_data;
2371 ret = hmac(NULL, c, t, sizeof(t), 0, key, &k1_c);
2372 if (ret)
2373 krb5_abortx(context, "hmac failed");
2375 memcpy (k2_c_data, k1_c_data, sizeof(k1_c_data));
2377 k2_c.checksum.length = sizeof(k2_c_data);
2378 k2_c.checksum.data = k2_c_data;
2380 ke.key = &kb;
2381 kb.keyvalue = k1_c.checksum;
2383 k3_c.checksum.length = sizeof(k3_c_data);
2384 k3_c.checksum.data = k3_c_data;
2386 ret = hmac(NULL, c, cdata, 16, 0, &ke, &k3_c);
2387 if (ret)
2388 krb5_abortx(context, "hmac failed");
2390 RC4_set_key (&rc4_key, k3_c.checksum.length, k3_c.checksum.data);
2391 RC4 (&rc4_key, len - 16, cdata + 16, cdata + 16);
2393 ke.key = &kb;
2394 kb.keyvalue = k2_c.checksum;
2396 cksum.checksum.length = 16;
2397 cksum.checksum.data = cksum_data;
2399 ret = hmac(NULL, c, cdata + 16, len - 16, 0, &ke, &cksum);
2400 if (ret)
2401 krb5_abortx(context, "hmac failed");
2403 memset (k1_c_data, 0, sizeof(k1_c_data));
2404 memset (k2_c_data, 0, sizeof(k2_c_data));
2405 memset (k3_c_data, 0, sizeof(k3_c_data));
2407 if (memcmp (cksum.checksum.data, data, 16) != 0) {
2408 krb5_clear_error_string (context);
2409 return KRB5KRB_AP_ERR_BAD_INTEGRITY;
2410 } else {
2411 return 0;
2416 * convert the usage numbers used in
2417 * draft-ietf-cat-kerb-key-derivation-00.txt to the ones in
2418 * draft-brezak-win2k-krb-rc4-hmac-04.txt
2421 static krb5_error_code
2422 usage2arcfour (krb5_context context, unsigned *usage)
2424 switch (*usage) {
2425 case KRB5_KU_AS_REP_ENC_PART : /* 3 */
2426 case KRB5_KU_TGS_REP_ENC_PART_SUB_KEY : /* 9 */
2427 *usage = 8;
2428 return 0;
2429 case KRB5_KU_USAGE_SEAL : /* 22 */
2430 *usage = 13;
2431 return 0;
2432 case KRB5_KU_USAGE_SIGN : /* 23 */
2433 *usage = 15;
2434 return 0;
2435 case KRB5_KU_USAGE_SEQ: /* 24 */
2436 *usage = 0;
2437 return 0;
2438 default :
2439 return 0;
2443 static krb5_error_code
2444 ARCFOUR_encrypt(krb5_context context,
2445 struct key_data *key,
2446 void *data,
2447 size_t len,
2448 krb5_boolean encryptp,
2449 int usage,
2450 void *ivec)
2452 krb5_error_code ret;
2453 unsigned keyusage = usage;
2455 if((ret = usage2arcfour (context, &keyusage)) != 0)
2456 return ret;
2458 if (encryptp)
2459 return ARCFOUR_subencrypt (context, key, data, len, keyusage, ivec);
2460 else
2461 return ARCFOUR_subdecrypt (context, key, data, len, keyusage, ivec);
2469 static krb5_error_code
2470 AES_PRF(krb5_context context,
2471 krb5_crypto crypto,
2472 const krb5_data *in,
2473 krb5_data *out)
2475 struct checksum_type *ct = crypto->et->checksum;
2476 krb5_error_code ret;
2477 Checksum result;
2478 krb5_keyblock *derived;
2480 result.cksumtype = ct->type;
2481 ret = krb5_data_alloc(&result.checksum, ct->checksumsize);
2482 if (ret) {
2483 krb5_set_error_string(context, "out memory");
2484 return ret;
2487 (*ct->checksum)(context, NULL, in->data, in->length, 0, &result);
2489 if (result.checksum.length < crypto->et->blocksize)
2490 krb5_abortx(context, "internal prf error");
2492 derived = NULL;
2493 ret = krb5_derive_key(context, crypto->key.key,
2494 crypto->et->type, "prf", 3, &derived);
2495 if (ret)
2496 krb5_abortx(context, "krb5_derive_key");
2498 ret = krb5_data_alloc(out, crypto->et->blocksize);
2499 if (ret)
2500 krb5_abortx(context, "malloc failed");
2503 AES_KEY key;
2505 AES_set_encrypt_key(derived->keyvalue.data,
2506 crypto->et->keytype->bits, &key);
2507 AES_encrypt(result.checksum.data, out->data, &key);
2508 memset(&key, 0, sizeof(key));
2511 krb5_data_free(&result.checksum);
2512 krb5_free_keyblock(context, derived);
2514 return ret;
2518 * these should currently be in reverse preference order.
2519 * (only relevant for !F_PSEUDO) */
2521 static struct encryption_type enctype_null = {
2522 ETYPE_NULL,
2523 "null",
2524 NULL,
2528 &keytype_null,
2529 &checksum_none,
2530 NULL,
2531 F_DISABLED,
2532 NULL_encrypt,
2534 NULL
2536 static struct encryption_type enctype_des_cbc_crc = {
2537 ETYPE_DES_CBC_CRC,
2538 "des-cbc-crc",
2539 NULL,
2543 &keytype_des,
2544 &checksum_crc32,
2545 NULL,
2547 DES_CBC_encrypt_key_ivec,
2549 NULL
2551 static struct encryption_type enctype_des_cbc_md4 = {
2552 ETYPE_DES_CBC_MD4,
2553 "des-cbc-md4",
2554 NULL,
2558 &keytype_des,
2559 &checksum_rsa_md4,
2560 &checksum_rsa_md4_des,
2562 DES_CBC_encrypt_null_ivec,
2564 NULL
2566 static struct encryption_type enctype_des_cbc_md5 = {
2567 ETYPE_DES_CBC_MD5,
2568 "des-cbc-md5",
2569 NULL,
2573 &keytype_des,
2574 &checksum_rsa_md5,
2575 &checksum_rsa_md5_des,
2577 DES_CBC_encrypt_null_ivec,
2579 NULL
2581 static struct encryption_type enctype_arcfour_hmac_md5 = {
2582 ETYPE_ARCFOUR_HMAC_MD5,
2583 "arcfour-hmac-md5",
2584 NULL,
2588 &keytype_arcfour,
2589 &checksum_hmac_md5,
2590 NULL,
2591 F_SPECIAL,
2592 ARCFOUR_encrypt,
2594 NULL
2596 static struct encryption_type enctype_des3_cbc_md5 = {
2597 ETYPE_DES3_CBC_MD5,
2598 "des3-cbc-md5",
2599 NULL,
2603 &keytype_des3,
2604 &checksum_rsa_md5,
2605 &checksum_rsa_md5_des3,
2607 DES3_CBC_encrypt,
2609 NULL
2611 static struct encryption_type enctype_des3_cbc_sha1 = {
2612 ETYPE_DES3_CBC_SHA1,
2613 "des3-cbc-sha1",
2614 NULL,
2618 &keytype_des3_derived,
2619 &checksum_sha1,
2620 &checksum_hmac_sha1_des3,
2621 F_DERIVED,
2622 DES3_CBC_encrypt,
2624 NULL
2626 static struct encryption_type enctype_old_des3_cbc_sha1 = {
2627 ETYPE_OLD_DES3_CBC_SHA1,
2628 "old-des3-cbc-sha1",
2629 NULL,
2633 &keytype_des3,
2634 &checksum_sha1,
2635 &checksum_hmac_sha1_des3,
2637 DES3_CBC_encrypt,
2639 NULL
2641 static struct encryption_type enctype_aes128_cts_hmac_sha1 = {
2642 ETYPE_AES128_CTS_HMAC_SHA1_96,
2643 "aes128-cts-hmac-sha1-96",
2644 NULL,
2648 &keytype_aes128,
2649 &checksum_sha1,
2650 &checksum_hmac_sha1_aes128,
2651 F_DERIVED,
2652 AES_CTS_encrypt,
2654 AES_PRF
2656 static struct encryption_type enctype_aes256_cts_hmac_sha1 = {
2657 ETYPE_AES256_CTS_HMAC_SHA1_96,
2658 "aes256-cts-hmac-sha1-96",
2659 NULL,
2663 &keytype_aes256,
2664 &checksum_sha1,
2665 &checksum_hmac_sha1_aes256,
2666 F_DERIVED,
2667 AES_CTS_encrypt,
2669 AES_PRF
2671 static struct encryption_type enctype_des_cbc_none = {
2672 ETYPE_DES_CBC_NONE,
2673 "des-cbc-none",
2674 NULL,
2678 &keytype_des,
2679 &checksum_none,
2680 NULL,
2681 F_PSEUDO,
2682 DES_CBC_encrypt_null_ivec,
2684 NULL
2686 static struct encryption_type enctype_des_cfb64_none = {
2687 ETYPE_DES_CFB64_NONE,
2688 "des-cfb64-none",
2689 NULL,
2693 &keytype_des,
2694 &checksum_none,
2695 NULL,
2696 F_PSEUDO,
2697 DES_CFB64_encrypt_null_ivec,
2699 NULL
2701 static struct encryption_type enctype_des_pcbc_none = {
2702 ETYPE_DES_PCBC_NONE,
2703 "des-pcbc-none",
2704 NULL,
2708 &keytype_des,
2709 &checksum_none,
2710 NULL,
2711 F_PSEUDO,
2712 DES_PCBC_encrypt_key_ivec,
2714 NULL
2716 static struct encryption_type enctype_des3_cbc_none = {
2717 ETYPE_DES3_CBC_NONE,
2718 "des3-cbc-none",
2719 NULL,
2723 &keytype_des3_derived,
2724 &checksum_none,
2725 NULL,
2726 F_PSEUDO,
2727 DES3_CBC_encrypt,
2729 NULL
2732 static struct encryption_type *etypes[] = {
2733 &enctype_null,
2734 &enctype_des_cbc_crc,
2735 &enctype_des_cbc_md4,
2736 &enctype_des_cbc_md5,
2737 &enctype_arcfour_hmac_md5,
2738 &enctype_des3_cbc_md5,
2739 &enctype_des3_cbc_sha1,
2740 &enctype_old_des3_cbc_sha1,
2741 &enctype_aes128_cts_hmac_sha1,
2742 &enctype_aes256_cts_hmac_sha1,
2743 &enctype_des_cbc_none,
2744 &enctype_des_cfb64_none,
2745 &enctype_des_pcbc_none,
2746 &enctype_des3_cbc_none
2749 static unsigned num_etypes = sizeof(etypes) / sizeof(etypes[0]);
2752 static struct encryption_type *
2753 _find_enctype(krb5_enctype type)
2755 int i;
2756 for(i = 0; i < num_etypes; i++)
2757 if(etypes[i]->type == type)
2758 return etypes[i];
2759 return NULL;
2763 krb5_error_code KRB5_LIB_FUNCTION
2764 krb5_enctype_to_string(krb5_context context,
2765 krb5_enctype etype,
2766 char **string)
2768 struct encryption_type *e;
2769 e = _find_enctype(etype);
2770 if(e == NULL) {
2771 krb5_set_error_string (context, "encryption type %d not supported",
2772 etype);
2773 *string = NULL;
2774 return KRB5_PROG_ETYPE_NOSUPP;
2776 *string = strdup(e->name);
2777 if(*string == NULL) {
2778 krb5_set_error_string(context, "malloc: out of memory");
2779 return ENOMEM;
2781 return 0;
2784 krb5_error_code KRB5_LIB_FUNCTION
2785 krb5_string_to_enctype(krb5_context context,
2786 const char *string,
2787 krb5_enctype *etype)
2789 int i;
2790 for(i = 0; i < num_etypes; i++)
2791 if(strcasecmp(etypes[i]->name, string) == 0){
2792 *etype = etypes[i]->type;
2793 return 0;
2795 krb5_set_error_string (context, "encryption type %s not supported",
2796 string);
2797 return KRB5_PROG_ETYPE_NOSUPP;
2800 krb5_error_code KRB5_LIB_FUNCTION
2801 _krb5_enctype_to_oid(krb5_context context,
2802 krb5_enctype etype,
2803 heim_oid *oid)
2805 struct encryption_type *et = _find_enctype(etype);
2806 if(et == NULL) {
2807 krb5_set_error_string (context, "encryption type %d not supported",
2808 etype);
2809 return KRB5_PROG_ETYPE_NOSUPP;
2811 if(et->oid == NULL) {
2812 krb5_set_error_string (context, "%s have not oid", et->name);
2813 return KRB5_PROG_ETYPE_NOSUPP;
2815 krb5_clear_error_string(context);
2816 return der_copy_oid(et->oid, oid);
2819 krb5_error_code KRB5_LIB_FUNCTION
2820 _krb5_oid_to_enctype(krb5_context context,
2821 const heim_oid *oid,
2822 krb5_enctype *etype)
2824 int i;
2825 for(i = 0; i < num_etypes; i++) {
2826 if(etypes[i]->oid && der_heim_oid_cmp(etypes[i]->oid, oid) == 0) {
2827 *etype = etypes[i]->type;
2828 return 0;
2831 krb5_set_error_string(context, "enctype for oid not supported");
2832 return KRB5_PROG_ETYPE_NOSUPP;
2835 krb5_error_code KRB5_LIB_FUNCTION
2836 krb5_enctype_to_keytype(krb5_context context,
2837 krb5_enctype etype,
2838 krb5_keytype *keytype)
2840 struct encryption_type *e = _find_enctype(etype);
2841 if(e == NULL) {
2842 krb5_set_error_string (context, "encryption type %d not supported",
2843 etype);
2844 return KRB5_PROG_ETYPE_NOSUPP;
2846 *keytype = e->keytype->type; /* XXX */
2847 return 0;
2850 #if 0
2851 krb5_error_code KRB5_LIB_FUNCTION
2852 krb5_keytype_to_enctype(krb5_context context,
2853 krb5_keytype keytype,
2854 krb5_enctype *etype)
2856 struct key_type *kt = _find_keytype(keytype);
2857 krb5_warnx(context, "krb5_keytype_to_enctype(%u)", keytype);
2858 if(kt == NULL)
2859 return KRB5_PROG_KEYTYPE_NOSUPP;
2860 *etype = kt->best_etype;
2861 return 0;
2863 #endif
2865 krb5_error_code KRB5_LIB_FUNCTION
2866 krb5_keytype_to_enctypes (krb5_context context,
2867 krb5_keytype keytype,
2868 unsigned *len,
2869 krb5_enctype **val)
2871 int i;
2872 unsigned n = 0;
2873 krb5_enctype *ret;
2875 for (i = num_etypes - 1; i >= 0; --i) {
2876 if (etypes[i]->keytype->type == keytype
2877 && !(etypes[i]->flags & F_PSEUDO))
2878 ++n;
2880 ret = malloc(n * sizeof(*ret));
2881 if (ret == NULL && n != 0) {
2882 krb5_set_error_string(context, "malloc: out of memory");
2883 return ENOMEM;
2885 n = 0;
2886 for (i = num_etypes - 1; i >= 0; --i) {
2887 if (etypes[i]->keytype->type == keytype
2888 && !(etypes[i]->flags & F_PSEUDO))
2889 ret[n++] = etypes[i]->type;
2891 *len = n;
2892 *val = ret;
2893 return 0;
2897 * First take the configured list of etypes for `keytype' if available,
2898 * else, do `krb5_keytype_to_enctypes'.
2901 krb5_error_code KRB5_LIB_FUNCTION
2902 krb5_keytype_to_enctypes_default (krb5_context context,
2903 krb5_keytype keytype,
2904 unsigned *len,
2905 krb5_enctype **val)
2907 int i, n;
2908 krb5_enctype *ret;
2910 if (keytype != KEYTYPE_DES || context->etypes_des == NULL)
2911 return krb5_keytype_to_enctypes (context, keytype, len, val);
2913 for (n = 0; context->etypes_des[n]; ++n)
2915 ret = malloc (n * sizeof(*ret));
2916 if (ret == NULL && n != 0) {
2917 krb5_set_error_string(context, "malloc: out of memory");
2918 return ENOMEM;
2920 for (i = 0; i < n; ++i)
2921 ret[i] = context->etypes_des[i];
2922 *len = n;
2923 *val = ret;
2924 return 0;
2927 krb5_error_code KRB5_LIB_FUNCTION
2928 krb5_enctype_valid(krb5_context context,
2929 krb5_enctype etype)
2931 struct encryption_type *e = _find_enctype(etype);
2932 if(e == NULL) {
2933 krb5_set_error_string (context, "encryption type %d not supported",
2934 etype);
2935 return KRB5_PROG_ETYPE_NOSUPP;
2937 if (e->flags & F_DISABLED) {
2938 krb5_set_error_string (context, "encryption type %s is disabled",
2939 e->name);
2940 return KRB5_PROG_ETYPE_NOSUPP;
2942 return 0;
2945 krb5_error_code KRB5_LIB_FUNCTION
2946 krb5_cksumtype_valid(krb5_context context,
2947 krb5_cksumtype ctype)
2949 struct checksum_type *c = _find_checksum(ctype);
2950 if (c == NULL) {
2951 krb5_set_error_string (context, "checksum type %d not supported",
2952 ctype);
2953 return KRB5_PROG_SUMTYPE_NOSUPP;
2955 if (c->flags & F_DISABLED) {
2956 krb5_set_error_string (context, "checksum type %s is disabled",
2957 c->name);
2958 return KRB5_PROG_SUMTYPE_NOSUPP;
2960 return 0;
2964 /* if two enctypes have compatible keys */
2965 krb5_boolean KRB5_LIB_FUNCTION
2966 krb5_enctypes_compatible_keys(krb5_context context,
2967 krb5_enctype etype1,
2968 krb5_enctype etype2)
2970 struct encryption_type *e1 = _find_enctype(etype1);
2971 struct encryption_type *e2 = _find_enctype(etype2);
2972 return e1 != NULL && e2 != NULL && e1->keytype == e2->keytype;
2975 static krb5_boolean
2976 derived_crypto(krb5_context context,
2977 krb5_crypto crypto)
2979 return (crypto->et->flags & F_DERIVED) != 0;
2982 static krb5_boolean
2983 special_crypto(krb5_context context,
2984 krb5_crypto crypto)
2986 return (crypto->et->flags & F_SPECIAL) != 0;
2989 #define CHECKSUMSIZE(C) ((C)->checksumsize)
2990 #define CHECKSUMTYPE(C) ((C)->type)
2992 static krb5_error_code
2993 encrypt_internal_derived(krb5_context context,
2994 krb5_crypto crypto,
2995 unsigned usage,
2996 const void *data,
2997 size_t len,
2998 krb5_data *result,
2999 void *ivec)
3001 size_t sz, block_sz, checksum_sz, total_sz;
3002 Checksum cksum;
3003 unsigned char *p, *q;
3004 krb5_error_code ret;
3005 struct key_data *dkey;
3006 const struct encryption_type *et = crypto->et;
3008 checksum_sz = CHECKSUMSIZE(et->keyed_checksum);
3010 sz = et->confoundersize + len;
3011 block_sz = (sz + et->padsize - 1) &~ (et->padsize - 1); /* pad */
3012 total_sz = block_sz + checksum_sz;
3013 p = calloc(1, total_sz);
3014 if(p == NULL) {
3015 krb5_set_error_string(context, "malloc: out of memory");
3016 return ENOMEM;
3019 q = p;
3020 krb5_generate_random_block(q, et->confoundersize); /* XXX */
3021 q += et->confoundersize;
3022 memcpy(q, data, len);
3024 ret = create_checksum(context,
3025 et->keyed_checksum,
3026 crypto,
3027 INTEGRITY_USAGE(usage),
3029 block_sz,
3030 &cksum);
3031 if(ret == 0 && cksum.checksum.length != checksum_sz) {
3032 free_Checksum (&cksum);
3033 krb5_clear_error_string (context);
3034 ret = KRB5_CRYPTO_INTERNAL;
3036 if(ret)
3037 goto fail;
3038 memcpy(p + block_sz, cksum.checksum.data, cksum.checksum.length);
3039 free_Checksum (&cksum);
3040 ret = _get_derived_key(context, crypto, ENCRYPTION_USAGE(usage), &dkey);
3041 if(ret)
3042 goto fail;
3043 ret = _key_schedule(context, dkey);
3044 if(ret)
3045 goto fail;
3046 #ifdef CRYPTO_DEBUG
3047 krb5_crypto_debug(context, 1, block_sz, dkey->key);
3048 #endif
3049 ret = (*et->encrypt)(context, dkey, p, block_sz, 1, usage, ivec);
3050 if (ret)
3051 goto fail;
3052 result->data = p;
3053 result->length = total_sz;
3054 return 0;
3055 fail:
3056 memset(p, 0, total_sz);
3057 free(p);
3058 return ret;
3062 static krb5_error_code
3063 encrypt_internal(krb5_context context,
3064 krb5_crypto crypto,
3065 const void *data,
3066 size_t len,
3067 krb5_data *result,
3068 void *ivec)
3070 size_t sz, block_sz, checksum_sz;
3071 Checksum cksum;
3072 unsigned char *p, *q;
3073 krb5_error_code ret;
3074 const struct encryption_type *et = crypto->et;
3076 checksum_sz = CHECKSUMSIZE(et->checksum);
3078 sz = et->confoundersize + checksum_sz + len;
3079 block_sz = (sz + et->padsize - 1) &~ (et->padsize - 1); /* pad */
3080 p = calloc(1, block_sz);
3081 if(p == NULL) {
3082 krb5_set_error_string(context, "malloc: out of memory");
3083 return ENOMEM;
3086 q = p;
3087 krb5_generate_random_block(q, et->confoundersize); /* XXX */
3088 q += et->confoundersize;
3089 memset(q, 0, checksum_sz);
3090 q += checksum_sz;
3091 memcpy(q, data, len);
3093 ret = create_checksum(context,
3094 et->checksum,
3095 crypto,
3098 block_sz,
3099 &cksum);
3100 if(ret == 0 && cksum.checksum.length != checksum_sz) {
3101 krb5_clear_error_string (context);
3102 free_Checksum(&cksum);
3103 ret = KRB5_CRYPTO_INTERNAL;
3105 if(ret)
3106 goto fail;
3107 memcpy(p + et->confoundersize, cksum.checksum.data, cksum.checksum.length);
3108 free_Checksum(&cksum);
3109 ret = _key_schedule(context, &crypto->key);
3110 if(ret)
3111 goto fail;
3112 #ifdef CRYPTO_DEBUG
3113 krb5_crypto_debug(context, 1, block_sz, crypto->key.key);
3114 #endif
3115 ret = (*et->encrypt)(context, &crypto->key, p, block_sz, 1, 0, ivec);
3116 if (ret) {
3117 memset(p, 0, block_sz);
3118 free(p);
3119 return ret;
3121 result->data = p;
3122 result->length = block_sz;
3123 return 0;
3124 fail:
3125 memset(p, 0, block_sz);
3126 free(p);
3127 return ret;
3130 static krb5_error_code
3131 encrypt_internal_special(krb5_context context,
3132 krb5_crypto crypto,
3133 int usage,
3134 const void *data,
3135 size_t len,
3136 krb5_data *result,
3137 void *ivec)
3139 struct encryption_type *et = crypto->et;
3140 size_t cksum_sz = CHECKSUMSIZE(et->checksum);
3141 size_t sz = len + cksum_sz + et->confoundersize;
3142 char *tmp, *p;
3143 krb5_error_code ret;
3145 tmp = malloc (sz);
3146 if (tmp == NULL) {
3147 krb5_set_error_string(context, "malloc: out of memory");
3148 return ENOMEM;
3150 p = tmp;
3151 memset (p, 0, cksum_sz);
3152 p += cksum_sz;
3153 krb5_generate_random_block(p, et->confoundersize);
3154 p += et->confoundersize;
3155 memcpy (p, data, len);
3156 ret = (*et->encrypt)(context, &crypto->key, tmp, sz, TRUE, usage, ivec);
3157 if (ret) {
3158 memset(tmp, 0, sz);
3159 free(tmp);
3160 return ret;
3162 result->data = tmp;
3163 result->length = sz;
3164 return 0;
3167 static krb5_error_code
3168 decrypt_internal_derived(krb5_context context,
3169 krb5_crypto crypto,
3170 unsigned usage,
3171 void *data,
3172 size_t len,
3173 krb5_data *result,
3174 void *ivec)
3176 size_t checksum_sz;
3177 Checksum cksum;
3178 unsigned char *p;
3179 krb5_error_code ret;
3180 struct key_data *dkey;
3181 struct encryption_type *et = crypto->et;
3182 unsigned long l;
3184 checksum_sz = CHECKSUMSIZE(et->keyed_checksum);
3185 if (len < checksum_sz + et->confoundersize) {
3186 krb5_set_error_string(context, "Encrypted data shorter then "
3187 "checksum + confunder");
3188 return KRB5_BAD_MSIZE;
3191 if (((len - checksum_sz) % et->padsize) != 0) {
3192 krb5_clear_error_string(context);
3193 return KRB5_BAD_MSIZE;
3196 p = malloc(len);
3197 if(len != 0 && p == NULL) {
3198 krb5_set_error_string(context, "malloc: out of memory");
3199 return ENOMEM;
3201 memcpy(p, data, len);
3203 len -= checksum_sz;
3205 ret = _get_derived_key(context, crypto, ENCRYPTION_USAGE(usage), &dkey);
3206 if(ret) {
3207 free(p);
3208 return ret;
3210 ret = _key_schedule(context, dkey);
3211 if(ret) {
3212 free(p);
3213 return ret;
3215 #ifdef CRYPTO_DEBUG
3216 krb5_crypto_debug(context, 0, len, dkey->key);
3217 #endif
3218 ret = (*et->encrypt)(context, dkey, p, len, 0, usage, ivec);
3219 if (ret) {
3220 free(p);
3221 return ret;
3224 cksum.checksum.data = p + len;
3225 cksum.checksum.length = checksum_sz;
3226 cksum.cksumtype = CHECKSUMTYPE(et->keyed_checksum);
3228 ret = verify_checksum(context,
3229 crypto,
3230 INTEGRITY_USAGE(usage),
3232 len,
3233 &cksum);
3234 if(ret) {
3235 free(p);
3236 return ret;
3238 l = len - et->confoundersize;
3239 memmove(p, p + et->confoundersize, l);
3240 result->data = realloc(p, l);
3241 if(result->data == NULL && l != 0) {
3242 free(p);
3243 krb5_set_error_string(context, "malloc: out of memory");
3244 return ENOMEM;
3246 result->length = l;
3247 return 0;
3250 static krb5_error_code
3251 decrypt_internal(krb5_context context,
3252 krb5_crypto crypto,
3253 void *data,
3254 size_t len,
3255 krb5_data *result,
3256 void *ivec)
3258 krb5_error_code ret;
3259 unsigned char *p;
3260 Checksum cksum;
3261 size_t checksum_sz, l;
3262 struct encryption_type *et = crypto->et;
3264 if ((len % et->padsize) != 0) {
3265 krb5_clear_error_string(context);
3266 return KRB5_BAD_MSIZE;
3269 checksum_sz = CHECKSUMSIZE(et->checksum);
3270 p = malloc(len);
3271 if(len != 0 && p == NULL) {
3272 krb5_set_error_string(context, "malloc: out of memory");
3273 return ENOMEM;
3275 memcpy(p, data, len);
3277 ret = _key_schedule(context, &crypto->key);
3278 if(ret) {
3279 free(p);
3280 return ret;
3282 #ifdef CRYPTO_DEBUG
3283 krb5_crypto_debug(context, 0, len, crypto->key.key);
3284 #endif
3285 ret = (*et->encrypt)(context, &crypto->key, p, len, 0, 0, ivec);
3286 if (ret) {
3287 free(p);
3288 return ret;
3290 ret = krb5_data_copy(&cksum.checksum, p + et->confoundersize, checksum_sz);
3291 if(ret) {
3292 free(p);
3293 return ret;
3295 memset(p + et->confoundersize, 0, checksum_sz);
3296 cksum.cksumtype = CHECKSUMTYPE(et->checksum);
3297 ret = verify_checksum(context, NULL, 0, p, len, &cksum);
3298 free_Checksum(&cksum);
3299 if(ret) {
3300 free(p);
3301 return ret;
3303 l = len - et->confoundersize - checksum_sz;
3304 memmove(p, p + et->confoundersize + checksum_sz, l);
3305 result->data = realloc(p, l);
3306 if(result->data == NULL && l != 0) {
3307 free(p);
3308 krb5_set_error_string(context, "malloc: out of memory");
3309 return ENOMEM;
3311 result->length = l;
3312 return 0;
3315 static krb5_error_code
3316 decrypt_internal_special(krb5_context context,
3317 krb5_crypto crypto,
3318 int usage,
3319 void *data,
3320 size_t len,
3321 krb5_data *result,
3322 void *ivec)
3324 struct encryption_type *et = crypto->et;
3325 size_t cksum_sz = CHECKSUMSIZE(et->checksum);
3326 size_t sz = len - cksum_sz - et->confoundersize;
3327 unsigned char *p;
3328 krb5_error_code ret;
3330 if ((len % et->padsize) != 0) {
3331 krb5_clear_error_string(context);
3332 return KRB5_BAD_MSIZE;
3335 p = malloc (len);
3336 if (p == NULL) {
3337 krb5_set_error_string(context, "malloc: out of memory");
3338 return ENOMEM;
3340 memcpy(p, data, len);
3342 ret = (*et->encrypt)(context, &crypto->key, p, len, FALSE, usage, ivec);
3343 if (ret) {
3344 free(p);
3345 return ret;
3348 memmove (p, p + cksum_sz + et->confoundersize, sz);
3349 result->data = realloc(p, sz);
3350 if(result->data == NULL && sz != 0) {
3351 free(p);
3352 krb5_set_error_string(context, "malloc: out of memory");
3353 return ENOMEM;
3355 result->length = sz;
3356 return 0;
3360 krb5_error_code KRB5_LIB_FUNCTION
3361 krb5_encrypt_ivec(krb5_context context,
3362 krb5_crypto crypto,
3363 unsigned usage,
3364 const void *data,
3365 size_t len,
3366 krb5_data *result,
3367 void *ivec)
3369 if(derived_crypto(context, crypto))
3370 return encrypt_internal_derived(context, crypto, usage,
3371 data, len, result, ivec);
3372 else if (special_crypto(context, crypto))
3373 return encrypt_internal_special (context, crypto, usage,
3374 data, len, result, ivec);
3375 else
3376 return encrypt_internal(context, crypto, data, len, result, ivec);
3379 krb5_error_code KRB5_LIB_FUNCTION
3380 krb5_encrypt(krb5_context context,
3381 krb5_crypto crypto,
3382 unsigned usage,
3383 const void *data,
3384 size_t len,
3385 krb5_data *result)
3387 return krb5_encrypt_ivec(context, crypto, usage, data, len, result, NULL);
3390 krb5_error_code KRB5_LIB_FUNCTION
3391 krb5_encrypt_EncryptedData(krb5_context context,
3392 krb5_crypto crypto,
3393 unsigned usage,
3394 void *data,
3395 size_t len,
3396 int kvno,
3397 EncryptedData *result)
3399 result->etype = CRYPTO_ETYPE(crypto);
3400 if(kvno){
3401 ALLOC(result->kvno, 1);
3402 *result->kvno = kvno;
3403 }else
3404 result->kvno = NULL;
3405 return krb5_encrypt(context, crypto, usage, data, len, &result->cipher);
3408 krb5_error_code KRB5_LIB_FUNCTION
3409 krb5_decrypt_ivec(krb5_context context,
3410 krb5_crypto crypto,
3411 unsigned usage,
3412 void *data,
3413 size_t len,
3414 krb5_data *result,
3415 void *ivec)
3417 if(derived_crypto(context, crypto))
3418 return decrypt_internal_derived(context, crypto, usage,
3419 data, len, result, ivec);
3420 else if (special_crypto (context, crypto))
3421 return decrypt_internal_special(context, crypto, usage,
3422 data, len, result, ivec);
3423 else
3424 return decrypt_internal(context, crypto, data, len, result, ivec);
3427 krb5_error_code KRB5_LIB_FUNCTION
3428 krb5_decrypt(krb5_context context,
3429 krb5_crypto crypto,
3430 unsigned usage,
3431 void *data,
3432 size_t len,
3433 krb5_data *result)
3435 return krb5_decrypt_ivec (context, crypto, usage, data, len, result,
3436 NULL);
3439 krb5_error_code KRB5_LIB_FUNCTION
3440 krb5_decrypt_EncryptedData(krb5_context context,
3441 krb5_crypto crypto,
3442 unsigned usage,
3443 const EncryptedData *e,
3444 krb5_data *result)
3446 return krb5_decrypt(context, crypto, usage,
3447 e->cipher.data, e->cipher.length, result);
3450 /************************************************************
3452 ************************************************************/
3454 #define ENTROPY_NEEDED 128
3456 static int
3457 seed_something(void)
3459 char buf[1024], seedfile[256];
3461 /* If there is a seed file, load it. But such a file cannot be trusted,
3462 so use 0 for the entropy estimate */
3463 if (RAND_file_name(seedfile, sizeof(seedfile))) {
3464 int fd;
3465 fd = open(seedfile, O_RDONLY);
3466 if (fd >= 0) {
3467 ssize_t ret;
3468 ret = read(fd, buf, sizeof(buf));
3469 if (ret > 0)
3470 RAND_add(buf, ret, 0.0);
3471 close(fd);
3472 } else
3473 seedfile[0] = '\0';
3474 } else
3475 seedfile[0] = '\0';
3477 /* Calling RAND_status() will try to use /dev/urandom if it exists so
3478 we do not have to deal with it. */
3479 if (RAND_status() != 1) {
3480 krb5_context context;
3481 const char *p;
3483 /* Try using egd */
3484 if (!krb5_init_context(&context)) {
3485 p = krb5_config_get_string(context, NULL, "libdefaults",
3486 "egd_socket", NULL);
3487 if (p != NULL)
3488 RAND_egd_bytes(p, ENTROPY_NEEDED);
3489 krb5_free_context(context);
3493 if (RAND_status() == 1) {
3494 /* Update the seed file */
3495 if (seedfile[0])
3496 RAND_write_file(seedfile);
3498 return 0;
3499 } else
3500 return -1;
3503 void KRB5_LIB_FUNCTION
3504 krb5_generate_random_block(void *buf, size_t len)
3506 static int rng_initialized = 0;
3508 HEIMDAL_MUTEX_lock(&crypto_mutex);
3509 if (!rng_initialized) {
3510 if (seed_something())
3511 krb5_abortx(NULL, "Fatal: could not seed the "
3512 "random number generator");
3514 rng_initialized = 1;
3516 HEIMDAL_MUTEX_unlock(&crypto_mutex);
3517 if (RAND_bytes(buf, len) != 1)
3518 krb5_abortx(NULL, "Failed to generate random block");
3521 static void
3522 DES3_postproc(krb5_context context,
3523 unsigned char *k, size_t len, struct key_data *key)
3525 DES3_random_to_key(context, key->key, k, len);
3527 if (key->schedule) {
3528 krb5_free_data(context, key->schedule);
3529 key->schedule = NULL;
3533 static krb5_error_code
3534 derive_key(krb5_context context,
3535 struct encryption_type *et,
3536 struct key_data *key,
3537 const void *constant,
3538 size_t len)
3540 unsigned char *k;
3541 unsigned int nblocks = 0, i;
3542 krb5_error_code ret = 0;
3543 struct key_type *kt = et->keytype;
3545 ret = _key_schedule(context, key);
3546 if(ret)
3547 return ret;
3548 if(et->blocksize * 8 < kt->bits || len != et->blocksize) {
3549 nblocks = (kt->bits + et->blocksize * 8 - 1) / (et->blocksize * 8);
3550 k = malloc(nblocks * et->blocksize);
3551 if(k == NULL) {
3552 krb5_set_error_string(context, "malloc: out of memory");
3553 return ENOMEM;
3555 ret = _krb5_n_fold(constant, len, k, et->blocksize);
3556 if (ret) {
3557 free(k);
3558 krb5_set_error_string(context, "out of memory");
3559 return ret;
3561 for(i = 0; i < nblocks; i++) {
3562 if(i > 0)
3563 memcpy(k + i * et->blocksize,
3564 k + (i - 1) * et->blocksize,
3565 et->blocksize);
3566 (*et->encrypt)(context, key, k + i * et->blocksize, et->blocksize,
3567 1, 0, NULL);
3569 } else {
3570 /* this case is probably broken, but won't be run anyway */
3571 void *c = malloc(len);
3572 size_t res_len = (kt->bits + 7) / 8;
3574 if(len != 0 && c == NULL) {
3575 krb5_set_error_string(context, "malloc: out of memory");
3576 return ENOMEM;
3578 memcpy(c, constant, len);
3579 (*et->encrypt)(context, key, c, len, 1, 0, NULL);
3580 k = malloc(res_len);
3581 if(res_len != 0 && k == NULL) {
3582 free(c);
3583 krb5_set_error_string(context, "malloc: out of memory");
3584 return ENOMEM;
3586 ret = _krb5_n_fold(c, len, k, res_len);
3587 if (ret) {
3588 free(k);
3589 krb5_set_error_string(context, "out of memory");
3590 return ret;
3592 free(c);
3595 /* XXX keytype dependent post-processing */
3596 switch(kt->type) {
3597 case KEYTYPE_DES3:
3598 DES3_postproc(context, k, nblocks * et->blocksize, key);
3599 break;
3600 case KEYTYPE_AES128:
3601 case KEYTYPE_AES256:
3602 memcpy(key->key->keyvalue.data, k, key->key->keyvalue.length);
3603 break;
3604 default:
3605 krb5_set_error_string(context,
3606 "derive_key() called with unknown keytype (%u)",
3607 kt->type);
3608 ret = KRB5_CRYPTO_INTERNAL;
3609 break;
3611 if (key->schedule) {
3612 krb5_free_data(context, key->schedule);
3613 key->schedule = NULL;
3615 memset(k, 0, nblocks * et->blocksize);
3616 free(k);
3617 return ret;
3620 static struct key_data *
3621 _new_derived_key(krb5_crypto crypto, unsigned usage)
3623 struct key_usage *d = crypto->key_usage;
3624 d = realloc(d, (crypto->num_key_usage + 1) * sizeof(*d));
3625 if(d == NULL)
3626 return NULL;
3627 crypto->key_usage = d;
3628 d += crypto->num_key_usage++;
3629 memset(d, 0, sizeof(*d));
3630 d->usage = usage;
3631 return &d->key;
3634 krb5_error_code KRB5_LIB_FUNCTION
3635 krb5_derive_key(krb5_context context,
3636 const krb5_keyblock *key,
3637 krb5_enctype etype,
3638 const void *constant,
3639 size_t constant_len,
3640 krb5_keyblock **derived_key)
3642 krb5_error_code ret;
3643 struct encryption_type *et;
3644 struct key_data d;
3646 *derived_key = NULL;
3648 et = _find_enctype (etype);
3649 if (et == NULL) {
3650 krb5_set_error_string(context, "encryption type %d not supported",
3651 etype);
3652 return KRB5_PROG_ETYPE_NOSUPP;
3655 ret = krb5_copy_keyblock(context, key, &d.key);
3656 if (ret)
3657 return ret;
3659 d.schedule = NULL;
3660 ret = derive_key(context, et, &d, constant, constant_len);
3661 if (ret == 0)
3662 ret = krb5_copy_keyblock(context, d.key, derived_key);
3663 free_key_data(context, &d);
3664 return ret;
3667 static krb5_error_code
3668 _get_derived_key(krb5_context context,
3669 krb5_crypto crypto,
3670 unsigned usage,
3671 struct key_data **key)
3673 int i;
3674 struct key_data *d;
3675 unsigned char constant[5];
3677 for(i = 0; i < crypto->num_key_usage; i++)
3678 if(crypto->key_usage[i].usage == usage) {
3679 *key = &crypto->key_usage[i].key;
3680 return 0;
3682 d = _new_derived_key(crypto, usage);
3683 if(d == NULL) {
3684 krb5_set_error_string(context, "malloc: out of memory");
3685 return ENOMEM;
3687 krb5_copy_keyblock(context, crypto->key.key, &d->key);
3688 _krb5_put_int(constant, usage, 5);
3689 derive_key(context, crypto->et, d, constant, sizeof(constant));
3690 *key = d;
3691 return 0;
3695 krb5_error_code KRB5_LIB_FUNCTION
3696 krb5_crypto_init(krb5_context context,
3697 const krb5_keyblock *key,
3698 krb5_enctype etype,
3699 krb5_crypto *crypto)
3701 krb5_error_code ret;
3702 ALLOC(*crypto, 1);
3703 if(*crypto == NULL) {
3704 krb5_set_error_string(context, "malloc: out of memory");
3705 return ENOMEM;
3707 if(etype == ETYPE_NULL)
3708 etype = key->keytype;
3709 (*crypto)->et = _find_enctype(etype);
3710 if((*crypto)->et == NULL || ((*crypto)->et->flags & F_DISABLED)) {
3711 free(*crypto);
3712 *crypto = NULL;
3713 krb5_set_error_string (context, "encryption type %d not supported",
3714 etype);
3715 return KRB5_PROG_ETYPE_NOSUPP;
3717 if((*crypto)->et->keytype->size != key->keyvalue.length) {
3718 free(*crypto);
3719 *crypto = NULL;
3720 krb5_set_error_string (context, "encryption key has bad length");
3721 return KRB5_BAD_KEYSIZE;
3723 ret = krb5_copy_keyblock(context, key, &(*crypto)->key.key);
3724 if(ret) {
3725 free(*crypto);
3726 *crypto = NULL;
3727 return ret;
3729 (*crypto)->key.schedule = NULL;
3730 (*crypto)->num_key_usage = 0;
3731 (*crypto)->key_usage = NULL;
3732 return 0;
3735 static void
3736 free_key_data(krb5_context context, struct key_data *key)
3738 krb5_free_keyblock(context, key->key);
3739 if(key->schedule) {
3740 memset(key->schedule->data, 0, key->schedule->length);
3741 krb5_free_data(context, key->schedule);
3745 static void
3746 free_key_usage(krb5_context context, struct key_usage *ku)
3748 free_key_data(context, &ku->key);
3751 krb5_error_code KRB5_LIB_FUNCTION
3752 krb5_crypto_destroy(krb5_context context,
3753 krb5_crypto crypto)
3755 int i;
3757 for(i = 0; i < crypto->num_key_usage; i++)
3758 free_key_usage(context, &crypto->key_usage[i]);
3759 free(crypto->key_usage);
3760 free_key_data(context, &crypto->key);
3761 free (crypto);
3762 return 0;
3765 krb5_error_code KRB5_LIB_FUNCTION
3766 krb5_crypto_getblocksize(krb5_context context,
3767 krb5_crypto crypto,
3768 size_t *blocksize)
3770 *blocksize = crypto->et->blocksize;
3771 return 0;
3774 krb5_error_code KRB5_LIB_FUNCTION
3775 krb5_crypto_getenctype(krb5_context context,
3776 krb5_crypto crypto,
3777 krb5_enctype *enctype)
3779 *enctype = crypto->et->type;
3780 return 0;
3783 krb5_error_code KRB5_LIB_FUNCTION
3784 krb5_crypto_getpadsize(krb5_context context,
3785 krb5_crypto crypto,
3786 size_t *padsize)
3788 *padsize = crypto->et->padsize;
3789 return 0;
3792 krb5_error_code KRB5_LIB_FUNCTION
3793 krb5_crypto_getconfoundersize(krb5_context context,
3794 krb5_crypto crypto,
3795 size_t *confoundersize)
3797 *confoundersize = crypto->et->confoundersize;
3798 return 0;
3801 krb5_error_code KRB5_LIB_FUNCTION
3802 krb5_enctype_disable(krb5_context context,
3803 krb5_enctype enctype)
3805 struct encryption_type *et = _find_enctype(enctype);
3806 if(et == NULL) {
3807 if (context)
3808 krb5_set_error_string (context, "encryption type %d not supported",
3809 enctype);
3810 return KRB5_PROG_ETYPE_NOSUPP;
3812 et->flags |= F_DISABLED;
3813 return 0;
3816 krb5_error_code KRB5_LIB_FUNCTION
3817 krb5_string_to_key_derived(krb5_context context,
3818 const void *str,
3819 size_t len,
3820 krb5_enctype etype,
3821 krb5_keyblock *key)
3823 struct encryption_type *et = _find_enctype(etype);
3824 krb5_error_code ret;
3825 struct key_data kd;
3826 size_t keylen;
3827 u_char *tmp;
3829 if(et == NULL) {
3830 krb5_set_error_string (context, "encryption type %d not supported",
3831 etype);
3832 return KRB5_PROG_ETYPE_NOSUPP;
3834 keylen = et->keytype->bits / 8;
3836 ALLOC(kd.key, 1);
3837 if(kd.key == NULL) {
3838 krb5_set_error_string (context, "malloc: out of memory");
3839 return ENOMEM;
3841 ret = krb5_data_alloc(&kd.key->keyvalue, et->keytype->size);
3842 if(ret) {
3843 free(kd.key);
3844 return ret;
3846 kd.key->keytype = etype;
3847 tmp = malloc (keylen);
3848 if(tmp == NULL) {
3849 krb5_free_keyblock(context, kd.key);
3850 krb5_set_error_string (context, "malloc: out of memory");
3851 return ENOMEM;
3853 ret = _krb5_n_fold(str, len, tmp, keylen);
3854 if (ret) {
3855 free(tmp);
3856 krb5_set_error_string(context, "out of memory");
3857 return ret;
3859 kd.schedule = NULL;
3860 DES3_postproc (context, tmp, keylen, &kd); /* XXX */
3861 memset(tmp, 0, keylen);
3862 free(tmp);
3863 ret = derive_key(context,
3865 &kd,
3866 "kerberos", /* XXX well known constant */
3867 strlen("kerberos"));
3868 ret = krb5_copy_keyblock_contents(context, kd.key, key);
3869 free_key_data(context, &kd);
3870 return ret;
3873 static size_t
3874 wrapped_length (krb5_context context,
3875 krb5_crypto crypto,
3876 size_t data_len)
3878 struct encryption_type *et = crypto->et;
3879 size_t padsize = et->padsize;
3880 size_t checksumsize = CHECKSUMSIZE(et->checksum);
3881 size_t res;
3883 res = et->confoundersize + checksumsize + data_len;
3884 res = (res + padsize - 1) / padsize * padsize;
3885 return res;
3888 static size_t
3889 wrapped_length_dervied (krb5_context context,
3890 krb5_crypto crypto,
3891 size_t data_len)
3893 struct encryption_type *et = crypto->et;
3894 size_t padsize = et->padsize;
3895 size_t res;
3897 res = et->confoundersize + data_len;
3898 res = (res + padsize - 1) / padsize * padsize;
3899 if (et->keyed_checksum)
3900 res += et->keyed_checksum->checksumsize;
3901 else
3902 res += et->checksum->checksumsize;
3903 return res;
3907 * Return the size of an encrypted packet of length `data_len'
3910 size_t
3911 krb5_get_wrapped_length (krb5_context context,
3912 krb5_crypto crypto,
3913 size_t data_len)
3915 if (derived_crypto (context, crypto))
3916 return wrapped_length_dervied (context, crypto, data_len);
3917 else
3918 return wrapped_length (context, crypto, data_len);
3922 * Return the size of an encrypted packet of length `data_len'
3925 static size_t
3926 crypto_overhead (krb5_context context,
3927 krb5_crypto crypto)
3929 struct encryption_type *et = crypto->et;
3930 size_t res;
3932 res = CHECKSUMSIZE(et->checksum);
3933 res += et->confoundersize;
3934 if (et->padsize > 1)
3935 res += et->padsize;
3936 return res;
3939 static size_t
3940 crypto_overhead_dervied (krb5_context context,
3941 krb5_crypto crypto)
3943 struct encryption_type *et = crypto->et;
3944 size_t res;
3946 if (et->keyed_checksum)
3947 res = CHECKSUMSIZE(et->keyed_checksum);
3948 else
3949 res = CHECKSUMSIZE(et->checksum);
3950 res += et->confoundersize;
3951 if (et->padsize > 1)
3952 res += et->padsize;
3953 return res;
3956 size_t
3957 krb5_crypto_overhead (krb5_context context, krb5_crypto crypto)
3959 if (derived_crypto (context, crypto))
3960 return crypto_overhead_dervied (context, crypto);
3961 else
3962 return crypto_overhead (context, crypto);
3965 krb5_error_code KRB5_LIB_FUNCTION
3966 krb5_random_to_key(krb5_context context,
3967 krb5_enctype type,
3968 const void *data,
3969 size_t size,
3970 krb5_keyblock *key)
3972 krb5_error_code ret;
3973 struct encryption_type *et = _find_enctype(type);
3974 if(et == NULL) {
3975 krb5_set_error_string(context, "encryption type %d not supported",
3976 type);
3977 return KRB5_PROG_ETYPE_NOSUPP;
3979 if ((et->keytype->bits + 7) / 8 > size) {
3980 krb5_set_error_string(context, "encryption key %s needs %d bytes "
3981 "of random to make an encryption key out of it",
3982 et->name, (int)et->keytype->size);
3983 return KRB5_PROG_ETYPE_NOSUPP;
3985 ret = krb5_data_alloc(&key->keyvalue, et->keytype->size);
3986 if(ret)
3987 return ret;
3988 key->keytype = type;
3989 if (et->keytype->random_to_key)
3990 (*et->keytype->random_to_key)(context, key, data, size);
3991 else
3992 memcpy(key->keyvalue.data, data, et->keytype->size);
3994 return 0;
3997 krb5_error_code
3998 _krb5_pk_octetstring2key(krb5_context context,
3999 krb5_enctype type,
4000 const void *dhdata,
4001 size_t dhsize,
4002 const heim_octet_string *c_n,
4003 const heim_octet_string *k_n,
4004 krb5_keyblock *key)
4006 struct encryption_type *et = _find_enctype(type);
4007 krb5_error_code ret;
4008 size_t keylen, offset;
4009 void *keydata;
4010 unsigned char counter;
4011 unsigned char shaoutput[20];
4013 if(et == NULL) {
4014 krb5_set_error_string(context, "encryption type %d not supported",
4015 type);
4016 return KRB5_PROG_ETYPE_NOSUPP;
4018 keylen = (et->keytype->bits + 7) / 8;
4020 keydata = malloc(keylen);
4021 if (keydata == NULL) {
4022 krb5_set_error_string(context, "malloc: out of memory");
4023 return ENOMEM;
4026 counter = 0;
4027 offset = 0;
4028 do {
4029 SHA_CTX m;
4031 SHA1_Init(&m);
4032 SHA1_Update(&m, &counter, 1);
4033 SHA1_Update(&m, dhdata, dhsize);
4034 if (c_n)
4035 SHA1_Update(&m, c_n->data, c_n->length);
4036 if (k_n)
4037 SHA1_Update(&m, k_n->data, k_n->length);
4038 SHA1_Final(shaoutput, &m);
4040 memcpy((unsigned char *)keydata + offset,
4041 shaoutput,
4042 min(keylen - offset, sizeof(shaoutput)));
4044 offset += sizeof(shaoutput);
4045 counter++;
4046 } while(offset < keylen);
4047 memset(shaoutput, 0, sizeof(shaoutput));
4049 ret = krb5_random_to_key(context, type, keydata, keylen, key);
4050 memset(keydata, 0, sizeof(keylen));
4051 free(keydata);
4052 return ret;
4055 krb5_error_code KRB5_LIB_FUNCTION
4056 krb5_crypto_prf_length(krb5_context context,
4057 krb5_enctype type,
4058 size_t *length)
4060 struct encryption_type *et = _find_enctype(type);
4062 if(et == NULL || et->prf_length == 0) {
4063 krb5_set_error_string(context, "encryption type %d not supported",
4064 type);
4065 return KRB5_PROG_ETYPE_NOSUPP;
4068 *length = et->prf_length;
4069 return 0;
4072 krb5_error_code KRB5_LIB_FUNCTION
4073 krb5_crypto_prf(krb5_context context,
4074 const krb5_crypto crypto,
4075 const krb5_data *input,
4076 krb5_data *output)
4078 struct encryption_type *et = crypto->et;
4080 krb5_data_zero(output);
4082 if(et->prf == NULL) {
4083 krb5_set_error_string(context, "kerberos prf for %s not supported",
4084 et->name);
4085 return KRB5_PROG_ETYPE_NOSUPP;
4088 return (*et->prf)(context, crypto, input, output);
4094 #ifdef CRYPTO_DEBUG
4096 static krb5_error_code
4097 krb5_get_keyid(krb5_context context,
4098 krb5_keyblock *key,
4099 uint32_t *keyid)
4101 MD5_CTX md5;
4102 unsigned char tmp[16];
4104 MD5_Init (&md5);
4105 MD5_Update (&md5, key->keyvalue.data, key->keyvalue.length);
4106 MD5_Final (tmp, &md5);
4107 *keyid = (tmp[12] << 24) | (tmp[13] << 16) | (tmp[14] << 8) | tmp[15];
4108 return 0;
4111 static void
4112 krb5_crypto_debug(krb5_context context,
4113 int encryptp,
4114 size_t len,
4115 krb5_keyblock *key)
4117 uint32_t keyid;
4118 char *kt;
4119 krb5_get_keyid(context, key, &keyid);
4120 krb5_enctype_to_string(context, key->keytype, &kt);
4121 krb5_warnx(context, "%s %lu bytes with key-id %#x (%s)",
4122 encryptp ? "encrypting" : "decrypting",
4123 (unsigned long)len,
4124 keyid,
4125 kt);
4126 free(kt);
4129 #endif /* CRYPTO_DEBUG */
4131 #if 0
4133 main()
4135 #if 0
4136 int i;
4137 krb5_context context;
4138 krb5_crypto crypto;
4139 struct key_data *d;
4140 krb5_keyblock key;
4141 char constant[4];
4142 unsigned usage = ENCRYPTION_USAGE(3);
4143 krb5_error_code ret;
4145 ret = krb5_init_context(&context);
4146 if (ret)
4147 errx (1, "krb5_init_context failed: %d", ret);
4149 key.keytype = ETYPE_NEW_DES3_CBC_SHA1;
4150 key.keyvalue.data = "\xb3\x85\x58\x94\xd9\xdc\x7c\xc8"
4151 "\x25\xe9\x85\xab\x3e\xb5\xfb\x0e"
4152 "\xc8\xdf\xab\x26\x86\x64\x15\x25";
4153 key.keyvalue.length = 24;
4155 krb5_crypto_init(context, &key, 0, &crypto);
4157 d = _new_derived_key(crypto, usage);
4158 if(d == NULL)
4159 krb5_errx(context, 1, "_new_derived_key failed");
4160 krb5_copy_keyblock(context, crypto->key.key, &d->key);
4161 _krb5_put_int(constant, usage, 4);
4162 derive_key(context, crypto->et, d, constant, sizeof(constant));
4163 return 0;
4164 #else
4165 int i;
4166 krb5_context context;
4167 krb5_crypto crypto;
4168 struct key_data *d;
4169 krb5_keyblock key;
4170 krb5_error_code ret;
4171 Checksum res;
4173 char *data = "what do ya want for nothing?";
4175 ret = krb5_init_context(&context);
4176 if (ret)
4177 errx (1, "krb5_init_context failed: %d", ret);
4179 key.keytype = ETYPE_NEW_DES3_CBC_SHA1;
4180 key.keyvalue.data = "Jefe";
4181 /* "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
4182 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"; */
4183 key.keyvalue.length = 4;
4185 d = ecalloc(1, sizeof(*d));
4186 d->key = &key;
4187 res.checksum.length = 20;
4188 res.checksum.data = emalloc(res.checksum.length);
4189 SP_HMAC_SHA1_checksum(context, d, data, 28, &res);
4191 return 0;
4192 #endif
4194 #endif