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 <jouni@codeaurora.org>
This commit is contained in:
Jouni Malinen 2019-04-05 12:45:16 +03:00 committed by Jouni Malinen
parent 4396f74a36
commit 89bbe6f87a
2 changed files with 16 additions and 37 deletions

View file

@ -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_ec_point *K = NULL;
struct crypto_bignum *mask = NULL, *cofactor = NULL; struct crypto_bignum *mask = NULL, *cofactor = NULL;
const u8 *ptr = payload; const u8 *ptr = payload;
u8 *scalar = NULL, *element = NULL; u8 *scalar, *element;
size_t prime_len, order_len; size_t prime_len, order_len;
const u8 *password; const u8 *password;
size_t password_len; 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 */ /* now do the response */
scalar = os_zalloc(order_len); data->outbuf = wpabuf_alloc(2 * prime_len + order_len);
element = os_zalloc(prime_len * 2); if (data->outbuf == NULL)
if (!scalar || !element) {
wpa_printf(MSG_INFO, "EAP-PWD (peer): data allocation fail");
goto fin; 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 * 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; 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: fin:
os_free(scalar);
os_free(element);
crypto_bignum_deinit(mask, 1); crypto_bignum_deinit(mask, 1);
crypto_bignum_deinit(cofactor, 1); crypto_bignum_deinit(cofactor, 1);
crypto_ec_point_deinit(K, 1); crypto_ec_point_deinit(K, 1);

View file

@ -236,7 +236,7 @@ static void eap_pwd_build_commit_req(struct eap_sm *sm,
struct eap_pwd_data *data, u8 id) struct eap_pwd_data *data, u8 id)
{ {
struct crypto_bignum *mask = NULL; struct crypto_bignum *mask = NULL;
u8 *scalar = NULL, *element = NULL; u8 *scalar, *element;
size_t prime_len, order_len; size_t prime_len, order_len;
wpa_printf(MSG_DEBUG, "EAP-pwd: Commit/Request"); 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; 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->outbuf = wpabuf_alloc(2 * prime_len + order_len +
(data->salt ? 1 + data->salt_len : 0)); (data->salt ? 1 + data->salt_len : 0));
if (data->outbuf == NULL) 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 */ /* We send the element as (x,y) followed by the scalar */
wpabuf_put_data(data->outbuf, element, 2 * prime_len); element = wpabuf_put(data->outbuf, 2 * prime_len);
wpabuf_put_data(data->outbuf, scalar, order_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: fin:
crypto_bignum_deinit(mask, 1); crypto_bignum_deinit(mask, 1);
os_free(scalar);
os_free(element);
if (data->outbuf == NULL) if (data->outbuf == NULL)
eap_pwd_state(data, FAILURE); eap_pwd_state(data, FAILURE);
} }