2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
36 #ifdef HEIM_WEAK_CRYPTO
38 #ifdef ENABLE_AFS_STRING_TO_KEY
40 /* This defines the Andrew string_to_key function. It accepts a password
41 * string as input and converts it via a one-way encryption algorithm to a DES
42 * encryption key. It is compatible with the original Andrew authentication
43 * service password database.
47 * Short passwords, i.e 8 characters or less.
50 krb5_DES_AFS3_CMU_string_to_key (krb5_data pw
,
54 char password
[8+1]; /* crypt is limited to 8 chars anyway */
57 for(i
= 0; i
< 8; i
++) {
58 char c
= ((i
< pw
.length
) ? ((char*)pw
.data
)[i
] : 0) ^
60 tolower(((unsigned char*)cell
.data
)[i
]) : 0);
61 password
[i
] = c
? c
: 'X';
65 memcpy(key
, crypt(password
, "p1") + 2, sizeof(DES_cblock
));
67 /* parity is inserted into the LSB so left shift each byte up one
68 bit. This allows ascii characters with a zero MSB to retain as
69 much significance as possible. */
70 for (i
= 0; i
< sizeof(DES_cblock
); i
++)
71 ((unsigned char*)key
)[i
] <<= 1;
72 DES_set_odd_parity (key
);
76 * Long passwords, i.e 9 characters or more.
79 krb5_DES_AFS3_Transarc_string_to_key (krb5_data pw
,
83 DES_key_schedule schedule
;
89 memcpy(password
, pw
.data
, min(pw
.length
, sizeof(password
)));
90 if(pw
.length
< sizeof(password
)) {
91 int len
= min(cell
.length
, sizeof(password
) - pw
.length
);
94 memcpy(password
+ pw
.length
, cell
.data
, len
);
95 for (i
= pw
.length
; i
< pw
.length
+ len
; ++i
)
96 password
[i
] = tolower((unsigned char)password
[i
]);
98 passlen
= min(sizeof(password
), pw
.length
+ cell
.length
);
99 memcpy(&ivec
, "kerberos", 8);
100 memcpy(&temp_key
, "kerberos", 8);
101 DES_set_odd_parity (&temp_key
);
102 DES_set_key_unchecked (&temp_key
, &schedule
);
103 DES_cbc_cksum ((void*)password
, &ivec
, passlen
, &schedule
, &ivec
);
105 memcpy(&temp_key
, &ivec
, 8);
106 DES_set_odd_parity (&temp_key
);
107 DES_set_key_unchecked (&temp_key
, &schedule
);
108 DES_cbc_cksum ((void*)password
, key
, passlen
, &schedule
, &ivec
);
109 memset(&schedule
, 0, sizeof(schedule
));
110 memset(&temp_key
, 0, sizeof(temp_key
));
111 memset(&ivec
, 0, sizeof(ivec
));
112 memset(password
, 0, sizeof(password
));
114 DES_set_odd_parity (key
);
117 static krb5_error_code
118 DES_AFS3_string_to_key(krb5_context context
,
119 krb5_enctype enctype
,
126 if(password
.length
> 8)
127 krb5_DES_AFS3_Transarc_string_to_key(password
, salt
.saltvalue
, &tmp
);
129 krb5_DES_AFS3_CMU_string_to_key(password
, salt
.saltvalue
, &tmp
);
130 key
->keytype
= enctype
;
131 krb5_data_copy(&key
->keyvalue
, tmp
, sizeof(tmp
));
132 memset(&key
, 0, sizeof(key
));
135 #endif /* ENABLE_AFS_STRING_TO_KEY */
138 DES_string_to_key_int(unsigned char *data
, size_t length
, DES_cblock
*key
)
140 DES_key_schedule schedule
;
145 unsigned char swap
[] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
146 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf };
149 p
= (unsigned char*)key
;
150 for (i
= 0; i
< length
; i
++) {
151 unsigned char tmp
= data
[i
];
155 *--p
^= (swap
[tmp
& 0xf] << 4) | swap
[(tmp
& 0xf0) >> 4];
159 DES_set_odd_parity(key
);
160 if(DES_is_weak_key(key
))
162 DES_set_key_unchecked(key
, &schedule
);
163 DES_cbc_cksum((void*)data
, key
, length
, &schedule
, key
);
164 memset(&schedule
, 0, sizeof(schedule
));
165 DES_set_odd_parity(key
);
166 if(DES_is_weak_key(key
))
170 static krb5_error_code
171 krb5_DES_string_to_key(krb5_context context
,
172 krb5_enctype enctype
,
182 #ifdef ENABLE_AFS_STRING_TO_KEY
183 if (opaque
.length
== 1) {
185 _krb5_get_int(opaque
.data
, &v
, 1);
187 return DES_AFS3_string_to_key(context
, enctype
, password
,
192 len
= password
.length
+ salt
.saltvalue
.length
;
194 if (len
> 0 && s
== NULL
)
195 return krb5_enomem(context
);
196 memcpy(s
, password
.data
, password
.length
);
197 memcpy(s
+ password
.length
, salt
.saltvalue
.data
, salt
.saltvalue
.length
);
198 DES_string_to_key_int(s
, len
, &tmp
);
199 key
->keytype
= enctype
;
200 krb5_data_copy(&key
->keyvalue
, tmp
, sizeof(tmp
));
201 memset(&tmp
, 0, sizeof(tmp
));
207 struct salt_type _krb5_des_salt
[] = {
211 krb5_DES_string_to_key
213 #ifdef ENABLE_AFS_STRING_TO_KEY
217 DES_AFS3_string_to_key