/src/nettle/ecc-gostdsa-sign.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* ecc-gostdsa-sign.c  | 
2  |  |  | 
3  |  |    Copyright (C) 2015 Dmitry Eremin-Solenikov  | 
4  |  |    Copyright (C) 2013, 2014 Niels Möller  | 
5  |  |  | 
6  |  |    This file is part of GNU Nettle.  | 
7  |  |  | 
8  |  |    GNU Nettle is free software: you can redistribute it and/or  | 
9  |  |    modify it under the terms of either:  | 
10  |  |  | 
11  |  |      * the GNU Lesser General Public License as published by the Free  | 
12  |  |        Software Foundation; either version 3 of the License, or (at your  | 
13  |  |        option) any later version.  | 
14  |  |  | 
15  |  |    or  | 
16  |  |  | 
17  |  |      * the GNU General Public License as published by the Free  | 
18  |  |        Software Foundation; either version 2 of the License, or (at your  | 
19  |  |        option) any later version.  | 
20  |  |  | 
21  |  |    or both in parallel, as here.  | 
22  |  |  | 
23  |  |    GNU Nettle is distributed in the hope that it will be useful,  | 
24  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
25  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
26  |  |    General Public License for more details.  | 
27  |  |  | 
28  |  |    You should have received copies of the GNU General Public License and  | 
29  |  |    the GNU Lesser General Public License along with this program.  If  | 
30  |  |    not, see http://www.gnu.org/licenses/.  | 
31  |  | */  | 
32  |  |  | 
33  |  | #if HAVE_CONFIG_H  | 
34  |  | # include "config.h"  | 
35  |  | #endif  | 
36  |  |  | 
37  |  | #include <assert.h>  | 
38  |  | #include <stdlib.h>  | 
39  |  |  | 
40  |  | #include "gostdsa.h"  | 
41  |  | #include "dsa-internal.h"  | 
42  |  | #include "ecc-internal.h"  | 
43  |  |  | 
44  |  | /* Low-level GOST DSA signing */  | 
45  |  |  | 
46  |  | mp_size_t  | 
47  |  | ecc_gostdsa_sign_itch (const struct ecc_curve *ecc)  | 
48  | 0  | { | 
49  |  |   /* Needs 3*ecc->p.size + scratch for ecc_mul_g. */  | 
50  | 0  |   return ECC_GOSTDSA_SIGN_ITCH (ecc->p.size);  | 
51  | 0  | }  | 
52  |  |  | 
53  |  | /* NOTE: Caller should check if r or s is zero. */  | 
54  |  | void  | 
55  |  | ecc_gostdsa_sign (const struct ecc_curve *ecc,  | 
56  |  |     const mp_limb_t *zp,  | 
57  |  |     const mp_limb_t *kp,  | 
58  |  |     size_t length, const uint8_t *digest,  | 
59  |  |     mp_limb_t *rp, mp_limb_t *sp,  | 
60  |  |     mp_limb_t *scratch)  | 
61  | 0  | { | 
62  | 0  | #define P     scratch  | 
63  | 0  | #define hp      (scratch + 4*ecc->p.size)  | 
64  | 0  | #define tp      (scratch + 2*ecc->p.size)  | 
65  | 0  | #define t2p     scratch  | 
66  |  |   /* Procedure, according to GOST 34.10. q denotes the group  | 
67  |  |      order.  | 
68  |  |  | 
69  |  |      1. k <-- uniformly random, 0 < k < q  | 
70  |  |  | 
71  |  |      2. C <-- (c_x, c_y) = k g  | 
72  |  |  | 
73  |  |      3. r <-- c_x mod q  | 
74  |  |  | 
75  |  |      4. s <-- (r*z + k*h) mod q.  | 
76  |  |   */  | 
77  |  | 
  | 
78  | 0  |   ecc_mul_g (ecc, P, kp, P + 3*ecc->p.size);  | 
79  |  |   /* x coordinate only, modulo q */  | 
80  | 0  |   ecc_j_to_a (ecc, 2, rp, P, P + 3*ecc->p.size);  | 
81  |  |  | 
82  |  |   /* Process hash digest */  | 
83  | 0  |   _nettle_gostdsa_hash (hp, ecc->q.bit_size, length, digest);  | 
84  | 0  |   if (mpn_zero_p (hp, ecc->p.size))  | 
85  | 0  |     mpn_add_1 (hp, hp, ecc->p.size, 1);  | 
86  |  | 
  | 
87  | 0  |   ecc_mod_mul (&ecc->q, tp, rp, zp, tp);  | 
88  | 0  |   ecc_mod_mul (&ecc->q, t2p, kp, hp, t2p);  | 
89  | 0  |   ecc_mod_add (&ecc->q, sp, tp, t2p);  | 
90  |  |  | 
91  |  |   /* Also reduce mod ecc->q. It should already be < 2*ecc->q,  | 
92  |  |    * so one subtraction should suffice. */  | 
93  |  | 
  | 
94  | 0  |   *scratch = mpn_sub_n (tp, sp, ecc->q.m, ecc->p.size);  | 
95  | 0  |   cnd_copy (is_zero_limb (*scratch), sp, tp, ecc->p.size);  | 
96  |  | 
  | 
97  | 0  | #undef P  | 
98  | 0  | #undef hp  | 
99  | 0  | #undef tp  | 
100  | 0  | #undef t2p  | 
101  | 0  | }  |