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 #include "krb5_locl.h"
36 #ifdef HEIM_WEAK_CRYPTO
40 krb5_DES_random_key(krb5_context context
,
43 DES_cblock
*k
= key
->keyvalue
.data
;
45 krb5_generate_random_block(k
, sizeof(DES_cblock
));
46 DES_set_odd_parity(k
);
47 } while(DES_is_weak_key(k
));
51 krb5_DES_schedule_old(krb5_context context
,
52 struct _krb5_key_type
*kt
,
53 struct _krb5_key_data
*key
)
55 DES_set_key_unchecked(key
->key
->keyvalue
.data
, key
->schedule
->data
);
59 krb5_DES_random_to_key(krb5_context context
,
64 DES_cblock
*k
= key
->keyvalue
.data
;
65 memcpy(k
, data
, key
->keyvalue
.length
);
66 DES_set_odd_parity(k
);
67 if(DES_is_weak_key(k
))
68 _krb5_xor(k
, (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
71 static struct _krb5_key_type keytype_des_old
= {
76 sizeof(DES_key_schedule
),
78 krb5_DES_schedule_old
,
80 krb5_DES_random_to_key
,
85 static struct _krb5_key_type keytype_des
= {
90 sizeof(struct _krb5_evp_schedule
),
94 krb5_DES_random_to_key
,
99 static krb5_error_code
100 CRC32_checksum(krb5_context context
,
101 struct _krb5_key_data
*key
,
108 unsigned char *r
= C
->checksum
.data
;
109 _krb5_crc_init_table ();
110 crc
= _krb5_crc_update (data
, len
, 0);
112 r
[1] = (crc
>> 8) & 0xff;
113 r
[2] = (crc
>> 16) & 0xff;
114 r
[3] = (crc
>> 24) & 0xff;
118 static krb5_error_code
119 RSA_MD4_checksum(krb5_context context
,
120 struct _krb5_key_data
*key
,
126 if (EVP_Digest(data
, len
, C
->checksum
.data
, NULL
, EVP_md4(), NULL
) != 1)
127 krb5_abortx(context
, "md4 checksum failed");
131 static krb5_error_code
132 RSA_MD4_DES_checksum(krb5_context context
,
133 struct _krb5_key_data
*key
,
139 return _krb5_des_checksum(context
, EVP_md4(), key
, data
, len
, cksum
);
142 static krb5_error_code
143 RSA_MD4_DES_verify(krb5_context context
,
144 struct _krb5_key_data
*key
,
150 return _krb5_des_verify(context
, EVP_md4(), key
, data
, len
, C
);
153 static krb5_error_code
154 RSA_MD5_DES_checksum(krb5_context context
,
155 struct _krb5_key_data
*key
,
161 return _krb5_des_checksum(context
, EVP_md5(), key
, data
, len
, C
);
164 static krb5_error_code
165 RSA_MD5_DES_verify(krb5_context context
,
166 struct _krb5_key_data
*key
,
172 return _krb5_des_verify(context
, EVP_md5(), key
, data
, len
, C
);
175 struct _krb5_checksum_type _krb5_checksum_crc32
= {
185 struct _krb5_checksum_type _krb5_checksum_rsa_md4
= {
195 struct _krb5_checksum_type _krb5_checksum_rsa_md4_des
= {
196 CKSUMTYPE_RSA_MD4_DES
,
200 F_KEYED
| F_CPROOF
| F_VARIANT
,
201 RSA_MD4_DES_checksum
,
205 struct _krb5_checksum_type _krb5_checksum_rsa_md5_des
= {
206 CKSUMTYPE_RSA_MD5_DES
,
210 F_KEYED
| F_CPROOF
| F_VARIANT
,
211 RSA_MD5_DES_checksum
,
215 static krb5_error_code
216 evp_des_encrypt_null_ivec(krb5_context context
,
217 struct _krb5_key_data
*key
,
220 krb5_boolean encryptp
,
224 struct _krb5_evp_schedule
*ctx
= key
->schedule
->data
;
227 memset(&ivec
, 0, sizeof(ivec
));
228 c
= encryptp
? &ctx
->ectx
: &ctx
->dctx
;
229 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, (void *)&ivec
, -1);
230 EVP_Cipher(c
, data
, data
, len
);
234 static krb5_error_code
235 evp_des_encrypt_key_ivec(krb5_context context
,
236 struct _krb5_key_data
*key
,
239 krb5_boolean encryptp
,
243 struct _krb5_evp_schedule
*ctx
= key
->schedule
->data
;
246 memcpy(&ivec
, key
->key
->keyvalue
.data
, sizeof(ivec
));
247 c
= encryptp
? &ctx
->ectx
: &ctx
->dctx
;
248 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, (void *)&ivec
, -1);
249 EVP_Cipher(c
, data
, data
, len
);
253 static krb5_error_code
254 DES_CFB64_encrypt_null_ivec(krb5_context context
,
255 struct _krb5_key_data
*key
,
258 krb5_boolean encryptp
,
264 DES_key_schedule
*s
= key
->schedule
->data
;
265 memset(&ivec
, 0, sizeof(ivec
));
267 DES_cfb64_encrypt(data
, data
, len
, s
, &ivec
, &num
, encryptp
);
271 static krb5_error_code
272 DES_PCBC_encrypt_key_ivec(krb5_context context
,
273 struct _krb5_key_data
*key
,
276 krb5_boolean encryptp
,
281 DES_key_schedule
*s
= key
->schedule
->data
;
282 memcpy(&ivec
, key
->key
->keyvalue
.data
, sizeof(ivec
));
284 DES_pcbc_encrypt(data
, data
, len
, s
, &ivec
, encryptp
);
288 struct _krb5_encryption_type _krb5_enctype_des_cbc_crc
= {
296 &_krb5_checksum_crc32
,
299 evp_des_encrypt_key_ivec
,
304 struct _krb5_encryption_type _krb5_enctype_des_cbc_md4
= {
312 &_krb5_checksum_rsa_md4
,
313 &_krb5_checksum_rsa_md4_des
,
315 evp_des_encrypt_null_ivec
,
320 struct _krb5_encryption_type _krb5_enctype_des_cbc_md5
= {
328 &_krb5_checksum_rsa_md5
,
329 &_krb5_checksum_rsa_md5_des
,
331 evp_des_encrypt_null_ivec
,
336 struct _krb5_encryption_type _krb5_enctype_des_cbc_none
= {
344 &_krb5_checksum_none
,
346 F_PSEUDO
|F_DISABLED
|F_WEAK
,
347 evp_des_encrypt_null_ivec
,
352 struct _krb5_encryption_type _krb5_enctype_des_cfb64_none
= {
353 ETYPE_DES_CFB64_NONE
,
360 &_krb5_checksum_none
,
362 F_PSEUDO
|F_DISABLED
|F_WEAK
,
363 DES_CFB64_encrypt_null_ivec
,
368 struct _krb5_encryption_type _krb5_enctype_des_pcbc_none
= {
376 &_krb5_checksum_none
,
378 F_PSEUDO
|F_DISABLED
|F_WEAK
,
379 DES_PCBC_encrypt_key_ivec
,
383 #endif /* HEIM_WEAK_CRYPTO */