Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / umac-l3.c
1 /* umac-l3.c
2  */
3
4 /* nettle, low-level cryptographics library
5  *
6  * Copyright (C) 2013 Niels Möller
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #if HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "umac.h"
29
30 #include "macros.h"
31
32 /* 2^36 - 5 */
33 #define P 0x0000000FFFFFFFFBULL
34
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
48
49 void
50 _umac_l3_init (unsigned size, uint64_t *k)
51 {
52   unsigned i;
53   for (i = 0; i < size; i++)
54     {
55       uint64_t w = k[i];
56       w = BE_SWAP64 (w);
57       k[i] = w % P;
58     }
59 }
60
61 static uint64_t
62 umac_l3_word (const uint64_t *k, uint64_t w)
63 {
64   unsigned i;
65   uint64_t y;
66
67   /* Since it's easiest to process the input word from the low end,
68    * loop over keys in reverse order. */
69
70   for (i = y = 0; i < 4; i++, w >>= 16)
71     y += (w & 0xffff) * k[3-i];
72
73   return y;
74 }
75
76 uint32_t
77 _umac_l3 (const uint64_t *key, const uint64_t *m)
78 {
79   uint32_t y = (umac_l3_word (key, m[0])
80                 + umac_l3_word (key + 4, m[1])) % P;
81
82 #if !WORDS_BIGENDIAN
83   y = ((ROTL32(8,  y) & 0x00FF00FFUL)
84        | (ROTL32(24, y) & 0xFF00FF00UL));
85 #endif
86   return y;
87 }