Debian Codegraph @

lib/krb5/crypto-des3.c

raw
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34 #include "krb5_locl.h"
35
36 /*
37 *
38 */
39
40 static void
41 DES3_random_key(krb5_context context,
42 krb5_keyblock *key)
43 {
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]));
53 }
54
55 static krb5_error_code
56 DES3_prf(krb5_context context,
57 krb5_crypto crypto,
58 const krb5_data *in,
59 krb5_data *out)
60 {
61 struct _krb5_checksum_type *ct = crypto->et->checksum;
62 krb5_error_code ret;
63 Checksum result;
64 krb5_keyblock *derived;
65
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;
71 }
72
73 ret = (*ct->checksum)(context, NULL, in->data, in->length, 0, &result);
74 if (ret) {
75 krb5_data_free(&result.checksum);
76 return ret;
77 }
78
79 if (result.checksum.length < crypto->et->blocksize)
80 krb5_abortx(context, "internal prf error");
81
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");
87
88 ret = krb5_data_alloc(out, crypto->et->prf_length);
89 if (ret)
90 krb5_abortx(context, "malloc failed");
91
92 {
93 const EVP_CIPHER *c = (*crypto->et->keytype->evp)();
94 EVP_CIPHER_CTX ctx;
95
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->prf_length);
100 EVP_CIPHER_CTX_cleanup(&ctx);
101 }
102
103 krb5_data_free(&result.checksum);
104 krb5_free_keyblock(context, derived);
105
106 return ret;
107 }
108
109 #ifdef DES3_OLD_ENCTYPE
110 static struct _krb5_key_type keytype_des3 = {
111 ETYPE_OLD_DES3_CBC_SHA1,
112 "des3",
113 168,
114 24,
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
122 };
123 #endif
124
125 static struct _krb5_key_type keytype_des3_derived = {
126 ETYPE_OLD_DES3_CBC_SHA1,
127 "des3",
128 168,
129 24,
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
137 };
138
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)
147 {
148 return _krb5_des_checksum(context, EVP_md5(), key, data, len, C);
149 }
150
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)
158 {
159 return _krb5_des_verify(context, EVP_md5(), key, data, len, C);
160 }
161
162 struct _krb5_checksum_type _krb5_checksum_rsa_md5_des3 = {
163 CKSUMTYPE_RSA_MD5_DES3,
164 "rsa-md5-des3",
165 64,
166 24,
167 F_KEYED | F_CPROOF | F_VARIANT,
168 RSA_MD5_DES3_checksum,
169 RSA_MD5_DES3_verify
170 };
171 #endif
172
173 struct _krb5_checksum_type _krb5_checksum_hmac_sha1_des3 = {
174 CKSUMTYPE_HMAC_SHA1_DES3,
175 "hmac-sha1-des3",
176 64,
177 20,
178 F_KEYED | F_CPROOF | F_DERIVED,
179 _krb5_SP_HMAC_SHA1_checksum,
180 NULL
181 };
182
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,
188 8,
189 8,
190 8,
191 &keytype_des3,
192 &_krb5_checksum_rsa_md5,
193 &_krb5_checksum_rsa_md5_des3,
194 0,
195 _krb5_evp_encrypt,
196 0,
197 NULL
198 };
199 #endif
200
201 struct _krb5_encryption_type _krb5_enctype_des3_cbc_sha1 = {
202 ETYPE_DES3_CBC_SHA1,
203 "des3-cbc-sha1",
204 NULL,
205 8,
206 8,
207 8,
208 &keytype_des3_derived,
209 &_krb5_checksum_sha1,
210 &_krb5_checksum_hmac_sha1_des3,
211 F_DERIVED | F_RFC3961_ENC | F_RFC3961_KDF,
212 _krb5_evp_encrypt,
213 16,
214 DES3_prf
215 };
216
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,
222 8,
223 8,
224 8,
225 &keytype_des3,
226 &_krb5_checksum_sha1,
227 &_krb5_checksum_hmac_sha1_des3,
228 0,
229 _krb5_evp_encrypt,
230 0,
231 NULL
232 };
233 #endif
234
235 struct _krb5_encryption_type _krb5_enctype_des3_cbc_none = {
236 ETYPE_DES3_CBC_NONE,
237 "des3-cbc-none",
238 NULL,
239 8,
240 8,
241 0,
242 &keytype_des3_derived,
243 &_krb5_checksum_none,
244 NULL,
245 F_PSEUDO,
246 _krb5_evp_encrypt,
247 0,
248 NULL
249 };
250
251 void
252 _krb5_DES3_random_to_key(krb5_context context,
253 krb5_keyblock *key,
254 const void *data,
255 size_t size)
256 {
257 unsigned char *x = key->keyvalue.data;
258 const u_char *q = data;
259 DES_cblock *k;
260 int i, j;
261
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];
267
268 x[8 * i + j] = b;
269 }
270 foo = 0;
271 for (j = 6; j >= 0; --j) {
272 foo |= q[7 * i + j] & 1;
273 foo <<= 1;
274 }
275 x[8 * i + 7] = foo;
276 }
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_xor8(k[i], (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
282 }
283 }