]> git.cryptolib.org Git - avr-crypto-lib.git/blob - ecdsa/ecdsa_sign.c
fixing and extending ecdsa
[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 "bigint.h"
24 #include "ecc.h"
25 #include "random_dummy.h"
26 #include "ecdsa.h"
27 #include "hfal-basic.h"
28
29 uint8_t ecdsa_signature_alloc(ecdsa_signature_t* s, size_t length_B){
30     if(!(s->r.wordv = malloc(length_B))){
31         return 1;
32     }
33     if(!(s->s.wordv = malloc(length_B))){
34         free(s->r.wordv);
35         return 1;
36     }
37     bigint_set_zero(&s->r);
38     bigint_set_zero(&s->s);
39     return 0;
40 }
41
42 void ecdsa_signature_free(ecdsa_signature_t* s){
43     free(s->r.wordv);
44     free(s->s.wordv);
45 }
46
47 uint8_t ecdsa_sign_bigint(ecdsa_signature_t* s, const bigint_t* m,
48                           const ecdsa_ctx_t* ctx, const bigint_t* k){
49
50     bigint_t t;
51     ecc_combi_point_t q;
52
53
54     if(!(t.wordv = malloc(ctx->curve->p->length_W * 2 * sizeof(bigint_word_t)))){
55         return 1;
56     }
57     if(!ecc_chudnovsky_point_alloc(&q.chudnovsky, ctx->curve->p->length_W * sizeof(bigint_word_t))){
58         free(t.wordv);
59         return 1;
60     }
61
62     ecc_chudnovsky_multiplication(&q.chudnovsky, k, ctx->basepoint, ctx->curve);
63     if(q.chudnovsky.x.length_W == 0){
64         return 2;
65     }
66     ecc_chudnovsky_to_affine_point(&q.affine, &q.chudnovsky, ctx->curve);
67     bigint_inverse(&s->r, k, ctx->curve->p);
68     bigint_mul_u(&t, &q.affine.x, ctx->priv);
69     ctx->curve->reduce_p(&t);
70     bigint_add_u(&t, &t, m);
71     ctx->curve->reduce_p(&t);
72     bigint_mul_u(&t, &t, &s->r);
73     ctx->curve->reduce_p(&t);
74     if(t.length_W == 0){
75         return 2;
76     }
77     bigint_copy(&s->r, &q.affine.x);
78     bigint_copy(&s->s, &t);
79
80     ecc_chudnovsky_point_free(&q.chudnovsky);
81     free(t.wordv);
82
83     return 0;
84 }
85 uint8_t ecdsa_sign_message(ecdsa_signature_t* s, const void* m, uint16_t m_len_b,
86                            const hfdesc_t* hash_desc, const ecdsa_ctx_t* ctx,
87                            const void *rand_in){
88     bigint_t m_int;
89     bigint_t r_int;
90     uint8_t r;
91
92     if(rand_in == NULL){
93         if(!(r_int.wordv = malloc(ctx->curve->p->length_W * sizeof(bigint_word_t)))){
94             return 1;
95         }
96     }
97
98     if(!(m_int.wordv = malloc(hfal_hash_getHashsize(hash_desc) / 8))){
99         if(rand_in == NULL){
100             free(r_int.wordv);
101         }
102         return 1;
103     }
104     hfal_hash_mem(hash_desc, m_int.wordv, m, m_len_b);
105
106     do{
107         if(rand_in == NULL){
108             size_t i;
109             do{
110                 i = ctx->curve->p->length_W * sizeof(bigint_word_t) - 1;
111                 r_int.length_W = ctx->curve->p->length_W;
112                 do{
113                     ((uint8_t*)r_int.wordv)[i] = prng_get_byte();
114                 }while(i--);
115                 bigint_adjust(&r_int);
116             }while(bigint_cmp_u(&r_int, ctx->curve->p) >= 0);
117         }
118     }while((r = ecdsa_sign_bigint(s, &m_int, ctx, &r_int)) == 2 && (rand_in == NULL));
119
120     return r;
121 }