Coverage Report

Created: 2026-10-28 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nettle/eddsa-verify.c
Line
Count
Source
1
/* eddsa-verify.c
2
3
   Copyright (C) 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
#if HAVE_CONFIG_H
33
# include "config.h"
34
#endif
35
36
#include <assert.h>
37
38
#include "eddsa.h"
39
#include "eddsa-internal.h"
40
41
#include "ecc.h"
42
#include "ecc-internal.h"
43
#include "nettle-meta.h"
44
45
/* Checks if x1/z1 == x2/z2 (mod p). Assumes z1 and z2 are
46
   non-zero. */
47
static int
48
equal_h (const struct ecc_modulo *p,
49
   const mp_limb_t *x1, const mp_limb_t *z1,
50
   const mp_limb_t *x2, const mp_limb_t *z2,
51
   mp_limb_t *scratch)
52
299
{
53
897
#define t0 scratch
54
897
#define t1 (scratch + p->size)
55
56
299
  ecc_mod_mul_canonical (p, t0, x1, z2, t0);
57
299
  ecc_mod_mul_canonical (p, t1, x2, z1, t1);
58
59
299
  return mpn_cmp (t0, t1, p->size) == 0;
60
61
299
#undef t0
62
299
#undef t1
63
299
}
64
65
mp_size_t
66
_eddsa_verify_itch (const struct ecc_curve *ecc)
67
393
{
68
393
  assert (_eddsa_decompress_itch (ecc) <= ecc->mul_itch);
69
393
  return 8*ecc->p.size + ecc->mul_itch;
70
393
}
71
72
int
73
_eddsa_verify (const struct ecc_curve *ecc,
74
         const struct ecc_eddsa *eddsa,
75
         const uint8_t *pub,
76
         const mp_limb_t *A,
77
         void *ctx,
78
         size_t length,
79
         const uint8_t *msg,
80
         const uint8_t *signature,
81
         mp_limb_t *scratch)
82
393
{
83
393
  size_t nbytes;
84
1.97k
#define R scratch
85
1.01k
#define sp (scratch + 2*ecc->p.size)
86
1.18k
#define hp (scratch + 3*ecc->p.size)
87
2.08k
#define P (scratch + 5*ecc->p.size)
88
1.19k
#define scratch_out (scratch + 8*ecc->p.size)
89
895
#define S R
90
594
#define hash ((uint8_t *) P)
91
92
393
  nbytes = 1 + ecc->p.bit_size / 8;
93
94
  /* Could maybe save some storage by delaying the R and S operations,
95
     but it makes sense to check them for validity up front. */
96
393
  if (!_eddsa_decompress (ecc, R, signature, R+2*ecc->p.size))
97
32
    return 0;
98
99
361
  mpn_set_base256_le (sp, ecc->q.size, signature + nbytes, nbytes);
100
  /* Check that s < q */
101
361
  if (mpn_cmp (sp, ecc->q.m, ecc->q.size) >= 0)
102
64
    return 0;
103
104
297
  eddsa->dom (ctx);
105
297
  eddsa->update (ctx, nbytes, signature);
106
297
  eddsa->update (ctx, nbytes, pub);
107
297
  eddsa->update (ctx, length, msg);
108
297
  eddsa->digest (ctx, 2*nbytes, hash);
109
297
  _eddsa_hash (&ecc->q, hp, 2*nbytes, hash);
110
111
  /* Compute h A + R - s G, which should be the neutral point */
112
297
  ecc->mul (ecc, P, hp, A, scratch_out);
113
297
  ecc->add_hh (ecc, P, P, R, scratch_out);
114
  /* Move out of the way. */
115
297
  mpn_copyi (hp, sp, ecc->q.size);
116
297
  ecc->mul_g (ecc, S, hp, scratch_out);
117
118
297
  return equal_h (&ecc->p,
119
297
       P, P + 2*ecc->p.size,
120
297
       S, S + 2*ecc->p.size, scratch_out)
121
2
    && equal_h (&ecc->p,
122
2
    P + ecc->p.size, P + 2*ecc->p.size,
123
2
    S + ecc->p.size, S + 2*ecc->p.size, scratch_out);
124
125
361
#undef R
126
361
#undef sp
127
361
#undef hp
128
361
#undef P
129
361
#undef S
130
361
}