X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=ecdsa%2Fecdsa_sign.c;h=bd5ff9097466cb8e950ae231ab5e6404c7b39eaf;hb=6c688ee8a14c2452afb4e3c855939b9d0dc858d7;hp=8bcf4f41bc6be599047285c976893c9d95e25920;hpb=33d65e8032f77c1cbec1bc99e898affc96966c3c;p=avr-crypto-lib.git diff --git a/ecdsa/ecdsa_sign.c b/ecdsa/ecdsa_sign.c index 8bcf4f4..bd5ff90 100644 --- a/ecdsa/ecdsa_sign.c +++ b/ecdsa/ecdsa_sign.c @@ -31,7 +31,7 @@ #include "bigint_io.h" -uint8_t ecdsa_signature_alloc(ecdsa_signature_t* s, size_t length_B){ +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; @@ -46,13 +46,13 @@ uint8_t ecdsa_signature_alloc(ecdsa_signature_t* s, size_t length_B){ return 0; } -void ecdsa_signature_free(ecdsa_signature_t* s){ +void ecdsa_signature_free(ecdsa_signature_t *s){ free(s->r.wordv); free(s->s.wordv); } -uint8_t ecdsa_sign_bigint(ecdsa_signature_t* s, const bigint_t* m, - const ecdsa_ctx_t* ctx, const bigint_t* k){ +uint8_t ecdsa_sign_bigint(ecdsa_signature_t *s, const bigint_t *m, + const ecdsa_ctx_t *ctx, const bigint_t *k){ bigint_t t; ecc_combi_point_t q; @@ -76,28 +76,14 @@ uint8_t ecdsa_sign_bigint(ecdsa_signature_t* s, const bigint_t* m, return 2; } ecc_chudnovsky_to_affine_point(&q.affine, &q.chudnovsky, ctx->curve); - bigint_inverse(&s->s, k, ctx->curve->p); + bigint_inverse(&s->s, k, ctx->curve->n); + 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_reduce(&t, ctx->curve->n); bigint_add_u(&t, &t, m); - ctx->curve->reduce_p(&t); - printf_P(PSTR("t (2): ")); - bigint_print_hex(&t); - putchar('\n'); + bigint_reduce(&t, ctx->curve->n); bigint_mul_u(&t, &t, &s->s); - ctx->curve->reduce_p(&t); + bigint_reduce(&t, ctx->curve->n); if(t.length_W == 0){ printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__); return 2; @@ -111,8 +97,8 @@ uint8_t ecdsa_sign_bigint(ecdsa_signature_t* s, const bigint_t* m, return 0; } -uint8_t ecdsa_sign_hash(ecdsa_signature_t* s, const void* hash, - size_t hash_len_B, 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; @@ -151,6 +137,7 @@ uint8_t ecdsa_sign_hash(ecdsa_signature_t* s, const void* hash, ((uint8_t*)m_int.wordv)[idx++] = ((uint8_t*)hash)[--hash_len_B]; } } + bigint_adjust(&m_int); do{ if(rand_in == NULL){ size_t i; @@ -169,8 +156,8 @@ uint8_t ecdsa_sign_hash(ecdsa_signature_t* s, const void* hash, 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, +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;