1 /* umac-l3.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 "umac.h"
37 #include "umac-internal.h"
38
39 #include "macros.h"
40
41 /* 2^36 - 5 */
42 #define P 0x0000000FFFFFFFFBULL
43
44 #if WORDS_BIGENDIAN
45 #define BE_SWAP64(x) x
46 #else
47 #define BE_SWAP64(x) \
48 (((x & 0xff) << 56) \
49 | ((x & 0xff00) << 40) \
50 | ((x & 0xff0000) << 24) \
51 | ((x & 0xff000000) << 8) \
52 | ((x >> 8) & 0xff000000) \
53 | ((x >> 24) & 0xff0000) \
54 | ((x >> 40) & 0xff00) \
55 | (x >> 56) )
56 #endif
57
58 void
_umac_l3_init(unsigned size,uint64_t * k)59 _umac_l3_init (unsigned size, uint64_t *k)
60 {
61 unsigned i;
62 for (i = 0; i < size; i++)
63 {
64 uint64_t w = k[i];
65 w = BE_SWAP64 (w);
66 k[i] = w % P;
67 }
68 }
69
70 static uint64_t
umac_l3_word(const uint64_t * k,uint64_t w)71 umac_l3_word (const uint64_t *k, uint64_t w)
72 {
73 unsigned i;
74 uint64_t y;
75
76 /* Since it's easiest to process the input word from the low end,
77 * loop over keys in reverse order. */
78
79 for (i = y = 0; i < 4; i++, w >>= 16)
80 y += (w & 0xffff) * k[3-i];
81
82 return y;
83 }
84
85 uint32_t
_umac_l3(const uint64_t * key,const uint64_t * m)86 _umac_l3 (const uint64_t *key, const uint64_t *m)
87 {
88 uint32_t y = (umac_l3_word (key, m[0])
89 + umac_l3_word (key + 4, m[1])) % P;
90
91 #if !WORDS_BIGENDIAN
92 y = ((ROTL32(8, y) & 0x00FF00FFUL)
93 | (ROTL32(24, y) & 0xFF00FF00UL));
94 #endif
95 return y;
96 }
97