Commit a7e1cb8c authored by Vitezslav Cizek's avatar Vitezslav Cizek Committed by Matt Caswell
Browse files

apps/speed.c: properly address NO_EC2M on systems without SIGALRM



The ecdh_c array is allocated of the same size as ecdh_choices,
whose size depends on whether the support for binary curves is enabled
or not.  (The same goes for ecdsa_c).
On systems without SIGALRM, ecdh_c is indexed by predefined constants
intended for representing the index of the ciphers in the ecdh_choices
array.
However, in case of NO_EC2M some of the #defined constants won't match
and would actually access the ecdh_c out-of-bounds.

Use enum instead of a macro to define the curve indexes so they're
within the bounds of the ecdh_c array.

Reviewed-by: default avatarPaul Dale <paul.dale@oracle.com>
Reviewed-by: default avatarMatt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/8422)

(cherry picked from commit f5c99167)
parent 958beb89
Loading
Loading
Loading
Loading
+39 −30
Original line number Original line Diff line number Diff line
@@ -489,30 +489,35 @@ static const OPT_PAIR rsa_choices[] = {
static double rsa_results[RSA_NUM][2];  /* 2 ops: sign then verify */
static double rsa_results[RSA_NUM][2];  /* 2 ops: sign then verify */
#endif /* OPENSSL_NO_RSA */
#endif /* OPENSSL_NO_RSA */


#define R_EC_P160    0
enum {
#define R_EC_P192    1
    R_EC_P160,
#define R_EC_P224    2
    R_EC_P192,
#define R_EC_P256    3
    R_EC_P224,
#define R_EC_P384    4
    R_EC_P256,
#define R_EC_P521    5
    R_EC_P384,
#define R_EC_K163    6
    R_EC_P521,
#define R_EC_K233    7
#ifndef OPENSSL_NO_EC2M
#define R_EC_K283    8
    R_EC_K163,
#define R_EC_K409    9
    R_EC_K233,
#define R_EC_K571    10
    R_EC_K283,
#define R_EC_B163    11
    R_EC_K409,
#define R_EC_B233    12
    R_EC_K571,
#define R_EC_B283    13
    R_EC_B163,
#define R_EC_B409    14
    R_EC_B233,
#define R_EC_B571    15
    R_EC_B283,
#define R_EC_BRP256R1  16
    R_EC_B409,
#define R_EC_BRP256T1  17
    R_EC_B571,
#define R_EC_BRP384R1  18
#endif
#define R_EC_BRP384T1  19
    R_EC_BRP256R1,
#define R_EC_BRP512R1  20
    R_EC_BRP256T1,
#define R_EC_BRP512T1  21
    R_EC_BRP384R1,
#define R_EC_X25519  22
    R_EC_BRP384T1,
#define R_EC_X448    23
    R_EC_BRP512R1,
    R_EC_BRP512T1,
    R_EC_X25519,
    R_EC_X448
};

#ifndef OPENSSL_NO_EC
#ifndef OPENSSL_NO_EC
static OPT_PAIR ecdsa_choices[] = {
static OPT_PAIR ecdsa_choices[] = {
    {"ecdsap160", R_EC_P160},
    {"ecdsap160", R_EC_P160},
@@ -2037,6 +2042,7 @@ int speed_main(int argc, char **argv)
            }
            }
        }
        }
    }
    }
#   ifndef OPENSSL_NO_EC2M
    ecdsa_c[R_EC_K163][0] = count / 1000;
    ecdsa_c[R_EC_K163][0] = count / 1000;
    ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
    ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
    for (i = R_EC_K233; i <= R_EC_K571; i++) {
    for (i = R_EC_K233; i <= R_EC_K571; i++) {
@@ -2065,6 +2071,7 @@ int speed_main(int argc, char **argv)
            }
            }
        }
        }
    }
    }
#   endif


    ecdh_c[R_EC_P160][0] = count / 1000;
    ecdh_c[R_EC_P160][0] = count / 1000;
    for (i = R_EC_P192; i <= R_EC_P521; i++) {
    for (i = R_EC_P192; i <= R_EC_P521; i++) {
@@ -2077,6 +2084,7 @@ int speed_main(int argc, char **argv)
            }
            }
        }
        }
    }
    }
#   ifndef OPENSSL_NO_EC2M
    ecdh_c[R_EC_K163][0] = count / 1000;
    ecdh_c[R_EC_K163][0] = count / 1000;
    for (i = R_EC_K233; i <= R_EC_K571; i++) {
    for (i = R_EC_K233; i <= R_EC_K571; i++) {
        ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
        ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
@@ -2099,6 +2107,7 @@ int speed_main(int argc, char **argv)
            }
            }
        }
        }
    }
    }
#   endif
    /* repeated code good to factorize */
    /* repeated code good to factorize */
    ecdh_c[R_EC_BRP256R1][0] = count / 1000;
    ecdh_c[R_EC_BRP256R1][0] = count / 1000;
    for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
    for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {