X-Git-Url: https://git.cryptolib.org/avr-crypto-lib.git?p=avr-crypto-lib.git;a=blobdiff_plain;f=ecdsa%2Fecdsa_sign.c;h=8bcf4f41bc6be599047285c976893c9d95e25920;hp=3a9fd21c97daa7f7ea22d6c3c51b62f467c63979;hb=33d65e8032f77c1cbec1bc99e898affc96966c3c;hpb=872dff3138536a68b6dd96b182a386d242bee6cb diff --git a/ecdsa/ecdsa_sign.c b/ecdsa/ecdsa_sign.c index 3a9fd21..8bcf4f4 100644 --- a/ecdsa/ecdsa_sign.c +++ b/ecdsa/ecdsa_sign.c @@ -20,17 +20,24 @@ #include #include +#include #include "bigint.h" #include "ecc.h" #include "random_dummy.h" #include "ecdsa.h" #include "hfal-basic.h" +#include +#include "bigint_io.h" + + uint8_t ecdsa_signature_alloc(ecdsa_signature_t* s, size_t length_B){ if(!(s->r.wordv = malloc(length_B))){ + printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__); return 1; } if(!(s->s.wordv = malloc(length_B))){ + printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__); free(s->r.wordv); return 1; } @@ -52,26 +59,47 @@ uint8_t ecdsa_sign_bigint(ecdsa_signature_t* s, const bigint_t* m, if(!(t.wordv = malloc(ctx->curve->p->length_W * 2 * sizeof(bigint_word_t)))){ + printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__); return 1; } - if(!ecc_chudnovsky_point_alloc(&q.chudnovsky, ctx->curve->p->length_W * sizeof(bigint_word_t))){ + t.info = 0; + if(ecc_chudnovsky_point_alloc(&q.chudnovsky, ctx->curve->p->length_W * sizeof(bigint_word_t))){ free(t.wordv); + printf_P(PSTR("item size: %u bytes\n"), ctx->curve->p->length_W * sizeof(bigint_word_t)); + printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__); return 1; } ecc_chudnovsky_multiplication(&q.chudnovsky, k, ctx->basepoint, ctx->curve); if(q.chudnovsky.x.length_W == 0){ + printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__); return 2; } ecc_chudnovsky_to_affine_point(&q.affine, &q.chudnovsky, ctx->curve); - bigint_inverse(&s->r, k, ctx->curve->p); + bigint_inverse(&s->s, k, ctx->curve->p); bigint_mul_u(&t, &q.affine.x, ctx->priv); ctx->curve->reduce_p(&t); + printf_P(PSTR("msg: ")); + bigint_print_hex(m); + putchar('\n'); + printf_P(PSTR("k: ")); + bigint_print_hex(k); + putchar('\n'); + printf_P(PSTR("k-inv: ")); + bigint_print_hex(&s->s); + putchar('\n'); + printf_P(PSTR("t (1): ")); + bigint_print_hex(&t); + putchar('\n'); bigint_add_u(&t, &t, m); ctx->curve->reduce_p(&t); - bigint_mul_u(&t, &t, &s->r); + printf_P(PSTR("t (2): ")); + bigint_print_hex(&t); + putchar('\n'); + bigint_mul_u(&t, &t, &s->s); ctx->curve->reduce_p(&t); if(t.length_W == 0){ + printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__); return 2; } bigint_copy(&s->r, &q.affine.x); @@ -82,33 +110,52 @@ uint8_t ecdsa_sign_bigint(ecdsa_signature_t* s, const bigint_t* m, return 0; } -uint8_t ecdsa_sign_message(ecdsa_signature_t* s, const void* m, uint16_t m_len_b, - const hfdesc_t* hash_desc, const ecdsa_ctx_t* ctx, + +uint8_t ecdsa_sign_hash(ecdsa_signature_t* s, const void* hash, + size_t hash_len_B, const ecdsa_ctx_t* ctx, const void *rand_in){ bigint_t m_int; bigint_t r_int; + size_t idx = 0; uint8_t r; + r_int.length_W = ctx->curve->p->length_W; + if(rand_in == NULL){ if(!(r_int.wordv = malloc(ctx->curve->p->length_W * sizeof(bigint_word_t)))){ return 1; } + }else{ + r_int.wordv = (bigint_word_t*)rand_in; + r_int.info = 0; + bigint_adjust(&r_int); } - if(!(m_int.wordv = malloc(hfal_hash_getHashsize(hash_desc) / 8))){ - if(rand_in == NULL){ - free(r_int.wordv); - } + m_int.length_W = ctx->curve->p->length_W; + m_int.wordv = malloc(m_int.length_W * sizeof(bigint_word_t)); + if(m_int.wordv == NULL){ + free(r_int.wordv); return 1; } - hfal_hash_mem(hash_desc, m_int.wordv, m, m_len_b); + m_int.wordv[m_int.length_W - 1] = 0; + if(hash_len_B > m_int.length_W * sizeof(bigint_word_t)){ + while(idx < m_int.length_W * sizeof(bigint_word_t)){ + ((uint8_t*)m_int.wordv)[idx] = ((uint8_t*)hash)[m_int.length_W * sizeof(bigint_word_t) - idx - 1]; + ++idx; + } + }else{ + memset(m_int.wordv, 0, m_int.length_W * sizeof(bigint_word_t)); + // idx += m_int.length_W * sizeof(bigint_word_t) - hash_len_B; + while(hash_len_B){ + ((uint8_t*)m_int.wordv)[idx++] = ((uint8_t*)hash)[--hash_len_B]; + } + } do{ if(rand_in == NULL){ size_t i; do{ i = ctx->curve->p->length_W * sizeof(bigint_word_t) - 1; - r_int.length_W = ctx->curve->p->length_W; do{ ((uint8_t*)r_int.wordv)[i] = prng_get_byte(); }while(i--); @@ -117,5 +164,26 @@ uint8_t ecdsa_sign_message(ecdsa_signature_t* s, const void* m, uint16_t m_len_b } }while((r = ecdsa_sign_bigint(s, &m_int, ctx, &r_int)) == 2 && (rand_in == NULL)); + free(m_int.wordv); + + return r; +} + +uint8_t ecdsa_sign_message(ecdsa_signature_t* s, const void* m, uint16_t m_len_b, + const hfdesc_t* hash_desc, const ecdsa_ctx_t* ctx, + const void *rand_in){ + + uint8_t *hash; + uint16_t hash_len = hfal_hash_getHashsize(hash_desc) / 8; + uint8_t r; + + + hash = malloc(hash_len); + if(hash == NULL){ + return 1; + } + hfal_hash_mem(hash_desc, hash, m, m_len_b); + ecdsa_sign_hash(s, hash, hash_len, ctx, rand_in); + return r; }