cfb8: Fix decrypt path
[gd/nettle] / block-internal.h
1 /* block-internal.h
2
3    Internal implementations of nettle_blockZ-related functions.
4
5    Copyright (C) 2011 Katholieke Universiteit Leuven
6    Copyright (C) 2011, 2013, 2018 Niels Möller
7    Copyright (C) 2018 Red Hat, Inc.
8    Copyright (C) 2019 Dmitry Eremin-Solenikov
9
10    This file is part of GNU Nettle.
11
12    GNU Nettle is free software: you can redistribute it and/or
13    modify it under the terms of either:
14
15      * the GNU Lesser General Public License as published by the Free
16        Software Foundation; either version 3 of the License, or (at your
17        option) any later version.
18
19    or
20
21      * the GNU General Public License as published by the Free
22        Software Foundation; either version 2 of the License, or (at your
23        option) any later version.
24
25    or both in parallel, as here.
26
27    GNU Nettle is distributed in the hope that it will be useful,
28    but WITHOUT ANY WARRANTY; without even the implied warranty of
29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30    General Public License for more details.
31
32    You should have received copies of the GNU General Public License and
33    the GNU Lesser General Public License along with this program.  If
34    not, see http://www.gnu.org/licenses/.
35 */
36
37 #ifndef NETTLE_BLOCK_INTERNAL_H_INCLUDED
38 #define NETTLE_BLOCK_INTERNAL_H_INCLUDED
39
40 #include <assert.h>
41
42 #include "nettle-types.h"
43 #include "memxor.h"
44
45 static inline void
46 block16_xor (union nettle_block16 *r,
47              const union nettle_block16 *x)
48 {
49   r->u64[0] ^= x->u64[0];
50   r->u64[1] ^= x->u64[1];
51 }
52
53 static inline void
54 block16_xor3 (union nettle_block16 *r,
55               const union nettle_block16 *x,
56               const union nettle_block16 *y)
57 {
58   r->u64[0] = x->u64[0] ^ y->u64[0];
59   r->u64[1] = x->u64[1] ^ y->u64[1];
60 }
61
62 static inline void
63 block16_xor_bytes (union nettle_block16 *r,
64                    const union nettle_block16 *x,
65                    const uint8_t *bytes)
66 {
67   memxor3 (r->b, x->b, bytes, 16);
68 }
69
70 static inline void
71 block8_xor (union nettle_block8 *r,
72             const union nettle_block8 *x)
73 {
74   r->u64 ^= x->u64;
75 }
76
77 static inline void
78 block8_xor3 (union nettle_block8 *r,
79              const union nettle_block8 *x,
80              const union nettle_block8 *y)
81 {
82   r->u64 = x->u64 ^ y->u64;
83 }
84
85 static inline void
86 block8_xor_bytes (union nettle_block8 *r,
87                   const union nettle_block8 *x,
88                   const uint8_t *bytes)
89 {
90   memxor3 (r->b, x->b, bytes, 8);
91 }
92
93 /* Do a foreign-endianness shift of data */
94
95 #define LSHIFT_ALIEN_UINT64(x) \
96         ((((x) & UINT64_C(0x7f7f7f7f7f7f7f7f)) << 1) | \
97          (((x) & UINT64_C(0x8080808080808080)) >> 15))
98 #define RSHIFT_ALIEN_UINT64(x) \
99         ((((x) & UINT64_C(0xfefefefefefefefe)) >> 1) | \
100          (((x) & UINT64_C(0x0001010101010101)) << 15))
101
102 /* Two typical defining polynoms */
103
104 #define BLOCK16_POLY (UINT64_C(0x87))
105 #define BLOCK8_POLY (UINT64_C(0x1b))
106 #define GHASH_POLY (UINT64_C(0xE1))
107
108 /* Galois multiplications by 2:
109  * functions differ in shifting right or left, big- or little- endianness
110  * and by defining polynom.
111  * r == x is allowed. */
112
113 #if WORDS_BIGENDIAN
114 static inline void
115 block16_mulx_be (union nettle_block16 *dst,
116                  const union nettle_block16 *src)
117 {
118   uint64_t carry = src->u64[0] >> 63;
119   dst->u64[0] = (src->u64[0] << 1) | (src->u64[1] >> 63);
120   dst->u64[1] = (src->u64[1] << 1) ^ (BLOCK16_POLY & -carry);
121 }
122
123 static inline void
124 block16_mulx_le (union nettle_block16 *dst,
125                  const union nettle_block16 *src)
126 {
127   uint64_t carry = (src->u64[1] & 0x80) >> 7;
128   dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) | ((src->u64[0] & 0x80) << 49);
129   dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) ^ ((BLOCK16_POLY << 56) & -carry);
130 }
131
132 static inline void
133 block8_mulx_be (union nettle_block8 *dst,
134                 const union nettle_block8 *src)
135 {
136   uint64_t carry = src->u64 >> 63;
137
138   dst->u64 = (src->u64 << 1) ^ (BLOCK8_POLY & -carry);
139 }
140
141 static inline void
142 block16_mulx_ghash (union nettle_block16 *r,
143                     const union nettle_block16 *x)
144 {
145   uint64_t mask;
146
147   /* Shift uses big-endian representation. */
148   mask = - (x->u64[1] & 1);
149   r->u64[1] = (x->u64[1] >> 1) | ((x->u64[0] & 1) << 63);
150   r->u64[0] = (x->u64[0] >> 1) ^ (mask & (GHASH_POLY << 56));
151 }
152 #else /* !WORDS_BIGENDIAN */
153 static inline void
154 block16_mulx_be (union nettle_block16 *dst,
155                  const union nettle_block16 *src)
156 {
157   uint64_t carry = (src->u64[0] & 0x80) >> 7;
158   dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
159   dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
160 }
161
162 static inline void
163 block16_mulx_le (union nettle_block16 *dst,
164                  const union nettle_block16 *src)
165 {
166   uint64_t carry = src->u64[1] >> 63;
167   dst->u64[1] = (src->u64[1] << 1) | (src->u64[0] >> 63);
168   dst->u64[0] = (src->u64[0] << 1) ^ (BLOCK16_POLY & -carry);
169 }
170
171 static inline void
172 block8_mulx_be (union nettle_block8 *dst,
173                 const union nettle_block8 *src)
174 {
175   uint64_t carry = (src->u64 & 0x80) >> 7;
176
177   dst->u64 = LSHIFT_ALIEN_UINT64(src->u64) ^ ((BLOCK8_POLY << 56) & -carry);
178 }
179
180 static inline void
181 block16_mulx_ghash (union nettle_block16 *r,
182                     const union nettle_block16 *x)
183 {
184   uint64_t mask;
185
186   /* Shift uses big-endian representation. */
187   mask = - ((x->u64[1] >> 56) & 1);
188   r->u64[1] = RSHIFT_ALIEN_UINT64(x->u64[1]) | ((x->u64[0] >> 49) & 0x80);
189   r->u64[0] = RSHIFT_ALIEN_UINT64(x->u64[0]) ^ (mask & GHASH_POLY);
190 }
191 #endif /* ! WORDS_BIGENDIAN */
192
193 #endif /* NETTLE_BLOCK_INTERNAL_H_INCLUDED */