/src/nettle-with-libgmp/ecc-ecdsa-sign.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* ecc-ecdsa-sign.c  | 
2  |  |  | 
3  |  |    Copyright (C) 2013, 2014 Niels Möller  | 
4  |  |  | 
5  |  |    This file is part of GNU Nettle.  | 
6  |  |  | 
7  |  |    GNU Nettle is free software: you can redistribute it and/or  | 
8  |  |    modify it under the terms of either:  | 
9  |  |  | 
10  |  |      * the GNU Lesser General Public License as published by the Free  | 
11  |  |        Software Foundation; either version 3 of the License, or (at your  | 
12  |  |        option) any later version.  | 
13  |  |  | 
14  |  |    or  | 
15  |  |  | 
16  |  |      * the GNU General Public License as published by the Free  | 
17  |  |        Software Foundation; either version 2 of the License, or (at your  | 
18  |  |        option) any later version.  | 
19  |  |  | 
20  |  |    or both in parallel, as here.  | 
21  |  |  | 
22  |  |    GNU Nettle is distributed in the hope that it will be useful,  | 
23  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
24  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
25  |  |    General Public License for more details.  | 
26  |  |  | 
27  |  |    You should have received copies of the GNU General Public License and  | 
28  |  |    the GNU Lesser General Public License along with this program.  If  | 
29  |  |    not, see http://www.gnu.org/licenses/.  | 
30  |  | */  | 
31  |  |  | 
32  |  | /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */  | 
33  |  |  | 
34  |  | #if HAVE_CONFIG_H  | 
35  |  | # include "config.h"  | 
36  |  | #endif  | 
37  |  |  | 
38  |  | #include <assert.h>  | 
39  |  | #include <stdlib.h>  | 
40  |  |  | 
41  |  | #include "ecdsa.h"  | 
42  |  | #include "ecc-internal.h"  | 
43  |  | #include "dsa-internal.h"  | 
44  |  |  | 
45  |  | /* Low-level ECDSA signing */  | 
46  |  |  | 
47  |  | mp_size_t  | 
48  |  | ecc_ecdsa_sign_itch (const struct ecc_curve *ecc)  | 
49  | 0  | { | 
50  |  |   /* Needs 3*ecc->p.size + scratch for ecc_mul_g. */  | 
51  | 0  |   assert (ecc->p.size + ecc->p.invert_itch  | 
52  | 0  |     <= 3*ecc->p.size + ECC_MUL_G_ITCH (ecc->p.size));  | 
53  | 0  |   return ECC_ECDSA_SIGN_ITCH (ecc->p.size);  | 
54  | 0  | }  | 
55  |  |  | 
56  |  | /* NOTE: Caller should check if r or s is zero. */  | 
57  |  | void  | 
58  |  | ecc_ecdsa_sign (const struct ecc_curve *ecc,  | 
59  |  |     const mp_limb_t *zp,  | 
60  |  |     /* Random nonce, must be invertible mod ecc group  | 
61  |  |        order. */  | 
62  |  |     const mp_limb_t *kp,  | 
63  |  |     size_t length, const uint8_t *digest,  | 
64  |  |     mp_limb_t *rp, mp_limb_t *sp,  | 
65  |  |     mp_limb_t *scratch)  | 
66  | 246  | { | 
67  | 984  | #define P     scratch  | 
68  | 492  | #define kinv      scratch  | 
69  | 984  | #define hp      (scratch  + ecc->p.size) /* NOTE: ecc->p.size + 1 limbs! */  | 
70  | 1.23k  | #define tp      (scratch + 2*ecc->p.size)  | 
71  |  |   /* Procedure, according to RFC 6090, "KT-I". q denotes the group  | 
72  |  |      order.  | 
73  |  |  | 
74  |  |      1. k <-- uniformly random, 0 < k < q  | 
75  |  |  | 
76  |  |      2. R <-- (r_x, r_y) = k g  | 
77  |  |  | 
78  |  |      3. s1 <-- r_x mod q  | 
79  |  |  | 
80  |  |      4. s2 <-- (h + z*s1)/k mod q.  | 
81  |  |   */  | 
82  |  |  | 
83  | 246  |   ecc_mul_g (ecc, P, kp, P + 3*ecc->p.size);  | 
84  |  |   /* x coordinate only, modulo q */  | 
85  | 246  |   ecc_j_to_a (ecc, 2, rp, P, P + 3*ecc->p.size);  | 
86  |  |  | 
87  |  |   /* Invert k, uses up to 7 * ecc->p.size including scratch (for secp384). */  | 
88  | 246  |   ecc->q.invert (&ecc->q, kinv, kp, tp);  | 
89  |  |     | 
90  |  |   /* Process hash digest */  | 
91  | 246  |   _nettle_dsa_hash (hp, ecc->q.bit_size, length, digest);  | 
92  |  |  | 
93  | 246  |   ecc_mod_mul (&ecc->q, tp, zp, rp, tp);  | 
94  | 246  |   ecc_mod_add (&ecc->q, hp, hp, tp);  | 
95  | 246  |   ecc_mod_mul_canonical (&ecc->q, sp, hp, kinv, tp);  | 
96  |  |  | 
97  | 246  | #undef P  | 
98  | 246  | #undef hp  | 
99  | 246  | #undef kinv  | 
100  | 246  | #undef tp  | 
101  | 246  | }  |