Dedup get_dsa*() code.
authortb <tb@openbsd.org>
Mon, 10 Jan 2022 15:04:06 +0000 (15:04 +0000)
committertb <tb@openbsd.org>
Mon, 10 Jan 2022 15:04:06 +0000 (15:04 +0000)
Pointed out by jsing

usr.bin/openssl/testdsa.h

index 2845248..20cc97e 100644 (file)
@@ -1,14 +1,21 @@
-/* $OpenBSD: testdsa.h,v 1.2 2022/01/10 14:47:09 tb Exp $ */
+/* $OpenBSD: testdsa.h,v 1.3 2022/01/10 15:04:06 tb Exp $ */
 
 DSA *get_dsa512(void);
 DSA *get_dsa1024(void);
 DSA *get_dsa2048(void);
 
-static unsigned char dsa512_priv[] = {
+DSA *get_dsa(const unsigned char *priv, size_t priv_size,
+    const unsigned char *pub, size_t pub_size,
+    const unsigned char *p, size_t p_size,
+    const unsigned char *q, size_t q_size,
+    const unsigned char *g, size_t g_size);
+
+static const unsigned char dsa512_priv[] = {
        0x65, 0xe5, 0xc7, 0x38, 0x60, 0x24, 0xb5, 0x89, 0xd4, 0x9c, 0xeb, 0x4c,
        0x9c, 0x1d, 0x7a, 0x22, 0xbd, 0xd1, 0xc2, 0xd2,
 };
-static unsigned char dsa512_pub[] = {
+
+static const unsigned char dsa512_pub[] = {
        0x00, 0x95, 0xa7, 0x0d, 0xec, 0x93, 0x68, 0xba, 0x5f, 0xf7, 0x5f, 0x07,
        0xf2, 0x3b, 0xad, 0x6b, 0x01, 0xdc, 0xbe, 0xec, 0xde, 0x04, 0x7a, 0x3a,
        0x27, 0xb3, 0xec, 0x49, 0xfd, 0x08, 0x43, 0x3d, 0x7e, 0xa8, 0x2c, 0x5e,
@@ -16,7 +23,8 @@ static unsigned char dsa512_pub[] = {
        0x8c, 0x38, 0x5d, 0x83, 0x56, 0x7d, 0xee, 0x53, 0x05, 0x3e, 0x24, 0x84,
        0xbe, 0xba, 0x0a, 0x6b, 0xc8,
 };
-static unsigned char dsa512_p[] = {
+
+static const unsigned char dsa512_p[] = {
        0x9D, 0x1B, 0x69, 0x8E, 0x26, 0xDB, 0xF2, 0x2B, 0x11, 0x70, 0x19, 0x86,
        0xF6, 0x19, 0xC8, 0xF8, 0x19, 0xF2, 0x18, 0x53, 0x94, 0x46, 0x06, 0xD0,
        0x62, 0x50, 0x33, 0x4B, 0x02, 0x3C, 0x52, 0x30, 0x03, 0x8B, 0x3B, 0xF9,
@@ -24,11 +32,13 @@ static unsigned char dsa512_p[] = {
        0x96, 0xE4, 0x37, 0x33, 0xBB, 0x2D, 0x5A, 0xD7, 0x5A, 0x11, 0x40, 0x66,
        0xA2, 0x76, 0x7D, 0x31,
 };
-static unsigned char dsa512_q[] = {
+
+static const unsigned char dsa512_q[] = {
        0xFB, 0x53, 0xEF, 0x50, 0xB4, 0x40, 0x92, 0x31, 0x56, 0x86, 0x53, 0x7A,
        0xE8, 0x8B, 0x22, 0x9A, 0x49, 0xFB, 0x71, 0x8F,
 };
-static unsigned char dsa512_g[] = {
+
+static const unsigned char dsa512_g[] = {
        0x83, 0x3E, 0x88, 0xE5, 0xC5, 0x89, 0x73, 0xCE, 0x3B, 0x6C, 0x01, 0x49,
        0xBF, 0xB3, 0xC7, 0x9F, 0x0A, 0xEA, 0x44, 0x91, 0xE5, 0x30, 0xAA, 0xD9,
        0xBE, 0x5B, 0x5F, 0xB7, 0x10, 0xD7, 0x89, 0xB7, 0x8E, 0x74, 0xFB, 0xCF,
@@ -38,48 +48,19 @@ static unsigned char dsa512_g[] = {
 };
 
 DSA *
-get_dsa512()
+get_dsa512(void)
 {
-       DSA *dsa;
-       BIGNUM *priv_key = NULL, *pub_key = NULL;
-       BIGNUM *p = NULL, *q = NULL, *g = NULL;
-
-       if ((dsa = DSA_new()) == NULL)
-               goto err;
-
-       priv_key = BN_bin2bn(dsa512_priv, sizeof(dsa512_priv), NULL);
-       pub_key = BN_bin2bn(dsa512_pub, sizeof(dsa512_pub), NULL);
-       if (priv_key == NULL || pub_key == NULL)
-               goto err;
-       if (!DSA_set0_key(dsa, pub_key, priv_key))
-               goto err;
-
-       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 (p == NULL || q == NULL || g == NULL)
-               goto err;
-       if (!DSA_set0_pqg(dsa, p, q, g))
-               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;
+       return get_dsa(dsa512_priv, sizeof(dsa512_priv),
+           dsa512_pub, sizeof(dsa512_pub), dsa512_p, sizeof(dsa512_p),
+           dsa512_q, sizeof(dsa512_q), dsa512_g, sizeof(dsa512_g));
 }
 
-static unsigned char dsa1024_priv[] = {
+static const unsigned char dsa1024_priv[] = {
        0x7d, 0x21, 0xda, 0xbb, 0x62, 0x15, 0x47, 0x36, 0x07, 0x67, 0x12, 0xe8,
        0x8c, 0xaa, 0x1c, 0xcd, 0x38, 0x12, 0x61, 0x18,
 };
-static unsigned char dsa1024_pub[] = {
+
+static const unsigned char dsa1024_pub[] = {
        0x3c, 0x4e, 0x9c, 0x2a, 0x7f, 0x16, 0xc1, 0x25, 0xeb, 0xac, 0x78, 0x63,
        0x90, 0x14, 0x8c, 0x8b, 0xf4, 0x68, 0x43, 0x3c, 0x2d, 0xee, 0x65, 0x50,
        0x7d, 0x9c, 0x8f, 0x8c, 0x8a, 0x51, 0xd6, 0x11, 0x2b, 0x99, 0xaf, 0x1e,
@@ -92,7 +73,8 @@ static unsigned char dsa1024_pub[] = {
        0x2c, 0x0b, 0xc3, 0x13, 0x50, 0x61, 0xe5, 0xad, 0xbd, 0x36, 0xb8, 0x97,
        0x4e, 0x40, 0x7d, 0xe8, 0x83, 0x0d, 0xbc, 0x4b
 };
-static unsigned char dsa1024_p[] = {
+
+static const unsigned char dsa1024_p[] = {
        0xA7, 0x3F, 0x6E, 0x85, 0xBF, 0x41, 0x6A, 0x29, 0x7D, 0xF0, 0x9F, 0x47,
        0x19, 0x30, 0x90, 0x9A, 0x09, 0x1D, 0xDA, 0x6A, 0x33, 0x1E, 0xC5, 0x3D,
        0x86, 0x96, 0xB3, 0x15, 0xE0, 0x53, 0x2E, 0x8F, 0xE0, 0x59, 0x82, 0x73,
@@ -105,11 +87,13 @@ static unsigned char dsa1024_p[] = {
        0x39, 0x4F, 0xFD, 0xB7, 0x43, 0x1F, 0xB5, 0xA4, 0x65, 0x6F, 0xCD, 0x80,
        0x11, 0xE4, 0x70, 0x95, 0x5B, 0x50, 0xCD, 0x49,
 };
-static unsigned char dsa1024_q[] = {
+
+static const unsigned char dsa1024_q[] = {
        0xF7, 0x07, 0x31, 0xED, 0xFA, 0x6C, 0x06, 0x03, 0xD5, 0x85, 0x8A, 0x1C,
        0xAC, 0x9C, 0x65, 0xE7, 0x50, 0x66, 0x65, 0x6F,
 };
-static unsigned char dsa1024_g[] = {
+
+static const unsigned char dsa1024_g[] = {
        0x4D, 0xDF, 0x4C, 0x03, 0xA6, 0x91, 0x8A, 0xF5, 0x19, 0x6F, 0x50, 0x46,
        0x25, 0x99, 0xE5, 0x68, 0x6F, 0x30, 0xE3, 0x69, 0xE1, 0xE5, 0xB3, 0x5D,
        0x98, 0xBB, 0x28, 0x86, 0x48, 0xFC, 0xDE, 0x99, 0x04, 0x3F, 0x5F, 0x88,
@@ -124,49 +108,19 @@ static unsigned char dsa1024_g[] = {
 };
 
 DSA *
-get_dsa1024()
+get_dsa1024(void)
 {
-       DSA *dsa;
-       BIGNUM *priv_key = NULL, *pub_key = NULL;
-       BIGNUM *p = NULL, *q = NULL, *g = NULL;
-
-       if ((dsa = DSA_new()) == NULL)
-               goto err;
-
-       priv_key = BN_bin2bn(dsa1024_priv, sizeof(dsa1024_priv), NULL);
-       pub_key = BN_bin2bn(dsa1024_pub, sizeof(dsa1024_pub), NULL);
-       if (priv_key == NULL || pub_key == NULL)
-               goto err;
-       if (!DSA_set0_key(dsa, pub_key, priv_key))
-               goto err;
-
-       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 (p == NULL || q == NULL || g == NULL)
-               goto err;
-
-       if (!DSA_set0_pqg(dsa, p, q, g))
-               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;
+       return get_dsa(dsa1024_priv, sizeof(dsa1024_priv),
+           dsa1024_pub, sizeof(dsa1024_pub), dsa1024_p, sizeof(dsa1024_p),
+           dsa1024_q, sizeof(dsa1024_q), dsa1024_g, sizeof(dsa1024_g));
 }
 
-static unsigned char dsa2048_priv[] = {
+static const unsigned char dsa2048_priv[] = {
        0x32, 0x67, 0x92, 0xf6, 0xc4, 0xe2, 0xe2, 0xe8, 0xa0, 0x8b, 0x6b, 0x45,
        0x0c, 0x8a, 0x76, 0xb0, 0xee, 0xcf, 0x91, 0xa7,
 };
-static unsigned char dsa2048_pub[] = {
+
+static const unsigned char dsa2048_pub[] = {
        0x17, 0x8f, 0xa8, 0x11, 0x84, 0x92, 0xec, 0x83, 0x47, 0xc7, 0x6a, 0xb0,
        0x92, 0xaf, 0x5a, 0x20, 0x37, 0xa3, 0x64, 0x79, 0xd2, 0xd0, 0x3d, 0xcd,
        0xe0, 0x61, 0x88, 0x88, 0x21, 0xcc, 0x74, 0x5d, 0xce, 0x4c, 0x51, 0x47,
@@ -190,7 +144,8 @@ static unsigned char dsa2048_pub[] = {
        0x72, 0xf4, 0x10, 0xe6, 0x8d, 0x52, 0x16, 0x7f, 0xf2, 0xc9, 0xf8, 0x33,
        0x8b, 0x33, 0xb7, 0xce,
 };
-static unsigned char dsa2048_p[] = {
+
+static const unsigned char dsa2048_p[] = {
        0xA0, 0x25, 0xFA, 0xAD, 0xF4, 0x8E, 0xB9, 0xE5, 0x99, 0xF3, 0x5D, 0x6F,
        0x4F, 0x83, 0x34, 0xE2, 0x7E, 0xCF, 0x6F, 0xBF, 0x30, 0xAF, 0x6F, 0x81,
        0xEB, 0xF8, 0xC4, 0x13, 0xD9, 0xA0, 0x5D, 0x8B, 0x5C, 0x8E, 0xDC, 0xC2,
@@ -214,11 +169,13 @@ static unsigned char dsa2048_p[] = {
        0x5D, 0xA7, 0xD8, 0x54, 0xC3, 0x65, 0x7D, 0xC3, 0xB0, 0x1D, 0xBF, 0xAE,
        0xF8, 0x68, 0xCF, 0x9B,
 };
-static unsigned char dsa2048_q[] = {
+
+static const unsigned char dsa2048_q[] = {
        0x97, 0xE7, 0x33, 0x4D, 0xD3, 0x94, 0x3E, 0x0B, 0xDB, 0x62, 0x74, 0xC6,
        0xA1, 0x08, 0xDD, 0x19, 0xA3, 0x75, 0x17, 0x1B,
 };
-static unsigned char dsa2048_g[] = {
+
+static const unsigned char dsa2048_g[] = {
        0x2C, 0x78, 0x16, 0x59, 0x34, 0x63, 0xF4, 0xF3, 0x92, 0xFC, 0xB5, 0xA5,
        0x4F, 0x13, 0xDE, 0x2F, 0x1C, 0xA4, 0x3C, 0xAE, 0xAD, 0x38, 0x3F, 0x7E,
        0x90, 0xBF, 0x96, 0xA6, 0xAE, 0x25, 0x90, 0x72, 0xF5, 0x8E, 0x80, 0x0C,
@@ -244,7 +201,19 @@ static unsigned char dsa2048_g[] = {
 };
 
 DSA *
-get_dsa2048()
+get_dsa2048(void)
+{
+       return get_dsa(dsa2048_priv, sizeof(dsa2048_priv),
+           dsa2048_pub, sizeof(dsa2048_pub), dsa2048_p, sizeof(dsa2048_p),
+           dsa2048_q, sizeof(dsa2048_q), dsa2048_g, sizeof(dsa2048_g));
+}
+
+DSA *
+get_dsa(const unsigned char *priv, size_t priv_size,
+    const unsigned char *pub, size_t pub_size,
+    const unsigned char *p, size_t p_size,
+    const unsigned char *q, size_t q_size,
+    const unsigned char *g, size_t g_size)
 {
        DSA *dsa;
        BIGNUM *priv_key = NULL, *pub_key = NULL;
@@ -252,16 +221,16 @@ get_dsa2048()
 
        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);
+       priv_key = BN_bin2bn(priv, priv_size, NULL);
+       pub_key = BN_bin2bn(pub, pub_size, NULL);
        if (priv_key == NULL || pub_key == NULL)
                goto err;
        if (!DSA_set0_key(dsa, pub_key, priv_key))
                goto err;
 
-       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);
+       p = BN_bin2bn(p, p_size, NULL);
+       q = BN_bin2bn(q, q_size, NULL);
+       g = BN_bin2bn(g, g_size, NULL);
        if (p == NULL || q == NULL || g == NULL)
                goto err;
        if (!DSA_set0_pqg(dsa, p, q, g))