1 /* $NetBSD: salt-des.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
4 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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
36 #include "krb5_locl.h"
38 #ifdef HEIM_WEAK_CRYPTO
40 #ifdef ENABLE_AFS_STRING_TO_KEY
42 /* This defines the Andrew string_to_key function. It accepts a password
43 * string as input and converts it via a one-way encryption algorithm to a DES
44 * encryption key. It is compatible with the original Andrew authentication
45 * service password database.
49 * Short passwords, i.e 8 characters or less.
52 krb5_DES_AFS3_CMU_string_to_key (krb5_data pw
,
56 char password
[8+1]; /* crypt is limited to 8 chars anyway */
59 for(i
= 0; i
< 8; i
++) {
60 char c
= ((i
< pw
.length
) ? ((char*)pw
.data
)[i
] : 0) ^
62 tolower(((unsigned char*)cell
.data
)[i
]) : 0);
63 password
[i
] = c
? c
: 'X';
67 memcpy(key
, crypt(password
, "p1") + 2, sizeof(DES_cblock
));
69 /* parity is inserted into the LSB so left shift each byte up one
70 bit. This allows ascii characters with a zero MSB to retain as
71 much significance as possible. */
72 for (i
= 0; i
< sizeof(DES_cblock
); i
++)
73 ((unsigned char*)key
)[i
] <<= 1;
74 DES_set_odd_parity (key
);
78 * Long passwords, i.e 9 characters or more.
81 krb5_DES_AFS3_Transarc_string_to_key (krb5_data pw
,
85 DES_key_schedule schedule
;
91 memcpy(password
, pw
.data
, min(pw
.length
, sizeof(password
)));
92 if(pw
.length
< sizeof(password
)) {
93 int len
= min(cell
.length
, sizeof(password
) - pw
.length
);
96 memcpy(password
+ pw
.length
, cell
.data
, len
);
97 for (i
= pw
.length
; i
< pw
.length
+ len
; ++i
)
98 password
[i
] = tolower((unsigned char)password
[i
]);
100 passlen
= min(sizeof(password
), pw
.length
+ cell
.length
);
101 memcpy(&ivec
, "kerberos", 8);
102 memcpy(&temp_key
, "kerberos", 8);
103 DES_set_odd_parity (&temp_key
);
104 DES_set_key_unchecked (&temp_key
, &schedule
);
105 DES_cbc_cksum ((void*)password
, &ivec
, passlen
, &schedule
, &ivec
);
107 memcpy(&temp_key
, &ivec
, 8);
108 DES_set_odd_parity (&temp_key
);
109 DES_set_key_unchecked (&temp_key
, &schedule
);
110 DES_cbc_cksum ((void*)password
, key
, passlen
, &schedule
, &ivec
);
111 memset(&schedule
, 0, sizeof(schedule
));
112 memset(&temp_key
, 0, sizeof(temp_key
));
113 memset(&ivec
, 0, sizeof(ivec
));
114 memset(password
, 0, sizeof(password
));
116 DES_set_odd_parity (key
);
119 static krb5_error_code
120 DES_AFS3_string_to_key(krb5_context context
,
121 krb5_enctype enctype
,
128 if(password
.length
> 8)
129 krb5_DES_AFS3_Transarc_string_to_key(password
, salt
.saltvalue
, &tmp
);
131 krb5_DES_AFS3_CMU_string_to_key(password
, salt
.saltvalue
, &tmp
);
132 key
->keytype
= enctype
;
133 krb5_data_copy(&key
->keyvalue
, tmp
, sizeof(tmp
));
134 memset(&key
, 0, sizeof(key
));
137 #endif /* ENABLE_AFS_STRING_TO_KEY */
140 DES_string_to_key_int(unsigned char *data
, size_t length
, DES_cblock
*key
)
142 DES_key_schedule schedule
;
147 unsigned char swap
[] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
148 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf };
151 p
= (unsigned char*)key
;
152 for (i
= 0; i
< length
; i
++) {
153 unsigned char tmp
= data
[i
];
157 *--p
^= (swap
[tmp
& 0xf] << 4) | swap
[(tmp
& 0xf0) >> 4];
161 DES_set_odd_parity(key
);
162 if(DES_is_weak_key(key
))
164 DES_set_key_unchecked(key
, &schedule
);
165 DES_cbc_cksum((void*)data
, key
, length
, &schedule
, key
);
166 memset(&schedule
, 0, sizeof(schedule
));
167 DES_set_odd_parity(key
);
168 if(DES_is_weak_key(key
))
172 static krb5_error_code
173 krb5_DES_string_to_key(krb5_context context
,
174 krb5_enctype enctype
,
184 #ifdef ENABLE_AFS_STRING_TO_KEY
185 if (opaque
.length
== 1) {
187 _krb5_get_int(opaque
.data
, &v
, 1);
189 return DES_AFS3_string_to_key(context
, enctype
, password
,
194 len
= password
.length
+ salt
.saltvalue
.length
;
196 if(len
> 0 && s
== NULL
) {
197 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
200 memcpy(s
, password
.data
, password
.length
);
201 memcpy(s
+ password
.length
, salt
.saltvalue
.data
, salt
.saltvalue
.length
);
202 DES_string_to_key_int(s
, len
, &tmp
);
203 key
->keytype
= enctype
;
204 krb5_data_copy(&key
->keyvalue
, tmp
, sizeof(tmp
));
205 memset(&tmp
, 0, sizeof(tmp
));
211 struct salt_type _krb5_des_salt
[] = {
215 krb5_DES_string_to_key
217 #ifdef ENABLE_AFS_STRING_TO_KEY
221 DES_AFS3_string_to_key