| 1 |
// Copyright (c) 2018, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_SHA_INTERNAL_H |
| 5 |
#define OPENSSL_HEADER_SHA_INTERNAL_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#include <openssl/hmac.h> |
| 10 |
|
| 11 |
#include "../../internal.h" |
| 12 |
#include "../cpucap/internal.h" |
| 13 |
|
| 14 |
#if defined(__cplusplus) |
| 15 |
extern "C" { |
| 16 |
#endif |
| 17 |
|
| 18 |
// Internal SHA2 constants |
| 19 |
|
| 20 |
// SHA*_CHAINING_LENGTH is the chaining length in bytes of SHA-* |
| 21 |
// It corresponds to the length in bytes of the h part of the state |
| 22 |
#define SHA1_CHAINING_LENGTH 20 |
| 23 |
#define SHA224_CHAINING_LENGTH 32 |
| 24 |
#define SHA256_CHAINING_LENGTH 32 |
| 25 |
#define SHA384_CHAINING_LENGTH 64 |
| 26 |
#define SHA512_CHAINING_LENGTH 64 |
| 27 |
#define SHA512_224_CHAINING_LENGTH 64 |
| 28 |
#define SHA512_256_CHAINING_LENGTH 64 |
| 29 |
|
| 30 |
|
| 31 |
// SHA3 constants, from NIST FIPS202. |
| 32 |
// https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf |
| 33 |
#define KECCAK1600_ROWS 5 |
| 34 |
#define KECCAK1600_WIDTH 1600 |
| 35 |
|
| 36 |
#define SHA3_224_CAPACITY_BYTES 56 |
| 37 |
#define SHA3_224_CBLOCK SHA3_BLOCKSIZE(SHA3_224_DIGEST_BITLENGTH) |
| 38 |
#define SHA3_224_DIGEST_BITLENGTH 224 |
| 39 |
#define SHA3_224_DIGEST_LENGTH 28 |
| 40 |
|
| 41 |
#define SHA3_256_CAPACITY_BYTES 64 |
| 42 |
#define SHA3_256_CBLOCK SHA3_BLOCKSIZE(SHA3_256_DIGEST_BITLENGTH) |
| 43 |
#define SHA3_256_DIGEST_BITLENGTH 256 |
| 44 |
#define SHA3_256_DIGEST_LENGTH 32 |
| 45 |
|
| 46 |
#define SHA3_384_CAPACITY_BYTES 96 |
| 47 |
#define SHA3_384_CBLOCK SHA3_BLOCKSIZE(SHA3_384_DIGEST_BITLENGTH) |
| 48 |
#define SHA3_384_DIGEST_BITLENGTH 384 |
| 49 |
#define SHA3_384_DIGEST_LENGTH 48 |
| 50 |
|
| 51 |
#define SHA3_512_CAPACITY_BYTES 128 |
| 52 |
#define SHA3_512_CBLOCK SHA3_BLOCKSIZE(SHA3_512_DIGEST_BITLENGTH) |
| 53 |
#define SHA3_512_DIGEST_BITLENGTH 512 |
| 54 |
#define SHA3_512_DIGEST_LENGTH 64 |
| 55 |
|
| 56 |
#define SHA3_BLOCKSIZE(bitlen) (KECCAK1600_WIDTH - bitlen * 2) / 8 |
| 57 |
#define SHA3_PAD_CHAR 0x06 |
| 58 |
|
| 59 |
// SHAKE constants, from NIST FIPS202. |
| 60 |
// https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf |
| 61 |
#define SHAKE_PAD_CHAR 0x1F |
| 62 |
#define SHAKE128_BLOCKSIZE ((KECCAK1600_WIDTH - 128 * 2) / 8) |
| 63 |
#define SHAKE256_BLOCKSIZE ((KECCAK1600_WIDTH - 256 * 2) / 8) |
| 64 |
#define XOF_BLOCKBYTES SHAKE128_BLOCKSIZE |
| 65 |
|
| 66 |
// SHAKE128 has the maximum block size among the SHA3/SHAKE algorithms. |
| 67 |
#define SHA3_MAX_BLOCKSIZE SHAKE128_BLOCKSIZE |
| 68 |
|
| 69 |
// Define state flag values for Keccak-based functions |
| 70 |
#define KECCAK1600_STATE_ABSORB 0 |
| 71 |
// KECCAK1600_STATE_SQUEEZE is set when |SHAKE_Squeeze| is called. |
| 72 |
// It remains set while |SHAKE_Squeeze| is called repeatedly to output |
| 73 |
// chunks of the XOF output. |
| 74 |
#define KECCAK1600_STATE_SQUEEZE 1 |
| 75 |
// KECCAK1600_STATE_FINAL is set once |SHAKE_Final| is called |
| 76 |
// so that |SHAKE_Squeeze| cannot be called anymore. |
| 77 |
#define KECCAK1600_STATE_FINAL 2 |
| 78 |
|
| 79 |
typedef struct keccak_ctx_st KECCAK1600_CTX; |
| 80 |
|
| 81 |
// The data buffer should have at least the maximum number of |
| 82 |
// block size bytes to fit any SHA3/SHAKE block length. |
| 83 |
struct keccak_ctx_st { |
| 84 |
uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS]; |
| 85 |
size_t block_size; // cached ctx->digest->block_size |
| 86 |
size_t md_size; // output length, variable in XOF (SHAKE) |
| 87 |
size_t buf_load; // used bytes in below buffer |
| 88 |
uint8_t buf[SHA3_MAX_BLOCKSIZE]; // should have at least the max data block size bytes |
| 89 |
uint8_t pad; // padding character |
| 90 |
uint8_t state; // denotes the keccak phase (absorb, squeeze, final) |
| 91 |
}; |
| 92 |
|
| 93 |
// To avoid externalizing KECCAK1600_CTX, we hard-code the context size in |
| 94 |
// hmac.h's |md_ctx_union| and use a compile time check here to make sure |
| 95 |
// |KECCAK1600_CTX|'s size never exceeds that of |md_ctx_union|. This means |
| 96 |
// that whenever a new field is added to |keccak_ctx_st| we must also update |
| 97 |
// the hard-coded size of |sha3| in hmac.h's |md_ctx_union| with the new |
| 98 |
// value given by |sizeof(keccaak_ctx_st)|. |
| 99 |
OPENSSL_STATIC_ASSERT(sizeof(KECCAK1600_CTX) <= sizeof(union md_ctx_union), |
| 100 |
hmac_md_ctx_union_sha3_size_needs_update) |
| 101 |
|
| 102 |
// KECCAK1600 x4 batched context structure |
| 103 |
typedef struct keccak_ctx_st_x4 KECCAK1600_CTX_x4; |
| 104 |
|
| 105 |
struct keccak_ctx_st_x4 { |
| 106 |
uint64_t A[4][KECCAK1600_ROWS][KECCAK1600_ROWS]; |
| 107 |
}; |
| 108 |
|
| 109 |
// Define SHA{n}[_{variant}]_ASM if sha{n}_block_data_order[_{variant}] is |
| 110 |
// defined in assembly. |
| 111 |
|
| 112 |
#if defined(OPENSSL_PPC64LE) |
| 113 |
#define SHA1_ALTIVEC |
| 114 |
|
| 115 |
void sha1_block_data_order(uint32_t *state, const uint8_t *data, |
| 116 |
size_t num_blocks); |
| 117 |
|
| 118 |
#elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_ARM) |
| 119 |
|
| 120 |
#define SHA1_ASM_NOHW |
| 121 |
#define SHA256_ASM_NOHW |
| 122 |
#define SHA512_ASM_NOHW |
| 123 |
|
| 124 |
#define SHA1_ASM_HW |
| 125 |
OPENSSL_INLINE int sha1_hw_capable(void) { |
| 126 |
return CRYPTO_is_ARMv8_SHA1_capable(); |
| 127 |
} |
| 128 |
|
| 129 |
#define SHA1_ASM_NEON |
| 130 |
void sha1_block_data_order_neon(uint32_t state[5], const uint8_t *data, |
| 131 |
size_t num); |
| 132 |
|
| 133 |
#define SHA256_ASM_HW |
| 134 |
OPENSSL_INLINE int sha256_hw_capable(void) { |
| 135 |
return CRYPTO_is_ARMv8_SHA256_capable(); |
| 136 |
} |
| 137 |
|
| 138 |
#define SHA256_ASM_NEON |
| 139 |
void sha256_block_data_order_neon(uint32_t state[8], const uint8_t *data, |
| 140 |
size_t num); |
| 141 |
|
| 142 |
// Armv8.2 SHA-512 instructions are not available in 32-bit. |
| 143 |
#define SHA512_ASM_NEON |
| 144 |
void sha512_block_data_order_neon(uint64_t state[8], const uint8_t *data, |
| 145 |
size_t num); |
| 146 |
|
| 147 |
#elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_AARCH64) |
| 148 |
|
| 149 |
#define SHA1_ASM_NOHW |
| 150 |
#define SHA256_ASM_NOHW |
| 151 |
#define SHA512_ASM_NOHW |
| 152 |
|
| 153 |
#define SHA1_ASM_HW |
| 154 |
OPENSSL_INLINE int sha1_hw_capable(void) { |
| 155 |
return CRYPTO_is_ARMv8_SHA1_capable(); |
| 156 |
} |
| 157 |
|
| 158 |
#define SHA256_ASM_HW |
| 159 |
OPENSSL_INLINE int sha256_hw_capable(void) { |
| 160 |
return CRYPTO_is_ARMv8_SHA256_capable(); |
| 161 |
} |
| 162 |
|
| 163 |
#define SHA512_ASM_HW |
| 164 |
OPENSSL_INLINE int sha512_hw_capable(void) { |
| 165 |
return CRYPTO_is_ARMv8_SHA512_capable(); |
| 166 |
} |
| 167 |
|
| 168 |
#elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) |
| 169 |
|
| 170 |
#define SHA1_ASM_NOHW |
| 171 |
#define SHA256_ASM_NOHW |
| 172 |
|
| 173 |
#define SHA1_ASM_SSSE3 |
| 174 |
OPENSSL_INLINE int sha1_ssse3_capable(void) { |
| 175 |
// TODO(davidben): Do we need to check the FXSR bit? The Intel manual does not |
| 176 |
// say to. |
| 177 |
return CRYPTO_is_SSSE3_capable() && CRYPTO_is_FXSR_capable(); |
| 178 |
} |
| 179 |
void sha1_block_data_order_ssse3(uint32_t state[5], const uint8_t *data, |
| 180 |
size_t num); |
| 181 |
|
| 182 |
#define SHA1_ASM_AVX |
| 183 |
OPENSSL_INLINE int sha1_avx_capable(void) { |
| 184 |
// Pre-Zen AMD CPUs had slow SHLD/SHRD; Zen added the SHA extension; see the |
| 185 |
// discussion in sha1-586.pl. |
| 186 |
// |
| 187 |
// TODO(davidben): Should we enable SHAEXT on 32-bit x86? |
| 188 |
// TODO(davidben): Do we need to check the FXSR bit? The Intel manual does not |
| 189 |
// say to. |
| 190 |
return CRYPTO_is_AVX_capable() && CRYPTO_is_intel_cpu() && |
| 191 |
CRYPTO_is_FXSR_capable(); |
| 192 |
} |
| 193 |
void sha1_block_data_order_avx(uint32_t state[5], const uint8_t *data, |
| 194 |
size_t num); |
| 195 |
|
| 196 |
#define SHA256_ASM_SSSE3 |
| 197 |
OPENSSL_INLINE int sha256_ssse3_capable(void) { |
| 198 |
// TODO(davidben): Do we need to check the FXSR bit? The Intel manual does not |
| 199 |
// say to. |
| 200 |
return CRYPTO_is_SSSE3_capable() && CRYPTO_is_FXSR_capable(); |
| 201 |
} |
| 202 |
void sha256_block_data_order_ssse3(uint32_t state[8], const uint8_t *data, |
| 203 |
size_t num); |
| 204 |
|
| 205 |
#define SHA256_ASM_AVX |
| 206 |
OPENSSL_INLINE int sha256_avx_capable(void) { |
| 207 |
// Pre-Zen AMD CPUs had slow SHLD/SHRD; Zen added the SHA extension; see the |
| 208 |
// discussion in sha1-586.pl. |
| 209 |
// |
| 210 |
// TODO(davidben): Should we enable SHAEXT on 32-bit x86? |
| 211 |
// TODO(davidben): Do we need to check the FXSR bit? The Intel manual does not |
| 212 |
// say to. |
| 213 |
return CRYPTO_is_AVX_capable() && CRYPTO_is_intel_cpu() && |
| 214 |
CRYPTO_is_FXSR_capable(); |
| 215 |
} |
| 216 |
void sha256_block_data_order_avx(uint32_t state[8], const uint8_t *data, |
| 217 |
size_t num); |
| 218 |
|
| 219 |
// TODO(crbug.com/boringssl/673): Move the remaining CPU dispatch to C. |
| 220 |
#define SHA512_ASM |
| 221 |
void sha512_block_data_order(uint64_t state[8], const uint8_t *data, |
| 222 |
size_t num_blocks); |
| 223 |
|
| 224 |
#elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) |
| 225 |
|
| 226 |
#define SHA1_ASM_NOHW |
| 227 |
#define SHA256_ASM_NOHW |
| 228 |
#define SHA512_ASM_NOHW |
| 229 |
|
| 230 |
#define SHA1_ASM_HW |
| 231 |
OPENSSL_INLINE int sha1_hw_capable(void) { |
| 232 |
return CRYPTO_is_SHAEXT_capable() && CRYPTO_is_SSSE3_capable(); |
| 233 |
} |
| 234 |
|
| 235 |
#define SHA1_ASM_AVX2 |
| 236 |
OPENSSL_INLINE int sha1_avx2_capable(void) { |
| 237 |
// TODO: Simplify this logic, which was extracted from the assembly: |
| 238 |
// * Does AVX2 imply SSSE3? |
| 239 |
// * sha1_block_data_order_avx2 does not seem to use SSSE3 instructions. |
| 240 |
return CRYPTO_is_AVX2_capable() && CRYPTO_is_BMI2_capable() && |
| 241 |
CRYPTO_is_BMI1_capable() && CRYPTO_is_SSSE3_capable(); |
| 242 |
} |
| 243 |
void sha1_block_data_order_avx2(uint32_t state[5], const uint8_t *data, |
| 244 |
size_t num); |
| 245 |
|
| 246 |
#define SHA1_ASM_AVX |
| 247 |
OPENSSL_INLINE int sha1_avx_capable(void) { |
| 248 |
// TODO: Simplify this logic, which was extracted from the assembly: |
| 249 |
// * Does AVX imply SSSE3? |
| 250 |
// * sha1_block_data_order_avx does not seem to use SSSE3 instructions. |
| 251 |
// Pre-Zen AMD CPUs had slow SHLD/SHRD; Zen added the SHA extension; see the |
| 252 |
// discussion in sha1-586.pl. |
| 253 |
return CRYPTO_is_AVX_capable() && CRYPTO_is_SSSE3_capable() && |
| 254 |
CRYPTO_is_intel_cpu(); |
| 255 |
} |
| 256 |
void sha1_block_data_order_avx(uint32_t state[5], const uint8_t *data, |
| 257 |
size_t num); |
| 258 |
|
| 259 |
#define SHA1_ASM_SSSE3 |
| 260 |
OPENSSL_INLINE int sha1_ssse3_capable(void) { |
| 261 |
return CRYPTO_is_SSSE3_capable(); |
| 262 |
} |
| 263 |
void sha1_block_data_order_ssse3(uint32_t state[5], const uint8_t *data, |
| 264 |
size_t num); |
| 265 |
|
| 266 |
#define SHA256_ASM_HW |
| 267 |
OPENSSL_INLINE int sha256_hw_capable(void) { |
| 268 |
return CRYPTO_is_SHAEXT_capable(); |
| 269 |
} |
| 270 |
|
| 271 |
#define SHA256_ASM_AVX |
| 272 |
OPENSSL_INLINE int sha256_avx_capable(void) { |
| 273 |
// TODO: Simplify this logic, which was extracted from the assembly: |
| 274 |
// * Does AVX imply SSSE3? |
| 275 |
// * sha256_block_data_order_avx does not seem to use SSSE3 instructions. |
| 276 |
// Pre-Zen AMD CPUs had slow SHLD/SHRD; Zen added the SHA extension; see the |
| 277 |
// discussion in sha1-586.pl. |
| 278 |
return CRYPTO_is_AVX_capable() && CRYPTO_is_SSSE3_capable() && |
| 279 |
CRYPTO_is_intel_cpu(); |
| 280 |
} |
| 281 |
void sha256_block_data_order_avx(uint32_t state[8], const uint8_t *data, |
| 282 |
size_t num); |
| 283 |
|
| 284 |
#define SHA256_ASM_SSSE3 |
| 285 |
OPENSSL_INLINE int sha256_ssse3_capable(void) { |
| 286 |
return CRYPTO_is_SSSE3_capable(); |
| 287 |
} |
| 288 |
void sha256_block_data_order_ssse3(uint32_t state[8], const uint8_t *data, |
| 289 |
size_t num); |
| 290 |
|
| 291 |
#define SHA512_ASM_AVX |
| 292 |
OPENSSL_INLINE int sha512_avx_capable(void) { |
| 293 |
// TODO: Simplify this logic, which was extracted from the assembly: |
| 294 |
// * Does AVX imply SSSE3? |
| 295 |
// * sha512_block_data_order_avx does not seem to use SSSE3 instructions. |
| 296 |
// Pre-Zen AMD CPUs had slow SHLD/SHRD; Zen added the SHA extension; see the |
| 297 |
// discussion in sha1-586.pl. |
| 298 |
return CRYPTO_is_AVX_capable() && CRYPTO_is_SSSE3_capable() && |
| 299 |
CRYPTO_is_intel_cpu(); |
| 300 |
} |
| 301 |
void sha512_block_data_order_avx(uint64_t state[8], const uint8_t *data, |
| 302 |
size_t num); |
| 303 |
|
| 304 |
#endif |
| 305 |
|
| 306 |
#if defined(SHA1_ASM_HW) |
| 307 |
void sha1_block_data_order_hw(uint32_t state[5], const uint8_t *data, |
| 308 |
size_t num); |
| 309 |
#endif |
| 310 |
#if defined(SHA1_ASM_NOHW) |
| 311 |
void sha1_block_data_order_nohw(uint32_t state[5], const uint8_t *data, |
| 312 |
size_t num); |
| 313 |
#endif |
| 314 |
|
| 315 |
#if defined(SHA256_ASM_HW) |
| 316 |
void sha256_block_data_order_hw(uint32_t state[8], const uint8_t *data, |
| 317 |
size_t num); |
| 318 |
#endif |
| 319 |
#if defined(SHA256_ASM_NOHW) |
| 320 |
void sha256_block_data_order_nohw(uint32_t state[8], const uint8_t *data, |
| 321 |
size_t num); |
| 322 |
#endif |
| 323 |
|
| 324 |
#if defined(SHA512_ASM_HW) |
| 325 |
void sha512_block_data_order_hw(uint64_t state[8], const uint8_t *data, |
| 326 |
size_t num); |
| 327 |
#endif |
| 328 |
|
| 329 |
#if defined(SHA512_ASM_NOHW) |
| 330 |
void sha512_block_data_order_nohw(uint64_t state[8], const uint8_t *data, |
| 331 |
size_t num); |
| 332 |
#endif |
| 333 |
|
| 334 |
#if !defined(OPENSSL_NO_ASM) |
| 335 |
#if defined(OPENSSL_AARCH64) |
| 336 |
#define KECCAK1600_ASM |
| 337 |
#if defined(OPENSSL_LINUX) || defined(OPENSSL_APPLE) |
| 338 |
#define KECCAK1600_S2N_BIGNUM_ASM |
| 339 |
#include "../../../third_party/s2n-bignum/s2n-bignum_aws-lc.h" |
| 340 |
#endif |
| 341 |
#endif |
| 342 |
#if defined(OPENSSL_X86_64) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX) |
| 343 |
#if defined(OPENSSL_LINUX) || defined(OPENSSL_APPLE) |
| 344 |
#define KECCAK1600_ASM |
| 345 |
#define KECCAK1600_S2N_BIGNUM_ASM |
| 346 |
#include "../../../third_party/s2n-bignum/s2n-bignum_aws-lc.h" |
| 347 |
#endif |
| 348 |
#endif |
| 349 |
#endif |
| 350 |
|
| 351 |
// SHAx_Init_from_state is a low-level function that initializes |sha| with a |
| 352 |
// custom state. |h| is the hash state in big endian. |n| is the number of bits |
| 353 |
// processed at this point. It must be a multiple of |SHAy_CBLOCK*8|, |
| 354 |
// where SHAy=SHA1 if SHAx=SHA1, SHAy=SHA256 if SHAx=SHA224 or SHA256, and |
| 355 |
// SHAy=SHA512 otherwise. |
| 356 |
// This function returns one on success and zero on error. |
| 357 |
// This function is for internal use only and should never be directly called. |
| 358 |
OPENSSL_EXPORT int SHA1_Init_from_state( |
| 359 |
SHA_CTX *sha, const uint8_t h[SHA1_CHAINING_LENGTH], uint64_t n); |
| 360 |
OPENSSL_EXPORT int SHA224_Init_from_state( |
| 361 |
SHA256_CTX *sha, const uint8_t h[SHA224_CHAINING_LENGTH], uint64_t n); |
| 362 |
OPENSSL_EXPORT int SHA256_Init_from_state( |
| 363 |
SHA256_CTX *sha, const uint8_t h[SHA256_CHAINING_LENGTH], uint64_t n); |
| 364 |
OPENSSL_EXPORT int SHA384_Init_from_state( |
| 365 |
SHA512_CTX *sha, const uint8_t h[SHA384_CHAINING_LENGTH], uint64_t n); |
| 366 |
OPENSSL_EXPORT int SHA512_Init_from_state( |
| 367 |
SHA512_CTX *sha, const uint8_t h[SHA512_CHAINING_LENGTH], uint64_t n); |
| 368 |
OPENSSL_EXPORT int SHA512_224_Init_from_state( |
| 369 |
SHA512_CTX *sha, const uint8_t h[SHA512_224_CHAINING_LENGTH], uint64_t n); |
| 370 |
OPENSSL_EXPORT int SHA512_256_Init_from_state( |
| 371 |
SHA512_CTX *sha, const uint8_t h[SHA512_256_CHAINING_LENGTH], uint64_t n); |
| 372 |
|
| 373 |
// SHAx_get_state is a low-level function that exports the hash state in big |
| 374 |
// endian into |out_h| and the number of bits processed at this point in |
| 375 |
// |out_n|. |SHAx_Final| must not have been called before (otherwise results |
| 376 |
// are not guaranteed). Furthermore, the number of bytes processed by |
| 377 |
// |SHAx_Update| must be a multiple of the block length |SHAy_CBLOCK| and |
| 378 |
// must be less than 2^61 (otherwise it fails). See comment above about |
| 379 |
// |SHAx_Init_from_state| for the definition of SHAy. |
| 380 |
// This function returns one on success and zero on error. |
| 381 |
// This function is for internal use only and should never be directly called. |
| 382 |
OPENSSL_EXPORT int SHA1_get_state( |
| 383 |
SHA_CTX *ctx, uint8_t out_h[SHA1_CHAINING_LENGTH], uint64_t *out_n); |
| 384 |
OPENSSL_EXPORT int SHA224_get_state( |
| 385 |
SHA256_CTX *ctx, uint8_t out_h[SHA224_CHAINING_LENGTH], uint64_t *out_n); |
| 386 |
OPENSSL_EXPORT int SHA256_get_state( |
| 387 |
SHA256_CTX *ctx, uint8_t out_h[SHA256_CHAINING_LENGTH], uint64_t *out_n); |
| 388 |
OPENSSL_EXPORT int SHA384_get_state( |
| 389 |
SHA512_CTX *ctx, uint8_t out_h[SHA384_CHAINING_LENGTH], uint64_t *out_n); |
| 390 |
OPENSSL_EXPORT int SHA512_get_state( |
| 391 |
SHA512_CTX *ctx, uint8_t out_h[SHA512_CHAINING_LENGTH], uint64_t *out_n); |
| 392 |
OPENSSL_EXPORT int SHA512_224_get_state( |
| 393 |
SHA512_CTX *ctx, uint8_t out_h[SHA512_224_CHAINING_LENGTH], |
| 394 |
uint64_t *out_n); |
| 395 |
OPENSSL_EXPORT int SHA512_256_get_state( |
| 396 |
SHA512_CTX *ctx, uint8_t out_h[SHA512_256_CHAINING_LENGTH], |
| 397 |
uint64_t *out_n); |
| 398 |
|
| 399 |
/* |
| 400 |
* SHA3/SHAKE single-shot APIs implement SHA3 functionalities on top |
| 401 |
* of SHA3/SHAKE API layer |
| 402 |
* |
| 403 |
* SHA3/SHAKE single-shot functions never fail when the later call-discipline is |
| 404 |
* adhered to: (a) the pointers passed to the functions are valid. |
| 405 |
*/ |
| 406 |
|
| 407 |
// SHA3_224 writes the digest of |len| bytes from |data| to |out| and returns |out|. |
| 408 |
// There must be at least |SHA3_224_DIGEST_LENGTH| bytes of space in |out|. |
| 409 |
// On failure |SHA3_224| returns NULL. |
| 410 |
OPENSSL_EXPORT uint8_t *SHA3_224(const uint8_t *data, size_t len, |
| 411 |
uint8_t out[SHA3_224_DIGEST_LENGTH]); |
| 412 |
|
| 413 |
// SHA3_256 writes the digest of |len| bytes from |data| to |out| and returns |out|. |
| 414 |
// There must be at least |SHA3_256_DIGEST_LENGTH| bytes of space in |out|. |
| 415 |
// On failure |SHA3_256| returns NULL. |
| 416 |
OPENSSL_EXPORT uint8_t *SHA3_256(const uint8_t *data, size_t len, |
| 417 |
uint8_t out[SHA3_256_DIGEST_LENGTH]); |
| 418 |
|
| 419 |
// SHA3_384 writes the digest of |len| bytes from |data| to |out| and returns |out|. |
| 420 |
// There must be at least |SHA3_384_DIGEST_LENGTH| bytes of space in |out|. |
| 421 |
// On failure |SHA3_384| returns NULL. |
| 422 |
OPENSSL_EXPORT uint8_t *SHA3_384(const uint8_t *data, size_t len, |
| 423 |
uint8_t out[SHA3_384_DIGEST_LENGTH]); |
| 424 |
|
| 425 |
// SHA3_512 writes the digest of |len| bytes from |data| to |out| and returns |out|. |
| 426 |
// There must be at least |SHA3_512_DIGEST_LENGTH| bytes of space in |out|. |
| 427 |
// On failure |SHA3_512| returns NULL. |
| 428 |
OPENSSL_EXPORT uint8_t *SHA3_512(const uint8_t *data, size_t len, |
| 429 |
uint8_t out[SHA3_512_DIGEST_LENGTH]); |
| 430 |
|
| 431 |
// SHAKE128 writes the |out_len| bytes output from |in_len| bytes |data| |
| 432 |
// to |out| and returns |out| on success and NULL on failure. |
| 433 |
OPENSSL_EXPORT uint8_t *SHAKE128(const uint8_t *data, const size_t in_len, |
| 434 |
uint8_t *out, size_t out_len); |
| 435 |
|
| 436 |
// SHAKE256 writes |out_len| bytes output from |in_len| bytes |data| |
| 437 |
// to |out| and returns |out| on success and NULL on failure. |
| 438 |
OPENSSL_EXPORT uint8_t *SHAKE256(const uint8_t *data, const size_t in_len, |
| 439 |
uint8_t *out, size_t out_len); |
| 440 |
/* |
| 441 |
* SHA3 APIs implement SHA3 functionalities on top of FIPS202 API layer |
| 442 |
* |
| 443 |
* SHA3 context must go through the flow: (a) Init, (b) Update [multiple times], |
| 444 |
* (c) Final [one time]. |
| 445 |
* |
| 446 |
* SHA3 functions never fail when the later call-discipline is adhered to: |
| 447 |
* (a) the context execution flow is followed (b) the pointers passed to the |
| 448 |
* functions are valid (c) any additional per-function parameter value conditions, |
| 449 |
* detailed above each SHA3_ function signature, is satisfied. |
| 450 |
*/ |
| 451 |
|
| 452 |
// SHA3_Init initialises |ctx| field through |FIPS202_Init| and |
| 453 |
// returns 1 on success and 0 on failure. When call-discipline is |
| 454 |
// maintained and |bitlen| value corresponds to a SHA3 digest length |
| 455 |
// in bits, this function never fails. |
| 456 |
OPENSSL_EXPORT int SHA3_Init(KECCAK1600_CTX *ctx, size_t bitlen); |
| 457 |
|
| 458 |
// SHA3_Update checks |ctx| pointer and |len| value, calls |FIPS202_Update| |
| 459 |
// and returns 1 on success and 0 on failure. When call-discipline is |
| 460 |
// maintained and |len| value corresponds to the input message length |
| 461 |
// (including zero), this function never fails. |
| 462 |
int SHA3_Update(KECCAK1600_CTX *ctx, const void *data, size_t len); |
| 463 |
|
| 464 |
// SHA3_Final pads the last data block and absorbs it through |FIPS202_Finalize|. |
| 465 |
// It then calls |Keccak1600_Squeeze| and returns 1 on success and 0 on failure. |
| 466 |
// When call-discipline is maintained, this function never fails. |
| 467 |
int SHA3_Final(uint8_t *md, KECCAK1600_CTX *ctx); |
| 468 |
|
| 469 |
// SHA3_224_Init initialises |sha| and returns 1. |
| 470 |
int SHA3_224_Init(KECCAK1600_CTX *sha); |
| 471 |
|
| 472 |
// SHA3_224_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 473 |
int SHA3_224_Update(KECCAK1600_CTX *sha, const void *data, size_t len); |
| 474 |
|
| 475 |
// SHA3_224_Final adds the final padding to |sha| and writes the resulting |
| 476 |
// digest to |out|. It returns one on success and zero on programmer error. |
| 477 |
int SHA3_224_Final(uint8_t out[SHA3_224_DIGEST_LENGTH], KECCAK1600_CTX *sha); |
| 478 |
|
| 479 |
// SHA3_256_Init initialises |sha| and returns 1. |
| 480 |
int SHA3_256_Init(KECCAK1600_CTX *sha); |
| 481 |
|
| 482 |
// SHA3_256_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 483 |
int SHA3_256_Update(KECCAK1600_CTX *sha, const void *data, size_t len); |
| 484 |
|
| 485 |
// SHA3_256_Final adds the final padding to |sha| and writes the resulting |
| 486 |
// digest to |out|. It returns one on success and zero on programmer error. |
| 487 |
int SHA3_256_Final(uint8_t out[SHA3_256_DIGEST_LENGTH], KECCAK1600_CTX *sha); |
| 488 |
|
| 489 |
// SHA3_384_Init initialises |sha| and returns 1. |
| 490 |
int SHA3_384_Init(KECCAK1600_CTX *sha); |
| 491 |
|
| 492 |
// SHA3_384_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 493 |
int SHA3_384_Update(KECCAK1600_CTX *sha, const void *data, size_t len); |
| 494 |
|
| 495 |
// SHA3_384_Final adds the final padding to |sha| and writes the resulting |
| 496 |
// digest to |out|. It returns one on success and zero on programmer error. |
| 497 |
int SHA3_384_Final(uint8_t out[SHA3_384_DIGEST_LENGTH], KECCAK1600_CTX *sha); |
| 498 |
|
| 499 |
// SHA3_512_Init initialises |sha| and returns 1. |
| 500 |
int SHA3_512_Init(KECCAK1600_CTX *sha); |
| 501 |
|
| 502 |
// SHA3_512_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 503 |
int SHA3_512_Update(KECCAK1600_CTX *sha, const void *data, size_t len); |
| 504 |
|
| 505 |
// SHA3_512_Final adds the final padding to |sha| and writes the resulting |
| 506 |
// digest to |out|. It returns one on success and zero on programmer error. |
| 507 |
int SHA3_512_Final(uint8_t out[SHA3_512_DIGEST_LENGTH], KECCAK1600_CTX *sha); |
| 508 |
|
| 509 |
/* |
| 510 |
* SHAKE APIs implement SHAKE functionalities on top of FIPS202 API layer |
| 511 |
* |
| 512 |
* SHAKE context must go through the flow: (a) Init, (b) Absorb [multiple times], |
| 513 |
* (c) Final [one time] or Squeeze [multiple times] |
| 514 |
* |
| 515 |
* SHAKE functions never fail when the later call-discipline is adhered to: |
| 516 |
* (a) the context execution flow is followed (b) the pointers passed to the |
| 517 |
* functions are valid (c) any additional per-function parameter value conditions, |
| 518 |
* detailed above each SHAKE_ function signature, is satisfied. |
| 519 |
*/ |
| 520 |
|
| 521 |
// SHAKE_Init initialises |ctx| fields through |FIPS202_Init| and |
| 522 |
// returns 1 on success and 0 on failure. When call-discipline is |
| 523 |
// maintained and |block_size| value corresponds to a SHAKE block size length |
| 524 |
// in bytes, this function never fails. |
| 525 |
int SHAKE_Init(KECCAK1600_CTX *ctx, size_t block_size); |
| 526 |
|
| 527 |
// SHAKE_Absorb checks |ctx| pointer and |len| values. It updates and absorbs |
| 528 |
// input blocks via |FIPS202_Update|. When call-discipline is |
| 529 |
// maintained and |len| value corresponds to the input message length |
| 530 |
// (including zero), this function never fails. |
| 531 |
int SHAKE_Absorb(KECCAK1600_CTX *ctx, const void *data, |
| 532 |
size_t len); |
| 533 |
|
| 534 |
// SHAKE_Squeeze pads the last data block and absorbs it through |
| 535 |
// |FIPS202_Finalize| on first call. It writes |len| bytes of incremental |
| 536 |
// XOF output to |md| and returns 1 on success and 0 on failure. It can be |
| 537 |
// called multiple times. When call-discipline is maintained, this function |
| 538 |
// never fails. |
| 539 |
int SHAKE_Squeeze(uint8_t *md, KECCAK1600_CTX *ctx, size_t len); |
| 540 |
|
| 541 |
// SHAKE_Final writes |len| bytes of finalized extendible output to |md|, returns 1 on |
| 542 |
// success and 0 on failure. It should be called once to finalize absorb and |
| 543 |
// squeeze phases. Incremental XOF output should be generated via |SHAKE_Squeeze|. |
| 544 |
// When call-discipline is maintained, this function never fails. |
| 545 |
int SHAKE_Final(uint8_t *md, KECCAK1600_CTX *ctx, size_t len); |
| 546 |
|
| 547 |
/* |
| 548 |
* SHAKE128_x4_ batched APIs implement x4 SHAKE functionalities on top of FIPS202 API layer |
| 549 |
* |
| 550 |
* SHAKE128_x4_ context must go through the flow: (a) Init_x4, (b) Absorb_once_x4 [one time; |
| 551 |
* maximum input length of |SHAKE128_BLOCKSIZE - 1|] (c) Squeezeblocks_x4 [multiple times] |
| 552 |
* |
| 553 |
* SHAKE128_x4_ functions never fail when the later call-discipline is adhered to: |
| 554 |
* (a) the context execution flow is followed (b) the pointers passed to the |
| 555 |
* functions are valid (c) any additional per-function parameter value conditions, |
| 556 |
* detailed above each SHAKE128_x4_ function signature, is satisfied. |
| 557 |
*/ |
| 558 |
|
| 559 |
// SHAKE128_Init_x4 is a batched API that operates on four independent |
| 560 |
// Keccak bitstates. It initialises all four |ctx| fields and returns |
| 561 |
// 1 on success and 0 on failure. When call-discipline is maintained, |
| 562 |
// this function never fails. |
| 563 |
OPENSSL_EXPORT int SHAKE128_Init_x4(KECCAK1600_CTX_x4 *ctx); |
| 564 |
|
| 565 |
// SHAKE128_Absorb_once_x4 is a batched API that operates on four independent |
| 566 |
// Keccak bitstates. It absorbs all four inputs |data0|, |data1|, |data2|, |data3| |
| 567 |
// of equal length of |len| bytes returns 1 on success and 0 on failure. When |
| 568 |
// is maintained and |len| value corresponds to the input messages length |
| 569 |
// call-discipline (including zero), this function never fails. |
| 570 |
OPENSSL_EXPORT int SHAKE128_Absorb_once_x4(KECCAK1600_CTX_x4 *ctx, const void *data0, const void *data1, |
| 571 |
const void *data2, const void *data3, size_t len); |
| 572 |
|
| 573 |
// SHAKE128_Squeezeblocks_x4 is a batched API that operates on four independent Keccak |
| 574 |
// bitstates. It squeezes |blks| number of blocks for all four XOF digests and returns |
| 575 |
// 1 on success and 0 on failure. When call-discipline is maintained, this function |
| 576 |
// never fails. |
| 577 |
OPENSSL_EXPORT int SHAKE128_Squeezeblocks_x4(uint8_t *md0, uint8_t *md1, uint8_t *md2, uint8_t *md3, |
| 578 |
KECCAK1600_CTX_x4 *ctx, size_t blks); |
| 579 |
/* |
| 580 |
* SHAKE256_x4_ signle-shot batched API implements x4 SHAKE256 functionalities on top |
| 581 |
* of FIPS202 API layer |
| 582 |
* |
| 583 |
* SHAKE256_x4_ function never fails when the later call-discipline is adhered to: |
| 584 |
* (a) the pointers passed to the functions are valid. |
| 585 |
*/ |
| 586 |
|
| 587 |
// SHAKE256_x4 is a batched API that operates on four independent |
| 588 |
// Keccak bitstates. It writes all four |out_len|-byte outputs from |
| 589 |
// |in_len|-byte inputs to |out0|, |out1|, |out2|, |out3| and returns |
| 590 |
// 1 on success and 0 on failure. |
| 591 |
// When call-discipline is maintained, this function never fails. |
| 592 |
OPENSSL_EXPORT int SHAKE256_x4(const uint8_t *data0, const uint8_t *data1, |
| 593 |
const uint8_t *data2, const uint8_t *data3, |
| 594 |
const size_t in_len, uint8_t *out0, uint8_t *out1, |
| 595 |
uint8_t *out2, uint8_t *out3, size_t out_len); |
| 596 |
|
| 597 |
/* |
| 598 |
* Keccak1600_ APIs implement Keccak absorb and squeeze phases |
| 599 |
*/ |
| 600 |
|
| 601 |
// Keccak1600_Absorb processes the largest multiple of |r| (block size) out of |
| 602 |
// |len| bytes and returns the remaining number of bytes. |
| 603 |
size_t Keccak1600_Absorb(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS], |
| 604 |
const uint8_t *data, size_t len, size_t r); |
| 605 |
|
| 606 |
// Keccak1600_Absorb_once_x4 absorbs exactly |len| bytes from four inputs into four |
| 607 |
// Keccak states, applying padding character |p|. Unlike Keccak1600_Absorb, this |
| 608 |
// processes a single block and takes the padding character as an additional argument. |
| 609 |
void Keccak1600_Absorb_once_x4(uint64_t A[4][KECCAK1600_ROWS][KECCAK1600_ROWS], |
| 610 |
const uint8_t *inp0, const uint8_t *inp1, |
| 611 |
const uint8_t *inp2, const uint8_t *inp3, |
| 612 |
size_t len, size_t r, uint8_t p); |
| 613 |
|
| 614 |
// Keccak1600_Squeezeblocks_x4 squeezes |num_blocks| blocks from four Keccak states |
| 615 |
// into four output buffers, with each block being |r| bytes. |
| 616 |
void Keccak1600_Squeezeblocks_x4(uint64_t A[4][KECCAK1600_ROWS][KECCAK1600_ROWS], |
| 617 |
uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_t *out3, |
| 618 |
size_t num_blocks, size_t r); |
| 619 |
|
| 620 |
// Keccak1600_Squeeze generates |out| value of |len| bytes (per call). It can be called |
| 621 |
// multiple times when used as eXtendable Output Function. |padded| indicates |
| 622 |
// whether it is the first call to Keccak1600_Squeeze; i.e., if the current block has |
| 623 |
// been already processed and padded right after the last call to Keccak1600_Absorb. |
| 624 |
// Squeezes full blocks of |r| bytes each. When performing multiple squeezes, any |
| 625 |
// left over bytes from previous squeezes are not consumed, and |len| must be a |
| 626 |
// multiple of the block size (except on the final squeeze). |
| 627 |
OPENSSL_EXPORT void Keccak1600_Squeeze(uint64_t A[KECCAK1600_ROWS][KECCAK1600_ROWS], |
| 628 |
uint8_t *out, size_t len, size_t r, int padded); |
| 629 |
|
| 630 |
#if defined(__cplusplus) |
| 631 |
} // extern "C" |
| 632 |
#endif |
| 633 |
|
| 634 |
#endif // OPENSSL_HEADER_SHA_INTERNAL_H |