From 0bb31590e68d0dc8de97a58cb8494eddfa9d89a2 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Sun, 24 Nov 2019 00:13:07 +0100 Subject: [PATCH] tests/pkg_micro-ecc: Cleanup and AVR fixes - Moved huge allocations from stack to data / bss - Increased verbosity of messages (one line per round) - Adapted test script to new output format --- tests/pkg_micro-ecc/main.c | 100 ++++++++++++++-------------- tests/pkg_micro-ecc/tests/01-run.py | 13 ++-- 2 files changed, 59 insertions(+), 54 deletions(-) diff --git a/tests/pkg_micro-ecc/main.c b/tests/pkg_micro-ecc/main.c index 0e41a82e68..a07035a98a 100644 --- a/tests/pkg_micro-ecc/main.c +++ b/tests/pkg_micro-ecc/main.c @@ -36,16 +36,60 @@ #include #include +#include "assert.h" #include "hashes/sha256.h" #include "uECC.h" -#define TESTROUNDS 16 +#define TESTROUNDS (4) +#define MAX_PUBLIC_KEY_SIZE (64) +#define MAX_CURVE_SIZE (32) typedef struct uECC_SHA256_HashContext { uECC_HashContext uECC; sha256_context_t ctx; } uECC_SHA256_HashContext; +static uint8_t l_secret1[MAX_CURVE_SIZE]; +static uint8_t l_secret2[MAX_CURVE_SIZE]; +static uint8_t l_sig[MAX_PUBLIC_KEY_SIZE]; +/* use pre-generated keys for no-HWRNG platforms */ +static const uint8_t l_private1[] = { + 0x9b, 0x4c, 0x4b, 0xa0, 0xb7, 0xb1, 0x25, 0x23, + 0x9c, 0x09, 0x85, 0x4f, 0x9a, 0x21, 0xb4, 0x14, + 0x70, 0xe0, 0xce, 0x21, 0x25, 0x00, 0xa5, 0x62, + 0x34, 0xa4, 0x25, 0xf0, 0x0f, 0x00, 0xeb, 0xe7, +}; +static const uint8_t l_public1[] = { + 0x54, 0x3e, 0x98, 0xf8, 0x14, 0x55, 0x08, 0x13, + 0xb5, 0x1a, 0x1d, 0x02, 0x02, 0xd7, 0x0e, 0xab, + 0xa0, 0x98, 0x74, 0x61, 0x91, 0x12, 0x3d, 0x96, + 0x50, 0xfa, 0xd5, 0x94, 0xa2, 0x86, 0xa8, 0xb0, + 0xd0, 0x7b, 0xda, 0x36, 0xba, 0x8e, 0xd3, 0x9a, + 0xa0, 0x16, 0x11, 0x0e, 0x1b, 0x6e, 0x81, 0x13, + 0xd7, 0xf4, 0x23, 0xa1, 0xb2, 0x9b, 0xaf, 0xf6, + 0x6b, 0xc4, 0x2a, 0xdf, 0xbd, 0xe4, 0x61, 0x5c, +}; +static const uint8_t l_private2[] = { + 0xb5, 0x45, 0xaf, 0xa0, 0x2e, 0x5c, 0xa6, 0x17, + 0x3b, 0x5a, 0x55, 0x76, 0x67, 0x5d, 0xd4, 0x5e, + 0x41, 0x7c, 0x4f, 0x19, 0x9f, 0xb9, 0x75, 0xdc, + 0xba, 0x57, 0xc4, 0xa2, 0x26, 0xc6, 0x86, 0x2a, +}; +static const uint8_t l_public2[] = { + 0x2e, 0x81, 0x24, 0x3c, 0x44, 0xac, 0x63, 0x13, + 0x9b, 0xc1, 0x27, 0xe9, 0x53, 0x3b, 0x0a, 0xe2, + 0xf9, 0x22, 0xcd, 0x06, 0xfd, 0x12, 0x17, 0x2e, + 0xe5, 0x0e, 0xb5, 0xce, 0x6b, 0x50, 0xe2, 0x44, + 0xbf, 0x6b, 0x3f, 0xe8, 0x4e, 0x70, 0xd1, 0x06, + 0x85, 0x84, 0xb8, 0xef, 0xe2, 0x25, 0x91, 0x21, + 0xf3, 0x46, 0x70, 0xa9, 0x1c, 0x79, 0x19, 0xe3, + 0xfb, 0x11, 0x36, 0x64, 0x37, 0x64, 0x58, 0xc9, +}; +static uint8_t tmp[2 * SHA256_DIGEST_LENGTH + SHA256_INTERNAL_BLOCK_SIZE]; + +/* reserve space for a SHA-256 hash */ +static uint8_t l_hash[32] = { 0 }; + static void _init_sha256(const uECC_HashContext *base) { uECC_SHA256_HashContext *context = (uECC_SHA256_HashContext*)base; @@ -71,58 +115,16 @@ int main(void) printf("micro-ecc compiled!\n"); const struct uECC_Curve_t *curve = uECC_secp256r1(); - int i, errorc = 0; + int errorc = 0; - int curve_size = uECC_curve_private_key_size(curve); - int public_key_size = uECC_curve_public_key_size(curve); - - uint8_t l_secret1[curve_size]; - uint8_t l_secret2[curve_size]; - - /* reserve space for a SHA-256 hash */ - uint8_t l_hash[32] = { 0 }; - - uint8_t l_sig[public_key_size]; + assert(uECC_curve_private_key_size(curve) <= MAX_CURVE_SIZE); + assert(uECC_curve_public_key_size(curve) <= MAX_PUBLIC_KEY_SIZE); printf("Testing %d random private key pairs and signature without using HWRNG\n", TESTROUNDS); - /* use pre-generated keys for no-HWRNG platforms */ - uint8_t l_private1[] = { - 0x9b, 0x4c, 0x4b, 0xa0, 0xb7, 0xb1, 0x25, 0x23, - 0x9c, 0x09, 0x85, 0x4f, 0x9a, 0x21, 0xb4, 0x14, - 0x70, 0xe0, 0xce, 0x21, 0x25, 0x00, 0xa5, 0x62, - 0x34, 0xa4, 0x25, 0xf0, 0x0f, 0x00, 0xeb, 0xe7, - }; - uint8_t l_public1[] = { - 0x54, 0x3e, 0x98, 0xf8, 0x14, 0x55, 0x08, 0x13, - 0xb5, 0x1a, 0x1d, 0x02, 0x02, 0xd7, 0x0e, 0xab, - 0xa0, 0x98, 0x74, 0x61, 0x91, 0x12, 0x3d, 0x96, - 0x50, 0xfa, 0xd5, 0x94, 0xa2, 0x86, 0xa8, 0xb0, - 0xd0, 0x7b, 0xda, 0x36, 0xba, 0x8e, 0xd3, 0x9a, - 0xa0, 0x16, 0x11, 0x0e, 0x1b, 0x6e, 0x81, 0x13, - 0xd7, 0xf4, 0x23, 0xa1, 0xb2, 0x9b, 0xaf, 0xf6, - 0x6b, 0xc4, 0x2a, 0xdf, 0xbd, 0xe4, 0x61, 0x5c, - }; - uint8_t l_private2[] = { - 0xb5, 0x45, 0xaf, 0xa0, 0x2e, 0x5c, 0xa6, 0x17, - 0x3b, 0x5a, 0x55, 0x76, 0x67, 0x5d, 0xd4, 0x5e, - 0x41, 0x7c, 0x4f, 0x19, 0x9f, 0xb9, 0x75, 0xdc, - 0xba, 0x57, 0xc4, 0xa2, 0x26, 0xc6, 0x86, 0x2a, - }; - uint8_t l_public2[] = { - 0x2e, 0x81, 0x24, 0x3c, 0x44, 0xac, 0x63, 0x13, - 0x9b, 0xc1, 0x27, 0xe9, 0x53, 0x3b, 0x0a, 0xe2, - 0xf9, 0x22, 0xcd, 0x06, 0xfd, 0x12, 0x17, 0x2e, - 0xe5, 0x0e, 0xb5, 0xce, 0x6b, 0x50, 0xe2, 0x44, - 0xbf, 0x6b, 0x3f, 0xe8, 0x4e, 0x70, 0xd1, 0x06, - 0x85, 0x84, 0xb8, 0xef, 0xe2, 0x25, 0x91, 0x21, - 0xf3, 0x46, 0x70, 0xa9, 0x1c, 0x79, 0x19, 0xe3, - 0xfb, 0x11, 0x36, 0x64, 0x37, 0x64, 0x58, 0xc9, - }; - uint8_t tmp[2 * SHA256_DIGEST_LENGTH + SHA256_INTERNAL_BLOCK_SIZE]; - for (i = 0; i < TESTROUNDS; ++i) { - printf("."); + for (int i = 0; i < TESTROUNDS; ++i) { + printf("Round %d\n", i); if (!uECC_shared_secret(l_public2, l_private1, l_secret1, curve)) { printf("\nRound %d: shared_secret() failed (1)", i); @@ -162,7 +164,7 @@ int main(void) } } - printf(" done with %d error(s)\n", errorc); + printf("Done with %d error(s)\n", errorc); if (errorc) { puts("FAILURE"); diff --git a/tests/pkg_micro-ecc/tests/01-run.py b/tests/pkg_micro-ecc/tests/01-run.py index 9f110e4495..a80cc03529 100755 --- a/tests/pkg_micro-ecc/tests/01-run.py +++ b/tests/pkg_micro-ecc/tests/01-run.py @@ -4,16 +4,19 @@ import sys from testrunner import run -# Use a custom global timeout for slow hardware. On microbit (nrf51), the -# test completes in 80s. +# Use a custom global timeout for slow hardware. On ATmegas clocked at 8MHz +# one test round completes in ~36s TIMEOUT = 100 def testfunc(child): child.expect_exact('micro-ecc compiled!') - child.expect_exact('Testing 16 random private key pairs and signature ' - 'without using HWRNG') - child.expect_exact('................ done with 0 error(s)') + child.expect(r'Testing (\d+) random private key pairs and signature ' + 'without using HWRNG') + testrounds = int(child.match.group(1)) + for i in range(testrounds): + child.expect_exact("Round {}".format(i)) + child.expect_exact('Done with 0 error(s)') child.expect_exact('SUCCESS')