3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/ms_funcs.h"
15 const u8 * mschapv2_remove_domain(const u8 *username, size_t *len)
20 * MSCHAPv2 does not include optional domain name in the
21 * challenge-response calculation, so remove domain prefix
25 for (i = 0; i < *len; i++) {
26 if (username[i] == '\\') {
28 return username + i + 1;
36 int mschapv2_derive_response(const u8 *identity, size_t identity_len,
37 const u8 *password, size_t password_len,
39 const u8 *auth_challenge,
40 const u8 *peer_challenge,
41 u8 *nt_response, u8 *auth_response,
46 u8 password_hash[16], password_hash_hash[16];
48 wpa_hexdump_ascii(MSG_DEBUG, "MSCHAPV2: Identity",
49 identity, identity_len);
50 username_len = identity_len;
51 username = mschapv2_remove_domain(identity, &username_len);
52 wpa_hexdump_ascii(MSG_DEBUG, "MSCHAPV2: Username",
53 username, username_len);
55 wpa_hexdump(MSG_DEBUG, "MSCHAPV2: auth_challenge",
56 auth_challenge, MSCHAPV2_CHAL_LEN);
57 wpa_hexdump(MSG_DEBUG, "MSCHAPV2: peer_challenge",
58 peer_challenge, MSCHAPV2_CHAL_LEN);
59 wpa_hexdump_ascii(MSG_DEBUG, "MSCHAPV2: username",
60 username, username_len);
61 /* Authenticator response is not really needed yet, but calculate it
62 * here so that challenges need not be saved. */
64 wpa_hexdump_key(MSG_DEBUG, "MSCHAPV2: password hash",
65 password, password_len);
66 if (generate_nt_response_pwhash(auth_challenge, peer_challenge,
67 username, username_len,
68 password, nt_response) ||
69 generate_authenticator_response_pwhash(
70 password, peer_challenge, auth_challenge,
71 username, username_len, nt_response,
75 wpa_hexdump_ascii_key(MSG_DEBUG, "MSCHAPV2: password",
76 password, password_len);
77 if (generate_nt_response(auth_challenge, peer_challenge,
78 username, username_len,
79 password, password_len,
81 generate_authenticator_response(password, password_len,
84 username, username_len,
89 wpa_hexdump(MSG_DEBUG, "MSCHAPV2: NT Response",
90 nt_response, MSCHAPV2_NT_RESPONSE_LEN);
91 wpa_hexdump(MSG_DEBUG, "MSCHAPV2: Auth Response",
92 auth_response, MSCHAPV2_AUTH_RESPONSE_LEN);
94 /* Generate master_key here since we have the needed data available. */
96 if (hash_nt_password_hash(password, password_hash_hash))
99 if (nt_password_hash(password, password_len, password_hash) ||
100 hash_nt_password_hash(password_hash, password_hash_hash))
103 if (get_master_key(password_hash_hash, nt_response, master_key))
105 wpa_hexdump_key(MSG_DEBUG, "MSCHAPV2: Master Key",
106 master_key, MSCHAPV2_MASTER_KEY_LEN);
112 int mschapv2_verify_auth_response(const u8 *auth_response,
113 const u8 *buf, size_t buf_len)
115 u8 recv_response[MSCHAPV2_AUTH_RESPONSE_LEN];
116 if (buf_len < 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN ||
117 buf[0] != 'S' || buf[1] != '=' ||
118 hexstr2bin((char *) (buf + 2), recv_response,
119 MSCHAPV2_AUTH_RESPONSE_LEN) ||
120 os_memcmp(auth_response, recv_response,
121 MSCHAPV2_AUTH_RESPONSE_LEN) != 0)