xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/salt-des3.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1 /*	$NetBSD: salt-des3.c,v 1.4 2023/06/19 21:41:44 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 #include "krb5_locl.h"
37 
38 #ifdef DES3_OLD_ENCTYPE
39 static krb5_error_code
DES3_string_to_key(krb5_context context,krb5_enctype enctype,krb5_data password,krb5_salt salt,krb5_data opaque,krb5_keyblock * key)40 DES3_string_to_key(krb5_context context,
41 		   krb5_enctype enctype,
42 		   krb5_data password,
43 		   krb5_salt salt,
44 		   krb5_data opaque,
45 		   krb5_keyblock *key)
46 {
47     char *str;
48     size_t len;
49     unsigned char tmp[24];
50     DES_cblock keys[3];
51     krb5_error_code ret;
52 
53     len = password.length + salt.saltvalue.length;
54     str = malloc(len);
55     if (len != 0 && str == NULL)
56 	return krb5_enomem(context);
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_s(str, len, 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 	    DES_set_odd_parity(keys + i);
75 	    if(DES_is_weak_key(keys + i))
76 		_krb5_xor8(*(keys + i), (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
77 	    DES_set_key_unchecked(keys + i, &s[i]);
78 	}
79 	memset_s(&ivec, sizeof(ivec), 0, sizeof(ivec));
80 	DES_ede3_cbc_encrypt(tmp,
81 			     tmp, sizeof(tmp),
82 			     &s[0], &s[1], &s[2], &ivec, DES_ENCRYPT);
83 	memset_s(s, sizeof(s), 0, sizeof(s));
84 	memset_s(&ivec, sizeof(ivec), 0, sizeof(ivec));
85 	for(i = 0; i < 3; i++){
86 	    memcpy(keys + i, tmp + i * 8, sizeof(keys[i]));
87 	    DES_set_odd_parity(keys + i);
88 	    if(DES_is_weak_key(keys + i))
89 		_krb5_xor8(*(keys + i), (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
90 	}
91 	memset_s(tmp, sizeof(tmp), 0, sizeof(tmp));
92     }
93     key->keytype = enctype;
94     krb5_data_copy(&key->keyvalue, keys, sizeof(keys));
95     memset_s(keys, sizeof(keys), 0, sizeof(keys));
96     memset_s(str, len, 0, len);
97     free(str);
98     return 0;
99 }
100 #endif
101 
102 static krb5_error_code
DES3_string_to_key_derived(krb5_context context,krb5_enctype enctype,krb5_data password,krb5_salt salt,krb5_data opaque,krb5_keyblock * key)103 DES3_string_to_key_derived(krb5_context context,
104 			   krb5_enctype enctype,
105 			   krb5_data password,
106 			   krb5_salt salt,
107 			   krb5_data opaque,
108 			   krb5_keyblock *key)
109 {
110     krb5_error_code ret;
111     size_t len = password.length + salt.saltvalue.length;
112     char *s;
113 
114     s = malloc(len);
115     if (len != 0 && s == NULL)
116 	return krb5_enomem(context);
117     memcpy(s, password.data, password.length);
118     if (salt.saltvalue.length)
119         memcpy(s + password.length, salt.saltvalue.data, salt.saltvalue.length);
120     ret = krb5_string_to_key_derived(context,
121 				     s,
122 				     len,
123 				     enctype,
124 				     key);
125     memset_s(s, len, 0, len);
126     free(s);
127     return ret;
128 }
129 
130 
131 #ifdef DES3_OLD_ENCTYPE
132 struct salt_type _krb5_des3_salt[] = {
133     {
134 	KRB5_PW_SALT,
135 	"pw-salt",
136 	DES3_string_to_key
137     },
138     { 0, NULL, NULL }
139 };
140 #endif
141 
142 struct salt_type _krb5_des3_salt_derived[] = {
143     {
144 	KRB5_PW_SALT,
145 	"pw-salt",
146 	DES3_string_to_key_derived
147     },
148     { 0, NULL, NULL }
149 };
150