3 Copyright (C) 2013 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/.
32 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
42 #include "ecc-internal.h"
44 /* Low-level ECDSA signing */
47 ecc_ecdsa_sign_itch (const struct ecc_curve *ecc)
49 /* Needs 3*ecc->size + scratch for ecc_mul_g. */
50 return ECC_ECDSA_SIGN_ITCH (ecc->size);
53 /* NOTE: Caller should check if r or s is zero. */
55 ecc_ecdsa_sign (const struct ecc_curve *ecc,
57 /* Random nonce, must be invertible mod ecc group
60 size_t length, const uint8_t *digest,
61 mp_limb_t *rp, mp_limb_t *sp,
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
72 1. k <-- uniformly random, 0 < k < q
74 2. R <-- (r_x, r_y) = k g
78 4. s2 <-- (h + z*s1)/k mod q.
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);
85 /* Invert k, uses 5 * ecc->size including scratch */
86 mpn_copyi (hp, kp, ecc->size);
87 ecc_modq_inv (ecc, kinv, hp, tp);
89 /* Process hash digest */
90 ecc_hash (ecc, hp, length, digest);
92 ecc_modq_mul (ecc, tp, zp, rp);
93 ecc_modq_add (ecc, hp, hp, tp);
94 ecc_modq_mul (ecc, tp, hp, kinv);
96 mpn_copyi (sp, tp, ecc->size);