Allow users to specify their own configuration file ~/.krb5/config
[heimdal.git] / lib / krb5 / crypto.c
blob91032f46d65722e19587afcf1234f914ce128634
1 /*
2 * Copyright (c) 1997 - 2008 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 #define KRB5_DEPRECATED
36 #include "krb5_locl.h"
37 #include <pkinit_asn1.h>
39 #ifndef HEIMDAL_SMALLER
40 #define DES3_OLD_ENCTYPE 1
41 #endif
43 struct key_data {
44 krb5_keyblock *key;
45 krb5_data *schedule;
48 struct key_usage {
49 unsigned usage;
50 struct key_data key;
53 struct krb5_crypto_data {
54 struct encryption_type *et;
55 struct key_data key;
56 int num_key_usage;
57 struct key_usage *key_usage;
60 #define CRYPTO_ETYPE(C) ((C)->et->type)
62 /* bits for `flags' below */
63 #define F_KEYED 1 /* checksum is keyed */
64 #define F_CPROOF 2 /* checksum is collision proof */
65 #define F_DERIVED 4 /* uses derived keys */
66 #define F_VARIANT 8 /* uses `variant' keys (6.4.3) */
67 #define F_PSEUDO 16 /* not a real protocol type */
68 #define F_SPECIAL 32 /* backwards */
69 #define F_DISABLED 64 /* enctype/checksum disabled */
70 #define F_WEAK 128 /* enctype is considered weak */
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 void (*random_key)(krb5_context, krb5_keyblock*);
86 void (*schedule)(krb5_context, struct key_type *, struct key_data *);
87 struct salt_type *string_to_key;
88 void (*random_to_key)(krb5_context, krb5_keyblock*, const void*, size_t);
89 void (*cleanup)(krb5_context, struct key_data *);
90 const EVP_CIPHER *(*evp)(void);
93 struct checksum_type {
94 krb5_cksumtype type;
95 const char *name;
96 size_t blocksize;
97 size_t checksumsize;
98 unsigned flags;
99 krb5_enctype (*checksum)(krb5_context context,
100 struct key_data *key,
101 const void *buf, size_t len,
102 unsigned usage,
103 Checksum *csum);
104 krb5_error_code (*verify)(krb5_context context,
105 struct key_data *key,
106 const void *buf, size_t len,
107 unsigned usage,
108 Checksum *csum);
111 struct encryption_type {
112 krb5_enctype type;
113 const char *name;
114 size_t blocksize;
115 size_t padsize;
116 size_t confoundersize;
117 struct key_type *keytype;
118 struct checksum_type *checksum;
119 struct checksum_type *keyed_checksum;
120 unsigned flags;
121 krb5_error_code (*encrypt)(krb5_context context,
122 struct key_data *key,
123 void *data, size_t len,
124 krb5_boolean encryptp,
125 int usage,
126 void *ivec);
127 size_t prf_length;
128 krb5_error_code (*prf)(krb5_context,
129 krb5_crypto, const krb5_data *, krb5_data *);
132 #define ENCRYPTION_USAGE(U) (((U) << 8) | 0xAA)
133 #define INTEGRITY_USAGE(U) (((U) << 8) | 0x55)
134 #define CHECKSUM_USAGE(U) (((U) << 8) | 0x99)
136 static struct checksum_type *_find_checksum(krb5_cksumtype type);
137 static struct encryption_type *_find_enctype(krb5_enctype type);
138 static krb5_error_code _get_derived_key(krb5_context, krb5_crypto,
139 unsigned, struct key_data**);
140 static struct key_data *_new_derived_key(krb5_crypto crypto, unsigned usage);
141 static krb5_error_code derive_key(krb5_context context,
142 struct encryption_type *et,
143 struct key_data *key,
144 const void *constant,
145 size_t len);
146 static krb5_error_code hmac(krb5_context context,
147 struct checksum_type *cm,
148 const void *data,
149 size_t len,
150 unsigned usage,
151 struct key_data *keyblock,
152 Checksum *result);
153 static void free_key_data(krb5_context,
154 struct key_data *,
155 struct encryption_type *);
156 static void free_key_schedule(krb5_context,
157 struct key_data *,
158 struct encryption_type *);
159 static krb5_error_code usage2arcfour (krb5_context, unsigned *);
160 static void xor (DES_cblock *, const unsigned char *);
162 /************************************************************
164 ************************************************************/
166 struct evp_schedule {
167 EVP_CIPHER_CTX ectx;
168 EVP_CIPHER_CTX dctx;
172 static HEIMDAL_MUTEX crypto_mutex = HEIMDAL_MUTEX_INITIALIZER;
174 #ifdef HEIM_WEAK_CRYPTO
175 static void
176 krb5_DES_random_key(krb5_context context,
177 krb5_keyblock *key)
179 DES_cblock *k = key->keyvalue.data;
180 do {
181 krb5_generate_random_block(k, sizeof(DES_cblock));
182 DES_set_odd_parity(k);
183 } while(DES_is_weak_key(k));
186 static void
187 krb5_DES_schedule_old(krb5_context context,
188 struct key_type *kt,
189 struct key_data *key)
191 DES_set_key_unchecked(key->key->keyvalue.data, key->schedule->data);
194 #ifdef ENABLE_AFS_STRING_TO_KEY
196 /* This defines the Andrew string_to_key function. It accepts a password
197 * string as input and converts it via a one-way encryption algorithm to a DES
198 * encryption key. It is compatible with the original Andrew authentication
199 * service password database.
203 * Short passwords, i.e 8 characters or less.
205 static void
206 krb5_DES_AFS3_CMU_string_to_key (krb5_data pw,
207 krb5_data cell,
208 DES_cblock *key)
210 char password[8+1]; /* crypt is limited to 8 chars anyway */
211 int i;
213 for(i = 0; i < 8; i++) {
214 char c = ((i < pw.length) ? ((char*)pw.data)[i] : 0) ^
215 ((i < cell.length) ?
216 tolower(((unsigned char*)cell.data)[i]) : 0);
217 password[i] = c ? c : 'X';
219 password[8] = '\0';
221 memcpy(key, crypt(password, "p1") + 2, sizeof(DES_cblock));
223 /* parity is inserted into the LSB so left shift each byte up one
224 bit. This allows ascii characters with a zero MSB to retain as
225 much significance as possible. */
226 for (i = 0; i < sizeof(DES_cblock); i++)
227 ((unsigned char*)key)[i] <<= 1;
228 DES_set_odd_parity (key);
232 * Long passwords, i.e 9 characters or more.
234 static void
235 krb5_DES_AFS3_Transarc_string_to_key (krb5_data pw,
236 krb5_data cell,
237 DES_cblock *key)
239 DES_key_schedule schedule;
240 DES_cblock temp_key;
241 DES_cblock ivec;
242 char password[512];
243 size_t passlen;
245 memcpy(password, pw.data, min(pw.length, sizeof(password)));
246 if(pw.length < sizeof(password)) {
247 int len = min(cell.length, sizeof(password) - pw.length);
248 int i;
250 memcpy(password + pw.length, cell.data, len);
251 for (i = pw.length; i < pw.length + len; ++i)
252 password[i] = tolower((unsigned char)password[i]);
254 passlen = min(sizeof(password), pw.length + cell.length);
255 memcpy(&ivec, "kerberos", 8);
256 memcpy(&temp_key, "kerberos", 8);
257 DES_set_odd_parity (&temp_key);
258 DES_set_key_unchecked (&temp_key, &schedule);
259 DES_cbc_cksum ((void*)password, &ivec, passlen, &schedule, &ivec);
261 memcpy(&temp_key, &ivec, 8);
262 DES_set_odd_parity (&temp_key);
263 DES_set_key_unchecked (&temp_key, &schedule);
264 DES_cbc_cksum ((void*)password, key, passlen, &schedule, &ivec);
265 memset(&schedule, 0, sizeof(schedule));
266 memset(&temp_key, 0, sizeof(temp_key));
267 memset(&ivec, 0, sizeof(ivec));
268 memset(password, 0, sizeof(password));
270 DES_set_odd_parity (key);
273 static krb5_error_code
274 DES_AFS3_string_to_key(krb5_context context,
275 krb5_enctype enctype,
276 krb5_data password,
277 krb5_salt salt,
278 krb5_data opaque,
279 krb5_keyblock *key)
281 DES_cblock tmp;
282 if(password.length > 8)
283 krb5_DES_AFS3_Transarc_string_to_key(password, salt.saltvalue, &tmp);
284 else
285 krb5_DES_AFS3_CMU_string_to_key(password, salt.saltvalue, &tmp);
286 key->keytype = enctype;
287 krb5_data_copy(&key->keyvalue, tmp, sizeof(tmp));
288 memset(&key, 0, sizeof(key));
289 return 0;
291 #endif /* ENABLE_AFS_STRING_TO_KEY */
293 static void
294 DES_string_to_key_int(unsigned char *data, size_t length, DES_cblock *key)
296 DES_key_schedule schedule;
297 int i;
298 int reverse = 0;
299 unsigned char *p;
301 unsigned char swap[] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
302 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf };
303 memset(key, 0, 8);
305 p = (unsigned char*)key;
306 for (i = 0; i < length; i++) {
307 unsigned char tmp = data[i];
308 if (!reverse)
309 *p++ ^= (tmp << 1);
310 else
311 *--p ^= (swap[tmp & 0xf] << 4) | swap[(tmp & 0xf0) >> 4];
312 if((i % 8) == 7)
313 reverse = !reverse;
315 DES_set_odd_parity(key);
316 if(DES_is_weak_key(key))
317 (*key)[7] ^= 0xF0;
318 DES_set_key_unchecked(key, &schedule);
319 DES_cbc_cksum((void*)data, key, length, &schedule, key);
320 memset(&schedule, 0, sizeof(schedule));
321 DES_set_odd_parity(key);
322 if(DES_is_weak_key(key))
323 (*key)[7] ^= 0xF0;
326 static krb5_error_code
327 krb5_DES_string_to_key(krb5_context context,
328 krb5_enctype enctype,
329 krb5_data password,
330 krb5_salt salt,
331 krb5_data opaque,
332 krb5_keyblock *key)
334 unsigned char *s;
335 size_t len;
336 DES_cblock tmp;
338 #ifdef ENABLE_AFS_STRING_TO_KEY
339 if (opaque.length == 1) {
340 unsigned long v;
341 _krb5_get_int(opaque.data, &v, 1);
342 if (v == 1)
343 return DES_AFS3_string_to_key(context, enctype, password,
344 salt, opaque, key);
346 #endif
348 len = password.length + salt.saltvalue.length;
349 s = malloc(len);
350 if(len > 0 && s == NULL) {
351 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
352 return ENOMEM;
354 memcpy(s, password.data, password.length);
355 memcpy(s + password.length, salt.saltvalue.data, salt.saltvalue.length);
356 DES_string_to_key_int(s, len, &tmp);
357 key->keytype = enctype;
358 krb5_data_copy(&key->keyvalue, tmp, sizeof(tmp));
359 memset(&tmp, 0, sizeof(tmp));
360 memset(s, 0, len);
361 free(s);
362 return 0;
365 static void
366 krb5_DES_random_to_key(krb5_context context,
367 krb5_keyblock *key,
368 const void *data,
369 size_t size)
371 DES_cblock *k = key->keyvalue.data;
372 memcpy(k, data, key->keyvalue.length);
373 DES_set_odd_parity(k);
374 if(DES_is_weak_key(k))
375 xor(k, (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
377 #endif
383 static void
384 DES3_random_key(krb5_context context,
385 krb5_keyblock *key)
387 DES_cblock *k = key->keyvalue.data;
388 do {
389 krb5_generate_random_block(k, 3 * sizeof(DES_cblock));
390 DES_set_odd_parity(&k[0]);
391 DES_set_odd_parity(&k[1]);
392 DES_set_odd_parity(&k[2]);
393 } while(DES_is_weak_key(&k[0]) ||
394 DES_is_weak_key(&k[1]) ||
395 DES_is_weak_key(&k[2]));
399 * A = A xor B. A & B are 8 bytes.
402 static void
403 xor (DES_cblock *key, const unsigned char *b)
405 unsigned char *a = (unsigned char*)key;
406 a[0] ^= b[0];
407 a[1] ^= b[1];
408 a[2] ^= b[2];
409 a[3] ^= b[3];
410 a[4] ^= b[4];
411 a[5] ^= b[5];
412 a[6] ^= b[6];
413 a[7] ^= b[7];
416 #ifdef DES3_OLD_ENCTYPE
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_message(context, ENOMEM, N_("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_message(context, ret, N_("malloc: 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_unchecked(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;
480 #endif
482 static krb5_error_code
483 DES3_string_to_key_derived(krb5_context context,
484 krb5_enctype enctype,
485 krb5_data password,
486 krb5_salt salt,
487 krb5_data opaque,
488 krb5_keyblock *key)
490 krb5_error_code ret;
491 size_t len = password.length + salt.saltvalue.length;
492 char *s;
494 s = malloc(len);
495 if(len != 0 && s == NULL) {
496 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
497 return ENOMEM;
499 memcpy(s, password.data, password.length);
500 memcpy(s + password.length, salt.saltvalue.data, salt.saltvalue.length);
501 ret = krb5_string_to_key_derived(context,
503 len,
504 enctype,
505 key);
506 memset(s, 0, len);
507 free(s);
508 return ret;
511 static void
512 DES3_random_to_key(krb5_context context,
513 krb5_keyblock *key,
514 const void *data,
515 size_t size)
517 unsigned char *x = key->keyvalue.data;
518 const u_char *q = data;
519 DES_cblock *k;
520 int i, j;
522 memset(x, 0, sizeof(x));
523 for (i = 0; i < 3; ++i) {
524 unsigned char foo;
525 for (j = 0; j < 7; ++j) {
526 unsigned char b = q[7 * i + j];
528 x[8 * i + j] = b;
530 foo = 0;
531 for (j = 6; j >= 0; --j) {
532 foo |= q[7 * i + j] & 1;
533 foo <<= 1;
535 x[8 * i + 7] = foo;
537 k = key->keyvalue.data;
538 for (i = 0; i < 3; i++) {
539 DES_set_odd_parity(&k[i]);
540 if(DES_is_weak_key(&k[i]))
541 xor(&k[i], (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
546 * ARCFOUR
549 static krb5_error_code
550 ARCFOUR_string_to_key(krb5_context context,
551 krb5_enctype enctype,
552 krb5_data password,
553 krb5_salt salt,
554 krb5_data opaque,
555 krb5_keyblock *key)
557 krb5_error_code ret;
558 uint16_t *s = NULL;
559 size_t len, i;
560 EVP_MD_CTX *m;
562 m = EVP_MD_CTX_create();
563 if (m == NULL) {
564 ret = ENOMEM;
565 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
566 goto out;
569 EVP_DigestInit_ex(m, EVP_md4(), NULL);
571 ret = wind_utf8ucs2_length(password.data, &len);
572 if (ret) {
573 krb5_set_error_message (context, ret,
574 N_("Password not an UCS2 string", ""));
575 goto out;
578 s = malloc (len * sizeof(s[0]));
579 if (len != 0 && s == NULL) {
580 krb5_set_error_message (context, ENOMEM,
581 N_("malloc: out of memory", ""));
582 ret = ENOMEM;
583 goto out;
586 ret = wind_utf8ucs2(password.data, s, &len);
587 if (ret) {
588 krb5_set_error_message (context, ret,
589 N_("Password not an UCS2 string", ""));
590 goto out;
593 /* LE encoding */
594 for (i = 0; i < len; i++) {
595 unsigned char p;
596 p = (s[i] & 0xff);
597 EVP_DigestUpdate (m, &p, 1);
598 p = (s[i] >> 8) & 0xff;
599 EVP_DigestUpdate (m, &p, 1);
602 key->keytype = enctype;
603 ret = krb5_data_alloc (&key->keyvalue, 16);
604 if (ret) {
605 krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
606 goto out;
608 EVP_DigestFinal_ex (m, key->keyvalue.data, NULL);
610 out:
611 EVP_MD_CTX_destroy(m);
612 if (s)
613 memset (s, 0, len);
614 free (s);
615 return ret;
619 * AES
622 int _krb5_AES_string_to_default_iterator = 4096;
624 static krb5_error_code
625 AES_string_to_key(krb5_context context,
626 krb5_enctype enctype,
627 krb5_data password,
628 krb5_salt salt,
629 krb5_data opaque,
630 krb5_keyblock *key)
632 krb5_error_code ret;
633 uint32_t iter;
634 struct encryption_type *et;
635 struct key_data kd;
637 if (opaque.length == 0)
638 iter = _krb5_AES_string_to_default_iterator;
639 else if (opaque.length == 4) {
640 unsigned long v;
641 _krb5_get_int(opaque.data, &v, 4);
642 iter = ((uint32_t)v);
643 } else
644 return KRB5_PROG_KEYTYPE_NOSUPP; /* XXX */
646 et = _find_enctype(enctype);
647 if (et == NULL)
648 return KRB5_PROG_KEYTYPE_NOSUPP;
650 kd.schedule = NULL;
651 ALLOC(kd.key, 1);
652 if(kd.key == NULL) {
653 krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
654 return ENOMEM;
656 kd.key->keytype = enctype;
657 ret = krb5_data_alloc(&kd.key->keyvalue, et->keytype->size);
658 if (ret) {
659 krb5_set_error_message (context, ret, N_("malloc: out of memory", ""));
660 return ret;
663 ret = PKCS5_PBKDF2_HMAC_SHA1(password.data, password.length,
664 salt.saltvalue.data, salt.saltvalue.length,
665 iter,
666 et->keytype->size, kd.key->keyvalue.data);
667 if (ret != 1) {
668 free_key_data(context, &kd, et);
669 krb5_set_error_message(context, KRB5_PROG_KEYTYPE_NOSUPP,
670 "Error calculating s2k");
671 return KRB5_PROG_KEYTYPE_NOSUPP;
674 ret = derive_key(context, et, &kd, "kerberos", strlen("kerberos"));
675 if (ret == 0)
676 ret = krb5_copy_keyblock_contents(context, kd.key, key);
677 free_key_data(context, &kd, et);
679 return ret;
682 static void
683 evp_schedule(krb5_context context, struct key_type *kt, struct key_data *kd)
685 struct evp_schedule *key = kd->schedule->data;
686 const EVP_CIPHER *c = (*kt->evp)();
688 EVP_CIPHER_CTX_init(&key->ectx);
689 EVP_CIPHER_CTX_init(&key->dctx);
691 EVP_CipherInit_ex(&key->ectx, c, NULL, kd->key->keyvalue.data, NULL, 1);
692 EVP_CipherInit_ex(&key->dctx, c, NULL, kd->key->keyvalue.data, NULL, 0);
695 static void
696 evp_cleanup(krb5_context context, struct key_data *kd)
698 struct evp_schedule *key = kd->schedule->data;
699 EVP_CIPHER_CTX_cleanup(&key->ectx);
700 EVP_CIPHER_CTX_cleanup(&key->dctx);
707 #ifdef HEIM_WEAK_CRYPTO
708 static struct salt_type des_salt[] = {
710 KRB5_PW_SALT,
711 "pw-salt",
712 krb5_DES_string_to_key
714 #ifdef ENABLE_AFS_STRING_TO_KEY
716 KRB5_AFS3_SALT,
717 "afs3-salt",
718 DES_AFS3_string_to_key
720 #endif
721 { 0 }
723 #endif
725 #ifdef DES3_OLD_ENCTYPE
726 static struct salt_type des3_salt[] = {
728 KRB5_PW_SALT,
729 "pw-salt",
730 DES3_string_to_key
732 { 0 }
734 #endif
736 static struct salt_type des3_salt_derived[] = {
738 KRB5_PW_SALT,
739 "pw-salt",
740 DES3_string_to_key_derived
742 { 0 }
745 static struct salt_type AES_salt[] = {
747 KRB5_PW_SALT,
748 "pw-salt",
749 AES_string_to_key
751 { 0 }
754 static struct salt_type arcfour_salt[] = {
756 KRB5_PW_SALT,
757 "pw-salt",
758 ARCFOUR_string_to_key
760 { 0 }
767 static struct key_type keytype_null = {
768 KEYTYPE_NULL,
769 "null",
773 NULL,
774 NULL,
775 NULL
778 #ifdef HEIM_WEAK_CRYPTO
779 static struct key_type keytype_des_old = {
780 KEYTYPE_DES,
781 "des-old",
784 sizeof(DES_key_schedule),
785 krb5_DES_random_key,
786 krb5_DES_schedule_old,
787 des_salt,
788 krb5_DES_random_to_key
791 static struct key_type keytype_des = {
792 KEYTYPE_DES,
793 "des",
796 sizeof(struct evp_schedule),
797 krb5_DES_random_key,
798 evp_schedule,
799 des_salt,
800 krb5_DES_random_to_key,
801 evp_cleanup,
802 EVP_des_cbc
804 #endif /* HEIM_WEAK_CRYPTO */
806 #ifdef DES3_OLD_ENCTYPE
807 static struct key_type keytype_des3 = {
808 KEYTYPE_DES3,
809 "des3",
810 168,
812 sizeof(struct evp_schedule),
813 DES3_random_key,
814 evp_schedule,
815 des3_salt,
816 DES3_random_to_key,
817 evp_cleanup,
818 EVP_des_ede3_cbc
820 #endif
822 static struct key_type keytype_des3_derived = {
823 KEYTYPE_DES3,
824 "des3",
825 168,
827 sizeof(struct evp_schedule),
828 DES3_random_key,
829 evp_schedule,
830 des3_salt_derived,
831 DES3_random_to_key,
832 evp_cleanup,
833 EVP_des_ede3_cbc
836 static struct key_type keytype_aes128 = {
837 KEYTYPE_AES128,
838 "aes-128",
839 128,
841 sizeof(struct evp_schedule),
842 NULL,
843 evp_schedule,
844 AES_salt,
845 NULL,
846 evp_cleanup,
847 EVP_aes_128_cbc
850 static struct key_type keytype_aes256 = {
851 KEYTYPE_AES256,
852 "aes-256",
853 256,
855 sizeof(struct evp_schedule),
856 NULL,
857 evp_schedule,
858 AES_salt,
859 NULL,
860 evp_cleanup,
861 EVP_aes_256_cbc
864 static struct key_type keytype_arcfour = {
865 KEYTYPE_ARCFOUR,
866 "arcfour",
867 128,
869 sizeof(struct evp_schedule),
870 NULL,
871 evp_schedule,
872 arcfour_salt,
873 NULL,
874 evp_cleanup,
875 EVP_rc4
878 krb5_error_code KRB5_LIB_FUNCTION
879 krb5_salttype_to_string (krb5_context context,
880 krb5_enctype etype,
881 krb5_salttype stype,
882 char **string)
884 struct encryption_type *e;
885 struct salt_type *st;
887 e = _find_enctype (etype);
888 if (e == NULL) {
889 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
890 "encryption type %d not supported",
891 etype);
892 return KRB5_PROG_ETYPE_NOSUPP;
894 for (st = e->keytype->string_to_key; st && st->type; st++) {
895 if (st->type == stype) {
896 *string = strdup (st->name);
897 if (*string == NULL) {
898 krb5_set_error_message (context, ENOMEM,
899 N_("malloc: out of memory", ""));
900 return ENOMEM;
902 return 0;
905 krb5_set_error_message (context, HEIM_ERR_SALTTYPE_NOSUPP,
906 "salttype %d not supported", stype);
907 return HEIM_ERR_SALTTYPE_NOSUPP;
910 krb5_error_code KRB5_LIB_FUNCTION
911 krb5_string_to_salttype (krb5_context context,
912 krb5_enctype etype,
913 const char *string,
914 krb5_salttype *salttype)
916 struct encryption_type *e;
917 struct salt_type *st;
919 e = _find_enctype (etype);
920 if (e == NULL) {
921 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
922 N_("encryption type %d not supported", ""),
923 etype);
924 return KRB5_PROG_ETYPE_NOSUPP;
926 for (st = e->keytype->string_to_key; st && st->type; st++) {
927 if (strcasecmp (st->name, string) == 0) {
928 *salttype = st->type;
929 return 0;
932 krb5_set_error_message(context, HEIM_ERR_SALTTYPE_NOSUPP,
933 N_("salttype %s not supported", ""), string);
934 return HEIM_ERR_SALTTYPE_NOSUPP;
937 krb5_error_code KRB5_LIB_FUNCTION
938 krb5_get_pw_salt(krb5_context context,
939 krb5_const_principal principal,
940 krb5_salt *salt)
942 size_t len;
943 int i;
944 krb5_error_code ret;
945 char *p;
947 salt->salttype = KRB5_PW_SALT;
948 len = strlen(principal->realm);
949 for (i = 0; i < principal->name.name_string.len; ++i)
950 len += strlen(principal->name.name_string.val[i]);
951 ret = krb5_data_alloc (&salt->saltvalue, len);
952 if (ret)
953 return ret;
954 p = salt->saltvalue.data;
955 memcpy (p, principal->realm, strlen(principal->realm));
956 p += strlen(principal->realm);
957 for (i = 0; i < principal->name.name_string.len; ++i) {
958 memcpy (p,
959 principal->name.name_string.val[i],
960 strlen(principal->name.name_string.val[i]));
961 p += strlen(principal->name.name_string.val[i]);
963 return 0;
966 krb5_error_code KRB5_LIB_FUNCTION
967 krb5_free_salt(krb5_context context,
968 krb5_salt salt)
970 krb5_data_free(&salt.saltvalue);
971 return 0;
974 krb5_error_code KRB5_LIB_FUNCTION
975 krb5_string_to_key_data (krb5_context context,
976 krb5_enctype enctype,
977 krb5_data password,
978 krb5_principal principal,
979 krb5_keyblock *key)
981 krb5_error_code ret;
982 krb5_salt salt;
984 ret = krb5_get_pw_salt(context, principal, &salt);
985 if(ret)
986 return ret;
987 ret = krb5_string_to_key_data_salt(context, enctype, password, salt, key);
988 krb5_free_salt(context, salt);
989 return ret;
992 krb5_error_code KRB5_LIB_FUNCTION
993 krb5_string_to_key (krb5_context context,
994 krb5_enctype enctype,
995 const char *password,
996 krb5_principal principal,
997 krb5_keyblock *key)
999 krb5_data pw;
1000 pw.data = rk_UNCONST(password);
1001 pw.length = strlen(password);
1002 return krb5_string_to_key_data(context, enctype, pw, principal, key);
1005 krb5_error_code KRB5_LIB_FUNCTION
1006 krb5_string_to_key_data_salt (krb5_context context,
1007 krb5_enctype enctype,
1008 krb5_data password,
1009 krb5_salt salt,
1010 krb5_keyblock *key)
1012 krb5_data opaque;
1013 krb5_data_zero(&opaque);
1014 return krb5_string_to_key_data_salt_opaque(context, enctype, password,
1015 salt, opaque, key);
1019 * Do a string -> key for encryption type `enctype' operation on
1020 * `password' (with salt `salt' and the enctype specific data string
1021 * `opaque'), returning the resulting key in `key'
1024 krb5_error_code KRB5_LIB_FUNCTION
1025 krb5_string_to_key_data_salt_opaque (krb5_context context,
1026 krb5_enctype enctype,
1027 krb5_data password,
1028 krb5_salt salt,
1029 krb5_data opaque,
1030 krb5_keyblock *key)
1032 struct encryption_type *et =_find_enctype(enctype);
1033 struct salt_type *st;
1034 if(et == NULL) {
1035 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
1036 N_("encryption type %d not supported", ""),
1037 enctype);
1038 return KRB5_PROG_ETYPE_NOSUPP;
1040 for(st = et->keytype->string_to_key; st && st->type; st++)
1041 if(st->type == salt.salttype)
1042 return (*st->string_to_key)(context, enctype, password,
1043 salt, opaque, key);
1044 krb5_set_error_message(context, HEIM_ERR_SALTTYPE_NOSUPP,
1045 N_("salt type %d not supported", ""),
1046 salt.salttype);
1047 return HEIM_ERR_SALTTYPE_NOSUPP;
1051 * Do a string -> key for encryption type `enctype' operation on the
1052 * string `password' (with salt `salt'), returning the resulting key
1053 * in `key'
1056 krb5_error_code KRB5_LIB_FUNCTION
1057 krb5_string_to_key_salt (krb5_context context,
1058 krb5_enctype enctype,
1059 const char *password,
1060 krb5_salt salt,
1061 krb5_keyblock *key)
1063 krb5_data pw;
1064 pw.data = rk_UNCONST(password);
1065 pw.length = strlen(password);
1066 return krb5_string_to_key_data_salt(context, enctype, pw, salt, key);
1069 krb5_error_code KRB5_LIB_FUNCTION
1070 krb5_string_to_key_salt_opaque (krb5_context context,
1071 krb5_enctype enctype,
1072 const char *password,
1073 krb5_salt salt,
1074 krb5_data opaque,
1075 krb5_keyblock *key)
1077 krb5_data pw;
1078 pw.data = rk_UNCONST(password);
1079 pw.length = strlen(password);
1080 return krb5_string_to_key_data_salt_opaque(context, enctype,
1081 pw, salt, opaque, key);
1084 krb5_error_code KRB5_LIB_FUNCTION
1085 krb5_enctype_keysize(krb5_context context,
1086 krb5_enctype type,
1087 size_t *keysize)
1089 struct encryption_type *et = _find_enctype(type);
1090 if(et == NULL) {
1091 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
1092 N_("encryption type %d not supported", ""),
1093 type);
1094 return KRB5_PROG_ETYPE_NOSUPP;
1096 *keysize = et->keytype->size;
1097 return 0;
1100 krb5_error_code KRB5_LIB_FUNCTION
1101 krb5_enctype_keybits(krb5_context context,
1102 krb5_enctype type,
1103 size_t *keybits)
1105 struct encryption_type *et = _find_enctype(type);
1106 if(et == NULL) {
1107 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
1108 "encryption type %d not supported",
1109 type);
1110 return KRB5_PROG_ETYPE_NOSUPP;
1112 *keybits = et->keytype->bits;
1113 return 0;
1116 krb5_error_code KRB5_LIB_FUNCTION
1117 krb5_generate_random_keyblock(krb5_context context,
1118 krb5_enctype type,
1119 krb5_keyblock *key)
1121 krb5_error_code ret;
1122 struct encryption_type *et = _find_enctype(type);
1123 if(et == NULL) {
1124 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
1125 N_("encryption type %d not supported", ""),
1126 type);
1127 return KRB5_PROG_ETYPE_NOSUPP;
1129 ret = krb5_data_alloc(&key->keyvalue, et->keytype->size);
1130 if(ret)
1131 return ret;
1132 key->keytype = type;
1133 if(et->keytype->random_key)
1134 (*et->keytype->random_key)(context, key);
1135 else
1136 krb5_generate_random_block(key->keyvalue.data,
1137 key->keyvalue.length);
1138 return 0;
1141 static krb5_error_code
1142 _key_schedule(krb5_context context,
1143 struct key_data *key)
1145 krb5_error_code ret;
1146 struct encryption_type *et = _find_enctype(key->key->keytype);
1147 struct key_type *kt;
1149 if (et == NULL) {
1150 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
1151 N_("encryption type %d not supported", ""),
1152 key->key->keytype);
1153 return KRB5_PROG_ETYPE_NOSUPP;
1156 kt = et->keytype;
1158 if(kt->schedule == NULL)
1159 return 0;
1160 if (key->schedule != NULL)
1161 return 0;
1162 ALLOC(key->schedule, 1);
1163 if(key->schedule == NULL) {
1164 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1165 return ENOMEM;
1167 ret = krb5_data_alloc(key->schedule, kt->schedule_size);
1168 if(ret) {
1169 free(key->schedule);
1170 key->schedule = NULL;
1171 return ret;
1173 (*kt->schedule)(context, kt, key);
1174 return 0;
1177 /************************************************************
1179 ************************************************************/
1181 static krb5_error_code
1182 NONE_checksum(krb5_context context,
1183 struct key_data *key,
1184 const void *data,
1185 size_t len,
1186 unsigned usage,
1187 Checksum *C)
1189 return 0;
1192 #if defined(DES3_OLD_ENCTYPE) || defined(HEIM_WEAK_CRYPTO)
1194 static krb5_error_code
1195 des_checksum(krb5_context context,
1196 const EVP_MD *evp_md,
1197 struct key_data *key,
1198 const void *data,
1199 size_t len,
1200 Checksum *cksum)
1202 struct evp_schedule *ctx = key->schedule->data;
1203 EVP_MD_CTX *m;
1204 DES_cblock ivec;
1205 unsigned char *p = cksum->checksum.data;
1207 krb5_generate_random_block(p, 8);
1209 m = EVP_MD_CTX_create();
1210 if (m == NULL) {
1211 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1212 return ENOMEM;
1215 EVP_DigestInit_ex(m, evp_md, NULL);
1216 EVP_DigestUpdate(m, p, 8);
1217 EVP_DigestUpdate(m, data, len);
1218 EVP_DigestFinal_ex (m, p + 8, NULL);
1219 EVP_MD_CTX_destroy(m);
1220 memset (&ivec, 0, sizeof(ivec));
1221 EVP_CipherInit_ex(&ctx->ectx, NULL, NULL, NULL, (void *)&ivec, -1);
1222 EVP_Cipher(&ctx->ectx, p, p, 24);
1224 return 0;
1227 static krb5_error_code
1228 des_verify(krb5_context context,
1229 const EVP_MD *evp_md,
1230 struct key_data *key,
1231 const void *data,
1232 size_t len,
1233 Checksum *C)
1235 struct evp_schedule *ctx = key->schedule->data;
1236 EVP_MD_CTX *m;
1237 unsigned char tmp[24];
1238 unsigned char res[16];
1239 DES_cblock ivec;
1240 krb5_error_code ret = 0;
1242 m = EVP_MD_CTX_create();
1243 if (m == NULL) {
1244 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1245 return ENOMEM;
1248 memset(&ivec, 0, sizeof(ivec));
1249 EVP_CipherInit_ex(&ctx->dctx, NULL, NULL, NULL, (void *)&ivec, -1);
1250 EVP_Cipher(&ctx->dctx, tmp, C->checksum.data, 24);
1252 EVP_DigestInit_ex(m, evp_md, NULL);
1253 EVP_DigestUpdate(m, tmp, 8); /* confounder */
1254 EVP_DigestUpdate(m, data, len);
1255 EVP_DigestFinal_ex (m, res, NULL);
1256 EVP_MD_CTX_destroy(m);
1257 if(ct_memcmp(res, tmp + 8, sizeof(res)) != 0) {
1258 krb5_clear_error_message (context);
1259 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1261 memset(tmp, 0, sizeof(tmp));
1262 memset(res, 0, sizeof(res));
1263 return ret;
1266 #endif
1268 #ifdef HEIM_WEAK_CRYPTO
1270 static krb5_error_code
1271 CRC32_checksum(krb5_context context,
1272 struct key_data *key,
1273 const void *data,
1274 size_t len,
1275 unsigned usage,
1276 Checksum *C)
1278 uint32_t crc;
1279 unsigned char *r = C->checksum.data;
1280 _krb5_crc_init_table ();
1281 crc = _krb5_crc_update (data, len, 0);
1282 r[0] = crc & 0xff;
1283 r[1] = (crc >> 8) & 0xff;
1284 r[2] = (crc >> 16) & 0xff;
1285 r[3] = (crc >> 24) & 0xff;
1286 return 0;
1289 static krb5_error_code
1290 RSA_MD4_checksum(krb5_context context,
1291 struct key_data *key,
1292 const void *data,
1293 size_t len,
1294 unsigned usage,
1295 Checksum *C)
1297 if (EVP_Digest(data, len, C->checksum.data, NULL, EVP_md4(), NULL) != 1)
1298 krb5_abortx(context, "md4 checksum failed");
1299 return 0;
1302 static krb5_error_code
1303 RSA_MD4_DES_checksum(krb5_context context,
1304 struct key_data *key,
1305 const void *data,
1306 size_t len,
1307 unsigned usage,
1308 Checksum *cksum)
1310 return des_checksum(context, EVP_md4(), key, data, len, cksum);
1313 static krb5_error_code
1314 RSA_MD4_DES_verify(krb5_context context,
1315 struct key_data *key,
1316 const void *data,
1317 size_t len,
1318 unsigned usage,
1319 Checksum *C)
1321 return des_verify(context, EVP_md5(), key, data, len, C);
1324 static krb5_error_code
1325 RSA_MD5_DES_checksum(krb5_context context,
1326 struct key_data *key,
1327 const void *data,
1328 size_t len,
1329 unsigned usage,
1330 Checksum *C)
1332 return des_checksum(context, EVP_md5(), key, data, len, C);
1335 static krb5_error_code
1336 RSA_MD5_DES_verify(krb5_context context,
1337 struct key_data *key,
1338 const void *data,
1339 size_t len,
1340 unsigned usage,
1341 Checksum *C)
1343 return des_verify(context, EVP_md5(), key, data, len, C);
1346 #endif /* HEIM_WEAK_CRYPTO */
1348 #ifdef DES3_OLD_ENCTYPE
1349 static krb5_error_code
1350 RSA_MD5_DES3_checksum(krb5_context context,
1351 struct key_data *key,
1352 const void *data,
1353 size_t len,
1354 unsigned usage,
1355 Checksum *C)
1357 return des_checksum(context, EVP_md5(), key, data, len, C);
1360 static krb5_error_code
1361 RSA_MD5_DES3_verify(krb5_context context,
1362 struct key_data *key,
1363 const void *data,
1364 size_t len,
1365 unsigned usage,
1366 Checksum *C)
1368 return des_verify(context, EVP_md5(), key, data, len, C);
1370 #endif
1372 static krb5_error_code
1373 SHA1_checksum(krb5_context context,
1374 struct key_data *key,
1375 const void *data,
1376 size_t len,
1377 unsigned usage,
1378 Checksum *C)
1380 if (EVP_Digest(data, len, C->checksum.data, NULL, EVP_sha1(), NULL) != 1)
1381 krb5_abortx(context, "sha1 checksum failed");
1382 return 0;
1385 /* HMAC according to RFC2104 */
1386 static krb5_error_code
1387 hmac(krb5_context context,
1388 struct checksum_type *cm,
1389 const void *data,
1390 size_t len,
1391 unsigned usage,
1392 struct key_data *keyblock,
1393 Checksum *result)
1395 unsigned char *ipad, *opad;
1396 unsigned char *key;
1397 size_t key_len;
1398 int i;
1400 ipad = malloc(cm->blocksize + len);
1401 if (ipad == NULL)
1402 return ENOMEM;
1403 opad = malloc(cm->blocksize + cm->checksumsize);
1404 if (opad == NULL) {
1405 free(ipad);
1406 return ENOMEM;
1408 memset(ipad, 0x36, cm->blocksize);
1409 memset(opad, 0x5c, cm->blocksize);
1411 if(keyblock->key->keyvalue.length > cm->blocksize){
1412 (*cm->checksum)(context,
1413 keyblock,
1414 keyblock->key->keyvalue.data,
1415 keyblock->key->keyvalue.length,
1416 usage,
1417 result);
1418 key = result->checksum.data;
1419 key_len = result->checksum.length;
1420 } else {
1421 key = keyblock->key->keyvalue.data;
1422 key_len = keyblock->key->keyvalue.length;
1424 for(i = 0; i < key_len; i++){
1425 ipad[i] ^= key[i];
1426 opad[i] ^= key[i];
1428 memcpy(ipad + cm->blocksize, data, len);
1429 (*cm->checksum)(context, keyblock, ipad, cm->blocksize + len,
1430 usage, result);
1431 memcpy(opad + cm->blocksize, result->checksum.data,
1432 result->checksum.length);
1433 (*cm->checksum)(context, keyblock, opad,
1434 cm->blocksize + cm->checksumsize, usage, result);
1435 memset(ipad, 0, cm->blocksize + len);
1436 free(ipad);
1437 memset(opad, 0, cm->blocksize + cm->checksumsize);
1438 free(opad);
1440 return 0;
1443 krb5_error_code KRB5_LIB_FUNCTION
1444 krb5_hmac(krb5_context context,
1445 krb5_cksumtype cktype,
1446 const void *data,
1447 size_t len,
1448 unsigned usage,
1449 krb5_keyblock *key,
1450 Checksum *result)
1452 struct checksum_type *c = _find_checksum(cktype);
1453 struct key_data kd;
1454 krb5_error_code ret;
1456 if (c == NULL) {
1457 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1458 N_("checksum type %d not supported", ""),
1459 cktype);
1460 return KRB5_PROG_SUMTYPE_NOSUPP;
1463 kd.key = key;
1464 kd.schedule = NULL;
1466 ret = hmac(context, c, data, len, usage, &kd, result);
1468 if (kd.schedule)
1469 krb5_free_data(context, kd.schedule);
1471 return ret;
1474 static krb5_error_code
1475 SP_HMAC_SHA1_checksum(krb5_context context,
1476 struct key_data *key,
1477 const void *data,
1478 size_t len,
1479 unsigned usage,
1480 Checksum *result)
1482 struct checksum_type *c = _find_checksum(CKSUMTYPE_SHA1);
1483 Checksum res;
1484 char sha1_data[20];
1485 krb5_error_code ret;
1487 res.checksum.data = sha1_data;
1488 res.checksum.length = sizeof(sha1_data);
1490 ret = hmac(context, c, data, len, usage, key, &res);
1491 if (ret)
1492 krb5_abortx(context, "hmac failed");
1493 memcpy(result->checksum.data, res.checksum.data, result->checksum.length);
1494 return 0;
1498 * checksum according to section 5. of draft-brezak-win2k-krb-rc4-hmac-03.txt
1501 static krb5_error_code
1502 HMAC_MD5_checksum(krb5_context context,
1503 struct key_data *key,
1504 const void *data,
1505 size_t len,
1506 unsigned usage,
1507 Checksum *result)
1509 EVP_MD_CTX *m;
1510 struct checksum_type *c = _find_checksum (CKSUMTYPE_RSA_MD5);
1511 const char signature[] = "signaturekey";
1512 Checksum ksign_c;
1513 struct key_data ksign;
1514 krb5_keyblock kb;
1515 unsigned char t[4];
1516 unsigned char tmp[16];
1517 unsigned char ksign_c_data[16];
1518 krb5_error_code ret;
1520 m = EVP_MD_CTX_create();
1521 if (m == NULL) {
1522 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1523 return ENOMEM;
1525 ksign_c.checksum.length = sizeof(ksign_c_data);
1526 ksign_c.checksum.data = ksign_c_data;
1527 ret = hmac(context, c, signature, sizeof(signature), 0, key, &ksign_c);
1528 if (ret) {
1529 EVP_MD_CTX_destroy(m);
1530 return ret;
1532 ksign.key = &kb;
1533 kb.keyvalue = ksign_c.checksum;
1534 EVP_DigestInit_ex(m, EVP_md5(), NULL);
1535 t[0] = (usage >> 0) & 0xFF;
1536 t[1] = (usage >> 8) & 0xFF;
1537 t[2] = (usage >> 16) & 0xFF;
1538 t[3] = (usage >> 24) & 0xFF;
1539 EVP_DigestUpdate(m, t, 4);
1540 EVP_DigestUpdate(m, data, len);
1541 EVP_DigestFinal_ex (m, tmp, NULL);
1542 EVP_MD_CTX_destroy(m);
1544 ret = hmac(context, c, tmp, sizeof(tmp), 0, &ksign, result);
1545 if (ret)
1546 return ret;
1547 return 0;
1550 static struct checksum_type checksum_none = {
1551 CKSUMTYPE_NONE,
1552 "none",
1556 NONE_checksum,
1557 NULL
1559 #ifdef HEIM_WEAK_CRYPTO
1560 static struct checksum_type checksum_crc32 = {
1561 CKSUMTYPE_CRC32,
1562 "crc32",
1566 CRC32_checksum,
1567 NULL
1569 static struct checksum_type checksum_rsa_md4 = {
1570 CKSUMTYPE_RSA_MD4,
1571 "rsa-md4",
1574 F_CPROOF,
1575 RSA_MD4_checksum,
1576 NULL
1578 static struct checksum_type checksum_rsa_md4_des = {
1579 CKSUMTYPE_RSA_MD4_DES,
1580 "rsa-md4-des",
1583 F_KEYED | F_CPROOF | F_VARIANT,
1584 RSA_MD4_DES_checksum,
1585 RSA_MD4_DES_verify
1587 static struct checksum_type checksum_rsa_md5_des = {
1588 CKSUMTYPE_RSA_MD5_DES,
1589 "rsa-md5-des",
1592 F_KEYED | F_CPROOF | F_VARIANT,
1593 RSA_MD5_DES_checksum,
1594 RSA_MD5_DES_verify
1596 #endif /* HEIM_WEAK_CRYPTO */
1598 static krb5_error_code
1599 RSA_MD5_checksum(krb5_context context,
1600 struct key_data *key,
1601 const void *data,
1602 size_t len,
1603 unsigned usage,
1604 Checksum *C)
1606 if (EVP_Digest(data, len, C->checksum.data, NULL, EVP_md5(), NULL) != 1)
1607 krb5_abortx(context, "md5 checksum failed");
1608 return 0;
1611 static struct checksum_type checksum_rsa_md5 = {
1612 CKSUMTYPE_RSA_MD5,
1613 "rsa-md5",
1616 F_CPROOF,
1617 RSA_MD5_checksum,
1618 NULL
1621 #ifdef DES3_OLD_ENCTYPE
1622 static struct checksum_type checksum_rsa_md5_des3 = {
1623 CKSUMTYPE_RSA_MD5_DES3,
1624 "rsa-md5-des3",
1627 F_KEYED | F_CPROOF | F_VARIANT,
1628 RSA_MD5_DES3_checksum,
1629 RSA_MD5_DES3_verify
1631 #endif
1632 static struct checksum_type checksum_sha1 = {
1633 CKSUMTYPE_SHA1,
1634 "sha1",
1637 F_CPROOF,
1638 SHA1_checksum,
1639 NULL
1641 static struct checksum_type checksum_hmac_sha1_des3 = {
1642 CKSUMTYPE_HMAC_SHA1_DES3,
1643 "hmac-sha1-des3",
1646 F_KEYED | F_CPROOF | F_DERIVED,
1647 SP_HMAC_SHA1_checksum,
1648 NULL
1651 static struct checksum_type checksum_hmac_sha1_aes128 = {
1652 CKSUMTYPE_HMAC_SHA1_96_AES_128,
1653 "hmac-sha1-96-aes128",
1656 F_KEYED | F_CPROOF | F_DERIVED,
1657 SP_HMAC_SHA1_checksum,
1658 NULL
1661 static struct checksum_type checksum_hmac_sha1_aes256 = {
1662 CKSUMTYPE_HMAC_SHA1_96_AES_256,
1663 "hmac-sha1-96-aes256",
1666 F_KEYED | F_CPROOF | F_DERIVED,
1667 SP_HMAC_SHA1_checksum,
1668 NULL
1671 static struct checksum_type checksum_hmac_md5 = {
1672 CKSUMTYPE_HMAC_MD5,
1673 "hmac-md5",
1676 F_KEYED | F_CPROOF,
1677 HMAC_MD5_checksum,
1678 NULL
1681 static struct checksum_type *checksum_types[] = {
1682 &checksum_none,
1683 #ifdef HEIM_WEAK_CRYPTO
1684 &checksum_crc32,
1685 &checksum_rsa_md4,
1686 &checksum_rsa_md4_des,
1687 &checksum_rsa_md5_des,
1688 #endif
1689 #ifdef DES3_OLD_ENCTYPE
1690 &checksum_rsa_md5_des3,
1691 #endif
1692 &checksum_rsa_md5,
1693 &checksum_sha1,
1694 &checksum_hmac_sha1_des3,
1695 &checksum_hmac_sha1_aes128,
1696 &checksum_hmac_sha1_aes256,
1697 &checksum_hmac_md5
1700 static int num_checksums = sizeof(checksum_types) / sizeof(checksum_types[0]);
1702 static struct checksum_type *
1703 _find_checksum(krb5_cksumtype type)
1705 int i;
1706 for(i = 0; i < num_checksums; i++)
1707 if(checksum_types[i]->type == type)
1708 return checksum_types[i];
1709 return NULL;
1712 static krb5_error_code
1713 get_checksum_key(krb5_context context,
1714 krb5_crypto crypto,
1715 unsigned usage, /* not krb5_key_usage */
1716 struct checksum_type *ct,
1717 struct key_data **key)
1719 krb5_error_code ret = 0;
1721 if(ct->flags & F_DERIVED)
1722 ret = _get_derived_key(context, crypto, usage, key);
1723 else if(ct->flags & F_VARIANT) {
1724 int i;
1726 *key = _new_derived_key(crypto, 0xff/* KRB5_KU_RFC1510_VARIANT */);
1727 if(*key == NULL) {
1728 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1729 return ENOMEM;
1731 ret = krb5_copy_keyblock(context, crypto->key.key, &(*key)->key);
1732 if(ret)
1733 return ret;
1734 for(i = 0; i < (*key)->key->keyvalue.length; i++)
1735 ((unsigned char*)(*key)->key->keyvalue.data)[i] ^= 0xF0;
1736 } else {
1737 *key = &crypto->key;
1739 if(ret == 0)
1740 ret = _key_schedule(context, *key);
1741 return ret;
1744 static krb5_error_code
1745 create_checksum (krb5_context context,
1746 struct checksum_type *ct,
1747 krb5_crypto crypto,
1748 unsigned usage,
1749 void *data,
1750 size_t len,
1751 Checksum *result)
1753 krb5_error_code ret;
1754 struct key_data *dkey;
1755 int keyed_checksum;
1757 if (ct->flags & F_DISABLED) {
1758 krb5_clear_error_message (context);
1759 return KRB5_PROG_SUMTYPE_NOSUPP;
1761 keyed_checksum = (ct->flags & F_KEYED) != 0;
1762 if(keyed_checksum && crypto == NULL) {
1763 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1764 N_("Checksum type %s is keyed but no "
1765 "crypto context (key) was passed in", ""),
1766 ct->name);
1767 return KRB5_PROG_SUMTYPE_NOSUPP; /* XXX */
1769 if(keyed_checksum) {
1770 ret = get_checksum_key(context, crypto, usage, ct, &dkey);
1771 if (ret)
1772 return ret;
1773 } else
1774 dkey = NULL;
1775 result->cksumtype = ct->type;
1776 ret = krb5_data_alloc(&result->checksum, ct->checksumsize);
1777 if (ret)
1778 return (ret);
1779 return (*ct->checksum)(context, dkey, data, len, usage, result);
1782 static int
1783 arcfour_checksum_p(struct checksum_type *ct, krb5_crypto crypto)
1785 return (ct->type == CKSUMTYPE_HMAC_MD5) &&
1786 (crypto->key.key->keytype == KEYTYPE_ARCFOUR);
1789 krb5_error_code KRB5_LIB_FUNCTION
1790 krb5_create_checksum(krb5_context context,
1791 krb5_crypto crypto,
1792 krb5_key_usage usage,
1793 int type,
1794 void *data,
1795 size_t len,
1796 Checksum *result)
1798 struct checksum_type *ct = NULL;
1799 unsigned keyusage;
1801 /* type 0 -> pick from crypto */
1802 if (type) {
1803 ct = _find_checksum(type);
1804 } else if (crypto) {
1805 ct = crypto->et->keyed_checksum;
1806 if (ct == NULL)
1807 ct = crypto->et->checksum;
1810 if(ct == NULL) {
1811 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1812 N_("checksum type %d not supported", ""),
1813 type);
1814 return KRB5_PROG_SUMTYPE_NOSUPP;
1817 if (arcfour_checksum_p(ct, crypto)) {
1818 keyusage = usage;
1819 usage2arcfour(context, &keyusage);
1820 } else
1821 keyusage = CHECKSUM_USAGE(usage);
1823 return create_checksum(context, ct, crypto, keyusage,
1824 data, len, result);
1827 static krb5_error_code
1828 verify_checksum(krb5_context context,
1829 krb5_crypto crypto,
1830 unsigned usage, /* not krb5_key_usage */
1831 void *data,
1832 size_t len,
1833 Checksum *cksum)
1835 krb5_error_code ret;
1836 struct key_data *dkey;
1837 int keyed_checksum;
1838 Checksum c;
1839 struct checksum_type *ct;
1841 ct = _find_checksum(cksum->cksumtype);
1842 if (ct == NULL || (ct->flags & F_DISABLED)) {
1843 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1844 N_("checksum type %d not supported", ""),
1845 cksum->cksumtype);
1846 return KRB5_PROG_SUMTYPE_NOSUPP;
1848 if(ct->checksumsize != cksum->checksum.length) {
1849 krb5_clear_error_message (context);
1850 return KRB5KRB_AP_ERR_BAD_INTEGRITY; /* XXX */
1852 keyed_checksum = (ct->flags & F_KEYED) != 0;
1853 if(keyed_checksum) {
1854 struct checksum_type *kct;
1855 if (crypto == NULL) {
1856 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1857 N_("Checksum type %s is keyed but no "
1858 "crypto context (key) was passed in", ""),
1859 ct->name);
1860 return KRB5_PROG_SUMTYPE_NOSUPP; /* XXX */
1862 kct = crypto->et->keyed_checksum;
1863 if (kct != NULL && kct->type != ct->type) {
1864 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1865 N_("Checksum type %s is keyed, but "
1866 "the key type %s passed didnt have that checksum "
1867 "type as the keyed type", ""),
1868 ct->name, crypto->et->name);
1869 return KRB5_PROG_SUMTYPE_NOSUPP; /* XXX */
1872 ret = get_checksum_key(context, crypto, usage, ct, &dkey);
1873 if (ret)
1874 return ret;
1875 } else
1876 dkey = NULL;
1877 if(ct->verify)
1878 return (*ct->verify)(context, dkey, data, len, usage, cksum);
1880 ret = krb5_data_alloc (&c.checksum, ct->checksumsize);
1881 if (ret)
1882 return ret;
1884 ret = (*ct->checksum)(context, dkey, data, len, usage, &c);
1885 if (ret) {
1886 krb5_data_free(&c.checksum);
1887 return ret;
1890 if(c.checksum.length != cksum->checksum.length ||
1891 ct_memcmp(c.checksum.data, cksum->checksum.data, c.checksum.length)) {
1892 krb5_clear_error_message (context);
1893 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1894 } else {
1895 ret = 0;
1897 krb5_data_free (&c.checksum);
1898 return ret;
1901 krb5_error_code KRB5_LIB_FUNCTION
1902 krb5_verify_checksum(krb5_context context,
1903 krb5_crypto crypto,
1904 krb5_key_usage usage,
1905 void *data,
1906 size_t len,
1907 Checksum *cksum)
1909 struct checksum_type *ct;
1910 unsigned keyusage;
1912 ct = _find_checksum(cksum->cksumtype);
1913 if(ct == NULL) {
1914 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1915 N_("checksum type %d not supported", ""),
1916 cksum->cksumtype);
1917 return KRB5_PROG_SUMTYPE_NOSUPP;
1920 if (arcfour_checksum_p(ct, crypto)) {
1921 keyusage = usage;
1922 usage2arcfour(context, &keyusage);
1923 } else
1924 keyusage = CHECKSUM_USAGE(usage);
1926 return verify_checksum(context, crypto, keyusage,
1927 data, len, cksum);
1930 krb5_error_code KRB5_LIB_FUNCTION
1931 krb5_crypto_get_checksum_type(krb5_context context,
1932 krb5_crypto crypto,
1933 krb5_cksumtype *type)
1935 struct checksum_type *ct = NULL;
1937 if (crypto != NULL) {
1938 ct = crypto->et->keyed_checksum;
1939 if (ct == NULL)
1940 ct = crypto->et->checksum;
1943 if (ct == NULL) {
1944 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1945 N_("checksum type not found", ""));
1946 return KRB5_PROG_SUMTYPE_NOSUPP;
1949 *type = ct->type;
1951 return 0;
1955 krb5_error_code KRB5_LIB_FUNCTION
1956 krb5_checksumsize(krb5_context context,
1957 krb5_cksumtype type,
1958 size_t *size)
1960 struct checksum_type *ct = _find_checksum(type);
1961 if(ct == NULL) {
1962 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1963 N_("checksum type %d not supported", ""),
1964 type);
1965 return KRB5_PROG_SUMTYPE_NOSUPP;
1967 *size = ct->checksumsize;
1968 return 0;
1971 krb5_boolean KRB5_LIB_FUNCTION
1972 krb5_checksum_is_keyed(krb5_context context,
1973 krb5_cksumtype type)
1975 struct checksum_type *ct = _find_checksum(type);
1976 if(ct == NULL) {
1977 if (context)
1978 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1979 N_("checksum type %d not supported", ""),
1980 type);
1981 return KRB5_PROG_SUMTYPE_NOSUPP;
1983 return ct->flags & F_KEYED;
1986 krb5_boolean KRB5_LIB_FUNCTION
1987 krb5_checksum_is_collision_proof(krb5_context context,
1988 krb5_cksumtype type)
1990 struct checksum_type *ct = _find_checksum(type);
1991 if(ct == NULL) {
1992 if (context)
1993 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
1994 N_("checksum type %d not supported", ""),
1995 type);
1996 return KRB5_PROG_SUMTYPE_NOSUPP;
1998 return ct->flags & F_CPROOF;
2001 krb5_error_code KRB5_LIB_FUNCTION
2002 krb5_checksum_disable(krb5_context context,
2003 krb5_cksumtype type)
2005 struct checksum_type *ct = _find_checksum(type);
2006 if(ct == NULL) {
2007 if (context)
2008 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
2009 N_("checksum type %d not supported", ""),
2010 type);
2011 return KRB5_PROG_SUMTYPE_NOSUPP;
2013 ct->flags |= F_DISABLED;
2014 return 0;
2017 /************************************************************
2019 ************************************************************/
2021 static krb5_error_code
2022 NULL_encrypt(krb5_context context,
2023 struct key_data *key,
2024 void *data,
2025 size_t len,
2026 krb5_boolean encryptp,
2027 int usage,
2028 void *ivec)
2030 return 0;
2033 static krb5_error_code
2034 evp_encrypt(krb5_context context,
2035 struct key_data *key,
2036 void *data,
2037 size_t len,
2038 krb5_boolean encryptp,
2039 int usage,
2040 void *ivec)
2042 struct evp_schedule *ctx = key->schedule->data;
2043 EVP_CIPHER_CTX *c;
2044 c = encryptp ? &ctx->ectx : &ctx->dctx;
2045 if (ivec == NULL) {
2046 /* alloca ? */
2047 size_t len = EVP_CIPHER_CTX_iv_length(c);
2048 void *loiv = malloc(len);
2049 if (loiv == NULL) {
2050 krb5_clear_error_message(context);
2051 return ENOMEM;
2053 memset(loiv, 0, len);
2054 EVP_CipherInit_ex(c, NULL, NULL, NULL, loiv, -1);
2055 free(loiv);
2056 } else
2057 EVP_CipherInit_ex(c, NULL, NULL, NULL, ivec, -1);
2058 EVP_Cipher(c, data, data, len);
2059 return 0;
2062 static const unsigned char zero_ivec[EVP_MAX_BLOCK_LENGTH] = { 0 };
2064 static krb5_error_code
2065 evp_encrypt_cts(krb5_context context,
2066 struct key_data *key,
2067 void *data,
2068 size_t len,
2069 krb5_boolean encryptp,
2070 int usage,
2071 void *ivec)
2073 size_t i, blocksize;
2074 struct evp_schedule *ctx = key->schedule->data;
2075 char tmp[EVP_MAX_BLOCK_LENGTH], ivec2[EVP_MAX_BLOCK_LENGTH];
2076 EVP_CIPHER_CTX *c;
2077 unsigned char *p;
2079 c = encryptp ? &ctx->ectx : &ctx->dctx;
2081 blocksize = EVP_CIPHER_CTX_block_size(c);
2083 if (len < blocksize) {
2084 krb5_set_error_message(context, EINVAL,
2085 "message block too short");
2086 return EINVAL;
2087 } else if (len == blocksize) {
2088 EVP_CipherInit_ex(c, NULL, NULL, NULL, zero_ivec, -1);
2089 EVP_Cipher(c, data, data, len);
2090 return 0;
2093 if (ivec)
2094 EVP_CipherInit_ex(c, NULL, NULL, NULL, ivec, -1);
2095 else
2096 EVP_CipherInit_ex(c, NULL, NULL, NULL, zero_ivec, -1);
2098 if (encryptp) {
2100 p = data;
2101 i = ((len - 1) / blocksize) * blocksize;
2102 EVP_Cipher(c, p, p, i);
2103 p += i - blocksize;
2104 len -= i;
2105 memcpy(ivec2, p, blocksize);
2107 for (i = 0; i < len; i++)
2108 tmp[i] = p[i + blocksize] ^ ivec2[i];
2109 for (; i < blocksize; i++)
2110 tmp[i] = 0 ^ ivec2[i];
2112 EVP_CipherInit_ex(c, NULL, NULL, NULL, zero_ivec, -1);
2113 EVP_Cipher(c, p, tmp, blocksize);
2115 memcpy(p + blocksize, ivec2, len);
2116 if (ivec)
2117 memcpy(ivec, p, blocksize);
2118 } else {
2119 char tmp2[EVP_MAX_BLOCK_LENGTH], tmp3[EVP_MAX_BLOCK_LENGTH];
2121 p = data;
2122 if (len > blocksize * 2) {
2123 /* remove last two blocks and round up, decrypt this with cbc, then do cts dance */
2124 i = ((((len - blocksize * 2) + blocksize - 1) / blocksize) * blocksize);
2125 memcpy(ivec2, p + i - blocksize, blocksize);
2126 EVP_Cipher(c, p, p, i);
2127 p += i;
2128 len -= i + blocksize;
2129 } else {
2130 if (ivec)
2131 memcpy(ivec2, ivec, blocksize);
2132 else
2133 memcpy(ivec2, zero_ivec, blocksize);
2134 len -= blocksize;
2137 memcpy(tmp, p, blocksize);
2138 EVP_CipherInit_ex(c, NULL, NULL, NULL, zero_ivec, -1);
2139 EVP_Cipher(c, tmp2, p, blocksize);
2141 memcpy(tmp3, p + blocksize, len);
2142 memcpy(tmp3 + len, tmp2 + len, blocksize - len); /* xor 0 */
2144 for (i = 0; i < len; i++)
2145 p[i + blocksize] = tmp2[i] ^ tmp3[i];
2147 EVP_CipherInit_ex(c, NULL, NULL, NULL, zero_ivec, -1);
2148 EVP_Cipher(c, p, tmp3, blocksize);
2150 for (i = 0; i < blocksize; i++)
2151 p[i] ^= ivec2[i];
2152 if (ivec)
2153 memcpy(ivec, tmp, blocksize);
2155 return 0;
2158 #ifdef HEIM_WEAK_CRYPTO
2159 static krb5_error_code
2160 evp_des_encrypt_null_ivec(krb5_context context,
2161 struct key_data *key,
2162 void *data,
2163 size_t len,
2164 krb5_boolean encryptp,
2165 int usage,
2166 void *ignore_ivec)
2168 struct evp_schedule *ctx = key->schedule->data;
2169 EVP_CIPHER_CTX *c;
2170 DES_cblock ivec;
2171 memset(&ivec, 0, sizeof(ivec));
2172 c = encryptp ? &ctx->ectx : &ctx->dctx;
2173 EVP_CipherInit_ex(c, NULL, NULL, NULL, (void *)&ivec, -1);
2174 EVP_Cipher(c, data, data, len);
2175 return 0;
2178 static krb5_error_code
2179 evp_des_encrypt_key_ivec(krb5_context context,
2180 struct key_data *key,
2181 void *data,
2182 size_t len,
2183 krb5_boolean encryptp,
2184 int usage,
2185 void *ignore_ivec)
2187 struct evp_schedule *ctx = key->schedule->data;
2188 EVP_CIPHER_CTX *c;
2189 DES_cblock ivec;
2190 memcpy(&ivec, key->key->keyvalue.data, sizeof(ivec));
2191 c = encryptp ? &ctx->ectx : &ctx->dctx;
2192 EVP_CipherInit_ex(c, NULL, NULL, NULL, (void *)&ivec, -1);
2193 EVP_Cipher(c, data, data, len);
2194 return 0;
2197 static krb5_error_code
2198 DES_CFB64_encrypt_null_ivec(krb5_context context,
2199 struct key_data *key,
2200 void *data,
2201 size_t len,
2202 krb5_boolean encryptp,
2203 int usage,
2204 void *ignore_ivec)
2206 DES_cblock ivec;
2207 int num = 0;
2208 DES_key_schedule *s = key->schedule->data;
2209 memset(&ivec, 0, sizeof(ivec));
2211 DES_cfb64_encrypt(data, data, len, s, &ivec, &num, encryptp);
2212 return 0;
2215 static krb5_error_code
2216 DES_PCBC_encrypt_key_ivec(krb5_context context,
2217 struct key_data *key,
2218 void *data,
2219 size_t len,
2220 krb5_boolean encryptp,
2221 int usage,
2222 void *ignore_ivec)
2224 DES_cblock ivec;
2225 DES_key_schedule *s = key->schedule->data;
2226 memcpy(&ivec, key->key->keyvalue.data, sizeof(ivec));
2228 DES_pcbc_encrypt(data, data, len, s, &ivec, encryptp);
2229 return 0;
2231 #endif
2234 * section 6 of draft-brezak-win2k-krb-rc4-hmac-03
2236 * warning: not for small children
2239 static krb5_error_code
2240 ARCFOUR_subencrypt(krb5_context context,
2241 struct key_data *key,
2242 void *data,
2243 size_t len,
2244 unsigned usage,
2245 void *ivec)
2247 EVP_CIPHER_CTX ctx;
2248 struct checksum_type *c = _find_checksum (CKSUMTYPE_RSA_MD5);
2249 Checksum k1_c, k2_c, k3_c, cksum;
2250 struct key_data ke;
2251 krb5_keyblock kb;
2252 unsigned char t[4];
2253 unsigned char *cdata = data;
2254 unsigned char k1_c_data[16], k2_c_data[16], k3_c_data[16];
2255 krb5_error_code ret;
2257 t[0] = (usage >> 0) & 0xFF;
2258 t[1] = (usage >> 8) & 0xFF;
2259 t[2] = (usage >> 16) & 0xFF;
2260 t[3] = (usage >> 24) & 0xFF;
2262 k1_c.checksum.length = sizeof(k1_c_data);
2263 k1_c.checksum.data = k1_c_data;
2265 ret = hmac(NULL, c, t, sizeof(t), 0, key, &k1_c);
2266 if (ret)
2267 krb5_abortx(context, "hmac failed");
2269 memcpy (k2_c_data, k1_c_data, sizeof(k1_c_data));
2271 k2_c.checksum.length = sizeof(k2_c_data);
2272 k2_c.checksum.data = k2_c_data;
2274 ke.key = &kb;
2275 kb.keyvalue = k2_c.checksum;
2277 cksum.checksum.length = 16;
2278 cksum.checksum.data = data;
2280 ret = hmac(NULL, c, cdata + 16, len - 16, 0, &ke, &cksum);
2281 if (ret)
2282 krb5_abortx(context, "hmac failed");
2284 ke.key = &kb;
2285 kb.keyvalue = k1_c.checksum;
2287 k3_c.checksum.length = sizeof(k3_c_data);
2288 k3_c.checksum.data = k3_c_data;
2290 ret = hmac(NULL, c, data, 16, 0, &ke, &k3_c);
2291 if (ret)
2292 krb5_abortx(context, "hmac failed");
2294 EVP_CIPHER_CTX_init(&ctx);
2296 EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, k3_c.checksum.data, NULL, 1);
2297 EVP_Cipher(&ctx, cdata + 16, cdata + 16, len - 16);
2298 EVP_CIPHER_CTX_cleanup(&ctx);
2300 memset (k1_c_data, 0, sizeof(k1_c_data));
2301 memset (k2_c_data, 0, sizeof(k2_c_data));
2302 memset (k3_c_data, 0, sizeof(k3_c_data));
2303 return 0;
2306 static krb5_error_code
2307 ARCFOUR_subdecrypt(krb5_context context,
2308 struct key_data *key,
2309 void *data,
2310 size_t len,
2311 unsigned usage,
2312 void *ivec)
2314 EVP_CIPHER_CTX ctx;
2315 struct checksum_type *c = _find_checksum (CKSUMTYPE_RSA_MD5);
2316 Checksum k1_c, k2_c, k3_c, cksum;
2317 struct key_data ke;
2318 krb5_keyblock kb;
2319 unsigned char t[4];
2320 unsigned char *cdata = data;
2321 unsigned char k1_c_data[16], k2_c_data[16], k3_c_data[16];
2322 unsigned char cksum_data[16];
2323 krb5_error_code ret;
2325 t[0] = (usage >> 0) & 0xFF;
2326 t[1] = (usage >> 8) & 0xFF;
2327 t[2] = (usage >> 16) & 0xFF;
2328 t[3] = (usage >> 24) & 0xFF;
2330 k1_c.checksum.length = sizeof(k1_c_data);
2331 k1_c.checksum.data = k1_c_data;
2333 ret = hmac(NULL, c, t, sizeof(t), 0, key, &k1_c);
2334 if (ret)
2335 krb5_abortx(context, "hmac failed");
2337 memcpy (k2_c_data, k1_c_data, sizeof(k1_c_data));
2339 k2_c.checksum.length = sizeof(k2_c_data);
2340 k2_c.checksum.data = k2_c_data;
2342 ke.key = &kb;
2343 kb.keyvalue = k1_c.checksum;
2345 k3_c.checksum.length = sizeof(k3_c_data);
2346 k3_c.checksum.data = k3_c_data;
2348 ret = hmac(NULL, c, cdata, 16, 0, &ke, &k3_c);
2349 if (ret)
2350 krb5_abortx(context, "hmac failed");
2352 EVP_CIPHER_CTX_init(&ctx);
2353 EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, k3_c.checksum.data, NULL, 0);
2354 EVP_Cipher(&ctx, cdata + 16, cdata + 16, len - 16);
2355 EVP_CIPHER_CTX_cleanup(&ctx);
2357 ke.key = &kb;
2358 kb.keyvalue = k2_c.checksum;
2360 cksum.checksum.length = 16;
2361 cksum.checksum.data = cksum_data;
2363 ret = hmac(NULL, c, cdata + 16, len - 16, 0, &ke, &cksum);
2364 if (ret)
2365 krb5_abortx(context, "hmac failed");
2367 memset (k1_c_data, 0, sizeof(k1_c_data));
2368 memset (k2_c_data, 0, sizeof(k2_c_data));
2369 memset (k3_c_data, 0, sizeof(k3_c_data));
2371 if (ct_memcmp (cksum.checksum.data, data, 16) != 0) {
2372 krb5_clear_error_message (context);
2373 return KRB5KRB_AP_ERR_BAD_INTEGRITY;
2374 } else {
2375 return 0;
2380 * convert the usage numbers used in
2381 * draft-ietf-cat-kerb-key-derivation-00.txt to the ones in
2382 * draft-brezak-win2k-krb-rc4-hmac-04.txt
2385 static krb5_error_code
2386 usage2arcfour (krb5_context context, unsigned *usage)
2388 switch (*usage) {
2389 case KRB5_KU_AS_REP_ENC_PART : /* 3 */
2390 *usage = 8;
2391 return 0;
2392 case KRB5_KU_USAGE_SEAL : /* 22 */
2393 *usage = 13;
2394 return 0;
2395 case KRB5_KU_USAGE_SIGN : /* 23 */
2396 *usage = 15;
2397 return 0;
2398 case KRB5_KU_USAGE_SEQ: /* 24 */
2399 *usage = 0;
2400 return 0;
2401 default :
2402 return 0;
2406 static krb5_error_code
2407 ARCFOUR_encrypt(krb5_context context,
2408 struct key_data *key,
2409 void *data,
2410 size_t len,
2411 krb5_boolean encryptp,
2412 int usage,
2413 void *ivec)
2415 krb5_error_code ret;
2416 unsigned keyusage = usage;
2418 if((ret = usage2arcfour (context, &keyusage)) != 0)
2419 return ret;
2421 if (encryptp)
2422 return ARCFOUR_subencrypt (context, key, data, len, keyusage, ivec);
2423 else
2424 return ARCFOUR_subdecrypt (context, key, data, len, keyusage, ivec);
2432 static krb5_error_code
2433 AES_PRF(krb5_context context,
2434 krb5_crypto crypto,
2435 const krb5_data *in,
2436 krb5_data *out)
2438 struct checksum_type *ct = crypto->et->checksum;
2439 krb5_error_code ret;
2440 Checksum result;
2441 krb5_keyblock *derived;
2443 result.cksumtype = ct->type;
2444 ret = krb5_data_alloc(&result.checksum, ct->checksumsize);
2445 if (ret) {
2446 krb5_set_error_message(context, ret, N_("malloc: out memory", ""));
2447 return ret;
2450 ret = (*ct->checksum)(context, NULL, in->data, in->length, 0, &result);
2451 if (ret) {
2452 krb5_data_free(&result.checksum);
2453 return ret;
2456 if (result.checksum.length < crypto->et->blocksize)
2457 krb5_abortx(context, "internal prf error");
2459 derived = NULL;
2460 ret = krb5_derive_key(context, crypto->key.key,
2461 crypto->et->type, "prf", 3, &derived);
2462 if (ret)
2463 krb5_abortx(context, "krb5_derive_key");
2465 ret = krb5_data_alloc(out, crypto->et->blocksize);
2466 if (ret)
2467 krb5_abortx(context, "malloc failed");
2470 const EVP_CIPHER *c = (*crypto->et->keytype->evp)();
2471 EVP_CIPHER_CTX ctx;
2473 EVP_CIPHER_CTX_init(&ctx); /* ivec all zero */
2474 EVP_CipherInit_ex(&ctx, c, NULL, derived->keyvalue.data, NULL, 1);
2475 EVP_Cipher(&ctx, out->data, result.checksum.data,
2476 crypto->et->blocksize);
2477 EVP_CIPHER_CTX_cleanup(&ctx);
2480 krb5_data_free(&result.checksum);
2481 krb5_free_keyblock(context, derived);
2483 return ret;
2487 * these should currently be in reverse preference order.
2488 * (only relevant for !F_PSEUDO) */
2490 static struct encryption_type enctype_null = {
2491 ETYPE_NULL,
2492 "null",
2496 &keytype_null,
2497 &checksum_none,
2498 NULL,
2499 F_DISABLED,
2500 NULL_encrypt,
2502 NULL
2504 static struct encryption_type enctype_arcfour_hmac_md5 = {
2505 ETYPE_ARCFOUR_HMAC_MD5,
2506 "arcfour-hmac-md5",
2510 &keytype_arcfour,
2511 &checksum_hmac_md5,
2512 NULL,
2513 F_SPECIAL,
2514 ARCFOUR_encrypt,
2516 NULL
2518 #ifdef DES3_OLD_ENCTYPE
2519 static struct encryption_type enctype_des3_cbc_md5 = {
2520 ETYPE_DES3_CBC_MD5,
2521 "des3-cbc-md5",
2525 &keytype_des3,
2526 &checksum_rsa_md5,
2527 &checksum_rsa_md5_des3,
2529 evp_encrypt,
2531 NULL
2533 #endif
2534 static struct encryption_type enctype_des3_cbc_sha1 = {
2535 ETYPE_DES3_CBC_SHA1,
2536 "des3-cbc-sha1",
2540 &keytype_des3_derived,
2541 &checksum_sha1,
2542 &checksum_hmac_sha1_des3,
2543 F_DERIVED,
2544 evp_encrypt,
2546 NULL
2548 #ifdef DES3_OLD_ENCTYPE
2549 static struct encryption_type enctype_old_des3_cbc_sha1 = {
2550 ETYPE_OLD_DES3_CBC_SHA1,
2551 "old-des3-cbc-sha1",
2555 &keytype_des3,
2556 &checksum_sha1,
2557 &checksum_hmac_sha1_des3,
2559 evp_encrypt,
2561 NULL
2563 #endif
2564 static struct encryption_type enctype_aes128_cts_hmac_sha1 = {
2565 ETYPE_AES128_CTS_HMAC_SHA1_96,
2566 "aes128-cts-hmac-sha1-96",
2570 &keytype_aes128,
2571 &checksum_sha1,
2572 &checksum_hmac_sha1_aes128,
2573 F_DERIVED,
2574 evp_encrypt_cts,
2576 AES_PRF
2578 static struct encryption_type enctype_aes256_cts_hmac_sha1 = {
2579 ETYPE_AES256_CTS_HMAC_SHA1_96,
2580 "aes256-cts-hmac-sha1-96",
2584 &keytype_aes256,
2585 &checksum_sha1,
2586 &checksum_hmac_sha1_aes256,
2587 F_DERIVED,
2588 evp_encrypt_cts,
2590 AES_PRF
2592 static struct encryption_type enctype_des3_cbc_none = {
2593 ETYPE_DES3_CBC_NONE,
2594 "des3-cbc-none",
2598 &keytype_des3_derived,
2599 &checksum_none,
2600 NULL,
2601 F_PSEUDO,
2602 evp_encrypt,
2604 NULL
2606 #ifdef HEIM_WEAK_CRYPTO
2607 static struct encryption_type enctype_des_cbc_crc = {
2608 ETYPE_DES_CBC_CRC,
2609 "des-cbc-crc",
2613 &keytype_des,
2614 &checksum_crc32,
2615 NULL,
2616 F_DISABLED|F_WEAK,
2617 evp_des_encrypt_key_ivec,
2619 NULL
2621 static struct encryption_type enctype_des_cbc_md4 = {
2622 ETYPE_DES_CBC_MD4,
2623 "des-cbc-md4",
2627 &keytype_des,
2628 &checksum_rsa_md4,
2629 &checksum_rsa_md4_des,
2630 F_DISABLED|F_WEAK,
2631 evp_des_encrypt_null_ivec,
2633 NULL
2635 static struct encryption_type enctype_des_cbc_md5 = {
2636 ETYPE_DES_CBC_MD5,
2637 "des-cbc-md5",
2641 &keytype_des,
2642 &checksum_rsa_md5,
2643 &checksum_rsa_md5_des,
2644 F_DISABLED|F_WEAK,
2645 evp_des_encrypt_null_ivec,
2647 NULL
2649 static struct encryption_type enctype_des_cbc_none = {
2650 ETYPE_DES_CBC_NONE,
2651 "des-cbc-none",
2655 &keytype_des,
2656 &checksum_none,
2657 NULL,
2658 F_PSEUDO|F_DISABLED|F_WEAK,
2659 evp_des_encrypt_null_ivec,
2661 NULL
2663 static struct encryption_type enctype_des_cfb64_none = {
2664 ETYPE_DES_CFB64_NONE,
2665 "des-cfb64-none",
2669 &keytype_des_old,
2670 &checksum_none,
2671 NULL,
2672 F_PSEUDO|F_DISABLED|F_WEAK,
2673 DES_CFB64_encrypt_null_ivec,
2675 NULL
2677 static struct encryption_type enctype_des_pcbc_none = {
2678 ETYPE_DES_PCBC_NONE,
2679 "des-pcbc-none",
2683 &keytype_des_old,
2684 &checksum_none,
2685 NULL,
2686 F_PSEUDO|F_DISABLED|F_WEAK,
2687 DES_PCBC_encrypt_key_ivec,
2689 NULL
2691 #endif /* HEIM_WEAK_CRYPTO */
2693 static struct encryption_type *etypes[] = {
2694 &enctype_aes256_cts_hmac_sha1,
2695 &enctype_aes128_cts_hmac_sha1,
2696 &enctype_des3_cbc_sha1,
2697 &enctype_des3_cbc_none, /* used by the gss-api mech */
2698 &enctype_arcfour_hmac_md5,
2699 #ifdef DES3_OLD_ENCTYPE
2700 &enctype_des3_cbc_md5,
2701 &enctype_old_des3_cbc_sha1,
2702 #endif
2703 #ifdef HEIM_WEAK_CRYPTO
2704 &enctype_des_cbc_crc,
2705 &enctype_des_cbc_md4,
2706 &enctype_des_cbc_md5,
2707 &enctype_des_cbc_none,
2708 &enctype_des_cfb64_none,
2709 &enctype_des_pcbc_none,
2710 #endif
2711 &enctype_null
2714 static unsigned num_etypes = sizeof(etypes) / sizeof(etypes[0]);
2717 static struct encryption_type *
2718 _find_enctype(krb5_enctype type)
2720 int i;
2721 for(i = 0; i < num_etypes; i++)
2722 if(etypes[i]->type == type)
2723 return etypes[i];
2724 return NULL;
2728 krb5_error_code KRB5_LIB_FUNCTION
2729 krb5_enctype_to_string(krb5_context context,
2730 krb5_enctype etype,
2731 char **string)
2733 struct encryption_type *e;
2734 e = _find_enctype(etype);
2735 if(e == NULL) {
2736 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
2737 N_("encryption type %d not supported", ""),
2738 etype);
2739 *string = NULL;
2740 return KRB5_PROG_ETYPE_NOSUPP;
2742 *string = strdup(e->name);
2743 if(*string == NULL) {
2744 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
2745 return ENOMEM;
2747 return 0;
2750 krb5_error_code KRB5_LIB_FUNCTION
2751 krb5_string_to_enctype(krb5_context context,
2752 const char *string,
2753 krb5_enctype *etype)
2755 int i;
2756 for(i = 0; i < num_etypes; i++)
2757 if(strcasecmp(etypes[i]->name, string) == 0){
2758 *etype = etypes[i]->type;
2759 return 0;
2761 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
2762 N_("encryption type %s not supported", ""),
2763 string);
2764 return KRB5_PROG_ETYPE_NOSUPP;
2767 krb5_error_code KRB5_LIB_FUNCTION
2768 krb5_enctype_to_keytype(krb5_context context,
2769 krb5_enctype etype,
2770 krb5_keytype *keytype)
2772 struct encryption_type *e = _find_enctype(etype);
2773 if(e == NULL) {
2774 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
2775 N_("encryption type %d not supported", ""),
2776 etype);
2777 return KRB5_PROG_ETYPE_NOSUPP;
2779 *keytype = e->keytype->type; /* XXX */
2780 return 0;
2783 krb5_error_code KRB5_LIB_FUNCTION
2784 krb5_enctype_valid(krb5_context context,
2785 krb5_enctype etype)
2787 struct encryption_type *e = _find_enctype(etype);
2788 if(e == NULL) {
2789 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
2790 N_("encryption type %d not supported", ""),
2791 etype);
2792 return KRB5_PROG_ETYPE_NOSUPP;
2794 if (e->flags & F_DISABLED) {
2795 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
2796 N_("encryption type %s is disabled", ""),
2797 e->name);
2798 return KRB5_PROG_ETYPE_NOSUPP;
2800 return 0;
2804 * Return the coresponding encryption type for a checksum type.
2806 * @param context Kerberos context
2807 * @param ctype The checksum type to get the result enctype for
2808 * @param etype The returned encryption, when the matching etype is
2809 * not found, etype is set to ETYPE_NULL.
2811 * @return Return an error code for an failure or 0 on success.
2812 * @ingroup krb5_crypto
2816 krb5_error_code KRB5_LIB_FUNCTION
2817 krb5_cksumtype_to_enctype(krb5_context context,
2818 krb5_cksumtype ctype,
2819 krb5_enctype *etype)
2821 int i;
2823 *etype = ETYPE_NULL;
2825 for(i = 0; i < num_etypes; i++) {
2826 if(etypes[i]->keyed_checksum &&
2827 etypes[i]->keyed_checksum->type == ctype)
2829 *etype = etypes[i]->type;
2830 return 0;
2834 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
2835 N_("checksum type %d not supported", ""),
2836 (int)ctype);
2837 return KRB5_PROG_SUMTYPE_NOSUPP;
2841 krb5_error_code KRB5_LIB_FUNCTION
2842 krb5_cksumtype_valid(krb5_context context,
2843 krb5_cksumtype ctype)
2845 struct checksum_type *c = _find_checksum(ctype);
2846 if (c == NULL) {
2847 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
2848 N_("checksum type %d not supported", ""),
2849 ctype);
2850 return KRB5_PROG_SUMTYPE_NOSUPP;
2852 if (c->flags & F_DISABLED) {
2853 krb5_set_error_message (context, KRB5_PROG_SUMTYPE_NOSUPP,
2854 N_("checksum type %s is disabled", ""),
2855 c->name);
2856 return KRB5_PROG_SUMTYPE_NOSUPP;
2858 return 0;
2862 static krb5_boolean
2863 derived_crypto(krb5_context context,
2864 krb5_crypto crypto)
2866 return (crypto->et->flags & F_DERIVED) != 0;
2869 static krb5_boolean
2870 special_crypto(krb5_context context,
2871 krb5_crypto crypto)
2873 return (crypto->et->flags & F_SPECIAL) != 0;
2876 #define CHECKSUMSIZE(C) ((C)->checksumsize)
2877 #define CHECKSUMTYPE(C) ((C)->type)
2879 static krb5_error_code
2880 encrypt_internal_derived(krb5_context context,
2881 krb5_crypto crypto,
2882 unsigned usage,
2883 const void *data,
2884 size_t len,
2885 krb5_data *result,
2886 void *ivec)
2888 size_t sz, block_sz, checksum_sz, total_sz;
2889 Checksum cksum;
2890 unsigned char *p, *q;
2891 krb5_error_code ret;
2892 struct key_data *dkey;
2893 const struct encryption_type *et = crypto->et;
2895 checksum_sz = CHECKSUMSIZE(et->keyed_checksum);
2897 sz = et->confoundersize + len;
2898 block_sz = (sz + et->padsize - 1) &~ (et->padsize - 1); /* pad */
2899 total_sz = block_sz + checksum_sz;
2900 p = calloc(1, total_sz);
2901 if(p == NULL) {
2902 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
2903 return ENOMEM;
2906 q = p;
2907 krb5_generate_random_block(q, et->confoundersize); /* XXX */
2908 q += et->confoundersize;
2909 memcpy(q, data, len);
2911 ret = create_checksum(context,
2912 et->keyed_checksum,
2913 crypto,
2914 INTEGRITY_USAGE(usage),
2916 block_sz,
2917 &cksum);
2918 if(ret == 0 && cksum.checksum.length != checksum_sz) {
2919 free_Checksum (&cksum);
2920 krb5_clear_error_message (context);
2921 ret = KRB5_CRYPTO_INTERNAL;
2923 if(ret)
2924 goto fail;
2925 memcpy(p + block_sz, cksum.checksum.data, cksum.checksum.length);
2926 free_Checksum (&cksum);
2927 ret = _get_derived_key(context, crypto, ENCRYPTION_USAGE(usage), &dkey);
2928 if(ret)
2929 goto fail;
2930 ret = _key_schedule(context, dkey);
2931 if(ret)
2932 goto fail;
2933 ret = (*et->encrypt)(context, dkey, p, block_sz, 1, usage, ivec);
2934 if (ret)
2935 goto fail;
2936 result->data = p;
2937 result->length = total_sz;
2938 return 0;
2939 fail:
2940 memset(p, 0, total_sz);
2941 free(p);
2942 return ret;
2946 static krb5_error_code
2947 encrypt_internal(krb5_context context,
2948 krb5_crypto crypto,
2949 const void *data,
2950 size_t len,
2951 krb5_data *result,
2952 void *ivec)
2954 size_t sz, block_sz, checksum_sz;
2955 Checksum cksum;
2956 unsigned char *p, *q;
2957 krb5_error_code ret;
2958 const struct encryption_type *et = crypto->et;
2960 checksum_sz = CHECKSUMSIZE(et->checksum);
2962 sz = et->confoundersize + checksum_sz + len;
2963 block_sz = (sz + et->padsize - 1) &~ (et->padsize - 1); /* pad */
2964 p = calloc(1, block_sz);
2965 if(p == NULL) {
2966 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
2967 return ENOMEM;
2970 q = p;
2971 krb5_generate_random_block(q, et->confoundersize); /* XXX */
2972 q += et->confoundersize;
2973 memset(q, 0, checksum_sz);
2974 q += checksum_sz;
2975 memcpy(q, data, len);
2977 ret = create_checksum(context,
2978 et->checksum,
2979 crypto,
2982 block_sz,
2983 &cksum);
2984 if(ret == 0 && cksum.checksum.length != checksum_sz) {
2985 krb5_clear_error_message (context);
2986 free_Checksum(&cksum);
2987 ret = KRB5_CRYPTO_INTERNAL;
2989 if(ret)
2990 goto fail;
2991 memcpy(p + et->confoundersize, cksum.checksum.data, cksum.checksum.length);
2992 free_Checksum(&cksum);
2993 ret = _key_schedule(context, &crypto->key);
2994 if(ret)
2995 goto fail;
2996 ret = (*et->encrypt)(context, &crypto->key, p, block_sz, 1, 0, ivec);
2997 if (ret) {
2998 memset(p, 0, block_sz);
2999 free(p);
3000 return ret;
3002 result->data = p;
3003 result->length = block_sz;
3004 return 0;
3005 fail:
3006 memset(p, 0, block_sz);
3007 free(p);
3008 return ret;
3011 static krb5_error_code
3012 encrypt_internal_special(krb5_context context,
3013 krb5_crypto crypto,
3014 int usage,
3015 const void *data,
3016 size_t len,
3017 krb5_data *result,
3018 void *ivec)
3020 struct encryption_type *et = crypto->et;
3021 size_t cksum_sz = CHECKSUMSIZE(et->checksum);
3022 size_t sz = len + cksum_sz + et->confoundersize;
3023 char *tmp, *p;
3024 krb5_error_code ret;
3026 tmp = malloc (sz);
3027 if (tmp == NULL) {
3028 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
3029 return ENOMEM;
3031 p = tmp;
3032 memset (p, 0, cksum_sz);
3033 p += cksum_sz;
3034 krb5_generate_random_block(p, et->confoundersize);
3035 p += et->confoundersize;
3036 memcpy (p, data, len);
3037 ret = (*et->encrypt)(context, &crypto->key, tmp, sz, TRUE, usage, ivec);
3038 if (ret) {
3039 memset(tmp, 0, sz);
3040 free(tmp);
3041 return ret;
3043 result->data = tmp;
3044 result->length = sz;
3045 return 0;
3048 static krb5_error_code
3049 decrypt_internal_derived(krb5_context context,
3050 krb5_crypto crypto,
3051 unsigned usage,
3052 void *data,
3053 size_t len,
3054 krb5_data *result,
3055 void *ivec)
3057 size_t checksum_sz;
3058 Checksum cksum;
3059 unsigned char *p;
3060 krb5_error_code ret;
3061 struct key_data *dkey;
3062 struct encryption_type *et = crypto->et;
3063 unsigned long l;
3065 checksum_sz = CHECKSUMSIZE(et->keyed_checksum);
3066 if (len < checksum_sz + et->confoundersize) {
3067 krb5_set_error_message(context, KRB5_BAD_MSIZE,
3068 N_("Encrypted data shorter then "
3069 "checksum + confunder", ""));
3070 return KRB5_BAD_MSIZE;
3073 if (((len - checksum_sz) % et->padsize) != 0) {
3074 krb5_clear_error_message(context);
3075 return KRB5_BAD_MSIZE;
3078 p = malloc(len);
3079 if(len != 0 && p == NULL) {
3080 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
3081 return ENOMEM;
3083 memcpy(p, data, len);
3085 len -= checksum_sz;
3087 ret = _get_derived_key(context, crypto, ENCRYPTION_USAGE(usage), &dkey);
3088 if(ret) {
3089 free(p);
3090 return ret;
3092 ret = _key_schedule(context, dkey);
3093 if(ret) {
3094 free(p);
3095 return ret;
3097 ret = (*et->encrypt)(context, dkey, p, len, 0, usage, ivec);
3098 if (ret) {
3099 free(p);
3100 return ret;
3103 cksum.checksum.data = p + len;
3104 cksum.checksum.length = checksum_sz;
3105 cksum.cksumtype = CHECKSUMTYPE(et->keyed_checksum);
3107 ret = verify_checksum(context,
3108 crypto,
3109 INTEGRITY_USAGE(usage),
3111 len,
3112 &cksum);
3113 if(ret) {
3114 free(p);
3115 return ret;
3117 l = len - et->confoundersize;
3118 memmove(p, p + et->confoundersize, l);
3119 result->data = realloc(p, l);
3120 if(result->data == NULL && l != 0) {
3121 free(p);
3122 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
3123 return ENOMEM;
3125 result->length = l;
3126 return 0;
3129 static krb5_error_code
3130 decrypt_internal(krb5_context context,
3131 krb5_crypto crypto,
3132 void *data,
3133 size_t len,
3134 krb5_data *result,
3135 void *ivec)
3137 krb5_error_code ret;
3138 unsigned char *p;
3139 Checksum cksum;
3140 size_t checksum_sz, l;
3141 struct encryption_type *et = crypto->et;
3143 if ((len % et->padsize) != 0) {
3144 krb5_clear_error_message(context);
3145 return KRB5_BAD_MSIZE;
3148 checksum_sz = CHECKSUMSIZE(et->checksum);
3149 p = malloc(len);
3150 if(len != 0 && p == NULL) {
3151 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
3152 return ENOMEM;
3154 memcpy(p, data, len);
3156 ret = _key_schedule(context, &crypto->key);
3157 if(ret) {
3158 free(p);
3159 return ret;
3161 ret = (*et->encrypt)(context, &crypto->key, p, len, 0, 0, ivec);
3162 if (ret) {
3163 free(p);
3164 return ret;
3166 ret = krb5_data_copy(&cksum.checksum, p + et->confoundersize, checksum_sz);
3167 if(ret) {
3168 free(p);
3169 return ret;
3171 memset(p + et->confoundersize, 0, checksum_sz);
3172 cksum.cksumtype = CHECKSUMTYPE(et->checksum);
3173 ret = verify_checksum(context, NULL, 0, p, len, &cksum);
3174 free_Checksum(&cksum);
3175 if(ret) {
3176 free(p);
3177 return ret;
3179 l = len - et->confoundersize - checksum_sz;
3180 memmove(p, p + et->confoundersize + checksum_sz, l);
3181 result->data = realloc(p, l);
3182 if(result->data == NULL && l != 0) {
3183 free(p);
3184 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
3185 return ENOMEM;
3187 result->length = l;
3188 return 0;
3191 static krb5_error_code
3192 decrypt_internal_special(krb5_context context,
3193 krb5_crypto crypto,
3194 int usage,
3195 void *data,
3196 size_t len,
3197 krb5_data *result,
3198 void *ivec)
3200 struct encryption_type *et = crypto->et;
3201 size_t cksum_sz = CHECKSUMSIZE(et->checksum);
3202 size_t sz = len - cksum_sz - et->confoundersize;
3203 unsigned char *p;
3204 krb5_error_code ret;
3206 if ((len % et->padsize) != 0) {
3207 krb5_clear_error_message(context);
3208 return KRB5_BAD_MSIZE;
3211 p = malloc (len);
3212 if (p == NULL) {
3213 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
3214 return ENOMEM;
3216 memcpy(p, data, len);
3218 ret = (*et->encrypt)(context, &crypto->key, p, len, FALSE, usage, ivec);
3219 if (ret) {
3220 free(p);
3221 return ret;
3224 memmove (p, p + cksum_sz + et->confoundersize, sz);
3225 result->data = realloc(p, sz);
3226 if(result->data == NULL && sz != 0) {
3227 free(p);
3228 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
3229 return ENOMEM;
3231 result->length = sz;
3232 return 0;
3235 static krb5_crypto_iov *
3236 find_iv(krb5_crypto_iov *data, int num_data, int type)
3238 int i;
3239 for (i = 0; i < num_data; i++)
3240 if (data[i].flags == type)
3241 return &data[i];
3242 return NULL;
3246 * Inline encrypt a kerberos message
3248 * @param context Kerberos context
3249 * @param crypto Kerberos crypto context
3250 * @param usage Key usage for this buffer
3251 * @param data array of buffers to process
3252 * @param num_data length of array
3253 * @param ivec initial cbc/cts vector
3255 * @return Return an error code or 0.
3256 * @ingroup krb5_crypto
3258 * Kerberos encrypted data look like this:
3260 * 1. KRB5_CRYPTO_TYPE_HEADER
3261 * 2. array [1,...] KRB5_CRYPTO_TYPE_DATA and array [0,...]
3262 * KRB5_CRYPTO_TYPE_SIGN_ONLY in any order, however the receiver
3263 * have to aware of the order. KRB5_CRYPTO_TYPE_SIGN_ONLY is
3264 * commonly used headers and trailers.
3265 * 3. KRB5_CRYPTO_TYPE_PADDING, at least on padsize long if padsize > 1
3266 * 4. KRB5_CRYPTO_TYPE_TRAILER
3269 krb5_error_code KRB5_LIB_FUNCTION
3270 krb5_encrypt_iov_ivec(krb5_context context,
3271 krb5_crypto crypto,
3272 unsigned usage,
3273 krb5_crypto_iov *data,
3274 int num_data,
3275 void *ivec)
3277 size_t headersz, trailersz, len;
3278 int i;
3279 size_t sz, block_sz, pad_sz;
3280 Checksum cksum;
3281 unsigned char *p, *q;
3282 krb5_error_code ret;
3283 struct key_data *dkey;
3284 const struct encryption_type *et = crypto->et;
3285 krb5_crypto_iov *tiv, *piv, *hiv;
3287 if (num_data < 0) {
3288 krb5_clear_error_message(context);
3289 return KRB5_CRYPTO_INTERNAL;
3292 if(!derived_crypto(context, crypto)) {
3293 krb5_clear_error_message(context);
3294 return KRB5_CRYPTO_INTERNAL;
3297 headersz = et->confoundersize;
3298 trailersz = CHECKSUMSIZE(et->keyed_checksum);
3300 for (len = 0, i = 0; i < num_data; i++) {
3301 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA)
3302 continue;
3303 len += data[i].data.length;
3306 sz = headersz + len;
3307 block_sz = (sz + et->padsize - 1) &~ (et->padsize - 1); /* pad */
3309 pad_sz = block_sz - sz;
3311 /* header */
3313 hiv = find_iv(data, num_data, KRB5_CRYPTO_TYPE_HEADER);
3314 if (hiv == NULL || hiv->data.length != headersz)
3315 return KRB5_BAD_MSIZE;
3317 krb5_generate_random_block(hiv->data.data, hiv->data.length);
3319 /* padding */
3320 piv = find_iv(data, num_data, KRB5_CRYPTO_TYPE_PADDING);
3321 /* its ok to have no TYPE_PADDING if there is no padding */
3322 if (piv == NULL && pad_sz != 0)
3323 return KRB5_BAD_MSIZE;
3324 if (piv) {
3325 if (piv->data.length < pad_sz)
3326 return KRB5_BAD_MSIZE;
3327 piv->data.length = pad_sz;
3328 if (pad_sz)
3329 memset(piv->data.data, pad_sz, pad_sz);
3330 else
3331 piv = NULL;
3334 /* trailer */
3335 tiv = find_iv(data, num_data, KRB5_CRYPTO_TYPE_TRAILER);
3336 if (tiv == NULL || tiv->data.length != trailersz)
3337 return KRB5_BAD_MSIZE;
3340 * XXX replace with EVP_Sign? at least make create_checksum an iov
3341 * function.
3342 * XXX CTS EVP is broken, can't handle multi buffers :(
3345 len = block_sz;
3346 for (i = 0; i < num_data; i++) {
3347 if (data[i].flags != KRB5_CRYPTO_TYPE_SIGN_ONLY)
3348 continue;
3349 len += data[i].data.length;
3352 p = q = malloc(len);
3354 memcpy(q, hiv->data.data, hiv->data.length);
3355 q += hiv->data.length;
3356 for (i = 0; i < num_data; i++) {
3357 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA &&
3358 data[i].flags != KRB5_CRYPTO_TYPE_SIGN_ONLY)
3359 continue;
3360 memcpy(q, data[i].data.data, data[i].data.length);
3361 q += data[i].data.length;
3363 if (piv)
3364 memset(q, 0, piv->data.length);
3366 ret = create_checksum(context,
3367 et->keyed_checksum,
3368 crypto,
3369 INTEGRITY_USAGE(usage),
3371 len,
3372 &cksum);
3373 free(p);
3374 if(ret == 0 && cksum.checksum.length != trailersz) {
3375 free_Checksum (&cksum);
3376 krb5_clear_error_message (context);
3377 ret = KRB5_CRYPTO_INTERNAL;
3379 if(ret)
3380 return ret;
3382 /* save cksum at end */
3383 memcpy(tiv->data.data, cksum.checksum.data, cksum.checksum.length);
3384 free_Checksum (&cksum);
3386 /* XXX replace with EVP_Cipher */
3387 p = q = malloc(block_sz);
3388 if(p == NULL)
3389 return ENOMEM;
3391 memcpy(q, hiv->data.data, hiv->data.length);
3392 q += hiv->data.length;
3394 for (i = 0; i < num_data; i++) {
3395 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA)
3396 continue;
3397 memcpy(q, data[i].data.data, data[i].data.length);
3398 q += data[i].data.length;
3400 if (piv)
3401 memset(q, 0, piv->data.length);
3404 ret = _get_derived_key(context, crypto, ENCRYPTION_USAGE(usage), &dkey);
3405 if(ret) {
3406 free(p);
3407 return ret;
3409 ret = _key_schedule(context, dkey);
3410 if(ret) {
3411 free(p);
3412 return ret;
3415 ret = (*et->encrypt)(context, dkey, p, block_sz, 1, usage, ivec);
3416 if (ret) {
3417 free(p);
3418 return ret;
3421 /* now copy data back to buffers */
3422 q = p;
3424 memcpy(hiv->data.data, q, hiv->data.length);
3425 q += hiv->data.length;
3427 for (i = 0; i < num_data; i++) {
3428 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA)
3429 continue;
3430 memcpy(data[i].data.data, q, data[i].data.length);
3431 q += data[i].data.length;
3433 if (piv)
3434 memcpy(piv->data.data, q, pad_sz);
3436 free(p);
3438 return ret;
3442 * Inline decrypt a Kerberos message.
3444 * @param context Kerberos context
3445 * @param crypto Kerberos crypto context
3446 * @param usage Key usage for this buffer
3447 * @param data array of buffers to process
3448 * @param num_data length of array
3449 * @param ivec initial cbc/cts vector
3451 * @return Return an error code or 0.
3452 * @ingroup krb5_crypto
3454 * 1. KRB5_CRYPTO_TYPE_HEADER
3455 * 2. one KRB5_CRYPTO_TYPE_DATA and array [0,...] of KRB5_CRYPTO_TYPE_SIGN_ONLY in
3456 * any order, however the receiver have to aware of the
3457 * order. KRB5_CRYPTO_TYPE_SIGN_ONLY is commonly used unencrypoted
3458 * protocol headers and trailers. The output data will be of same
3459 * size as the input data or shorter.
3462 krb5_error_code KRB5_LIB_FUNCTION
3463 krb5_decrypt_iov_ivec(krb5_context context,
3464 krb5_crypto crypto,
3465 unsigned usage,
3466 krb5_crypto_iov *data,
3467 unsigned int num_data,
3468 void *ivec)
3470 unsigned int i;
3471 size_t headersz, trailersz, len;
3472 Checksum cksum;
3473 unsigned char *p, *q;
3474 krb5_error_code ret;
3475 struct key_data *dkey;
3476 struct encryption_type *et = crypto->et;
3477 krb5_crypto_iov *tiv, *hiv;
3479 if (num_data < 0) {
3480 krb5_clear_error_message(context);
3481 return KRB5_CRYPTO_INTERNAL;
3484 if(!derived_crypto(context, crypto)) {
3485 krb5_clear_error_message(context);
3486 return KRB5_CRYPTO_INTERNAL;
3489 headersz = et->confoundersize;
3491 hiv = find_iv(data, num_data, KRB5_CRYPTO_TYPE_HEADER);
3492 if (hiv == NULL || hiv->data.length != headersz)
3493 return KRB5_BAD_MSIZE;
3495 /* trailer */
3496 trailersz = CHECKSUMSIZE(et->keyed_checksum);
3498 tiv = find_iv(data, num_data, KRB5_CRYPTO_TYPE_TRAILER);
3499 if (tiv->data.length != trailersz)
3500 return KRB5_BAD_MSIZE;
3502 /* Find length of data we will decrypt */
3504 len = headersz;
3505 for (i = 0; i < num_data; i++) {
3506 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA)
3507 continue;
3508 len += data[i].data.length;
3511 if ((len % et->padsize) != 0) {
3512 krb5_clear_error_message(context);
3513 return KRB5_BAD_MSIZE;
3516 /* XXX replace with EVP_Cipher */
3518 p = q = malloc(len);
3519 if (p == NULL)
3520 return ENOMEM;
3522 memcpy(q, hiv->data.data, hiv->data.length);
3523 q += hiv->data.length;
3525 for (i = 0; i < num_data; i++) {
3526 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA)
3527 continue;
3528 memcpy(q, data[i].data.data, data[i].data.length);
3529 q += data[i].data.length;
3532 ret = _get_derived_key(context, crypto, ENCRYPTION_USAGE(usage), &dkey);
3533 if(ret) {
3534 free(p);
3535 return ret;
3537 ret = _key_schedule(context, dkey);
3538 if(ret) {
3539 free(p);
3540 return ret;
3543 ret = (*et->encrypt)(context, dkey, p, len, 0, usage, ivec);
3544 if (ret) {
3545 free(p);
3546 return ret;
3549 /* copy data back to buffers */
3550 memcpy(hiv->data.data, p, hiv->data.length);
3551 q = p + hiv->data.length;
3552 for (i = 0; i < num_data; i++) {
3553 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA)
3554 continue;
3555 memcpy(data[i].data.data, q, data[i].data.length);
3556 q += data[i].data.length;
3559 free(p);
3561 /* check signature */
3562 for (i = 0; i < num_data; i++) {
3563 if (data[i].flags != KRB5_CRYPTO_TYPE_SIGN_ONLY)
3564 continue;
3565 len += data[i].data.length;
3568 p = q = malloc(len);
3569 if (p == NULL)
3570 return ENOMEM;
3572 memcpy(q, hiv->data.data, hiv->data.length);
3573 q += hiv->data.length;
3574 for (i = 0; i < num_data; i++) {
3575 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA &&
3576 data[i].flags != KRB5_CRYPTO_TYPE_SIGN_ONLY)
3577 continue;
3578 memcpy(q, data[i].data.data, data[i].data.length);
3579 q += data[i].data.length;
3582 cksum.checksum.data = tiv->data.data;
3583 cksum.checksum.length = tiv->data.length;
3584 cksum.cksumtype = CHECKSUMTYPE(et->keyed_checksum);
3586 ret = verify_checksum(context,
3587 crypto,
3588 INTEGRITY_USAGE(usage),
3590 len,
3591 &cksum);
3592 free(p);
3593 return ret;
3597 * Create a Kerberos message checksum.
3599 * @param context Kerberos context
3600 * @param crypto Kerberos crypto context
3601 * @param usage Key usage for this buffer
3602 * @param data array of buffers to process
3603 * @param num_data length of array
3604 * @param type output data
3606 * @return Return an error code or 0.
3607 * @ingroup krb5_crypto
3610 krb5_error_code KRB5_LIB_FUNCTION
3611 krb5_create_checksum_iov(krb5_context context,
3612 krb5_crypto crypto,
3613 unsigned usage,
3614 krb5_crypto_iov *data,
3615 unsigned int num_data,
3616 krb5_cksumtype *type)
3618 Checksum cksum;
3619 krb5_crypto_iov *civ;
3620 krb5_error_code ret;
3621 int i;
3622 size_t len;
3623 char *p, *q;
3625 if (num_data < 0) {
3626 krb5_clear_error_message(context);
3627 return KRB5_CRYPTO_INTERNAL;
3630 if(!derived_crypto(context, crypto)) {
3631 krb5_clear_error_message(context);
3632 return KRB5_CRYPTO_INTERNAL;
3635 civ = find_iv(data, num_data, KRB5_CRYPTO_TYPE_CHECKSUM);
3636 if (civ == NULL)
3637 return KRB5_BAD_MSIZE;
3639 len = 0;
3640 for (i = 0; i < num_data; i++) {
3641 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA &&
3642 data[i].flags != KRB5_CRYPTO_TYPE_SIGN_ONLY)
3643 continue;
3644 len += data[i].data.length;
3647 p = q = malloc(len);
3649 for (i = 0; i < num_data; i++) {
3650 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA &&
3651 data[i].flags != KRB5_CRYPTO_TYPE_SIGN_ONLY)
3652 continue;
3653 memcpy(q, data[i].data.data, data[i].data.length);
3654 q += data[i].data.length;
3657 ret = krb5_create_checksum(context, crypto, usage, 0, p, len, &cksum);
3658 free(p);
3659 if (ret)
3660 return ret;
3662 if (type)
3663 *type = cksum.cksumtype;
3665 if (cksum.checksum.length > civ->data.length) {
3666 krb5_set_error_message(context, KRB5_BAD_MSIZE,
3667 N_("Checksum larger then input buffer", ""));
3668 free_Checksum(&cksum);
3669 return KRB5_BAD_MSIZE;
3672 civ->data.length = cksum.checksum.length;
3673 memcpy(civ->data.data, cksum.checksum.data, civ->data.length);
3674 free_Checksum(&cksum);
3676 return 0;
3680 * Verify a Kerberos message checksum.
3682 * @param context Kerberos context
3683 * @param crypto Kerberos crypto context
3684 * @param usage Key usage for this buffer
3685 * @param data array of buffers to process
3686 * @param num_data length of array
3687 * @param type return checksum type if not NULL
3689 * @return Return an error code or 0.
3690 * @ingroup krb5_crypto
3693 krb5_error_code KRB5_LIB_FUNCTION
3694 krb5_verify_checksum_iov(krb5_context context,
3695 krb5_crypto crypto,
3696 unsigned usage,
3697 krb5_crypto_iov *data,
3698 unsigned int num_data,
3699 krb5_cksumtype *type)
3701 struct encryption_type *et = crypto->et;
3702 Checksum cksum;
3703 krb5_crypto_iov *civ;
3704 krb5_error_code ret;
3705 int i;
3706 size_t len;
3707 char *p, *q;
3709 if (num_data < 0) {
3710 krb5_clear_error_message(context);
3711 return KRB5_CRYPTO_INTERNAL;
3714 if(!derived_crypto(context, crypto)) {
3715 krb5_clear_error_message(context);
3716 return KRB5_CRYPTO_INTERNAL;
3719 civ = find_iv(data, num_data, KRB5_CRYPTO_TYPE_CHECKSUM);
3720 if (civ == NULL)
3721 return KRB5_BAD_MSIZE;
3723 len = 0;
3724 for (i = 0; i < num_data; i++) {
3725 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA &&
3726 data[i].flags != KRB5_CRYPTO_TYPE_SIGN_ONLY)
3727 continue;
3728 len += data[i].data.length;
3731 p = q = malloc(len);
3733 for (i = 0; i < num_data; i++) {
3734 if (data[i].flags != KRB5_CRYPTO_TYPE_DATA &&
3735 data[i].flags != KRB5_CRYPTO_TYPE_SIGN_ONLY)
3736 continue;
3737 memcpy(q, data[i].data.data, data[i].data.length);
3738 q += data[i].data.length;
3741 cksum.cksumtype = CHECKSUMTYPE(et->keyed_checksum);
3742 cksum.checksum.length = civ->data.length;
3743 cksum.checksum.data = civ->data.data;
3745 ret = krb5_verify_checksum(context, crypto, usage, p, len, &cksum);
3746 free(p);
3748 if (ret == 0 && type)
3749 *type = cksum.cksumtype;
3751 return ret;
3755 krb5_error_code KRB5_LIB_FUNCTION
3756 krb5_crypto_length(krb5_context context,
3757 krb5_crypto crypto,
3758 int type,
3759 size_t *len)
3761 if (!derived_crypto(context, crypto)) {
3762 krb5_set_error_message(context, EINVAL, "not a derived crypto");
3763 return EINVAL;
3766 switch(type) {
3767 case KRB5_CRYPTO_TYPE_EMPTY:
3768 *len = 0;
3769 return 0;
3770 case KRB5_CRYPTO_TYPE_HEADER:
3771 *len = crypto->et->blocksize;
3772 return 0;
3773 case KRB5_CRYPTO_TYPE_DATA:
3774 case KRB5_CRYPTO_TYPE_SIGN_ONLY:
3775 /* len must already been filled in */
3776 return 0;
3777 case KRB5_CRYPTO_TYPE_PADDING:
3778 if (crypto->et->padsize > 1)
3779 *len = crypto->et->padsize;
3780 else
3781 *len = 0;
3782 return 0;
3783 case KRB5_CRYPTO_TYPE_TRAILER:
3784 *len = CHECKSUMSIZE(crypto->et->keyed_checksum);
3785 return 0;
3786 case KRB5_CRYPTO_TYPE_CHECKSUM:
3787 if (crypto->et->keyed_checksum)
3788 *len = CHECKSUMSIZE(crypto->et->keyed_checksum);
3789 else
3790 *len = CHECKSUMSIZE(crypto->et->checksum);
3791 return 0;
3793 krb5_set_error_message(context, EINVAL,
3794 "%d not a supported type", type);
3795 return EINVAL;
3799 krb5_error_code KRB5_LIB_FUNCTION
3800 krb5_crypto_length_iov(krb5_context context,
3801 krb5_crypto crypto,
3802 krb5_crypto_iov *data,
3803 unsigned int num_data)
3805 krb5_error_code ret;
3806 int i;
3808 for (i = 0; i < num_data; i++) {
3809 ret = krb5_crypto_length(context, crypto,
3810 data[i].flags,
3811 &data[i].data.length);
3812 if (ret)
3813 return ret;
3815 return 0;
3819 krb5_error_code KRB5_LIB_FUNCTION
3820 krb5_encrypt_ivec(krb5_context context,
3821 krb5_crypto crypto,
3822 unsigned usage,
3823 const void *data,
3824 size_t len,
3825 krb5_data *result,
3826 void *ivec)
3828 if(derived_crypto(context, crypto))
3829 return encrypt_internal_derived(context, crypto, usage,
3830 data, len, result, ivec);
3831 else if (special_crypto(context, crypto))
3832 return encrypt_internal_special (context, crypto, usage,
3833 data, len, result, ivec);
3834 else
3835 return encrypt_internal(context, crypto, data, len, result, ivec);
3838 krb5_error_code KRB5_LIB_FUNCTION
3839 krb5_encrypt(krb5_context context,
3840 krb5_crypto crypto,
3841 unsigned usage,
3842 const void *data,
3843 size_t len,
3844 krb5_data *result)
3846 return krb5_encrypt_ivec(context, crypto, usage, data, len, result, NULL);
3849 krb5_error_code KRB5_LIB_FUNCTION
3850 krb5_encrypt_EncryptedData(krb5_context context,
3851 krb5_crypto crypto,
3852 unsigned usage,
3853 void *data,
3854 size_t len,
3855 int kvno,
3856 EncryptedData *result)
3858 result->etype = CRYPTO_ETYPE(crypto);
3859 if(kvno){
3860 ALLOC(result->kvno, 1);
3861 *result->kvno = kvno;
3862 }else
3863 result->kvno = NULL;
3864 return krb5_encrypt(context, crypto, usage, data, len, &result->cipher);
3867 krb5_error_code KRB5_LIB_FUNCTION
3868 krb5_decrypt_ivec(krb5_context context,
3869 krb5_crypto crypto,
3870 unsigned usage,
3871 void *data,
3872 size_t len,
3873 krb5_data *result,
3874 void *ivec)
3876 if(derived_crypto(context, crypto))
3877 return decrypt_internal_derived(context, crypto, usage,
3878 data, len, result, ivec);
3879 else if (special_crypto (context, crypto))
3880 return decrypt_internal_special(context, crypto, usage,
3881 data, len, result, ivec);
3882 else
3883 return decrypt_internal(context, crypto, data, len, result, ivec);
3886 krb5_error_code KRB5_LIB_FUNCTION
3887 krb5_decrypt(krb5_context context,
3888 krb5_crypto crypto,
3889 unsigned usage,
3890 void *data,
3891 size_t len,
3892 krb5_data *result)
3894 return krb5_decrypt_ivec (context, crypto, usage, data, len, result,
3895 NULL);
3898 krb5_error_code KRB5_LIB_FUNCTION
3899 krb5_decrypt_EncryptedData(krb5_context context,
3900 krb5_crypto crypto,
3901 unsigned usage,
3902 const EncryptedData *e,
3903 krb5_data *result)
3905 return krb5_decrypt(context, crypto, usage,
3906 e->cipher.data, e->cipher.length, result);
3909 /************************************************************
3911 ************************************************************/
3913 #define ENTROPY_NEEDED 128
3915 static int
3916 seed_something(void)
3918 char buf[1024], seedfile[256];
3920 /* If there is a seed file, load it. But such a file cannot be trusted,
3921 so use 0 for the entropy estimate */
3922 if (RAND_file_name(seedfile, sizeof(seedfile))) {
3923 int fd;
3924 fd = open(seedfile, O_RDONLY | O_BINARY | O_CLOEXEC);
3925 if (fd >= 0) {
3926 ssize_t ret;
3927 rk_cloexec(fd);
3928 ret = read(fd, buf, sizeof(buf));
3929 if (ret > 0)
3930 RAND_add(buf, ret, 0.0);
3931 close(fd);
3932 } else
3933 seedfile[0] = '\0';
3934 } else
3935 seedfile[0] = '\0';
3937 /* Calling RAND_status() will try to use /dev/urandom if it exists so
3938 we do not have to deal with it. */
3939 if (RAND_status() != 1) {
3940 krb5_context context;
3941 const char *p;
3943 /* Try using egd */
3944 if (!krb5_init_context(&context)) {
3945 p = krb5_config_get_string(context, NULL, "libdefaults",
3946 "egd_socket", NULL);
3947 if (p != NULL)
3948 RAND_egd_bytes(p, ENTROPY_NEEDED);
3949 krb5_free_context(context);
3953 if (RAND_status() == 1) {
3954 /* Update the seed file */
3955 if (seedfile[0])
3956 RAND_write_file(seedfile);
3958 return 0;
3959 } else
3960 return -1;
3963 void KRB5_LIB_FUNCTION
3964 krb5_generate_random_block(void *buf, size_t len)
3966 static int rng_initialized = 0;
3968 HEIMDAL_MUTEX_lock(&crypto_mutex);
3969 if (!rng_initialized) {
3970 if (seed_something())
3971 krb5_abortx(NULL, "Fatal: could not seed the "
3972 "random number generator");
3974 rng_initialized = 1;
3976 HEIMDAL_MUTEX_unlock(&crypto_mutex);
3977 if (RAND_bytes(buf, len) != 1)
3978 krb5_abortx(NULL, "Failed to generate random block");
3981 static krb5_error_code
3982 derive_key(krb5_context context,
3983 struct encryption_type *et,
3984 struct key_data *key,
3985 const void *constant,
3986 size_t len)
3988 unsigned char *k = NULL;
3989 unsigned int nblocks = 0, i;
3990 krb5_error_code ret = 0;
3991 struct key_type *kt = et->keytype;
3993 ret = _key_schedule(context, key);
3994 if(ret)
3995 return ret;
3996 if(et->blocksize * 8 < kt->bits || len != et->blocksize) {
3997 nblocks = (kt->bits + et->blocksize * 8 - 1) / (et->blocksize * 8);
3998 k = malloc(nblocks * et->blocksize);
3999 if(k == NULL) {
4000 ret = ENOMEM;
4001 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
4002 goto out;
4004 ret = _krb5_n_fold(constant, len, k, et->blocksize);
4005 if (ret) {
4006 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
4007 goto out;
4010 for(i = 0; i < nblocks; i++) {
4011 if(i > 0)
4012 memcpy(k + i * et->blocksize,
4013 k + (i - 1) * et->blocksize,
4014 et->blocksize);
4015 (*et->encrypt)(context, key, k + i * et->blocksize, et->blocksize,
4016 1, 0, NULL);
4018 } else {
4019 /* this case is probably broken, but won't be run anyway */
4020 void *c = malloc(len);
4021 size_t res_len = (kt->bits + 7) / 8;
4023 if(len != 0 && c == NULL) {
4024 ret = ENOMEM;
4025 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
4026 goto out;
4028 memcpy(c, constant, len);
4029 (*et->encrypt)(context, key, c, len, 1, 0, NULL);
4030 k = malloc(res_len);
4031 if(res_len != 0 && k == NULL) {
4032 free(c);
4033 ret = ENOMEM;
4034 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
4035 goto out;
4037 ret = _krb5_n_fold(c, len, k, res_len);
4038 free(c);
4039 if (ret) {
4040 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
4041 goto out;
4045 /* XXX keytype dependent post-processing */
4046 switch(kt->type) {
4047 case KEYTYPE_DES3:
4048 DES3_random_to_key(context, key->key, k, nblocks * et->blocksize);
4049 break;
4050 case KEYTYPE_AES128:
4051 case KEYTYPE_AES256:
4052 memcpy(key->key->keyvalue.data, k, key->key->keyvalue.length);
4053 break;
4054 default:
4055 ret = KRB5_CRYPTO_INTERNAL;
4056 krb5_set_error_message(context, ret,
4057 N_("derive_key() called with unknown keytype (%u)", ""),
4058 kt->type);
4059 break;
4061 out:
4062 if (key->schedule) {
4063 free_key_schedule(context, key, et);
4064 key->schedule = NULL;
4066 if (k) {
4067 memset(k, 0, nblocks * et->blocksize);
4068 free(k);
4070 return ret;
4073 static struct key_data *
4074 _new_derived_key(krb5_crypto crypto, unsigned usage)
4076 struct key_usage *d = crypto->key_usage;
4077 d = realloc(d, (crypto->num_key_usage + 1) * sizeof(*d));
4078 if(d == NULL)
4079 return NULL;
4080 crypto->key_usage = d;
4081 d += crypto->num_key_usage++;
4082 memset(d, 0, sizeof(*d));
4083 d->usage = usage;
4084 return &d->key;
4087 krb5_error_code KRB5_LIB_FUNCTION
4088 krb5_derive_key(krb5_context context,
4089 const krb5_keyblock *key,
4090 krb5_enctype etype,
4091 const void *constant,
4092 size_t constant_len,
4093 krb5_keyblock **derived_key)
4095 krb5_error_code ret;
4096 struct encryption_type *et;
4097 struct key_data d;
4099 *derived_key = NULL;
4101 et = _find_enctype (etype);
4102 if (et == NULL) {
4103 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
4104 N_("encryption type %d not supported", ""),
4105 etype);
4106 return KRB5_PROG_ETYPE_NOSUPP;
4109 ret = krb5_copy_keyblock(context, key, &d.key);
4110 if (ret)
4111 return ret;
4113 d.schedule = NULL;
4114 ret = derive_key(context, et, &d, constant, constant_len);
4115 if (ret == 0)
4116 ret = krb5_copy_keyblock(context, d.key, derived_key);
4117 free_key_data(context, &d, et);
4118 return ret;
4121 static krb5_error_code
4122 _get_derived_key(krb5_context context,
4123 krb5_crypto crypto,
4124 unsigned usage,
4125 struct key_data **key)
4127 int i;
4128 struct key_data *d;
4129 unsigned char constant[5];
4131 for(i = 0; i < crypto->num_key_usage; i++)
4132 if(crypto->key_usage[i].usage == usage) {
4133 *key = &crypto->key_usage[i].key;
4134 return 0;
4136 d = _new_derived_key(crypto, usage);
4137 if(d == NULL) {
4138 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
4139 return ENOMEM;
4141 krb5_copy_keyblock(context, crypto->key.key, &d->key);
4142 _krb5_put_int(constant, usage, 5);
4143 derive_key(context, crypto->et, d, constant, sizeof(constant));
4144 *key = d;
4145 return 0;
4149 * Create a crypto context used for all encryption and signature
4150 * operation. The encryption type to use is taken from the key, but
4151 * can be overridden with the enctype parameter. This can be useful
4152 * for encryptions types which is compatiable (DES for example).
4154 * To free the crypto context, use krb5_crypto_destroy().
4156 * @param context Kerberos context
4157 * @param key the key block information with all key data
4158 * @param etype the encryption type
4159 * @param crypto the resulting crypto context
4161 * @return Return an error code or 0.
4163 * @ingroup krb5_crypto
4166 krb5_error_code KRB5_LIB_FUNCTION
4167 krb5_crypto_init(krb5_context context,
4168 const krb5_keyblock *key,
4169 krb5_enctype etype,
4170 krb5_crypto *crypto)
4172 krb5_error_code ret;
4173 ALLOC(*crypto, 1);
4174 if(*crypto == NULL) {
4175 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
4176 return ENOMEM;
4178 if(etype == ETYPE_NULL)
4179 etype = key->keytype;
4180 (*crypto)->et = _find_enctype(etype);
4181 if((*crypto)->et == NULL || ((*crypto)->et->flags & F_DISABLED)) {
4182 free(*crypto);
4183 *crypto = NULL;
4184 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
4185 N_("encryption type %d not supported", ""),
4186 etype);
4187 return KRB5_PROG_ETYPE_NOSUPP;
4189 if((*crypto)->et->keytype->size != key->keyvalue.length) {
4190 free(*crypto);
4191 *crypto = NULL;
4192 krb5_set_error_message (context, KRB5_BAD_KEYSIZE,
4193 "encryption key has bad length");
4194 return KRB5_BAD_KEYSIZE;
4196 ret = krb5_copy_keyblock(context, key, &(*crypto)->key.key);
4197 if(ret) {
4198 free(*crypto);
4199 *crypto = NULL;
4200 return ret;
4202 (*crypto)->key.schedule = NULL;
4203 (*crypto)->num_key_usage = 0;
4204 (*crypto)->key_usage = NULL;
4205 return 0;
4208 static void
4209 free_key_schedule(krb5_context context,
4210 struct key_data *key,
4211 struct encryption_type *et)
4213 if (et->keytype->cleanup)
4214 (*et->keytype->cleanup)(context, key);
4215 memset(key->schedule->data, 0, key->schedule->length);
4216 krb5_free_data(context, key->schedule);
4219 static void
4220 free_key_data(krb5_context context, struct key_data *key,
4221 struct encryption_type *et)
4223 krb5_free_keyblock(context, key->key);
4224 if(key->schedule) {
4225 free_key_schedule(context, key, et);
4226 key->schedule = NULL;
4230 static void
4231 free_key_usage(krb5_context context, struct key_usage *ku,
4232 struct encryption_type *et)
4234 free_key_data(context, &ku->key, et);
4238 * Free a crypto context created by krb5_crypto_init().
4240 * @param context Kerberos context
4241 * @param crypto crypto context to free
4243 * @return Return an error code or 0.
4245 * @ingroup krb5_crypto
4248 krb5_error_code KRB5_LIB_FUNCTION
4249 krb5_crypto_destroy(krb5_context context,
4250 krb5_crypto crypto)
4252 int i;
4254 for(i = 0; i < crypto->num_key_usage; i++)
4255 free_key_usage(context, &crypto->key_usage[i], crypto->et);
4256 free(crypto->key_usage);
4257 free_key_data(context, &crypto->key, crypto->et);
4258 free (crypto);
4259 return 0;
4263 * Return the blocksize used algorithm referenced by the crypto context
4265 * @param context Kerberos context
4266 * @param crypto crypto context to query
4267 * @param blocksize the resulting blocksize
4269 * @return Return an error code or 0.
4271 * @ingroup krb5_crypto
4274 krb5_error_code KRB5_LIB_FUNCTION
4275 krb5_crypto_getblocksize(krb5_context context,
4276 krb5_crypto crypto,
4277 size_t *blocksize)
4279 *blocksize = crypto->et->blocksize;
4280 return 0;
4284 * Return the encryption type used by the crypto context
4286 * @param context Kerberos context
4287 * @param crypto crypto context to query
4288 * @param enctype the resulting encryption type
4290 * @return Return an error code or 0.
4292 * @ingroup krb5_crypto
4295 krb5_error_code KRB5_LIB_FUNCTION
4296 krb5_crypto_getenctype(krb5_context context,
4297 krb5_crypto crypto,
4298 krb5_enctype *enctype)
4300 *enctype = crypto->et->type;
4301 return 0;
4305 * Return the padding size used by the crypto context
4307 * @param context Kerberos context
4308 * @param crypto crypto context to query
4309 * @param padsize the return padding size
4311 * @return Return an error code or 0.
4313 * @ingroup krb5_crypto
4316 krb5_error_code KRB5_LIB_FUNCTION
4317 krb5_crypto_getpadsize(krb5_context context,
4318 krb5_crypto crypto,
4319 size_t *padsize)
4321 *padsize = crypto->et->padsize;
4322 return 0;
4326 * Return the confounder size used by the crypto context
4328 * @param context Kerberos context
4329 * @param crypto crypto context to query
4330 * @param confoundersize the returned confounder size
4332 * @return Return an error code or 0.
4334 * @ingroup krb5_crypto
4337 krb5_error_code KRB5_LIB_FUNCTION
4338 krb5_crypto_getconfoundersize(krb5_context context,
4339 krb5_crypto crypto,
4340 size_t *confoundersize)
4342 *confoundersize = crypto->et->confoundersize;
4343 return 0;
4348 * Disable encryption type
4350 * @param context Kerberos 5 context
4351 * @param enctype encryption type to disable
4353 * @return Return an error code or 0.
4355 * @ingroup krb5_crypto
4358 krb5_error_code KRB5_LIB_FUNCTION
4359 krb5_enctype_disable(krb5_context context,
4360 krb5_enctype enctype)
4362 struct encryption_type *et = _find_enctype(enctype);
4363 if(et == NULL) {
4364 if (context)
4365 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
4366 N_("encryption type %d not supported", ""),
4367 enctype);
4368 return KRB5_PROG_ETYPE_NOSUPP;
4370 et->flags |= F_DISABLED;
4371 return 0;
4375 * Enable encryption type
4377 * @param context Kerberos 5 context
4378 * @param enctype encryption type to enable
4380 * @return Return an error code or 0.
4382 * @ingroup krb5_crypto
4385 krb5_error_code KRB5_LIB_FUNCTION
4386 krb5_enctype_enable(krb5_context context,
4387 krb5_enctype enctype)
4389 struct encryption_type *et = _find_enctype(enctype);
4390 if(et == NULL) {
4391 if (context)
4392 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
4393 N_("encryption type %d not supported", ""),
4394 enctype);
4395 return KRB5_PROG_ETYPE_NOSUPP;
4397 et->flags &= ~F_DISABLED;
4398 return 0;
4402 * Enable or disable all weak encryption types
4404 * @param context Kerberos 5 context
4405 * @param enable true to enable, false to disable
4407 * @return Return an error code or 0.
4409 * @ingroup krb5_crypto
4412 krb5_error_code KRB5_LIB_FUNCTION
4413 krb5_allow_weak_crypto(krb5_context context,
4414 krb5_boolean enable)
4416 int i;
4418 for(i = 0; i < num_etypes; i++)
4419 if(etypes[i]->flags & F_WEAK) {
4420 if(enable)
4421 etypes[i]->flags &= ~F_DISABLED;
4422 else
4423 etypes[i]->flags |= F_DISABLED;
4425 return 0;
4429 krb5_error_code KRB5_LIB_FUNCTION
4430 krb5_string_to_key_derived(krb5_context context,
4431 const void *str,
4432 size_t len,
4433 krb5_enctype etype,
4434 krb5_keyblock *key)
4436 struct encryption_type *et = _find_enctype(etype);
4437 krb5_error_code ret;
4438 struct key_data kd;
4439 size_t keylen;
4440 u_char *tmp;
4442 if(et == NULL) {
4443 krb5_set_error_message (context, KRB5_PROG_ETYPE_NOSUPP,
4444 N_("encryption type %d not supported", ""),
4445 etype);
4446 return KRB5_PROG_ETYPE_NOSUPP;
4448 keylen = et->keytype->bits / 8;
4450 ALLOC(kd.key, 1);
4451 if(kd.key == NULL) {
4452 krb5_set_error_message (context, ENOMEM,
4453 N_("malloc: out of memory", ""));
4454 return ENOMEM;
4456 ret = krb5_data_alloc(&kd.key->keyvalue, et->keytype->size);
4457 if(ret) {
4458 free(kd.key);
4459 return ret;
4461 kd.key->keytype = etype;
4462 tmp = malloc (keylen);
4463 if(tmp == NULL) {
4464 krb5_free_keyblock(context, kd.key);
4465 krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
4466 return ENOMEM;
4468 ret = _krb5_n_fold(str, len, tmp, keylen);
4469 if (ret) {
4470 free(tmp);
4471 krb5_set_error_message (context, ENOMEM, N_("malloc: out of memory", ""));
4472 return ret;
4474 kd.schedule = NULL;
4475 DES3_random_to_key(context, kd.key, tmp, keylen);
4476 memset(tmp, 0, keylen);
4477 free(tmp);
4478 ret = derive_key(context,
4480 &kd,
4481 "kerberos", /* XXX well known constant */
4482 strlen("kerberos"));
4483 if (ret) {
4484 free_key_data(context, &kd, et);
4485 return ret;
4487 ret = krb5_copy_keyblock_contents(context, kd.key, key);
4488 free_key_data(context, &kd, et);
4489 return ret;
4492 static size_t
4493 wrapped_length (krb5_context context,
4494 krb5_crypto crypto,
4495 size_t data_len)
4497 struct encryption_type *et = crypto->et;
4498 size_t padsize = et->padsize;
4499 size_t checksumsize = CHECKSUMSIZE(et->checksum);
4500 size_t res;
4502 res = et->confoundersize + checksumsize + data_len;
4503 res = (res + padsize - 1) / padsize * padsize;
4504 return res;
4507 static size_t
4508 wrapped_length_dervied (krb5_context context,
4509 krb5_crypto crypto,
4510 size_t data_len)
4512 struct encryption_type *et = crypto->et;
4513 size_t padsize = et->padsize;
4514 size_t res;
4516 res = et->confoundersize + data_len;
4517 res = (res + padsize - 1) / padsize * padsize;
4518 if (et->keyed_checksum)
4519 res += et->keyed_checksum->checksumsize;
4520 else
4521 res += et->checksum->checksumsize;
4522 return res;
4526 * Return the size of an encrypted packet of length `data_len'
4529 size_t
4530 krb5_get_wrapped_length (krb5_context context,
4531 krb5_crypto crypto,
4532 size_t data_len)
4534 if (derived_crypto (context, crypto))
4535 return wrapped_length_dervied (context, crypto, data_len);
4536 else
4537 return wrapped_length (context, crypto, data_len);
4541 * Return the size of an encrypted packet of length `data_len'
4544 static size_t
4545 crypto_overhead (krb5_context context,
4546 krb5_crypto crypto)
4548 struct encryption_type *et = crypto->et;
4549 size_t res;
4551 res = CHECKSUMSIZE(et->checksum);
4552 res += et->confoundersize;
4553 if (et->padsize > 1)
4554 res += et->padsize;
4555 return res;
4558 static size_t
4559 crypto_overhead_dervied (krb5_context context,
4560 krb5_crypto crypto)
4562 struct encryption_type *et = crypto->et;
4563 size_t res;
4565 if (et->keyed_checksum)
4566 res = CHECKSUMSIZE(et->keyed_checksum);
4567 else
4568 res = CHECKSUMSIZE(et->checksum);
4569 res += et->confoundersize;
4570 if (et->padsize > 1)
4571 res += et->padsize;
4572 return res;
4575 size_t
4576 krb5_crypto_overhead (krb5_context context, krb5_crypto crypto)
4578 if (derived_crypto (context, crypto))
4579 return crypto_overhead_dervied (context, crypto);
4580 else
4581 return crypto_overhead (context, crypto);
4585 * Converts the random bytestring to a protocol key according to
4586 * Kerberos crypto frame work. It may be assumed that all the bits of
4587 * the input string are equally random, even though the entropy
4588 * present in the random source may be limited.
4590 * @param context Kerberos 5 context
4591 * @param type the enctype resulting key will be of
4592 * @param data input random data to convert to a key
4593 * @param size size of input random data, at least krb5_enctype_keysize() long
4594 * @param key key, output key, free with krb5_free_keyblock_contents()
4596 * @return Return an error code or 0.
4598 * @ingroup krb5_crypto
4601 krb5_error_code KRB5_LIB_FUNCTION
4602 krb5_random_to_key(krb5_context context,
4603 krb5_enctype type,
4604 const void *data,
4605 size_t size,
4606 krb5_keyblock *key)
4608 krb5_error_code ret;
4609 struct encryption_type *et = _find_enctype(type);
4610 if(et == NULL) {
4611 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
4612 N_("encryption type %d not supported", ""),
4613 type);
4614 return KRB5_PROG_ETYPE_NOSUPP;
4616 if ((et->keytype->bits + 7) / 8 > size) {
4617 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
4618 N_("encryption key %s needs %d bytes "
4619 "of random to make an encryption key "
4620 "out of it", ""),
4621 et->name, (int)et->keytype->size);
4622 return KRB5_PROG_ETYPE_NOSUPP;
4624 ret = krb5_data_alloc(&key->keyvalue, et->keytype->size);
4625 if(ret)
4626 return ret;
4627 key->keytype = type;
4628 if (et->keytype->random_to_key)
4629 (*et->keytype->random_to_key)(context, key, data, size);
4630 else
4631 memcpy(key->keyvalue.data, data, et->keytype->size);
4633 return 0;
4636 krb5_error_code
4637 _krb5_pk_octetstring2key(krb5_context context,
4638 krb5_enctype type,
4639 const void *dhdata,
4640 size_t dhsize,
4641 const heim_octet_string *c_n,
4642 const heim_octet_string *k_n,
4643 krb5_keyblock *key)
4645 struct encryption_type *et = _find_enctype(type);
4646 krb5_error_code ret;
4647 size_t keylen, offset;
4648 void *keydata;
4649 unsigned char counter;
4650 unsigned char shaoutput[SHA_DIGEST_LENGTH];
4651 EVP_MD_CTX *m;
4653 if(et == NULL) {
4654 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
4655 N_("encryption type %d not supported", ""),
4656 type);
4657 return KRB5_PROG_ETYPE_NOSUPP;
4659 keylen = (et->keytype->bits + 7) / 8;
4661 keydata = malloc(keylen);
4662 if (keydata == NULL) {
4663 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
4664 return ENOMEM;
4667 m = EVP_MD_CTX_create();
4668 if (m == NULL) {
4669 free(keydata);
4670 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
4671 return ENOMEM;
4674 counter = 0;
4675 offset = 0;
4676 do {
4678 EVP_DigestInit_ex(m, EVP_sha1(), NULL);
4679 EVP_DigestUpdate(m, &counter, 1);
4680 EVP_DigestUpdate(m, dhdata, dhsize);
4682 if (c_n)
4683 EVP_DigestUpdate(m, c_n->data, c_n->length);
4684 if (k_n)
4685 EVP_DigestUpdate(m, k_n->data, k_n->length);
4687 EVP_DigestFinal_ex(m, shaoutput, NULL);
4689 memcpy((unsigned char *)keydata + offset,
4690 shaoutput,
4691 min(keylen - offset, sizeof(shaoutput)));
4693 offset += sizeof(shaoutput);
4694 counter++;
4695 } while(offset < keylen);
4696 memset(shaoutput, 0, sizeof(shaoutput));
4698 EVP_MD_CTX_destroy(m);
4700 ret = krb5_random_to_key(context, type, keydata, keylen, key);
4701 memset(keydata, 0, sizeof(keylen));
4702 free(keydata);
4703 return ret;
4706 static krb5_error_code
4707 encode_uvinfo(krb5_context context, krb5_const_principal p, krb5_data *data)
4709 KRB5PrincipalName pn;
4710 krb5_error_code ret;
4711 size_t size;
4713 pn.principalName = p->name;
4714 pn.realm = p->realm;
4716 ASN1_MALLOC_ENCODE(KRB5PrincipalName, data->data, data->length,
4717 &pn, &size, ret);
4718 if (ret) {
4719 krb5_data_zero(data);
4720 krb5_set_error_message(context, ret,
4721 N_("Failed to encode KRB5PrincipalName", ""));
4722 return ret;
4724 if (data->length != size)
4725 krb5_abortx(context, "asn1 compiler internal error");
4726 return 0;
4729 static krb5_error_code
4730 encode_otherinfo(krb5_context context,
4731 const AlgorithmIdentifier *ai,
4732 krb5_const_principal client,
4733 krb5_const_principal server,
4734 krb5_enctype enctype,
4735 const krb5_data *as_req,
4736 const krb5_data *pk_as_rep,
4737 const Ticket *ticket,
4738 krb5_data *other)
4740 PkinitSP80056AOtherInfo otherinfo;
4741 PkinitSuppPubInfo pubinfo;
4742 krb5_error_code ret;
4743 krb5_data pub;
4744 size_t size;
4746 krb5_data_zero(other);
4747 memset(&otherinfo, 0, sizeof(otherinfo));
4748 memset(&pubinfo, 0, sizeof(pubinfo));
4750 pubinfo.enctype = enctype;
4751 pubinfo.as_REQ = *as_req;
4752 pubinfo.pk_as_rep = *pk_as_rep;
4753 pubinfo.ticket = *ticket;
4754 ASN1_MALLOC_ENCODE(PkinitSuppPubInfo, pub.data, pub.length,
4755 &pubinfo, &size, ret);
4756 if (ret) {
4757 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
4758 return ret;
4760 if (pub.length != size)
4761 krb5_abortx(context, "asn1 compiler internal error");
4763 ret = encode_uvinfo(context, client, &otherinfo.partyUInfo);
4764 if (ret) {
4765 free(pub.data);
4766 return ret;
4768 ret = encode_uvinfo(context, server, &otherinfo.partyVInfo);
4769 if (ret) {
4770 free(otherinfo.partyUInfo.data);
4771 free(pub.data);
4772 return ret;
4775 otherinfo.algorithmID = *ai;
4776 otherinfo.suppPubInfo = &pub;
4778 ASN1_MALLOC_ENCODE(PkinitSP80056AOtherInfo, other->data, other->length,
4779 &otherinfo, &size, ret);
4780 free(otherinfo.partyUInfo.data);
4781 free(otherinfo.partyVInfo.data);
4782 free(pub.data);
4783 if (ret) {
4784 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
4785 return ret;
4787 if (other->length != size)
4788 krb5_abortx(context, "asn1 compiler internal error");
4790 return 0;
4793 krb5_error_code
4794 _krb5_pk_kdf(krb5_context context,
4795 const struct AlgorithmIdentifier *ai,
4796 const void *dhdata,
4797 size_t dhsize,
4798 krb5_const_principal client,
4799 krb5_const_principal server,
4800 krb5_enctype enctype,
4801 const krb5_data *as_req,
4802 const krb5_data *pk_as_rep,
4803 const Ticket *ticket,
4804 krb5_keyblock *key)
4806 struct encryption_type *et;
4807 krb5_error_code ret;
4808 krb5_data other;
4809 size_t keylen, offset;
4810 uint32_t counter;
4811 unsigned char *keydata;
4812 unsigned char shaoutput[SHA_DIGEST_LENGTH];
4813 EVP_MD_CTX *m;
4815 if (der_heim_oid_cmp(&asn1_oid_id_pkinit_kdf_ah_sha1, &ai->algorithm) != 0) {
4816 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
4817 N_("KDF not supported", ""));
4818 return KRB5_PROG_ETYPE_NOSUPP;
4820 if (ai->parameters != NULL &&
4821 (ai->parameters->length != 2 ||
4822 memcmp(ai->parameters->data, "\x05\x00", 2) != 0))
4824 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
4825 N_("kdf params not NULL or the NULL-type",
4826 ""));
4827 return KRB5_PROG_ETYPE_NOSUPP;
4830 et = _find_enctype(enctype);
4831 if(et == NULL) {
4832 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
4833 N_("encryption type %d not supported", ""),
4834 enctype);
4835 return KRB5_PROG_ETYPE_NOSUPP;
4837 keylen = (et->keytype->bits + 7) / 8;
4839 keydata = malloc(keylen);
4840 if (keydata == NULL) {
4841 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
4842 return ENOMEM;
4845 ret = encode_otherinfo(context, ai, client, server,
4846 enctype, as_req, pk_as_rep, ticket, &other);
4847 if (ret) {
4848 free(keydata);
4849 return ret;
4852 m = EVP_MD_CTX_create();
4853 if (m == NULL) {
4854 free(keydata);
4855 free(other.data);
4856 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
4857 return ENOMEM;
4860 offset = 0;
4861 counter = 1;
4862 do {
4863 unsigned char cdata[4];
4865 EVP_DigestInit_ex(m, EVP_sha1(), NULL);
4866 _krb5_put_int(cdata, counter, 4);
4867 EVP_DigestUpdate(m, cdata, 4);
4868 EVP_DigestUpdate(m, dhdata, dhsize);
4869 EVP_DigestUpdate(m, other.data, other.length);
4871 EVP_DigestFinal_ex(m, shaoutput, NULL);
4873 memcpy((unsigned char *)keydata + offset,
4874 shaoutput,
4875 min(keylen - offset, sizeof(shaoutput)));
4877 offset += sizeof(shaoutput);
4878 counter++;
4879 } while(offset < keylen);
4880 memset(shaoutput, 0, sizeof(shaoutput));
4882 EVP_MD_CTX_destroy(m);
4883 free(other.data);
4885 ret = krb5_random_to_key(context, enctype, keydata, keylen, key);
4886 memset(keydata, 0, sizeof(keylen));
4887 free(keydata);
4889 return ret;
4893 krb5_error_code KRB5_LIB_FUNCTION
4894 krb5_crypto_prf_length(krb5_context context,
4895 krb5_enctype type,
4896 size_t *length)
4898 struct encryption_type *et = _find_enctype(type);
4900 if(et == NULL || et->prf_length == 0) {
4901 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
4902 N_("encryption type %d not supported", ""),
4903 type);
4904 return KRB5_PROG_ETYPE_NOSUPP;
4907 *length = et->prf_length;
4908 return 0;
4911 krb5_error_code KRB5_LIB_FUNCTION
4912 krb5_crypto_prf(krb5_context context,
4913 const krb5_crypto crypto,
4914 const krb5_data *input,
4915 krb5_data *output)
4917 struct encryption_type *et = crypto->et;
4919 krb5_data_zero(output);
4921 if(et->prf == NULL) {
4922 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
4923 "kerberos prf for %s not supported",
4924 et->name);
4925 return KRB5_PROG_ETYPE_NOSUPP;
4928 return (*et->prf)(context, crypto, input, output);
4931 static krb5_error_code
4932 krb5_crypto_prfplus(krb5_context context,
4933 const krb5_crypto crypto,
4934 const krb5_data *input,
4935 size_t length,
4936 krb5_data *output)
4938 krb5_error_code ret;
4939 krb5_data input2;
4940 unsigned char i = 1;
4941 unsigned char *p;
4943 krb5_data_zero(&input2);
4944 krb5_data_zero(output);
4946 krb5_clear_error_message(context);
4948 ret = krb5_data_alloc(output, length);
4949 if (ret) goto out;
4950 ret = krb5_data_alloc(&input2, input->length + 1);
4951 if (ret) goto out;
4953 krb5_clear_error_message(context);
4955 memcpy(((unsigned char *)input2.data) + 1, input->data, input->length);
4957 p = output->data;
4959 while (length) {
4960 krb5_data block;
4962 ((unsigned char *)input2.data)[0] = i++;
4964 ret = krb5_crypto_prf(context, crypto, &input2, &block);
4965 if (ret)
4966 goto out;
4968 if (block.length < length) {
4969 memcpy(p, block.data, block.length);
4970 length -= block.length;
4971 } else {
4972 memcpy(p, block.data, length);
4973 length = 0;
4975 p += block.length;
4976 krb5_data_free(&block);
4979 out:
4980 krb5_data_free(&input2);
4981 if (ret)
4982 krb5_data_free(output);
4983 return 0;
4987 * The FX-CF2 key derivation function, used in FAST and preauth framework.
4989 * @param context Kerberos 5 context
4990 * @param crypto1 first key to combine
4991 * @param crypto2 second key to combine
4992 * @param pepper1 factor to combine with first key to garante uniqueness
4993 * @param pepper2 factor to combine with second key to garante uniqueness
4994 * @param enctype the encryption type of the resulting key
4995 * @param res allocated key, free with krb5_free_keyblock_contents()
4997 * @return Return an error code or 0.
4999 * @ingroup krb5_crypto
5002 krb5_error_code KRB5_LIB_FUNCTION
5003 krb5_crypto_fx_cf2(krb5_context context,
5004 const krb5_crypto crypto1,
5005 const krb5_crypto crypto2,
5006 krb5_data *pepper1,
5007 krb5_data *pepper2,
5008 krb5_enctype enctype,
5009 krb5_keyblock *res)
5011 krb5_error_code ret;
5012 krb5_data os1, os2;
5013 size_t i, keysize;
5015 memset(res, 0, sizeof(*res));
5017 ret = krb5_enctype_keysize(context, enctype, &keysize);
5018 if (ret)
5019 return ret;
5021 ret = krb5_data_alloc(&res->keyvalue, keysize);
5022 if (ret)
5023 goto out;
5024 ret = krb5_crypto_prfplus(context, crypto1, pepper1, keysize, &os1);
5025 if (ret)
5026 goto out;
5027 ret = krb5_crypto_prfplus(context, crypto2, pepper2, keysize, &os2);
5028 if (ret)
5029 goto out;
5031 res->keytype = enctype;
5033 unsigned char *p1 = os1.data, *p2 = os2.data, *p3 = res->keyvalue.data;
5034 for (i = 0; i < keysize; i++)
5035 p3[i] = p1[i] ^ p2[i];
5037 out:
5038 if (ret)
5039 krb5_data_free(&res->keyvalue);
5040 krb5_data_free(&os1);
5041 krb5_data_free(&os2);
5043 return ret;
5048 #ifndef HEIMDAL_SMALLER
5051 * Deprecated: keytypes doesn't exists, they are really enctypes.
5053 * @ingroup krb5_deprecated
5056 krb5_error_code KRB5_LIB_FUNCTION
5057 krb5_keytype_to_enctypes (krb5_context context,
5058 krb5_keytype keytype,
5059 unsigned *len,
5060 krb5_enctype **val)
5061 KRB5_DEPRECATED
5063 int i;
5064 unsigned n = 0;
5065 krb5_enctype *ret;
5067 for (i = num_etypes - 1; i >= 0; --i) {
5068 if (etypes[i]->keytype->type == keytype
5069 && !(etypes[i]->flags & F_PSEUDO)
5070 && krb5_enctype_valid(context, etypes[i]->type) == 0)
5071 ++n;
5073 if (n == 0) {
5074 krb5_set_error_message(context, KRB5_PROG_KEYTYPE_NOSUPP,
5075 "Keytype have no mapping");
5076 return KRB5_PROG_KEYTYPE_NOSUPP;
5079 ret = malloc(n * sizeof(*ret));
5080 if (ret == NULL && n != 0) {
5081 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
5082 return ENOMEM;
5084 n = 0;
5085 for (i = num_etypes - 1; i >= 0; --i) {
5086 if (etypes[i]->keytype->type == keytype
5087 && !(etypes[i]->flags & F_PSEUDO)
5088 && krb5_enctype_valid(context, etypes[i]->type) == 0)
5089 ret[n++] = etypes[i]->type;
5091 *len = n;
5092 *val = ret;
5093 return 0;
5097 * Deprecated: keytypes doesn't exists, they are really enctypes.
5099 * @ingroup krb5_deprecated
5102 /* if two enctypes have compatible keys */
5103 krb5_boolean KRB5_LIB_FUNCTION
5104 krb5_enctypes_compatible_keys(krb5_context context,
5105 krb5_enctype etype1,
5106 krb5_enctype etype2)
5107 KRB5_DEPRECATED
5109 struct encryption_type *e1 = _find_enctype(etype1);
5110 struct encryption_type *e2 = _find_enctype(etype2);
5111 return e1 != NULL && e2 != NULL && e1->keytype == e2->keytype;
5114 #endif /* HEIMDAL_SMALLER */