/src/nettle-with-libgmp/umac-poly64.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* umac-poly64.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 "umac-internal.h"  | 
40  |  |  | 
41  |  | static uint64_t  | 
42  |  | poly64_mul (uint32_t kh, uint32_t kl, uint64_t y)  | 
43  | 4.48k  | { | 
44  | 4.48k  |   uint64_t yl, yh, pl, ph, ml, mh;  | 
45  | 4.48k  |   yl = y & 0xffffffff;  | 
46  | 4.48k  |   yh = y >> 32;  | 
47  | 4.48k  |   pl = yl * kl;  | 
48  | 4.48k  |   ph = yh * kh;  | 
49  | 4.48k  |   ml = yh * kl + yl * kh; /* No overflow, thanks to special form */  | 
50  | 4.48k  |   mh = ml >> 32;  | 
51  | 4.48k  |   ml <<= 32;  | 
52  | 4.48k  |   pl += ml;  | 
53  | 4.48k  |   ph += mh + (pl < ml);  | 
54  |  |  | 
55  |  |   /* Reduce, using 2^64 = UMAC_P64_OFFSET (mod p) */  | 
56  | 4.48k  |   assert (ph < ((uint64_t) 1 << 57));  | 
57  | 4.48k  |   ph *= UMAC_P64_OFFSET;  | 
58  | 4.48k  |   pl += ph;  | 
59  | 4.48k  |   if (pl < ph)  | 
60  | 429  |     pl += UMAC_P64_OFFSET;  | 
61  |  |  | 
62  | 4.48k  |   return pl;  | 
63  | 4.48k  | }  | 
64  |  |  | 
65  |  | uint64_t  | 
66  |  | _nettle_umac_poly64 (uint32_t kh, uint32_t kl, uint64_t y, uint64_t m)  | 
67  | 4.46k  | { | 
68  | 4.46k  |   if ( (m >> 32) == 0xffffffff)  | 
69  | 25  |     { | 
70  | 25  |       y = poly64_mul (kh, kl, y);  | 
71  | 25  |       if (y == 0)  | 
72  | 0  |   y = UMAC_P64 - 1;  | 
73  | 25  |       else  | 
74  | 25  |   y--;  | 
75  | 25  |       m -= UMAC_P64_OFFSET;  | 
76  | 25  |     }  | 
77  | 4.46k  |   y = poly64_mul (kh, kl, y);  | 
78  | 4.46k  |   y += m;  | 
79  | 4.46k  |   if (y < m)  | 
80  | 1.51k  |     y += UMAC_P64_OFFSET;  | 
81  |  |  | 
82  | 4.46k  |   return y;  | 
83  | 4.46k  | }  |