3 Copyright (C) 2013 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/.
43 /* Same mask applied to low and high halves */
44 #define KEY_MASK 0x01ffffffUL
47 #define BE_SWAP32(x) x
49 #define BE_SWAP32(x) \
50 ((ROTL32(8, x) & 0x00FF00FFUL) | \
51 (ROTL32(24, x) & 0xFF00FF00UL))
55 _umac_l2_init (unsigned size, uint32_t *k)
58 for (i = 0; i < size; i++)
67 _umac_l2(const uint32_t *key, uint64_t *state, unsigned n,
68 uint64_t count, const uint64_t *m)
70 uint64_t *prev = state + 2*n;
74 memcpy (prev, m, n * sizeof(*m));
76 for (i = 0; i < n; i++, key += 6)
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]);
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)
86 if (count == UMAC_POLY64_BLOCKS)
87 for (i = 0, key += 2; i < n; i++, key += 6)
89 uint64_t y = state[2*i+1];
95 _umac_poly128 (key, state + 2*i, 0, y);
97 memcpy (prev, m, n * sizeof(*m));
100 for (i = 0, key += 2; i < n; i++, key += 6)
101 _umac_poly128 (key, state + 2*i, prev[i], m[i]);
105 _umac_l2_final(const uint32_t *key, uint64_t *state, unsigned n,
108 uint64_t *prev = state + 2*n;
113 for (i = 0; i < n; i++)
118 else if (count <= UMAC_POLY64_BLOCKS)
119 for (i = 0; i < n; i++)
131 uint64_t pad = (uint64_t) 1 << 63;
133 for (i = 0, key += 2; i < n; i++, key += 6)
134 _umac_poly128 (key, state + 2*i, prev[i], pad);
136 for (i = 0, key += 2; i < n; i++, key += 6)
137 _umac_poly128 (key, state + 2*i, pad, 0);
139 for (i = 0; i < n; i++, state += 2)
145 if (yh == UMAC_P128_HI && yl >= UMAC_P128_LO)
148 state[1] = yl -= UMAC_P128_LO;