1 /* $NetBSD: crypto-des.c,v 1.1.1.2 2014/04/24 12:45:49 pettai Exp $ */
4 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
38 #ifdef HEIM_WEAK_CRYPTO
42 krb5_DES_random_key(krb5_context context
,
45 DES_cblock
*k
= key
->keyvalue
.data
;
47 krb5_generate_random_block(k
, sizeof(DES_cblock
));
48 DES_set_odd_parity(k
);
49 } while(DES_is_weak_key(k
));
53 krb5_DES_schedule_old(krb5_context context
,
54 struct _krb5_key_type
*kt
,
55 struct _krb5_key_data
*key
)
57 DES_set_key_unchecked(key
->key
->keyvalue
.data
, key
->schedule
->data
);
61 krb5_DES_random_to_key(krb5_context context
,
66 DES_cblock
*k
= key
->keyvalue
.data
;
67 memcpy(k
, data
, key
->keyvalue
.length
);
68 DES_set_odd_parity(k
);
69 if(DES_is_weak_key(k
))
70 _krb5_xor(k
, (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
73 static struct _krb5_key_type keytype_des_old
= {
78 sizeof(DES_key_schedule
),
80 krb5_DES_schedule_old
,
82 krb5_DES_random_to_key
,
87 static struct _krb5_key_type keytype_des
= {
92 sizeof(struct _krb5_evp_schedule
),
96 krb5_DES_random_to_key
,
101 static krb5_error_code
102 CRC32_checksum(krb5_context context
,
103 struct _krb5_key_data
*key
,
110 unsigned char *r
= C
->checksum
.data
;
111 _krb5_crc_init_table ();
112 crc
= _krb5_crc_update (data
, len
, 0);
114 r
[1] = (crc
>> 8) & 0xff;
115 r
[2] = (crc
>> 16) & 0xff;
116 r
[3] = (crc
>> 24) & 0xff;
120 static krb5_error_code
121 RSA_MD4_checksum(krb5_context context
,
122 struct _krb5_key_data
*key
,
128 if (EVP_Digest(data
, len
, C
->checksum
.data
, NULL
, EVP_md4(), NULL
) != 1)
129 krb5_abortx(context
, "md4 checksum failed");
133 static krb5_error_code
134 RSA_MD4_DES_checksum(krb5_context context
,
135 struct _krb5_key_data
*key
,
141 return _krb5_des_checksum(context
, EVP_md4(), key
, data
, len
, cksum
);
144 static krb5_error_code
145 RSA_MD4_DES_verify(krb5_context context
,
146 struct _krb5_key_data
*key
,
152 return _krb5_des_verify(context
, EVP_md4(), key
, data
, len
, C
);
155 static krb5_error_code
156 RSA_MD5_DES_checksum(krb5_context context
,
157 struct _krb5_key_data
*key
,
163 return _krb5_des_checksum(context
, EVP_md5(), key
, data
, len
, C
);
166 static krb5_error_code
167 RSA_MD5_DES_verify(krb5_context context
,
168 struct _krb5_key_data
*key
,
174 return _krb5_des_verify(context
, EVP_md5(), key
, data
, len
, C
);
177 struct _krb5_checksum_type _krb5_checksum_crc32
= {
187 struct _krb5_checksum_type _krb5_checksum_rsa_md4
= {
197 struct _krb5_checksum_type _krb5_checksum_rsa_md4_des
= {
198 CKSUMTYPE_RSA_MD4_DES
,
202 F_KEYED
| F_CPROOF
| F_VARIANT
,
203 RSA_MD4_DES_checksum
,
207 struct _krb5_checksum_type _krb5_checksum_rsa_md5_des
= {
208 CKSUMTYPE_RSA_MD5_DES
,
212 F_KEYED
| F_CPROOF
| F_VARIANT
,
213 RSA_MD5_DES_checksum
,
217 static krb5_error_code
218 evp_des_encrypt_null_ivec(krb5_context context
,
219 struct _krb5_key_data
*key
,
222 krb5_boolean encryptp
,
226 struct _krb5_evp_schedule
*ctx
= key
->schedule
->data
;
229 memset(&ivec
, 0, sizeof(ivec
));
230 c
= encryptp
? &ctx
->ectx
: &ctx
->dctx
;
231 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, (void *)&ivec
, -1);
232 EVP_Cipher(c
, data
, data
, len
);
236 static krb5_error_code
237 evp_des_encrypt_key_ivec(krb5_context context
,
238 struct _krb5_key_data
*key
,
241 krb5_boolean encryptp
,
245 struct _krb5_evp_schedule
*ctx
= key
->schedule
->data
;
248 memcpy(&ivec
, key
->key
->keyvalue
.data
, sizeof(ivec
));
249 c
= encryptp
? &ctx
->ectx
: &ctx
->dctx
;
250 EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, (void *)&ivec
, -1);
251 EVP_Cipher(c
, data
, data
, len
);
255 static krb5_error_code
256 DES_CFB64_encrypt_null_ivec(krb5_context context
,
257 struct _krb5_key_data
*key
,
260 krb5_boolean encryptp
,
266 DES_key_schedule
*s
= key
->schedule
->data
;
267 memset(&ivec
, 0, sizeof(ivec
));
269 DES_cfb64_encrypt(data
, data
, len
, s
, &ivec
, &num
, encryptp
);
273 static krb5_error_code
274 DES_PCBC_encrypt_key_ivec(krb5_context context
,
275 struct _krb5_key_data
*key
,
278 krb5_boolean encryptp
,
283 DES_key_schedule
*s
= key
->schedule
->data
;
284 memcpy(&ivec
, key
->key
->keyvalue
.data
, sizeof(ivec
));
286 DES_pcbc_encrypt(data
, data
, len
, s
, &ivec
, encryptp
);
290 struct _krb5_encryption_type _krb5_enctype_des_cbc_crc
= {
297 &_krb5_checksum_crc32
,
300 evp_des_encrypt_key_ivec
,
305 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
= {
327 &_krb5_checksum_rsa_md5
,
328 &_krb5_checksum_rsa_md5_des
,
330 evp_des_encrypt_null_ivec
,
335 struct _krb5_encryption_type _krb5_enctype_des_cbc_none
= {
342 &_krb5_checksum_none
,
344 F_PSEUDO
|F_DISABLED
|F_WEAK
,
345 evp_des_encrypt_null_ivec
,
350 struct _krb5_encryption_type _krb5_enctype_des_cfb64_none
= {
351 ETYPE_DES_CFB64_NONE
,
357 &_krb5_checksum_none
,
359 F_PSEUDO
|F_DISABLED
|F_WEAK
,
360 DES_CFB64_encrypt_null_ivec
,
365 struct _krb5_encryption_type _krb5_enctype_des_pcbc_none
= {
372 &_krb5_checksum_none
,
374 F_PSEUDO
|F_DISABLED
|F_WEAK
,
375 DES_PCBC_encrypt_key_ivec
,
379 #endif /* HEIM_WEAK_CRYPTO */