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