X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=test_src%2Fmain-ecdsa-test.c;h=926163e5347f074668a3241101e428429100cfa8;hb=2c909fca2a13cd76a526515bda5d0292483d7a55;hp=f5b0db3ee648bfe90e447c46f8b77f1c0abebbd7;hpb=33d65e8032f77c1cbec1bc99e898affc96966c3c;p=avr-crypto-lib.git diff --git a/test_src/main-ecdsa-test.c b/test_src/main-ecdsa-test.c index f5b0db3..926163e 100644 --- a/test_src/main-ecdsa-test.c +++ b/test_src/main-ecdsa-test.c @@ -45,7 +45,7 @@ #include "base64_enc.h" #include "base64_dec.h" -char* algo_name = "ECDSA"; +char *algo_name = "ECDSA"; /***************************************************************************** * additional validation-functions * @@ -95,7 +95,7 @@ uint8_t convert_hex_to_byte(char a, char b){ return (convert_hexchar_to_value(a) << 4) | convert_hexchar_to_value(b); } -void *hash_message(hfdesc_t* hash_function){ +void *hash_message(hfdesc_t *hash_function){ uint8_t *block, *hash_value; uint16_t index = 0; hfgen_ctx_t ctx; @@ -492,6 +492,9 @@ void testrun_genkey1(void){ printf_P(PSTR("\n Qy: ")); bigint_print_hex(&qa.y); puts("\n"); + + ecc_affine_point_free(&qa); + ecc_chudnovsky_point_free(&q); } void testrun_genkey3(void){ @@ -531,6 +534,10 @@ void testrun_genkey3(void){ printf_P(PSTR("\n Qy: ")); bigint_print_hex(&qa.y); puts("\n"); + + ecc_affine_point_free(&qa); + ecc_chudnovsky_point_free(&q); + } void testrun_genkey(void){ @@ -589,6 +596,78 @@ void testrun_genkey(void){ ecc_chudnovsky_point_free(&q); ecc_affine_point_free(&qa); } +/* +N is +3128D2B4 B1C96B14 36F8DE99 FFFFFFFF FFFFFFFF FFFFFFFF 99DEF836 146BC9B1 B4D22831 +-------------------------------------------------------------- +C is +78916860 32FD8057 F636B44B 1F47CCE5 64D25099 23A7465A +-------------------------------------------------------------- +D is +78916860 32FD8057 F636B44B 1F47CCE5 64D25099 23A7465B +Q_x is +FBA2AAC6 47884B50 4EB8CD5A 0A1287BA BCC62163 F606A9A2 +Q_y is +DAE6D4CC 05EF4F27 D79EE38B 71C9C8EF 4865D988 50D84AA5 +*/ + +void testrun_interm(void){ + ecc_chudnovsky_point_t q; + ecc_affine_point_t qa; + uint32_t time; + bigint_t k; + uint8_t r; + + printf_P(PSTR("\n== testing key generation ==\n")); + + printf_P(PSTR("enter secret key d: ")); + bigint_read_hex_echo(&k); + putchar('\n'); + + if(ecc_chudnovsky_point_alloc(&q, 192)){ + printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__); + return; + } + if(ecc_affine_point_alloc(&qa, 192)){ + ecc_chudnovsky_point_free(&q); + printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__); + return; + } + + printf_P(PSTR("(naf) k: ")); + bigint_print_hex(&k); + startTimer(1); + START_TIMER; + r = ecc_chudnovsky_naf_multiplication(&q, &k, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192); + STOP_TIMER; + time = stopTimer(); + ecc_chudnovsky_to_affine_point(&qa, &q, &nist_curve_p192); + + printf_P(PSTR("\n Qx: ")); + bigint_print_hex(&qa.x); + printf_P(PSTR("\n Qy: ")); + bigint_print_hex(&qa.y); + printf_P(PSTR("\n time: %"PRIu32" cycles (r code: %"PRIu8")\n"), time, r); + + printf_P(PSTR("(d&a) k: ")); + bigint_print_hex(&k); + startTimer(1); + START_TIMER; + r = ecc_chudnovsky_double_and_add(&q, &k, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192); + STOP_TIMER; + time = stopTimer(); + ecc_chudnovsky_to_affine_point(&qa, &q, &nist_curve_p192); + + printf_P(PSTR("\n Qx: ")); + bigint_print_hex(&qa.x); + printf_P(PSTR("\n Qy: ")); + bigint_print_hex(&qa.y); + printf_P(PSTR("\n time: %"PRIu32" cycles (r code: %"PRIu8")\n"), time, r); + free(k.wordv); + ecc_chudnovsky_point_free(&q); + ecc_affine_point_free(&qa); +} + #endif @@ -630,19 +709,27 @@ const uint8_t ecdsa_test_1_msg[] PROGMEM = { 0x11, 0x43, 0x59, 0xce, 0xe4, 0xa0, 0x71, 0xcf }; +/* + * d = e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3 + */ + const uint8_t ecdsa_test_1_d[] PROGMEM = { 0xf3, 0xd7, 0x60, 0xd6, 0x75, 0xf2, 0xcc, 0xeb, 0xf0, 0xd2, 0xfd, 0xb3, 0xb9, 0x41, 0x3f, 0xb0, 0xf8, 0x4f, 0x37, 0xd1, 0xb3, 0x37, 0x4f, 0xe1 }; +/* + * k = cb0abc7043a10783684556fb12c4154d57bc31a289685f25 + */ + const uint8_t ecdsa_test_1_k[] PROGMEM = { 0x25, 0x5f, 0x68, 0x89, 0xa2, 0x31, 0xbc, 0x57, 0x4d, 0x15, 0xc4, 0x12, 0xfb, 0x56, 0x45, 0x68, 0x83, 0x07, 0xa1, 0x43, 0x70, 0xbc, 0x0a, 0xcb }; -void hash_mem_P(const hfdesc_t* hfdesc, void* dest, const void* msg, uint16_t msg_len_b){ +void hash_mem_P(const hfdesc_t *hfdesc, void *dest, const void *msg, uint16_t msg_len_b){ uint16_t blocksize = hfal_hash_getBlocksize(hfdesc); uint8_t block[blocksize / 8]; hfgen_ctx_t ctx; @@ -690,6 +777,7 @@ const uint8_t ecdsa_test_2_k[] PROGMEM = { 0x47, 0xfa, 0x11, 0xbc, 0xb1, 0xc2, 0xe8, 0x41 }; +#if 0 void test_sign1(void){ bigint_word_t d_w[sizeof(ecdsa_test_1_d)]; uint8_t rnd[sizeof(ecdsa_test_1_k)]; @@ -704,8 +792,86 @@ void test_sign1(void){ putchar('\n'); d.wordv = d_w; memcpy_P(rnd, ecdsa_test_1_k, sizeof(ecdsa_test_1_k)); - memcpy_P(d_w, ecdsa_test_1_d, sizeof(ecdsa_test_1_d) * sizeof(bigint_word_t)); - d.length_W = sizeof(ecdsa_test_1_d) / sizeof(bigint_word_t); + memcpy_P(d_w, ecdsa_test_1_d, sizeof(ecdsa_test_1_d)); + d.length_W = (sizeof(ecdsa_test_1_d) + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); + d.info = 0; + bigint_adjust(&d); + + hash_desc = &sha1_desc; //hash_select(); + hash = malloc(hfal_hash_getHashsize(hash_desc) / 8); + if(hash == NULL){ + printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__); + } + hash_mem_P(hash_desc, hash, ecdsa_test_1_msg, sizeof(ecdsa_test_1_msg) * 8); + printf_P(PSTR("msg hash: ")); + cli_hexdump(hash, hfal_hash_getHashsize(hash_desc) / 8); + putchar('\n'); + + ecc_affine_point_alloc(&q.affine, nist_curve_p192_p.length_W * sizeof(bigint_word_t)); +// ecc_chudnovsky_point_alloc(&q.chudnovsky, nist_curve_p192_p.length_W * sizeof(bigint_word_t)); +// ctx.basepoint = &nist_curve_p192_basepoint.chudnovsky; + ctx.basepoint = &nist_curve_p192_basepoint.affine; + ctx.priv = &d; + ctx.curve = &nist_curve_p192; + + printf("\n d: "); + bigint_print_hex(&d); + printf_P(PSTR("\n Gx: ")); + bigint_print_hex(&nist_curve_p192_basepoint.affine.x); + printf_P(PSTR("\n Gy: ")); + bigint_print_hex(&nist_curve_p192_basepoint.affine.y); + +// r = ecc_chudnovsky_multiplication(&q.chudnovsky, &d, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192); + r = ecc_???? + if(r){ + printf_P(PSTR("ERROR: ecc_chudnovsky_multiplication() returned: %"PRIu8"\n"), r); + } +// r = ecc_chudnovsky_to_affine_point(&q.affine, &q.chudnovsky, &nist_curve_p192); + if(r){ + printf_P(PSTR("ERROR: ecc_chudnovsky_to_affine_point() returned: %"PRIu8"\n"), r); + } + + printf_P(PSTR("\n Qx: ")); + bigint_print_hex(&q.affine.x); + printf_P(PSTR("\n Qy: ")); + bigint_print_hex(&q.affine.y); + putchar('\n'); + ctx.pub = &q.affine; + + ecdsa_signature_alloc(&sign, sizeof(ecdsa_test_1_d) * sizeof(bigint_word_t)); + + r = ecdsa_sign_hash(&sign, hash, hfal_hash_getHashsize(hash_desc) / 8, &ctx, rnd); + if(r){ + printf_P(PSTR("ERROR: ecdsa_sign_message() returned: %"PRIu8"\n"), r); + } + printf_P(PSTR(" r: ")); + bigint_print_hex(&sign.r); + printf_P(PSTR("\n s: ")); + bigint_print_hex(&sign.s); + + free(hash); + ecdsa_signature_free(&sign); + ecc_chudnovsky_point_free(&q.chudnovsky); +} + +#else + +void test_sign1(void){ + bigint_word_t d_w[sizeof(ecdsa_test_1_d)]; + uint8_t rnd[sizeof(ecdsa_test_1_k)]; + uint8_t *hash; + bigint_t d; + const hfdesc_t *hash_desc; + ecc_combi_point_t q; + ecdsa_signature_t sign; + ecdsa_ctx_t ctx; + uint8_t r; + + putchar('\n'); + d.wordv = d_w; + memcpy_P(rnd, ecdsa_test_1_k, sizeof(ecdsa_test_1_k)); + memcpy_P(d_w, ecdsa_test_1_d, sizeof(ecdsa_test_1_d)); + d.length_W = (sizeof(ecdsa_test_1_d) + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); d.info = 0; bigint_adjust(&d); @@ -763,6 +929,8 @@ void test_sign1(void){ ecc_chudnovsky_point_free(&q.chudnovsky); } +#endif + void test_sign2(void){ bigint_word_t d_w[sizeof(ecdsa_test_2_d)]; uint8_t rnd[sizeof(ecdsa_test_2_k)];