Coverage Report

Created: 2023-02-22 06:14

/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
6.00k
{
44
6.00k
  uint64_t yl, yh, pl, ph, ml, mh;
45
6.00k
  yl = y & 0xffffffff;
46
6.00k
  yh = y >> 32;
47
6.00k
  pl = yl * kl;
48
6.00k
  ph = yh * kh;
49
6.00k
  ml = yh * kl + yl * kh; /* No overflow, thanks to special form */
50
6.00k
  mh = ml >> 32;
51
6.00k
  ml <<= 32;
52
6.00k
  pl += ml;
53
6.00k
  ph += mh + (pl < ml);
54
55
  /* Reduce, using 2^64 = UMAC_P64_OFFSET (mod p) */
56
6.00k
  assert (ph < ((uint64_t) 1 << 57));
57
6.00k
  ph *= UMAC_P64_OFFSET;
58
6.00k
  pl += ph;
59
6.00k
  if (pl < ph)
60
512
    pl += UMAC_P64_OFFSET;
61
62
6.00k
  return pl;
63
6.00k
}
64
65
uint64_t
66
_nettle_umac_poly64 (uint32_t kh, uint32_t kl, uint64_t y, uint64_t m)
67
5.90k
{
68
5.90k
  if ( (m >> 32) == 0xffffffff)
69
97
    {
70
97
      y = poly64_mul (kh, kl, y);
71
97
      if (y == 0)
72
0
  y = UMAC_P64 - 1;
73
97
      else
74
97
  y--;
75
97
      m -= UMAC_P64_OFFSET;
76
97
    }
77
5.90k
  y = poly64_mul (kh, kl, y);
78
5.90k
  y += m;
79
5.90k
  if (y < m)
80
2.01k
    y += UMAC_P64_OFFSET;
81
82
5.90k
  return y;
83
5.90k
}