Commit 0848e01b authored by Paul Yang's avatar Paul Yang Committed by Rich Salz
Browse files

Refactor functions in testdsa.h



To reduce duplicate code

Signed-off-by: default avatarPaul Yang <paulyang.inf@gmail.com>

Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
Reviewed-by: default avatarRich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/3656)
parent 8ea404fb
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -1595,9 +1595,9 @@ int speed_main(int argc, char **argv)
#endif
#ifndef OPENSSL_NO_DSA
    for (i = 0; i < loopargs_len; i++) {
        loopargs[i].dsa_key[0] = get_dsa512();
        loopargs[i].dsa_key[1] = get_dsa1024();
        loopargs[i].dsa_key[2] = get_dsa2048();
        loopargs[i].dsa_key[0] = get_dsa(512);
        loopargs[i].dsa_key[1] = get_dsa(1024);
        loopargs[i].dsa_key[2] = get_dsa(2048);
    }
#endif
#ifndef OPENSSL_NO_DES
+51 −81
Original line number Diff line number Diff line
@@ -8,9 +8,7 @@
 */

/* used by speed.c */
DSA *get_dsa512(void);
DSA *get_dsa1024(void);
DSA *get_dsa2048(void);
DSA *get_dsa(int);

static unsigned char dsa512_priv[] = {
    0x65, 0xe5, 0xc7, 0x38, 0x60, 0x24, 0xb5, 0x89, 0xd4, 0x9c, 0xeb, 0x4c,
@@ -49,40 +47,6 @@ static unsigned char dsa512_g[] = {
    0xA2, 0x03, 0x9D, 0x20,
};

DSA *get_dsa512()
{
    DSA *dsa;
    BIGNUM *priv_key, *pub_key, *p, *q, *g;

    if ((dsa = DSA_new()) == NULL)
        return (NULL);
    priv_key = BN_bin2bn(dsa512_priv, sizeof(dsa512_priv), NULL);
    pub_key = BN_bin2bn(dsa512_pub, sizeof(dsa512_pub), NULL);
    p = BN_bin2bn(dsa512_p, sizeof(dsa512_p), NULL);
    q = BN_bin2bn(dsa512_q, sizeof(dsa512_q), NULL);
    g = BN_bin2bn(dsa512_g, sizeof(dsa512_g), NULL);
    if ((priv_key == NULL) || (pub_key == NULL) || (p == NULL) || (q == NULL)
            || (g == NULL)) {
        goto err;
    }
    if (!DSA_set0_pqg(dsa, p, q, g))
        goto err;
    p = q = g = NULL;

    if (!DSA_set0_key(dsa, pub_key, priv_key))
        goto err;

    return dsa;
 err:
    DSA_free(dsa);
    BN_free(priv_key);
    BN_free(pub_key);
    BN_free(p);
    BN_free(q);
    BN_free(g);
    return NULL;
}

static unsigned char dsa1024_priv[] = {
    0x7d, 0x21, 0xda, 0xbb, 0x62, 0x15, 0x47, 0x36, 0x07, 0x67, 0x12, 0xe8,
    0x8c, 0xaa, 0x1c, 0xcd, 0x38, 0x12, 0x61, 0x18,
@@ -135,40 +99,6 @@ static unsigned char dsa1024_g[] = {
    0x6A, 0x7E, 0xD8, 0x32, 0xED, 0x0E, 0x02, 0xB8,
};

DSA *get_dsa1024()
{
    DSA *dsa;
    BIGNUM *priv_key, *pub_key, *p, *q, *g;

    if ((dsa = DSA_new()) == NULL)
        return (NULL);
    priv_key = BN_bin2bn(dsa1024_priv, sizeof(dsa1024_priv), NULL);
    pub_key = BN_bin2bn(dsa1024_pub, sizeof(dsa1024_pub), NULL);
    p = BN_bin2bn(dsa1024_p, sizeof(dsa1024_p), NULL);
    q = BN_bin2bn(dsa1024_q, sizeof(dsa1024_q), NULL);
    g = BN_bin2bn(dsa1024_g, sizeof(dsa1024_g), NULL);
    if ((priv_key == NULL) || (pub_key == NULL) || (p == NULL) || (q == NULL)
            || (g == NULL)) {
        goto err;
    }
    if (!DSA_set0_pqg(dsa, p, q, g))
        goto err;
    p = q = g = NULL;

    if (!DSA_set0_key(dsa, pub_key, priv_key))
        goto err;

    return dsa;
 err:
    DSA_free(dsa);
    BN_free(priv_key);
    BN_free(pub_key);
    BN_free(p);
    BN_free(q);
    BN_free(g);
    return NULL;
}

static unsigned char dsa2048_priv[] = {
    0x32, 0x67, 0x92, 0xf6, 0xc4, 0xe2, 0xe2, 0xe8, 0xa0, 0x8b, 0x6b, 0x45,
    0x0c, 0x8a, 0x76, 0xb0, 0xee, 0xcf, 0x91, 0xa7,
@@ -254,25 +184,66 @@ static unsigned char dsa2048_g[] = {
    0xF8, 0xB2, 0xE5, 0x38,
};

DSA *get_dsa2048()
typedef struct testdsa_st {
    unsigned char *priv;
    unsigned char *pub;
    unsigned char *p;
    unsigned char *g;
    unsigned char *q;
    int priv_l;
    int pub_l;
    int p_l;
    int g_l;
    int q_l;
} testdsa;

#define set_dsa_ptr(st, bits) \
    do { \
        st.priv = dsa##bits##_priv; \
        st.pub = dsa##bits##_pub; \
        st.p = dsa##bits##_p; \
        st.g = dsa##bits##_g; \
        st.q = dsa##bits##_q; \
        st.priv_l = sizeof(dsa##bits##_priv); \
        st.pub_l = sizeof(dsa##bits##_pub); \
        st.p_l = sizeof(dsa##bits##_p); \
        st.g_l = sizeof(dsa##bits##_g); \
        st.q_l = sizeof(dsa##bits##_q); \
    } while (0)

DSA *get_dsa(int dsa_bits)
{
    DSA *dsa;
    BIGNUM *priv_key, *pub_key, *p, *q, *g;
    testdsa dsa_t;

    switch (dsa_bits) {
    case 512:
        set_dsa_ptr(dsa_t, 512);
        break;
    case 1024:
        set_dsa_ptr(dsa_t, 1024);
        break;
    case 2048:
        set_dsa_ptr(dsa_t, 2048);
        break;
    default:
        return NULL;
    }

    if ((dsa = DSA_new()) == NULL)
        return (NULL);
    priv_key = BN_bin2bn(dsa2048_priv, sizeof(dsa2048_priv), NULL);
    pub_key = BN_bin2bn(dsa2048_pub, sizeof(dsa2048_pub), NULL);
    p = BN_bin2bn(dsa2048_p, sizeof(dsa2048_p), NULL);
    q = BN_bin2bn(dsa2048_q, sizeof(dsa2048_q), NULL);
    g = BN_bin2bn(dsa2048_g, sizeof(dsa2048_g), NULL);
        return NULL;
    priv_key = BN_bin2bn(dsa_t.priv, dsa_t.priv_l, NULL);
    pub_key = BN_bin2bn(dsa_t.pub, dsa_t.pub_l, NULL);
    p = BN_bin2bn(dsa_t.p, dsa_t.p_l, NULL);
    q = BN_bin2bn(dsa_t.q, dsa_t.q_l, NULL);
    g = BN_bin2bn(dsa_t.g, dsa_t.g_l, NULL);
    if ((priv_key == NULL) || (pub_key == NULL) || (p == NULL) || (q == NULL)
         || (g == NULL)) {
        goto err;
    }
    if (!DSA_set0_pqg(dsa, p, q, g))
        goto err;
    p = q = g = NULL;

    if (!DSA_set0_key(dsa, pub_key, priv_key))
        goto err;
@@ -287,4 +258,3 @@ DSA *get_dsa2048()
    BN_free(g);
    return NULL;
}