Coverage Report

Created: 2024-11-21 07:00

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