Line | Count | Source (jump to first uncovered line) |
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 | 0 | #define P 0x0000000FFFFFFFFBULL |
43 | | |
44 | | #if WORDS_BIGENDIAN |
45 | | #define BE_SWAP64(x) x |
46 | | #else |
47 | | #define BE_SWAP64(x) \ |
48 | 0 | (((x & 0xff) << 56) \ |
49 | 0 | | ((x & 0xff00) << 40) \ |
50 | 0 | | ((x & 0xff0000) << 24) \ |
51 | 0 | | ((x & 0xff000000) << 8) \ |
52 | 0 | | ((x >> 8) & 0xff000000) \ |
53 | 0 | | ((x >> 24) & 0xff0000) \ |
54 | 0 | | ((x >> 40) & 0xff00) \ |
55 | 0 | | (x >> 56) ) |
56 | | #endif |
57 | | |
58 | | void |
59 | | _nettle_umac_l3_init (unsigned size, uint64_t *k) |
60 | 0 | { |
61 | 0 | unsigned i; |
62 | 0 | for (i = 0; i < size; i++) |
63 | 0 | { |
64 | 0 | uint64_t w = k[i]; |
65 | 0 | w = BE_SWAP64 (w); |
66 | 0 | k[i] = w % P; |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | static uint64_t |
71 | | umac_l3_word (const uint64_t *k, uint64_t w) |
72 | 0 | { |
73 | 0 | unsigned i; |
74 | 0 | 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 | 0 | for (i = y = 0; i < 4; i++, w >>= 16) |
80 | 0 | y += (w & 0xffff) * k[3-i]; |
81 | |
|
82 | 0 | return y; |
83 | 0 | } |
84 | | |
85 | | uint32_t |
86 | | _nettle_umac_l3 (const uint64_t *key, const uint64_t *m) |
87 | 0 | { |
88 | 0 | uint32_t y = (umac_l3_word (key, m[0]) |
89 | 0 | + umac_l3_word (key + 4, m[1])) % P; |
90 | |
|
91 | 0 | #if !WORDS_BIGENDIAN |
92 | 0 | y = ((ROTL32(8, y) & 0x00FF00FFUL) |
93 | 0 | | (ROTL32(24, y) & 0xFF00FF00UL)); |
94 | 0 | #endif |
95 | 0 | return y; |
96 | 0 | } |