100 u_char LANManResp[24];
114 extern void setkey(
const char *);
115 extern void encrypt(
char *,
int);
117 static void DesEncrypt (u_char *, u_char *, u_char *);
118 static void MakeKey (u_char *, u_char *);
121 static void Expand (u_char *, u_char *);
122 static void Collapse (u_char *, u_char *);
125 static void ChallengeResponse(
130 static void ChapMS_NT(
135 MS_ChapResponse *response
137 static u_char Get7Bits(
143 ChallengeResponse( u_char *challenge,
147 u_char ZPasswordHash[21];
149 BZERO(ZPasswordHash,
sizeof(ZPasswordHash));
150 BCOPY(pwHash, ZPasswordHash, 16);
153 log_packet(ZPasswordHash,
sizeof(ZPasswordHash),
"ChallengeResponse - ZPasswordHash",
LOG_DEBUG);
156 DesEncrypt(challenge, ZPasswordHash + 0, response + 0);
157 DesEncrypt(challenge, ZPasswordHash + 7, response + 8);
158 DesEncrypt(challenge, ZPasswordHash + 14, response + 16);
161 log_packet(response, 24,
"ChallengeResponse - response",
LOG_DEBUG);
168 DesEncrypt( u_char *clear,
173 u_char crypt_key[66];
174 u_char des_input[66];
176 MakeKey(key, des_key);
178 Expand(des_key, crypt_key);
179 setkey((
char*)crypt_key);
182 CHAPDEBUG(
LOG_INFO, (
"DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X\n",
183 clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
186 Expand(clear, des_input);
187 encrypt((
char*)des_input, 0);
188 Collapse(des_input, cipher);
191 CHAPDEBUG(
LOG_INFO, (
"DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X\n",
192 cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
199 DesEncrypt( u_char *clear,
204 des_key_schedule key_schedule;
206 MakeKey(key, des_key);
208 des_set_key(&des_key, key_schedule);
211 CHAPDEBUG(
LOG_INFO, (
"DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X\n",
212 clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
215 des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher, key_schedule, 1);
218 CHAPDEBUG(
LOG_INFO, (
"DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X\n",
219 cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
227 Get7Bits( u_char *input,
int startBit)
229 register unsigned int word;
231 word = (unsigned)input[startBit / 8] << 8;
232 word |= (unsigned)input[startBit / 8 + 1];
234 word >>= 15 - (startBit % 8 + 7);
246 Expand(u_char *in, u_char *out)
251 for(i = 0; i < 64; in++){
253 for(j = 7; j >= 0; j--) {
254 *out++ = (c >> j) & 01;
263 Collapse(u_char *in, u_char *out)
269 for (i = 0; i < 64; i += 8, out++) {
271 for (j = 7; j >= 0; j--, in++) {
280 MakeKey( u_char *key,
283 des_key[0] = Get7Bits(key, 0);
284 des_key[1] = Get7Bits(key, 7);
285 des_key[2] = Get7Bits(key, 14);
286 des_key[3] = Get7Bits(key, 21);
287 des_key[4] = Get7Bits(key, 28);
288 des_key[5] = Get7Bits(key, 35);
289 des_key[6] = Get7Bits(key, 42);
290 des_key[7] = Get7Bits(key, 49);
293 des_set_odd_parity((des_cblock *)des_key);
298 key[0], key[1], key[2], key[3], key[4], key[5], key[6]));
299 CHAPDEBUG(
LOG_INFO, (
"MakeKey: 64-bit output: %02X%02X%02X%02X%02X%02X%02X%02X\n",
300 des_key[0], des_key[1], des_key[2], des_key[3], des_key[4], des_key[5], des_key[6], des_key[7]));
305 ChapMS_NT(
char *rchallenge,
309 MS_ChapResponse *response)
314 static int low_byte_first = -1;
320 BZERO(unicodePassword,
sizeof(unicodePassword));
321 for (i = 0; i < secret_len; i++) {
322 unicodePassword[i * 2] = (u_char)secret[i];
324 MDbegin(&md4Context);
325 MDupdate(&md4Context, unicodePassword, secret_len * 2 * 8);
327 if (low_byte_first == -1) {
328 low_byte_first = (
PP_HTONS((
unsigned short int)1) != 1);
330 if (low_byte_first == 0) {
332 MDreverse((
unsigned int*)&md4Context);
335 MDupdate(&md4Context,
NULL, 0);
337 ChallengeResponse((u_char*)rchallenge, (u_char*)md4Context.buffer, response->NTResp);
341 static u_char *StdText = (u_char *)
"KGS!@#$%";
344 ChapMS_LANMan(
char *rchallenge,
348 MS_ChapResponse *response)
352 u_char PasswordHash[16];
355 BZERO(UcasePassword,
sizeof(UcasePassword));
356 for (i = 0; i < secret_len; i++) {
357 UcasePassword[i] = (u_char)toupper(secret[i]);
359 DesEncrypt( StdText, UcasePassword + 0, PasswordHash + 0 );
360 DesEncrypt( StdText, UcasePassword + 7, PasswordHash + 8 );
361 ChallengeResponse(rchallenge, PasswordHash, response->LANManResp);
368 MS_ChapResponse response;
370 extern int ms_lanman;
376 BZERO(&response,
sizeof(response));
379 ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, &response);
382 ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, &response);
385 response.UseNT = !ms_lanman;