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"
41 DES3_random_key(krb5_context context
,
44 DES_cblock
*k
= key
->keyvalue
.data
;
46 krb5_generate_random_block(k
, 3 * sizeof(DES_cblock
));
47 DES_set_odd_parity(&k
[0]);
48 DES_set_odd_parity(&k
[1]);
49 DES_set_odd_parity(&k
[2]);
50 } while(DES_is_weak_key(&k
[0]) ||
51 DES_is_weak_key(&k
[1]) ||
52 DES_is_weak_key(&k
[2]));
55 static krb5_error_code
56 DES3_prf(krb5_context context
,
61 struct _krb5_checksum_type
*ct
= crypto
->et
->checksum
;
64 krb5_keyblock
*derived
;
66 result
.cksumtype
= ct
->type
;
67 ret
= krb5_data_alloc(&result
.checksum
, ct
->checksumsize
);
69 krb5_set_error_message(context
, ret
, N_("malloc: out memory", ""));
73 ret
= (*ct
->checksum
)(context
, NULL
, in
->data
, in
->length
, 0, &result
);
75 krb5_data_free(&result
.checksum
);
79 if (result
.checksum
.length
< crypto
->et
->blocksize
)
80 krb5_abortx(context
, "internal prf error");
83 ret
= krb5_derive_key(context
, crypto
->key
.key
,
84 crypto
->et
->type
, "prf", 3, &derived
);
86 krb5_abortx(context
, "krb5_derive_key");
88 ret
= krb5_data_alloc(out
, crypto
->et
->blocksize
);
90 krb5_abortx(context
, "malloc failed");
93 const EVP_CIPHER
*c
= (*crypto
->et
->keytype
->evp
)();
96 EVP_CIPHER_CTX_init(&ctx
); /* ivec all zero */
97 EVP_CipherInit_ex(&ctx
, c
, NULL
, derived
->keyvalue
.data
, NULL
, 1);
98 EVP_Cipher(&ctx
, out
->data
, result
.checksum
.data
,
99 crypto
->et
->blocksize
);
100 EVP_CIPHER_CTX_cleanup(&ctx
);
103 krb5_data_free(&result
.checksum
);
104 krb5_free_keyblock(context
, derived
);
109 #ifdef DES3_OLD_ENCTYPE
110 static struct _krb5_key_type keytype_des3
= {
111 ETYPE_OLD_DES3_CBC_SHA1
,
115 sizeof(struct _krb5_evp_schedule
),
119 _krb5_DES3_random_to_key
,
125 static struct _krb5_key_type keytype_des3_derived
= {
126 ETYPE_OLD_DES3_CBC_SHA1
,
130 sizeof(struct _krb5_evp_schedule
),
133 _krb5_des3_salt_derived
,
134 _krb5_DES3_random_to_key
,
139 #ifdef DES3_OLD_ENCTYPE
140 static krb5_error_code
141 RSA_MD5_DES3_checksum(krb5_context context
,
142 struct _krb5_key_data
*key
,
148 return _krb5_des_checksum(context
, EVP_md5(), key
, data
, len
, C
);
151 static krb5_error_code
152 RSA_MD5_DES3_verify(krb5_context context
,
153 struct _krb5_key_data
*key
,
159 return _krb5_des_verify(context
, EVP_md5(), key
, data
, len
, C
);
162 struct _krb5_checksum_type _krb5_checksum_rsa_md5_des3
= {
163 CKSUMTYPE_RSA_MD5_DES3
,
167 F_KEYED
| F_CPROOF
| F_VARIANT
,
168 RSA_MD5_DES3_checksum
,
173 struct _krb5_checksum_type _krb5_checksum_hmac_sha1_des3
= {
174 CKSUMTYPE_HMAC_SHA1_DES3
,
178 F_KEYED
| F_CPROOF
| F_DERIVED
,
179 _krb5_SP_HMAC_SHA1_checksum
,
183 #ifdef DES3_OLD_ENCTYPE
184 struct _krb5_encryption_type _krb5_enctype_des3_cbc_md5
= {
192 &_krb5_checksum_rsa_md5
,
193 &_krb5_checksum_rsa_md5_des3
,
201 struct _krb5_encryption_type _krb5_enctype_des3_cbc_sha1
= {
208 &keytype_des3_derived
,
209 &_krb5_checksum_sha1
,
210 &_krb5_checksum_hmac_sha1_des3
,
217 #ifdef DES3_OLD_ENCTYPE
218 struct _krb5_encryption_type _krb5_enctype_old_des3_cbc_sha1
= {
219 ETYPE_OLD_DES3_CBC_SHA1
,
226 &_krb5_checksum_sha1
,
227 &_krb5_checksum_hmac_sha1_des3
,
235 struct _krb5_encryption_type _krb5_enctype_des3_cbc_none
= {
242 &keytype_des3_derived
,
243 &_krb5_checksum_none
,
252 _krb5_DES3_random_to_key(krb5_context context
,
257 unsigned char *x
= key
->keyvalue
.data
;
258 const u_char
*q
= data
;
262 memset(key
->keyvalue
.data
, 0, key
->keyvalue
.length
);
263 for (i
= 0; i
< 3; ++i
) {
265 for (j
= 0; j
< 7; ++j
) {
266 unsigned char b
= q
[7 * i
+ j
];
271 for (j
= 6; j
>= 0; --j
) {
272 foo
|= q
[7 * i
+ j
] & 1;
277 k
= key
->keyvalue
.data
;
278 for (i
= 0; i
< 3; i
++) {
279 DES_set_odd_parity(&k
[i
]);
280 if(DES_is_weak_key(&k
[i
]))
281 _krb5_xor(&k
[i
], (const unsigned char*)"\0\0\0\0\0\0\0\xf0");