ecc_j_to_a interface change, optionally reduce x mod q.
[gd/nettle] / ecc-ecdsa-sign.c
1 /* ecc-ecdsa-sign.c
2
3    Copyright (C) 2013 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
44 /* Low-level ECDSA signing */
45
46 mp_size_t
47 ecc_ecdsa_sign_itch (const struct ecc_curve *ecc)
48 {
49   /* Needs 3*ecc->size + scratch for ecc_mul_g. */
50   return ECC_ECDSA_SIGN_ITCH (ecc->size);
51 }
52
53 /* NOTE: Caller should check if r or s is zero. */
54 void
55 ecc_ecdsa_sign (const struct ecc_curve *ecc,
56                 const mp_limb_t *zp,
57                 /* Random nonce, must be invertible mod ecc group
58                    order. */
59                 const mp_limb_t *kp,
60                 size_t length, const uint8_t *digest,
61                 mp_limb_t *rp, mp_limb_t *sp,
62                 mp_limb_t *scratch)
63 {
64   mp_limb_t cy;
65 #define P           scratch
66 #define kinv        scratch                /* Needs 5*ecc->size for computation */
67 #define hp          (scratch  + ecc->size) /* NOTE: ecc->size + 1 limbs! */
68 #define tp          (scratch + 2*ecc->size)
69   /* Procedure, according to RFC 6090, "KT-I". q denotes the group
70      order.
71
72      1. k <-- uniformly random, 0 < k < q
73
74      2. R <-- (r_x, r_y) = k g
75
76      3. s1 <-- r_x mod q
77
78      4. s2 <-- (h + z*s1)/k mod q.
79   */
80
81   ecc_mul_g (ecc, P, kp, P + 3*ecc->size);
82   /* x coordinate only, modulo q */
83   ecc_j_to_a (ecc, 2, rp, P, P + 3*ecc->size);
84
85   /* Invert k, uses 5 * ecc->size including scratch */
86   mpn_copyi (hp, kp, ecc->size);
87   ecc_modq_inv (ecc, kinv, hp, tp);
88   
89   /* Process hash digest */
90   ecc_hash (ecc, hp, length, digest);
91
92   ecc_modq_mul (ecc, tp, zp, rp);
93   ecc_modq_add (ecc, hp, hp, tp);
94   ecc_modq_mul (ecc, tp, hp, kinv);
95
96   mpn_copyi (sp, tp, ecc->size);
97 #undef P
98 #undef hp
99 #undef kinv
100 #undef tp
101 }