Coverage Report

Created: 2023-02-22 06:14

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