2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #define KRB5_DEPRECATED
36 #include "krb5_locl.h"
37 #include <pkinit_asn1.h>
39 #ifndef HEIMDAL_SMALLER
40 #define DES3_OLD_ENCTYPE 1
53 struct krb5_crypto_data
{
54 struct encryption_type
*et
;
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 */
75 krb5_error_code (*string_to_key
)(krb5_context
, krb5_enctype
, krb5_data
,
76 krb5_salt
, krb5_data
, krb5_keyblock
*);
80 krb5_keytype type
; /* XXX */
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
{
99 krb5_enctype (*checksum
)(krb5_context context
,
100 struct key_data
*key
,
101 const void *buf
, size_t len
,
104 krb5_error_code (*verify
)(krb5_context context
,
105 struct key_data
*key
,
106 const void *buf
, size_t len
,
111 struct encryption_type
{
116 size_t confoundersize
;
117 struct key_type
*keytype
;
118 struct checksum_type
*checksum
;
119 struct checksum_type
*keyed_checksum
;
121 krb5_error_code (*encrypt
)(krb5_context context
,
122 struct key_data
*key
,
123 void *data
, size_t len
,
124 krb5_boolean encryptp
,
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
,
146 static krb5_error_code
hmac(krb5_context context
,
147 struct checksum_type
*cm
,
151 struct key_data
*keyblock
,
153 static void free_key_data(krb5_context
,
155 struct encryption_type
*);
156 static void free_key_schedule(krb5_context
,
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
{
172 static HEIMDAL_MUTEX crypto_mutex
= HEIMDAL_MUTEX_INITIALIZER
;
174 #ifdef HEIM_WEAK_CRYPTO
176 krb5_DES_random_key(krb5_context context
,
179 DES_cblock
*k
= key
->keyvalue
.data
;
181 krb5_generate_random_block(k
, sizeof(DES_cblock
));
182 DES_set_odd_parity(k
);
183 } while(DES_is_weak_key(k
));
187 krb5_DES_schedule_old(krb5_context context
,
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.
206 krb5_DES_AFS3_CMU_string_to_key (krb5_data pw
,
210 char password
[8+1]; /* crypt is limited to 8 chars anyway */
213 for(i
= 0; i
< 8; i
++) {
214 char c
= ((i
< pw
.length
) ? ((char*)pw
.data
)[i
] : 0) ^
216 tolower(((unsigned char*)cell
.data
)[i
]) : 0);
217 password
[i
] = c
? c
: 'X';
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.
235 krb5_DES_AFS3_Transarc_string_to_key (krb5_data pw
,
239 DES_key_schedule schedule
;
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
);
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
,
282 if(password
.length
> 8)
283 krb5_DES_AFS3_Transarc_string_to_key(password
, salt
.saltvalue
, &tmp
);
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
));
291 #endif /* ENABLE_AFS_STRING_TO_KEY */
294 DES_string_to_key_int(unsigned char *data
, size_t length
, DES_cblock
*key
)
296 DES_key_schedule schedule
;
301 unsigned char swap
[] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
302 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf };
305 p
= (unsigned char*)key
;
306 for (i
= 0; i
< length
; i
++) {
307 unsigned char tmp
= data
[i
];
311 *--p
^= (swap
[tmp
& 0xf] << 4) | swap
[(tmp
& 0xf0) >> 4];
315 DES_set_odd_parity(key
);
316 if(DES_is_weak_key(key
))
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
))
326 static krb5_error_code
327 krb5_DES_string_to_key(krb5_context context
,
328 krb5_enctype enctype
,
338 #ifdef ENABLE_AFS_STRING_TO_KEY
339 if (opaque
.length
== 1) {
341 _krb5_get_int(opaque
.data
, &v
, 1);
343 return DES_AFS3_string_to_key(context
, enctype
, password
,
348 len
= password
.length
+ salt
.saltvalue
.length
;
350 if(len
> 0 && s
== NULL
) {
351 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
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
));
366 krb5_DES_random_to_key(krb5_context context
,
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");
384 DES3_random_key(krb5_context context
,
387 DES_cblock
*k
= key
->keyvalue
.data
;
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.
403 xor (DES_cblock
*key
, const unsigned char *b
)
405 unsigned char *a
= (unsigned char*)key
;
416 #ifdef DES3_OLD_ENCTYPE
417 static krb5_error_code
418 DES3_string_to_key(krb5_context context
,
419 krb5_enctype enctype
,
427 unsigned char tmp
[24];
431 len
= password
.length
+ salt
.saltvalue
.length
;
433 if(len
!= 0 && str
== NULL
) {
434 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
437 memcpy(str
, password
.data
, password
.length
);
438 memcpy(str
+ password
.length
, salt
.saltvalue
.data
, salt
.saltvalue
.length
);
441 DES_key_schedule s
[3];
444 ret
= _krb5_n_fold(str
, len
, tmp
, 24);
448 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
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
,
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
));
482 static krb5_error_code
483 DES3_string_to_key_derived(krb5_context context
,
484 krb5_enctype enctype
,
491 size_t len
= password
.length
+ salt
.saltvalue
.length
;
495 if(len
!= 0 && s
== NULL
) {
496 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
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
,
512 DES3_random_to_key(krb5_context context
,
517 unsigned char *x
= key
->keyvalue
.data
;
518 const u_char
*q
= data
;
522 memset(x
, 0, sizeof(x
));
523 for (i
= 0; i
< 3; ++i
) {
525 for (j
= 0; j
< 7; ++j
) {
526 unsigned char b
= q
[7 * i
+ j
];
531 for (j
= 6; j
>= 0; --j
) {
532 foo
|= q
[7 * i
+ j
] & 1;
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");
549 static krb5_error_code
550 ARCFOUR_string_to_key(krb5_context context
,
551 krb5_enctype enctype
,
562 m
= EVP_MD_CTX_create();
565 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
569 EVP_DigestInit_ex(m
, EVP_md4(), NULL
);
571 ret
= wind_utf8ucs2_length(password
.data
, &len
);
573 krb5_set_error_message (context
, ret
,
574 N_("Password not an UCS2 string", ""));
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", ""));
586 ret
= wind_utf8ucs2(password
.data
, s
, &len
);
588 krb5_set_error_message (context
, ret
,
589 N_("Password not an UCS2 string", ""));
594 for (i
= 0; i
< len
; i
++) {
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);
605 krb5_set_error_message (context
, ENOMEM
, N_("malloc: out of memory", ""));
608 EVP_DigestFinal_ex (m
, key
->keyvalue
.data
, NULL
);
611 EVP_MD_CTX_destroy(m
);
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
,
634 struct encryption_type
*et
;
637 if (opaque
.length
== 0)
638 iter
= _krb5_AES_string_to_default_iterator
;
639 else if (opaque
.length
== 4) {
641 _krb5_get_int(opaque
.data
, &v
, 4);
642 iter
= ((uint32_t)v
);
644 return KRB5_PROG_KEYTYPE_NOSUPP
; /* XXX */
646 et
= _find_enctype(enctype
);
648 return KRB5_PROG_KEYTYPE_NOSUPP
;
653 krb5_set_error_message (context
, ENOMEM
, N_("malloc: out of memory", ""));
656 kd
.key
->keytype
= enctype
;
657 ret
= krb5_data_alloc(&kd
.key
->keyvalue
, et
->keytype
->size
);
659 krb5_set_error_message (context
, ret
, N_("malloc: out of memory", ""));
663 ret
= PKCS5_PBKDF2_HMAC_SHA1(password
.data
, password
.length
,
664 salt
.saltvalue
.data
, salt
.saltvalue
.length
,
666 et
->keytype
->size
, kd
.key
->keyvalue
.data
);
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"));
676 ret
= krb5_copy_keyblock_contents(context
, kd
.key
, key
);
677 free_key_data(context
, &kd
, et
);
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);
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
[] = {
712 krb5_DES_string_to_key
714 #ifdef ENABLE_AFS_STRING_TO_KEY
718 DES_AFS3_string_to_key
725 #ifdef DES3_OLD_ENCTYPE
726 static struct salt_type des3_salt
[] = {
736 static struct salt_type des3_salt_derived
[] = {
740 DES3_string_to_key_derived
745 static struct salt_type AES_salt
[] = {
754 static struct salt_type arcfour_salt
[] = {
758 ARCFOUR_string_to_key
767 static struct key_type keytype_null
= {
778 #ifdef HEIM_WEAK_CRYPTO
779 static struct key_type keytype_des_old
= {
784 sizeof(DES_key_schedule
),
786 krb5_DES_schedule_old
,
788 krb5_DES_random_to_key
791 static struct key_type keytype_des
= {
796 sizeof(struct evp_schedule
),
800 krb5_DES_random_to_key
,
804 #endif /* HEIM_WEAK_CRYPTO */
806 #ifdef DES3_OLD_ENCTYPE
807 static struct key_type keytype_des3
= {
812 sizeof(struct evp_schedule
),
822 static struct key_type keytype_des3_derived
= {
827 sizeof(struct evp_schedule
),
836 static struct key_type keytype_aes128
= {
841 sizeof(struct evp_schedule
),
850 static struct key_type keytype_aes256
= {
855 sizeof(struct evp_schedule
),
864 static struct key_type keytype_arcfour
= {
869 sizeof(struct evp_schedule
),
878 krb5_error_code KRB5_LIB_FUNCTION
879 krb5_salttype_to_string (krb5_context context
,
884 struct encryption_type
*e
;
885 struct salt_type
*st
;
887 e
= _find_enctype (etype
);
889 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
890 "encryption type %d not supported",
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", ""));
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
,
914 krb5_salttype
*salttype
)
916 struct encryption_type
*e
;
917 struct salt_type
*st
;
919 e
= _find_enctype (etype
);
921 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
922 N_("encryption type %d not supported", ""),
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
;
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
,
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
);
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
) {
959 principal
->name
.name_string
.val
[i
],
960 strlen(principal
->name
.name_string
.val
[i
]));
961 p
+= strlen(principal
->name
.name_string
.val
[i
]);
966 krb5_error_code KRB5_LIB_FUNCTION
967 krb5_free_salt(krb5_context context
,
970 krb5_data_free(&salt
.saltvalue
);
974 krb5_error_code KRB5_LIB_FUNCTION
975 krb5_string_to_key_data (krb5_context context
,
976 krb5_enctype enctype
,
978 krb5_principal principal
,
984 ret
= krb5_get_pw_salt(context
, principal
, &salt
);
987 ret
= krb5_string_to_key_data_salt(context
, enctype
, password
, salt
, key
);
988 krb5_free_salt(context
, salt
);
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
,
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
,
1013 krb5_data_zero(&opaque
);
1014 return krb5_string_to_key_data_salt_opaque(context
, enctype
, password
,
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
,
1032 struct encryption_type
*et
=_find_enctype(enctype
);
1033 struct salt_type
*st
;
1035 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
1036 N_("encryption type %d not supported", ""),
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
,
1044 krb5_set_error_message(context
, HEIM_ERR_SALTTYPE_NOSUPP
,
1045 N_("salt type %d not supported", ""),
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
1056 krb5_error_code KRB5_LIB_FUNCTION
1057 krb5_string_to_key_salt (krb5_context context
,
1058 krb5_enctype enctype
,
1059 const char *password
,
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
,
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
,
1089 struct encryption_type
*et
= _find_enctype(type
);
1091 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
1092 N_("encryption type %d not supported", ""),
1094 return KRB5_PROG_ETYPE_NOSUPP
;
1096 *keysize
= et
->keytype
->size
;
1100 krb5_error_code KRB5_LIB_FUNCTION
1101 krb5_enctype_keybits(krb5_context context
,
1105 struct encryption_type
*et
= _find_enctype(type
);
1107 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
1108 "encryption type %d not supported",
1110 return KRB5_PROG_ETYPE_NOSUPP
;
1112 *keybits
= et
->keytype
->bits
;
1116 krb5_error_code KRB5_LIB_FUNCTION
1117 krb5_generate_random_keyblock(krb5_context context
,
1121 krb5_error_code ret
;
1122 struct encryption_type
*et
= _find_enctype(type
);
1124 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
1125 N_("encryption type %d not supported", ""),
1127 return KRB5_PROG_ETYPE_NOSUPP
;
1129 ret
= krb5_data_alloc(&key
->keyvalue
, et
->keytype
->size
);
1132 key
->keytype
= type
;
1133 if(et
->keytype
->random_key
)
1134 (*et
->keytype
->random_key
)(context
, key
);
1136 krb5_generate_random_block(key
->keyvalue
.data
,
1137 key
->keyvalue
.length
);
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
;
1150 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
1151 N_("encryption type %d not supported", ""),
1153 return KRB5_PROG_ETYPE_NOSUPP
;
1158 if(kt
->schedule
== NULL
)
1160 if (key
->schedule
!= NULL
)
1162 ALLOC(key
->schedule
, 1);
1163 if(key
->schedule
== NULL
) {
1164 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
1167 ret
= krb5_data_alloc(key
->schedule
, kt
->schedule_size
);
1169 free(key
->schedule
);
1170 key
->schedule
= NULL
;
1173 (*kt
->schedule
)(context
, kt
, key
);
1177 /************************************************************
1179 ************************************************************/
1181 static krb5_error_code
1182 NONE_checksum(krb5_context context
,
1183 struct key_data
*key
,
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
,
1202 struct evp_schedule
*ctx
= key
->schedule
->data
;
1205 unsigned char *p
= cksum
->checksum
.data
;
1207 krb5_generate_random_block(p
, 8);
1209 m
= EVP_MD_CTX_create();
1211 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
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);
1227 static krb5_error_code
1228 des_verify(krb5_context context
,
1229 const EVP_MD
*evp_md
,
1230 struct key_data
*key
,
1235 struct evp_schedule
*ctx
= key
->schedule
->data
;
1237 unsigned char tmp
[24];
1238 unsigned char res
[16];
1240 krb5_error_code ret
= 0;
1242 m
= EVP_MD_CTX_create();
1244 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
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
));
1268 #ifdef HEIM_WEAK_CRYPTO
1270 static krb5_error_code
1271 CRC32_checksum(krb5_context context
,
1272 struct key_data
*key
,
1279 unsigned char *r
= C
->checksum
.data
;
1280 _krb5_crc_init_table ();
1281 crc
= _krb5_crc_update (data
, len
, 0);
1283 r
[1] = (crc
>> 8) & 0xff;
1284 r
[2] = (crc
>> 16) & 0xff;
1285 r
[3] = (crc
>> 24) & 0xff;
1289 static krb5_error_code
1290 RSA_MD4_checksum(krb5_context context
,
1291 struct key_data
*key
,
1297 if (EVP_Digest(data
, len
, C
->checksum
.data
, NULL
, EVP_md4(), NULL
) != 1)
1298 krb5_abortx(context
, "md4 checksum failed");
1302 static krb5_error_code
1303 RSA_MD4_DES_checksum(krb5_context context
,
1304 struct key_data
*key
,
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
,
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
,
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
,
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
,
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
,
1368 return des_verify(context
, EVP_md5(), key
, data
, len
, C
);
1372 static krb5_error_code
1373 SHA1_checksum(krb5_context context
,
1374 struct key_data
*key
,
1380 if (EVP_Digest(data
, len
, C
->checksum
.data
, NULL
, EVP_sha1(), NULL
) != 1)
1381 krb5_abortx(context
, "sha1 checksum failed");
1385 /* HMAC according to RFC2104 */
1386 static krb5_error_code
1387 hmac(krb5_context context
,
1388 struct checksum_type
*cm
,
1392 struct key_data
*keyblock
,
1395 unsigned char *ipad
, *opad
;
1400 ipad
= malloc(cm
->blocksize
+ len
);
1403 opad
= malloc(cm
->blocksize
+ cm
->checksumsize
);
1408 memset(ipad
, 0x36, cm
->blocksize
);
1409 memset(opad
, 0x5c, cm
->blocksize
);
1411 if(keyblock
->key
->keyvalue
.length
> cm
->blocksize
){
1412 (*cm
->checksum
)(context
,
1414 keyblock
->key
->keyvalue
.data
,
1415 keyblock
->key
->keyvalue
.length
,
1418 key
= result
->checksum
.data
;
1419 key_len
= result
->checksum
.length
;
1421 key
= keyblock
->key
->keyvalue
.data
;
1422 key_len
= keyblock
->key
->keyvalue
.length
;
1424 for(i
= 0; i
< key_len
; i
++){
1428 memcpy(ipad
+ cm
->blocksize
, data
, len
);
1429 (*cm
->checksum
)(context
, keyblock
, ipad
, cm
->blocksize
+ len
,
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
);
1437 memset(opad
, 0, cm
->blocksize
+ cm
->checksumsize
);
1443 krb5_error_code KRB5_LIB_FUNCTION
1444 krb5_hmac(krb5_context context
,
1445 krb5_cksumtype cktype
,
1452 struct checksum_type
*c
= _find_checksum(cktype
);
1454 krb5_error_code ret
;
1457 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
1458 N_("checksum type %d not supported", ""),
1460 return KRB5_PROG_SUMTYPE_NOSUPP
;
1466 ret
= hmac(context
, c
, data
, len
, usage
, &kd
, result
);
1469 krb5_free_data(context
, kd
.schedule
);
1474 static krb5_error_code
1475 SP_HMAC_SHA1_checksum(krb5_context context
,
1476 struct key_data
*key
,
1482 struct checksum_type
*c
= _find_checksum(CKSUMTYPE_SHA1
);
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
);
1492 krb5_abortx(context
, "hmac failed");
1493 memcpy(result
->checksum
.data
, res
.checksum
.data
, result
->checksum
.length
);
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
,
1510 struct checksum_type
*c
= _find_checksum (CKSUMTYPE_RSA_MD5
);
1511 const char signature
[] = "signaturekey";
1513 struct key_data ksign
;
1516 unsigned char tmp
[16];
1517 unsigned char ksign_c_data
[16];
1518 krb5_error_code ret
;
1520 m
= EVP_MD_CTX_create();
1522 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
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
);
1529 EVP_MD_CTX_destroy(m
);
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
);
1550 static struct checksum_type checksum_none
= {
1559 #ifdef HEIM_WEAK_CRYPTO
1560 static struct checksum_type checksum_crc32
= {
1569 static struct checksum_type checksum_rsa_md4
= {
1578 static struct checksum_type checksum_rsa_md4_des
= {
1579 CKSUMTYPE_RSA_MD4_DES
,
1583 F_KEYED
| F_CPROOF
| F_VARIANT
,
1584 RSA_MD4_DES_checksum
,
1587 static struct checksum_type checksum_rsa_md5_des
= {
1588 CKSUMTYPE_RSA_MD5_DES
,
1592 F_KEYED
| F_CPROOF
| F_VARIANT
,
1593 RSA_MD5_DES_checksum
,
1596 #endif /* HEIM_WEAK_CRYPTO */
1598 static krb5_error_code
1599 RSA_MD5_checksum(krb5_context context
,
1600 struct key_data
*key
,
1606 if (EVP_Digest(data
, len
, C
->checksum
.data
, NULL
, EVP_md5(), NULL
) != 1)
1607 krb5_abortx(context
, "md5 checksum failed");
1611 static struct checksum_type checksum_rsa_md5
= {
1621 #ifdef DES3_OLD_ENCTYPE
1622 static struct checksum_type checksum_rsa_md5_des3
= {
1623 CKSUMTYPE_RSA_MD5_DES3
,
1627 F_KEYED
| F_CPROOF
| F_VARIANT
,
1628 RSA_MD5_DES3_checksum
,
1632 static struct checksum_type checksum_sha1
= {
1641 static struct checksum_type checksum_hmac_sha1_des3
= {
1642 CKSUMTYPE_HMAC_SHA1_DES3
,
1646 F_KEYED
| F_CPROOF
| F_DERIVED
,
1647 SP_HMAC_SHA1_checksum
,
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
,
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
,
1671 static struct checksum_type checksum_hmac_md5
= {
1681 static struct checksum_type
*checksum_types
[] = {
1683 #ifdef HEIM_WEAK_CRYPTO
1686 &checksum_rsa_md4_des
,
1687 &checksum_rsa_md5_des
,
1689 #ifdef DES3_OLD_ENCTYPE
1690 &checksum_rsa_md5_des3
,
1694 &checksum_hmac_sha1_des3
,
1695 &checksum_hmac_sha1_aes128
,
1696 &checksum_hmac_sha1_aes256
,
1700 static int num_checksums
= sizeof(checksum_types
) / sizeof(checksum_types
[0]);
1702 static struct checksum_type
*
1703 _find_checksum(krb5_cksumtype type
)
1706 for(i
= 0; i
< num_checksums
; i
++)
1707 if(checksum_types
[i
]->type
== type
)
1708 return checksum_types
[i
];
1712 static krb5_error_code
1713 get_checksum_key(krb5_context context
,
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
) {
1726 *key
= _new_derived_key(crypto
, 0xff/* KRB5_KU_RFC1510_VARIANT */);
1728 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
1731 ret
= krb5_copy_keyblock(context
, crypto
->key
.key
, &(*key
)->key
);
1734 for(i
= 0; i
< (*key
)->key
->keyvalue
.length
; i
++)
1735 ((unsigned char*)(*key
)->key
->keyvalue
.data
)[i
] ^= 0xF0;
1737 *key
= &crypto
->key
;
1740 ret
= _key_schedule(context
, *key
);
1744 static krb5_error_code
1745 create_checksum (krb5_context context
,
1746 struct checksum_type
*ct
,
1753 krb5_error_code ret
;
1754 struct key_data
*dkey
;
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", ""),
1767 return KRB5_PROG_SUMTYPE_NOSUPP
; /* XXX */
1769 if(keyed_checksum
) {
1770 ret
= get_checksum_key(context
, crypto
, usage
, ct
, &dkey
);
1775 result
->cksumtype
= ct
->type
;
1776 ret
= krb5_data_alloc(&result
->checksum
, ct
->checksumsize
);
1779 return (*ct
->checksum
)(context
, dkey
, data
, len
, usage
, result
);
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
,
1792 krb5_key_usage usage
,
1798 struct checksum_type
*ct
= NULL
;
1801 /* type 0 -> pick from crypto */
1803 ct
= _find_checksum(type
);
1804 } else if (crypto
) {
1805 ct
= crypto
->et
->keyed_checksum
;
1807 ct
= crypto
->et
->checksum
;
1811 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
1812 N_("checksum type %d not supported", ""),
1814 return KRB5_PROG_SUMTYPE_NOSUPP
;
1817 if (arcfour_checksum_p(ct
, crypto
)) {
1819 usage2arcfour(context
, &keyusage
);
1821 keyusage
= CHECKSUM_USAGE(usage
);
1823 return create_checksum(context
, ct
, crypto
, keyusage
,
1827 static krb5_error_code
1828 verify_checksum(krb5_context context
,
1830 unsigned usage
, /* not krb5_key_usage */
1835 krb5_error_code ret
;
1836 struct key_data
*dkey
;
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", ""),
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", ""),
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
);
1878 return (*ct
->verify
)(context
, dkey
, data
, len
, usage
, cksum
);
1880 ret
= krb5_data_alloc (&c
.checksum
, ct
->checksumsize
);
1884 ret
= (*ct
->checksum
)(context
, dkey
, data
, len
, usage
, &c
);
1886 krb5_data_free(&c
.checksum
);
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
;
1897 krb5_data_free (&c
.checksum
);
1901 krb5_error_code KRB5_LIB_FUNCTION
1902 krb5_verify_checksum(krb5_context context
,
1904 krb5_key_usage usage
,
1909 struct checksum_type
*ct
;
1912 ct
= _find_checksum(cksum
->cksumtype
);
1914 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
1915 N_("checksum type %d not supported", ""),
1917 return KRB5_PROG_SUMTYPE_NOSUPP
;
1920 if (arcfour_checksum_p(ct
, crypto
)) {
1922 usage2arcfour(context
, &keyusage
);
1924 keyusage
= CHECKSUM_USAGE(usage
);
1926 return verify_checksum(context
, crypto
, keyusage
,
1930 krb5_error_code KRB5_LIB_FUNCTION
1931 krb5_crypto_get_checksum_type(krb5_context context
,
1933 krb5_cksumtype
*type
)
1935 struct checksum_type
*ct
= NULL
;
1937 if (crypto
!= NULL
) {
1938 ct
= crypto
->et
->keyed_checksum
;
1940 ct
= crypto
->et
->checksum
;
1944 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
1945 N_("checksum type not found", ""));
1946 return KRB5_PROG_SUMTYPE_NOSUPP
;
1955 krb5_error_code KRB5_LIB_FUNCTION
1956 krb5_checksumsize(krb5_context context
,
1957 krb5_cksumtype type
,
1960 struct checksum_type
*ct
= _find_checksum(type
);
1962 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
1963 N_("checksum type %d not supported", ""),
1965 return KRB5_PROG_SUMTYPE_NOSUPP
;
1967 *size
= ct
->checksumsize
;
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
);
1978 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
1979 N_("checksum type %d not supported", ""),
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
);
1993 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
1994 N_("checksum type %d not supported", ""),
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
);
2008 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
2009 N_("checksum type %d not supported", ""),
2011 return KRB5_PROG_SUMTYPE_NOSUPP
;
2013 ct
->flags
|= F_DISABLED
;
2017 /************************************************************
2019 ************************************************************/
2021 static krb5_error_code
2022 NULL_encrypt(krb5_context context
,
2023 struct key_data
*key
,
2026 krb5_boolean encryptp
,
2033 static krb5_error_code
2034 evp_encrypt(krb5_context context
,
2035 struct key_data
*key
,
2038 krb5_boolean encryptp
,
2042 struct evp_schedule
*ctx
= key
->schedule
->data
;
2044 c
= encryptp
? &ctx
->ectx
: &ctx
->dctx
;
2047 size_t len
= EVP_CIPHER_CTX_iv_length(c
);
2048 void *loiv
= malloc(len
);
2050 krb5_clear_error_message(context
);
2053 memset(loiv
, 0, len
);
2054 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, loiv
, -1);
2057 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, ivec
, -1);
2058 EVP_Cipher(c
, data
, data
, len
);
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
,
2069 krb5_boolean encryptp
,
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
];
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");
2087 } else if (len
== blocksize
) {
2088 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, zero_ivec
, -1);
2089 EVP_Cipher(c
, data
, data
, len
);
2094 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, ivec
, -1);
2096 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, zero_ivec
, -1);
2101 i
= ((len
- 1) / blocksize
) * blocksize
;
2102 EVP_Cipher(c
, p
, p
, 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
);
2117 memcpy(ivec
, p
, blocksize
);
2119 char tmp2
[EVP_MAX_BLOCK_LENGTH
], tmp3
[EVP_MAX_BLOCK_LENGTH
];
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
);
2128 len
-= i
+ blocksize
;
2131 memcpy(ivec2
, ivec
, blocksize
);
2133 memcpy(ivec2
, zero_ivec
, 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
++)
2153 memcpy(ivec
, tmp
, blocksize
);
2158 #ifdef HEIM_WEAK_CRYPTO
2159 static krb5_error_code
2160 evp_des_encrypt_null_ivec(krb5_context context
,
2161 struct key_data
*key
,
2164 krb5_boolean encryptp
,
2168 struct evp_schedule
*ctx
= key
->schedule
->data
;
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
);
2178 static krb5_error_code
2179 evp_des_encrypt_key_ivec(krb5_context context
,
2180 struct key_data
*key
,
2183 krb5_boolean encryptp
,
2187 struct evp_schedule
*ctx
= key
->schedule
->data
;
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
);
2197 static krb5_error_code
2198 DES_CFB64_encrypt_null_ivec(krb5_context context
,
2199 struct key_data
*key
,
2202 krb5_boolean encryptp
,
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
);
2215 static krb5_error_code
2216 DES_PCBC_encrypt_key_ivec(krb5_context context
,
2217 struct key_data
*key
,
2220 krb5_boolean encryptp
,
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
);
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
,
2248 struct checksum_type
*c
= _find_checksum (CKSUMTYPE_RSA_MD5
);
2249 Checksum k1_c
, k2_c
, k3_c
, cksum
;
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
);
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
;
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
);
2282 krb5_abortx(context
, "hmac failed");
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
);
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
));
2306 static krb5_error_code
2307 ARCFOUR_subdecrypt(krb5_context context
,
2308 struct key_data
*key
,
2315 struct checksum_type
*c
= _find_checksum (CKSUMTYPE_RSA_MD5
);
2316 Checksum k1_c
, k2_c
, k3_c
, cksum
;
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
);
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
;
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
);
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
);
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
);
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
;
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
)
2389 case KRB5_KU_AS_REP_ENC_PART
: /* 3 */
2392 case KRB5_KU_USAGE_SEAL
: /* 22 */
2395 case KRB5_KU_USAGE_SIGN
: /* 23 */
2398 case KRB5_KU_USAGE_SEQ
: /* 24 */
2406 static krb5_error_code
2407 ARCFOUR_encrypt(krb5_context context
,
2408 struct key_data
*key
,
2411 krb5_boolean encryptp
,
2415 krb5_error_code ret
;
2416 unsigned keyusage
= usage
;
2418 if((ret
= usage2arcfour (context
, &keyusage
)) != 0)
2422 return ARCFOUR_subencrypt (context
, key
, data
, len
, keyusage
, ivec
);
2424 return ARCFOUR_subdecrypt (context
, key
, data
, len
, keyusage
, ivec
);
2432 static krb5_error_code
2433 AES_PRF(krb5_context context
,
2435 const krb5_data
*in
,
2438 struct checksum_type
*ct
= crypto
->et
->checksum
;
2439 krb5_error_code ret
;
2441 krb5_keyblock
*derived
;
2443 result
.cksumtype
= ct
->type
;
2444 ret
= krb5_data_alloc(&result
.checksum
, ct
->checksumsize
);
2446 krb5_set_error_message(context
, ret
, N_("malloc: out memory", ""));
2450 ret
= (*ct
->checksum
)(context
, NULL
, in
->data
, in
->length
, 0, &result
);
2452 krb5_data_free(&result
.checksum
);
2456 if (result
.checksum
.length
< crypto
->et
->blocksize
)
2457 krb5_abortx(context
, "internal prf error");
2460 ret
= krb5_derive_key(context
, crypto
->key
.key
,
2461 crypto
->et
->type
, "prf", 3, &derived
);
2463 krb5_abortx(context
, "krb5_derive_key");
2465 ret
= krb5_data_alloc(out
, crypto
->et
->blocksize
);
2467 krb5_abortx(context
, "malloc failed");
2470 const EVP_CIPHER
*c
= (*crypto
->et
->keytype
->evp
)();
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
);
2487 * these should currently be in reverse preference order.
2488 * (only relevant for !F_PSEUDO) */
2490 static struct encryption_type enctype_null
= {
2504 static struct encryption_type enctype_arcfour_hmac_md5
= {
2505 ETYPE_ARCFOUR_HMAC_MD5
,
2518 #ifdef DES3_OLD_ENCTYPE
2519 static struct encryption_type enctype_des3_cbc_md5
= {
2527 &checksum_rsa_md5_des3
,
2534 static struct encryption_type enctype_des3_cbc_sha1
= {
2535 ETYPE_DES3_CBC_SHA1
,
2540 &keytype_des3_derived
,
2542 &checksum_hmac_sha1_des3
,
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",
2557 &checksum_hmac_sha1_des3
,
2564 static struct encryption_type enctype_aes128_cts_hmac_sha1
= {
2565 ETYPE_AES128_CTS_HMAC_SHA1_96
,
2566 "aes128-cts-hmac-sha1-96",
2572 &checksum_hmac_sha1_aes128
,
2578 static struct encryption_type enctype_aes256_cts_hmac_sha1
= {
2579 ETYPE_AES256_CTS_HMAC_SHA1_96
,
2580 "aes256-cts-hmac-sha1-96",
2586 &checksum_hmac_sha1_aes256
,
2592 static struct encryption_type enctype_des3_cbc_none
= {
2593 ETYPE_DES3_CBC_NONE
,
2598 &keytype_des3_derived
,
2606 #ifdef HEIM_WEAK_CRYPTO
2607 static struct encryption_type enctype_des_cbc_crc
= {
2617 evp_des_encrypt_key_ivec
,
2621 static struct encryption_type enctype_des_cbc_md4
= {
2629 &checksum_rsa_md4_des
,
2631 evp_des_encrypt_null_ivec
,
2635 static struct encryption_type enctype_des_cbc_md5
= {
2643 &checksum_rsa_md5_des
,
2645 evp_des_encrypt_null_ivec
,
2649 static struct encryption_type enctype_des_cbc_none
= {
2658 F_PSEUDO
|F_DISABLED
|F_WEAK
,
2659 evp_des_encrypt_null_ivec
,
2663 static struct encryption_type enctype_des_cfb64_none
= {
2664 ETYPE_DES_CFB64_NONE
,
2672 F_PSEUDO
|F_DISABLED
|F_WEAK
,
2673 DES_CFB64_encrypt_null_ivec
,
2677 static struct encryption_type enctype_des_pcbc_none
= {
2678 ETYPE_DES_PCBC_NONE
,
2686 F_PSEUDO
|F_DISABLED
|F_WEAK
,
2687 DES_PCBC_encrypt_key_ivec
,
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
,
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
,
2714 static unsigned num_etypes
= sizeof(etypes
) / sizeof(etypes
[0]);
2717 static struct encryption_type
*
2718 _find_enctype(krb5_enctype type
)
2721 for(i
= 0; i
< num_etypes
; i
++)
2722 if(etypes
[i
]->type
== type
)
2728 krb5_error_code KRB5_LIB_FUNCTION
2729 krb5_enctype_to_string(krb5_context context
,
2733 struct encryption_type
*e
;
2734 e
= _find_enctype(etype
);
2736 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
2737 N_("encryption type %d not supported", ""),
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", ""));
2750 krb5_error_code KRB5_LIB_FUNCTION
2751 krb5_string_to_enctype(krb5_context context
,
2753 krb5_enctype
*etype
)
2756 for(i
= 0; i
< num_etypes
; i
++)
2757 if(strcasecmp(etypes
[i
]->name
, string
) == 0){
2758 *etype
= etypes
[i
]->type
;
2761 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
2762 N_("encryption type %s not supported", ""),
2764 return KRB5_PROG_ETYPE_NOSUPP
;
2767 krb5_error_code KRB5_LIB_FUNCTION
2768 krb5_enctype_to_keytype(krb5_context context
,
2770 krb5_keytype
*keytype
)
2772 struct encryption_type
*e
= _find_enctype(etype
);
2774 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
2775 N_("encryption type %d not supported", ""),
2777 return KRB5_PROG_ETYPE_NOSUPP
;
2779 *keytype
= e
->keytype
->type
; /* XXX */
2783 krb5_error_code KRB5_LIB_FUNCTION
2784 krb5_enctype_valid(krb5_context context
,
2787 struct encryption_type
*e
= _find_enctype(etype
);
2789 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
2790 N_("encryption type %d not supported", ""),
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", ""),
2798 return KRB5_PROG_ETYPE_NOSUPP
;
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
)
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
;
2834 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
2835 N_("checksum type %d not supported", ""),
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
);
2847 krb5_set_error_message (context
, KRB5_PROG_SUMTYPE_NOSUPP
,
2848 N_("checksum type %d not supported", ""),
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", ""),
2856 return KRB5_PROG_SUMTYPE_NOSUPP
;
2863 derived_crypto(krb5_context context
,
2866 return (crypto
->et
->flags
& F_DERIVED
) != 0;
2870 special_crypto(krb5_context context
,
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
,
2888 size_t sz
, block_sz
, checksum_sz
, total_sz
;
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
);
2902 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
2907 krb5_generate_random_block(q
, et
->confoundersize
); /* XXX */
2908 q
+= et
->confoundersize
;
2909 memcpy(q
, data
, len
);
2911 ret
= create_checksum(context
,
2914 INTEGRITY_USAGE(usage
),
2918 if(ret
== 0 && cksum
.checksum
.length
!= checksum_sz
) {
2919 free_Checksum (&cksum
);
2920 krb5_clear_error_message (context
);
2921 ret
= KRB5_CRYPTO_INTERNAL
;
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
);
2930 ret
= _key_schedule(context
, dkey
);
2933 ret
= (*et
->encrypt
)(context
, dkey
, p
, block_sz
, 1, usage
, ivec
);
2937 result
->length
= total_sz
;
2940 memset(p
, 0, total_sz
);
2946 static krb5_error_code
2947 encrypt_internal(krb5_context context
,
2954 size_t sz
, block_sz
, checksum_sz
;
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
);
2966 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
2971 krb5_generate_random_block(q
, et
->confoundersize
); /* XXX */
2972 q
+= et
->confoundersize
;
2973 memset(q
, 0, checksum_sz
);
2975 memcpy(q
, data
, len
);
2977 ret
= create_checksum(context
,
2984 if(ret
== 0 && cksum
.checksum
.length
!= checksum_sz
) {
2985 krb5_clear_error_message (context
);
2986 free_Checksum(&cksum
);
2987 ret
= KRB5_CRYPTO_INTERNAL
;
2991 memcpy(p
+ et
->confoundersize
, cksum
.checksum
.data
, cksum
.checksum
.length
);
2992 free_Checksum(&cksum
);
2993 ret
= _key_schedule(context
, &crypto
->key
);
2996 ret
= (*et
->encrypt
)(context
, &crypto
->key
, p
, block_sz
, 1, 0, ivec
);
2998 memset(p
, 0, block_sz
);
3003 result
->length
= block_sz
;
3006 memset(p
, 0, block_sz
);
3011 static krb5_error_code
3012 encrypt_internal_special(krb5_context context
,
3020 struct encryption_type
*et
= crypto
->et
;
3021 size_t cksum_sz
= CHECKSUMSIZE(et
->checksum
);
3022 size_t sz
= len
+ cksum_sz
+ et
->confoundersize
;
3024 krb5_error_code ret
;
3028 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
3032 memset (p
, 0, 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
);
3044 result
->length
= sz
;
3048 static krb5_error_code
3049 decrypt_internal_derived(krb5_context context
,
3060 krb5_error_code ret
;
3061 struct key_data
*dkey
;
3062 struct encryption_type
*et
= crypto
->et
;
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
;
3079 if(len
!= 0 && p
== NULL
) {
3080 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
3083 memcpy(p
, data
, len
);
3087 ret
= _get_derived_key(context
, crypto
, ENCRYPTION_USAGE(usage
), &dkey
);
3092 ret
= _key_schedule(context
, dkey
);
3097 ret
= (*et
->encrypt
)(context
, dkey
, p
, len
, 0, usage
, ivec
);
3103 cksum
.checksum
.data
= p
+ len
;
3104 cksum
.checksum
.length
= checksum_sz
;
3105 cksum
.cksumtype
= CHECKSUMTYPE(et
->keyed_checksum
);
3107 ret
= verify_checksum(context
,
3109 INTEGRITY_USAGE(usage
),
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) {
3122 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
3129 static krb5_error_code
3130 decrypt_internal(krb5_context context
,
3137 krb5_error_code ret
;
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
);
3150 if(len
!= 0 && p
== NULL
) {
3151 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
3154 memcpy(p
, data
, len
);
3156 ret
= _key_schedule(context
, &crypto
->key
);
3161 ret
= (*et
->encrypt
)(context
, &crypto
->key
, p
, len
, 0, 0, ivec
);
3166 ret
= krb5_data_copy(&cksum
.checksum
, p
+ et
->confoundersize
, checksum_sz
);
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
);
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) {
3184 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
3191 static krb5_error_code
3192 decrypt_internal_special(krb5_context context
,
3200 struct encryption_type
*et
= crypto
->et
;
3201 size_t cksum_sz
= CHECKSUMSIZE(et
->checksum
);
3202 size_t sz
= len
- cksum_sz
- et
->confoundersize
;
3204 krb5_error_code ret
;
3206 if ((len
% et
->padsize
) != 0) {
3207 krb5_clear_error_message(context
);
3208 return KRB5_BAD_MSIZE
;
3213 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
3216 memcpy(p
, data
, len
);
3218 ret
= (*et
->encrypt
)(context
, &crypto
->key
, p
, len
, FALSE
, usage
, ivec
);
3224 memmove (p
, p
+ cksum_sz
+ et
->confoundersize
, sz
);
3225 result
->data
= realloc(p
, sz
);
3226 if(result
->data
== NULL
&& sz
!= 0) {
3228 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
3231 result
->length
= sz
;
3235 static krb5_crypto_iov
*
3236 find_iv(krb5_crypto_iov
*data
, int num_data
, int type
)
3239 for (i
= 0; i
< num_data
; i
++)
3240 if (data
[i
].flags
== type
)
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
,
3273 krb5_crypto_iov
*data
,
3277 size_t headersz
, trailersz
, len
;
3279 size_t sz
, block_sz
, pad_sz
;
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
;
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
)
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
;
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
);
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
;
3325 if (piv
->data
.length
< pad_sz
)
3326 return KRB5_BAD_MSIZE
;
3327 piv
->data
.length
= pad_sz
;
3329 memset(piv
->data
.data
, pad_sz
, pad_sz
);
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
3342 * XXX CTS EVP is broken, can't handle multi buffers :(
3346 for (i
= 0; i
< num_data
; i
++) {
3347 if (data
[i
].flags
!= KRB5_CRYPTO_TYPE_SIGN_ONLY
)
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
)
3360 memcpy(q
, data
[i
].data
.data
, data
[i
].data
.length
);
3361 q
+= data
[i
].data
.length
;
3364 memset(q
, 0, piv
->data
.length
);
3366 ret
= create_checksum(context
,
3369 INTEGRITY_USAGE(usage
),
3374 if(ret
== 0 && cksum
.checksum
.length
!= trailersz
) {
3375 free_Checksum (&cksum
);
3376 krb5_clear_error_message (context
);
3377 ret
= KRB5_CRYPTO_INTERNAL
;
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
);
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
)
3397 memcpy(q
, data
[i
].data
.data
, data
[i
].data
.length
);
3398 q
+= data
[i
].data
.length
;
3401 memset(q
, 0, piv
->data
.length
);
3404 ret
= _get_derived_key(context
, crypto
, ENCRYPTION_USAGE(usage
), &dkey
);
3409 ret
= _key_schedule(context
, dkey
);
3415 ret
= (*et
->encrypt
)(context
, dkey
, p
, block_sz
, 1, usage
, ivec
);
3421 /* now copy data back to buffers */
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
)
3430 memcpy(data
[i
].data
.data
, q
, data
[i
].data
.length
);
3431 q
+= data
[i
].data
.length
;
3434 memcpy(piv
->data
.data
, q
, pad_sz
);
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
,
3466 krb5_crypto_iov
*data
,
3467 unsigned int num_data
,
3471 size_t headersz
, trailersz
, len
;
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
;
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
;
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 */
3505 for (i
= 0; i
< num_data
; i
++) {
3506 if (data
[i
].flags
!= KRB5_CRYPTO_TYPE_DATA
)
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
);
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
)
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
);
3537 ret
= _key_schedule(context
, dkey
);
3543 ret
= (*et
->encrypt
)(context
, dkey
, p
, len
, 0, usage
, ivec
);
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
)
3555 memcpy(data
[i
].data
.data
, q
, data
[i
].data
.length
);
3556 q
+= data
[i
].data
.length
;
3561 /* check signature */
3562 for (i
= 0; i
< num_data
; i
++) {
3563 if (data
[i
].flags
!= KRB5_CRYPTO_TYPE_SIGN_ONLY
)
3565 len
+= data
[i
].data
.length
;
3568 p
= q
= malloc(len
);
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
)
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
,
3588 INTEGRITY_USAGE(usage
),
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
,
3614 krb5_crypto_iov
*data
,
3615 unsigned int num_data
,
3616 krb5_cksumtype
*type
)
3619 krb5_crypto_iov
*civ
;
3620 krb5_error_code ret
;
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
);
3637 return KRB5_BAD_MSIZE
;
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
)
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
)
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
);
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
);
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
,
3697 krb5_crypto_iov
*data
,
3698 unsigned int num_data
,
3699 krb5_cksumtype
*type
)
3701 struct encryption_type
*et
= crypto
->et
;
3703 krb5_crypto_iov
*civ
;
3704 krb5_error_code ret
;
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
);
3721 return KRB5_BAD_MSIZE
;
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
)
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
)
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
);
3748 if (ret
== 0 && type
)
3749 *type
= cksum
.cksumtype
;
3755 krb5_error_code KRB5_LIB_FUNCTION
3756 krb5_crypto_length(krb5_context context
,
3761 if (!derived_crypto(context
, crypto
)) {
3762 krb5_set_error_message(context
, EINVAL
, "not a derived crypto");
3767 case KRB5_CRYPTO_TYPE_EMPTY
:
3770 case KRB5_CRYPTO_TYPE_HEADER
:
3771 *len
= crypto
->et
->blocksize
;
3773 case KRB5_CRYPTO_TYPE_DATA
:
3774 case KRB5_CRYPTO_TYPE_SIGN_ONLY
:
3775 /* len must already been filled in */
3777 case KRB5_CRYPTO_TYPE_PADDING
:
3778 if (crypto
->et
->padsize
> 1)
3779 *len
= crypto
->et
->padsize
;
3783 case KRB5_CRYPTO_TYPE_TRAILER
:
3784 *len
= CHECKSUMSIZE(crypto
->et
->keyed_checksum
);
3786 case KRB5_CRYPTO_TYPE_CHECKSUM
:
3787 if (crypto
->et
->keyed_checksum
)
3788 *len
= CHECKSUMSIZE(crypto
->et
->keyed_checksum
);
3790 *len
= CHECKSUMSIZE(crypto
->et
->checksum
);
3793 krb5_set_error_message(context
, EINVAL
,
3794 "%d not a supported type", type
);
3799 krb5_error_code KRB5_LIB_FUNCTION
3800 krb5_crypto_length_iov(krb5_context context
,
3802 krb5_crypto_iov
*data
,
3803 unsigned int num_data
)
3805 krb5_error_code ret
;
3808 for (i
= 0; i
< num_data
; i
++) {
3809 ret
= krb5_crypto_length(context
, crypto
,
3811 &data
[i
].data
.length
);
3819 krb5_error_code KRB5_LIB_FUNCTION
3820 krb5_encrypt_ivec(krb5_context context
,
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
);
3835 return encrypt_internal(context
, crypto
, data
, len
, result
, ivec
);
3838 krb5_error_code KRB5_LIB_FUNCTION
3839 krb5_encrypt(krb5_context context
,
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
,
3856 EncryptedData
*result
)
3858 result
->etype
= CRYPTO_ETYPE(crypto
);
3860 ALLOC(result
->kvno
, 1);
3861 *result
->kvno
= kvno
;
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
,
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
);
3883 return decrypt_internal(context
, crypto
, data
, len
, result
, ivec
);
3886 krb5_error_code KRB5_LIB_FUNCTION
3887 krb5_decrypt(krb5_context context
,
3894 return krb5_decrypt_ivec (context
, crypto
, usage
, data
, len
, result
,
3898 krb5_error_code KRB5_LIB_FUNCTION
3899 krb5_decrypt_EncryptedData(krb5_context context
,
3902 const EncryptedData
*e
,
3905 return krb5_decrypt(context
, crypto
, usage
,
3906 e
->cipher
.data
, e
->cipher
.length
, result
);
3909 /************************************************************
3911 ************************************************************/
3913 #define ENTROPY_NEEDED 128
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
))) {
3924 fd
= open(seedfile
, O_RDONLY
| O_BINARY
| O_CLOEXEC
);
3928 ret
= read(fd
, buf
, sizeof(buf
));
3930 RAND_add(buf
, ret
, 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
;
3944 if (!krb5_init_context(&context
)) {
3945 p
= krb5_config_get_string(context
, NULL
, "libdefaults",
3946 "egd_socket", NULL
);
3948 RAND_egd_bytes(p
, ENTROPY_NEEDED
);
3949 krb5_free_context(context
);
3953 if (RAND_status() == 1) {
3954 /* Update the seed file */
3956 RAND_write_file(seedfile
);
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
,
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
);
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
);
4001 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
4004 ret
= _krb5_n_fold(constant
, len
, k
, et
->blocksize
);
4006 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
4010 for(i
= 0; i
< nblocks
; i
++) {
4012 memcpy(k
+ i
* et
->blocksize
,
4013 k
+ (i
- 1) * et
->blocksize
,
4015 (*et
->encrypt
)(context
, key
, k
+ i
* et
->blocksize
, et
->blocksize
,
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
) {
4025 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
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
) {
4034 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
4037 ret
= _krb5_n_fold(c
, len
, k
, res_len
);
4040 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
4045 /* XXX keytype dependent post-processing */
4048 DES3_random_to_key(context
, key
->key
, k
, nblocks
* et
->blocksize
);
4050 case KEYTYPE_AES128
:
4051 case KEYTYPE_AES256
:
4052 memcpy(key
->key
->keyvalue
.data
, k
, key
->key
->keyvalue
.length
);
4055 ret
= KRB5_CRYPTO_INTERNAL
;
4056 krb5_set_error_message(context
, ret
,
4057 N_("derive_key() called with unknown keytype (%u)", ""),
4062 if (key
->schedule
) {
4063 free_key_schedule(context
, key
, et
);
4064 key
->schedule
= NULL
;
4067 memset(k
, 0, nblocks
* et
->blocksize
);
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
));
4080 crypto
->key_usage
= d
;
4081 d
+= crypto
->num_key_usage
++;
4082 memset(d
, 0, sizeof(*d
));
4087 krb5_error_code KRB5_LIB_FUNCTION
4088 krb5_derive_key(krb5_context context
,
4089 const krb5_keyblock
*key
,
4091 const void *constant
,
4092 size_t constant_len
,
4093 krb5_keyblock
**derived_key
)
4095 krb5_error_code ret
;
4096 struct encryption_type
*et
;
4099 *derived_key
= NULL
;
4101 et
= _find_enctype (etype
);
4103 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
4104 N_("encryption type %d not supported", ""),
4106 return KRB5_PROG_ETYPE_NOSUPP
;
4109 ret
= krb5_copy_keyblock(context
, key
, &d
.key
);
4114 ret
= derive_key(context
, et
, &d
, constant
, constant_len
);
4116 ret
= krb5_copy_keyblock(context
, d
.key
, derived_key
);
4117 free_key_data(context
, &d
, et
);
4121 static krb5_error_code
4122 _get_derived_key(krb5_context context
,
4125 struct key_data
**key
)
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
;
4136 d
= _new_derived_key(crypto
, usage
);
4138 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
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
));
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
,
4170 krb5_crypto
*crypto
)
4172 krb5_error_code ret
;
4174 if(*crypto
== NULL
) {
4175 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
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
)) {
4184 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
4185 N_("encryption type %d not supported", ""),
4187 return KRB5_PROG_ETYPE_NOSUPP
;
4189 if((*crypto
)->et
->keytype
->size
!= key
->keyvalue
.length
) {
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
);
4202 (*crypto
)->key
.schedule
= NULL
;
4203 (*crypto
)->num_key_usage
= 0;
4204 (*crypto
)->key_usage
= NULL
;
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
);
4220 free_key_data(krb5_context context
, struct key_data
*key
,
4221 struct encryption_type
*et
)
4223 krb5_free_keyblock(context
, key
->key
);
4225 free_key_schedule(context
, key
, et
);
4226 key
->schedule
= NULL
;
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
,
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
);
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
,
4279 *blocksize
= crypto
->et
->blocksize
;
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
,
4298 krb5_enctype
*enctype
)
4300 *enctype
= crypto
->et
->type
;
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
,
4321 *padsize
= crypto
->et
->padsize
;
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
,
4340 size_t *confoundersize
)
4342 *confoundersize
= crypto
->et
->confoundersize
;
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
);
4365 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
4366 N_("encryption type %d not supported", ""),
4368 return KRB5_PROG_ETYPE_NOSUPP
;
4370 et
->flags
|= F_DISABLED
;
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
);
4392 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
4393 N_("encryption type %d not supported", ""),
4395 return KRB5_PROG_ETYPE_NOSUPP
;
4397 et
->flags
&= ~F_DISABLED
;
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
)
4418 for(i
= 0; i
< num_etypes
; i
++)
4419 if(etypes
[i
]->flags
& F_WEAK
) {
4421 etypes
[i
]->flags
&= ~F_DISABLED
;
4423 etypes
[i
]->flags
|= F_DISABLED
;
4429 krb5_error_code KRB5_LIB_FUNCTION
4430 krb5_string_to_key_derived(krb5_context context
,
4436 struct encryption_type
*et
= _find_enctype(etype
);
4437 krb5_error_code ret
;
4443 krb5_set_error_message (context
, KRB5_PROG_ETYPE_NOSUPP
,
4444 N_("encryption type %d not supported", ""),
4446 return KRB5_PROG_ETYPE_NOSUPP
;
4448 keylen
= et
->keytype
->bits
/ 8;
4451 if(kd
.key
== NULL
) {
4452 krb5_set_error_message (context
, ENOMEM
,
4453 N_("malloc: out of memory", ""));
4456 ret
= krb5_data_alloc(&kd
.key
->keyvalue
, et
->keytype
->size
);
4461 kd
.key
->keytype
= etype
;
4462 tmp
= malloc (keylen
);
4464 krb5_free_keyblock(context
, kd
.key
);
4465 krb5_set_error_message (context
, ENOMEM
, N_("malloc: out of memory", ""));
4468 ret
= _krb5_n_fold(str
, len
, tmp
, keylen
);
4471 krb5_set_error_message (context
, ENOMEM
, N_("malloc: out of memory", ""));
4475 DES3_random_to_key(context
, kd
.key
, tmp
, keylen
);
4476 memset(tmp
, 0, keylen
);
4478 ret
= derive_key(context
,
4481 "kerberos", /* XXX well known constant */
4482 strlen("kerberos"));
4484 free_key_data(context
, &kd
, et
);
4487 ret
= krb5_copy_keyblock_contents(context
, kd
.key
, key
);
4488 free_key_data(context
, &kd
, et
);
4493 wrapped_length (krb5_context context
,
4497 struct encryption_type
*et
= crypto
->et
;
4498 size_t padsize
= et
->padsize
;
4499 size_t checksumsize
= CHECKSUMSIZE(et
->checksum
);
4502 res
= et
->confoundersize
+ checksumsize
+ data_len
;
4503 res
= (res
+ padsize
- 1) / padsize
* padsize
;
4508 wrapped_length_dervied (krb5_context context
,
4512 struct encryption_type
*et
= crypto
->et
;
4513 size_t padsize
= et
->padsize
;
4516 res
= et
->confoundersize
+ data_len
;
4517 res
= (res
+ padsize
- 1) / padsize
* padsize
;
4518 if (et
->keyed_checksum
)
4519 res
+= et
->keyed_checksum
->checksumsize
;
4521 res
+= et
->checksum
->checksumsize
;
4526 * Return the size of an encrypted packet of length `data_len'
4530 krb5_get_wrapped_length (krb5_context context
,
4534 if (derived_crypto (context
, crypto
))
4535 return wrapped_length_dervied (context
, crypto
, data_len
);
4537 return wrapped_length (context
, crypto
, data_len
);
4541 * Return the size of an encrypted packet of length `data_len'
4545 crypto_overhead (krb5_context context
,
4548 struct encryption_type
*et
= crypto
->et
;
4551 res
= CHECKSUMSIZE(et
->checksum
);
4552 res
+= et
->confoundersize
;
4553 if (et
->padsize
> 1)
4559 crypto_overhead_dervied (krb5_context context
,
4562 struct encryption_type
*et
= crypto
->et
;
4565 if (et
->keyed_checksum
)
4566 res
= CHECKSUMSIZE(et
->keyed_checksum
);
4568 res
= CHECKSUMSIZE(et
->checksum
);
4569 res
+= et
->confoundersize
;
4570 if (et
->padsize
> 1)
4576 krb5_crypto_overhead (krb5_context context
, krb5_crypto crypto
)
4578 if (derived_crypto (context
, crypto
))
4579 return crypto_overhead_dervied (context
, crypto
);
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
,
4608 krb5_error_code ret
;
4609 struct encryption_type
*et
= _find_enctype(type
);
4611 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
4612 N_("encryption type %d not supported", ""),
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 "
4621 et
->name
, (int)et
->keytype
->size
);
4622 return KRB5_PROG_ETYPE_NOSUPP
;
4624 ret
= krb5_data_alloc(&key
->keyvalue
, et
->keytype
->size
);
4627 key
->keytype
= type
;
4628 if (et
->keytype
->random_to_key
)
4629 (*et
->keytype
->random_to_key
)(context
, key
, data
, size
);
4631 memcpy(key
->keyvalue
.data
, data
, et
->keytype
->size
);
4637 _krb5_pk_octetstring2key(krb5_context context
,
4641 const heim_octet_string
*c_n
,
4642 const heim_octet_string
*k_n
,
4645 struct encryption_type
*et
= _find_enctype(type
);
4646 krb5_error_code ret
;
4647 size_t keylen
, offset
;
4649 unsigned char counter
;
4650 unsigned char shaoutput
[SHA_DIGEST_LENGTH
];
4654 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
4655 N_("encryption type %d not supported", ""),
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", ""));
4667 m
= EVP_MD_CTX_create();
4670 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
4678 EVP_DigestInit_ex(m
, EVP_sha1(), NULL
);
4679 EVP_DigestUpdate(m
, &counter
, 1);
4680 EVP_DigestUpdate(m
, dhdata
, dhsize
);
4683 EVP_DigestUpdate(m
, c_n
->data
, c_n
->length
);
4685 EVP_DigestUpdate(m
, k_n
->data
, k_n
->length
);
4687 EVP_DigestFinal_ex(m
, shaoutput
, NULL
);
4689 memcpy((unsigned char *)keydata
+ offset
,
4691 min(keylen
- offset
, sizeof(shaoutput
)));
4693 offset
+= sizeof(shaoutput
);
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
));
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
;
4713 pn
.principalName
= p
->name
;
4714 pn
.realm
= p
->realm
;
4716 ASN1_MALLOC_ENCODE(KRB5PrincipalName
, data
->data
, data
->length
,
4719 krb5_data_zero(data
);
4720 krb5_set_error_message(context
, ret
,
4721 N_("Failed to encode KRB5PrincipalName", ""));
4724 if (data
->length
!= size
)
4725 krb5_abortx(context
, "asn1 compiler internal error");
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
,
4740 PkinitSP80056AOtherInfo otherinfo
;
4741 PkinitSuppPubInfo pubinfo
;
4742 krb5_error_code ret
;
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
);
4757 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
4760 if (pub
.length
!= size
)
4761 krb5_abortx(context
, "asn1 compiler internal error");
4763 ret
= encode_uvinfo(context
, client
, &otherinfo
.partyUInfo
);
4768 ret
= encode_uvinfo(context
, server
, &otherinfo
.partyVInfo
);
4770 free(otherinfo
.partyUInfo
.data
);
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
);
4784 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
4787 if (other
->length
!= size
)
4788 krb5_abortx(context
, "asn1 compiler internal error");
4794 _krb5_pk_kdf(krb5_context context
,
4795 const struct AlgorithmIdentifier
*ai
,
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
,
4806 struct encryption_type
*et
;
4807 krb5_error_code ret
;
4809 size_t keylen
, offset
;
4811 unsigned char *keydata
;
4812 unsigned char shaoutput
[SHA_DIGEST_LENGTH
];
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",
4827 return KRB5_PROG_ETYPE_NOSUPP
;
4830 et
= _find_enctype(enctype
);
4832 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
4833 N_("encryption type %d not supported", ""),
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", ""));
4845 ret
= encode_otherinfo(context
, ai
, client
, server
,
4846 enctype
, as_req
, pk_as_rep
, ticket
, &other
);
4852 m
= EVP_MD_CTX_create();
4856 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
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
,
4875 min(keylen
- offset
, sizeof(shaoutput
)));
4877 offset
+= sizeof(shaoutput
);
4879 } while(offset
< keylen
);
4880 memset(shaoutput
, 0, sizeof(shaoutput
));
4882 EVP_MD_CTX_destroy(m
);
4885 ret
= krb5_random_to_key(context
, enctype
, keydata
, keylen
, key
);
4886 memset(keydata
, 0, sizeof(keylen
));
4893 krb5_error_code KRB5_LIB_FUNCTION
4894 krb5_crypto_prf_length(krb5_context context
,
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", ""),
4904 return KRB5_PROG_ETYPE_NOSUPP
;
4907 *length
= et
->prf_length
;
4911 krb5_error_code KRB5_LIB_FUNCTION
4912 krb5_crypto_prf(krb5_context context
,
4913 const krb5_crypto crypto
,
4914 const krb5_data
*input
,
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",
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
,
4938 krb5_error_code ret
;
4940 unsigned char i
= 1;
4943 krb5_data_zero(&input2
);
4944 krb5_data_zero(output
);
4946 krb5_clear_error_message(context
);
4948 ret
= krb5_data_alloc(output
, length
);
4950 ret
= krb5_data_alloc(&input2
, input
->length
+ 1);
4953 krb5_clear_error_message(context
);
4955 memcpy(((unsigned char *)input2
.data
) + 1, input
->data
, input
->length
);
4962 ((unsigned char *)input2
.data
)[0] = i
++;
4964 ret
= krb5_crypto_prf(context
, crypto
, &input2
, &block
);
4968 if (block
.length
< length
) {
4969 memcpy(p
, block
.data
, block
.length
);
4970 length
-= block
.length
;
4972 memcpy(p
, block
.data
, length
);
4976 krb5_data_free(&block
);
4980 krb5_data_free(&input2
);
4982 krb5_data_free(output
);
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
,
5008 krb5_enctype enctype
,
5011 krb5_error_code ret
;
5015 memset(res
, 0, sizeof(*res
));
5017 ret
= krb5_enctype_keysize(context
, enctype
, &keysize
);
5021 ret
= krb5_data_alloc(&res
->keyvalue
, keysize
);
5024 ret
= krb5_crypto_prfplus(context
, crypto1
, pepper1
, keysize
, &os1
);
5027 ret
= krb5_crypto_prfplus(context
, crypto2
, pepper2
, keysize
, &os2
);
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
];
5039 krb5_data_free(&res
->keyvalue
);
5040 krb5_data_free(&os1
);
5041 krb5_data_free(&os2
);
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
,
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)
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");
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
;
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
)
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 */