Coverage Report

Created: 2026-10-28 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nettle/eddsa-decompress.c
Line
Count
Source
1
/* eddsa-decompress.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-internal.h"
42
#include "gmp-glue.h"
43
44
mp_size_t
45
_eddsa_decompress_itch (const struct ecc_curve *ecc)
46
393
{
47
393
  return 4*ecc->p.size + ecc->p.sqrt_ratio_itch;
48
393
}
49
50
int
51
_eddsa_decompress (const struct ecc_curve *ecc, mp_limb_t *p,
52
       const uint8_t *cp,
53
       mp_limb_t *scratch)
54
786
{
55
786
  mp_limb_t sign, cy;
56
786
  mp_size_t nlimbs;
57
786
  size_t nbytes;
58
786
  int res;
59
60
4.71k
#define xp p
61
1.57k
#define yp (p + ecc->p.size)
62
63
3.14k
#define y2 scratch
64
3.93k
#define vp (scratch + ecc->p.size)
65
1.57k
#define up scratch
66
4.71k
#define tp (scratch + 2*ecc->p.size)
67
786
#define scratch_out (scratch + 4*ecc->p.size)
68
69
786
  nbytes = 1 + ecc->p.bit_size / 8;
70
  /* By RFC 8032, sign bit is always the most significant bit of the
71
     last byte. */
72
786
  sign = cp[nbytes-1] >> 7;
73
74
  /* May need an extra limb. */
75
786
  nlimbs = (nbytes * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;
76
786
  assert (nlimbs <= ecc->p.size + 1);
77
786
  mpn_set_base256_le (scratch, nlimbs, cp, nbytes);
78
79
  /* Clear out the sign bit */
80
786
  scratch[nlimbs - 1] &=
81
786
    ((mp_limb_t) 1 << ((nbytes * 8 - 1) % GMP_NUMB_BITS)) - 1;
82
786
  mpn_copyi (yp, scratch, ecc->p.size);
83
84
  /* Check range. */
85
786
  if (nlimbs > ecc->p.size)
86
172
    res = (scratch[nlimbs - 1] == 0);
87
614
  else
88
614
    res = 1;
89
90
  /* For a valid input, y < p, so subtraction should underflow. */
91
786
  res &= mpn_sub_n (scratch, scratch, ecc->p.m, ecc->p.size);
92
93
786
  ecc_mod_sqr (&ecc->p, y2, yp, y2);
94
786
  ecc_mod_mul (&ecc->p, vp, y2, ecc->b, vp);
95
786
  ecc_mod_sub (&ecc->p, vp, vp, ecc->unit);
96
  /* The sign is different between curve25519 and curve448.  */
97
786
  if (ecc->p.bit_size == 255)
98
614
    ecc_mod_sub (&ecc->p, up, ecc->unit, y2);
99
172
  else
100
172
    ecc_mod_sub (&ecc->p, up, y2, ecc->unit);
101
786
  res &= ecc->p.sqrt_ratio (&ecc->p, tp, up, vp, scratch_out);
102
103
786
  cy = mpn_sub_n (xp, tp, ecc->p.m, ecc->p.size);
104
786
  cnd_copy (cy, xp, tp, ecc->p.size);
105
786
  sign ^= xp[0] & 1;
106
786
  mpn_sub_n (tp, ecc->p.m, xp, ecc->p.size);
107
786
  cnd_copy (sign, xp, tp, ecc->p.size);
108
  /* Fails if the square root is zero but (original) sign was 1 */
109
786
  res &= mpn_sub_n (tp, xp, ecc->p.m, ecc->p.size);
110
786
  return res;
111
786
}