3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "random_dummy.h"
28 #include "hfal-basic.h"
31 #include "bigint_io.h"
34 uint8_t ecdsa_signature_alloc(ecdsa_signature_t *s, size_t length_B){
35 if(!(s->r.wordv = malloc(length_B))){
36 printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
39 if(!(s->s.wordv = malloc(length_B))){
40 printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
44 bigint_set_zero(&s->r);
45 bigint_set_zero(&s->s);
49 void ecdsa_signature_free(ecdsa_signature_t *s){
54 uint8_t ecdsa_sign_bigint(ecdsa_signature_t *s, const bigint_t *m,
55 const ecdsa_ctx_t *ctx, const bigint_t *k){
61 if(!(t.wordv = malloc(ctx->curve->p->length_W * 2 * sizeof(bigint_word_t)))){
62 printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
66 if(ecc_chudnovsky_point_alloc(&q.chudnovsky, ctx->curve->p->length_W * sizeof(bigint_word_t))){
68 printf_P(PSTR("item size: %u bytes\n"), ctx->curve->p->length_W * sizeof(bigint_word_t));
69 printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
73 ecc_chudnovsky_multiplication(&q.chudnovsky, k, ctx->basepoint, ctx->curve);
74 if(q.chudnovsky.x.length_W == 0){
75 printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
78 ecc_chudnovsky_to_affine_point(&q.affine, &q.chudnovsky, ctx->curve);
79 bigint_inverse(&s->s, k, ctx->curve->n);
81 bigint_mul_u(&t, &q.affine.x, ctx->priv);
82 bigint_reduce(&t, ctx->curve->n);
83 bigint_add_u(&t, &t, m);
84 bigint_reduce(&t, ctx->curve->n);
85 bigint_mul_u(&t, &t, &s->s);
86 bigint_reduce(&t, ctx->curve->n);
88 printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
91 bigint_copy(&s->r, &q.affine.x);
92 bigint_copy(&s->s, &t);
94 ecc_chudnovsky_point_free(&q.chudnovsky);
100 uint8_t ecdsa_sign_hash(ecdsa_signature_t *s, const void *hash,
101 size_t hash_len_B, const ecdsa_ctx_t *ctx,
102 const void *rand_in){
108 r_int.length_W = ctx->curve->p->length_W;
111 if(!(r_int.wordv = malloc(ctx->curve->p->length_W * sizeof(bigint_word_t)))){
115 r_int.wordv = (bigint_word_t*)rand_in;
117 bigint_adjust(&r_int);
120 m_int.length_W = ctx->curve->p->length_W;
121 m_int.wordv = malloc(m_int.length_W * sizeof(bigint_word_t));
122 if(m_int.wordv == NULL){
126 m_int.wordv[m_int.length_W - 1] = 0;
128 if(hash_len_B > m_int.length_W * sizeof(bigint_word_t)){
129 while(idx < m_int.length_W * sizeof(bigint_word_t)){
130 ((uint8_t*)m_int.wordv)[idx] = ((uint8_t*)hash)[m_int.length_W * sizeof(bigint_word_t) - idx - 1];
134 memset(m_int.wordv, 0, m_int.length_W * sizeof(bigint_word_t));
135 // idx += m_int.length_W * sizeof(bigint_word_t) - hash_len_B;
137 ((uint8_t*)m_int.wordv)[idx++] = ((uint8_t*)hash)[--hash_len_B];
140 bigint_adjust(&m_int);
145 i = ctx->curve->p->length_W * sizeof(bigint_word_t) - 1;
147 ((uint8_t*)r_int.wordv)[i] = prng_get_byte();
149 bigint_adjust(&r_int);
150 }while(bigint_cmp_u(&r_int, ctx->curve->p) >= 0);
152 }while((r = ecdsa_sign_bigint(s, &m_int, ctx, &r_int)) == 2 && (rand_in == NULL));
159 uint8_t ecdsa_sign_message(ecdsa_signature_t *s, const void *m, uint16_t m_len_b,
160 const hfdesc_t *hash_desc, const ecdsa_ctx_t *ctx,
161 const void *rand_in){
164 uint16_t hash_len = hfal_hash_getHashsize(hash_desc) / 8;
168 hash = malloc(hash_len);
172 hfal_hash_mem(hash_desc, hash, m, m_len_b);
173 ecdsa_sign_hash(s, hash, hash_len, ctx, rand_in);