Coverage Report

Created: 2023-03-26 08:33

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