]> git.cryptolib.org Git - avr-crypto-lib.git/blob - ecdsa/ecdsa_sign.c
bug fixing and support for malloc instead of stack memory (some functions)
[avr-crypto-lib.git] / ecdsa / ecdsa_sign.c
1 /* ecdsa.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2012 Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19
20
21 #include <stdlib.h>
22 #include <inttypes.h>
23 #include <string.h>
24 #include "bigint.h"
25 #include "ecc.h"
26 #include "random_dummy.h"
27 #include "ecdsa.h"
28 #include "hfal-basic.h"
29
30 #include <stdio.h>
31 #include "bigint_io.h"
32
33
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__);
37         return 1;
38     }
39     if(!(s->s.wordv = malloc(length_B))){
40         printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
41         free(s->r.wordv);
42         return 1;
43     }
44     bigint_set_zero(&s->r);
45     bigint_set_zero(&s->s);
46     return 0;
47 }
48
49 void ecdsa_signature_free(ecdsa_signature_t *s){
50     free(s->r.wordv);
51     free(s->s.wordv);
52 }
53
54 uint8_t ecdsa_sign_bigint(ecdsa_signature_t *s, const bigint_t *m,
55                           const ecdsa_ctx_t *ctx, const bigint_t *k){
56
57     bigint_t t;
58     ecc_combi_point_t q;
59
60
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__);
63         return 1;
64     }
65     t.info = 0;
66     if(ecc_chudnovsky_point_alloc(&q.chudnovsky, ctx->curve->p->length_W * sizeof(bigint_word_t))){
67         free(t.wordv);
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__);
70         return 1;
71     }
72
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__);
76         return 2;
77     }
78     ecc_chudnovsky_to_affine_point(&q.affine, &q.chudnovsky, ctx->curve);
79     bigint_inverse(&s->s, k, ctx->curve->p);
80     bigint_mul_u(&t, &q.affine.x, ctx->priv);
81     ctx->curve->reduce_p(&t);
82     printf_P(PSTR("msg:   "));
83     bigint_print_hex(m);
84     putchar('\n');
85     printf_P(PSTR("k:     "));
86     bigint_print_hex(k);
87     putchar('\n');
88     printf_P(PSTR("k-inv: "));
89     bigint_print_hex(&s->s);
90     putchar('\n');
91     printf_P(PSTR("t (1): "));
92     bigint_print_hex(&t);
93     putchar('\n');
94     bigint_add_u(&t, &t, m);
95     ctx->curve->reduce_p(&t);
96     printf_P(PSTR("t (2): "));
97     bigint_print_hex(&t);
98     putchar('\n');
99     bigint_mul_u(&t, &t, &s->s);
100     ctx->curve->reduce_p(&t);
101     if(t.length_W == 0){
102         printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
103         return 2;
104     }
105     bigint_copy(&s->r, &q.affine.x);
106     bigint_copy(&s->s, &t);
107
108     ecc_chudnovsky_point_free(&q.chudnovsky);
109     free(t.wordv);
110
111     return 0;
112 }
113
114 uint8_t ecdsa_sign_hash(ecdsa_signature_t *s, const void *hash,
115                            size_t hash_len_B, const ecdsa_ctx_t *ctx,
116                            const void *rand_in){
117     bigint_t m_int;
118     bigint_t r_int;
119     size_t idx = 0;
120     uint8_t r;
121
122     r_int.length_W = ctx->curve->p->length_W;
123
124     if(rand_in == NULL){
125         if(!(r_int.wordv = malloc(ctx->curve->p->length_W * sizeof(bigint_word_t)))){
126             return 1;
127         }
128     }else{
129         r_int.wordv = (bigint_word_t*)rand_in;
130         r_int.info = 0;
131         bigint_adjust(&r_int);
132     }
133
134     m_int.length_W = ctx->curve->p->length_W;
135     m_int.wordv = malloc(m_int.length_W *  sizeof(bigint_word_t));
136     if(m_int.wordv == NULL){
137         free(r_int.wordv);
138         return 1;
139     }
140     m_int.wordv[m_int.length_W - 1] = 0;
141
142     if(hash_len_B > m_int.length_W * sizeof(bigint_word_t)){
143         while(idx < m_int.length_W * sizeof(bigint_word_t)){
144             ((uint8_t*)m_int.wordv)[idx] = ((uint8_t*)hash)[m_int.length_W * sizeof(bigint_word_t) - idx - 1];
145             ++idx;
146         }
147     }else{
148         memset(m_int.wordv, 0, m_int.length_W * sizeof(bigint_word_t));
149         // idx += m_int.length_W * sizeof(bigint_word_t) - hash_len_B;
150         while(hash_len_B){
151             ((uint8_t*)m_int.wordv)[idx++] = ((uint8_t*)hash)[--hash_len_B];
152         }
153     }
154     bigint_adjust(&m_int);
155     do{
156         if(rand_in == NULL){
157             size_t i;
158             do{
159                 i = ctx->curve->p->length_W * sizeof(bigint_word_t) - 1;
160                 do{
161                     ((uint8_t*)r_int.wordv)[i] = prng_get_byte();
162                 }while(i--);
163                 bigint_adjust(&r_int);
164             }while(bigint_cmp_u(&r_int, ctx->curve->p) >= 0);
165         }
166     }while((r = ecdsa_sign_bigint(s, &m_int, ctx, &r_int)) == 2 && (rand_in == NULL));
167
168     free(m_int.wordv);
169
170     return r;
171 }
172
173 uint8_t ecdsa_sign_message(ecdsa_signature_t *s, const void *m, uint16_t m_len_b,
174                        const hfdesc_t *hash_desc, const ecdsa_ctx_t *ctx,
175                        const void *rand_in){
176
177     uint8_t *hash;
178     uint16_t hash_len = hfal_hash_getHashsize(hash_desc) / 8;
179     uint8_t r;
180
181
182     hash = malloc(hash_len);
183     if(hash == NULL){
184         return 1;
185     }
186     hfal_hash_mem(hash_desc, hash, m, m_len_b);
187     ecdsa_sign_hash(s, hash, hash_len, ctx, rand_in);
188
189     return r;
190 }