Coverage Report

Created: 2023-09-25 06:33

/src/nettle-with-libgmp/umac-poly64.c
Line
Count
Source (jump to first uncovered line)
1
/* umac-poly64.c
2
3
   Copyright (C) 2013 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 "umac.h"
39
#include "umac-internal.h"
40
41
static uint64_t
42
poly64_mul (uint32_t kh, uint32_t kl, uint64_t y)
43
1.57k
{
44
1.57k
  uint64_t yl, yh, pl, ph, ml, mh;
45
1.57k
  yl = y & 0xffffffff;
46
1.57k
  yh = y >> 32;
47
1.57k
  pl = yl * kl;
48
1.57k
  ph = yh * kh;
49
1.57k
  ml = yh * kl + yl * kh; /* No overflow, thanks to special form */
50
1.57k
  mh = ml >> 32;
51
1.57k
  ml <<= 32;
52
1.57k
  pl += ml;
53
1.57k
  ph += mh + (pl < ml);
54
55
  /* Reduce, using 2^64 = UMAC_P64_OFFSET (mod p) */
56
1.57k
  assert (ph < ((uint64_t) 1 << 57));
57
1.57k
  ph *= UMAC_P64_OFFSET;
58
1.57k
  pl += ph;
59
1.57k
  if (pl < ph)
60
207
    pl += UMAC_P64_OFFSET;
61
62
1.57k
  return pl;
63
1.57k
}
64
65
uint64_t
66
_nettle_umac_poly64 (uint32_t kh, uint32_t kl, uint64_t y, uint64_t m)
67
1.55k
{
68
1.55k
  if ( (m >> 32) == 0xffffffff)
69
16
    {
70
16
      y = poly64_mul (kh, kl, y);
71
16
      if (y == 0)
72
0
  y = UMAC_P64 - 1;
73
16
      else
74
16
  y--;
75
16
      m -= UMAC_P64_OFFSET;
76
16
    }
77
1.55k
  y = poly64_mul (kh, kl, y);
78
1.55k
  y += m;
79
1.55k
  if (y < m)
80
457
    y += UMAC_P64_OFFSET;
81
82
1.55k
  return y;
83
1.55k
}