ab1b392ae997e7a39ad004b360e7615c8ae521f2
[platform/upstream/nettle.git] / umac-nh.c
1 /* umac-nh.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
38 #include "umac.h"
39 #include "macros.h"
40
41 /* For fat builds */
42 #if HAVE_NATIVE_umac_nh
43 uint64_t
44 _nettle_umac_nh_c (const uint32_t *key, unsigned length, const uint8_t *msg);
45 #define _nettle_umac_nh _nettle_umac_nh_c
46 #endif
47
48 uint64_t
49 _umac_nh (const uint32_t *key, unsigned length, const uint8_t *msg)
50 {
51   uint64_t y;
52
53   assert (length > 0);
54   assert (length <= 1024);
55   assert (length % 32 == 0);
56   for (y = 0; length > 0; length -= 32, msg += 32, key += 8)
57     {
58       uint32_t a, b;
59       a = LE_READ_UINT32 (msg)      + key[0];
60       b = LE_READ_UINT32 (msg + 16) + key[4];
61       y += (uint64_t) a * b;
62       a = LE_READ_UINT32 (msg +  4) + key[1];
63       b = LE_READ_UINT32 (msg + 20) + key[5];
64       y += (uint64_t) a * b;
65       a = LE_READ_UINT32 (msg +  8) + key[2];
66       b = LE_READ_UINT32 (msg + 24) + key[6];
67       y += (uint64_t) a * b;
68       a = LE_READ_UINT32 (msg + 12) + key[3];
69       b = LE_READ_UINT32 (msg + 28) + key[7];
70       y += (uint64_t) a * b;
71     }
72
73   return y;
74 }