Coverage Report

Created: 2026-11-11 07:07

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
319
{
53
957
#define t0 scratch
54
957
#define t1 (scratch + p->size)
55
56
319
  ecc_mod_mul_canonical (p, t0, x1, z2, t0);
57
319
  ecc_mod_mul_canonical (p, t1, x2, z1, t1);
58
59
319
  return mpn_cmp (t0, t1, p->size) == 0;
60
61
319
#undef t0
62
319
#undef t1
63
319
}
64
65
mp_size_t
66
_eddsa_verify_itch (const struct ecc_curve *ecc)
67
437
{
68
437
  assert (_eddsa_decompress_itch (ecc) <= ecc->mul_itch);
69
437
  return 8*ecc->p.size + ecc->mul_itch;
70
437
}
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
437
{
83
437
  size_t nbytes;
84
2.14k
#define R scratch
85
1.10k
#define sp (scratch + 2*ecc->p.size)
86
1.26k
#define hp (scratch + 3*ecc->p.size)
87
2.22k
#define P (scratch + 5*ecc->p.size)
88
1.27k
#define scratch_out (scratch + 8*ecc->p.size)
89
955
#define S R
90
634
#define hash ((uint8_t *) P)
91
92
437
  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
437
  if (!_eddsa_decompress (ecc, R, signature, R+2*ecc->p.size))
97
43
    return 0;
98
99
394
  mpn_set_base256_le (sp, ecc->q.size, signature + nbytes, nbytes);
100
  /* Check that s < q */
101
394
  if (mpn_cmp (sp, ecc->q.m, ecc->q.size) >= 0)
102
77
    return 0;
103
104
317
  eddsa->dom (ctx);
105
317
  eddsa->update (ctx, nbytes, signature);
106
317
  eddsa->update (ctx, nbytes, pub);
107
317
  eddsa->update (ctx, length, msg);
108
317
  eddsa->digest (ctx, 2*nbytes, hash);
109
317
  _eddsa_hash (&ecc->q, hp, 2*nbytes, hash);
110
111
  /* Compute h A + R - s G, which should be the neutral point */
112
317
  ecc->mul (ecc, P, hp, A, scratch_out);
113
317
  ecc->add_hh (ecc, P, P, R, scratch_out);
114
  /* Move out of the way. */
115
317
  mpn_copyi (hp, sp, ecc->q.size);
116
317
  ecc->mul_g (ecc, S, hp, scratch_out);
117
118
317
  return equal_h (&ecc->p,
119
317
       P, P + 2*ecc->p.size,
120
317
       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
394
#undef R
126
394
#undef sp
127
394
#undef hp
128
394
#undef P
129
394
#undef S
130
394
}