Coverage Report

Created: 2023-09-25 06:33

/src/nettle-with-libgmp/umac-l2.c
Line
Count
Source (jump to first uncovered line)
1
/* umac-l2.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
#include <string.h>
38
39
#include "umac.h"
40
#include "umac-internal.h"
41
42
#include "macros.h"
43
44
/* Same mask applied to low and high halves */
45
6.02k
#define KEY_MASK 0x01ffffffUL
46
47
#if WORDS_BIGENDIAN
48
#define BE_SWAP32(x) x
49
#else
50
#define BE_SWAP32(x)        \
51
6.02k
  ((ROTL32(8,  x) & 0x00FF00FFUL) |   \
52
6.02k
   (ROTL32(24, x) & 0xFF00FF00UL))
53
#endif
54
55
void
56
_nettle_umac_l2_init (unsigned size, uint32_t *k)
57
368
{
58
368
  unsigned i;
59
6.39k
  for (i = 0; i < size; i++)
60
6.02k
    {
61
6.02k
      uint32_t w = k[i];
62
6.02k
      w = BE_SWAP32 (w);
63
6.02k
      k[i] = w & KEY_MASK;
64
6.02k
    }
65
368
}
66
67
void
68
_nettle_umac_l2(const uint32_t *key, uint64_t *state, unsigned n,
69
    uint64_t count, const uint64_t *m)
70
740
{
71
740
  uint64_t *prev = state + 2*n;
72
740
  unsigned i;
73
74
740
  if (count == 0)
75
368
    memcpy (prev, m, n * sizeof(*m));
76
372
  else if (count == 1)
77
689
    for (i = 0; i < n; i++, key += 6)
78
524
      {
79
524
  uint64_t y = _nettle_umac_poly64 (key[0], key[1], 1, prev[i]);
80
524
  state[2*i+1] = _nettle_umac_poly64 (key[0], key[1], y, m[i]);
81
524
      }
82
207
  else if (count < UMAC_POLY64_BLOCKS)
83
714
    for (i = 0; i < n; i++, key += 6)
84
507
      state[2*i+1] = _nettle_umac_poly64 (key[0], key[1], state[2*i+1], m[i]);
85
0
  else if (count % 2 == 0)
86
0
    {
87
0
      if (count == UMAC_POLY64_BLOCKS)
88
0
  for (i = 0, key += 2; i < n; i++, key += 6)
89
0
    {
90
0
      uint64_t y = state[2*i+1];
91
0
      if (y >= UMAC_P64)
92
0
        y -= UMAC_P64;
93
0
      state[2*i] = 0;
94
0
      state[2*i+1] = 1;
95
96
0
      _nettle_umac_poly128 (key, state + 2*i, 0, y);
97
0
    }
98
0
      memcpy (prev, m, n * sizeof(*m));
99
0
    }
100
0
  else
101
0
    for (i = 0, key += 2; i < n; i++, key += 6)
102
0
      _nettle_umac_poly128 (key, state + 2*i, prev[i], m[i]);
103
740
}
104
105
void
106
_nettle_umac_l2_final(const uint32_t *key, uint64_t *state, unsigned n,
107
          uint64_t count)
108
368
{
109
368
  uint64_t *prev = state + 2*n;
110
368
  unsigned i;
111
112
368
  assert (count > 0);
113
368
  if (count == 1)
114
683
    for (i = 0; i < n; i++)
115
480
      {
116
480
  *state++ = 0;
117
480
  *state++ = *prev++;
118
480
      }
119
165
  else if (count <= UMAC_POLY64_BLOCKS)
120
689
    for (i = 0; i < n; i++)
121
524
      {
122
524
  uint64_t y;
123
524
  *state++ = 0;
124
125
524
  y = *state;
126
524
  if (y >= UMAC_P64)
127
0
    y -= UMAC_P64;
128
524
  *state++ = y;
129
524
      }
130
0
  else
131
0
    {
132
0
      uint64_t pad = (uint64_t) 1 << 63;
133
0
      if (count % 2 == 1)
134
0
  for (i = 0, key += 2; i < n; i++, key += 6)
135
0
    _nettle_umac_poly128 (key, state + 2*i, prev[i], pad);
136
0
      else
137
0
  for (i = 0, key += 2; i < n; i++, key += 6)
138
0
    _nettle_umac_poly128 (key, state + 2*i, pad, 0);
139
140
0
      for (i = 0; i < n; i++, state += 2)
141
0
  {
142
0
    uint64_t yh, yl;
143
144
0
    yh = state[0];
145
0
    yl = state[1];
146
0
    if (yh == UMAC_P128_HI && yl >= UMAC_P128_LO)
147
0
      {
148
0
        state[0] = 0;
149
0
        state[1] = yl -= UMAC_P128_LO;
150
0
      }
151
0
  }
152
0
    }
153
368
}