xref: /dflybsd-src/contrib/wpa_supplicant/src/eap_peer/eap_pwd.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
13ff40c12SJohn Marino /*
23ff40c12SJohn Marino  * EAP peer method: EAP-pwd (RFC 5931)
33ff40c12SJohn Marino  * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org>
43ff40c12SJohn Marino  *
53ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
63ff40c12SJohn Marino  * See README for more details.
73ff40c12SJohn Marino  */
83ff40c12SJohn Marino 
93ff40c12SJohn Marino #include "includes.h"
103ff40c12SJohn Marino 
113ff40c12SJohn Marino #include "common.h"
12*a1157835SDaniel Fojt #include "crypto/sha1.h"
133ff40c12SJohn Marino #include "crypto/sha256.h"
14*a1157835SDaniel Fojt #include "crypto/sha512.h"
15*a1157835SDaniel Fojt #include "crypto/ms_funcs.h"
16*a1157835SDaniel Fojt #include "crypto/crypto.h"
173ff40c12SJohn Marino #include "eap_peer/eap_i.h"
183ff40c12SJohn Marino #include "eap_common/eap_pwd_common.h"
193ff40c12SJohn Marino 
203ff40c12SJohn Marino 
213ff40c12SJohn Marino struct eap_pwd_data {
223ff40c12SJohn Marino 	enum {
23*a1157835SDaniel Fojt 		PWD_ID_Req, PWD_Commit_Req, PWD_Confirm_Req,
24*a1157835SDaniel Fojt 		SUCCESS_ON_FRAG_COMPLETION, SUCCESS, FAILURE
253ff40c12SJohn Marino 	} state;
263ff40c12SJohn Marino 	u8 *id_peer;
273ff40c12SJohn Marino 	size_t id_peer_len;
283ff40c12SJohn Marino 	u8 *id_server;
293ff40c12SJohn Marino 	size_t id_server_len;
303ff40c12SJohn Marino 	u8 *password;
313ff40c12SJohn Marino 	size_t password_len;
32*a1157835SDaniel Fojt 	int password_hash;
33*a1157835SDaniel Fojt 	struct wpa_freq_range_list allowed_groups;
343ff40c12SJohn Marino 	u16 group_num;
35*a1157835SDaniel Fojt 	u8 prep;
36*a1157835SDaniel Fojt 	u8 token[4];
373ff40c12SJohn Marino 	EAP_PWD_group *grp;
383ff40c12SJohn Marino 
393ff40c12SJohn Marino 	struct wpabuf *inbuf;
403ff40c12SJohn Marino 	size_t in_frag_pos;
413ff40c12SJohn Marino 	struct wpabuf *outbuf;
423ff40c12SJohn Marino 	size_t out_frag_pos;
433ff40c12SJohn Marino 	size_t mtu;
443ff40c12SJohn Marino 
45*a1157835SDaniel Fojt 	struct crypto_bignum *k;
46*a1157835SDaniel Fojt 	struct crypto_bignum *private_value;
47*a1157835SDaniel Fojt 	struct crypto_bignum *server_scalar;
48*a1157835SDaniel Fojt 	struct crypto_bignum *my_scalar;
49*a1157835SDaniel Fojt 	struct crypto_ec_point *my_element;
50*a1157835SDaniel Fojt 	struct crypto_ec_point *server_element;
513ff40c12SJohn Marino 
523ff40c12SJohn Marino 	u8 msk[EAP_MSK_LEN];
533ff40c12SJohn Marino 	u8 emsk[EAP_EMSK_LEN];
54*a1157835SDaniel Fojt 	u8 session_id[1 + SHA256_MAC_LEN];
553ff40c12SJohn Marino };
563ff40c12SJohn Marino 
573ff40c12SJohn Marino 
58*a1157835SDaniel Fojt static void eap_pwd_deinit(struct eap_sm *sm, void *priv);
59*a1157835SDaniel Fojt 
60*a1157835SDaniel Fojt 
613ff40c12SJohn Marino #ifndef CONFIG_NO_STDOUT_DEBUG
eap_pwd_state_txt(int state)623ff40c12SJohn Marino static const char * eap_pwd_state_txt(int state)
633ff40c12SJohn Marino {
643ff40c12SJohn Marino 	switch (state) {
653ff40c12SJohn Marino         case PWD_ID_Req:
663ff40c12SJohn Marino 		return "PWD-ID-Req";
673ff40c12SJohn Marino         case PWD_Commit_Req:
683ff40c12SJohn Marino 		return "PWD-Commit-Req";
693ff40c12SJohn Marino         case PWD_Confirm_Req:
703ff40c12SJohn Marino 		return "PWD-Confirm-Req";
71*a1157835SDaniel Fojt 	case SUCCESS_ON_FRAG_COMPLETION:
72*a1157835SDaniel Fojt 		return "SUCCESS_ON_FRAG_COMPLETION";
733ff40c12SJohn Marino         case SUCCESS:
743ff40c12SJohn Marino 		return "SUCCESS";
753ff40c12SJohn Marino         case FAILURE:
763ff40c12SJohn Marino 		return "FAILURE";
773ff40c12SJohn Marino         default:
783ff40c12SJohn Marino 		return "PWD-UNK";
793ff40c12SJohn Marino 	}
803ff40c12SJohn Marino }
813ff40c12SJohn Marino #endif  /* CONFIG_NO_STDOUT_DEBUG */
823ff40c12SJohn Marino 
833ff40c12SJohn Marino 
eap_pwd_state(struct eap_pwd_data * data,int state)843ff40c12SJohn Marino static void eap_pwd_state(struct eap_pwd_data *data, int state)
853ff40c12SJohn Marino {
863ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "EAP-PWD: %s -> %s",
873ff40c12SJohn Marino 		   eap_pwd_state_txt(data->state), eap_pwd_state_txt(state));
883ff40c12SJohn Marino 	data->state = state;
893ff40c12SJohn Marino }
903ff40c12SJohn Marino 
913ff40c12SJohn Marino 
eap_pwd_init(struct eap_sm * sm)923ff40c12SJohn Marino static void * eap_pwd_init(struct eap_sm *sm)
933ff40c12SJohn Marino {
943ff40c12SJohn Marino 	struct eap_pwd_data *data;
953ff40c12SJohn Marino 	const u8 *identity, *password;
963ff40c12SJohn Marino 	size_t identity_len, password_len;
973ff40c12SJohn Marino 	int fragment_size;
98*a1157835SDaniel Fojt 	int pwhash;
99*a1157835SDaniel Fojt 	const char *phase1;
1003ff40c12SJohn Marino 
101*a1157835SDaniel Fojt 	password = eap_get_config_password2(sm, &password_len, &pwhash);
1023ff40c12SJohn Marino 	if (password == NULL) {
1033ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD: No password configured!");
1043ff40c12SJohn Marino 		return NULL;
1053ff40c12SJohn Marino 	}
1063ff40c12SJohn Marino 
1073ff40c12SJohn Marino 	identity = eap_get_config_identity(sm, &identity_len);
1083ff40c12SJohn Marino 	if (identity == NULL) {
1093ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD: No identity configured!");
1103ff40c12SJohn Marino 		return NULL;
1113ff40c12SJohn Marino 	}
1123ff40c12SJohn Marino 
1133ff40c12SJohn Marino 	if ((data = os_zalloc(sizeof(*data))) == NULL) {
1143ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD: memory allocation data fail");
1153ff40c12SJohn Marino 		return NULL;
1163ff40c12SJohn Marino 	}
1173ff40c12SJohn Marino 
1183ff40c12SJohn Marino 	if ((data->id_peer = os_malloc(identity_len)) == NULL) {
1193ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail");
1203ff40c12SJohn Marino 		os_free(data);
1213ff40c12SJohn Marino 		return NULL;
1223ff40c12SJohn Marino 	}
1233ff40c12SJohn Marino 
1243ff40c12SJohn Marino 	os_memcpy(data->id_peer, identity, identity_len);
1253ff40c12SJohn Marino 	data->id_peer_len = identity_len;
1263ff40c12SJohn Marino 
1273ff40c12SJohn Marino 	if ((data->password = os_malloc(password_len)) == NULL) {
1283ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD: memory allocation psk fail");
129*a1157835SDaniel Fojt 		bin_clear_free(data->id_peer, data->id_peer_len);
1303ff40c12SJohn Marino 		os_free(data);
1313ff40c12SJohn Marino 		return NULL;
1323ff40c12SJohn Marino 	}
1333ff40c12SJohn Marino 	os_memcpy(data->password, password, password_len);
1343ff40c12SJohn Marino 	data->password_len = password_len;
135*a1157835SDaniel Fojt 	data->password_hash = pwhash;
136*a1157835SDaniel Fojt 
137*a1157835SDaniel Fojt 	phase1 = eap_get_config_phase1(sm);
138*a1157835SDaniel Fojt 	if (phase1) {
139*a1157835SDaniel Fojt 		const char *pos, *end;
140*a1157835SDaniel Fojt 		char *copy = NULL;
141*a1157835SDaniel Fojt 		int res;
142*a1157835SDaniel Fojt 
143*a1157835SDaniel Fojt 		pos = os_strstr(phase1, "eap_pwd_groups=");
144*a1157835SDaniel Fojt 		if (pos) {
145*a1157835SDaniel Fojt 			pos += 15;
146*a1157835SDaniel Fojt 			end = os_strchr(pos, ' ');
147*a1157835SDaniel Fojt 			if (end) {
148*a1157835SDaniel Fojt 				copy = os_zalloc(end - pos + 1);
149*a1157835SDaniel Fojt 				if (!copy)
150*a1157835SDaniel Fojt 					goto fail;
151*a1157835SDaniel Fojt 				os_memcpy(copy, pos, end - pos);
152*a1157835SDaniel Fojt 				pos = copy;
153*a1157835SDaniel Fojt 			}
154*a1157835SDaniel Fojt 			res = freq_range_list_parse(&data->allowed_groups, pos);
155*a1157835SDaniel Fojt 			os_free(copy);
156*a1157835SDaniel Fojt 			if (res)
157*a1157835SDaniel Fojt 				goto fail;
158*a1157835SDaniel Fojt 		}
159*a1157835SDaniel Fojt 	}
1603ff40c12SJohn Marino 
1613ff40c12SJohn Marino 	data->out_frag_pos = data->in_frag_pos = 0;
1623ff40c12SJohn Marino 	data->inbuf = data->outbuf = NULL;
1633ff40c12SJohn Marino 	fragment_size = eap_get_config_fragment_size(sm);
1643ff40c12SJohn Marino 	if (fragment_size <= 0)
1653ff40c12SJohn Marino 		data->mtu = 1020; /* default from RFC 5931 */
1663ff40c12SJohn Marino 	else
1673ff40c12SJohn Marino 		data->mtu = fragment_size;
1683ff40c12SJohn Marino 
1693ff40c12SJohn Marino 	data->state = PWD_ID_Req;
1703ff40c12SJohn Marino 
1713ff40c12SJohn Marino 	return data;
172*a1157835SDaniel Fojt fail:
173*a1157835SDaniel Fojt 	eap_pwd_deinit(sm, data);
174*a1157835SDaniel Fojt 	return NULL;
1753ff40c12SJohn Marino }
1763ff40c12SJohn Marino 
1773ff40c12SJohn Marino 
eap_pwd_deinit(struct eap_sm * sm,void * priv)1783ff40c12SJohn Marino static void eap_pwd_deinit(struct eap_sm *sm, void *priv)
1793ff40c12SJohn Marino {
1803ff40c12SJohn Marino 	struct eap_pwd_data *data = priv;
1813ff40c12SJohn Marino 
182*a1157835SDaniel Fojt 	crypto_bignum_deinit(data->private_value, 1);
183*a1157835SDaniel Fojt 	crypto_bignum_deinit(data->server_scalar, 1);
184*a1157835SDaniel Fojt 	crypto_bignum_deinit(data->my_scalar, 1);
185*a1157835SDaniel Fojt 	crypto_bignum_deinit(data->k, 1);
186*a1157835SDaniel Fojt 	crypto_ec_point_deinit(data->my_element, 1);
187*a1157835SDaniel Fojt 	crypto_ec_point_deinit(data->server_element, 1);
188*a1157835SDaniel Fojt 	bin_clear_free(data->id_peer, data->id_peer_len);
189*a1157835SDaniel Fojt 	bin_clear_free(data->id_server, data->id_server_len);
190*a1157835SDaniel Fojt 	bin_clear_free(data->password, data->password_len);
1913ff40c12SJohn Marino 	if (data->grp) {
192*a1157835SDaniel Fojt 		crypto_ec_deinit(data->grp->group);
193*a1157835SDaniel Fojt 		crypto_ec_point_deinit(data->grp->pwe, 1);
1943ff40c12SJohn Marino 		os_free(data->grp);
1953ff40c12SJohn Marino 	}
196*a1157835SDaniel Fojt 	wpabuf_free(data->inbuf);
197*a1157835SDaniel Fojt 	wpabuf_free(data->outbuf);
198*a1157835SDaniel Fojt 	os_free(data->allowed_groups.range);
199*a1157835SDaniel Fojt 	bin_clear_free(data, sizeof(*data));
2003ff40c12SJohn Marino }
2013ff40c12SJohn Marino 
2023ff40c12SJohn Marino 
eap_pwd_getkey(struct eap_sm * sm,void * priv,size_t * len)2033ff40c12SJohn Marino static u8 * eap_pwd_getkey(struct eap_sm *sm, void *priv, size_t *len)
2043ff40c12SJohn Marino {
2053ff40c12SJohn Marino 	struct eap_pwd_data *data = priv;
2063ff40c12SJohn Marino 	u8 *key;
2073ff40c12SJohn Marino 
2083ff40c12SJohn Marino 	if (data->state != SUCCESS)
2093ff40c12SJohn Marino 		return NULL;
2103ff40c12SJohn Marino 
211*a1157835SDaniel Fojt 	key = os_memdup(data->msk, EAP_MSK_LEN);
2123ff40c12SJohn Marino 	if (key == NULL)
2133ff40c12SJohn Marino 		return NULL;
2143ff40c12SJohn Marino 
2153ff40c12SJohn Marino 	*len = EAP_MSK_LEN;
2163ff40c12SJohn Marino 
2173ff40c12SJohn Marino 	return key;
2183ff40c12SJohn Marino }
2193ff40c12SJohn Marino 
2203ff40c12SJohn Marino 
eap_pwd_get_session_id(struct eap_sm * sm,void * priv,size_t * len)221*a1157835SDaniel Fojt static u8 * eap_pwd_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
222*a1157835SDaniel Fojt {
223*a1157835SDaniel Fojt 	struct eap_pwd_data *data = priv;
224*a1157835SDaniel Fojt 	u8 *id;
225*a1157835SDaniel Fojt 
226*a1157835SDaniel Fojt 	if (data->state != SUCCESS)
227*a1157835SDaniel Fojt 		return NULL;
228*a1157835SDaniel Fojt 
229*a1157835SDaniel Fojt 	id = os_memdup(data->session_id, 1 + SHA256_MAC_LEN);
230*a1157835SDaniel Fojt 	if (id == NULL)
231*a1157835SDaniel Fojt 		return NULL;
232*a1157835SDaniel Fojt 
233*a1157835SDaniel Fojt 	*len = 1 + SHA256_MAC_LEN;
234*a1157835SDaniel Fojt 
235*a1157835SDaniel Fojt 	return id;
236*a1157835SDaniel Fojt }
237*a1157835SDaniel Fojt 
238*a1157835SDaniel Fojt 
eap_pwd_allowed_group(struct eap_pwd_data * data,u16 group)239*a1157835SDaniel Fojt static int eap_pwd_allowed_group(struct eap_pwd_data *data, u16 group)
240*a1157835SDaniel Fojt {
241*a1157835SDaniel Fojt 	if (!data->allowed_groups.range) {
242*a1157835SDaniel Fojt 		/* By default, allow the groups using NIST curves P-256, P-384,
243*a1157835SDaniel Fojt 		 * and P-521. */
244*a1157835SDaniel Fojt 		return group == 19 || group == 20 || group == 21;
245*a1157835SDaniel Fojt 	}
246*a1157835SDaniel Fojt 
247*a1157835SDaniel Fojt 	return freq_range_list_includes(&data->allowed_groups, group);
248*a1157835SDaniel Fojt }
249*a1157835SDaniel Fojt 
250*a1157835SDaniel Fojt 
2513ff40c12SJohn Marino static void
eap_pwd_perform_id_exchange(struct eap_sm * sm,struct eap_pwd_data * data,struct eap_method_ret * ret,const struct wpabuf * reqData,const u8 * payload,size_t payload_len)2523ff40c12SJohn Marino eap_pwd_perform_id_exchange(struct eap_sm *sm, struct eap_pwd_data *data,
2533ff40c12SJohn Marino 			    struct eap_method_ret *ret,
2543ff40c12SJohn Marino 			    const struct wpabuf *reqData,
2553ff40c12SJohn Marino 			    const u8 *payload, size_t payload_len)
2563ff40c12SJohn Marino {
2573ff40c12SJohn Marino 	struct eap_pwd_id *id;
2583ff40c12SJohn Marino 
2593ff40c12SJohn Marino 	if (data->state != PWD_ID_Req) {
2603ff40c12SJohn Marino 		ret->ignore = TRUE;
2613ff40c12SJohn Marino 		eap_pwd_state(data, FAILURE);
2623ff40c12SJohn Marino 		return;
2633ff40c12SJohn Marino 	}
2643ff40c12SJohn Marino 
2653ff40c12SJohn Marino 	if (payload_len < sizeof(struct eap_pwd_id)) {
2663ff40c12SJohn Marino 		ret->ignore = TRUE;
2673ff40c12SJohn Marino 		eap_pwd_state(data, FAILURE);
2683ff40c12SJohn Marino 		return;
2693ff40c12SJohn Marino 	}
2703ff40c12SJohn Marino 
2713ff40c12SJohn Marino 	id = (struct eap_pwd_id *) payload;
2723ff40c12SJohn Marino 	data->group_num = be_to_host16(id->group_num);
273*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
274*a1157835SDaniel Fojt 		   "EAP-PWD: Server EAP-pwd-ID proposal: group=%u random=%u prf=%u prep=%u",
275*a1157835SDaniel Fojt 		   data->group_num, id->random_function, id->prf, id->prep);
276*a1157835SDaniel Fojt 	if (id->random_function != EAP_PWD_DEFAULT_RAND_FUNC ||
277*a1157835SDaniel Fojt 	    id->prf != EAP_PWD_DEFAULT_PRF ||
278*a1157835SDaniel Fojt 	    !eap_pwd_allowed_group(data, data->group_num)) {
279*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
280*a1157835SDaniel Fojt 			   "EAP-pwd: Unsupported or disabled proposal");
281*a1157835SDaniel Fojt 		eap_pwd_state(data, FAILURE);
282*a1157835SDaniel Fojt 		return;
283*a1157835SDaniel Fojt 	}
284*a1157835SDaniel Fojt 
285*a1157835SDaniel Fojt 	if (id->prep != EAP_PWD_PREP_NONE &&
286*a1157835SDaniel Fojt 	    id->prep != EAP_PWD_PREP_MS &&
287*a1157835SDaniel Fojt 	    id->prep != EAP_PWD_PREP_SSHA1 &&
288*a1157835SDaniel Fojt 	    id->prep != EAP_PWD_PREP_SSHA256 &&
289*a1157835SDaniel Fojt 	    id->prep != EAP_PWD_PREP_SSHA512) {
290*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
291*a1157835SDaniel Fojt 			   "EAP-PWD: Unsupported password pre-processing technique (Prep=%u)",
292*a1157835SDaniel Fojt 			   id->prep);
293*a1157835SDaniel Fojt 		eap_pwd_state(data, FAILURE);
294*a1157835SDaniel Fojt 		return;
295*a1157835SDaniel Fojt 	}
296*a1157835SDaniel Fojt 
297*a1157835SDaniel Fojt 	if (id->prep == EAP_PWD_PREP_NONE && data->password_hash) {
298*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
299*a1157835SDaniel Fojt 			   "EAP-PWD: Unhashed password not available");
3003ff40c12SJohn Marino 		eap_pwd_state(data, FAILURE);
3013ff40c12SJohn Marino 		return;
3023ff40c12SJohn Marino 	}
3033ff40c12SJohn Marino 
3043ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "EAP-PWD (peer): using group %d",
3053ff40c12SJohn Marino 		   data->group_num);
3063ff40c12SJohn Marino 
307*a1157835SDaniel Fojt 	data->prep = id->prep;
308*a1157835SDaniel Fojt 	os_memcpy(data->token, id->token, sizeof(id->token));
309*a1157835SDaniel Fojt 
310*a1157835SDaniel Fojt 	if (data->id_server || data->grp) {
311*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "EAP-pwd: data was already allocated");
312*a1157835SDaniel Fojt 		eap_pwd_state(data, FAILURE);
313*a1157835SDaniel Fojt 		return;
314*a1157835SDaniel Fojt 	}
315*a1157835SDaniel Fojt 
3163ff40c12SJohn Marino 	data->id_server = os_malloc(payload_len - sizeof(struct eap_pwd_id));
3173ff40c12SJohn Marino 	if (data->id_server == NULL) {
3183ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail");
3193ff40c12SJohn Marino 		eap_pwd_state(data, FAILURE);
3203ff40c12SJohn Marino 		return;
3213ff40c12SJohn Marino 	}
3223ff40c12SJohn Marino 	data->id_server_len = payload_len - sizeof(struct eap_pwd_id);
3233ff40c12SJohn Marino 	os_memcpy(data->id_server, id->identity, data->id_server_len);
3243ff40c12SJohn Marino 	wpa_hexdump_ascii(MSG_INFO, "EAP-PWD (peer): server sent id of",
3253ff40c12SJohn Marino 			  data->id_server, data->id_server_len);
3263ff40c12SJohn Marino 
327*a1157835SDaniel Fojt 	data->grp = get_eap_pwd_group(data->group_num);
328*a1157835SDaniel Fojt 	if (data->grp == NULL) {
3293ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD: failed to allocate memory for "
3303ff40c12SJohn Marino 			   "group");
3313ff40c12SJohn Marino 		eap_pwd_state(data, FAILURE);
3323ff40c12SJohn Marino 		return;
3333ff40c12SJohn Marino 	}
3343ff40c12SJohn Marino 
3353ff40c12SJohn Marino 	data->outbuf = wpabuf_alloc(sizeof(struct eap_pwd_id) +
3363ff40c12SJohn Marino 				    data->id_peer_len);
3373ff40c12SJohn Marino 	if (data->outbuf == NULL) {
3383ff40c12SJohn Marino 		eap_pwd_state(data, FAILURE);
3393ff40c12SJohn Marino 		return;
3403ff40c12SJohn Marino 	}
3413ff40c12SJohn Marino 	wpabuf_put_be16(data->outbuf, data->group_num);
3423ff40c12SJohn Marino 	wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_RAND_FUNC);
3433ff40c12SJohn Marino 	wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_PRF);
3443ff40c12SJohn Marino 	wpabuf_put_data(data->outbuf, id->token, sizeof(id->token));
345*a1157835SDaniel Fojt 	wpabuf_put_u8(data->outbuf, id->prep);
3463ff40c12SJohn Marino 	wpabuf_put_data(data->outbuf, data->id_peer, data->id_peer_len);
3473ff40c12SJohn Marino 
3483ff40c12SJohn Marino 	eap_pwd_state(data, PWD_Commit_Req);
3493ff40c12SJohn Marino }
3503ff40c12SJohn Marino 
3513ff40c12SJohn Marino 
3523ff40c12SJohn Marino static void
eap_pwd_perform_commit_exchange(struct eap_sm * sm,struct eap_pwd_data * data,struct eap_method_ret * ret,const struct wpabuf * reqData,const u8 * payload,size_t payload_len)3533ff40c12SJohn Marino eap_pwd_perform_commit_exchange(struct eap_sm *sm, struct eap_pwd_data *data,
3543ff40c12SJohn Marino 				struct eap_method_ret *ret,
3553ff40c12SJohn Marino 				const struct wpabuf *reqData,
3563ff40c12SJohn Marino 				const u8 *payload, size_t payload_len)
3573ff40c12SJohn Marino {
358*a1157835SDaniel Fojt 	struct crypto_ec_point *K = NULL;
359*a1157835SDaniel Fojt 	struct crypto_bignum *mask = NULL;
360*a1157835SDaniel Fojt 	const u8 *ptr = payload;
361*a1157835SDaniel Fojt 	u8 *scalar, *element;
362*a1157835SDaniel Fojt 	size_t prime_len, order_len;
363*a1157835SDaniel Fojt 	const u8 *password;
364*a1157835SDaniel Fojt 	size_t password_len;
365*a1157835SDaniel Fojt 	u8 pwhashhash[16];
366*a1157835SDaniel Fojt 	const u8 *salt_pwd[2];
367*a1157835SDaniel Fojt 	size_t salt_pwd_len[2], exp_len;
368*a1157835SDaniel Fojt 	u8 salt_len, salthashpwd[64]; /* 64 = SHA512_DIGEST_LENGTH */
369*a1157835SDaniel Fojt 	int res;
3703ff40c12SJohn Marino 
371*a1157835SDaniel Fojt 	if (data->state != PWD_Commit_Req) {
372*a1157835SDaniel Fojt 		ret->ignore = TRUE;
373*a1157835SDaniel Fojt 		goto fin;
374*a1157835SDaniel Fojt 	}
375*a1157835SDaniel Fojt 
376*a1157835SDaniel Fojt 	if (!data->grp) {
377*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
378*a1157835SDaniel Fojt 			   "EAP-PWD (client): uninitialized EAP-pwd group");
379*a1157835SDaniel Fojt 		ret->ignore = TRUE;
380*a1157835SDaniel Fojt 		goto fin;
381*a1157835SDaniel Fojt 	}
382*a1157835SDaniel Fojt 
383*a1157835SDaniel Fojt 	prime_len = crypto_ec_prime_len(data->grp->group);
384*a1157835SDaniel Fojt 	order_len = crypto_ec_order_len(data->grp->group);
385*a1157835SDaniel Fojt 
386*a1157835SDaniel Fojt 	switch (data->prep) {
387*a1157835SDaniel Fojt 	case EAP_PWD_PREP_MS:
388*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
389*a1157835SDaniel Fojt 			   "EAP-pwd commit request, password prep is MS");
390*a1157835SDaniel Fojt #ifdef CONFIG_FIPS
391*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR,
392*a1157835SDaniel Fojt 			   "EAP-PWD (peer): MS password hash not supported in FIPS mode");
393*a1157835SDaniel Fojt 		eap_pwd_state(data, FAILURE);
394*a1157835SDaniel Fojt 		return;
395*a1157835SDaniel Fojt #else /* CONFIG_FIPS */
396*a1157835SDaniel Fojt 		if (payload_len != 2 * prime_len + order_len) {
397*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
398*a1157835SDaniel Fojt 				   "EAP-pwd: Unexpected Commit payload length %u (expected %u)",
399*a1157835SDaniel Fojt 				   (unsigned int) payload_len,
400*a1157835SDaniel Fojt 				   (unsigned int) (2 * prime_len + order_len));
401*a1157835SDaniel Fojt 			goto fin;
402*a1157835SDaniel Fojt 		}
403*a1157835SDaniel Fojt 		if (data->password_hash) {
404*a1157835SDaniel Fojt 			res = hash_nt_password_hash(data->password, pwhashhash);
405*a1157835SDaniel Fojt 		} else {
406*a1157835SDaniel Fojt 			u8 pwhash[16];
407*a1157835SDaniel Fojt 
408*a1157835SDaniel Fojt 			res = nt_password_hash(data->password,
409*a1157835SDaniel Fojt 					       data->password_len, pwhash);
410*a1157835SDaniel Fojt 			if (res == 0)
411*a1157835SDaniel Fojt 				res = hash_nt_password_hash(pwhash, pwhashhash);
412*a1157835SDaniel Fojt 			forced_memzero(pwhash, sizeof(pwhash));
413*a1157835SDaniel Fojt 		}
414*a1157835SDaniel Fojt 
415*a1157835SDaniel Fojt 		if (res) {
416*a1157835SDaniel Fojt 			eap_pwd_state(data, FAILURE);
417*a1157835SDaniel Fojt 			return;
418*a1157835SDaniel Fojt 		}
419*a1157835SDaniel Fojt 
420*a1157835SDaniel Fojt 		password = pwhashhash;
421*a1157835SDaniel Fojt 		password_len = sizeof(pwhashhash);
422*a1157835SDaniel Fojt #endif /* CONFIG_FIPS */
423*a1157835SDaniel Fojt 		break;
424*a1157835SDaniel Fojt 	case EAP_PWD_PREP_SSHA1:
425*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
426*a1157835SDaniel Fojt 			   "EAP-pwd commit request, password prep is salted sha1");
427*a1157835SDaniel Fojt 		if (payload_len < 1 || *ptr == 0) {
428*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "EAP-pwd: Invalid Salt-len");
429*a1157835SDaniel Fojt 			goto fin;
430*a1157835SDaniel Fojt 		}
431*a1157835SDaniel Fojt 		salt_len = *ptr++;
432*a1157835SDaniel Fojt 		exp_len = 1 + salt_len + 2 * prime_len + order_len;
433*a1157835SDaniel Fojt 		if (payload_len != exp_len) {
434*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
435*a1157835SDaniel Fojt 				   "EAP-pwd: Unexpected Commit payload length %u (expected %u)",
436*a1157835SDaniel Fojt 				   (unsigned int) payload_len,
437*a1157835SDaniel Fojt 				   (unsigned int) exp_len);
438*a1157835SDaniel Fojt 			goto fin;
439*a1157835SDaniel Fojt 		}
440*a1157835SDaniel Fojt 
441*a1157835SDaniel Fojt 		/* salted-password = Hash(password | salt) */
442*a1157835SDaniel Fojt 		wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: Unsalted password",
443*a1157835SDaniel Fojt 				data->password, data->password_len);
444*a1157835SDaniel Fojt 		wpa_hexdump(MSG_DEBUG, "EAP-pwd: Salt", ptr, salt_len);
445*a1157835SDaniel Fojt 		salt_pwd[0] = data->password;
446*a1157835SDaniel Fojt 		salt_pwd[1] = ptr;
447*a1157835SDaniel Fojt 		salt_pwd_len[0] = data->password_len;
448*a1157835SDaniel Fojt 		salt_pwd_len[1] = salt_len;
449*a1157835SDaniel Fojt 		if (sha1_vector(2, salt_pwd, salt_pwd_len, salthashpwd) < 0)
450*a1157835SDaniel Fojt 			goto fin;
451*a1157835SDaniel Fojt 
452*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
453*a1157835SDaniel Fojt 			   "EAP-pwd: sha1 hashed %d byte salt with password",
454*a1157835SDaniel Fojt 			   (int) salt_len);
455*a1157835SDaniel Fojt 		ptr += salt_len;
456*a1157835SDaniel Fojt 		password = salthashpwd;
457*a1157835SDaniel Fojt 		password_len = SHA1_MAC_LEN;
458*a1157835SDaniel Fojt 		wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: Salted password",
459*a1157835SDaniel Fojt 				password, password_len);
460*a1157835SDaniel Fojt 		break;
461*a1157835SDaniel Fojt 	case EAP_PWD_PREP_SSHA256:
462*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
463*a1157835SDaniel Fojt 			   "EAP-pwd commit request, password prep is salted sha256");
464*a1157835SDaniel Fojt 		if (payload_len < 1 || *ptr == 0) {
465*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "EAP-pwd: Invalid Salt-len");
466*a1157835SDaniel Fojt 			goto fin;
467*a1157835SDaniel Fojt 		}
468*a1157835SDaniel Fojt 		salt_len = *ptr++;
469*a1157835SDaniel Fojt 		exp_len = 1 + salt_len + 2 * prime_len + order_len;
470*a1157835SDaniel Fojt 		if (payload_len != exp_len) {
471*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
472*a1157835SDaniel Fojt 				   "EAP-pwd: Unexpected Commit payload length %u (expected %u)",
473*a1157835SDaniel Fojt 				   (unsigned int) payload_len,
474*a1157835SDaniel Fojt 				   (unsigned int) exp_len);
475*a1157835SDaniel Fojt 			goto fin;
476*a1157835SDaniel Fojt 		}
477*a1157835SDaniel Fojt 
478*a1157835SDaniel Fojt 		/* salted-password = Hash(password | salt) */
479*a1157835SDaniel Fojt 		wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: Unsalted password",
480*a1157835SDaniel Fojt 				data->password, data->password_len);
481*a1157835SDaniel Fojt 		wpa_hexdump(MSG_DEBUG, "EAP-pwd: Salt", ptr, salt_len);
482*a1157835SDaniel Fojt 		salt_pwd[0] = data->password;
483*a1157835SDaniel Fojt 		salt_pwd[1] = ptr;
484*a1157835SDaniel Fojt 		salt_pwd_len[0] = data->password_len;
485*a1157835SDaniel Fojt 		salt_pwd_len[1] = salt_len;
486*a1157835SDaniel Fojt 		if (sha256_vector(2, salt_pwd, salt_pwd_len, salthashpwd) < 0)
487*a1157835SDaniel Fojt 			goto fin;
488*a1157835SDaniel Fojt 
489*a1157835SDaniel Fojt 		ptr += salt_len;
490*a1157835SDaniel Fojt 		password = salthashpwd;
491*a1157835SDaniel Fojt 		password_len = SHA256_MAC_LEN;
492*a1157835SDaniel Fojt 		wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: Salted password",
493*a1157835SDaniel Fojt 				password, password_len);
494*a1157835SDaniel Fojt 		break;
495*a1157835SDaniel Fojt #ifdef CONFIG_SHA512
496*a1157835SDaniel Fojt 	case EAP_PWD_PREP_SSHA512:
497*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
498*a1157835SDaniel Fojt 			   "EAP-pwd commit request, password prep is salted sha512");
499*a1157835SDaniel Fojt 		if (payload_len < 1 || *ptr == 0) {
500*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "EAP-pwd: Invalid Salt-len");
501*a1157835SDaniel Fojt 			goto fin;
502*a1157835SDaniel Fojt 		}
503*a1157835SDaniel Fojt 		salt_len = *ptr++;
504*a1157835SDaniel Fojt 		exp_len = 1 + salt_len + 2 * prime_len + order_len;
505*a1157835SDaniel Fojt 		if (payload_len != exp_len) {
506*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
507*a1157835SDaniel Fojt 				   "EAP-pwd: Unexpected Commit payload length %u (expected %u)",
508*a1157835SDaniel Fojt 				   (unsigned int) payload_len,
509*a1157835SDaniel Fojt 				   (unsigned int) exp_len);
510*a1157835SDaniel Fojt 			goto fin;
511*a1157835SDaniel Fojt 		}
512*a1157835SDaniel Fojt 
513*a1157835SDaniel Fojt 		/* salted-password = Hash(password | salt) */
514*a1157835SDaniel Fojt 		wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: Unsalted password",
515*a1157835SDaniel Fojt 				data->password, data->password_len);
516*a1157835SDaniel Fojt 		wpa_hexdump(MSG_DEBUG, "EAP-pwd: Salt", ptr, salt_len);
517*a1157835SDaniel Fojt 		salt_pwd[0] = data->password;
518*a1157835SDaniel Fojt 		salt_pwd[1] = ptr;
519*a1157835SDaniel Fojt 		salt_pwd_len[0] = data->password_len;
520*a1157835SDaniel Fojt 		salt_pwd_len[1] = salt_len;
521*a1157835SDaniel Fojt 		if (sha512_vector(2, salt_pwd, salt_pwd_len, salthashpwd) < 0)
522*a1157835SDaniel Fojt 			goto fin;
523*a1157835SDaniel Fojt 
524*a1157835SDaniel Fojt 		ptr += salt_len;
525*a1157835SDaniel Fojt 		password = salthashpwd;
526*a1157835SDaniel Fojt 		password_len = SHA512_MAC_LEN;
527*a1157835SDaniel Fojt 		wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: Salted password",
528*a1157835SDaniel Fojt 				password, password_len);
529*a1157835SDaniel Fojt 		break;
530*a1157835SDaniel Fojt #endif /* CONFIG_SHA512 */
531*a1157835SDaniel Fojt 	case EAP_PWD_PREP_NONE:
532*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
533*a1157835SDaniel Fojt 			   "EAP-pwd commit request, password prep is NONE");
534*a1157835SDaniel Fojt 		if (data->password_hash) {
535*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
536*a1157835SDaniel Fojt 				   "EAP-PWD: Unhashed password not available");
537*a1157835SDaniel Fojt 			eap_pwd_state(data, FAILURE);
538*a1157835SDaniel Fojt 			return;
539*a1157835SDaniel Fojt 		}
540*a1157835SDaniel Fojt 		if (payload_len != 2 * prime_len + order_len) {
541*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
542*a1157835SDaniel Fojt 				   "EAP-pwd: Unexpected Commit payload length %u (expected %u)",
543*a1157835SDaniel Fojt 				   (unsigned int) payload_len,
544*a1157835SDaniel Fojt 				   (unsigned int) (2 * prime_len + order_len));
545*a1157835SDaniel Fojt 			goto fin;
546*a1157835SDaniel Fojt 		}
547*a1157835SDaniel Fojt 		password = data->password;
548*a1157835SDaniel Fojt 		password_len = data->password_len;
549*a1157835SDaniel Fojt 		break;
550*a1157835SDaniel Fojt 	default:
551*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
552*a1157835SDaniel Fojt 			   "EAP-pwd: Unsupported password pre-processing technique (Prep=%u)",
553*a1157835SDaniel Fojt 			   data->prep);
554*a1157835SDaniel Fojt 		eap_pwd_state(data, FAILURE);
555*a1157835SDaniel Fojt 		return;
556*a1157835SDaniel Fojt 	}
557*a1157835SDaniel Fojt 
558*a1157835SDaniel Fojt 	/* compute PWE */
559*a1157835SDaniel Fojt 	res = compute_password_element(data->grp, data->group_num,
560*a1157835SDaniel Fojt 				       password, password_len,
561*a1157835SDaniel Fojt 				       data->id_server, data->id_server_len,
562*a1157835SDaniel Fojt 				       data->id_peer, data->id_peer_len,
563*a1157835SDaniel Fojt 				       data->token);
564*a1157835SDaniel Fojt 	forced_memzero(pwhashhash, sizeof(pwhashhash));
565*a1157835SDaniel Fojt 	forced_memzero(salthashpwd, sizeof(salthashpwd));
566*a1157835SDaniel Fojt 	if (res) {
567*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to compute PWE");
568*a1157835SDaniel Fojt 		eap_pwd_state(data, FAILURE);
569*a1157835SDaniel Fojt 		return;
570*a1157835SDaniel Fojt 	}
571*a1157835SDaniel Fojt 
572*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "EAP-PWD (peer): computed %d bit PWE...",
573*a1157835SDaniel Fojt 		   (int) crypto_ec_prime_len_bits(data->grp->group));
574*a1157835SDaniel Fojt 
575*a1157835SDaniel Fojt 	data->private_value = crypto_bignum_init();
576*a1157835SDaniel Fojt 	data->my_element = crypto_ec_point_init(data->grp->group);
577*a1157835SDaniel Fojt 	data->my_scalar = crypto_bignum_init();
578*a1157835SDaniel Fojt 	mask = crypto_bignum_init();
579*a1157835SDaniel Fojt 	if (!data->private_value || !data->my_element ||
580*a1157835SDaniel Fojt 	    !data->my_scalar || !mask) {
5813ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): scalar allocation fail");
5823ff40c12SJohn Marino 		goto fin;
5833ff40c12SJohn Marino 	}
5843ff40c12SJohn Marino 
585*a1157835SDaniel Fojt 	if (eap_pwd_get_rand_mask(data->grp, data->private_value, mask,
586*a1157835SDaniel Fojt 				  data->my_scalar) < 0)
5873ff40c12SJohn Marino 		goto fin;
5883ff40c12SJohn Marino 
589*a1157835SDaniel Fojt 	if (crypto_ec_point_mul(data->grp->group, data->grp->pwe, mask,
590*a1157835SDaniel Fojt 				data->my_element) < 0) {
5913ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): element allocation "
5923ff40c12SJohn Marino 			   "fail");
5933ff40c12SJohn Marino 		eap_pwd_state(data, FAILURE);
5943ff40c12SJohn Marino 		goto fin;
5953ff40c12SJohn Marino 	}
5963ff40c12SJohn Marino 
597*a1157835SDaniel Fojt 	if (crypto_ec_point_invert(data->grp->group, data->my_element) < 0) {
5983ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): element inversion fail");
5993ff40c12SJohn Marino 		goto fin;
6003ff40c12SJohn Marino 	}
6013ff40c12SJohn Marino 
6023ff40c12SJohn Marino 	/* process the request */
603*a1157835SDaniel Fojt 	data->k = crypto_bignum_init();
604*a1157835SDaniel Fojt 	K = crypto_ec_point_init(data->grp->group);
605*a1157835SDaniel Fojt 	if (!data->k || !K) {
6063ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): peer data allocation "
6073ff40c12SJohn Marino 			   "fail");
6083ff40c12SJohn Marino 		goto fin;
6093ff40c12SJohn Marino 	}
6103ff40c12SJohn Marino 
6113ff40c12SJohn Marino 	/* element, x then y, followed by scalar */
612*a1157835SDaniel Fojt 	data->server_element = eap_pwd_get_element(data->grp, ptr);
613*a1157835SDaniel Fojt 	if (!data->server_element) {
6143ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): setting peer element "
6153ff40c12SJohn Marino 			   "fail");
6163ff40c12SJohn Marino 		goto fin;
6173ff40c12SJohn Marino 	}
618*a1157835SDaniel Fojt 	ptr += prime_len * 2;
619*a1157835SDaniel Fojt 	data->server_scalar = eap_pwd_get_scalar(data->grp, ptr);
620*a1157835SDaniel Fojt 	if (!data->server_scalar) {
621*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
622*a1157835SDaniel Fojt 			   "EAP-PWD (peer): setting peer scalar fail");
6233ff40c12SJohn Marino 		goto fin;
6243ff40c12SJohn Marino 	}
6253ff40c12SJohn Marino 
6263ff40c12SJohn Marino 	/* compute the shared key, k */
627*a1157835SDaniel Fojt 	if (crypto_ec_point_mul(data->grp->group, data->grp->pwe,
628*a1157835SDaniel Fojt 				data->server_scalar, K) < 0 ||
629*a1157835SDaniel Fojt 	    crypto_ec_point_add(data->grp->group, K, data->server_element,
630*a1157835SDaniel Fojt 				K) < 0 ||
631*a1157835SDaniel Fojt 	    crypto_ec_point_mul(data->grp->group, K, data->private_value,
632*a1157835SDaniel Fojt 				K) < 0) {
6333ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): computing shared key "
6343ff40c12SJohn Marino 			   "fail");
6353ff40c12SJohn Marino 		goto fin;
6363ff40c12SJohn Marino 	}
6373ff40c12SJohn Marino 
6383ff40c12SJohn Marino 	/*
639*a1157835SDaniel Fojt 	 * This check is strictly speaking just for the case where
6403ff40c12SJohn Marino 	 * co-factor > 1 but it was suggested that even though this is probably
6413ff40c12SJohn Marino 	 * never going to happen it is a simple and safe check "just to be
6423ff40c12SJohn Marino 	 * sure" so let's be safe.
6433ff40c12SJohn Marino 	 */
644*a1157835SDaniel Fojt 	if (crypto_ec_point_is_at_infinity(data->grp->group, K)) {
6453ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): shared key point is at "
6463ff40c12SJohn Marino 			   "infinity!\n");
6473ff40c12SJohn Marino 		goto fin;
6483ff40c12SJohn Marino 	}
6493ff40c12SJohn Marino 
650*a1157835SDaniel Fojt 	if (crypto_ec_point_x(data->grp->group, K, data->k) < 0) {
6513ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to extract "
6523ff40c12SJohn Marino 			   "shared secret from point");
6533ff40c12SJohn Marino 		goto fin;
6543ff40c12SJohn Marino 	}
6553ff40c12SJohn Marino 
6563ff40c12SJohn Marino 	/* now do the response */
657*a1157835SDaniel Fojt 	data->outbuf = wpabuf_alloc(2 * prime_len + order_len);
658*a1157835SDaniel Fojt 	if (data->outbuf == NULL)
6593ff40c12SJohn Marino 		goto fin;
660*a1157835SDaniel Fojt 	/* We send the element as (x,y) followed by the scalar */
661*a1157835SDaniel Fojt 	element = wpabuf_put(data->outbuf, 2 * prime_len);
662*a1157835SDaniel Fojt 	scalar = wpabuf_put(data->outbuf, order_len);
6633ff40c12SJohn Marino 
6643ff40c12SJohn Marino 	/*
6653ff40c12SJohn Marino 	 * bignums occupy as little memory as possible so one that is
6663ff40c12SJohn Marino 	 * sufficiently smaller than the prime or order might need pre-pending
6673ff40c12SJohn Marino 	 * with zeros.
6683ff40c12SJohn Marino 	 */
669*a1157835SDaniel Fojt 	crypto_bignum_to_bin(data->my_scalar, scalar, order_len, order_len);
670*a1157835SDaniel Fojt 	if (crypto_ec_point_to_bin(data->grp->group, data->my_element, element,
671*a1157835SDaniel Fojt 				   element + prime_len) != 0) {
672*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "EAP-PWD (peer): point assignment fail");
6733ff40c12SJohn Marino 		goto fin;
674*a1157835SDaniel Fojt 	}
6753ff40c12SJohn Marino 
6763ff40c12SJohn Marino fin:
677*a1157835SDaniel Fojt 	crypto_bignum_deinit(mask, 1);
678*a1157835SDaniel Fojt 	crypto_ec_point_deinit(K, 1);
6793ff40c12SJohn Marino 	if (data->outbuf == NULL)
6803ff40c12SJohn Marino 		eap_pwd_state(data, FAILURE);
6813ff40c12SJohn Marino 	else
6823ff40c12SJohn Marino 		eap_pwd_state(data, PWD_Confirm_Req);
6833ff40c12SJohn Marino }
6843ff40c12SJohn Marino 
6853ff40c12SJohn Marino 
6863ff40c12SJohn Marino static void
eap_pwd_perform_confirm_exchange(struct eap_sm * sm,struct eap_pwd_data * data,struct eap_method_ret * ret,const struct wpabuf * reqData,const u8 * payload,size_t payload_len)6873ff40c12SJohn Marino eap_pwd_perform_confirm_exchange(struct eap_sm *sm, struct eap_pwd_data *data,
6883ff40c12SJohn Marino 				 struct eap_method_ret *ret,
6893ff40c12SJohn Marino 				 const struct wpabuf *reqData,
6903ff40c12SJohn Marino 				 const u8 *payload, size_t payload_len)
6913ff40c12SJohn Marino {
692*a1157835SDaniel Fojt 	struct crypto_hash *hash = NULL;
6933ff40c12SJohn Marino 	u32 cs;
6943ff40c12SJohn Marino 	u16 grp;
6953ff40c12SJohn Marino 	u8 conf[SHA256_MAC_LEN], *cruft = NULL, *ptr;
696*a1157835SDaniel Fojt 	size_t prime_len = 0, order_len = 0;
697*a1157835SDaniel Fojt 
698*a1157835SDaniel Fojt 	if (data->state != PWD_Confirm_Req) {
699*a1157835SDaniel Fojt 		ret->ignore = TRUE;
700*a1157835SDaniel Fojt 		goto fin;
701*a1157835SDaniel Fojt 	}
702*a1157835SDaniel Fojt 
703*a1157835SDaniel Fojt 	if (payload_len != SHA256_MAC_LEN) {
704*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
705*a1157835SDaniel Fojt 			   "EAP-pwd: Unexpected Confirm payload length %u (expected %u)",
706*a1157835SDaniel Fojt 			   (unsigned int) payload_len, SHA256_MAC_LEN);
707*a1157835SDaniel Fojt 		goto fin;
708*a1157835SDaniel Fojt 	}
709*a1157835SDaniel Fojt 
710*a1157835SDaniel Fojt 	prime_len = crypto_ec_prime_len(data->grp->group);
711*a1157835SDaniel Fojt 	order_len = crypto_ec_order_len(data->grp->group);
7123ff40c12SJohn Marino 
7133ff40c12SJohn Marino 	/*
7143ff40c12SJohn Marino 	 * first build up the ciphersuite which is group | random_function |
7153ff40c12SJohn Marino 	 *	prf
7163ff40c12SJohn Marino 	 */
7173ff40c12SJohn Marino 	grp = htons(data->group_num);
7183ff40c12SJohn Marino 	ptr = (u8 *) &cs;
7193ff40c12SJohn Marino 	os_memcpy(ptr, &grp, sizeof(u16));
7203ff40c12SJohn Marino 	ptr += sizeof(u16);
7213ff40c12SJohn Marino 	*ptr = EAP_PWD_DEFAULT_RAND_FUNC;
7223ff40c12SJohn Marino 	ptr += sizeof(u8);
7233ff40c12SJohn Marino 	*ptr = EAP_PWD_DEFAULT_PRF;
7243ff40c12SJohn Marino 
725*a1157835SDaniel Fojt 	/* each component of the point will be at most as big as the prime */
726*a1157835SDaniel Fojt 	cruft = os_malloc(prime_len * 2);
727*a1157835SDaniel Fojt 	if (!cruft) {
7283ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (server): confirm allocation "
7293ff40c12SJohn Marino 			   "fail");
7303ff40c12SJohn Marino 		goto fin;
7313ff40c12SJohn Marino 	}
7323ff40c12SJohn Marino 
7333ff40c12SJohn Marino 	/*
7343ff40c12SJohn Marino 	 * server's commit is H(k | server_element | server_scalar |
7353ff40c12SJohn Marino 	 *			peer_element | peer_scalar | ciphersuite)
7363ff40c12SJohn Marino 	 */
7373ff40c12SJohn Marino 	hash = eap_pwd_h_init();
7383ff40c12SJohn Marino 	if (hash == NULL)
7393ff40c12SJohn Marino 		goto fin;
7403ff40c12SJohn Marino 
7413ff40c12SJohn Marino 	/*
7423ff40c12SJohn Marino 	 * zero the memory each time because this is mod prime math and some
7433ff40c12SJohn Marino 	 * value may start with a few zeros and the previous one did not.
7443ff40c12SJohn Marino 	 */
745*a1157835SDaniel Fojt 	crypto_bignum_to_bin(data->k, cruft, prime_len, prime_len);
746*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, prime_len);
7473ff40c12SJohn Marino 
7483ff40c12SJohn Marino 	/* server element: x, y */
749*a1157835SDaniel Fojt 	if (crypto_ec_point_to_bin(data->grp->group, data->server_element,
750*a1157835SDaniel Fojt 				   cruft, cruft + prime_len) != 0) {
7513ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
7523ff40c12SJohn Marino 			   "assignment fail");
7533ff40c12SJohn Marino 		goto fin;
7543ff40c12SJohn Marino 	}
755*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, prime_len * 2);
7563ff40c12SJohn Marino 
7573ff40c12SJohn Marino 	/* server scalar */
758*a1157835SDaniel Fojt 	crypto_bignum_to_bin(data->server_scalar, cruft, order_len, order_len);
759*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, order_len);
7603ff40c12SJohn Marino 
7613ff40c12SJohn Marino 	/* my element: x, y */
762*a1157835SDaniel Fojt 	if (crypto_ec_point_to_bin(data->grp->group, data->my_element, cruft,
763*a1157835SDaniel Fojt 				   cruft + prime_len) != 0) {
7643ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
7653ff40c12SJohn Marino 			   "assignment fail");
7663ff40c12SJohn Marino 		goto fin;
7673ff40c12SJohn Marino 	}
768*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, prime_len * 2);
7693ff40c12SJohn Marino 
7703ff40c12SJohn Marino 	/* my scalar */
771*a1157835SDaniel Fojt 	crypto_bignum_to_bin(data->my_scalar, cruft, order_len, order_len);
772*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, order_len);
7733ff40c12SJohn Marino 
7743ff40c12SJohn Marino 	/* the ciphersuite */
7753ff40c12SJohn Marino 	eap_pwd_h_update(hash, (u8 *) &cs, sizeof(u32));
7763ff40c12SJohn Marino 
7773ff40c12SJohn Marino 	/* random function fin */
7783ff40c12SJohn Marino 	eap_pwd_h_final(hash, conf);
779*a1157835SDaniel Fojt 	hash = NULL;
7803ff40c12SJohn Marino 
7813ff40c12SJohn Marino 	ptr = (u8 *) payload;
782*a1157835SDaniel Fojt 	if (os_memcmp_const(conf, ptr, SHA256_MAC_LEN)) {
7833ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm did not verify");
7843ff40c12SJohn Marino 		goto fin;
7853ff40c12SJohn Marino 	}
7863ff40c12SJohn Marino 
7873ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "EAP-pwd (peer): confirm verified");
7883ff40c12SJohn Marino 
7893ff40c12SJohn Marino 	/*
7903ff40c12SJohn Marino 	 * compute confirm:
7913ff40c12SJohn Marino 	 *  H(k | peer_element | peer_scalar | server_element | server_scalar |
7923ff40c12SJohn Marino 	 *    ciphersuite)
7933ff40c12SJohn Marino 	 */
7943ff40c12SJohn Marino 	hash = eap_pwd_h_init();
7953ff40c12SJohn Marino 	if (hash == NULL)
7963ff40c12SJohn Marino 		goto fin;
7973ff40c12SJohn Marino 
7983ff40c12SJohn Marino 	/* k */
799*a1157835SDaniel Fojt 	crypto_bignum_to_bin(data->k, cruft, prime_len, prime_len);
800*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, prime_len);
8013ff40c12SJohn Marino 
8023ff40c12SJohn Marino 	/* my element */
803*a1157835SDaniel Fojt 	if (crypto_ec_point_to_bin(data->grp->group, data->my_element, cruft,
804*a1157835SDaniel Fojt 				   cruft + prime_len) != 0) {
8053ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm point "
8063ff40c12SJohn Marino 			   "assignment fail");
8073ff40c12SJohn Marino 		goto fin;
8083ff40c12SJohn Marino 	}
809*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, prime_len * 2);
8103ff40c12SJohn Marino 
8113ff40c12SJohn Marino 	/* my scalar */
812*a1157835SDaniel Fojt 	crypto_bignum_to_bin(data->my_scalar, cruft, order_len, order_len);
813*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, order_len);
8143ff40c12SJohn Marino 
8153ff40c12SJohn Marino 	/* server element: x, y */
816*a1157835SDaniel Fojt 	if (crypto_ec_point_to_bin(data->grp->group, data->server_element,
817*a1157835SDaniel Fojt 				   cruft, cruft + prime_len) != 0) {
8183ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm point "
8193ff40c12SJohn Marino 			   "assignment fail");
8203ff40c12SJohn Marino 		goto fin;
8213ff40c12SJohn Marino 	}
822*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, prime_len * 2);
8233ff40c12SJohn Marino 
8243ff40c12SJohn Marino 	/* server scalar */
825*a1157835SDaniel Fojt 	crypto_bignum_to_bin(data->server_scalar, cruft, order_len, order_len);
826*a1157835SDaniel Fojt 	eap_pwd_h_update(hash, cruft, order_len);
8273ff40c12SJohn Marino 
8283ff40c12SJohn Marino 	/* the ciphersuite */
8293ff40c12SJohn Marino 	eap_pwd_h_update(hash, (u8 *) &cs, sizeof(u32));
8303ff40c12SJohn Marino 
8313ff40c12SJohn Marino 	/* all done */
8323ff40c12SJohn Marino 	eap_pwd_h_final(hash, conf);
833*a1157835SDaniel Fojt 	hash = NULL;
8343ff40c12SJohn Marino 
835*a1157835SDaniel Fojt 	if (compute_keys(data->grp, data->k,
8363ff40c12SJohn Marino 			 data->my_scalar, data->server_scalar, conf, ptr,
837*a1157835SDaniel Fojt 			 &cs, data->msk, data->emsk, data->session_id) < 0) {
8383ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to compute MSK | "
8393ff40c12SJohn Marino 			   "EMSK");
8403ff40c12SJohn Marino 		goto fin;
8413ff40c12SJohn Marino 	}
8423ff40c12SJohn Marino 
8433ff40c12SJohn Marino 	data->outbuf = wpabuf_alloc(SHA256_MAC_LEN);
8443ff40c12SJohn Marino 	if (data->outbuf == NULL)
8453ff40c12SJohn Marino 		goto fin;
8463ff40c12SJohn Marino 
8473ff40c12SJohn Marino 	wpabuf_put_data(data->outbuf, conf, SHA256_MAC_LEN);
8483ff40c12SJohn Marino 
8493ff40c12SJohn Marino fin:
850*a1157835SDaniel Fojt 	bin_clear_free(cruft, prime_len * 2);
8513ff40c12SJohn Marino 	if (data->outbuf == NULL) {
852*a1157835SDaniel Fojt 		ret->methodState = METHOD_DONE;
8533ff40c12SJohn Marino 		ret->decision = DECISION_FAIL;
8543ff40c12SJohn Marino 		eap_pwd_state(data, FAILURE);
8553ff40c12SJohn Marino 	} else {
856*a1157835SDaniel Fojt 		eap_pwd_state(data, SUCCESS_ON_FRAG_COMPLETION);
8573ff40c12SJohn Marino 	}
858*a1157835SDaniel Fojt 
859*a1157835SDaniel Fojt 	/* clean allocated memory */
860*a1157835SDaniel Fojt 	if (hash)
861*a1157835SDaniel Fojt 		eap_pwd_h_final(hash, conf);
8623ff40c12SJohn Marino }
8633ff40c12SJohn Marino 
8643ff40c12SJohn Marino 
8653ff40c12SJohn Marino static struct wpabuf *
eap_pwd_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)8663ff40c12SJohn Marino eap_pwd_process(struct eap_sm *sm, void *priv, struct eap_method_ret *ret,
8673ff40c12SJohn Marino 		const struct wpabuf *reqData)
8683ff40c12SJohn Marino {
8693ff40c12SJohn Marino 	struct eap_pwd_data *data = priv;
8703ff40c12SJohn Marino 	struct wpabuf *resp = NULL;
8713ff40c12SJohn Marino 	const u8 *pos, *buf;
8723ff40c12SJohn Marino 	size_t len;
8733ff40c12SJohn Marino 	u16 tot_len = 0;
8743ff40c12SJohn Marino 	u8 lm_exch;
8753ff40c12SJohn Marino 
8763ff40c12SJohn Marino 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, reqData, &len);
8773ff40c12SJohn Marino 	if ((pos == NULL) || (len < 1)) {
8783ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "EAP-pwd: Got a frame but pos is %s and "
8793ff40c12SJohn Marino 			   "len is %d",
8803ff40c12SJohn Marino 			   pos == NULL ? "NULL" : "not NULL", (int) len);
8813ff40c12SJohn Marino 		ret->ignore = TRUE;
8823ff40c12SJohn Marino 		return NULL;
8833ff40c12SJohn Marino 	}
8843ff40c12SJohn Marino 
8853ff40c12SJohn Marino 	ret->ignore = FALSE;
8863ff40c12SJohn Marino 	ret->methodState = METHOD_MAY_CONT;
8873ff40c12SJohn Marino 	ret->decision = DECISION_FAIL;
8883ff40c12SJohn Marino 	ret->allowNotifications = FALSE;
8893ff40c12SJohn Marino 
8903ff40c12SJohn Marino 	lm_exch = *pos;
8913ff40c12SJohn Marino 	pos++;                  /* skip over the bits and the exch */
8923ff40c12SJohn Marino 	len--;
8933ff40c12SJohn Marino 
8943ff40c12SJohn Marino 	/*
8953ff40c12SJohn Marino 	 * we're fragmenting so send out the next fragment
8963ff40c12SJohn Marino 	 */
8973ff40c12SJohn Marino 	if (data->out_frag_pos) {
8983ff40c12SJohn Marino 		/*
8993ff40c12SJohn Marino 		 * this should be an ACK
9003ff40c12SJohn Marino 		 */
9013ff40c12SJohn Marino 		if (len)
9023ff40c12SJohn Marino 			wpa_printf(MSG_INFO, "Bad Response! Fragmenting but "
9033ff40c12SJohn Marino 				   "not an ACK");
9043ff40c12SJohn Marino 
9053ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "EAP-pwd: Got an ACK for a fragment");
9063ff40c12SJohn Marino 		/*
9073ff40c12SJohn Marino 		 * check if there are going to be more fragments
9083ff40c12SJohn Marino 		 */
9093ff40c12SJohn Marino 		len = wpabuf_len(data->outbuf) - data->out_frag_pos;
9103ff40c12SJohn Marino 		if ((len + EAP_PWD_HDR_SIZE) > data->mtu) {
9113ff40c12SJohn Marino 			len = data->mtu - EAP_PWD_HDR_SIZE;
9123ff40c12SJohn Marino 			EAP_PWD_SET_MORE_BIT(lm_exch);
9133ff40c12SJohn Marino 		}
9143ff40c12SJohn Marino 		resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
9153ff40c12SJohn Marino 				     EAP_PWD_HDR_SIZE + len,
9163ff40c12SJohn Marino 				     EAP_CODE_RESPONSE, eap_get_id(reqData));
9173ff40c12SJohn Marino 		if (resp == NULL) {
9183ff40c12SJohn Marino 			wpa_printf(MSG_INFO, "Unable to allocate memory for "
9193ff40c12SJohn Marino 				   "next fragment!");
9203ff40c12SJohn Marino 			return NULL;
9213ff40c12SJohn Marino 		}
9223ff40c12SJohn Marino 		wpabuf_put_u8(resp, lm_exch);
9233ff40c12SJohn Marino 		buf = wpabuf_head_u8(data->outbuf);
9243ff40c12SJohn Marino 		wpabuf_put_data(resp, buf + data->out_frag_pos, len);
9253ff40c12SJohn Marino 		data->out_frag_pos += len;
9263ff40c12SJohn Marino 		/*
9273ff40c12SJohn Marino 		 * this is the last fragment so get rid of the out buffer
9283ff40c12SJohn Marino 		 */
9293ff40c12SJohn Marino 		if (data->out_frag_pos >= wpabuf_len(data->outbuf)) {
9303ff40c12SJohn Marino 			wpabuf_free(data->outbuf);
9313ff40c12SJohn Marino 			data->outbuf = NULL;
9323ff40c12SJohn Marino 			data->out_frag_pos = 0;
9333ff40c12SJohn Marino 		}
9343ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "EAP-pwd: Send %s fragment of %d bytes",
9353ff40c12SJohn Marino 			   data->out_frag_pos == 0 ? "last" : "next",
9363ff40c12SJohn Marino 			   (int) len);
937*a1157835SDaniel Fojt 		if (data->state == SUCCESS_ON_FRAG_COMPLETION) {
938*a1157835SDaniel Fojt 			ret->methodState = METHOD_DONE;
939*a1157835SDaniel Fojt 			ret->decision = DECISION_UNCOND_SUCC;
940*a1157835SDaniel Fojt 			eap_pwd_state(data, SUCCESS);
941*a1157835SDaniel Fojt 		}
9423ff40c12SJohn Marino 		return resp;
9433ff40c12SJohn Marino 	}
9443ff40c12SJohn Marino 
9453ff40c12SJohn Marino 	/*
9463ff40c12SJohn Marino 	 * see if this is a fragment that needs buffering
9473ff40c12SJohn Marino 	 *
9483ff40c12SJohn Marino 	 * if it's the first fragment there'll be a length field
9493ff40c12SJohn Marino 	 */
9503ff40c12SJohn Marino 	if (EAP_PWD_GET_LENGTH_BIT(lm_exch)) {
951*a1157835SDaniel Fojt 		if (len < 2) {
952*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
953*a1157835SDaniel Fojt 				   "EAP-pwd: Frame too short to contain Total-Length field");
954*a1157835SDaniel Fojt 			ret->ignore = TRUE;
955*a1157835SDaniel Fojt 			return NULL;
956*a1157835SDaniel Fojt 		}
9573ff40c12SJohn Marino 		tot_len = WPA_GET_BE16(pos);
9583ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "EAP-pwd: Incoming fragments whose "
9593ff40c12SJohn Marino 			   "total length = %d", tot_len);
960*a1157835SDaniel Fojt 		if (tot_len > 15000)
961*a1157835SDaniel Fojt 			return NULL;
962*a1157835SDaniel Fojt 		if (data->inbuf) {
963*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
964*a1157835SDaniel Fojt 				   "EAP-pwd: Unexpected new fragment start when previous fragment is still in use");
965*a1157835SDaniel Fojt 			ret->ignore = TRUE;
966*a1157835SDaniel Fojt 			return NULL;
967*a1157835SDaniel Fojt 		}
9683ff40c12SJohn Marino 		data->inbuf = wpabuf_alloc(tot_len);
9693ff40c12SJohn Marino 		if (data->inbuf == NULL) {
9703ff40c12SJohn Marino 			wpa_printf(MSG_INFO, "Out of memory to buffer "
9713ff40c12SJohn Marino 				   "fragments!");
9723ff40c12SJohn Marino 			return NULL;
9733ff40c12SJohn Marino 		}
974*a1157835SDaniel Fojt 		data->in_frag_pos = 0;
9753ff40c12SJohn Marino 		pos += sizeof(u16);
9763ff40c12SJohn Marino 		len -= sizeof(u16);
9773ff40c12SJohn Marino 	}
9783ff40c12SJohn Marino 	/*
9793ff40c12SJohn Marino 	 * buffer and ACK the fragment
9803ff40c12SJohn Marino 	 */
981*a1157835SDaniel Fojt 	if (EAP_PWD_GET_MORE_BIT(lm_exch) || data->in_frag_pos) {
982*a1157835SDaniel Fojt 		if (!data->inbuf) {
983*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
984*a1157835SDaniel Fojt 				   "EAP-pwd: No buffer for reassembly");
985*a1157835SDaniel Fojt 			ret->methodState = METHOD_DONE;
986*a1157835SDaniel Fojt 			ret->decision = DECISION_FAIL;
987*a1157835SDaniel Fojt 			return NULL;
988*a1157835SDaniel Fojt 		}
9893ff40c12SJohn Marino 		data->in_frag_pos += len;
9903ff40c12SJohn Marino 		if (data->in_frag_pos > wpabuf_size(data->inbuf)) {
9913ff40c12SJohn Marino 			wpa_printf(MSG_INFO, "EAP-pwd: Buffer overflow attack "
9923ff40c12SJohn Marino 				   "detected (%d vs. %d)!",
9933ff40c12SJohn Marino 				   (int) data->in_frag_pos,
9943ff40c12SJohn Marino 				   (int) wpabuf_len(data->inbuf));
9953ff40c12SJohn Marino 			wpabuf_free(data->inbuf);
996*a1157835SDaniel Fojt 			data->inbuf = NULL;
9973ff40c12SJohn Marino 			data->in_frag_pos = 0;
9983ff40c12SJohn Marino 			return NULL;
9993ff40c12SJohn Marino 		}
10003ff40c12SJohn Marino 		wpabuf_put_data(data->inbuf, pos, len);
1001*a1157835SDaniel Fojt 	}
1002*a1157835SDaniel Fojt 	if (EAP_PWD_GET_MORE_BIT(lm_exch)) {
10033ff40c12SJohn Marino 		resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
10043ff40c12SJohn Marino 				     EAP_PWD_HDR_SIZE,
10053ff40c12SJohn Marino 				     EAP_CODE_RESPONSE, eap_get_id(reqData));
10063ff40c12SJohn Marino 		if (resp != NULL)
10073ff40c12SJohn Marino 			wpabuf_put_u8(resp, (EAP_PWD_GET_EXCHANGE(lm_exch)));
10083ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "EAP-pwd: ACKing a %d byte fragment",
10093ff40c12SJohn Marino 			   (int) len);
10103ff40c12SJohn Marino 		return resp;
10113ff40c12SJohn Marino 	}
10123ff40c12SJohn Marino 	/*
10133ff40c12SJohn Marino 	 * we're buffering and this is the last fragment
10143ff40c12SJohn Marino 	 */
1015*a1157835SDaniel Fojt 	if (data->in_frag_pos && data->inbuf) {
10163ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "EAP-pwd: Last fragment, %d bytes",
10173ff40c12SJohn Marino 			   (int) len);
10183ff40c12SJohn Marino 		pos = wpabuf_head_u8(data->inbuf);
10193ff40c12SJohn Marino 		len = data->in_frag_pos;
10203ff40c12SJohn Marino 	}
10213ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "EAP-pwd: processing frame: exch %d, len %d",
10223ff40c12SJohn Marino 		   EAP_PWD_GET_EXCHANGE(lm_exch), (int) len);
10233ff40c12SJohn Marino 
10243ff40c12SJohn Marino 	switch (EAP_PWD_GET_EXCHANGE(lm_exch)) {
10253ff40c12SJohn Marino 	case EAP_PWD_OPCODE_ID_EXCH:
10263ff40c12SJohn Marino 		eap_pwd_perform_id_exchange(sm, data, ret, reqData,
10273ff40c12SJohn Marino 					    pos, len);
10283ff40c12SJohn Marino 		break;
10293ff40c12SJohn Marino 	case EAP_PWD_OPCODE_COMMIT_EXCH:
10303ff40c12SJohn Marino 		eap_pwd_perform_commit_exchange(sm, data, ret, reqData,
10313ff40c12SJohn Marino 						pos, len);
10323ff40c12SJohn Marino 		break;
10333ff40c12SJohn Marino 	case EAP_PWD_OPCODE_CONFIRM_EXCH:
10343ff40c12SJohn Marino 		eap_pwd_perform_confirm_exchange(sm, data, ret, reqData,
10353ff40c12SJohn Marino 						 pos, len);
10363ff40c12SJohn Marino 		break;
10373ff40c12SJohn Marino 	default:
10383ff40c12SJohn Marino 		wpa_printf(MSG_INFO, "EAP-pwd: Ignoring message with unknown "
10393ff40c12SJohn Marino 			   "opcode %d", lm_exch);
10403ff40c12SJohn Marino 		break;
10413ff40c12SJohn Marino 	}
10423ff40c12SJohn Marino 	/*
10433ff40c12SJohn Marino 	 * if we buffered the just processed input now's the time to free it
10443ff40c12SJohn Marino 	 */
10453ff40c12SJohn Marino 	if (data->in_frag_pos) {
10463ff40c12SJohn Marino 		wpabuf_free(data->inbuf);
1047*a1157835SDaniel Fojt 		data->inbuf = NULL;
10483ff40c12SJohn Marino 		data->in_frag_pos = 0;
10493ff40c12SJohn Marino 	}
10503ff40c12SJohn Marino 
1051*a1157835SDaniel Fojt 	if (data->outbuf == NULL) {
1052*a1157835SDaniel Fojt 		ret->methodState = METHOD_DONE;
1053*a1157835SDaniel Fojt 		ret->decision = DECISION_FAIL;
10543ff40c12SJohn Marino 		return NULL;        /* generic failure */
1055*a1157835SDaniel Fojt 	}
10563ff40c12SJohn Marino 
10573ff40c12SJohn Marino 	/*
10583ff40c12SJohn Marino 	 * we have output! Do we need to fragment it?
10593ff40c12SJohn Marino 	 */
1060*a1157835SDaniel Fojt 	lm_exch = EAP_PWD_GET_EXCHANGE(lm_exch);
10613ff40c12SJohn Marino 	len = wpabuf_len(data->outbuf);
10623ff40c12SJohn Marino 	if ((len + EAP_PWD_HDR_SIZE) > data->mtu) {
10633ff40c12SJohn Marino 		resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, data->mtu,
10643ff40c12SJohn Marino 				     EAP_CODE_RESPONSE, eap_get_id(reqData));
10653ff40c12SJohn Marino 		/*
10663ff40c12SJohn Marino 		 * if so it's the first so include a length field
10673ff40c12SJohn Marino 		 */
10683ff40c12SJohn Marino 		EAP_PWD_SET_LENGTH_BIT(lm_exch);
10693ff40c12SJohn Marino 		EAP_PWD_SET_MORE_BIT(lm_exch);
10703ff40c12SJohn Marino 		tot_len = len;
10713ff40c12SJohn Marino 		/*
10723ff40c12SJohn Marino 		 * keep the packet at the MTU
10733ff40c12SJohn Marino 		 */
10743ff40c12SJohn Marino 		len = data->mtu - EAP_PWD_HDR_SIZE - sizeof(u16);
10753ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "EAP-pwd: Fragmenting output, total "
10763ff40c12SJohn Marino 			   "length = %d", tot_len);
10773ff40c12SJohn Marino 	} else {
10783ff40c12SJohn Marino 		resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
10793ff40c12SJohn Marino 				     EAP_PWD_HDR_SIZE + len,
10803ff40c12SJohn Marino 				     EAP_CODE_RESPONSE, eap_get_id(reqData));
10813ff40c12SJohn Marino 	}
10823ff40c12SJohn Marino 	if (resp == NULL)
10833ff40c12SJohn Marino 		return NULL;
10843ff40c12SJohn Marino 
10853ff40c12SJohn Marino 	wpabuf_put_u8(resp, lm_exch);
10863ff40c12SJohn Marino 	if (EAP_PWD_GET_LENGTH_BIT(lm_exch)) {
10873ff40c12SJohn Marino 		wpabuf_put_be16(resp, tot_len);
10883ff40c12SJohn Marino 		data->out_frag_pos += len;
10893ff40c12SJohn Marino 	}
10903ff40c12SJohn Marino 	buf = wpabuf_head_u8(data->outbuf);
10913ff40c12SJohn Marino 	wpabuf_put_data(resp, buf, len);
10923ff40c12SJohn Marino 	/*
10933ff40c12SJohn Marino 	 * if we're not fragmenting then there's no need to carry this around
10943ff40c12SJohn Marino 	 */
10953ff40c12SJohn Marino 	if (data->out_frag_pos == 0) {
10963ff40c12SJohn Marino 		wpabuf_free(data->outbuf);
10973ff40c12SJohn Marino 		data->outbuf = NULL;
10983ff40c12SJohn Marino 		data->out_frag_pos = 0;
1099*a1157835SDaniel Fojt 		if (data->state == SUCCESS_ON_FRAG_COMPLETION) {
1100*a1157835SDaniel Fojt 			ret->methodState = METHOD_DONE;
1101*a1157835SDaniel Fojt 			ret->decision = DECISION_UNCOND_SUCC;
1102*a1157835SDaniel Fojt 			eap_pwd_state(data, SUCCESS);
1103*a1157835SDaniel Fojt 		}
11043ff40c12SJohn Marino 	}
11053ff40c12SJohn Marino 
11063ff40c12SJohn Marino 	return resp;
11073ff40c12SJohn Marino }
11083ff40c12SJohn Marino 
11093ff40c12SJohn Marino 
eap_pwd_key_available(struct eap_sm * sm,void * priv)11103ff40c12SJohn Marino static Boolean eap_pwd_key_available(struct eap_sm *sm, void *priv)
11113ff40c12SJohn Marino {
11123ff40c12SJohn Marino 	struct eap_pwd_data *data = priv;
11133ff40c12SJohn Marino 	return data->state == SUCCESS;
11143ff40c12SJohn Marino }
11153ff40c12SJohn Marino 
11163ff40c12SJohn Marino 
eap_pwd_get_emsk(struct eap_sm * sm,void * priv,size_t * len)11173ff40c12SJohn Marino static u8 * eap_pwd_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
11183ff40c12SJohn Marino {
11193ff40c12SJohn Marino 	struct eap_pwd_data *data = priv;
11203ff40c12SJohn Marino 	u8 *key;
11213ff40c12SJohn Marino 
11223ff40c12SJohn Marino 	if (data->state != SUCCESS)
11233ff40c12SJohn Marino 		return NULL;
11243ff40c12SJohn Marino 
11253ff40c12SJohn Marino 	if ((key = os_malloc(EAP_EMSK_LEN)) == NULL)
11263ff40c12SJohn Marino 		return NULL;
11273ff40c12SJohn Marino 
11283ff40c12SJohn Marino 	os_memcpy(key, data->emsk, EAP_EMSK_LEN);
11293ff40c12SJohn Marino 	*len = EAP_EMSK_LEN;
11303ff40c12SJohn Marino 
11313ff40c12SJohn Marino 	return key;
11323ff40c12SJohn Marino }
11333ff40c12SJohn Marino 
11343ff40c12SJohn Marino 
eap_peer_pwd_register(void)11353ff40c12SJohn Marino int eap_peer_pwd_register(void)
11363ff40c12SJohn Marino {
11373ff40c12SJohn Marino 	struct eap_method *eap;
11383ff40c12SJohn Marino 
11393ff40c12SJohn Marino 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
11403ff40c12SJohn Marino 				    EAP_VENDOR_IETF, EAP_TYPE_PWD, "PWD");
11413ff40c12SJohn Marino 	if (eap == NULL)
11423ff40c12SJohn Marino 		return -1;
11433ff40c12SJohn Marino 
11443ff40c12SJohn Marino 	eap->init = eap_pwd_init;
11453ff40c12SJohn Marino 	eap->deinit = eap_pwd_deinit;
11463ff40c12SJohn Marino 	eap->process = eap_pwd_process;
11473ff40c12SJohn Marino 	eap->isKeyAvailable = eap_pwd_key_available;
11483ff40c12SJohn Marino 	eap->getKey = eap_pwd_getkey;
1149*a1157835SDaniel Fojt 	eap->getSessionId = eap_pwd_get_session_id;
11503ff40c12SJohn Marino 	eap->get_emsk = eap_pwd_get_emsk;
11513ff40c12SJohn Marino 
1152*a1157835SDaniel Fojt 	return eap_peer_method_register(eap);
11533ff40c12SJohn Marino }
1154