3 Internal implementations of nettle_blockZ-related functions.
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
10 This file is part of GNU Nettle.
12 GNU Nettle is free software: you can redistribute it and/or
13 modify it under the terms of either:
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.
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.
25 or both in parallel, as here.
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.
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/.
37 #ifndef NETTLE_BLOCK_INTERNAL_H_INCLUDED
38 #define NETTLE_BLOCK_INTERNAL_H_INCLUDED
42 #include "nettle-types.h"
46 block16_xor (union nettle_block16 *r,
47 const union nettle_block16 *x)
49 r->u64[0] ^= x->u64[0];
50 r->u64[1] ^= x->u64[1];
54 block16_xor3 (union nettle_block16 *r,
55 const union nettle_block16 *x,
56 const union nettle_block16 *y)
58 r->u64[0] = x->u64[0] ^ y->u64[0];
59 r->u64[1] = x->u64[1] ^ y->u64[1];
63 block16_xor_bytes (union nettle_block16 *r,
64 const union nettle_block16 *x,
67 memxor3 (r->b, x->b, bytes, 16);
71 block8_xor (union nettle_block8 *r,
72 const union nettle_block8 *x)
78 block8_xor3 (union nettle_block8 *r,
79 const union nettle_block8 *x,
80 const union nettle_block8 *y)
82 r->u64 = x->u64 ^ y->u64;
86 block8_xor_bytes (union nettle_block8 *r,
87 const union nettle_block8 *x,
90 memxor3 (r->b, x->b, bytes, 8);
93 /* Do a foreign-endianness shift of data */
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))
102 /* Two typical defining polynoms */
104 #define BLOCK16_POLY (UINT64_C(0x87))
105 #define BLOCK8_POLY (UINT64_C(0x1b))
106 #define GHASH_POLY (UINT64_C(0xE1))
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. */
115 block16_mulx_be (union nettle_block16 *dst,
116 const union nettle_block16 *src)
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);
124 block16_mulx_le (union nettle_block16 *dst,
125 const union nettle_block16 *src)
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);
133 block8_mulx_be (union nettle_block8 *dst,
134 const union nettle_block8 *src)
136 uint64_t carry = src->u64 >> 63;
138 dst->u64 = (src->u64 << 1) ^ (BLOCK8_POLY & -carry);
142 block16_mulx_ghash (union nettle_block16 *r,
143 const union nettle_block16 *x)
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));
152 #else /* !WORDS_BIGENDIAN */
154 block16_mulx_be (union nettle_block16 *dst,
155 const union nettle_block16 *src)
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);
163 block16_mulx_le (union nettle_block16 *dst,
164 const union nettle_block16 *src)
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);
172 block8_mulx_be (union nettle_block8 *dst,
173 const union nettle_block8 *src)
175 uint64_t carry = (src->u64 & 0x80) >> 7;
177 dst->u64 = LSHIFT_ALIEN_UINT64(src->u64) ^ ((BLOCK8_POLY << 56) & -carry);
181 block16_mulx_ghash (union nettle_block16 *r,
182 const union nettle_block16 *x)
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);
191 #endif /* ! WORDS_BIGENDIAN */
193 #endif /* NETTLE_BLOCK_INTERNAL_H_INCLUDED */