Add clean sources of nettle. 2.7.1 for DNSSEC.
[tomato.git] / release / src / router / nettle / umac-l3.c
blob3a896e504386fff78c5abfee42dda9a7385bdc49
1 /* umac-l3.c
2 */
4 /* nettle, low-level cryptographics library
6 * Copyright (C) 2013 Niels Möller
8 * The nettle library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or (at your
11 * option) any later version.
13 * The nettle library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with the nettle library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02111-1301, USA.
24 #if HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include "umac.h"
30 #include "macros.h"
32 /* 2^36 - 5 */
33 #define P 0x0000000FFFFFFFFBULL
35 #if WORDS_BIGENDIAN
36 #define BE_SWAP64(x) x
37 #else
38 #define BE_SWAP64(x) \
39 (((x & 0xff) << 56) \
40 | ((x & 0xff00) << 40) \
41 | ((x & 0xff0000) << 24) \
42 | ((x & 0xff000000) << 8) \
43 | ((x >> 8) & 0xff000000) \
44 | ((x >> 24) & 0xff0000) \
45 | ((x >> 40) & 0xff00) \
46 | (x >> 56) )
47 #endif
49 void
50 _umac_l3_init (unsigned size, uint64_t *k)
52 unsigned i;
53 for (i = 0; i < size; i++)
55 uint64_t w = k[i];
56 w = BE_SWAP64 (w);
57 k[i] = w % P;
61 static uint64_t
62 umac_l3_word (const uint64_t *k, uint64_t w)
64 unsigned i;
65 uint64_t y;
67 /* Since it's easiest to process the input word from the low end,
68 * loop over keys in reverse order. */
70 for (i = y = 0; i < 4; i++, w >>= 16)
71 y += (w & 0xffff) * k[3-i];
73 return y;
76 uint32_t
77 _umac_l3 (const uint64_t *key, const uint64_t *m)
79 uint32_t y = (umac_l3_word (key, m[0])
80 + umac_l3_word (key + 4, m[1])) % P;
82 #if !WORDS_BIGENDIAN
83 y = ((ROTL32(8, y) & 0x00FF00FFUL)
84 | (ROTL32(24, y) & 0xFF00FF00UL));
85 #endif
86 return y;