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_xor8(*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
,
102 struct _krb5_key_data
*key
,
104 const struct krb5_crypto_iov
*iov
,
109 unsigned char *r
= C
->checksum
.data
;
112 _krb5_crc_init_table ();
114 for (i
= 0; i
< niov
; i
++) {
115 if (_krb5_crypto_iov_should_sign(&iov
[i
]))
116 crc
= _krb5_crc_update(iov
[i
].data
.data
, iov
[i
].data
.length
, crc
);
120 r
[1] = (crc
>> 8) & 0xff;
121 r
[2] = (crc
>> 16) & 0xff;
122 r
[3] = (crc
>> 24) & 0xff;
126 static krb5_error_code
127 RSA_MD4_checksum(krb5_context context
,
129 struct _krb5_key_data
*key
,
131 const struct krb5_crypto_iov
*iov
,
135 if (_krb5_evp_digest_iov(crypto
, iov
, niov
, C
->checksum
.data
,
136 NULL
, EVP_md4(), NULL
) != 1)
137 krb5_abortx(context
, "md4 checksum failed");
141 static krb5_error_code
142 RSA_MD4_DES_checksum(krb5_context context
,
144 struct _krb5_key_data
*key
,
146 const struct krb5_crypto_iov
*iov
,
150 return _krb5_des_checksum(context
, EVP_md4(), key
, iov
, niov
, cksum
);
153 static krb5_error_code
154 RSA_MD4_DES_verify(krb5_context context
,
156 struct _krb5_key_data
*key
,
158 const struct krb5_crypto_iov
*iov
,
162 return _krb5_des_verify(context
, EVP_md4(), key
, iov
, niov
, C
);
165 static krb5_error_code
166 RSA_MD5_DES_checksum(krb5_context context
,
168 struct _krb5_key_data
*key
,
170 const struct krb5_crypto_iov
*iov
,
174 return _krb5_des_checksum(context
, EVP_md5(), key
, iov
, niov
, C
);
177 static krb5_error_code
178 RSA_MD5_DES_verify(krb5_context context
,
180 struct _krb5_key_data
*key
,
182 const struct krb5_crypto_iov
*iov
,
186 return _krb5_des_verify(context
, EVP_md5(), key
, iov
, niov
, C
);
189 struct _krb5_checksum_type _krb5_checksum_crc32
= {
199 struct _krb5_checksum_type _krb5_checksum_rsa_md4
= {
209 struct _krb5_checksum_type _krb5_checksum_rsa_md4_des
= {
210 CKSUMTYPE_RSA_MD4_DES
,
214 F_KEYED
| F_CPROOF
| F_VARIANT
,
215 RSA_MD4_DES_checksum
,
219 struct _krb5_checksum_type _krb5_checksum_rsa_md5_des
= {
220 CKSUMTYPE_RSA_MD5_DES
,
224 F_KEYED
| F_CPROOF
| F_VARIANT
,
225 RSA_MD5_DES_checksum
,
229 static krb5_error_code
230 evp_des_encrypt_null_ivec(krb5_context context
,
231 struct _krb5_key_data
*key
,
234 krb5_boolean encryptp
,
238 struct _krb5_evp_schedule
*ctx
= key
->schedule
->data
;
241 memset(&ivec
, 0, sizeof(ivec
));
242 c
= encryptp
? &ctx
->ectx
: &ctx
->dctx
;
243 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, (void *)&ivec
, -1);
244 EVP_Cipher(c
, data
, data
, len
);
248 static krb5_error_code
249 evp_des_encrypt_key_ivec(krb5_context context
,
250 struct _krb5_key_data
*key
,
253 krb5_boolean encryptp
,
257 struct _krb5_evp_schedule
*ctx
= key
->schedule
->data
;
260 memcpy(&ivec
, key
->key
->keyvalue
.data
, sizeof(ivec
));
261 c
= encryptp
? &ctx
->ectx
: &ctx
->dctx
;
262 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, (void *)&ivec
, -1);
263 EVP_Cipher(c
, data
, data
, len
);
267 static krb5_error_code
268 DES_CFB64_encrypt_null_ivec(krb5_context context
,
269 struct _krb5_key_data
*key
,
272 krb5_boolean encryptp
,
278 DES_key_schedule
*s
= key
->schedule
->data
;
279 memset(&ivec
, 0, sizeof(ivec
));
281 DES_cfb64_encrypt(data
, data
, len
, s
, &ivec
, &num
, encryptp
);
285 static krb5_error_code
286 DES_PCBC_encrypt_key_ivec(krb5_context context
,
287 struct _krb5_key_data
*key
,
290 krb5_boolean encryptp
,
295 DES_key_schedule
*s
= key
->schedule
->data
;
296 memcpy(&ivec
, key
->key
->keyvalue
.data
, sizeof(ivec
));
298 DES_pcbc_encrypt(data
, data
, len
, s
, &ivec
, encryptp
);
302 struct _krb5_encryption_type _krb5_enctype_des_cbc_crc
= {
310 &_krb5_checksum_crc32
,
312 F_DISABLED
|F_WEAK
|F_OLD
,
313 evp_des_encrypt_key_ivec
,
319 struct _krb5_encryption_type _krb5_enctype_des_cbc_md4
= {
327 &_krb5_checksum_rsa_md4
,
328 &_krb5_checksum_rsa_md4_des
,
329 F_DISABLED
|F_WEAK
|F_OLD
,
330 evp_des_encrypt_null_ivec
,
336 struct _krb5_encryption_type _krb5_enctype_des_cbc_md5
= {
344 &_krb5_checksum_rsa_md5
,
345 &_krb5_checksum_rsa_md5_des
,
346 F_DISABLED
|F_WEAK
|F_OLD
,
347 evp_des_encrypt_null_ivec
,
353 struct _krb5_encryption_type _krb5_enctype_des_cbc_none
= {
361 &_krb5_checksum_none
,
363 F_PSEUDO
|F_DISABLED
|F_WEAK
|F_OLD
,
364 evp_des_encrypt_null_ivec
,
370 struct _krb5_encryption_type _krb5_enctype_des_cfb64_none
= {
371 ETYPE_DES_CFB64_NONE
,
378 &_krb5_checksum_none
,
380 F_PSEUDO
|F_DISABLED
|F_WEAK
|F_OLD
,
381 DES_CFB64_encrypt_null_ivec
,
387 struct _krb5_encryption_type _krb5_enctype_des_pcbc_none
= {
395 &_krb5_checksum_none
,
397 F_PSEUDO
|F_DISABLED
|F_WEAK
|F_OLD
,
398 DES_PCBC_encrypt_key_ivec
,
403 #endif /* HEIM_WEAK_CRYPTO */