| 1 |
// Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
#include "internal.h" |
| 6 |
#include "../../internal.h" |
| 7 |
#include "../cpucap/internal.h" |
| 8 |
|
| 9 |
static const uint64_t iotas[] = { |
| 10 |
0x0000000000000001ULL, |
| 11 |
0x0000000000008082ULL, |
| 12 |
0x800000000000808aULL, |
| 13 |
0x8000000080008000ULL, |
| 14 |
0x000000000000808bULL, |
| 15 |
0x0000000080000001ULL, |
| 16 |
0x8000000080008081ULL, |
| 17 |
0x8000000000008009ULL, |
| 18 |
0x000000000000008aULL, |
| 19 |
0x0000000000000088ULL, |
| 20 |
0x0000000080008009ULL, |
| 21 |
0x000000008000000aULL, |
| 22 |
0x000000008000808bULL, |
| 23 |
0x800000000000008bULL, |
| 24 |
0x8000000000008089ULL, |
| 25 |
0x8000000000008003ULL, |
| 26 |
0x8000000000008002ULL, |
| 27 |
0x8000000000000080ULL, |
| 28 |
0x000000000000800aULL, |
| 29 |
0x800000008000000aULL, |
| 30 |
0x8000000080008081ULL, |
| 31 |
0x8000000000008080ULL, |
| 32 |
0x0000000080000001ULL, |
| 33 |
0x8000000080008008ULL |
| 34 |
}; |
| 35 |
|
| 36 |
#if defined(OPENSSL_X86_64) |
| 37 |
static const uint64_t keccak_rho8[4] = { |
| 38 |
0x0605040302010007ULL, 0x0E0D0C0B0A09080FULL, |
| 39 |
0x0605040302010007ULL, 0x0E0D0C0B0A09080FULL |
| 40 |
}; |
| 41 |
|
| 42 |
static const uint64_t keccak_rho56[4] = { |
| 43 |
0x0007060504030201ULL, 0x080F0E0D0C0B0A09ULL, |
| 44 |
0x0007060504030201ULL, 0x080F0E0D0C0B0A09ULL |
| 45 |
}; |
| 46 |
#endif |
| 47 |
|
| 48 |
#if !defined(KECCAK1600_ASM) |
| 49 |
|
| 50 |
static const uint8_t rhotates[KECCAK1600_ROWS][KECCAK1600_ROWS] = { |
| 51 |
{ 0, 1, 62, 28, 27 }, |
| 52 |
{ 36, 44, 6, 55, 20 }, |
| 53 |
{ 3, 10, 43, 25, 39 }, |
| 54 |
{ 41, 45, 15, 21, 8 }, |
| 55 |
{ 18, 2, 61, 56, 14 } |
| 56 |
}; |
| 57 |
|
| 58 |
#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 59 |
(defined(__x86_64) && !defined(__BMI__)) || defined(_M_X64) || \ |
| 60 |
defined(__mips) || defined(__riscv) || defined(__s390__) || defined(__loongarch__) || \ |
| 61 |
defined(__EMSCRIPTEN__) |
| 62 |
|
| 63 |
// These platforms don't support "logical and with complement" instruction. |
| 64 |
# define KECCAK_COMPLEMENTING_TRANSFORM |
| 65 |
#endif |
| 66 |
|
| 67 |
static uint64_t ROL64(uint64_t val, int offset) { |
| 68 |
if (offset == 0) { |
| 69 |
return val; |
| 70 |
} else { |
| 71 |
return (val << offset) | (val >> (64-offset)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// KECCAK_2X: |
| 76 |
// This is the default implementation used in OpenSSL and the most efficient; |
| 77 |
// the other implementations were removed from this file. |
| 78 |
// This implementation is a variant of KECCAK_1X (see OpenSSL) |
| 79 |
// This implementation allows to take temporary storage |
| 80 |
// out of round procedure and simplify references to it by alternating |
| 81 |
// it with actual data (see round loop below). |
| 82 |
// It ensures best compiler interpretation to assembly and provides best |
| 83 |
// instruction per processed byte ratio at minimal round unroll factor. |
| 84 |
static void Round(uint64_t R[KECCAK1600_ROWS][KECCAK1600_ROWS], uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS], size_t i) { |
| 85 |
uint64_t C[KECCAK1600_ROWS], D[KECCAK1600_ROWS]; |
| 86 |
|
| 87 |
assert(i < (sizeof(iotas) / sizeof(iotas[0]))); |
| 88 |
|
| 89 |
C[0] = A[0][0] ^ A[1][0] ^ A[2][0] ^ A[3][0] ^ A[4][0]; |
| 90 |
C[1] = A[0][1] ^ A[1][1] ^ A[2][1] ^ A[3][1] ^ A[4][1]; |
| 91 |
C[2] = A[0][2] ^ A[1][2] ^ A[2][2] ^ A[3][2] ^ A[4][2]; |
| 92 |
C[3] = A[0][3] ^ A[1][3] ^ A[2][3] ^ A[3][3] ^ A[4][3]; |
| 93 |
C[4] = A[0][4] ^ A[1][4] ^ A[2][4] ^ A[3][4] ^ A[4][4]; |
| 94 |
|
| 95 |
D[0] = ROL64(C[1], 1) ^ C[4]; |
| 96 |
D[1] = ROL64(C[2], 1) ^ C[0]; |
| 97 |
D[2] = ROL64(C[3], 1) ^ C[1]; |
| 98 |
D[3] = ROL64(C[4], 1) ^ C[2]; |
| 99 |
D[4] = ROL64(C[0], 1) ^ C[3]; |
| 100 |
|
| 101 |
C[0] = A[0][0] ^ D[0]; |
| 102 |
C[1] = ROL64(A[1][1] ^ D[1], rhotates[1][1]); |
| 103 |
C[2] = ROL64(A[2][2] ^ D[2], rhotates[2][2]); |
| 104 |
C[3] = ROL64(A[3][3] ^ D[3], rhotates[3][3]); |
| 105 |
C[4] = ROL64(A[4][4] ^ D[4], rhotates[4][4]); |
| 106 |
|
| 107 |
#ifdef KECCAK_COMPLEMENTING_TRANSFORM |
| 108 |
R[0][0] = C[0] ^ ( C[1] | C[2]) ^ iotas[i]; |
| 109 |
R[0][1] = C[1] ^ (~C[2] | C[3]); |
| 110 |
R[0][2] = C[2] ^ ( C[3] & C[4]); |
| 111 |
R[0][3] = C[3] ^ ( C[4] | C[0]); |
| 112 |
R[0][4] = C[4] ^ ( C[0] & C[1]); |
| 113 |
#else |
| 114 |
R[0][0] = C[0] ^ (~C[1] & C[2]) ^ iotas[i]; |
| 115 |
R[0][1] = C[1] ^ (~C[2] & C[3]); |
| 116 |
R[0][2] = C[2] ^ (~C[3] & C[4]); |
| 117 |
R[0][3] = C[3] ^ (~C[4] & C[0]); |
| 118 |
R[0][4] = C[4] ^ (~C[0] & C[1]); |
| 119 |
#endif |
| 120 |
|
| 121 |
C[0] = ROL64(A[0][3] ^ D[3], rhotates[0][3]); |
| 122 |
C[1] = ROL64(A[1][4] ^ D[4], rhotates[1][4]); |
| 123 |
C[2] = ROL64(A[2][0] ^ D[0], rhotates[2][0]); |
| 124 |
C[3] = ROL64(A[3][1] ^ D[1], rhotates[3][1]); |
| 125 |
C[4] = ROL64(A[4][2] ^ D[2], rhotates[4][2]); |
| 126 |
|
| 127 |
#ifdef KECCAK_COMPLEMENTING_TRANSFORM |
| 128 |
R[1][0] = C[0] ^ (C[1] | C[2]); |
| 129 |
R[1][1] = C[1] ^ (C[2] & C[3]); |
| 130 |
R[1][2] = C[2] ^ (C[3] | ~C[4]); |
| 131 |
R[1][3] = C[3] ^ (C[4] | C[0]); |
| 132 |
R[1][4] = C[4] ^ (C[0] & C[1]); |
| 133 |
#else |
| 134 |
R[1][0] = C[0] ^ (~C[1] & C[2]); |
| 135 |
R[1][1] = C[1] ^ (~C[2] & C[3]); |
| 136 |
R[1][2] = C[2] ^ (~C[3] & C[4]); |
| 137 |
R[1][3] = C[3] ^ (~C[4] & C[0]); |
| 138 |
R[1][4] = C[4] ^ (~C[0] & C[1]); |
| 139 |
#endif |
| 140 |
|
| 141 |
C[0] = ROL64(A[0][1] ^ D[1], rhotates[0][1]); |
| 142 |
C[1] = ROL64(A[1][2] ^ D[2], rhotates[1][2]); |
| 143 |
C[2] = ROL64(A[2][3] ^ D[3], rhotates[2][3]); |
| 144 |
C[3] = ROL64(A[3][4] ^ D[4], rhotates[3][4]); |
| 145 |
C[4] = ROL64(A[4][0] ^ D[0], rhotates[4][0]); |
| 146 |
|
| 147 |
#ifdef KECCAK_COMPLEMENTING_TRANSFORM |
| 148 |
R[2][0] = C[0] ^ ( C[1] | C[2]); |
| 149 |
R[2][1] = C[1] ^ ( C[2] & C[3]); |
| 150 |
R[2][2] = C[2] ^ (~C[3] & C[4]); |
| 151 |
R[2][3] = ~C[3] ^ ( C[4] | C[0]); |
| 152 |
R[2][4] = C[4] ^ ( C[0] & C[1]); |
| 153 |
#else |
| 154 |
R[2][0] = C[0] ^ (~C[1] & C[2]); |
| 155 |
R[2][1] = C[1] ^ (~C[2] & C[3]); |
| 156 |
R[2][2] = C[2] ^ (~C[3] & C[4]); |
| 157 |
R[2][3] = C[3] ^ (~C[4] & C[0]); |
| 158 |
R[2][4] = C[4] ^ (~C[0] & C[1]); |
| 159 |
#endif |
| 160 |
|
| 161 |
C[0] = ROL64(A[0][4] ^ D[4], rhotates[0][4]); |
| 162 |
C[1] = ROL64(A[1][0] ^ D[0], rhotates[1][0]); |
| 163 |
C[2] = ROL64(A[2][1] ^ D[1], rhotates[2][1]); |
| 164 |
C[3] = ROL64(A[3][2] ^ D[2], rhotates[3][2]); |
| 165 |
C[4] = ROL64(A[4][3] ^ D[3], rhotates[4][3]); |
| 166 |
|
| 167 |
#ifdef KECCAK_COMPLEMENTING_TRANSFORM |
| 168 |
R[3][0] = C[0] ^ ( C[1] & C[2]); |
| 169 |
R[3][1] = C[1] ^ ( C[2] | C[3]); |
| 170 |
R[3][2] = C[2] ^ (~C[3] | C[4]); |
| 171 |
R[3][3] = ~C[3] ^ ( C[4] & C[0]); |
| 172 |
R[3][4] = C[4] ^ ( C[0] | C[1]); |
| 173 |
#else |
| 174 |
R[3][0] = C[0] ^ (~C[1] & C[2]); |
| 175 |
R[3][1] = C[1] ^ (~C[2] & C[3]); |
| 176 |
R[3][2] = C[2] ^ (~C[3] & C[4]); |
| 177 |
R[3][3] = C[3] ^ (~C[4] & C[0]); |
| 178 |
R[3][4] = C[4] ^ (~C[0] & C[1]); |
| 179 |
#endif |
| 180 |
|
| 181 |
C[0] = ROL64(A[0][2] ^ D[2], rhotates[0][2]); |
| 182 |
C[1] = ROL64(A[1][3] ^ D[3], rhotates[1][3]); |
| 183 |
C[2] = ROL64(A[2][4] ^ D[4], rhotates[2][4]); |
| 184 |
C[3] = ROL64(A[3][0] ^ D[0], rhotates[3][0]); |
| 185 |
C[4] = ROL64(A[4][1] ^ D[1], rhotates[4][1]); |
| 186 |
|
| 187 |
#ifdef KECCAK_COMPLEMENTING_TRANSFORM |
| 188 |
R[4][0] = C[0] ^ (~C[1] & C[2]); |
| 189 |
R[4][1] = ~C[1] ^ ( C[2] | C[3]); |
| 190 |
R[4][2] = C[2] ^ ( C[3] & C[4]); |
| 191 |
R[4][3] = C[3] ^ ( C[4] | C[0]); |
| 192 |
R[4][4] = C[4] ^ ( C[0] & C[1]); |
| 193 |
#else |
| 194 |
R[4][0] = C[0] ^ (~C[1] & C[2]); |
| 195 |
R[4][1] = C[1] ^ (~C[2] & C[3]); |
| 196 |
R[4][2] = C[2] ^ (~C[3] & C[4]); |
| 197 |
R[4][3] = C[3] ^ (~C[4] & C[0]); |
| 198 |
R[4][4] = C[4] ^ (~C[0] & C[1]); |
| 199 |
#endif |
| 200 |
} |
| 201 |
|
| 202 |
static void KeccakF1600_c(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS]) { |
| 203 |
uint64_t T[KECCAK1600_ROWS][KECCAK1600_ROWS]; |
| 204 |
size_t i; |
| 205 |
|
| 206 |
#ifdef KECCAK_COMPLEMENTING_TRANSFORM |
| 207 |
A[0][1] = ~A[0][1]; |
| 208 |
A[0][2] = ~A[0][2]; |
| 209 |
A[1][3] = ~A[1][3]; |
| 210 |
A[2][2] = ~A[2][2]; |
| 211 |
A[3][2] = ~A[3][2]; |
| 212 |
A[4][0] = ~A[4][0]; |
| 213 |
#endif |
| 214 |
|
| 215 |
for (i = 0; i < 24; i += 2) { |
| 216 |
Round(T, A, i); |
| 217 |
Round(A, T, i + 1); |
| 218 |
} |
| 219 |
|
| 220 |
#ifdef KECCAK_COMPLEMENTING_TRANSFORM |
| 221 |
A[0][1] = ~A[0][1]; |
| 222 |
A[0][2] = ~A[0][2]; |
| 223 |
A[1][3] = ~A[1][3]; |
| 224 |
A[2][2] = ~A[2][2]; |
| 225 |
A[3][2] = ~A[3][2]; |
| 226 |
A[4][0] = ~A[4][0]; |
| 227 |
#endif |
| 228 |
} |
| 229 |
#endif // !KECCAK1600_ASM |
| 230 |
|
| 231 |
// Forward declaration for KeccakF1600 function |
| 232 |
void KeccakF1600(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS]); |
| 233 |
|
| 234 |
// Keccak1600_Absorb can be called multiple times; at each invocation the |
| 235 |
// largest multiple of |r| out of |len| bytes are processed. The |
| 236 |
// remaining amount of bytes is returned. This is done to spare caller |
| 237 |
// trouble of calculating the largest multiple of |r|. |r| can be viewed |
| 238 |
// as blocksize. It is commonly (1600 - 256*n)/8, e.g. 168, 136, 104, |
| 239 |
// 72, but can also be (1600 - 448)/8 = 144. All this means that message |
| 240 |
// padding and intermediate sub-block buffering, byte- or bitwise, is |
| 241 |
// caller's responsibility. |
| 242 |
|
| 243 |
// KeccakF1600_XORBytes XORs |len| bytes from |inp| into the Keccak state |A|. |
| 244 |
// |len| must be a multiple of 8. |
| 245 |
static void KeccakF1600_XORBytes(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS], const uint8_t *inp, size_t len) { |
| 246 |
assert(len <= SHA3_MAX_BLOCKSIZE); |
| 247 |
assert((len % 8) == 0); |
| 248 |
|
| 249 |
uint64_t *A_flat = (uint64_t *)A; |
| 250 |
size_t w = len / 8; |
| 251 |
|
| 252 |
for (size_t i = 0; i < w; i++) { |
| 253 |
uint64_t Ai = (uint64_t)inp[0] | (uint64_t)inp[1] << 8 | |
| 254 |
(uint64_t)inp[2] << 16 | (uint64_t)inp[3] << 24 | |
| 255 |
(uint64_t)inp[4] << 32 | (uint64_t)inp[5] << 40 | |
| 256 |
(uint64_t)inp[6] << 48 | (uint64_t)inp[7] << 56; |
| 257 |
inp += 8; |
| 258 |
A_flat[i] ^= Ai; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
size_t Keccak1600_Absorb(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS], const uint8_t *inp, size_t len, |
| 263 |
size_t r) { |
| 264 |
assert(r < (25 * sizeof(A[0][0])) && (r % 8) == 0); |
| 265 |
|
| 266 |
while (len >= r) { |
| 267 |
KeccakF1600_XORBytes(A, inp, r); |
| 268 |
KeccakF1600(A); |
| 269 |
inp += r; |
| 270 |
len -= r; |
| 271 |
} |
| 272 |
|
| 273 |
return len; |
| 274 |
} |
| 275 |
|
| 276 |
// KeccakF1600_ExtractBytes extracts |len| bytes from the Keccak state |A| into |out|. |
| 277 |
// This function operates on up to block_size bytes (a single block). For extracting |
| 278 |
// more data, the state must be processed again through KeccakF1600 (see Keccak1600_Squeeze). |
| 279 |
static void KeccakF1600_ExtractBytes(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS], uint8_t *out, size_t len) { |
| 280 |
uint64_t *A_flat = (uint64_t *)A; |
| 281 |
assert(len <= SHA3_MAX_BLOCKSIZE); |
| 282 |
size_t i = 0; |
| 283 |
|
| 284 |
while (len != 0) { |
| 285 |
uint64_t Ai = A_flat[i]; |
| 286 |
|
| 287 |
if (len < 8) { |
| 288 |
for (size_t j = 0; j < len; j++) { |
| 289 |
*out++ = (uint8_t)Ai; |
| 290 |
Ai >>= 8; |
| 291 |
} |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
out[0] = (uint8_t)(Ai); |
| 296 |
out[1] = (uint8_t)(Ai >> 8); |
| 297 |
out[2] = (uint8_t)(Ai >> 16); |
| 298 |
out[3] = (uint8_t)(Ai >> 24); |
| 299 |
out[4] = (uint8_t)(Ai >> 32); |
| 300 |
out[5] = (uint8_t)(Ai >> 40); |
| 301 |
out[6] = (uint8_t)(Ai >> 48); |
| 302 |
out[7] = (uint8_t)(Ai >> 56); |
| 303 |
out += 8; |
| 304 |
len -= 8; |
| 305 |
i++; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
void Keccak1600_Squeeze(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS], uint8_t *out, size_t len, size_t r, int padded) { |
| 310 |
assert(r < (25 * sizeof(A[0][0])) && (r % 8) == 0); |
| 311 |
|
| 312 |
while (len != 0) { |
| 313 |
if (padded) { |
| 314 |
KeccakF1600(A); |
| 315 |
} |
| 316 |
padded = 1; |
| 317 |
|
| 318 |
size_t extract_len = len < r ? len : r; |
| 319 |
KeccakF1600_ExtractBytes(A, out, extract_len); |
| 320 |
out += extract_len; |
| 321 |
len -= extract_len; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
#if defined(KECCAK1600_ASM) |
| 326 |
|
| 327 |
// Scalar implementation from OpenSSL provided by keccak1600-armv8.pl |
| 328 |
extern void KeccakF1600_hw(uint64_t state[25]); |
| 329 |
|
| 330 |
#if defined(OPENSSL_AARCH64) || defined(OPENSSL_X86_64) |
| 331 |
static void keccak_log_dispatch(size_t id) { |
| 332 |
#if BORINGSSL_DISPATCH_TEST |
| 333 |
BORINGSSL_function_hit[id] = 1; |
| 334 |
#endif |
| 335 |
} |
| 336 |
#endif |
| 337 |
|
| 338 |
void KeccakF1600(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS]) { |
| 339 |
// Dispatch logic for Keccak-x1 on AArch64: |
| 340 |
// |
| 341 |
// 1. If ASM is disabled, we use the C implementation. |
| 342 |
// 2. If ASM is enabled: |
| 343 |
// - For Neoverse N1, V1, V2, we use scalar Keccak assembly from s2n-bignum |
| 344 |
// (`sha3_keccak_f1600()`) |
| 345 |
// leveraging lazy rotations from https://eprint.iacr.org/2022/1243. |
| 346 |
// - Otherwise, if the Neon SHA3 extension is supported, we use the Neon |
| 347 |
// Keccak assembly from s2n-bignum (`sha3_keccak_f1600_alt()`), |
| 348 |
// leveraging that extension. |
| 349 |
// - Otherwise, fall back to scalar Keccak implementation from OpenSSL, |
| 350 |
// (`Keccak1600_hw()`), not using lazy rotations. |
| 351 |
// |
| 352 |
// Lazy rotations improve performance by up to 10% on CPUs with free |
| 353 |
// Barrel shifting, which includes Neoverse N1, V1, and V2. Not all |
| 354 |
// CPUs have free Barrel shifting (e.g. Apple M1 or Cortex-A72), so we |
| 355 |
// don't use it by default. |
| 356 |
// |
| 357 |
// Neoverse V1 and V2 do support SHA3 instructions, but they are only |
| 358 |
// implemented on 1/4 of Neon units, and are thus slower than a scalar |
| 359 |
// implementation. |
| 360 |
#if defined(OPENSSL_AARCH64) |
| 361 |
#if defined(KECCAK1600_S2N_BIGNUM_ASM) |
| 362 |
if (CRYPTO_is_Neoverse_N1() || CRYPTO_is_Neoverse_V1() || CRYPTO_is_Neoverse_V2()) { |
| 363 |
keccak_log_dispatch(10); // kFlag_sha3_keccak_f1600 |
| 364 |
sha3_keccak_f1600((uint64_t *)A, iotas); |
| 365 |
return; |
| 366 |
} |
| 367 |
|
| 368 |
#if defined(MY_ASSEMBLER_SUPPORTS_NEON_SHA3_EXTENSION) |
| 369 |
if (CRYPTO_is_ARMv8_SHA3_capable()) { |
| 370 |
keccak_log_dispatch(11); // kFlag_sha3_keccak_f1600_alt |
| 371 |
sha3_keccak_f1600_alt((uint64_t *)A, iotas); |
| 372 |
return; |
| 373 |
} |
| 374 |
#endif |
| 375 |
#endif |
| 376 |
|
| 377 |
keccak_log_dispatch(9); // kFlag_KeccakF1600_hw |
| 378 |
KeccakF1600_hw((uint64_t *) A); |
| 379 |
|
| 380 |
#elif defined(OPENSSL_X86_64) |
| 381 |
keccak_log_dispatch(9); // kFlag_sha3_keccak_f1600 |
| 382 |
sha3_keccak_f1600((uint64_t *)A, iotas); |
| 383 |
#endif |
| 384 |
} |
| 385 |
|
| 386 |
#else // KECCAK1600_ASM |
| 387 |
|
| 388 |
void KeccakF1600(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS]) |
| 389 |
{ |
| 390 |
KeccakF1600_c(A); |
| 391 |
} |
| 392 |
|
| 393 |
#endif // !KECCAK1600_ASM |
| 394 |
|
| 395 |
// KeccakF1600_XORBytes_x4 XORs |len| bytes from |inp0|, |inp1|, |inp2|, |inp3| |
| 396 |
// into the four Keccak states in |A|. |len| must be a multiple of 8. |
| 397 |
static void KeccakF1600_XORBytes_x4(uint64_t A[4][KECCAK1600_ROWS][KECCAK1600_ROWS], |
| 398 |
const uint8_t *inp0, const uint8_t *inp1, |
| 399 |
const uint8_t *inp2, const uint8_t *inp3, |
| 400 |
size_t len) { |
| 401 |
KeccakF1600_XORBytes(A[0], inp0, len); |
| 402 |
KeccakF1600_XORBytes(A[1], inp1, len); |
| 403 |
KeccakF1600_XORBytes(A[2], inp2, len); |
| 404 |
KeccakF1600_XORBytes(A[3], inp3, len); |
| 405 |
} |
| 406 |
|
| 407 |
// KeccakF1600_ExtractBytes_x4 extracts |len| bytes from the four Keccak states in |A| |
| 408 |
// into |out0|, |out1|, |out2|, |out3|. |
| 409 |
static void KeccakF1600_ExtractBytes_x4(uint64_t A[4][KECCAK1600_ROWS][KECCAK1600_ROWS], |
| 410 |
uint8_t *out0, uint8_t *out1, |
| 411 |
uint8_t *out2, uint8_t *out3, |
| 412 |
size_t len) { |
| 413 |
KeccakF1600_ExtractBytes(A[0], out0, len); |
| 414 |
KeccakF1600_ExtractBytes(A[1], out1, len); |
| 415 |
KeccakF1600_ExtractBytes(A[2], out2, len); |
| 416 |
KeccakF1600_ExtractBytes(A[3], out3, len); |
| 417 |
} |
| 418 |
|
| 419 |
static void Keccak1600_x4(uint64_t A[4][KECCAK1600_ROWS][KECCAK1600_ROWS]) { |
| 420 |
// Dispatch logic for Keccak-x4 on AArch64: |
| 421 |
// |
| 422 |
// 1. If ASM is disabled, we use 4x the C implementation. |
| 423 |
// 2. If ASM is enabled: |
| 424 |
// - For Neoverse N1, we use scalar batched hybrid Keccak assembly from s2n-bignum |
| 425 |
// (`sha3_keccak4_f1600_alt()`) leveraging Neon and scalar assembly with |
| 426 |
// lazy rotations. |
| 427 |
// - For Neoverse V1, V2, we use SIMD batched hybrid Keccak assembly from s2n-bignum |
| 428 |
// (`sha3_keccak4_f1600_alt2()`) leveraging Neon, Neon SHA3 extension, |
| 429 |
// and scalar assembly with lazy rotations. |
| 430 |
// - Otherwise, if the Neon SHA3 extension is supported, we use the 2-fold |
| 431 |
// Keccak assembly from s2n-bignum (`sha3_keccak2_f1600()`) twice, |
| 432 |
// which is a straightforward implementation using the SHA3 extension. |
| 433 |
// - Otherwise, fall back to four times the 1-fold Keccak implementation |
| 434 |
// (which has its own dispatch logic). |
| 435 |
#if defined(KECCAK1600_S2N_BIGNUM_ASM) && defined(OPENSSL_AARCH64) |
| 436 |
if (CRYPTO_is_Neoverse_N1()) { |
| 437 |
keccak_log_dispatch(13); // kFlag_sha3_keccak4_f1600_alt |
| 438 |
sha3_keccak4_f1600_alt((uint64_t *)A, iotas); |
| 439 |
return; |
| 440 |
} |
| 441 |
|
| 442 |
#if defined(MY_ASSEMBLER_SUPPORTS_NEON_SHA3_EXTENSION) |
| 443 |
if (CRYPTO_is_Neoverse_V1() || CRYPTO_is_Neoverse_V2()) { |
| 444 |
keccak_log_dispatch(14); // kFlag_sha3_keccak4_f1600_alt2 |
| 445 |
sha3_keccak4_f1600_alt2((uint64_t *)A, iotas); |
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
if (CRYPTO_is_ARMv8_SHA3_capable()) { |
| 450 |
keccak_log_dispatch(12); // kFlag_sha3_keccak2_f1600 |
| 451 |
// Use 2-fold function twice: A[0:1] and A[2:3] |
| 452 |
sha3_keccak2_f1600((uint64_t *)&A[0], iotas); |
| 453 |
sha3_keccak2_f1600((uint64_t *)&A[2], iotas); |
| 454 |
return; |
| 455 |
} |
| 456 |
#endif |
| 457 |
#endif |
| 458 |
|
| 459 |
#if defined(KECCAK1600_S2N_BIGNUM_ASM) && defined(OPENSSL_X86_64) |
| 460 |
if (CRYPTO_is_AVX2_capable()) { |
| 461 |
keccak_log_dispatch(10); // kFlag_sha3_keccak4_f1600_alt |
| 462 |
sha3_keccak4_f1600_alt((uint64_t *)A, iotas, keccak_rho8, keccak_rho56); |
| 463 |
return; |
| 464 |
} |
| 465 |
#endif |
| 466 |
|
| 467 |
// Fallback: 4x individual KeccakF1600 calls (each with their own dispatch) |
| 468 |
KeccakF1600(A[0]); |
| 469 |
KeccakF1600(A[1]); |
| 470 |
KeccakF1600(A[2]); |
| 471 |
KeccakF1600(A[3]); |
| 472 |
} |
| 473 |
|
| 474 |
// One-shot absorb + finalize. Note that in contract to non-batched Keccak, |
| 475 |
// this does _not_ run a Keccak permutation at the end, allowing for a uniform |
| 476 |
// implementation of Keccak1600_Squeezeblocks_x4() without `padded` parameter |
| 477 |
// as in the non-batched implementation. |
| 478 |
void Keccak1600_Absorb_once_x4(uint64_t A[4][KECCAK1600_ROWS][KECCAK1600_ROWS], |
| 479 |
const uint8_t *inp0, const uint8_t *inp1, |
| 480 |
const uint8_t *inp2, const uint8_t *inp3, |
| 481 |
size_t len, size_t r, uint8_t p) { |
| 482 |
assert(r <= SHA3_MAX_BLOCKSIZE); |
| 483 |
|
| 484 |
while (len >= r) { |
| 485 |
KeccakF1600_XORBytes_x4(A, inp0, inp1, inp2, inp3, r); |
| 486 |
Keccak1600_x4(A); |
| 487 |
inp0 += r; |
| 488 |
inp1 += r; |
| 489 |
inp2 += r; |
| 490 |
inp3 += r; |
| 491 |
len -= r; |
| 492 |
} |
| 493 |
|
| 494 |
// Build 16-byte aligned final blocks for each input |
| 495 |
alignas(16) uint8_t final[4][SHA3_MAX_BLOCKSIZE] = {{0}}; |
| 496 |
|
| 497 |
// Copy the remainder bytes to final blocks |
| 498 |
OPENSSL_memcpy(final[0], inp0, len); |
| 499 |
OPENSSL_memcpy(final[1], inp1, len); |
| 500 |
OPENSSL_memcpy(final[2], inp2, len); |
| 501 |
OPENSSL_memcpy(final[3], inp3, len); |
| 502 |
|
| 503 |
if (len == r - 1) { |
| 504 |
p |= 128; |
| 505 |
} else { |
| 506 |
final[0][r - 1] |= 128; |
| 507 |
final[1][r - 1] |= 128; |
| 508 |
final[2][r - 1] |= 128; |
| 509 |
final[3][r - 1] |= 128; |
| 510 |
} |
| 511 |
|
| 512 |
final[0][len] |= p; |
| 513 |
final[1][len] |= p; |
| 514 |
final[2][len] |= p; |
| 515 |
final[3][len] |= p; |
| 516 |
|
| 517 |
KeccakF1600_XORBytes_x4(A, final[0], final[1], final[2], final[3], r); |
| 518 |
|
| 519 |
// Clean up final blocks to avoid stack leakage |
| 520 |
OPENSSL_cleanse(final, sizeof(final)); |
| 521 |
} |
| 522 |
|
| 523 |
void Keccak1600_Squeezeblocks_x4(uint64_t A[4][KECCAK1600_ROWS][KECCAK1600_ROWS], uint8_t *out0, uint8_t *out1, |
| 524 |
uint8_t *out2, uint8_t *out3, |
| 525 |
size_t num_blocks, size_t r) { |
| 526 |
while (num_blocks != 0) { |
| 527 |
Keccak1600_x4(A); |
| 528 |
KeccakF1600_ExtractBytes_x4(A, out0, out1, out2, out3, r); |
| 529 |
|
| 530 |
out0 += r; |
| 531 |
out1 += r; |
| 532 |
out2 += r; |
| 533 |
out3 += r; |
| 534 |
num_blocks--; |
| 535 |
} |
| 536 |
} |