From 89bbe6f87a04ce5faf8c68dba235d0f7e545872f Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Fri, 5 Apr 2019 12:45:16 +0300 Subject: [PATCH] EAP-pwd: Get rid of unnecessary allocation of temporary buffer Binary presentations of element and scalar can be written directly to the allocated commit message buffer instead of having to first write them into temporary buffers just to copy them to the actual message buffer. Signed-off-by: Jouni Malinen --- src/eap_peer/eap_pwd.c | 22 ++++++---------------- src/eap_server/eap_server_pwd.c | 31 ++++++++++--------------------- 2 files changed, 16 insertions(+), 37 deletions(-) diff --git a/src/eap_peer/eap_pwd.c b/src/eap_peer/eap_pwd.c index 5f6c00218..4be4fcf35 100644 --- a/src/eap_peer/eap_pwd.c +++ b/src/eap_peer/eap_pwd.c @@ -311,7 +311,7 @@ eap_pwd_perform_commit_exchange(struct eap_sm *sm, struct eap_pwd_data *data, struct crypto_ec_point *K = NULL; struct crypto_bignum *mask = NULL, *cofactor = NULL; const u8 *ptr = payload; - u8 *scalar = NULL, *element = NULL; + u8 *scalar, *element; size_t prime_len, order_len; const u8 *password; size_t password_len; @@ -623,12 +623,12 @@ eap_pwd_perform_commit_exchange(struct eap_sm *sm, struct eap_pwd_data *data, } /* now do the response */ - scalar = os_zalloc(order_len); - element = os_zalloc(prime_len * 2); - if (!scalar || !element) { - wpa_printf(MSG_INFO, "EAP-PWD (peer): data allocation fail"); + data->outbuf = wpabuf_alloc(2 * prime_len + order_len); + if (data->outbuf == NULL) goto fin; - } + /* We send the element as (x,y) followed by the scalar */ + element = wpabuf_put(data->outbuf, 2 * prime_len); + scalar = wpabuf_put(data->outbuf, order_len); /* * bignums occupy as little memory as possible so one that is @@ -642,17 +642,7 @@ eap_pwd_perform_commit_exchange(struct eap_sm *sm, struct eap_pwd_data *data, goto fin; } - data->outbuf = wpabuf_alloc(order_len + 2 * prime_len); - if (data->outbuf == NULL) - goto fin; - - /* we send the element as (x,y) follwed by the scalar */ - wpabuf_put_data(data->outbuf, element, 2 * prime_len); - wpabuf_put_data(data->outbuf, scalar, order_len); - fin: - os_free(scalar); - os_free(element); crypto_bignum_deinit(mask, 1); crypto_bignum_deinit(cofactor, 1); crypto_ec_point_deinit(K, 1); diff --git a/src/eap_server/eap_server_pwd.c b/src/eap_server/eap_server_pwd.c index cf6affdaf..9799c8197 100644 --- a/src/eap_server/eap_server_pwd.c +++ b/src/eap_server/eap_server_pwd.c @@ -236,7 +236,7 @@ static void eap_pwd_build_commit_req(struct eap_sm *sm, struct eap_pwd_data *data, u8 id) { struct crypto_bignum *mask = NULL; - u8 *scalar = NULL, *element = NULL; + u8 *scalar, *element; size_t prime_len, order_len; wpa_printf(MSG_DEBUG, "EAP-pwd: Commit/Request"); @@ -279,22 +279,6 @@ static void eap_pwd_build_commit_req(struct eap_sm *sm, goto fin; } - scalar = os_malloc(order_len); - element = os_malloc(prime_len * 2); - if (!scalar || !element) { - wpa_printf(MSG_INFO, "EAP-PWD (server): data allocation fail"); - goto fin; - } - - if (crypto_ec_point_to_bin(data->grp->group, data->my_element, element, - element + prime_len) < 0) { - wpa_printf(MSG_INFO, "EAP-PWD (server): point assignment " - "fail"); - goto fin; - } - - crypto_bignum_to_bin(data->my_scalar, scalar, order_len, order_len); - data->outbuf = wpabuf_alloc(2 * prime_len + order_len + (data->salt ? 1 + data->salt_len : 0)); if (data->outbuf == NULL) @@ -307,13 +291,18 @@ static void eap_pwd_build_commit_req(struct eap_sm *sm, } /* We send the element as (x,y) followed by the scalar */ - wpabuf_put_data(data->outbuf, element, 2 * prime_len); - wpabuf_put_data(data->outbuf, scalar, order_len); + element = wpabuf_put(data->outbuf, 2 * prime_len); + scalar = wpabuf_put(data->outbuf, order_len); + crypto_bignum_to_bin(data->my_scalar, scalar, order_len, order_len); + if (crypto_ec_point_to_bin(data->grp->group, data->my_element, element, + element + prime_len) < 0) { + wpa_printf(MSG_INFO, "EAP-PWD (server): point assignment " + "fail"); + goto fin; + } fin: crypto_bignum_deinit(mask, 1); - os_free(scalar); - os_free(element); if (data->outbuf == NULL) eap_pwd_state(data, FAILURE); }