Fix racy file ccache corruption in cred_delete()
[heimdal.git] / lib / krb5 / crypto-des3.c
blob5cbe4c05ae32f2e368d6ce38e4aad45438bfd53e
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
40 static void
41 DES3_random_key(krb5_context context,
42 krb5_keyblock *key)
44 DES_cblock *k = key->keyvalue.data;
45 do {
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,
57 krb5_crypto crypto,
58 const krb5_data *in,
59 krb5_data *out)
61 struct _krb5_checksum_type *ct = crypto->et->checksum;
62 krb5_error_code ret;
63 Checksum result;
64 krb5_keyblock *derived;
66 result.cksumtype = ct->type;
67 ret = krb5_data_alloc(&result.checksum, ct->checksumsize);
68 if (ret) {
69 krb5_set_error_message(context, ret, N_("malloc: out memory", ""));
70 return ret;
73 ret = (*ct->checksum)(context, NULL, in->data, in->length, 0, &result);
74 if (ret) {
75 krb5_data_free(&result.checksum);
76 return ret;
79 if (result.checksum.length < crypto->et->blocksize)
80 krb5_abortx(context, "internal prf error");
82 derived = NULL;
83 ret = krb5_derive_key(context, crypto->key.key,
84 crypto->et->type, "prf", 3, &derived);
85 if (ret)
86 krb5_abortx(context, "krb5_derive_key");
88 ret = krb5_data_alloc(out, crypto->et->blocksize);
89 if (ret)
90 krb5_abortx(context, "malloc failed");
93 const EVP_CIPHER *c = (*crypto->et->keytype->evp)();
94 EVP_CIPHER_CTX ctx;
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);
106 return ret;
109 #ifdef DES3_OLD_ENCTYPE
110 static struct _krb5_key_type keytype_des3 = {
111 ETYPE_OLD_DES3_CBC_SHA1,
112 "des3",
113 168,
115 sizeof(struct _krb5_evp_schedule),
116 DES3_random_key,
117 _krb5_evp_schedule,
118 _krb5_des3_salt,
119 _krb5_DES3_random_to_key,
120 _krb5_evp_cleanup,
121 EVP_des_ede3_cbc
123 #endif
125 static struct _krb5_key_type keytype_des3_derived = {
126 ETYPE_OLD_DES3_CBC_SHA1,
127 "des3",
128 168,
130 sizeof(struct _krb5_evp_schedule),
131 DES3_random_key,
132 _krb5_evp_schedule,
133 _krb5_des3_salt_derived,
134 _krb5_DES3_random_to_key,
135 _krb5_evp_cleanup,
136 EVP_des_ede3_cbc
139 #ifdef DES3_OLD_ENCTYPE
140 static krb5_error_code
141 RSA_MD5_DES3_checksum(krb5_context context,
142 struct _krb5_key_data *key,
143 const void *data,
144 size_t len,
145 unsigned usage,
146 Checksum *C)
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,
154 const void *data,
155 size_t len,
156 unsigned usage,
157 Checksum *C)
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,
164 "rsa-md5-des3",
167 F_KEYED | F_CPROOF | F_VARIANT,
168 RSA_MD5_DES3_checksum,
169 RSA_MD5_DES3_verify
171 #endif
173 struct _krb5_checksum_type _krb5_checksum_hmac_sha1_des3 = {
174 CKSUMTYPE_HMAC_SHA1_DES3,
175 "hmac-sha1-des3",
178 F_KEYED | F_CPROOF | F_DERIVED,
179 _krb5_SP_HMAC_SHA1_checksum,
180 NULL
183 #ifdef DES3_OLD_ENCTYPE
184 struct _krb5_encryption_type _krb5_enctype_des3_cbc_md5 = {
185 ETYPE_DES3_CBC_MD5,
186 "des3-cbc-md5",
187 NULL,
191 &keytype_des3,
192 &_krb5_checksum_rsa_md5,
193 &_krb5_checksum_rsa_md5_des3,
195 _krb5_evp_encrypt,
197 NULL
199 #endif
201 struct _krb5_encryption_type _krb5_enctype_des3_cbc_sha1 = {
202 ETYPE_DES3_CBC_SHA1,
203 "des3-cbc-sha1",
204 NULL,
208 &keytype_des3_derived,
209 &_krb5_checksum_sha1,
210 &_krb5_checksum_hmac_sha1_des3,
211 F_DERIVED,
212 _krb5_evp_encrypt,
214 DES3_prf
217 #ifdef DES3_OLD_ENCTYPE
218 struct _krb5_encryption_type _krb5_enctype_old_des3_cbc_sha1 = {
219 ETYPE_OLD_DES3_CBC_SHA1,
220 "old-des3-cbc-sha1",
221 NULL,
225 &keytype_des3,
226 &_krb5_checksum_sha1,
227 &_krb5_checksum_hmac_sha1_des3,
229 _krb5_evp_encrypt,
231 NULL
233 #endif
235 struct _krb5_encryption_type _krb5_enctype_des3_cbc_none = {
236 ETYPE_DES3_CBC_NONE,
237 "des3-cbc-none",
238 NULL,
242 &keytype_des3_derived,
243 &_krb5_checksum_none,
244 NULL,
245 F_PSEUDO,
246 _krb5_evp_encrypt,
248 NULL
251 void
252 _krb5_DES3_random_to_key(krb5_context context,
253 krb5_keyblock *key,
254 const void *data,
255 size_t size)
257 unsigned char *x = key->keyvalue.data;
258 const u_char *q = data;
259 DES_cblock *k;
260 int i, j;
262 memset(key->keyvalue.data, 0, key->keyvalue.length);
263 for (i = 0; i < 3; ++i) {
264 unsigned char foo;
265 for (j = 0; j < 7; ++j) {
266 unsigned char b = q[7 * i + j];
268 x[8 * i + j] = b;
270 foo = 0;
271 for (j = 6; j >= 0; --j) {
272 foo |= q[7 * i + j] & 1;
273 foo <<= 1;
275 x[8 * i + 7] = foo;
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");