| 1 |
/* keccak.c - SHA3 hash functions |
| 2 |
* Copyright (C) 2015 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of Libgcrypt. |
| 5 |
* |
| 6 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
#include <string.h> |
| 23 |
#include "g10lib.h" |
| 24 |
#include "bithelp.h" |
| 25 |
#include "bufhelp.h" |
| 26 |
#include "cipher.h" |
| 27 |
#include "hash-common.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/* USE_64BIT indicates whether to use 64-bit generic implementation. |
| 32 |
* USE_32BIT indicates whether to use 32-bit generic implementation. */ |
| 33 |
#undef USE_64BIT |
| 34 |
#if defined(__x86_64__) || SIZEOF_UNSIGNED_LONG == 8 |
| 35 |
# define USE_64BIT 1 |
| 36 |
#else |
| 37 |
# define USE_32BIT 1 |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
/* USE_64BIT_BMI2 indicates whether to compile with 64-bit Intel BMI2 code. */ |
| 42 |
#undef USE_64BIT_BMI2 |
| 43 |
#if defined(USE_64BIT) && defined(HAVE_GCC_INLINE_ASM_BMI2) && \ |
| 44 |
defined(HAVE_CPU_ARCH_X86) |
| 45 |
# define USE_64BIT_BMI2 1 |
| 46 |
#endif |
| 47 |
|
| 48 |
|
| 49 |
/* USE_64BIT_SHLD indicates whether to compile with 64-bit Intel SHLD code. */ |
| 50 |
#undef USE_64BIT_SHLD |
| 51 |
#if defined(USE_64BIT) && defined (__GNUC__) && defined(__x86_64__) && \ |
| 52 |
defined(HAVE_CPU_ARCH_X86) |
| 53 |
# define USE_64BIT_SHLD 1 |
| 54 |
#endif |
| 55 |
|
| 56 |
|
| 57 |
/* USE_32BIT_BMI2 indicates whether to compile with 32-bit Intel BMI2 code. */ |
| 58 |
#undef USE_32BIT_BMI2 |
| 59 |
#if defined(USE_32BIT) && defined(HAVE_GCC_INLINE_ASM_BMI2) && \ |
| 60 |
defined(HAVE_CPU_ARCH_X86) |
| 61 |
# define USE_32BIT_BMI2 1 |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
/* USE_64BIT_AVX512 indicates whether to compile with Intel AVX512 code. */ |
| 66 |
#undef USE_64BIT_AVX512 |
| 67 |
#if defined(USE_64BIT) && defined(__x86_64__) && \ |
| 68 |
defined(HAVE_GCC_INLINE_ASM_AVX512) && \ |
| 69 |
(defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \ |
| 70 |
defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) |
| 71 |
# define USE_64BIT_AVX512 1 |
| 72 |
#endif |
| 73 |
|
| 74 |
|
| 75 |
/* USE_64BIT_ARM_NEON indicates whether to enable 64-bit ARM/NEON assembly |
| 76 |
* code. */ |
| 77 |
#undef USE_64BIT_ARM_NEON |
| 78 |
#ifdef ENABLE_NEON_SUPPORT |
| 79 |
# if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \ |
| 80 |
&& defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \ |
| 81 |
&& defined(HAVE_GCC_INLINE_ASM_NEON) |
| 82 |
# define USE_64BIT_ARM_NEON 1 |
| 83 |
# endif |
| 84 |
#endif /*ENABLE_NEON_SUPPORT*/ |
| 85 |
|
| 86 |
|
| 87 |
/* USE_S390X_CRYPTO indicates whether to enable zSeries code. */ |
| 88 |
#undef USE_S390X_CRYPTO |
| 89 |
#if defined(HAVE_GCC_INLINE_ASM_S390X) |
| 90 |
# define USE_S390X_CRYPTO 1 |
| 91 |
#endif /* USE_S390X_CRYPTO */ |
| 92 |
|
| 93 |
|
| 94 |
/* GCM_USE_RISCV_ZBB indicates whether to compile GCM with RISC-V Zbb extension |
| 95 |
* code. */ |
| 96 |
#undef USE_RISCV_ZBB |
| 97 |
#if defined (__riscv) && (__riscv_xlen == 64) && \ |
| 98 |
defined(HAVE_GCC_INLINE_ASM_RISCV) |
| 99 |
# define USE_RISCV_ZBB 1 |
| 100 |
#endif |
| 101 |
|
| 102 |
|
| 103 |
/* x86-64 vector register assembly implementations use SystemV ABI, ABI |
| 104 |
* conversion needed on Win64 through function attribute. */ |
| 105 |
#undef ASM_FUNC_ABI |
| 106 |
#if defined(USE_64BIT_AVX512) && defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS) |
| 107 |
# define ASM_FUNC_ABI __attribute__((sysv_abi)) |
| 108 |
#else |
| 109 |
# define ASM_FUNC_ABI |
| 110 |
#endif |
| 111 |
|
| 112 |
|
| 113 |
#if defined(USE_64BIT) || defined(USE_64BIT_ARM_NEON) |
| 114 |
# define NEED_COMMON64 1 |
| 115 |
#endif |
| 116 |
|
| 117 |
#ifdef USE_32BIT |
| 118 |
# define NEED_COMMON32BI 1 |
| 119 |
#endif |
| 120 |
|
| 121 |
|
| 122 |
#define SHA3_DELIMITED_SUFFIX 0x06 |
| 123 |
#define SHAKE_DELIMITED_SUFFIX 0x1F |
| 124 |
#define CSHAKE_DELIMITED_SUFFIX 0x04 |
| 125 |
|
| 126 |
typedef struct |
| 127 |
{ |
| 128 |
union { |
| 129 |
#ifdef NEED_COMMON64 |
| 130 |
u64 state64[25]; |
| 131 |
#endif |
| 132 |
#ifdef NEED_COMMON32BI |
| 133 |
u32 state32bi[50]; |
| 134 |
#endif |
| 135 |
} u; |
| 136 |
} KECCAK_STATE; |
| 137 |
|
| 138 |
|
| 139 |
typedef struct |
| 140 |
{ |
| 141 |
unsigned int (*permute)(KECCAK_STATE *hd); |
| 142 |
unsigned int (*absorb)(KECCAK_STATE *hd, int pos, const byte *lanes, |
| 143 |
size_t nlanes, int blocklanes); |
| 144 |
unsigned int (*extract) (KECCAK_STATE *hd, unsigned int pos, byte *outbuf, |
| 145 |
unsigned int outlen); |
| 146 |
} keccak_ops_t; |
| 147 |
|
| 148 |
|
| 149 |
typedef struct KECCAK_CONTEXT_S |
| 150 |
{ |
| 151 |
KECCAK_STATE state; |
| 152 |
unsigned int outlen; |
| 153 |
unsigned int blocksize; |
| 154 |
unsigned int count; |
| 155 |
unsigned int suffix:8; |
| 156 |
unsigned int shake_in_extract_mode:1; |
| 157 |
unsigned int shake_in_read_mode:1; |
| 158 |
const keccak_ops_t *ops; |
| 159 |
#ifdef USE_S390X_CRYPTO |
| 160 |
unsigned int kimd_func; |
| 161 |
unsigned int buf_pos; |
| 162 |
byte buf[1344 / 8]; /* SHAKE128 requires biggest buffer, 1344 bits. */ |
| 163 |
#endif |
| 164 |
} KECCAK_CONTEXT; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
#ifdef NEED_COMMON64 |
| 169 |
|
| 170 |
const u64 _gcry_keccak_round_consts_64bit[24 + 1] = |
| 171 |
{ |
| 172 |
U64_C(0x0000000000000001), U64_C(0x0000000000008082), |
| 173 |
U64_C(0x800000000000808A), U64_C(0x8000000080008000), |
| 174 |
U64_C(0x000000000000808B), U64_C(0x0000000080000001), |
| 175 |
U64_C(0x8000000080008081), U64_C(0x8000000000008009), |
| 176 |
U64_C(0x000000000000008A), U64_C(0x0000000000000088), |
| 177 |
U64_C(0x0000000080008009), U64_C(0x000000008000000A), |
| 178 |
U64_C(0x000000008000808B), U64_C(0x800000000000008B), |
| 179 |
U64_C(0x8000000000008089), U64_C(0x8000000000008003), |
| 180 |
U64_C(0x8000000000008002), U64_C(0x8000000000000080), |
| 181 |
U64_C(0x000000000000800A), U64_C(0x800000008000000A), |
| 182 |
U64_C(0x8000000080008081), U64_C(0x8000000000008080), |
| 183 |
U64_C(0x0000000080000001), U64_C(0x8000000080008008), |
| 184 |
U64_C(0xFFFFFFFFFFFFFFFF) |
| 185 |
}; |
| 186 |
|
| 187 |
|
| 188 |
#if defined (__riscv) && (__riscv_xlen == 64) \ |
| 189 |
&& defined(HAVE_GCC_INLINE_ASM_RISCV) |
| 190 |
|
| 191 |
#define load_aligned_u64(ptr, offset) ({ \ |
| 192 |
u64 tmp; \ |
| 193 |
asm ("ld %0, " #offset " (%1)" : "=r" (tmp) : "r" (ptr) : "memory"); \ |
| 194 |
tmp; }) |
| 195 |
|
| 196 |
#define HAVE_ALIGNED_ABSORB_LANES64 1 |
| 197 |
|
| 198 |
static inline void aligned_absorb_lanes64_8(u64 *dst, const byte *in) |
| 199 |
{ |
| 200 |
dst[0] ^= load_aligned_u64(in, 0); |
| 201 |
dst[1] ^= load_aligned_u64(in, 8); |
| 202 |
dst[2] ^= load_aligned_u64(in, 16); |
| 203 |
dst[3] ^= load_aligned_u64(in, 24); |
| 204 |
dst[4] ^= load_aligned_u64(in, 32); |
| 205 |
dst[5] ^= load_aligned_u64(in, 40); |
| 206 |
dst[6] ^= load_aligned_u64(in, 48); |
| 207 |
dst[7] ^= load_aligned_u64(in, 56); |
| 208 |
} |
| 209 |
|
| 210 |
static inline void aligned_absorb_lanes64_4(u64 *dst, const byte *in) |
| 211 |
{ |
| 212 |
dst[0] ^= load_aligned_u64(in, 0); |
| 213 |
dst[1] ^= load_aligned_u64(in, 8); |
| 214 |
dst[2] ^= load_aligned_u64(in, 16); |
| 215 |
dst[3] ^= load_aligned_u64(in, 24); |
| 216 |
} |
| 217 |
|
| 218 |
static inline void aligned_absorb_lanes64_2(u64 *dst, const byte *in) |
| 219 |
{ |
| 220 |
dst[0] ^= load_aligned_u64(in, 0); |
| 221 |
dst[1] ^= load_aligned_u64(in, 8); |
| 222 |
} |
| 223 |
|
| 224 |
static inline void aligned_absorb_lanes64_1(u64 *dst, const byte *in) |
| 225 |
{ |
| 226 |
dst[0] ^= load_aligned_u64(in, 0); |
| 227 |
} |
| 228 |
|
| 229 |
#undef load_aligned_u64 |
| 230 |
|
| 231 |
|
| 232 |
#define HAVE_ALIGNED_EXTRACT64 |
| 233 |
static inline unsigned int |
| 234 |
aligned_extract64(KECCAK_STATE *hd, unsigned int pos, byte *outbuf, |
| 235 |
unsigned int outlen) |
| 236 |
{ |
| 237 |
unsigned int i; |
| 238 |
|
| 239 |
/* NOTE: when pos == 0, hd and outbuf may point to same memory (SHA-3). */ |
| 240 |
|
| 241 |
for (i = pos; i < pos + outlen / 8 + !!(outlen % 8); i++) |
| 242 |
{ |
| 243 |
u64 tmp = hd->u.state64[i]; |
| 244 |
asm ("sd %1, 0(%0)" :: "r" (outbuf), "r" (tmp) : "memory"); |
| 245 |
outbuf += 8; |
| 246 |
} |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
#endif /* __riscv */ |
| 252 |
|
| 253 |
|
| 254 |
static unsigned int |
| 255 |
keccak_extract64(KECCAK_STATE *hd, unsigned int pos, byte *outbuf, |
| 256 |
unsigned int outlen) |
| 257 |
{ |
| 258 |
unsigned int i; |
| 259 |
|
| 260 |
#ifdef HAVE_ALIGNED_EXTRACT64 |
| 261 |
if (LIKELY(((uintptr_t)outbuf & 7) == 0)) |
| 262 |
return aligned_extract64(hd, pos, outbuf, outlen); |
| 263 |
#endif |
| 264 |
|
| 265 |
/* NOTE: when pos == 0, hd and outbuf may point to same memory (SHA-3). */ |
| 266 |
|
| 267 |
for (i = pos; i < pos + outlen / 8 + !!(outlen % 8); i++) |
| 268 |
{ |
| 269 |
u64 tmp = hd->u.state64[i]; |
| 270 |
buf_put_le64(outbuf, tmp); |
| 271 |
outbuf += 8; |
| 272 |
} |
| 273 |
|
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
#endif /* NEED_COMMON64 */ |
| 278 |
|
| 279 |
|
| 280 |
#ifdef NEED_COMMON32BI |
| 281 |
|
| 282 |
static const u32 round_consts_32bit[2 * 24] = |
| 283 |
{ |
| 284 |
0x00000001UL, 0x00000000UL, 0x00000000UL, 0x00000089UL, |
| 285 |
0x00000000UL, 0x8000008bUL, 0x00000000UL, 0x80008080UL, |
| 286 |
0x00000001UL, 0x0000008bUL, 0x00000001UL, 0x00008000UL, |
| 287 |
0x00000001UL, 0x80008088UL, 0x00000001UL, 0x80000082UL, |
| 288 |
0x00000000UL, 0x0000000bUL, 0x00000000UL, 0x0000000aUL, |
| 289 |
0x00000001UL, 0x00008082UL, 0x00000000UL, 0x00008003UL, |
| 290 |
0x00000001UL, 0x0000808bUL, 0x00000001UL, 0x8000000bUL, |
| 291 |
0x00000001UL, 0x8000008aUL, 0x00000001UL, 0x80000081UL, |
| 292 |
0x00000000UL, 0x80000081UL, 0x00000000UL, 0x80000008UL, |
| 293 |
0x00000000UL, 0x00000083UL, 0x00000000UL, 0x80008003UL, |
| 294 |
0x00000001UL, 0x80008088UL, 0x00000000UL, 0x80000088UL, |
| 295 |
0x00000001UL, 0x00008000UL, 0x00000000UL, 0x80008082UL |
| 296 |
}; |
| 297 |
|
| 298 |
static unsigned int |
| 299 |
keccak_extract32bi(KECCAK_STATE *hd, unsigned int pos, byte *outbuf, |
| 300 |
unsigned int outlen) |
| 301 |
{ |
| 302 |
unsigned int i; |
| 303 |
u32 x0; |
| 304 |
u32 x1; |
| 305 |
u32 t; |
| 306 |
|
| 307 |
/* NOTE: when pos == 0, hd and outbuf may point to same memory (SHA-3). */ |
| 308 |
|
| 309 |
for (i = pos; i < pos + outlen / 8 + !!(outlen % 8); i++) |
| 310 |
{ |
| 311 |
x0 = hd->u.state32bi[i * 2 + 0]; |
| 312 |
x1 = hd->u.state32bi[i * 2 + 1]; |
| 313 |
|
| 314 |
t = (x0 & 0x0000FFFFUL) + (x1 << 16); |
| 315 |
x1 = (x0 >> 16) + (x1 & 0xFFFF0000UL); |
| 316 |
x0 = t; |
| 317 |
t = (x0 ^ (x0 >> 8)) & 0x0000FF00UL; x0 = x0 ^ t ^ (t << 8); |
| 318 |
t = (x0 ^ (x0 >> 4)) & 0x00F000F0UL; x0 = x0 ^ t ^ (t << 4); |
| 319 |
t = (x0 ^ (x0 >> 2)) & 0x0C0C0C0CUL; x0 = x0 ^ t ^ (t << 2); |
| 320 |
t = (x0 ^ (x0 >> 1)) & 0x22222222UL; x0 = x0 ^ t ^ (t << 1); |
| 321 |
t = (x1 ^ (x1 >> 8)) & 0x0000FF00UL; x1 = x1 ^ t ^ (t << 8); |
| 322 |
t = (x1 ^ (x1 >> 4)) & 0x00F000F0UL; x1 = x1 ^ t ^ (t << 4); |
| 323 |
t = (x1 ^ (x1 >> 2)) & 0x0C0C0C0CUL; x1 = x1 ^ t ^ (t << 2); |
| 324 |
t = (x1 ^ (x1 >> 1)) & 0x22222222UL; x1 = x1 ^ t ^ (t << 1); |
| 325 |
|
| 326 |
buf_put_le32(&outbuf[0], x0); |
| 327 |
buf_put_le32(&outbuf[4], x1); |
| 328 |
outbuf += 8; |
| 329 |
} |
| 330 |
|
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
static inline void |
| 335 |
keccak_absorb_lane32bi(u32 *lane, u32 x0, u32 x1) |
| 336 |
{ |
| 337 |
u32 t; |
| 338 |
|
| 339 |
t = (x0 ^ (x0 >> 1)) & 0x22222222UL; x0 = x0 ^ t ^ (t << 1); |
| 340 |
t = (x0 ^ (x0 >> 2)) & 0x0C0C0C0CUL; x0 = x0 ^ t ^ (t << 2); |
| 341 |
t = (x0 ^ (x0 >> 4)) & 0x00F000F0UL; x0 = x0 ^ t ^ (t << 4); |
| 342 |
t = (x0 ^ (x0 >> 8)) & 0x0000FF00UL; x0 = x0 ^ t ^ (t << 8); |
| 343 |
t = (x1 ^ (x1 >> 1)) & 0x22222222UL; x1 = x1 ^ t ^ (t << 1); |
| 344 |
t = (x1 ^ (x1 >> 2)) & 0x0C0C0C0CUL; x1 = x1 ^ t ^ (t << 2); |
| 345 |
t = (x1 ^ (x1 >> 4)) & 0x00F000F0UL; x1 = x1 ^ t ^ (t << 4); |
| 346 |
t = (x1 ^ (x1 >> 8)) & 0x0000FF00UL; x1 = x1 ^ t ^ (t << 8); |
| 347 |
lane[0] ^= (x0 & 0x0000FFFFUL) + (x1 << 16); |
| 348 |
lane[1] ^= (x0 >> 16) + (x1 & 0xFFFF0000UL); |
| 349 |
} |
| 350 |
|
| 351 |
#endif /* NEED_COMMON32BI */ |
| 352 |
|
| 353 |
|
| 354 |
/* Construct generic 64-bit implementation. */ |
| 355 |
#ifdef USE_64BIT |
| 356 |
|
| 357 |
#if __GNUC__ >= 4 && defined(__x86_64__) |
| 358 |
|
| 359 |
static inline void unaligned_absorb_lanes64_8(u64 *dst, const byte *in) |
| 360 |
{ |
| 361 |
asm ("movdqu 0*16(%[dst]), %%xmm0\n\t" |
| 362 |
"movdqu 0*16(%[in]), %%xmm4\n\t" |
| 363 |
"movdqu 1*16(%[dst]), %%xmm1\n\t" |
| 364 |
"movdqu 1*16(%[in]), %%xmm5\n\t" |
| 365 |
"movdqu 2*16(%[dst]), %%xmm2\n\t" |
| 366 |
"movdqu 3*16(%[dst]), %%xmm3\n\t" |
| 367 |
"pxor %%xmm4, %%xmm0\n\t" |
| 368 |
"pxor %%xmm5, %%xmm1\n\t" |
| 369 |
"movdqu 2*16(%[in]), %%xmm4\n\t" |
| 370 |
"movdqu 3*16(%[in]), %%xmm5\n\t" |
| 371 |
"movdqu %%xmm0, 0*16(%[dst])\n\t" |
| 372 |
"pxor %%xmm4, %%xmm2\n\t" |
| 373 |
"movdqu %%xmm1, 1*16(%[dst])\n\t" |
| 374 |
"pxor %%xmm5, %%xmm3\n\t" |
| 375 |
"movdqu %%xmm2, 2*16(%[dst])\n\t" |
| 376 |
"movdqu %%xmm3, 3*16(%[dst])\n\t" |
| 377 |
: |
| 378 |
: [dst] "r" (dst), [in] "r" (in) |
| 379 |
: "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "memory"); |
| 380 |
} |
| 381 |
|
| 382 |
static inline void unaligned_absorb_lanes64_4(u64 *dst, const byte *in) |
| 383 |
{ |
| 384 |
asm ("movdqu 0*16(%[dst]), %%xmm0\n\t" |
| 385 |
"movdqu 0*16(%[in]), %%xmm4\n\t" |
| 386 |
"movdqu 1*16(%[dst]), %%xmm1\n\t" |
| 387 |
"movdqu 1*16(%[in]), %%xmm5\n\t" |
| 388 |
"pxor %%xmm4, %%xmm0\n\t" |
| 389 |
"pxor %%xmm5, %%xmm1\n\t" |
| 390 |
"movdqu %%xmm0, 0*16(%[dst])\n\t" |
| 391 |
"movdqu %%xmm1, 1*16(%[dst])\n\t" |
| 392 |
: |
| 393 |
: [dst] "r" (dst), [in] "r" (in) |
| 394 |
: "xmm0", "xmm1", "xmm4", "xmm5", "memory"); |
| 395 |
} |
| 396 |
|
| 397 |
static inline void unaligned_absorb_lanes64_2(u64 *dst, const byte *in) |
| 398 |
{ |
| 399 |
asm ("movdqu 0*16(%[dst]), %%xmm0\n\t" |
| 400 |
"movdqu 0*16(%[in]), %%xmm4\n\t" |
| 401 |
"pxor %%xmm4, %%xmm0\n\t" |
| 402 |
"movdqu %%xmm0, 0*16(%[dst])\n\t" |
| 403 |
: |
| 404 |
: [dst] "r" (dst), [in] "r" (in) |
| 405 |
: "xmm0", "xmm4", "memory"); |
| 406 |
} |
| 407 |
|
| 408 |
#else /* __x86_64__ */ |
| 409 |
|
| 410 |
static inline void unaligned_absorb_lanes64_8(u64 *dst, const byte *in) |
| 411 |
{ |
| 412 |
dst[0] ^= buf_get_le64(in + 8 * 0); |
| 413 |
dst[1] ^= buf_get_le64(in + 8 * 1); |
| 414 |
dst[2] ^= buf_get_le64(in + 8 * 2); |
| 415 |
dst[3] ^= buf_get_le64(in + 8 * 3); |
| 416 |
dst[4] ^= buf_get_le64(in + 8 * 4); |
| 417 |
dst[5] ^= buf_get_le64(in + 8 * 5); |
| 418 |
dst[6] ^= buf_get_le64(in + 8 * 6); |
| 419 |
dst[7] ^= buf_get_le64(in + 8 * 7); |
| 420 |
} |
| 421 |
|
| 422 |
static inline void unaligned_absorb_lanes64_4(u64 *dst, const byte *in) |
| 423 |
{ |
| 424 |
dst[0] ^= buf_get_le64(in + 8 * 0); |
| 425 |
dst[1] ^= buf_get_le64(in + 8 * 1); |
| 426 |
dst[2] ^= buf_get_le64(in + 8 * 2); |
| 427 |
dst[3] ^= buf_get_le64(in + 8 * 3); |
| 428 |
} |
| 429 |
|
| 430 |
static inline void unaligned_absorb_lanes64_2(u64 *dst, const byte *in) |
| 431 |
{ |
| 432 |
dst[0] ^= buf_get_le64(in + 8 * 0); |
| 433 |
dst[1] ^= buf_get_le64(in + 8 * 1); |
| 434 |
} |
| 435 |
|
| 436 |
#endif /* !__x86_64__ */ |
| 437 |
|
| 438 |
static inline void unaligned_absorb_lanes64_1(u64 *dst, const byte *in) |
| 439 |
{ |
| 440 |
dst[0] ^= buf_get_le64(in + 8 * 0); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
static inline void absorb_lanes64_21(u64 *dst, const byte *in) |
| 445 |
{ |
| 446 |
#ifdef HAVE_ALIGNED_ABSORB_LANES64 |
| 447 |
if (LIKELY(((uintptr_t)in & 7) == 0)) |
| 448 |
{ |
| 449 |
aligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 450 |
aligned_absorb_lanes64_8(&dst[8], in); in += 8 * 8; |
| 451 |
aligned_absorb_lanes64_4(&dst[16], in); in += 8 * 4; |
| 452 |
aligned_absorb_lanes64_1(&dst[20], in); |
| 453 |
return; |
| 454 |
} |
| 455 |
#endif |
| 456 |
|
| 457 |
unaligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 458 |
unaligned_absorb_lanes64_8(&dst[8], in); in += 8 * 8; |
| 459 |
unaligned_absorb_lanes64_4(&dst[16], in); in += 8 * 4; |
| 460 |
unaligned_absorb_lanes64_1(&dst[20], in); |
| 461 |
} |
| 462 |
|
| 463 |
static inline void absorb_lanes64_18(u64 *dst, const byte *in) |
| 464 |
{ |
| 465 |
#ifdef HAVE_ALIGNED_ABSORB_LANES64 |
| 466 |
if (LIKELY(((uintptr_t)in & 7) == 0)) |
| 467 |
{ |
| 468 |
aligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 469 |
aligned_absorb_lanes64_8(&dst[8], in); in += 8 * 8; |
| 470 |
aligned_absorb_lanes64_2(&dst[16], in); |
| 471 |
return; |
| 472 |
} |
| 473 |
#endif |
| 474 |
|
| 475 |
unaligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 476 |
unaligned_absorb_lanes64_8(&dst[8], in); in += 8 * 8; |
| 477 |
unaligned_absorb_lanes64_2(&dst[16], in); |
| 478 |
} |
| 479 |
|
| 480 |
static inline void absorb_lanes64_17(u64 *dst, const byte *in) |
| 481 |
{ |
| 482 |
#ifdef HAVE_ALIGNED_ABSORB_LANES64 |
| 483 |
if (LIKELY(((uintptr_t)in & 7) == 0)) |
| 484 |
{ |
| 485 |
aligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 486 |
aligned_absorb_lanes64_8(&dst[8], in); in += 8 * 8; |
| 487 |
aligned_absorb_lanes64_1(&dst[16], in); |
| 488 |
return; |
| 489 |
} |
| 490 |
#endif |
| 491 |
|
| 492 |
unaligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 493 |
unaligned_absorb_lanes64_8(&dst[8], in); in += 8 * 8; |
| 494 |
unaligned_absorb_lanes64_1(&dst[16], in); |
| 495 |
} |
| 496 |
|
| 497 |
static inline void absorb_lanes64_13(u64 *dst, const byte *in) |
| 498 |
{ |
| 499 |
#ifdef HAVE_ALIGNED_ABSORB_LANES64 |
| 500 |
if (LIKELY(((uintptr_t)in & 7) == 0)) |
| 501 |
{ |
| 502 |
aligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 503 |
aligned_absorb_lanes64_4(&dst[8], in); in += 4 * 8; |
| 504 |
aligned_absorb_lanes64_1(&dst[12], in); |
| 505 |
return; |
| 506 |
} |
| 507 |
#endif |
| 508 |
|
| 509 |
unaligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 510 |
unaligned_absorb_lanes64_4(&dst[8], in); in += 4 * 8; |
| 511 |
unaligned_absorb_lanes64_1(&dst[12], in); |
| 512 |
} |
| 513 |
|
| 514 |
static inline void absorb_lanes64_9(u64 *dst, const byte *in) |
| 515 |
{ |
| 516 |
#ifdef HAVE_ALIGNED_ABSORB_LANES64 |
| 517 |
if (LIKELY(((uintptr_t)in & 7) == 0)) |
| 518 |
{ |
| 519 |
aligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 520 |
aligned_absorb_lanes64_1(&dst[8], in); |
| 521 |
return; |
| 522 |
} |
| 523 |
#endif |
| 524 |
|
| 525 |
unaligned_absorb_lanes64_8(&dst[0], in); in += 8 * 8; |
| 526 |
unaligned_absorb_lanes64_1(&dst[8], in); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
# define ANDN64(x, y) (~(x) & (y)) |
| 531 |
# define ROL64(x, n) (((x) << ((unsigned int)n & 63)) | \ |
| 532 |
((x) >> ((64 - (unsigned int)(n)) & 63))) |
| 533 |
|
| 534 |
# define KECCAK_F1600_PERMUTE_FUNC_NAME keccak_f1600_state_permute64 |
| 535 |
# define KECCAK_F1600_ABSORB_FUNC_NAME keccak_absorb_lanes64 |
| 536 |
# include "keccak_permute_64.h" |
| 537 |
|
| 538 |
# undef ANDN64 |
| 539 |
# undef ROL64 |
| 540 |
# undef KECCAK_F1600_PERMUTE_FUNC_NAME |
| 541 |
# undef KECCAK_F1600_ABSORB_FUNC_NAME |
| 542 |
|
| 543 |
static const keccak_ops_t keccak_generic64_ops = |
| 544 |
{ |
| 545 |
.permute = keccak_f1600_state_permute64, |
| 546 |
.absorb = keccak_absorb_lanes64, |
| 547 |
.extract = keccak_extract64, |
| 548 |
}; |
| 549 |
|
| 550 |
#endif /* USE_64BIT */ |
| 551 |
|
| 552 |
|
| 553 |
/* Construct 64-bit Intel SHLD implementation. */ |
| 554 |
#ifdef USE_64BIT_SHLD |
| 555 |
|
| 556 |
# define ANDN64(x, y) (~(x) & (y)) |
| 557 |
# define ROL64(x, n) ({ \ |
| 558 |
u64 tmp = (x); \ |
| 559 |
asm ("shldq %1, %0, %0" \ |
| 560 |
: "+r" (tmp) \ |
| 561 |
: "J" ((n) & 63) \ |
| 562 |
: "cc"); \ |
| 563 |
tmp; }) |
| 564 |
|
| 565 |
# define KECCAK_F1600_PERMUTE_FUNC_NAME keccak_f1600_state_permute64_shld |
| 566 |
# define KECCAK_F1600_ABSORB_FUNC_NAME keccak_absorb_lanes64_shld |
| 567 |
# include "keccak_permute_64.h" |
| 568 |
|
| 569 |
# undef ANDN64 |
| 570 |
# undef ROL64 |
| 571 |
# undef KECCAK_F1600_PERMUTE_FUNC_NAME |
| 572 |
# undef KECCAK_F1600_ABSORB_FUNC_NAME |
| 573 |
|
| 574 |
static const keccak_ops_t keccak_shld_64_ops = |
| 575 |
{ |
| 576 |
.permute = keccak_f1600_state_permute64_shld, |
| 577 |
.absorb = keccak_absorb_lanes64_shld, |
| 578 |
.extract = keccak_extract64, |
| 579 |
}; |
| 580 |
|
| 581 |
#endif /* USE_64BIT_SHLD */ |
| 582 |
|
| 583 |
|
| 584 |
/* Construct 64-bit Intel BMI2 implementation. */ |
| 585 |
#ifdef USE_64BIT_BMI2 |
| 586 |
|
| 587 |
# define ANDN64(x, y) ({ \ |
| 588 |
u64 tmp; \ |
| 589 |
asm ("andnq %2, %1, %0" \ |
| 590 |
: "=r" (tmp) \ |
| 591 |
: "r0" (x), "rm" (y)); \ |
| 592 |
tmp; }) |
| 593 |
|
| 594 |
# define ROL64(x, n) ({ \ |
| 595 |
u64 tmp; \ |
| 596 |
asm ("rorxq %2, %1, %0" \ |
| 597 |
: "=r" (tmp) \ |
| 598 |
: "rm0" (x), "J" (64 - ((n) & 63))); \ |
| 599 |
tmp; }) |
| 600 |
|
| 601 |
# define KECCAK_F1600_PERMUTE_FUNC_NAME keccak_f1600_state_permute64_bmi2 |
| 602 |
# define KECCAK_F1600_ABSORB_FUNC_NAME keccak_absorb_lanes64_bmi2 |
| 603 |
# include "keccak_permute_64.h" |
| 604 |
|
| 605 |
# undef ANDN64 |
| 606 |
# undef ROL64 |
| 607 |
# undef KECCAK_F1600_PERMUTE_FUNC_NAME |
| 608 |
# undef KECCAK_F1600_ABSORB_FUNC_NAME |
| 609 |
|
| 610 |
static const keccak_ops_t keccak_bmi2_64_ops = |
| 611 |
{ |
| 612 |
.permute = keccak_f1600_state_permute64_bmi2, |
| 613 |
.absorb = keccak_absorb_lanes64_bmi2, |
| 614 |
.extract = keccak_extract64, |
| 615 |
}; |
| 616 |
|
| 617 |
#endif /* USE_64BIT_BMI2 */ |
| 618 |
|
| 619 |
|
| 620 |
/* Construct 64-bit RISC-V Zbb extension implementation. */ |
| 621 |
#ifdef USE_RISCV_ZBB |
| 622 |
|
| 623 |
# define ANDN64(x, y) ({ \ |
| 624 |
u64 tmp; \ |
| 625 |
asm (".option push;\n\t" \ |
| 626 |
".option arch, +zbb;\n\t" \ |
| 627 |
"andn %0, %1, %2;\n\t" \ |
| 628 |
".option pop;\n\t" \ |
| 629 |
: "=r" (tmp) \ |
| 630 |
: "r" (y), "r" (x)); \ |
| 631 |
tmp; }) |
| 632 |
|
| 633 |
# define ROL64(x, n) ({ \ |
| 634 |
u64 tmp; \ |
| 635 |
asm (".option push;\n\t" \ |
| 636 |
".option arch, +zbb;\n\t" \ |
| 637 |
"rori %0, %1, %2;\n\t" \ |
| 638 |
".option pop;\n\t" \ |
| 639 |
: "=r" (tmp) \ |
| 640 |
: "r" (x), "I" ((64 - n) & 63)); \ |
| 641 |
tmp; }) |
| 642 |
|
| 643 |
# define KECCAK_F1600_PERMUTE_FUNC_NAME keccak_f1600_state_permute64_riscv_zbb |
| 644 |
# define KECCAK_F1600_ABSORB_FUNC_NAME keccak_absorb_lanes64_riscv_zbb |
| 645 |
# include "keccak_permute_64.h" |
| 646 |
|
| 647 |
# undef ANDN64 |
| 648 |
# undef ROL64 |
| 649 |
# undef KECCAK_F1600_PERMUTE_FUNC_NAME |
| 650 |
# undef KECCAK_F1600_ABSORB_FUNC_NAME |
| 651 |
|
| 652 |
static const keccak_ops_t keccak_riscv_zbb_64_ops = |
| 653 |
{ |
| 654 |
.permute = keccak_f1600_state_permute64_riscv_zbb, |
| 655 |
.absorb = keccak_absorb_lanes64_riscv_zbb, |
| 656 |
.extract = keccak_extract64, |
| 657 |
}; |
| 658 |
|
| 659 |
#endif /* USE_RISCV_ZBB */ |
| 660 |
|
| 661 |
|
| 662 |
/* 64-bit Intel AVX512 implementation. */ |
| 663 |
#ifdef USE_64BIT_AVX512 |
| 664 |
|
| 665 |
extern ASM_FUNC_ABI unsigned int |
| 666 |
_gcry_keccak_f1600_state_permute64_avx512(u64 *state, const u64 *rconst); |
| 667 |
|
| 668 |
extern ASM_FUNC_ABI unsigned int |
| 669 |
_gcry_keccak_absorb_blocks_avx512(u64 *state, const u64 *rconst, |
| 670 |
const byte *lanes, u64 nlanes, |
| 671 |
u64 blocklanes, u64 *new_lanes); |
| 672 |
|
| 673 |
static unsigned int |
| 674 |
keccak_f1600_state_permute64_avx512(KECCAK_STATE *hd) |
| 675 |
{ |
| 676 |
return _gcry_keccak_f1600_state_permute64_avx512 ( |
| 677 |
hd->u.state64, _gcry_keccak_round_consts_64bit); |
| 678 |
} |
| 679 |
|
| 680 |
static unsigned int |
| 681 |
keccak_absorb_lanes64_avx512(KECCAK_STATE *hd, int pos, const byte *lanes, |
| 682 |
size_t nlanes, int blocklanes) |
| 683 |
{ |
| 684 |
while (nlanes) |
| 685 |
{ |
| 686 |
if (pos == 0 && blocklanes > 0 && nlanes >= (size_t)blocklanes) |
| 687 |
{ |
| 688 |
/* Get new pointer through u64 variable for "x32" compatibility. */ |
| 689 |
u64 new_lanes; |
| 690 |
nlanes = _gcry_keccak_absorb_blocks_avx512 ( |
| 691 |
hd->u.state64, _gcry_keccak_round_consts_64bit, |
| 692 |
lanes, nlanes, blocklanes, &new_lanes); |
| 693 |
lanes = (const byte *)(uintptr_t)new_lanes; |
| 694 |
} |
| 695 |
|
| 696 |
while (nlanes) |
| 697 |
{ |
| 698 |
hd->u.state64[pos] ^= buf_get_le64 (lanes); |
| 699 |
lanes += 8; |
| 700 |
nlanes--; |
| 701 |
|
| 702 |
if (++pos == blocklanes) |
| 703 |
{ |
| 704 |
keccak_f1600_state_permute64_avx512 (hd); |
| 705 |
pos = 0; |
| 706 |
break; |
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
return 0; |
| 712 |
} |
| 713 |
|
| 714 |
static const keccak_ops_t keccak_avx512_64_ops = |
| 715 |
{ |
| 716 |
.permute = keccak_f1600_state_permute64_avx512, |
| 717 |
.absorb = keccak_absorb_lanes64_avx512, |
| 718 |
.extract = keccak_extract64, |
| 719 |
}; |
| 720 |
|
| 721 |
#endif /* USE_64BIT_AVX512 */ |
| 722 |
|
| 723 |
|
| 724 |
/* 64-bit ARMv7/NEON implementation. */ |
| 725 |
#ifdef USE_64BIT_ARM_NEON |
| 726 |
|
| 727 |
unsigned int _gcry_keccak_permute_armv7_neon(u64 *state); |
| 728 |
unsigned int _gcry_keccak_absorb_lanes64_armv7_neon(u64 *state, int pos, |
| 729 |
const byte *lanes, |
| 730 |
size_t nlanes, |
| 731 |
int blocklanes); |
| 732 |
|
| 733 |
static unsigned int keccak_permute64_armv7_neon(KECCAK_STATE *hd) |
| 734 |
{ |
| 735 |
return _gcry_keccak_permute_armv7_neon(hd->u.state64); |
| 736 |
} |
| 737 |
|
| 738 |
static unsigned int |
| 739 |
keccak_absorb_lanes64_armv7_neon(KECCAK_STATE *hd, int pos, const byte *lanes, |
| 740 |
size_t nlanes, int blocklanes) |
| 741 |
{ |
| 742 |
if (blocklanes < 0) |
| 743 |
{ |
| 744 |
/* blocklanes == -1, permutationless absorb from keccak_final. */ |
| 745 |
|
| 746 |
while (nlanes) |
| 747 |
{ |
| 748 |
hd->u.state64[pos] ^= buf_get_le64(lanes); |
| 749 |
lanes += 8; |
| 750 |
nlanes--; |
| 751 |
} |
| 752 |
|
| 753 |
return 0; |
| 754 |
} |
| 755 |
else |
| 756 |
{ |
| 757 |
return _gcry_keccak_absorb_lanes64_armv7_neon(hd->u.state64, pos, lanes, |
| 758 |
nlanes, blocklanes); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
static const keccak_ops_t keccak_armv7_neon_64_ops = |
| 763 |
{ |
| 764 |
.permute = keccak_permute64_armv7_neon, |
| 765 |
.absorb = keccak_absorb_lanes64_armv7_neon, |
| 766 |
.extract = keccak_extract64, |
| 767 |
}; |
| 768 |
|
| 769 |
#endif /* USE_64BIT_ARM_NEON */ |
| 770 |
|
| 771 |
|
| 772 |
/* Construct generic 32-bit implementation. */ |
| 773 |
#ifdef USE_32BIT |
| 774 |
|
| 775 |
# define ANDN32(x, y) (~(x) & (y)) |
| 776 |
# define ROL32(x, n) (((x) << ((unsigned int)n & 31)) | \ |
| 777 |
((x) >> ((32 - (unsigned int)(n)) & 31))) |
| 778 |
|
| 779 |
# define KECCAK_F1600_PERMUTE_FUNC_NAME keccak_f1600_state_permute32bi |
| 780 |
# include "keccak_permute_32.h" |
| 781 |
|
| 782 |
# undef ANDN32 |
| 783 |
# undef ROL32 |
| 784 |
# undef KECCAK_F1600_PERMUTE_FUNC_NAME |
| 785 |
|
| 786 |
static unsigned int |
| 787 |
keccak_absorb_lanes32bi(KECCAK_STATE *hd, int pos, const byte *lanes, |
| 788 |
size_t nlanes, int blocklanes) |
| 789 |
{ |
| 790 |
unsigned int burn = 0; |
| 791 |
|
| 792 |
while (nlanes) |
| 793 |
{ |
| 794 |
keccak_absorb_lane32bi(&hd->u.state32bi[pos * 2], |
| 795 |
buf_get_le32(lanes + 0), |
| 796 |
buf_get_le32(lanes + 4)); |
| 797 |
lanes += 8; |
| 798 |
nlanes--; |
| 799 |
|
| 800 |
if (++pos == blocklanes) |
| 801 |
{ |
| 802 |
burn = keccak_f1600_state_permute32bi(hd); |
| 803 |
pos = 0; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
return burn; |
| 808 |
} |
| 809 |
|
| 810 |
static const keccak_ops_t keccak_generic32bi_ops = |
| 811 |
{ |
| 812 |
.permute = keccak_f1600_state_permute32bi, |
| 813 |
.absorb = keccak_absorb_lanes32bi, |
| 814 |
.extract = keccak_extract32bi, |
| 815 |
}; |
| 816 |
|
| 817 |
#endif /* USE_32BIT */ |
| 818 |
|
| 819 |
|
| 820 |
/* Construct 32-bit Intel BMI2 implementation. */ |
| 821 |
#ifdef USE_32BIT_BMI2 |
| 822 |
|
| 823 |
# define ANDN32(x, y) ({ \ |
| 824 |
u32 tmp; \ |
| 825 |
asm ("andnl %2, %1, %0" \ |
| 826 |
: "=r" (tmp) \ |
| 827 |
: "r0" (x), "rm" (y)); \ |
| 828 |
tmp; }) |
| 829 |
|
| 830 |
# define ROL32(x, n) ({ \ |
| 831 |
u32 tmp; \ |
| 832 |
asm ("rorxl %2, %1, %0" \ |
| 833 |
: "=r" (tmp) \ |
| 834 |
: "rm0" (x), "J" (32 - ((n) & 31))); \ |
| 835 |
tmp; }) |
| 836 |
|
| 837 |
# define KECCAK_F1600_PERMUTE_FUNC_NAME keccak_f1600_state_permute32bi_bmi2 |
| 838 |
# include "keccak_permute_32.h" |
| 839 |
|
| 840 |
# undef ANDN32 |
| 841 |
# undef ROL32 |
| 842 |
# undef KECCAK_F1600_PERMUTE_FUNC_NAME |
| 843 |
|
| 844 |
static inline u32 pext(u32 x, u32 mask) |
| 845 |
{ |
| 846 |
u32 tmp; |
| 847 |
asm ("pextl %2, %1, %0" : "=r" (tmp) : "r0" (x), "rm" (mask)); |
| 848 |
return tmp; |
| 849 |
} |
| 850 |
|
| 851 |
static inline u32 pdep(u32 x, u32 mask) |
| 852 |
{ |
| 853 |
u32 tmp; |
| 854 |
asm ("pdepl %2, %1, %0" : "=r" (tmp) : "r0" (x), "rm" (mask)); |
| 855 |
return tmp; |
| 856 |
} |
| 857 |
|
| 858 |
static inline void |
| 859 |
keccak_absorb_lane32bi_bmi2(u32 *lane, u32 x0, u32 x1) |
| 860 |
{ |
| 861 |
x0 = pdep(pext(x0, 0x55555555), 0x0000ffff) | (pext(x0, 0xaaaaaaaa) << 16); |
| 862 |
x1 = pdep(pext(x1, 0x55555555), 0x0000ffff) | (pext(x1, 0xaaaaaaaa) << 16); |
| 863 |
|
| 864 |
lane[0] ^= (x0 & 0x0000FFFFUL) + (x1 << 16); |
| 865 |
lane[1] ^= (x0 >> 16) + (x1 & 0xFFFF0000UL); |
| 866 |
} |
| 867 |
|
| 868 |
static unsigned int |
| 869 |
keccak_absorb_lanes32bi_bmi2(KECCAK_STATE *hd, int pos, const byte *lanes, |
| 870 |
size_t nlanes, int blocklanes) |
| 871 |
{ |
| 872 |
unsigned int burn = 0; |
| 873 |
|
| 874 |
while (nlanes) |
| 875 |
{ |
| 876 |
keccak_absorb_lane32bi_bmi2(&hd->u.state32bi[pos * 2], |
| 877 |
buf_get_le32(lanes + 0), |
| 878 |
buf_get_le32(lanes + 4)); |
| 879 |
lanes += 8; |
| 880 |
nlanes--; |
| 881 |
|
| 882 |
if (++pos == blocklanes) |
| 883 |
{ |
| 884 |
burn = keccak_f1600_state_permute32bi_bmi2(hd); |
| 885 |
pos = 0; |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
return burn; |
| 890 |
} |
| 891 |
|
| 892 |
static unsigned int |
| 893 |
keccak_extract32bi_bmi2(KECCAK_STATE *hd, unsigned int pos, byte *outbuf, |
| 894 |
unsigned int outlen) |
| 895 |
{ |
| 896 |
unsigned int i; |
| 897 |
u32 x0; |
| 898 |
u32 x1; |
| 899 |
u32 t; |
| 900 |
|
| 901 |
/* NOTE: when pos == 0, hd and outbuf may point to same memory (SHA-3). */ |
| 902 |
|
| 903 |
for (i = pos; i < pos + outlen / 8 + !!(outlen % 8); i++) |
| 904 |
{ |
| 905 |
x0 = hd->u.state32bi[i * 2 + 0]; |
| 906 |
x1 = hd->u.state32bi[i * 2 + 1]; |
| 907 |
|
| 908 |
t = (x0 & 0x0000FFFFUL) + (x1 << 16); |
| 909 |
x1 = (x0 >> 16) + (x1 & 0xFFFF0000UL); |
| 910 |
x0 = t; |
| 911 |
|
| 912 |
x0 = pdep(pext(x0, 0xffff0001), 0xaaaaaaab) | pdep(x0 >> 1, 0x55555554); |
| 913 |
x1 = pdep(pext(x1, 0xffff0001), 0xaaaaaaab) | pdep(x1 >> 1, 0x55555554); |
| 914 |
|
| 915 |
buf_put_le32(&outbuf[0], x0); |
| 916 |
buf_put_le32(&outbuf[4], x1); |
| 917 |
outbuf += 8; |
| 918 |
} |
| 919 |
|
| 920 |
return 0; |
| 921 |
} |
| 922 |
|
| 923 |
static const keccak_ops_t keccak_bmi2_32bi_ops = |
| 924 |
{ |
| 925 |
.permute = keccak_f1600_state_permute32bi_bmi2, |
| 926 |
.absorb = keccak_absorb_lanes32bi_bmi2, |
| 927 |
.extract = keccak_extract32bi_bmi2, |
| 928 |
}; |
| 929 |
|
| 930 |
#endif /* USE_32BIT_BMI2 */ |
| 931 |
|
| 932 |
|
| 933 |
#ifdef USE_S390X_CRYPTO |
| 934 |
#include "asm-inline-s390x.h" |
| 935 |
|
| 936 |
static inline void |
| 937 |
keccak_bwrite_s390x (void *context, const byte *in, size_t inlen) |
| 938 |
{ |
| 939 |
KECCAK_CONTEXT *ctx = context; |
| 940 |
|
| 941 |
/* Write full-blocks. */ |
| 942 |
kimd_execute (ctx->kimd_func, &ctx->state, in, inlen); |
| 943 |
return; |
| 944 |
} |
| 945 |
|
| 946 |
static inline void |
| 947 |
keccak_final_s390x (void *context) |
| 948 |
{ |
| 949 |
KECCAK_CONTEXT *ctx = context; |
| 950 |
|
| 951 |
if (ctx->suffix == SHA3_DELIMITED_SUFFIX) |
| 952 |
{ |
| 953 |
klmd_execute (ctx->kimd_func, &ctx->state, ctx->buf, ctx->count); |
| 954 |
} |
| 955 |
else |
| 956 |
{ |
| 957 |
gcry_assert(ctx->suffix == SHAKE_DELIMITED_SUFFIX); |
| 958 |
|
| 959 |
klmd_shake_execute (ctx->kimd_func, &ctx->state, NULL, 0, ctx->buf, |
| 960 |
ctx->count); |
| 961 |
ctx->count = 0; |
| 962 |
ctx->buf_pos = 0; |
| 963 |
} |
| 964 |
|
| 965 |
return; |
| 966 |
} |
| 967 |
|
| 968 |
static inline void |
| 969 |
keccak_bextract_s390x (void *context, byte *out, size_t outlen) |
| 970 |
{ |
| 971 |
KECCAK_CONTEXT *ctx = context; |
| 972 |
|
| 973 |
/* Extract full-blocks. */ |
| 974 |
klmd_shake_execute (ctx->kimd_func | KLMD_PADDING_STATE, &ctx->state, |
| 975 |
out, outlen, NULL, 0); |
| 976 |
return; |
| 977 |
} |
| 978 |
|
| 979 |
static void |
| 980 |
keccak_write_s390x (void *context, const byte *inbuf, size_t inlen) |
| 981 |
{ |
| 982 |
KECCAK_CONTEXT *hd = context; |
| 983 |
const size_t blocksize = hd->blocksize; |
| 984 |
size_t inblocks; |
| 985 |
size_t copylen; |
| 986 |
|
| 987 |
while (hd->count) |
| 988 |
{ |
| 989 |
if (hd->count == blocksize) /* Flush the buffer. */ |
| 990 |
{ |
| 991 |
keccak_bwrite_s390x (hd, hd->buf, blocksize); |
| 992 |
hd->count = 0; |
| 993 |
} |
| 994 |
else |
| 995 |
{ |
| 996 |
copylen = inlen; |
| 997 |
if (copylen > blocksize - hd->count) |
| 998 |
copylen = blocksize - hd->count; |
| 999 |
|
| 1000 |
if (copylen == 0) |
| 1001 |
break; |
| 1002 |
|
| 1003 |
buf_cpy (&hd->buf[hd->count], inbuf, copylen); |
| 1004 |
hd->count += copylen; |
| 1005 |
inbuf += copylen; |
| 1006 |
inlen -= copylen; |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
if (inlen == 0) |
| 1011 |
return; |
| 1012 |
|
| 1013 |
if (inlen >= blocksize) |
| 1014 |
{ |
| 1015 |
inblocks = inlen / blocksize; |
| 1016 |
keccak_bwrite_s390x (hd, inbuf, inblocks * blocksize); |
| 1017 |
hd->count = 0; |
| 1018 |
inlen -= inblocks * blocksize; |
| 1019 |
inbuf += inblocks * blocksize; |
| 1020 |
} |
| 1021 |
|
| 1022 |
if (inlen) |
| 1023 |
{ |
| 1024 |
buf_cpy (hd->buf, inbuf, inlen); |
| 1025 |
hd->count = inlen; |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
static void |
| 1030 |
keccak_extract_s390x (void *context, void *outbuf_arg, size_t outlen) |
| 1031 |
{ |
| 1032 |
KECCAK_CONTEXT *hd = context; |
| 1033 |
const size_t blocksize = hd->blocksize; |
| 1034 |
byte *outbuf = outbuf_arg; |
| 1035 |
|
| 1036 |
while (outlen) |
| 1037 |
{ |
| 1038 |
gcry_assert(hd->count == 0 || hd->buf_pos < hd->count); |
| 1039 |
|
| 1040 |
if (hd->buf_pos < hd->count && outlen) |
| 1041 |
{ |
| 1042 |
size_t copylen = hd->count - hd->buf_pos; |
| 1043 |
|
| 1044 |
if (copylen > outlen) |
| 1045 |
copylen = outlen; |
| 1046 |
|
| 1047 |
buf_cpy (outbuf, &hd->buf[hd->buf_pos], copylen); |
| 1048 |
|
| 1049 |
outbuf += copylen; |
| 1050 |
outlen -= copylen; |
| 1051 |
hd->buf_pos += copylen; |
| 1052 |
} |
| 1053 |
|
| 1054 |
if (hd->buf_pos == hd->count) |
| 1055 |
{ |
| 1056 |
hd->buf_pos = 0; |
| 1057 |
hd->count = 0; |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (outlen == 0) |
| 1061 |
return; |
| 1062 |
|
| 1063 |
if (outlen >= blocksize) |
| 1064 |
{ |
| 1065 |
size_t outblocks = outlen / blocksize; |
| 1066 |
|
| 1067 |
keccak_bextract_s390x (context, outbuf, outblocks * blocksize); |
| 1068 |
|
| 1069 |
outlen -= outblocks * blocksize; |
| 1070 |
outbuf += outblocks * blocksize; |
| 1071 |
|
| 1072 |
if (outlen == 0) |
| 1073 |
return; |
| 1074 |
} |
| 1075 |
|
| 1076 |
keccak_bextract_s390x (context, hd->buf, blocksize); |
| 1077 |
hd->count = blocksize; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
#endif /* USE_S390X_CRYPTO */ |
| 1081 |
|
| 1082 |
|
| 1083 |
static void |
| 1084 |
keccak_write (void *context, const void *inbuf_arg, size_t inlen) |
| 1085 |
{ |
| 1086 |
KECCAK_CONTEXT *ctx = context; |
| 1087 |
const size_t bsize = ctx->blocksize; |
| 1088 |
const size_t blocklanes = bsize / 8; |
| 1089 |
const byte *inbuf = inbuf_arg; |
| 1090 |
unsigned int nburn, burn = 0; |
| 1091 |
unsigned int count, i; |
| 1092 |
unsigned int pos; |
| 1093 |
size_t nlanes; |
| 1094 |
|
| 1095 |
#ifdef USE_S390X_CRYPTO |
| 1096 |
if (ctx->kimd_func) |
| 1097 |
{ |
| 1098 |
keccak_write_s390x (context, inbuf, inlen); |
| 1099 |
return; |
| 1100 |
} |
| 1101 |
#endif |
| 1102 |
|
| 1103 |
count = ctx->count; |
| 1104 |
|
| 1105 |
if (inlen && (count % 8)) |
| 1106 |
{ |
| 1107 |
byte lane[8] = { 0, }; |
| 1108 |
|
| 1109 |
/* Complete absorbing partial input lane. */ |
| 1110 |
|
| 1111 |
pos = count / 8; |
| 1112 |
|
| 1113 |
for (i = count % 8; inlen && i < 8; i++) |
| 1114 |
{ |
| 1115 |
lane[i] = *inbuf++; |
| 1116 |
inlen--; |
| 1117 |
count++; |
| 1118 |
} |
| 1119 |
|
| 1120 |
if (count == bsize) |
| 1121 |
count = 0; |
| 1122 |
|
| 1123 |
nburn = ctx->ops->absorb(&ctx->state, pos, lane, 1, |
| 1124 |
(count % 8) ? -1 : blocklanes); |
| 1125 |
burn = nburn > burn ? nburn : burn; |
| 1126 |
} |
| 1127 |
|
| 1128 |
/* Absorb full input lanes. */ |
| 1129 |
|
| 1130 |
pos = count / 8; |
| 1131 |
nlanes = inlen / 8; |
| 1132 |
if (nlanes > 0) |
| 1133 |
{ |
| 1134 |
nburn = ctx->ops->absorb(&ctx->state, pos, inbuf, nlanes, blocklanes); |
| 1135 |
burn = nburn > burn ? nburn : burn; |
| 1136 |
inlen -= nlanes * 8; |
| 1137 |
inbuf += nlanes * 8; |
| 1138 |
count = ((size_t) count + nlanes * 8) % bsize; |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (inlen) |
| 1142 |
{ |
| 1143 |
byte lane[8] = { 0, }; |
| 1144 |
|
| 1145 |
/* Absorb remaining partial input lane. */ |
| 1146 |
|
| 1147 |
pos = count / 8; |
| 1148 |
|
| 1149 |
for (i = count % 8; inlen && i < 8; i++) |
| 1150 |
{ |
| 1151 |
lane[i] = *inbuf++; |
| 1152 |
inlen--; |
| 1153 |
count++; |
| 1154 |
} |
| 1155 |
|
| 1156 |
nburn = ctx->ops->absorb(&ctx->state, pos, lane, 1, -1); |
| 1157 |
burn = nburn > burn ? nburn : burn; |
| 1158 |
|
| 1159 |
gcry_assert(count < bsize); |
| 1160 |
} |
| 1161 |
|
| 1162 |
ctx->count = count; |
| 1163 |
|
| 1164 |
if (burn) |
| 1165 |
_gcry_burn_stack (burn); |
| 1166 |
} |
| 1167 |
|
| 1168 |
|
| 1169 |
static void |
| 1170 |
keccak_init (int algo, void *context, unsigned int flags) |
| 1171 |
{ |
| 1172 |
KECCAK_CONTEXT *ctx = context; |
| 1173 |
KECCAK_STATE *hd = &ctx->state; |
| 1174 |
unsigned int features = _gcry_get_hw_features (); |
| 1175 |
|
| 1176 |
(void)flags; |
| 1177 |
(void)features; |
| 1178 |
|
| 1179 |
memset (hd, 0, sizeof *hd); |
| 1180 |
|
| 1181 |
ctx->count = 0; |
| 1182 |
ctx->shake_in_extract_mode = 0; |
| 1183 |
ctx->shake_in_read_mode = 0; |
| 1184 |
|
| 1185 |
/* Select generic implementation. */ |
| 1186 |
#ifdef USE_64BIT |
| 1187 |
ctx->ops = &keccak_generic64_ops; |
| 1188 |
#elif defined USE_32BIT |
| 1189 |
ctx->ops = &keccak_generic32bi_ops; |
| 1190 |
#endif |
| 1191 |
|
| 1192 |
/* Select optimized implementation based in hw features. */ |
| 1193 |
if (0) {} |
| 1194 |
#ifdef USE_64BIT_AVX512 |
| 1195 |
else if (features & HWF_INTEL_AVX512) |
| 1196 |
ctx->ops = &keccak_avx512_64_ops; |
| 1197 |
#endif |
| 1198 |
#ifdef USE_64BIT_ARM_NEON |
| 1199 |
else if (features & HWF_ARM_NEON) |
| 1200 |
ctx->ops = &keccak_armv7_neon_64_ops; |
| 1201 |
#endif |
| 1202 |
#ifdef USE_64BIT_BMI2 |
| 1203 |
else if (features & HWF_INTEL_BMI2) |
| 1204 |
ctx->ops = &keccak_bmi2_64_ops; |
| 1205 |
#endif |
| 1206 |
#ifdef USE_32BIT_BMI2 |
| 1207 |
else if (features & HWF_INTEL_BMI2) |
| 1208 |
ctx->ops = &keccak_bmi2_32bi_ops; |
| 1209 |
#endif |
| 1210 |
#ifdef USE_64BIT_SHLD |
| 1211 |
else if (features & HWF_INTEL_FAST_SHLD) |
| 1212 |
ctx->ops = &keccak_shld_64_ops; |
| 1213 |
#endif |
| 1214 |
#ifdef USE_RISCV_ZBB |
| 1215 |
else if ((features & HWF_RISCV_IMAFDC) && (features & HWF_RISCV_ZBB)) |
| 1216 |
ctx->ops = &keccak_riscv_zbb_64_ops; |
| 1217 |
#endif |
| 1218 |
|
| 1219 |
/* Set input block size, in Keccak terms this is called 'rate'. */ |
| 1220 |
|
| 1221 |
switch (algo) |
| 1222 |
{ |
| 1223 |
case GCRY_MD_SHA3_224: |
| 1224 |
ctx->suffix = SHA3_DELIMITED_SUFFIX; |
| 1225 |
ctx->blocksize = 1152 / 8; |
| 1226 |
ctx->outlen = 224 / 8; |
| 1227 |
break; |
| 1228 |
case GCRY_MD_SHA3_256: |
| 1229 |
ctx->suffix = SHA3_DELIMITED_SUFFIX; |
| 1230 |
ctx->blocksize = 1088 / 8; |
| 1231 |
ctx->outlen = 256 / 8; |
| 1232 |
break; |
| 1233 |
case GCRY_MD_SHA3_384: |
| 1234 |
ctx->suffix = SHA3_DELIMITED_SUFFIX; |
| 1235 |
ctx->blocksize = 832 / 8; |
| 1236 |
ctx->outlen = 384 / 8; |
| 1237 |
break; |
| 1238 |
case GCRY_MD_SHA3_512: |
| 1239 |
ctx->suffix = SHA3_DELIMITED_SUFFIX; |
| 1240 |
ctx->blocksize = 576 / 8; |
| 1241 |
ctx->outlen = 512 / 8; |
| 1242 |
break; |
| 1243 |
case GCRY_MD_CSHAKE128: |
| 1244 |
case GCRY_MD_SHAKE128: |
| 1245 |
ctx->suffix = SHAKE_DELIMITED_SUFFIX; |
| 1246 |
ctx->blocksize = 1344 / 8; |
| 1247 |
ctx->outlen = 256 / 8; |
| 1248 |
break; |
| 1249 |
case GCRY_MD_CSHAKE256: |
| 1250 |
case GCRY_MD_SHAKE256: |
| 1251 |
ctx->suffix = SHAKE_DELIMITED_SUFFIX; |
| 1252 |
ctx->blocksize = 1088 / 8; |
| 1253 |
ctx->outlen = 512 / 8; |
| 1254 |
break; |
| 1255 |
default: |
| 1256 |
BUG(); |
| 1257 |
} |
| 1258 |
|
| 1259 |
#ifdef USE_S390X_CRYPTO |
| 1260 |
ctx->kimd_func = 0; |
| 1261 |
if ((features & HWF_S390X_MSA) != 0) |
| 1262 |
{ |
| 1263 |
unsigned int kimd_func = 0; |
| 1264 |
|
| 1265 |
switch (algo) |
| 1266 |
{ |
| 1267 |
case GCRY_MD_SHA3_224: |
| 1268 |
kimd_func = KMID_FUNCTION_SHA3_224; |
| 1269 |
break; |
| 1270 |
case GCRY_MD_SHA3_256: |
| 1271 |
kimd_func = KMID_FUNCTION_SHA3_256; |
| 1272 |
break; |
| 1273 |
case GCRY_MD_SHA3_384: |
| 1274 |
kimd_func = KMID_FUNCTION_SHA3_384; |
| 1275 |
break; |
| 1276 |
case GCRY_MD_SHA3_512: |
| 1277 |
kimd_func = KMID_FUNCTION_SHA3_512; |
| 1278 |
break; |
| 1279 |
case GCRY_MD_CSHAKE128: |
| 1280 |
case GCRY_MD_SHAKE128: |
| 1281 |
kimd_func = KMID_FUNCTION_SHAKE128; |
| 1282 |
break; |
| 1283 |
case GCRY_MD_CSHAKE256: |
| 1284 |
case GCRY_MD_SHAKE256: |
| 1285 |
kimd_func = KMID_FUNCTION_SHAKE256; |
| 1286 |
break; |
| 1287 |
} |
| 1288 |
|
| 1289 |
if ((kimd_query () & km_function_to_mask (kimd_func)) && |
| 1290 |
(klmd_query () & km_function_to_mask (kimd_func))) |
| 1291 |
{ |
| 1292 |
ctx->kimd_func = kimd_func; |
| 1293 |
} |
| 1294 |
} |
| 1295 |
#endif |
| 1296 |
} |
| 1297 |
|
| 1298 |
static void |
| 1299 |
sha3_224_init (void *context, unsigned int flags) |
| 1300 |
{ |
| 1301 |
keccak_init (GCRY_MD_SHA3_224, context, flags); |
| 1302 |
} |
| 1303 |
|
| 1304 |
static void |
| 1305 |
sha3_256_init (void *context, unsigned int flags) |
| 1306 |
{ |
| 1307 |
keccak_init (GCRY_MD_SHA3_256, context, flags); |
| 1308 |
} |
| 1309 |
|
| 1310 |
static void |
| 1311 |
sha3_384_init (void *context, unsigned int flags) |
| 1312 |
{ |
| 1313 |
keccak_init (GCRY_MD_SHA3_384, context, flags); |
| 1314 |
} |
| 1315 |
|
| 1316 |
static void |
| 1317 |
sha3_512_init (void *context, unsigned int flags) |
| 1318 |
{ |
| 1319 |
keccak_init (GCRY_MD_SHA3_512, context, flags); |
| 1320 |
} |
| 1321 |
|
| 1322 |
static void |
| 1323 |
shake128_init (void *context, unsigned int flags) |
| 1324 |
{ |
| 1325 |
keccak_init (GCRY_MD_SHAKE128, context, flags); |
| 1326 |
} |
| 1327 |
|
| 1328 |
static void |
| 1329 |
shake256_init (void *context, unsigned int flags) |
| 1330 |
{ |
| 1331 |
keccak_init (GCRY_MD_SHAKE256, context, flags); |
| 1332 |
} |
| 1333 |
|
| 1334 |
/* The routine final terminates the computation and |
| 1335 |
* returns the digest. |
| 1336 |
* The handle is prepared for a new cycle, but adding bytes to the |
| 1337 |
* handle will the destroy the returned buffer. |
| 1338 |
* Returns: 64 bytes representing the digest. When used for sha384, |
| 1339 |
* we take the leftmost 48 of those bytes. |
| 1340 |
*/ |
| 1341 |
static void |
| 1342 |
keccak_final (void *context) |
| 1343 |
{ |
| 1344 |
KECCAK_CONTEXT *ctx = context; |
| 1345 |
KECCAK_STATE *hd = &ctx->state; |
| 1346 |
const size_t bsize = ctx->blocksize; |
| 1347 |
const byte suffix = ctx->suffix; |
| 1348 |
unsigned int nburn, burn = 0; |
| 1349 |
unsigned int lastbytes; |
| 1350 |
byte lane[8]; |
| 1351 |
|
| 1352 |
#ifdef USE_S390X_CRYPTO |
| 1353 |
if (ctx->kimd_func) |
| 1354 |
{ |
| 1355 |
keccak_final_s390x (context); |
| 1356 |
return; |
| 1357 |
} |
| 1358 |
#endif |
| 1359 |
|
| 1360 |
lastbytes = ctx->count; |
| 1361 |
|
| 1362 |
/* Do the padding and switch to the squeezing phase */ |
| 1363 |
|
| 1364 |
/* Absorb the last few bits and add the first bit of padding (which |
| 1365 |
coincides with the delimiter in delimited suffix) */ |
| 1366 |
buf_put_le64(lane, (u64)suffix << ((lastbytes % 8) * 8)); |
| 1367 |
nburn = ctx->ops->absorb(&ctx->state, lastbytes / 8, lane, 1, -1); |
| 1368 |
burn = nburn > burn ? nburn : burn; |
| 1369 |
|
| 1370 |
/* Add the second bit of padding. */ |
| 1371 |
buf_put_le64(lane, (u64)0x80 << (((bsize - 1) % 8) * 8)); |
| 1372 |
nburn = ctx->ops->absorb(&ctx->state, (bsize - 1) / 8, lane, 1, -1); |
| 1373 |
burn = nburn > burn ? nburn : burn; |
| 1374 |
|
| 1375 |
if (suffix == SHA3_DELIMITED_SUFFIX) |
| 1376 |
{ |
| 1377 |
/* Switch to the squeezing phase. */ |
| 1378 |
nburn = ctx->ops->permute(hd); |
| 1379 |
burn = nburn > burn ? nburn : burn; |
| 1380 |
|
| 1381 |
/* Squeeze out the SHA3 digest. */ |
| 1382 |
nburn = ctx->ops->extract(hd, 0, (void *)hd, ctx->outlen); |
| 1383 |
burn = nburn > burn ? nburn : burn; |
| 1384 |
} |
| 1385 |
else |
| 1386 |
{ |
| 1387 |
/* Output for SHAKE can now be read with md_extract(). */ |
| 1388 |
|
| 1389 |
ctx->count = 0; |
| 1390 |
} |
| 1391 |
|
| 1392 |
wipememory(lane, sizeof(lane)); |
| 1393 |
if (burn) |
| 1394 |
_gcry_burn_stack (burn); |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
static byte * |
| 1399 |
keccak_read (void *context) |
| 1400 |
{ |
| 1401 |
KECCAK_CONTEXT *ctx = (KECCAK_CONTEXT *) context; |
| 1402 |
KECCAK_STATE *hd = &ctx->state; |
| 1403 |
return (byte *)&hd->u; |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
static gcry_err_code_t |
| 1408 |
do_keccak_extract (void *context, void *out, size_t outlen) |
| 1409 |
{ |
| 1410 |
KECCAK_CONTEXT *ctx = context; |
| 1411 |
KECCAK_STATE *hd = &ctx->state; |
| 1412 |
const size_t bsize = ctx->blocksize; |
| 1413 |
unsigned int nburn, burn = 0; |
| 1414 |
byte *outbuf = out; |
| 1415 |
unsigned int nlanes; |
| 1416 |
unsigned int nleft; |
| 1417 |
unsigned int count; |
| 1418 |
unsigned int i; |
| 1419 |
byte lane[8]; |
| 1420 |
|
| 1421 |
#ifdef USE_S390X_CRYPTO |
| 1422 |
if (ctx->kimd_func) |
| 1423 |
{ |
| 1424 |
keccak_extract_s390x (context, out, outlen); |
| 1425 |
return 0; |
| 1426 |
} |
| 1427 |
#endif |
| 1428 |
|
| 1429 |
count = ctx->count; |
| 1430 |
|
| 1431 |
while (count && outlen && (outlen < 8 || count % 8)) |
| 1432 |
{ |
| 1433 |
/* Extract partial lane. */ |
| 1434 |
nburn = ctx->ops->extract(hd, count / 8, lane, 8); |
| 1435 |
burn = nburn > burn ? nburn : burn; |
| 1436 |
|
| 1437 |
for (i = count % 8; outlen && i < 8; i++) |
| 1438 |
{ |
| 1439 |
*outbuf++ = lane[i]; |
| 1440 |
outlen--; |
| 1441 |
count++; |
| 1442 |
} |
| 1443 |
|
| 1444 |
gcry_assert(count <= bsize); |
| 1445 |
|
| 1446 |
if (count == bsize) |
| 1447 |
count = 0; |
| 1448 |
} |
| 1449 |
|
| 1450 |
if (outlen >= 8 && count) |
| 1451 |
{ |
| 1452 |
/* Extract tail of partial block. */ |
| 1453 |
nlanes = outlen / 8; |
| 1454 |
nleft = (bsize - count) / 8; |
| 1455 |
nlanes = nlanes < nleft ? nlanes : nleft; |
| 1456 |
|
| 1457 |
nburn = ctx->ops->extract(hd, count / 8, outbuf, nlanes * 8); |
| 1458 |
burn = nburn > burn ? nburn : burn; |
| 1459 |
outlen -= nlanes * 8; |
| 1460 |
outbuf += nlanes * 8; |
| 1461 |
count += nlanes * 8; |
| 1462 |
|
| 1463 |
gcry_assert(count <= bsize); |
| 1464 |
|
| 1465 |
if (count == bsize) |
| 1466 |
count = 0; |
| 1467 |
} |
| 1468 |
|
| 1469 |
while (outlen >= bsize) |
| 1470 |
{ |
| 1471 |
gcry_assert(count == 0); |
| 1472 |
|
| 1473 |
/* Squeeze more. */ |
| 1474 |
nburn = ctx->ops->permute(hd); |
| 1475 |
burn = nburn > burn ? nburn : burn; |
| 1476 |
|
| 1477 |
/* Extract full block. */ |
| 1478 |
nburn = ctx->ops->extract(hd, 0, outbuf, bsize); |
| 1479 |
burn = nburn > burn ? nburn : burn; |
| 1480 |
|
| 1481 |
outlen -= bsize; |
| 1482 |
outbuf += bsize; |
| 1483 |
} |
| 1484 |
|
| 1485 |
if (outlen) |
| 1486 |
{ |
| 1487 |
gcry_assert(outlen < bsize); |
| 1488 |
|
| 1489 |
if (count == 0) |
| 1490 |
{ |
| 1491 |
/* Squeeze more. */ |
| 1492 |
nburn = ctx->ops->permute(hd); |
| 1493 |
burn = nburn > burn ? nburn : burn; |
| 1494 |
} |
| 1495 |
|
| 1496 |
if (outlen >= 8) |
| 1497 |
{ |
| 1498 |
/* Extract head of partial block. */ |
| 1499 |
nlanes = outlen / 8; |
| 1500 |
nburn = ctx->ops->extract(hd, count / 8, outbuf, nlanes * 8); |
| 1501 |
burn = nburn > burn ? nburn : burn; |
| 1502 |
outlen -= nlanes * 8; |
| 1503 |
outbuf += nlanes * 8; |
| 1504 |
count += nlanes * 8; |
| 1505 |
|
| 1506 |
gcry_assert(count < bsize); |
| 1507 |
} |
| 1508 |
|
| 1509 |
if (outlen) |
| 1510 |
{ |
| 1511 |
/* Extract head of partial lane. */ |
| 1512 |
nburn = ctx->ops->extract(hd, count / 8, lane, 8); |
| 1513 |
burn = nburn > burn ? nburn : burn; |
| 1514 |
|
| 1515 |
for (i = count % 8; outlen && i < 8; i++) |
| 1516 |
{ |
| 1517 |
*outbuf++ = lane[i]; |
| 1518 |
outlen--; |
| 1519 |
count++; |
| 1520 |
} |
| 1521 |
|
| 1522 |
gcry_assert(count < bsize); |
| 1523 |
} |
| 1524 |
} |
| 1525 |
|
| 1526 |
ctx->count = count; |
| 1527 |
|
| 1528 |
if (burn) |
| 1529 |
_gcry_burn_stack (burn); |
| 1530 |
|
| 1531 |
return 0; |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
static gcry_err_code_t |
| 1536 |
keccak_extract (void *context, void *out, size_t outlen) |
| 1537 |
{ |
| 1538 |
KECCAK_CONTEXT *ctx = context; |
| 1539 |
|
| 1540 |
if (ctx->shake_in_read_mode) |
| 1541 |
return GPG_ERR_INV_STATE; |
| 1542 |
if (!ctx->shake_in_extract_mode) |
| 1543 |
ctx->shake_in_extract_mode = 1; |
| 1544 |
|
| 1545 |
return do_keccak_extract (context, out, outlen); |
| 1546 |
} |
| 1547 |
|
| 1548 |
|
| 1549 |
static byte * |
| 1550 |
keccak_shake_read (void *context) |
| 1551 |
{ |
| 1552 |
KECCAK_CONTEXT *ctx = (KECCAK_CONTEXT *) context; |
| 1553 |
KECCAK_STATE *hd = &ctx->state; |
| 1554 |
|
| 1555 |
if (ctx->shake_in_extract_mode) |
| 1556 |
{ |
| 1557 |
/* Already in extract mode. */ |
| 1558 |
return NULL; |
| 1559 |
} |
| 1560 |
|
| 1561 |
if (!ctx->shake_in_read_mode) |
| 1562 |
{ |
| 1563 |
byte tmpbuf[64]; |
| 1564 |
|
| 1565 |
gcry_assert(sizeof(tmpbuf) >= ctx->outlen); |
| 1566 |
|
| 1567 |
ctx->shake_in_read_mode = 1; |
| 1568 |
|
| 1569 |
do_keccak_extract (context, tmpbuf, ctx->outlen); |
| 1570 |
buf_cpy (&hd->u, tmpbuf, ctx->outlen); |
| 1571 |
|
| 1572 |
wipememory(tmpbuf, sizeof(tmpbuf)); |
| 1573 |
} |
| 1574 |
|
| 1575 |
return (byte *)&hd->u; |
| 1576 |
} |
| 1577 |
|
| 1578 |
|
| 1579 |
/* Variant of the above shortcut function using multiple buffers. */ |
| 1580 |
static void |
| 1581 |
_gcry_sha3_hash_buffers (void *outbuf, size_t nbytes, const gcry_buffer_t *iov, |
| 1582 |
int iovcnt, const gcry_md_spec_t *spec) |
| 1583 |
{ |
| 1584 |
KECCAK_CONTEXT hd; |
| 1585 |
|
| 1586 |
spec->init (&hd, 0); |
| 1587 |
for (;iovcnt > 0; iov++, iovcnt--) |
| 1588 |
keccak_write (&hd, (const char*)iov[0].data + iov[0].off, iov[0].len); |
| 1589 |
keccak_final (&hd); |
| 1590 |
if (hd.suffix == SHA3_DELIMITED_SUFFIX) |
| 1591 |
memcpy (outbuf, keccak_read (&hd), spec->mdlen); |
| 1592 |
else |
| 1593 |
do_keccak_extract (&hd, outbuf, nbytes); |
| 1594 |
} |
| 1595 |
|
| 1596 |
|
| 1597 |
static void |
| 1598 |
_gcry_sha3_224_hash_buffers (void *outbuf, size_t nbytes, |
| 1599 |
const gcry_buffer_t *iov, int iovcnt) |
| 1600 |
{ |
| 1601 |
_gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt, |
| 1602 |
&_gcry_digest_spec_sha3_224); |
| 1603 |
} |
| 1604 |
|
| 1605 |
static void |
| 1606 |
_gcry_sha3_256_hash_buffers (void *outbuf, size_t nbytes, |
| 1607 |
const gcry_buffer_t *iov, int iovcnt) |
| 1608 |
{ |
| 1609 |
_gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt, |
| 1610 |
&_gcry_digest_spec_sha3_256); |
| 1611 |
} |
| 1612 |
|
| 1613 |
static void |
| 1614 |
_gcry_sha3_384_hash_buffers (void *outbuf, size_t nbytes, |
| 1615 |
const gcry_buffer_t *iov, int iovcnt) |
| 1616 |
{ |
| 1617 |
_gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt, |
| 1618 |
&_gcry_digest_spec_sha3_384); |
| 1619 |
} |
| 1620 |
|
| 1621 |
static void |
| 1622 |
_gcry_sha3_512_hash_buffers (void *outbuf, size_t nbytes, |
| 1623 |
const gcry_buffer_t *iov, int iovcnt) |
| 1624 |
{ |
| 1625 |
_gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt, |
| 1626 |
&_gcry_digest_spec_sha3_512); |
| 1627 |
} |
| 1628 |
|
| 1629 |
static void |
| 1630 |
_gcry_shake128_hash_buffers (void *outbuf, size_t nbytes, |
| 1631 |
const gcry_buffer_t *iov, int iovcnt) |
| 1632 |
{ |
| 1633 |
_gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt, |
| 1634 |
&_gcry_digest_spec_shake128); |
| 1635 |
} |
| 1636 |
|
| 1637 |
static void |
| 1638 |
_gcry_shake256_hash_buffers (void *outbuf, size_t nbytes, |
| 1639 |
const gcry_buffer_t *iov, int iovcnt) |
| 1640 |
{ |
| 1641 |
_gcry_sha3_hash_buffers (outbuf, nbytes, iov, iovcnt, |
| 1642 |
&_gcry_digest_spec_shake256); |
| 1643 |
} |
| 1644 |
|
| 1645 |
|
| 1646 |
static unsigned int |
| 1647 |
cshake_input_n (KECCAK_CONTEXT *ctx, const void *n, unsigned int n_len) |
| 1648 |
{ |
| 1649 |
unsigned char buf[3]; |
| 1650 |
|
| 1651 |
buf[0] = 1; |
| 1652 |
buf[1] = ctx->blocksize; |
| 1653 |
keccak_write (ctx, buf, 2); |
| 1654 |
|
| 1655 |
/* Here, N_LEN must be less than 255 */ |
| 1656 |
if (n_len < 32) |
| 1657 |
{ |
| 1658 |
buf[0] = 1; |
| 1659 |
buf[1] = n_len * 8; |
| 1660 |
} |
| 1661 |
else |
| 1662 |
{ |
| 1663 |
buf[0] = 2; |
| 1664 |
buf[1] = (n_len * 8) >> 8; |
| 1665 |
buf[2] = (n_len * 8) & 0xff; |
| 1666 |
} |
| 1667 |
|
| 1668 |
keccak_write (ctx, buf, buf[0] + 1); |
| 1669 |
keccak_write (ctx, n, n_len); |
| 1670 |
return 2 + buf[0] + 1 + n_len; |
| 1671 |
} |
| 1672 |
|
| 1673 |
static void |
| 1674 |
cshake_input_s (KECCAK_CONTEXT *ctx, const void *s, unsigned int s_len, |
| 1675 |
unsigned int len_written) |
| 1676 |
{ |
| 1677 |
unsigned char buf[168]; |
| 1678 |
unsigned int padlen; |
| 1679 |
|
| 1680 |
/* Here, S_LEN must be less than 255 */ |
| 1681 |
if (s_len < 32) |
| 1682 |
{ |
| 1683 |
buf[0] = 1; |
| 1684 |
buf[1] = s_len * 8; |
| 1685 |
} |
| 1686 |
else |
| 1687 |
{ |
| 1688 |
buf[0] = 2; |
| 1689 |
buf[1] = (s_len * 8) >> 8; |
| 1690 |
buf[2] = (s_len * 8) & 0xff; |
| 1691 |
} |
| 1692 |
|
| 1693 |
keccak_write (ctx, buf, buf[0] + 1); |
| 1694 |
keccak_write (ctx, s, s_len); |
| 1695 |
|
| 1696 |
len_written += buf[0] + 1 + s_len; |
| 1697 |
padlen = ctx->blocksize - (len_written % ctx->blocksize); |
| 1698 |
memset (buf, 0, padlen); |
| 1699 |
keccak_write (ctx, buf, padlen); |
| 1700 |
} |
| 1701 |
|
| 1702 |
gpg_err_code_t |
| 1703 |
_gcry_cshake_customize (void *context, struct gcry_cshake_customization *p) |
| 1704 |
{ |
| 1705 |
KECCAK_CONTEXT *ctx = (KECCAK_CONTEXT *) context; |
| 1706 |
unsigned int len_written; |
| 1707 |
|
| 1708 |
if (p->n_len >= 255 || p->s_len >= 255) |
| 1709 |
return GPG_ERR_TOO_LARGE; |
| 1710 |
|
| 1711 |
if (p->n_len == 0 && p->s_len == 0) |
| 1712 |
/* No customization */ |
| 1713 |
return 0; |
| 1714 |
|
| 1715 |
ctx->suffix = CSHAKE_DELIMITED_SUFFIX; |
| 1716 |
#ifdef USE_S390X_CRYPTO |
| 1717 |
/* CSHAKE suffix is not supported by s390x/kimd. */ |
| 1718 |
ctx->kimd_func = 0; |
| 1719 |
#endif |
| 1720 |
|
| 1721 |
len_written = cshake_input_n (ctx, p->n, p->n_len); |
| 1722 |
cshake_input_s (ctx, p->s, p->s_len, len_written); |
| 1723 |
return 0; |
| 1724 |
} |
| 1725 |
|
| 1726 |
|
| 1727 |
static void |
| 1728 |
cshake128_init (void *context, unsigned int flags) |
| 1729 |
{ |
| 1730 |
keccak_init (GCRY_MD_CSHAKE128, context, flags); |
| 1731 |
} |
| 1732 |
|
| 1733 |
static void |
| 1734 |
cshake256_init (void *context, unsigned int flags) |
| 1735 |
{ |
| 1736 |
keccak_init (GCRY_MD_CSHAKE256, context, flags); |
| 1737 |
} |
| 1738 |
|
| 1739 |
static void |
| 1740 |
cshake_hash_buffers (const gcry_md_spec_t *spec, void *outbuf, size_t nbytes, |
| 1741 |
const gcry_buffer_t *iov, int iovcnt) |
| 1742 |
{ |
| 1743 |
KECCAK_CONTEXT ctx; |
| 1744 |
|
| 1745 |
spec->init (&ctx, 0); |
| 1746 |
|
| 1747 |
if (iovcnt < 2) |
| 1748 |
; /* No customization, do same as SHAKE does. */ |
| 1749 |
else |
| 1750 |
{ |
| 1751 |
if (iov[0].len != 0 || iov[1].len != 0) |
| 1752 |
{ |
| 1753 |
const void *n = (unsigned char *)iov[0].data + iov[0].off; |
| 1754 |
size_t n_len = iov[0].len; |
| 1755 |
const void *s = (unsigned char *)iov[1].data + iov[1].off; |
| 1756 |
size_t s_len = iov[1].len; |
| 1757 |
size_t len; |
| 1758 |
|
| 1759 |
ctx.suffix = CSHAKE_DELIMITED_SUFFIX; |
| 1760 |
#ifdef USE_S390X_CRYPTO |
| 1761 |
/* CSHAKE suffix is not supported by s390x/kimd. */ |
| 1762 |
ctx.kimd_func = 0; |
| 1763 |
#endif |
| 1764 |
|
| 1765 |
len = cshake_input_n (&ctx, n, n_len); |
| 1766 |
cshake_input_s (&ctx, s, s_len, len); |
| 1767 |
} |
| 1768 |
iovcnt -= 2; |
| 1769 |
iov += 2; |
| 1770 |
} |
| 1771 |
|
| 1772 |
for (;iovcnt > 0; iov++, iovcnt--) |
| 1773 |
keccak_write (&ctx, (const char*)iov[0].data + iov[0].off, iov[0].len); |
| 1774 |
keccak_final (&ctx); |
| 1775 |
do_keccak_extract (&ctx, outbuf, nbytes); |
| 1776 |
} |
| 1777 |
|
| 1778 |
static void |
| 1779 |
_gcry_cshake128_hash_buffers (void *outbuf, size_t nbytes, |
| 1780 |
const gcry_buffer_t *iov, int iovcnt) |
| 1781 |
{ |
| 1782 |
const gcry_md_spec_t *spec = &_gcry_digest_spec_shake128; |
| 1783 |
|
| 1784 |
cshake_hash_buffers (spec, outbuf, nbytes, iov, iovcnt); |
| 1785 |
} |
| 1786 |
|
| 1787 |
static void |
| 1788 |
_gcry_cshake256_hash_buffers (void *outbuf, size_t nbytes, |
| 1789 |
const gcry_buffer_t *iov, int iovcnt) |
| 1790 |
{ |
| 1791 |
const gcry_md_spec_t *spec = &_gcry_digest_spec_shake256; |
| 1792 |
|
| 1793 |
cshake_hash_buffers (spec, outbuf, nbytes, iov, iovcnt); |
| 1794 |
} |
| 1795 |
|
| 1796 |
/* |
| 1797 |
Self-test section. |
| 1798 |
*/ |
| 1799 |
|
| 1800 |
|
| 1801 |
static gpg_err_code_t |
| 1802 |
selftests_keccak (int algo, int extended, selftest_report_func_t report) |
| 1803 |
{ |
| 1804 |
const char *what; |
| 1805 |
const char *errtxt; |
| 1806 |
const char *short_hash; |
| 1807 |
const char *long_hash; |
| 1808 |
const char *one_million_a_hash; |
| 1809 |
int hash_len; |
| 1810 |
|
| 1811 |
switch (algo) |
| 1812 |
{ |
| 1813 |
default: |
| 1814 |
BUG(); |
| 1815 |
|
| 1816 |
case GCRY_MD_SHA3_224: |
| 1817 |
short_hash = |
| 1818 |
"\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76\x6f" |
| 1819 |
"\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf"; |
| 1820 |
long_hash = |
| 1821 |
"\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a\xe5" |
| 1822 |
"\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc"; |
| 1823 |
one_million_a_hash = |
| 1824 |
"\xd6\x93\x35\xb9\x33\x25\x19\x2e\x51\x6a\x91\x2e\x6d\x19\xa1\x5c" |
| 1825 |
"\xb5\x1c\x6e\xd5\xc1\x52\x43\xe7\xa7\xfd\x65\x3c"; |
| 1826 |
hash_len = 28; |
| 1827 |
break; |
| 1828 |
|
| 1829 |
case GCRY_MD_SHA3_256: |
| 1830 |
short_hash = |
| 1831 |
"\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90\xbd" |
| 1832 |
"\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43\x15\x32"; |
| 1833 |
long_hash = |
| 1834 |
"\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf\xdb" |
| 1835 |
"\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad\x1d\x18"; |
| 1836 |
one_million_a_hash = |
| 1837 |
"\x5c\x88\x75\xae\x47\x4a\x36\x34\xba\x4f\xd5\x5e\xc8\x5b\xff\xd6" |
| 1838 |
"\x61\xf3\x2a\xca\x75\xc6\xd6\x99\xd0\xcd\xcb\x6c\x11\x58\x91\xc1"; |
| 1839 |
hash_len = 32; |
| 1840 |
break; |
| 1841 |
|
| 1842 |
case GCRY_MD_SHA3_384: |
| 1843 |
short_hash = |
| 1844 |
"\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad\x8d" |
| 1845 |
"\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b\xe4\xb2" |
| 1846 |
"\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28\x37\x6d\x25"; |
| 1847 |
long_hash = |
| 1848 |
"\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47\x91" |
| 1849 |
"\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6\x25\xdc" |
| 1850 |
"\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79\xaa\x7f\xc7"; |
| 1851 |
one_million_a_hash = |
| 1852 |
"\xee\xe9\xe2\x4d\x78\xc1\x85\x53\x37\x98\x34\x51\xdf\x97\xc8\xad" |
| 1853 |
"\x9e\xed\xf2\x56\xc6\x33\x4f\x8e\x94\x8d\x25\x2d\x5e\x0e\x76\x84" |
| 1854 |
"\x7a\xa0\x77\x4d\xdb\x90\xa8\x42\x19\x0d\x2c\x55\x8b\x4b\x83\x40"; |
| 1855 |
hash_len = 48; |
| 1856 |
break; |
| 1857 |
|
| 1858 |
case GCRY_MD_SHA3_512: |
| 1859 |
short_hash = |
| 1860 |
"\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09\x6e" |
| 1861 |
"\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2\x71\x2e" |
| 1862 |
"\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47\xe3\x93\x40" |
| 1863 |
"\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27\x4e\xec\x53\xf0"; |
| 1864 |
long_hash = |
| 1865 |
"\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78\xf9" |
| 1866 |
"\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18\xa4\xfa" |
| 1867 |
"\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8\x2e\x21\x89" |
| 1868 |
"\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55\xf2\x1d\xd1\x85"; |
| 1869 |
one_million_a_hash = |
| 1870 |
"\x3c\x3a\x87\x6d\xa1\x40\x34\xab\x60\x62\x7c\x07\x7b\xb9\x8f\x7e" |
| 1871 |
"\x12\x0a\x2a\x53\x70\x21\x2d\xff\xb3\x38\x5a\x18\xd4\xf3\x88\x59" |
| 1872 |
"\xed\x31\x1d\x0a\x9d\x51\x41\xce\x9c\xc5\xc6\x6e\xe6\x89\xb2\x66" |
| 1873 |
"\xa8\xaa\x18\xac\xe8\x28\x2a\x0e\x0d\xb5\x96\xc9\x0b\x0a\x7b\x87"; |
| 1874 |
hash_len = 64; |
| 1875 |
break; |
| 1876 |
|
| 1877 |
case GCRY_MD_CSHAKE128: |
| 1878 |
case GCRY_MD_SHAKE128: |
| 1879 |
short_hash = |
| 1880 |
"\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb\xa7" |
| 1881 |
"\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f\x2c\xc8"; |
| 1882 |
long_hash = |
| 1883 |
"\x7b\x6d\xf6\xff\x18\x11\x73\xb6\xd7\x89\x8d\x7f\xf6\x3f\xb0\x7b" |
| 1884 |
"\x7c\x23\x7d\xaf\x47\x1a\x5a\xe5\x60\x2a\xdb\xcc\xef\x9c\xcf\x4b"; |
| 1885 |
one_million_a_hash = |
| 1886 |
"\x9d\x22\x2c\x79\xc4\xff\x9d\x09\x2c\xf6\xca\x86\x14\x3a\xa4\x11" |
| 1887 |
"\xe3\x69\x97\x38\x08\xef\x97\x09\x32\x55\x82\x6c\x55\x72\xef\x58"; |
| 1888 |
hash_len = 32; |
| 1889 |
break; |
| 1890 |
|
| 1891 |
case GCRY_MD_CSHAKE256: |
| 1892 |
case GCRY_MD_SHAKE256: |
| 1893 |
short_hash = |
| 1894 |
"\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11\x4d" |
| 1895 |
"\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b\x57\x39"; |
| 1896 |
long_hash = |
| 1897 |
"\x98\xbe\x04\x51\x6c\x04\xcc\x73\x59\x3f\xef\x3e\xd0\x35\x2e\xa9" |
| 1898 |
"\xf6\x44\x39\x42\xd6\x95\x0e\x29\xa3\x72\xa6\x81\xc3\xde\xaf\x45"; |
| 1899 |
one_million_a_hash = |
| 1900 |
"\x35\x78\xa7\xa4\xca\x91\x37\x56\x9c\xdf\x76\xed\x61\x7d\x31\xbb" |
| 1901 |
"\x99\x4f\xca\x9c\x1b\xbf\x8b\x18\x40\x13\xde\x82\x34\xdf\xd1\x3a"; |
| 1902 |
hash_len = 32; |
| 1903 |
break; |
| 1904 |
} |
| 1905 |
|
| 1906 |
what = "short string"; |
| 1907 |
errtxt = _gcry_hash_selftest_check_one (algo, 0, "abc", 3, short_hash, |
| 1908 |
hash_len); |
| 1909 |
if (errtxt) |
| 1910 |
goto failed; |
| 1911 |
|
| 1912 |
if (extended) |
| 1913 |
{ |
| 1914 |
what = "long string"; |
| 1915 |
errtxt = _gcry_hash_selftest_check_one |
| 1916 |
(algo, 0, |
| 1917 |
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" |
| 1918 |
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112, |
| 1919 |
long_hash, hash_len); |
| 1920 |
if (errtxt) |
| 1921 |
goto failed; |
| 1922 |
|
| 1923 |
what = "one million \"a\""; |
| 1924 |
errtxt = _gcry_hash_selftest_check_one (algo, 1, NULL, 0, |
| 1925 |
one_million_a_hash, hash_len); |
| 1926 |
if (errtxt) |
| 1927 |
goto failed; |
| 1928 |
} |
| 1929 |
|
| 1930 |
return 0; /* Succeeded. */ |
| 1931 |
|
| 1932 |
failed: |
| 1933 |
if (report) |
| 1934 |
report ("digest", algo, what, errtxt); |
| 1935 |
return GPG_ERR_SELFTEST_FAILED; |
| 1936 |
} |
| 1937 |
|
| 1938 |
|
| 1939 |
/* Run a full self-test for ALGO and return 0 on success. */ |
| 1940 |
static gpg_err_code_t |
| 1941 |
run_selftests (int algo, int extended, selftest_report_func_t report) |
| 1942 |
{ |
| 1943 |
gpg_err_code_t ec; |
| 1944 |
|
| 1945 |
switch (algo) |
| 1946 |
{ |
| 1947 |
case GCRY_MD_SHA3_224: |
| 1948 |
case GCRY_MD_SHA3_256: |
| 1949 |
case GCRY_MD_SHA3_384: |
| 1950 |
case GCRY_MD_SHA3_512: |
| 1951 |
case GCRY_MD_CSHAKE128: |
| 1952 |
case GCRY_MD_SHAKE128: |
| 1953 |
case GCRY_MD_CSHAKE256: |
| 1954 |
case GCRY_MD_SHAKE256: |
| 1955 |
ec = selftests_keccak (algo, extended, report); |
| 1956 |
break; |
| 1957 |
default: |
| 1958 |
ec = GPG_ERR_DIGEST_ALGO; |
| 1959 |
break; |
| 1960 |
} |
| 1961 |
|
| 1962 |
return ec; |
| 1963 |
} |
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
/* Object IDs obtained from |
| 1969 |
* https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration#Hash |
| 1970 |
*/ |
| 1971 |
static const byte sha3_224_asn[] = |
| 1972 |
{ 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, |
| 1973 |
0x01, 0x65, 0x03, 0x04, 0x02, 0x07, 0x05, 0x00, 0x04, |
| 1974 |
0x1c |
| 1975 |
}; |
| 1976 |
static const gcry_md_oid_spec_t oid_spec_sha3_224[] = |
| 1977 |
{ |
| 1978 |
{ "2.16.840.1.101.3.4.2.7" }, |
| 1979 |
/* id-rsassa-pkcs1-v1-5-with-sha3-224 */ |
| 1980 |
{ "2.16.840.1.101.3.4.3.13" }, |
| 1981 |
/* id-ecdsa-with-sha3-224 */ |
| 1982 |
{ "2.16.840.1.101.3.4.3.9" }, |
| 1983 |
{ NULL } |
| 1984 |
}; |
| 1985 |
static const byte sha3_256_asn[] = |
| 1986 |
{ 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, |
| 1987 |
0x01, 0x65, 0x03, 0x04, 0x02, 0x08, 0x05, 0x00, 0x04, |
| 1988 |
0x20 |
| 1989 |
}; |
| 1990 |
static const gcry_md_oid_spec_t oid_spec_sha3_256[] = |
| 1991 |
{ |
| 1992 |
{ "2.16.840.1.101.3.4.2.8" }, |
| 1993 |
/* id-rsassa-pkcs1-v1-5-with-sha3-256 */ |
| 1994 |
{ "2.16.840.1.101.3.4.3.14" }, |
| 1995 |
/* id-ecdsa-with-sha3-256 */ |
| 1996 |
{ "2.16.840.1.101.3.4.3.10" }, |
| 1997 |
{ NULL } |
| 1998 |
}; |
| 1999 |
static const byte sha3_384_asn[] = |
| 2000 |
{ 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, |
| 2001 |
0x01, 0x65, 0x03, 0x04, 0x02, 0x09, 0x05, 0x00, 0x04, |
| 2002 |
0x30 |
| 2003 |
}; |
| 2004 |
static const gcry_md_oid_spec_t oid_spec_sha3_384[] = |
| 2005 |
{ |
| 2006 |
{ "2.16.840.1.101.3.4.2.9" }, |
| 2007 |
/* id-rsassa-pkcs1-v1-5-with-sha3-384 */ |
| 2008 |
{ "2.16.840.1.101.3.4.3.15" }, |
| 2009 |
/* id-ecdsa-with-sha3-384 */ |
| 2010 |
{ "2.16.840.1.101.3.4.3.11" }, |
| 2011 |
{ NULL } |
| 2012 |
}; |
| 2013 |
static const byte sha3_512_asn[] = |
| 2014 |
{ 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, |
| 2015 |
0x01, 0x65, 0x03, 0x04, 0x02, 0x0a, 0x05, 0x00, 0x04, |
| 2016 |
0x40 |
| 2017 |
}; |
| 2018 |
static const gcry_md_oid_spec_t oid_spec_sha3_512[] = |
| 2019 |
{ |
| 2020 |
{ "2.16.840.1.101.3.4.2.10" }, |
| 2021 |
/* id-rsassa-pkcs1-v1-5-with-sha3-512 */ |
| 2022 |
{ "2.16.840.1.101.3.4.3.16" }, |
| 2023 |
/* id-ecdsa-with-sha3-512 */ |
| 2024 |
{ "2.16.840.1.101.3.4.3.12" }, |
| 2025 |
{ NULL } |
| 2026 |
}; |
| 2027 |
static const byte shake128_asn[] = |
| 2028 |
{ 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, |
| 2029 |
0x01, 0x65, 0x03, 0x04, 0x02, 0x0b, 0x05, 0x00, 0x04, |
| 2030 |
0x20 |
| 2031 |
}; |
| 2032 |
static const gcry_md_oid_spec_t oid_spec_shake128[] = |
| 2033 |
{ |
| 2034 |
{ "2.16.840.1.101.3.4.2.11" }, |
| 2035 |
/* RFC 8692 id-RSASSA-PSS-SHAKE128 */ |
| 2036 |
{ "1.3.6.1.5.5.7.6.30" }, |
| 2037 |
/* RFC 8692 id-ecdsa-with-shake128 */ |
| 2038 |
{ "1.3.6.1.5.5.7.6.32" }, |
| 2039 |
{ NULL } |
| 2040 |
}; |
| 2041 |
static const byte shake256_asn[] = |
| 2042 |
{ 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, |
| 2043 |
0x01, 0x65, 0x03, 0x04, 0x02, 0x0c, 0x05, 0x00, 0x04, |
| 2044 |
0x40 |
| 2045 |
}; |
| 2046 |
static const gcry_md_oid_spec_t oid_spec_shake256[] = |
| 2047 |
{ |
| 2048 |
{ "2.16.840.1.101.3.4.2.12" }, |
| 2049 |
/* RFC 8692 id-RSASSA-PSS-SHAKE256 */ |
| 2050 |
{ "1.3.6.1.5.5.7.6.31" }, |
| 2051 |
/* RFC 8692 id-ecdsa-with-shake256 */ |
| 2052 |
{ "1.3.6.1.5.5.7.6.33" }, |
| 2053 |
{ NULL } |
| 2054 |
}; |
| 2055 |
|
| 2056 |
const gcry_md_spec_t _gcry_digest_spec_sha3_224 = |
| 2057 |
{ |
| 2058 |
GCRY_MD_SHA3_224, {0, 1}, |
| 2059 |
"SHA3-224", sha3_224_asn, DIM (sha3_224_asn), oid_spec_sha3_224, 28, |
| 2060 |
sha3_224_init, keccak_write, keccak_final, keccak_read, NULL, |
| 2061 |
_gcry_sha3_224_hash_buffers, |
| 2062 |
sizeof (KECCAK_CONTEXT), |
| 2063 |
run_selftests |
| 2064 |
}; |
| 2065 |
const gcry_md_spec_t _gcry_digest_spec_sha3_256 = |
| 2066 |
{ |
| 2067 |
GCRY_MD_SHA3_256, {0, 1}, |
| 2068 |
"SHA3-256", sha3_256_asn, DIM (sha3_256_asn), oid_spec_sha3_256, 32, |
| 2069 |
sha3_256_init, keccak_write, keccak_final, keccak_read, NULL, |
| 2070 |
_gcry_sha3_256_hash_buffers, |
| 2071 |
sizeof (KECCAK_CONTEXT), |
| 2072 |
run_selftests |
| 2073 |
}; |
| 2074 |
const gcry_md_spec_t _gcry_digest_spec_sha3_384 = |
| 2075 |
{ |
| 2076 |
GCRY_MD_SHA3_384, {0, 1}, |
| 2077 |
"SHA3-384", sha3_384_asn, DIM (sha3_384_asn), oid_spec_sha3_384, 48, |
| 2078 |
sha3_384_init, keccak_write, keccak_final, keccak_read, NULL, |
| 2079 |
_gcry_sha3_384_hash_buffers, |
| 2080 |
sizeof (KECCAK_CONTEXT), |
| 2081 |
run_selftests |
| 2082 |
}; |
| 2083 |
const gcry_md_spec_t _gcry_digest_spec_sha3_512 = |
| 2084 |
{ |
| 2085 |
GCRY_MD_SHA3_512, {0, 1}, |
| 2086 |
"SHA3-512", sha3_512_asn, DIM (sha3_512_asn), oid_spec_sha3_512, 64, |
| 2087 |
sha3_512_init, keccak_write, keccak_final, keccak_read, NULL, |
| 2088 |
_gcry_sha3_512_hash_buffers, |
| 2089 |
sizeof (KECCAK_CONTEXT), |
| 2090 |
run_selftests |
| 2091 |
}; |
| 2092 |
const gcry_md_spec_t _gcry_digest_spec_shake128 = |
| 2093 |
{ |
| 2094 |
GCRY_MD_SHAKE128, {0, 1}, |
| 2095 |
"SHAKE128", shake128_asn, DIM (shake128_asn), oid_spec_shake128, 32, |
| 2096 |
shake128_init, keccak_write, keccak_final, keccak_shake_read, |
| 2097 |
keccak_extract, |
| 2098 |
_gcry_shake128_hash_buffers, |
| 2099 |
sizeof (KECCAK_CONTEXT), |
| 2100 |
run_selftests |
| 2101 |
}; |
| 2102 |
const gcry_md_spec_t _gcry_digest_spec_shake256 = |
| 2103 |
{ |
| 2104 |
GCRY_MD_SHAKE256, {0, 1}, |
| 2105 |
"SHAKE256", shake256_asn, DIM (shake256_asn), oid_spec_shake256, 64, |
| 2106 |
shake256_init, keccak_write, keccak_final, keccak_shake_read, |
| 2107 |
keccak_extract, |
| 2108 |
_gcry_shake256_hash_buffers, |
| 2109 |
sizeof (KECCAK_CONTEXT), |
| 2110 |
run_selftests |
| 2111 |
}; |
| 2112 |
const gcry_md_spec_t _gcry_digest_spec_cshake128 = |
| 2113 |
{ |
| 2114 |
GCRY_MD_CSHAKE128, {0, 1}, |
| 2115 |
"CSHAKE128", NULL, 0, NULL, 32, |
| 2116 |
cshake128_init, keccak_write, keccak_final, keccak_shake_read, |
| 2117 |
keccak_extract, _gcry_cshake128_hash_buffers, |
| 2118 |
sizeof (KECCAK_CONTEXT), |
| 2119 |
run_selftests |
| 2120 |
}; |
| 2121 |
const gcry_md_spec_t _gcry_digest_spec_cshake256 = |
| 2122 |
{ |
| 2123 |
GCRY_MD_CSHAKE256, {0, 1}, |
| 2124 |
"CSHAKE256", NULL, 0, NULL, 64, |
| 2125 |
cshake256_init, keccak_write, keccak_final, keccak_shake_read, |
| 2126 |
keccak_extract, _gcry_cshake256_hash_buffers, |
| 2127 |
sizeof (KECCAK_CONTEXT), |
| 2128 |
run_selftests |
| 2129 |
}; |