Coverage Report

Created: 2025-03-18 06:55

/src/nettle/umac-poly128.c
Line
Count
Source (jump to first uncovered line)
1
/* umac-poly128.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
0
#define HI(x) (x >> 32)
42
0
#define LO(x) (x & 0xffffffffUL)
43
44
static void
45
poly128_mul (const uint32_t *k, uint64_t *y)
46
0
{
47
0
  uint64_t y0,y1,y2,y3,p0,p1,p2,p3,m0,m1,m2;
48
0
  y0 = LO (y[1]);
49
0
  y1 = HI (y[1]);
50
0
  y2 = LO (y[0]);
51
0
  y3 = HI (y[0]);
52
53
0
  p0 = y0 * k[3];
54
0
  m0 = y0 * k[2] + y1 * k[3];
55
0
  p1 = y0 * k[1] + y1 * k[2] + y2 * k[3];
56
0
  m1 = y0 * k[0] + y1 * k[1] + y2 * k[2] + y3 * k[3];
57
0
  p2 = y1 * k[0] + y2 * k[1] + y3 * k[2];
58
0
  m2 = y2 * k[0] + y3 * k[1];
59
0
  p3 = y3 * k[0];
60
61
  /* Collaps to 4 64-bit words,
62
     +---+---+---+---+
63
     | p3| p2| p1| p0|
64
     +-+-+-+-+-+-+-+-+
65
    +  | m2| m1| m0|
66
    -+-+-+-+-+-+-+-+-+
67
  */
68
  /* But it's convenient to reduce (p3,p2,p1,p0) and (m2,m1,m0) mod p first.*/
69
0
  m1 += UMAC_P128_OFFSET * HI(p3);
70
0
  p1 += UMAC_P128_OFFSET * (LO(p3) + HI(m2));
71
0
  m0 += UMAC_P128_OFFSET * (HI(p2) + LO(m2));
72
0
  p0 += UMAC_P128_OFFSET * (LO(p2) + HI(m1));
73
74
  /* Left to add
75
     +---+---+
76
     | p1| p0|
77
     +-+-+-+-+
78
     m1| m0|
79
     +-+---+
80
  */
81
  /* First add high parts, with no possibilities for carries */
82
0
  p1 += m0 >> 32;
83
84
0
  m0 <<= 32;
85
0
  m1 <<= 32;
86
87
  /* Remains:
88
     +---+---+
89
     | p1| p0|
90
     +-+-+---+
91
    +| m1| m0|
92
    -+---+---+
93
  */
94
0
  p0 += m0;
95
0
  p1 += (p0 < m0);
96
0
  p1 += m1;
97
0
  if (p1 < m1)
98
0
    {
99
0
      p0 += UMAC_P128_OFFSET;
100
0
      p1 += (p0 < UMAC_P128_OFFSET);
101
0
    }
102
103
0
  y[0] = p1;
104
0
  y[1] = p0;
105
0
}
106
107
void
108
_nettle_umac_poly128 (const uint32_t *k, uint64_t *y, uint64_t mh, uint64_t ml)
109
0
{
110
0
  uint64_t yh, yl, cy;
111
112
0
  if ( (mh >> 32) == 0xffffffff)
113
0
    {
114
0
      poly128_mul (k, y);
115
0
      if (y[1] > 0)
116
0
  y[1]--;
117
0
      else if (y[0] > 0)
118
0
  {
119
0
    y[0]--;
120
0
    y[1] = UMAC_P128_HI;
121
0
  }
122
0
      else
123
0
  {
124
0
    y[0] = UMAC_P128_HI;
125
0
    y[1] = UMAC_P128_LO-1;
126
0
  }
127
128
0
      mh -= (ml < UMAC_P128_OFFSET);
129
0
      ml -= UMAC_P128_OFFSET;
130
0
    }
131
0
  assert (mh < UMAC_P128_HI || ml < UMAC_P128_LO);
132
133
0
  poly128_mul (k, y);
134
0
  yl = y[1] + ml;
135
0
  cy = (yl < ml);
136
0
  yh = y[0] + cy;
137
0
  cy = (yh < cy);
138
0
  yh += mh;
139
0
  cy += (yh < mh);
140
0
  assert (cy <= 1);
141
0
  if (cy)
142
0
    {
143
0
      yl += UMAC_P128_OFFSET;
144
0
      yh += yl < UMAC_P128_OFFSET;
145
0
    }
146
147
0
  y[0] = yh;
148
0
  y[1] = yl;
149
0
}