]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
EAP-pwd: Get rid of unnecessary allocation of temporary buffer
authorJouni Malinen <jouni@codeaurora.org>
Fri, 5 Apr 2019 09:45:16 +0000 (12:45 +0300)
committerJouni Malinen <j@w1.fi>
Tue, 9 Apr 2019 14:11:15 +0000 (17:11 +0300)
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>
src/eap_peer/eap_pwd.c
src/eap_server/eap_server_pwd.c

index 5f6c0021833d1b3bfd1c028599908501be093044..4be4fcf35f653579435fc5c12136ed250f638410 100644 (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_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);
index cf6affdaf428193000ddd71585cf62e334f9b696..9799c81971bfdf38c6257a568d319ef1a0f37cee 100644 (file)
@@ -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);
 }