aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/test-ecdsa.c110
-rw-r--r--tests/test-ecdsa.h116
-rw-r--r--tests/test-ecdsa.py123
3 files changed, 295 insertions, 54 deletions
diff --git a/tests/test-ecdsa.c b/tests/test-ecdsa.c
index 0d3334e..fbdb300 100644
--- a/tests/test-ecdsa.c
+++ b/tests/test-ecdsa.c
@@ -110,53 +110,83 @@ static void set_next_random(const uint8_t * const data, const size_t length)
* Run one keygen test from test vectors.
*/
-static int test_keygen_static(const hal_ecdsa_curve_t curve)
+static int test_against_static_vectors(const ecdsa_tc_t * const tc)
{
- uint8_t keybuf[hal_ecdsa_key_t_size];
- hal_ecdsa_key_t *key = NULL;
hal_error_t err;
- const uint8_t *d, *Qx, *Qy;
- size_t d_len, Qx_len, Qy_len;
- switch (curve) {
-
- case HAL_ECDSA_CURVE_P256:
- printf("ECDSA P-256 key generation test\n");
- d = p256_d; d_len = sizeof(p256_d);
- Qx = p256_Qx; Qx_len = sizeof(p256_Qx);
- Qy = p256_Qy; Qy_len = sizeof(p256_Qy);
- break;
+ set_next_random(tc->d, tc->d_len);
- case HAL_ECDSA_CURVE_P384:
- printf("ECDSA P-384 key generation test\n");
- d = p384_d; d_len = sizeof(p384_d);
- Qx = p384_Qx; Qx_len = sizeof(p384_Qx);
- Qy = p384_Qy; Qy_len = sizeof(p384_Qy);
- break;
+ uint8_t keybuf1[hal_ecdsa_key_t_size];
+ hal_ecdsa_key_t *key1 = NULL;
- default:
- printf("Unsupported ECDSA curve type\n");
- return 0;
- }
-
- set_next_random(d, d_len);
-
- if ((err = hal_ecdsa_key_gen(&key, keybuf, sizeof(keybuf), curve)) != HAL_OK)
+ if ((err = hal_ecdsa_key_gen(&key1, keybuf1, sizeof(keybuf1), tc->curve)) != HAL_OK)
return printf("hal_ecdsa_key_gen() failed: %s\n", hal_error_string(err)), 0;
- uint8_t Rx[Qx_len], Ry[Qy_len];
- size_t Rx_len, Ry_len;
+ uint8_t Qx[tc->Qx_len], Qy[tc->Qy_len];
+ size_t Qx_len, Qy_len;
- if ((err = hal_ecdsa_key_get_public(key, Rx, &Rx_len, sizeof(Rx), Ry, &Ry_len, sizeof(Ry))) != HAL_OK)
+ if ((err = hal_ecdsa_key_get_public(key1, Qx, &Qx_len, sizeof(Qx), Qy, &Qy_len, sizeof(Qy))) != HAL_OK)
return printf("hal_ecdsa_key_get_public() failed: %s\n", hal_error_string(err)), 0;
- if (Qx_len != Rx_len || memcmp(Qx, Rx, Rx_len) != 0)
+ if (tc->Qx_len != Qx_len || memcmp(tc->Qx, Qx, Qx_len) != 0)
return printf("Qx mismatch\n"), 0;
- if (Qy_len != Ry_len || memcmp(Qy, Ry, Ry_len) != 0)
+ if (tc->Qy_len != Qy_len || memcmp(tc->Qy, Qy, Qy_len) != 0)
return printf("Qy mismatch\n"), 0;
+ if (hal_ecdsa_key_to_der_len(key1) != tc->key_len)
+ return printf("DER Key length mismatch\n"), 0;
+
+ uint8_t keyder[tc->key_len];
+ size_t keyder_len;
+
+ if ((err = hal_ecdsa_key_to_der(key1, keyder, &keyder_len, sizeof(keyder))) != HAL_OK)
+ return printf("hal_ecdsa_key_to_der() failed: %s\n", hal_error_string(err)), 0;
+
+ uint8_t keybuf2[hal_ecdsa_key_t_size];
+ hal_ecdsa_key_t *key2 = NULL;
+
+ if ((err = hal_ecdsa_key_from_der(&key2, keybuf2, sizeof(keybuf2), keyder, keyder_len)) != HAL_OK)
+ return printf("hal_ecdsa_key_from_der() failed: %s\n", hal_error_string(err)), 0;
+
+ if (memcmp(key1, key2, hal_ecdsa_key_t_size) != 0)
+ return printf("Key mismatch after read/write cycle\n"), 0;
+
+ set_next_random(tc->k, tc->k_len);
+
+ uint8_t sig[tc->sig_len];
+ size_t sig_len;
+
+ if ((err = hal_ecdsa_sign(key1, tc->H, tc->H_len, sig, &sig_len, sizeof(sig))) != HAL_OK)
+ return printf("hal_ecdsa_sign() failed: %s\n", hal_error_string(err)), 0;
+
+ if (sig_len != tc->sig_len || memcmp(sig, tc->sig, tc->sig_len) != 0)
+ return printf("Signature mismatch\n"), 0;
+
+ if ((err = hal_ecdsa_verify(key2, tc->H, tc->H_len, sig, sig_len)) != HAL_OK)
+ return printf("hal_ecdsa_verify(private) failed: %s\n", hal_error_string(err)), 0;
+
+ hal_ecdsa_key_clear(key2);
+ key2 = NULL;
+
+ if ((err = hal_ecdsa_key_load_private(&key2, keybuf2, sizeof(keybuf2), tc->curve,
+ tc->Qx, tc->Qx_len, tc->Qy, tc->Qy_len, tc->d, tc->d_len)) != HAL_OK)
+ return printf("hal_ecdsa_load_private() failed: %s\n", hal_error_string(err)), 0;
+
+ if (memcmp(key1, key2, hal_ecdsa_key_t_size) != 0)
+ return printf("Key mismatch after hal_ecdsa_load_private_key()\n"), 0;
+
+ hal_ecdsa_key_clear(key2);
+ key2 = NULL;
+
+ if ((err = hal_ecdsa_key_load_public(&key2, keybuf2, sizeof(keybuf2), tc->curve,
+ tc->Qx, tc->Qx_len, tc->Qy, tc->Qy_len)) != HAL_OK)
+ return printf("hal_ecdsa_load_public() failed: %s\n", hal_error_string(err)), 0;
+
+ if ((err = hal_ecdsa_verify(key2, tc->H, tc->H_len, sig, sig_len)) != HAL_OK)
+ return printf("hal_ecdsa_verify(public) failed: %s\n", hal_error_string(err)), 0;
+
return 1;
}
@@ -261,16 +291,12 @@ static void _time_check(const struct timeval t0, const int ok)
* Run tests for one ECDSA curve.
*/
-static int test_ecdsa(const hal_ecdsa_curve_t curve)
+static int test_ecdsa(const ecdsa_tc_t * const tc)
{
int ok = 1;
-
- if (curve == HAL_ECDSA_CURVE_P256 || curve == HAL_ECDSA_CURVE_P384)
- time_check(test_keygen_static(curve));
-
- time_check(test_keygen_sign_verify(curve));
-
+ time_check(test_against_static_vectors(tc));
+ time_check(test_keygen_sign_verify(tc->curve));
return ok;
}
@@ -291,7 +317,11 @@ int main(int argc, char *argv[])
printf("\"%8.8s\" \"%4.4s\"\n\n", name, version);
- return !test_ecdsa(HAL_ECDSA_CURVE_P256) || !test_ecdsa(HAL_ECDSA_CURVE_P384);
+ for (int i = 0; i < sizeof(ecdsa_tc)/sizeof(*ecdsa_tc); i++)
+ if (!test_ecdsa(&ecdsa_tc[i]))
+ return 1;
+
+ return 0;
}
/*
diff --git a/tests/test-ecdsa.h b/tests/test-ecdsa.h
index 61124e4..ca51858 100644
--- a/tests/test-ecdsa.h
+++ b/tests/test-ecdsa.h
@@ -58,6 +58,19 @@ static const uint8_t p256_k[] = { /* 32 bytes */
0x19, 0x65, 0xc7, 0xb1, 0x34, 0xee, 0x45, 0xd0
};
+static const uint8_t p256_key[] = { /* 121 bytes */
+ 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x70, 0xa1, 0x2c, 0x2d, 0xb1,
+ 0x68, 0x45, 0xed, 0x56, 0xff, 0x68, 0xcf, 0xc2, 0x1a, 0x47, 0x2b, 0x3f,
+ 0x04, 0xd7, 0xd6, 0x85, 0x1b, 0xf6, 0x34, 0x9f, 0x2d, 0x7d, 0x5b, 0x34,
+ 0x52, 0xb3, 0x8a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
+ 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0x81, 0x01, 0xec,
+ 0xe4, 0x74, 0x64, 0xa6, 0xea, 0xd7, 0x0c, 0xf6, 0x9a, 0x6e, 0x2b, 0xd3,
+ 0xd8, 0x86, 0x91, 0xa3, 0x26, 0x2d, 0x22, 0xcb, 0xa4, 0xf7, 0x63, 0x5e,
+ 0xaf, 0xf2, 0x66, 0x80, 0xa8, 0xd8, 0xa1, 0x2b, 0xa6, 0x1d, 0x59, 0x92,
+ 0x35, 0xf6, 0x7d, 0x9c, 0xb4, 0xd5, 0x8f, 0x17, 0x83, 0xd3, 0xca, 0x43,
+ 0xe7, 0x8f, 0x0a, 0x5a, 0xba, 0xa6, 0x24, 0x07, 0x99, 0x36, 0xc0, 0xc3, 0xa9
+};
+
static const uint8_t p256_kinv[] = { /* 32 bytes */
0x6a, 0x66, 0x4f, 0xa1, 0x15, 0x35, 0x6d, 0x33, 0xf1, 0x63, 0x31, 0xb5,
0x4c, 0x4e, 0x7c, 0xe9, 0x67, 0x96, 0x53, 0x86, 0xc7, 0xdc, 0xbf, 0x29,
@@ -76,6 +89,15 @@ static const uint8_t p256_s[] = { /* 32 bytes */
0x92, 0xdb, 0xea, 0xa1, 0xaf, 0x2b, 0xc3, 0x67
};
+static const uint8_t p256_sig[] = { /* 70 bytes */
+ 0x30, 0x44, 0x02, 0x20, 0x72, 0x14, 0xbc, 0x96, 0x47, 0x16, 0x0b, 0xbd,
+ 0x39, 0xff, 0x2f, 0x80, 0x53, 0x3f, 0x5d, 0xc6, 0xdd, 0xd7, 0x0d, 0xdf,
+ 0x86, 0xbb, 0x81, 0x56, 0x61, 0xe8, 0x05, 0xd5, 0xd4, 0xe6, 0xf2, 0x7c,
+ 0x02, 0x20, 0x7d, 0x1f, 0xf9, 0x61, 0x98, 0x0f, 0x96, 0x1b, 0xda, 0xa3,
+ 0x23, 0x3b, 0x62, 0x09, 0xf4, 0x01, 0x33, 0x17, 0xd3, 0xe3, 0xf9, 0xe1,
+ 0x49, 0x35, 0x92, 0xdb, 0xea, 0xa1, 0xaf, 0x2b, 0xc3, 0x67
+};
+
static const uint8_t p256_u1[] = { /* 32 bytes */
0xbb, 0x25, 0x24, 0x01, 0xd6, 0xfb, 0x32, 0x2b, 0xb7, 0x47, 0x18, 0x4c,
0xf2, 0xac, 0x52, 0xbf, 0x8d, 0x54, 0xb9, 0x5a, 0x15, 0x15, 0x06, 0x2a,
@@ -163,6 +185,23 @@ static const uint8_t p384_k[] = { /* 48 bytes */
0x5a, 0xa1, 0xfd, 0x45, 0xe2, 0xd2, 0xa7, 0x35, 0xf8, 0x74, 0x93, 0x59
};
+static const uint8_t p384_key[] = { /* 167 bytes */
+ 0x30, 0x81, 0xa4, 0x02, 0x01, 0x01, 0x04, 0x30, 0xc8, 0x38, 0xb8, 0x52,
+ 0x53, 0xef, 0x8d, 0xc7, 0x39, 0x4f, 0xa5, 0x80, 0x8a, 0x51, 0x83, 0x98,
+ 0x1c, 0x7d, 0xee, 0xf5, 0xa6, 0x9b, 0xa8, 0xf4, 0xf2, 0x11, 0x7f, 0xfe,
+ 0xa3, 0x9c, 0xfc, 0xd9, 0x0e, 0x95, 0xf6, 0xcb, 0xc8, 0x54, 0xab, 0xac,
+ 0xab, 0x70, 0x1d, 0x50, 0xc1, 0xf3, 0xcf, 0x24, 0xa0, 0x07, 0x06, 0x05,
+ 0x2b, 0x81, 0x04, 0x00, 0x22, 0xa1, 0x64, 0x03, 0x62, 0x00, 0x04, 0x1f,
+ 0xba, 0xc8, 0xee, 0xbd, 0x0c, 0xbf, 0x35, 0x64, 0x0b, 0x39, 0xef, 0xe0,
+ 0x80, 0x8d, 0xd7, 0x74, 0xde, 0xbf, 0xf2, 0x0a, 0x2a, 0x32, 0x9e, 0x91,
+ 0x71, 0x3b, 0xaf, 0x7d, 0x7f, 0x3c, 0x3e, 0x81, 0x54, 0x6d, 0x88, 0x37,
+ 0x30, 0xbe, 0xe7, 0xe4, 0x86, 0x78, 0xf8, 0x57, 0xb0, 0x2c, 0xa0, 0xeb,
+ 0x21, 0x31, 0x03, 0xbd, 0x68, 0xce, 0x34, 0x33, 0x65, 0xa8, 0xa4, 0xc3,
+ 0xd4, 0x55, 0x5f, 0xa3, 0x85, 0xf5, 0x33, 0x02, 0x03, 0xbd, 0xd7, 0x6f,
+ 0xfa, 0xd1, 0xf3, 0xaf, 0xfb, 0x95, 0x75, 0x1c, 0x13, 0x20, 0x07, 0xe1,
+ 0xb2, 0x40, 0x35, 0x3c, 0xb0, 0xa4, 0xcf, 0x16, 0x93, 0xbd, 0xf9
+};
+
static const uint8_t p384_kinv[] = { /* 48 bytes */
0x74, 0x36, 0xf0, 0x30, 0x88, 0xe6, 0x5c, 0x37, 0xba, 0x8e, 0x7b, 0x33,
0x88, 0x7f, 0xbc, 0x87, 0x75, 0x75, 0x14, 0xd6, 0x11, 0xf7, 0xd1, 0xfb,
@@ -184,6 +223,18 @@ static const uint8_t p384_s[] = { /* 48 bytes */
0x67, 0xad, 0xad, 0xf1, 0x68, 0xeb, 0xbe, 0x80, 0x37, 0x94, 0xa4, 0x02
};
+static const uint8_t p384_sig[] = { /* 103 bytes */
+ 0x30, 0x65, 0x02, 0x31, 0x00, 0xa0, 0xc2, 0x7e, 0xc8, 0x93, 0x09, 0x2d,
+ 0xea, 0x1e, 0x1b, 0xd2, 0xcc, 0xfe, 0xd3, 0xcf, 0x94, 0x5c, 0x81, 0x34,
+ 0xed, 0x0c, 0x9f, 0x81, 0x31, 0x1a, 0x0f, 0x4a, 0x05, 0x94, 0x2d, 0xb8,
+ 0xdb, 0xed, 0x8d, 0xd5, 0x9f, 0x26, 0x74, 0x71, 0xd5, 0x46, 0x2a, 0xa1,
+ 0x4f, 0xe7, 0x2d, 0xe8, 0x56, 0x02, 0x30, 0x20, 0xab, 0x3f, 0x45, 0xb7,
+ 0x4f, 0x10, 0xb6, 0xe1, 0x1f, 0x96, 0xa2, 0xc8, 0xeb, 0x69, 0x4d, 0x20,
+ 0x6b, 0x9d, 0xda, 0x86, 0xd3, 0xc7, 0xe3, 0x31, 0xc2, 0x6b, 0x22, 0xc9,
+ 0x87, 0xb7, 0x53, 0x77, 0x26, 0x57, 0x76, 0x67, 0xad, 0xad, 0xf1, 0x68,
+ 0xeb, 0xbe, 0x80, 0x37, 0x94, 0xa4, 0x02
+};
+
static const uint8_t p384_u1[] = { /* 48 bytes */
0x6c, 0xe2, 0x56, 0x49, 0xd4, 0x2d, 0x22, 0x3e, 0x02, 0x0c, 0x11, 0x14,
0x0f, 0xe7, 0x72, 0x32, 0x66, 0x12, 0xbb, 0x11, 0xb6, 0x86, 0xd3, 0x5e,
@@ -211,3 +262,68 @@ static const uint8_t p384_w[] = { /* 48 bytes */
0x0a, 0x7c, 0x86, 0x1a, 0xf8, 0xfe, 0x82, 0x25, 0x46, 0x7a, 0x25, 0x7f,
0x5b, 0xf9, 0x1a, 0x4a, 0xaa, 0x5a, 0x79, 0xa8, 0x63, 0x7d, 0x21, 0x8a
};
+
+typedef struct {
+ hal_ecdsa_curve_t curve;
+ const uint8_t * H; size_t H_len;
+ const uint8_t * M; size_t M_len;
+ const uint8_t * Qx; size_t Qx_len;
+ const uint8_t * Qy; size_t Qy_len;
+ const uint8_t * Rx; size_t Rx_len;
+ const uint8_t * Ry; size_t Ry_len;
+ const uint8_t * d; size_t d_len;
+ const uint8_t * e; size_t e_len;
+ const uint8_t * k; size_t k_len;
+ const uint8_t * key; size_t key_len;
+ const uint8_t * kinv; size_t kinv_len;
+ const uint8_t * r; size_t r_len;
+ const uint8_t * s; size_t s_len;
+ const uint8_t * sig; size_t sig_len;
+ const uint8_t * u1; size_t u1_len;
+ const uint8_t * u2; size_t u2_len;
+ const uint8_t * v; size_t v_len;
+ const uint8_t * w; size_t w_len;
+} ecdsa_tc_t;
+
+static const ecdsa_tc_t ecdsa_tc[] = {
+ { HAL_ECDSA_CURVE_P256,
+ p256_H, sizeof(p256_H),
+ p256_M, sizeof(p256_M),
+ p256_Qx, sizeof(p256_Qx),
+ p256_Qy, sizeof(p256_Qy),
+ p256_Rx, sizeof(p256_Rx),
+ p256_Ry, sizeof(p256_Ry),
+ p256_d, sizeof(p256_d),
+ p256_e, sizeof(p256_e),
+ p256_k, sizeof(p256_k),
+ p256_key, sizeof(p256_key),
+ p256_kinv, sizeof(p256_kinv),
+ p256_r, sizeof(p256_r),
+ p256_s, sizeof(p256_s),
+ p256_sig, sizeof(p256_sig),
+ p256_u1, sizeof(p256_u1),
+ p256_u2, sizeof(p256_u2),
+ p256_v, sizeof(p256_v),
+ p256_w, sizeof(p256_w),
+ },
+ { HAL_ECDSA_CURVE_P384,
+ p384_H, sizeof(p384_H),
+ p384_M, sizeof(p384_M),
+ p384_Qx, sizeof(p384_Qx),
+ p384_Qy, sizeof(p384_Qy),
+ p384_Rx, sizeof(p384_Rx),
+ p384_Ry, sizeof(p384_Ry),
+ p384_d, sizeof(p384_d),
+ p384_e, sizeof(p384_e),
+ p384_k, sizeof(p384_k),
+ p384_key, sizeof(p384_key),
+ p384_kinv, sizeof(p384_kinv),
+ p384_r, sizeof(p384_r),
+ p384_s, sizeof(p384_s),
+ p384_sig, sizeof(p384_sig),
+ p384_u1, sizeof(p384_u1),
+ p384_u2, sizeof(p384_u2),
+ p384_v, sizeof(p384_v),
+ p384_w, sizeof(p384_w),
+ },
+};
diff --git a/tests/test-ecdsa.py b/tests/test-ecdsa.py
index 8fb33f1..a52f85b 100644
--- a/tests/test-ecdsa.py
+++ b/tests/test-ecdsa.py
@@ -2,9 +2,6 @@
#
# e is given in decimal, all other values are hex, because that's how
# these were given in the paper
-#
-# This script will probably become a bit more elaborate at some later date, eg,
-# to add ASN.1 encoding.
p256_d = 0x70a12c2db16845ed56ff68cfc21a472b3f04d7d6851bf6349f2d7d5b3452b38a
p256_Qx = 0x8101ece47464a6ead70cf69a6e2bd3d88691a3262d22cba4f7635eaff26680a8
@@ -40,22 +37,120 @@ p384_u1 = 0x6ce25649d42d223e020c11140fe772326612bb11b686d35ee98ed4550e0635d9dd
p384_u2 = 0xf3b240751d5d8ed394a4b5bf8e2a4c0e1e21aa51f2620a08b8c55a2bc334c9689923162648f06e5f4659fc526d9c1fd6
p384_v = 0xa0c27ec893092dea1e1bd2ccfed3cf945c8134ed0c9f81311a0f4a05942db8dbed8dd59f267471d5462aa14fe72de856
-from textwrap import TextWrapper
-from os.path import basename
-from sys import argv
+from textwrap import TextWrapper
+from os.path import basename
+from sys import argv
+from pyasn1.type.univ import Sequence, Choice, Integer, OctetString, ObjectIdentifier, BitString
+from pyasn1.type.namedtype import NamedTypes, NamedType, OptionalNamedType
+from pyasn1.type.namedval import NamedValues
+from pyasn1.type.tag import Tag, tagClassContext, tagFormatSimple
+from pyasn1.type.constraint import SingleValueConstraint
+from pyasn1.codec.der.encoder import encode as DER_Encode
+from pyasn1.codec.der.decoder import decode as DER_Decode
wrapper = TextWrapper(width = 78, initial_indent = " " * 2, subsequent_indent = " " * 2)
+def long_to_bytes(l):
+ #
+ # This is just plain nasty.
+ #
+ s = "%x" % l
+ return ("0" + s if len(s) & 1 else s).decode("hex")
+
+def bytes_to_bits(b):
+ #
+ # This, on the other hand, is not just plain nasty, this is fancy nasty.
+ # This is nasty with rasins in it.
+ #
+ bits = bin(long(b.encode("hex"), 16))[2:]
+ if len(bits) % 8:
+ bits = ("0" * (8 - len(bits) % 8)) + bits
+ return tuple(int(i) for i in bits)
+
+###
+
+class ECDSA_Sig_Value(Sequence):
+ componentType = NamedTypes(
+ NamedType("r", Integer()),
+ NamedType("s", Integer()))
+
+def encode_sig(r, s):
+ sig = ECDSA_Sig_Value()
+ sig["r"] = r
+ sig["s"] = s
+ return DER_Encode(sig)
+
+p256_sig = encode_sig(p256_r, p256_s)
+p384_sig = encode_sig(p384_r, p384_s)
+
+###
+
+class ECPrivateKey(Sequence):
+ componentType = NamedTypes(
+ NamedType("version", Integer(namedValues = NamedValues(("ecPrivkeyVer1", 1))
+ ).subtype(subtypeSpec = Integer.subtypeSpec + SingleValueConstraint(1))),
+ NamedType("privateKey", OctetString()),
+ OptionalNamedType("parameters", ObjectIdentifier().subtype(explicitTag = Tag(tagClassContext, tagFormatSimple, 0))),
+ OptionalNamedType("publicKey", BitString().subtype(explicitTag = Tag(tagClassContext, tagFormatSimple, 1))))
+
+def encode_key(d, Qx, Qy, oid):
+ private_key = long_to_bytes(d)
+ public_key = bytes_to_bits(chr(0x04) + long_to_bytes(Qx) + long_to_bytes(Qy))
+ parameters = oid
+ key = ECPrivateKey()
+ key["version"] = 1
+ key["privateKey"] = private_key
+ key["parameters"] = parameters
+ key["publicKey"] = public_key
+ return DER_Encode(key)
+
+p256_key = encode_key(p256_d, p256_Qx, p256_Qy, "1.2.840.10045.3.1.7")
+p384_key = encode_key(p384_d, p384_Qx, p384_Qy, "1.3.132.0.34")
+
+###
+
print "/*"
print " * ECDSA test data."
print " * File automatically generated by", basename(argv[0])
print " */"
-for name in sorted(dir()):
- if name.startswith("p256_") or name.startswith("p384_"):
- value = "%x" % globals()[name]
- value = ("0" + value if len(value) & 1 else value).decode("hex")
- print
- print "static const uint8_t %s[] = { /* %d bytes */" % (name, len(value))
- print wrapper.fill(", ".join("0x%02x" % ord(v) for v in value))
- print "};"
+curves = ("p256", "p384")
+vars = set()
+
+for name in dir():
+ head, sep, tail = name.partition("_")
+ if head in curves:
+ vars.add(tail)
+
+vars = sorted(vars)
+
+for curve in curves:
+ for var in vars:
+ name = curve + "_" + var
+ value = globals().get(name, None)
+ if isinstance(value, (int, long)):
+ value = long_to_bytes(value)
+ if value is not None:
+ print
+ print "static const uint8_t %s[] = { /* %d bytes */" % (name, len(value))
+ print wrapper.fill(", ".join("0x%02x" % ord(v) for v in value))
+ print "};"
+
+print
+print "typedef struct {"
+print " hal_ecdsa_curve_t curve;"
+for var in vars:
+ print " const uint8_t *%8s; size_t %8s_len;" % (var, var)
+print "} ecdsa_tc_t;"
+print
+print "static const ecdsa_tc_t ecdsa_tc[] = {"
+for curve in curves:
+ print " { HAL_ECDSA_CURVE_%s," % curve.upper()
+ for var in vars:
+ name = curve + "_" + var
+ if name in globals():
+ print " %-14s sizeof(%s)," % (name + ",", name)
+ else:
+ print " %-14s 0," % "NULL,"
+ print " },"
+print "};"