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#ifdef DES3_OLD_ENCTYPE
37static krb5_error_code
38DES3_string_to_key(krb5_context context,
39		   krb5_enctype enctype,
40		   krb5_data password,
41		   krb5_salt salt,
42		   krb5_data opaque,
43		   krb5_keyblock *key)
44{
45    char *str;
46    size_t len;
47    unsigned char tmp[24];
48    DES_cblock keys[3];
49    krb5_error_code ret;
50
51    len = password.length + salt.saltvalue.length;
52    str = malloc(len);
53    if(len != 0 && str == NULL) {
54	krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
55	return ENOMEM;
56    }
57    memcpy(str, password.data, password.length);
58    memcpy(str + password.length, salt.saltvalue.data, salt.saltvalue.length);
59    {
60	DES_cblock ivec;
61	DES_key_schedule s[3];
62	int i;
63
64	ret = _krb5_n_fold(str, len, tmp, 24);
65	if (ret) {
66	    memset(str, 0, len);
67	    free(str);
68	    krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
69	    return ret;
70	}
71
72	for(i = 0; i < 3; i++){
73	    memcpy(keys + i, tmp + i * 8, sizeof(keys[i]));
74	    CCDesSetOddParity(keys + i, sizeof(keys[i]));
75	    if(CCDesIsWeakKey(keys + i, sizeof(keys[i])))
76		_krb5_xor((void *)(keys + i),
77			  (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
78	    DES_set_key_unchecked(keys + i, &s[i]);
79	}
80	memset(&ivec, 0, sizeof(ivec));
81	DES_ede3_cbc_encrypt(tmp,
82			     tmp, sizeof(tmp),
83			     &s[0], &s[1], &s[2], &ivec, DES_ENCRYPT);
84	memset(s, 0, sizeof(s));
85	memset(&ivec, 0, sizeof(ivec));
86	for(i = 0; i < 3; i++){
87	    memcpy(keys + i, tmp + i * 8, sizeof(keys[i]));
88	    CCDesSetOddParity(keys + i, sizeof(keys[i]));
89	    if(CCDesIsWeakKey(keys + i, sizeof(keys[i])))
90		_krb5_xor((void *)(keys + i),
91			  (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
92	}
93	memset(tmp, 0, sizeof(tmp));
94    }
95    key->keytype = enctype;
96    krb5_data_copy(&key->keyvalue, keys, sizeof(keys));
97    memset(keys, 0, sizeof(keys));
98    memset(str, 0, len);
99    free(str);
100    return 0;
101}
102#endif
103
104static krb5_error_code
105DES3_string_to_key_derived(krb5_context context,
106			   krb5_enctype enctype,
107			   krb5_data password,
108			   krb5_salt salt,
109			   krb5_data opaque,
110			   krb5_keyblock *key)
111{
112    krb5_error_code ret;
113    size_t len = password.length + salt.saltvalue.length;
114    char *s;
115
116    s = malloc(len);
117    if(len != 0 && s == NULL) {
118	krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
119	return ENOMEM;
120    }
121    memcpy(s, password.data, password.length);
122    memcpy(s + password.length, salt.saltvalue.data, salt.saltvalue.length);
123    ret = krb5_string_to_key_derived(context,
124				     s,
125				     len,
126				     enctype,
127				     key);
128    memset(s, 0, len);
129    free(s);
130    return ret;
131}
132
133
134#ifdef DES3_OLD_ENCTYPE
135struct salt_type _krb5_des3_salt[] = {
136    {
137	KRB5_PW_SALT,
138	"pw-salt",
139	DES3_string_to_key
140    },
141    { 0 }
142};
143#endif
144
145struct salt_type _krb5_des3_salt_derived[] = {
146    {
147	KRB5_PW_SALT,
148	"pw-salt",
149	DES3_string_to_key_derived
150    },
151    { 0 }
152};
153