4 /* nettle, low-level cryptographics library
6 * Copyright (C) 2013 Niels Möller
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.
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.
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,
35 /* Same mask applied to low and high halves */
36 #define KEY_MASK 0x01ffffffUL
39 #define BE_SWAP32(x) x
41 #define BE_SWAP32(x) \
42 ((ROTL32(8, x) & 0x00FF00FFUL) | \
43 (ROTL32(24, x) & 0xFF00FF00UL))
47 _umac_l2_init (unsigned size
, uint32_t *k
)
50 for (i
= 0; i
< size
; i
++)
59 _umac_l2(const uint32_t *key
, uint64_t *state
, unsigned n
,
60 uint64_t count
, const uint64_t *m
)
62 uint64_t *prev
= state
+ 2*n
;
66 memcpy (prev
, m
, n
* sizeof(*m
));
68 for (i
= 0; i
< n
; i
++, key
+= 6)
70 uint64_t y
= _umac_poly64 (key
[0], key
[1], 1, prev
[i
]);
71 state
[2*i
+1] = _umac_poly64 (key
[0], key
[1], y
, m
[i
]);
73 else if (count
< UMAC_POLY64_BLOCKS
)
74 for (i
= 0; i
< n
; i
++, key
+= 6)
75 state
[2*i
+1] = _umac_poly64 (key
[0], key
[1], state
[2*i
+1], m
[i
]);
76 else if (count
% 2 == 0)
78 if (count
== UMAC_POLY64_BLOCKS
)
79 for (i
= 0, key
+= 2; i
< n
; i
++, key
+= 6)
81 uint64_t y
= state
[2*i
+1];
87 _umac_poly128 (key
, state
+ 2*i
, 0, y
);
89 memcpy (prev
, m
, n
* sizeof(*m
));
92 for (i
= 0, key
+= 2; i
< n
; i
++, key
+= 6)
93 _umac_poly128 (key
, state
+ 2*i
, prev
[i
], m
[i
]);
97 _umac_l2_final(const uint32_t *key
, uint64_t *state
, unsigned n
,
100 uint64_t *prev
= state
+ 2*n
;
105 for (i
= 0; i
< n
; i
++)
110 else if (count
<= UMAC_POLY64_BLOCKS
)
111 for (i
= 0; i
< n
; i
++)
123 uint64_t pad
= (uint64_t) 1 << 63;
125 for (i
= 0, key
+= 2; i
< n
; i
++, key
+= 6)
126 _umac_poly128 (key
, state
+ 2*i
, prev
[i
], pad
);
128 for (i
= 0, key
+= 2; i
< n
; i
++, key
+= 6)
129 _umac_poly128 (key
, state
+ 2*i
, pad
, 0);
131 for (i
= 0; i
< n
; i
++, state
+= 2)
137 if (yh
== UMAC_P128_HI
&& yl
>= UMAC_P128_LO
)
140 state
[1] = yl
-= UMAC_P128_LO
;