Coverage Report

Created: 2023-03-26 08:33

/src/nettle/eddsa-hash.c
Line
Count
Source (jump to first uncovered line)
1
/* eddsa-hash.c
2
3
   Copyright (C) 2014, 2019 Niels Möller
4
   Copyright (C) 2017 Daiki Ueno
5
   Copyright (C) 2017 Red Hat, Inc.
6
7
   This file is part of GNU Nettle.
8
9
   GNU Nettle is free software: you can redistribute it and/or
10
   modify it under the terms of either:
11
12
     * the GNU Lesser General Public License as published by the Free
13
       Software Foundation; either version 3 of the License, or (at your
14
       option) any later version.
15
16
   or
17
18
     * the GNU General Public License as published by the Free
19
       Software Foundation; either version 2 of the License, or (at your
20
       option) any later version.
21
22
   or both in parallel, as here.
23
24
   GNU Nettle is distributed in the hope that it will be useful,
25
   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
   General Public License for more details.
28
29
   You should have received copies of the GNU General Public License and
30
   the GNU Lesser General Public License along with this program.  If
31
   not, see http://www.gnu.org/licenses/.
32
*/
33
34
#if HAVE_CONFIG_H
35
# include "config.h"
36
#endif
37
38
#include <assert.h>
39
40
#include "eddsa.h"
41
#include "eddsa-internal.h"
42
43
#include "ecc.h"
44
#include "ecc-internal.h"
45
#include "nettle-internal.h"
46
47
/* Convert hash digest to integer, and reduce canonically modulo q.
48
   Needs space for 2*m->size + 1 at rp. */
49
void
50
_eddsa_hash (const struct ecc_modulo *m,
51
       mp_limb_t *rp, size_t digest_size, const uint8_t *digest)
52
0
{
53
0
  mp_size_t nlimbs = (8*digest_size + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;
54
0
  mp_limb_t cy;
55
56
0
  mpn_set_base256_le (rp, nlimbs, digest, digest_size);
57
58
0
  if (nlimbs > 2*m->size)
59
0
    {
60
      /* Special case for Ed448: reduce rp to 2*m->size limbs.
61
   After decoding rp from a hash of size 2*rn:
62
63
   rp = r2 || r1 || r0
64
65
   where r0 and r1 have m->size limbs.  Reduce this to:
66
67
   rp = r1' || r0
68
69
   where r1' has m->size limbs.  */
70
0
      mp_limb_t hi = rp[2*m->size];
71
0
      assert (nlimbs == 2*m->size + 1);
72
73
0
      hi = mpn_addmul_1 (rp + m->size, m->B, m->size, hi);
74
0
      assert (hi <= 1);
75
0
      hi = mpn_cnd_add_n (hi, rp + m->size, rp + m->size, m->B, m->size);
76
0
      assert (hi == 0);
77
0
    }
78
0
  m->mod (m, rp + m->size , rp);
79
  /* Ensure canonical reduction. */
80
0
  cy = mpn_sub_n (rp, rp + m->size, m->m, m->size);
81
0
  cnd_copy (cy, rp, rp + m->size, m->size);
82
0
}