/src/nettle-with-libgmp/umac-l2.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* umac-l2.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  |  | #include <string.h>  | 
38  |  |  | 
39  |  | #include "umac.h"  | 
40  |  | #include "umac-internal.h"  | 
41  |  |  | 
42  |  | #include "bswap-internal.h"  | 
43  |  |  | 
44  |  | /* Same mask applied to low and high halves */  | 
45  | 38.6k  | #define KEY_MASK 0x01ffffffUL  | 
46  |  |  | 
47  |  | void  | 
48  |  | _nettle_umac_l2_init (unsigned size, uint32_t *k)  | 
49  | 2.57k  | { | 
50  | 2.57k  |   unsigned i;  | 
51  | 41.1k  |   for (i = 0; i < size; i++)  | 
52  | 38.6k  |     { | 
53  | 38.6k  |       uint32_t w = k[i];  | 
54  | 38.6k  |       w = bswap32_if_le (w);  | 
55  | 38.6k  |       k[i] = w & KEY_MASK;  | 
56  | 38.6k  |     }  | 
57  | 2.57k  | }  | 
58  |  |  | 
59  |  | void  | 
60  |  | _nettle_umac_l2(const uint32_t *key, uint64_t *state, unsigned n,  | 
61  |  |     uint64_t count, const uint64_t *m)  | 
62  | 4.53k  | { | 
63  | 4.53k  |   uint64_t *prev = state + 2*n;  | 
64  | 4.53k  |   unsigned i;  | 
65  |  |  | 
66  | 4.53k  |   if (count == 0)  | 
67  | 2.57k  |     memcpy (prev, m, n * sizeof(*m));  | 
68  | 1.95k  |   else if (count == 1)  | 
69  | 3.71k  |     for (i = 0; i < n; i++, key += 6)  | 
70  | 2.75k  |       { | 
71  | 2.75k  |   uint64_t y = _nettle_umac_poly64 (key[0], key[1], 1, prev[i]);  | 
72  | 2.75k  |   state[2*i+1] = _nettle_umac_poly64 (key[0], key[1], y, m[i]);  | 
73  | 2.75k  |       }  | 
74  | 999  |   else if (count < UMAC_POLY64_BLOCKS)  | 
75  | 3.76k  |     for (i = 0; i < n; i++, key += 6)  | 
76  | 2.76k  |       state[2*i+1] = _nettle_umac_poly64 (key[0], key[1], state[2*i+1], m[i]);  | 
77  | 0  |   else if (count % 2 == 0)  | 
78  | 0  |     { | 
79  | 0  |       if (count == UMAC_POLY64_BLOCKS)  | 
80  | 0  |   for (i = 0, key += 2; i < n; i++, key += 6)  | 
81  | 0  |     { | 
82  | 0  |       uint64_t y = state[2*i+1];  | 
83  | 0  |       if (y >= UMAC_P64)  | 
84  | 0  |         y -= UMAC_P64;  | 
85  | 0  |       state[2*i] = 0;  | 
86  | 0  |       state[2*i+1] = 1;  | 
87  |  | 
  | 
88  | 0  |       _nettle_umac_poly128 (key, state + 2*i, 0, y);  | 
89  | 0  |     }  | 
90  | 0  |       memcpy (prev, m, n * sizeof(*m));  | 
91  | 0  |     }  | 
92  | 0  |   else  | 
93  | 0  |     for (i = 0, key += 2; i < n; i++, key += 6)  | 
94  | 0  |       _nettle_umac_poly128 (key, state + 2*i, prev[i], m[i]);  | 
95  | 4.53k  | }  | 
96  |  |  | 
97  |  | void  | 
98  |  | _nettle_umac_l2_final(const uint32_t *key, uint64_t *state, unsigned n,  | 
99  |  |           uint64_t count)  | 
100  | 2.57k  | { | 
101  | 2.57k  |   uint64_t *prev = state + 2*n;  | 
102  | 2.57k  |   unsigned i;  | 
103  |  |  | 
104  | 2.57k  |   assert (count > 0);  | 
105  | 2.57k  |   if (count == 1)  | 
106  | 5.30k  |     for (i = 0; i < n; i++)  | 
107  | 3.68k  |       { | 
108  | 3.68k  |   *state++ = 0;  | 
109  | 3.68k  |   *state++ = *prev++;  | 
110  | 3.68k  |       }  | 
111  | 956  |   else if (count <= UMAC_POLY64_BLOCKS)  | 
112  | 3.71k  |     for (i = 0; i < n; i++)  | 
113  | 2.75k  |       { | 
114  | 2.75k  |   uint64_t y;  | 
115  | 2.75k  |   *state++ = 0;  | 
116  |  |  | 
117  | 2.75k  |   y = *state;  | 
118  | 2.75k  |   if (y >= UMAC_P64)  | 
119  | 0  |     y -= UMAC_P64;  | 
120  | 2.75k  |   *state++ = y;  | 
121  | 2.75k  |       }  | 
122  | 0  |   else  | 
123  | 0  |     { | 
124  | 0  |       uint64_t pad = (uint64_t) 1 << 63;  | 
125  | 0  |       if (count % 2 == 1)  | 
126  | 0  |   for (i = 0, key += 2; i < n; i++, key += 6)  | 
127  | 0  |     _nettle_umac_poly128 (key, state + 2*i, prev[i], pad);  | 
128  | 0  |       else  | 
129  | 0  |   for (i = 0, key += 2; i < n; i++, key += 6)  | 
130  | 0  |     _nettle_umac_poly128 (key, state + 2*i, pad, 0);  | 
131  |  | 
  | 
132  | 0  |       for (i = 0; i < n; i++, state += 2)  | 
133  | 0  |   { | 
134  | 0  |     uint64_t yh, yl;  | 
135  |  | 
  | 
136  | 0  |     yh = state[0];  | 
137  | 0  |     yl = state[1];  | 
138  | 0  |     if (yh == UMAC_P128_HI && yl >= UMAC_P128_LO)  | 
139  | 0  |       { | 
140  | 0  |         state[0] = 0;  | 
141  | 0  |         state[1] = yl - UMAC_P128_LO;  | 
142  | 0  |       }  | 
143  | 0  |   }  | 
144  | 0  |     }  | 
145  | 2.57k  | }  |