Skip to content

Commit f694e35

Browse files
committed
Verify that the imported keys match
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent eb57534 commit f694e35

File tree

5 files changed

+186
-85
lines changed

5 files changed

+186
-85
lines changed

tests/common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66
#include <tomcrypt.h>
77

88
extern prng_state yarrow_prng;
9+
#if defined(LTC_MDSA)
10+
extern const unsigned char ltc_dsa_private_test_key[];
11+
extern const unsigned long ltc_dsa_private_test_key_sz;
12+
int dsa_key_cmp(const int should_type, const dsa_key *should, const dsa_key *is);
13+
#endif
14+
#if defined(LTC_MRSA)
15+
extern const unsigned char ltc_rsa_private_test_key[];
16+
extern const unsigned long ltc_rsa_private_test_key_sz;
17+
int rsa_key_cmp(const int should_type, const rsa_key *should, const rsa_key *is);
18+
#endif
19+
#if defined(LTC_MECC)
20+
extern const unsigned char ltc_ecc_long_pri_test_key[];
21+
extern const unsigned long ltc_ecc_long_pri_test_key_sz;
22+
int ecc_key_cmp(const int should_type, const ecc_key *should, const ecc_key *is);
23+
#endif
924

1025
#ifdef LTC_VERBOSE
1126
#define DO(x) do { fprintf(stderr, "%s:\n", #x); run_cmd((x), __LINE__, __FILE__, #x, NULL); } while (0)

tests/dsa_test.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#if defined(LTC_MDSA)
66

77
/* This is the private key from test_dsa.key */
8-
static const unsigned char openssl_priv_dsa[] = {
8+
const unsigned char ltc_dsa_private_test_key[] = {
99
0x30, 0x82, 0x01, 0xbb, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xc5,
1010
0x0a, 0x37, 0x51, 0x5c, 0xab, 0xd6, 0x18, 0xd5, 0xa2, 0x70, 0xbd, 0x4a,
1111
0x6f, 0x6b, 0x4a, 0xf9, 0xe1, 0x39, 0x95, 0x0f, 0x2b, 0x99, 0x38, 0x7d,
@@ -45,6 +45,7 @@ static const unsigned char openssl_priv_dsa[] = {
4545
0xfb, 0x28, 0xbe, 0x91, 0xf5, 0x06, 0x5f, 0xe8, 0xc9, 0x35, 0xb3, 0xf5,
4646
0xd8, 0x1f, 0xc5
4747
};
48+
const unsigned long ltc_dsa_private_test_key_sz = sizeof(ltc_dsa_private_test_key);
4849

4950
/* private key - raw hexadecimal numbers */
5051
static const char *hex_g = "3B92E4FF5929150B08995A7BF2AD1440556FA047FF9099B344B3D4FC451505AE6722439CBA3710A5894737ECCCF5AEADA8B47A35CB9D935CEDE6B07E9694C4A60C7DD6708A094F814A0EC213FBEB16BFEAA4F456FF723005DE8A443FBEC6852655D62D1D1EDB15DAA445833C1797980B8D87F3490D90BDA9AB676E87687223DC";
@@ -123,6 +124,20 @@ static unsigned char dsaparam_der[] = {
123124
};
124125

125126

127+
int dsa_key_cmp(const int should_type, const dsa_key *should, const dsa_key *is)
128+
{
129+
if (should_type != is->type) return CRYPT_ERROR;
130+
if (should_type == PK_PRIVATE) {
131+
if (mp_cmp(should->x, is->x) != LTC_MP_EQ) return CRYPT_ERROR;
132+
}
133+
if (mp_cmp(should->y, is->y) != LTC_MP_EQ) return CRYPT_ERROR;
134+
if (mp_cmp(should->g, is->g) != LTC_MP_EQ) return CRYPT_ERROR;
135+
if (mp_cmp(should->p, is->p) != LTC_MP_EQ) return CRYPT_ERROR;
136+
if (mp_cmp(should->q, is->q) != LTC_MP_EQ) return CRYPT_ERROR;
137+
if (should->qord != is->qord) return CRYPT_ERROR;
138+
return CRYPT_OK;
139+
}
140+
126141
static int s_dsa_compat_test(void)
127142
{
128143
dsa_key key;
@@ -132,11 +147,11 @@ static int s_dsa_compat_test(void)
132147
unsigned long key_lens[5];
133148
int stat;
134149

135-
DO(dsa_import(openssl_priv_dsa, sizeof(openssl_priv_dsa), &key));
150+
DO(dsa_import(ltc_dsa_private_test_key, sizeof(ltc_dsa_private_test_key), &key));
136151

137152
x = sizeof(tmp);
138153
DO(dsa_export(tmp, &x, PK_PRIVATE | PK_STD, &key));
139-
DO(do_compare_testvector(tmp, x, openssl_priv_dsa, sizeof(openssl_priv_dsa),
154+
DO(do_compare_testvector(tmp, x, ltc_dsa_private_test_key, sizeof(ltc_dsa_private_test_key),
140155
"DSA private export failed from dsa_import(priv_key)\n", __LINE__));
141156

142157
x = sizeof(tmp);
@@ -172,7 +187,7 @@ static int s_dsa_compat_test(void)
172187
&key));
173188
len = sizeof(buf);
174189
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
175-
DO(do_compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
190+
DO(do_compare_testvector(buf, len, ltc_dsa_private_test_key, sizeof(ltc_dsa_private_test_key),
176191
"DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__));
177192
dsa_free(&key);
178193

@@ -219,7 +234,7 @@ static int s_dsa_compat_test(void)
219234
&key));
220235
len = sizeof(buf);
221236
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
222-
DO(do_compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
237+
DO(do_compare_testvector(buf, len, ltc_dsa_private_test_key, sizeof(ltc_dsa_private_test_key),
223238
"DSA private export failed from dsa_set_pqg_dsaparam()\n", __LINE__));
224239
dsa_free(&key);
225240

0 commit comments

Comments
 (0)