| 1 |
// Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include "internal.h" |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
uint8_t *SHA3_224(const uint8_t *data, size_t len, |
| 8 |
uint8_t out[SHA3_224_DIGEST_LENGTH]) { |
| 9 |
FIPS_service_indicator_lock_state(); |
| 10 |
KECCAK1600_CTX ctx; |
| 11 |
int ok = (SHA3_Init(&ctx, SHA3_224_DIGEST_BITLENGTH) && |
| 12 |
SHA3_Update(&ctx, data, len) && |
| 13 |
SHA3_Final(out, &ctx)); |
| 14 |
|
| 15 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 16 |
FIPS_service_indicator_unlock_state(); |
| 17 |
if (ok == 0) { |
| 18 |
return NULL; |
| 19 |
} |
| 20 |
FIPS_service_indicator_update_state(); |
| 21 |
return out; |
| 22 |
} |
| 23 |
|
| 24 |
uint8_t *SHA3_256(const uint8_t *data, size_t len, |
| 25 |
uint8_t out[SHA3_256_DIGEST_LENGTH]) { |
| 26 |
FIPS_service_indicator_lock_state(); |
| 27 |
KECCAK1600_CTX ctx; |
| 28 |
int ok = (SHA3_Init(&ctx, SHA3_256_DIGEST_BITLENGTH) && |
| 29 |
SHA3_Update(&ctx, data, len) && |
| 30 |
SHA3_Final(out, &ctx)); |
| 31 |
|
| 32 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 33 |
FIPS_service_indicator_unlock_state(); |
| 34 |
if (ok == 0) { |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
FIPS_service_indicator_update_state(); |
| 38 |
return out; |
| 39 |
} |
| 40 |
|
| 41 |
uint8_t *SHA3_384(const uint8_t *data, size_t len, |
| 42 |
uint8_t out[SHA3_384_DIGEST_LENGTH]) { |
| 43 |
FIPS_service_indicator_lock_state(); |
| 44 |
KECCAK1600_CTX ctx; |
| 45 |
int ok = (SHA3_Init(&ctx, SHA3_384_DIGEST_BITLENGTH) && |
| 46 |
SHA3_Update(&ctx, data, len) && |
| 47 |
SHA3_Final(out, &ctx)); |
| 48 |
|
| 49 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 50 |
FIPS_service_indicator_unlock_state(); |
| 51 |
if (ok == 0) { |
| 52 |
return NULL; |
| 53 |
} |
| 54 |
FIPS_service_indicator_update_state(); |
| 55 |
return out; |
| 56 |
} |
| 57 |
|
| 58 |
uint8_t *SHA3_512(const uint8_t *data, size_t len, |
| 59 |
uint8_t out[SHA3_512_DIGEST_LENGTH]) { |
| 60 |
FIPS_service_indicator_lock_state(); |
| 61 |
KECCAK1600_CTX ctx; |
| 62 |
int ok = (SHA3_Init(&ctx, SHA3_512_DIGEST_BITLENGTH) && |
| 63 |
SHA3_Update(&ctx, data, len) && |
| 64 |
SHA3_Final(out, &ctx)); |
| 65 |
|
| 66 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 67 |
FIPS_service_indicator_unlock_state(); |
| 68 |
if (ok == 0) { |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
FIPS_service_indicator_update_state(); |
| 72 |
return out; |
| 73 |
} |
| 74 |
|
| 75 |
uint8_t *SHAKE128(const uint8_t *data, const size_t in_len, uint8_t *out, size_t out_len) { |
| 76 |
FIPS_service_indicator_lock_state(); |
| 77 |
KECCAK1600_CTX ctx; |
| 78 |
int ok = (SHAKE_Init(&ctx, SHAKE128_BLOCKSIZE) && |
| 79 |
SHAKE_Absorb(&ctx, data, in_len) && |
| 80 |
SHAKE_Final(out, &ctx, out_len)); |
| 81 |
|
| 82 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 83 |
FIPS_service_indicator_unlock_state(); |
| 84 |
if (ok == 0) { |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
FIPS_service_indicator_update_state(); |
| 88 |
return out; |
| 89 |
} |
| 90 |
|
| 91 |
uint8_t *SHAKE256(const uint8_t *data, const size_t in_len, uint8_t *out, size_t out_len) { |
| 92 |
FIPS_service_indicator_lock_state(); |
| 93 |
KECCAK1600_CTX ctx; |
| 94 |
int ok = (SHAKE_Init(&ctx, SHAKE256_BLOCKSIZE) && |
| 95 |
SHAKE_Absorb(&ctx, data, in_len) && |
| 96 |
SHAKE_Final(out, &ctx, out_len)); |
| 97 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 98 |
FIPS_service_indicator_unlock_state(); |
| 99 |
if (ok == 0) { |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
FIPS_service_indicator_update_state(); |
| 103 |
return out; |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* FIPS202 APIs manage internal input/output buffer on top of Keccak1600 API layer |
| 108 |
*/ |
| 109 |
// FIPS202_Reset zero's |ctx| fields. |
| 110 |
static void FIPS202_Reset(KECCAK1600_CTX *ctx) { |
| 111 |
OPENSSL_memset(ctx->A, 0, sizeof(ctx->A)); |
| 112 |
ctx->buf_load = 0; |
| 113 |
ctx->state = KECCAK1600_STATE_ABSORB; |
| 114 |
} |
| 115 |
|
| 116 |
// FIPS202_Init checks the correctness of the padding character and size of |
| 117 |
// the internal buffer. It initialises the |ctx| fields and returns 1 on |
| 118 |
// success and 0 on failure. |
| 119 |
static int FIPS202_Init(KECCAK1600_CTX *ctx, uint8_t pad, size_t block_size, size_t bit_len) { |
| 120 |
if (pad != SHA3_PAD_CHAR && |
| 121 |
pad != SHAKE_PAD_CHAR) { |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
if (block_size <= sizeof(ctx->buf)) { |
| 126 |
FIPS202_Reset(ctx); |
| 127 |
ctx->block_size = block_size; |
| 128 |
ctx->md_size = bit_len / 8; |
| 129 |
ctx->pad = pad; |
| 130 |
return 1; |
| 131 |
} |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
// FIPS202_Update checks the state of the |ctx| and processes intermediate buffer from |
| 136 |
// previous calls. It processes |data| in blocks through |Keccak1600_Absorb| and places |
| 137 |
// the rest in the intermediate buffer. FIPS202_Update fails if called from inappropriate |
| 138 |
// |ctx->state| or on |Keccak1600_Absorb| error. Otherwise, it returns 1. |
| 139 |
static int FIPS202_Update(KECCAK1600_CTX *ctx, const void *data, size_t len) { |
| 140 |
uint8_t *data_ptr_copy = (uint8_t *) data; |
| 141 |
size_t block_size = ctx->block_size; |
| 142 |
size_t num, rem; |
| 143 |
|
| 144 |
if (ctx->state == KECCAK1600_STATE_SQUEEZE || |
| 145 |
ctx->state == KECCAK1600_STATE_FINAL ) { |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
// Case |len| equals 0 is checked in SHA3/SHAKE higher level APIs |
| 150 |
// Process intermediate buffer. |
| 151 |
num = ctx->buf_load; |
| 152 |
if (num != 0) { |
| 153 |
rem = block_size - num; |
| 154 |
if (len < rem) { |
| 155 |
OPENSSL_memcpy(ctx->buf + num, data_ptr_copy, len); |
| 156 |
ctx->buf_load += len; |
| 157 |
return 1; |
| 158 |
} |
| 159 |
|
| 160 |
// There is enough data to fill or overflow the intermediate |
| 161 |
// buffer. So we append |rem| bytes and process the block, |
| 162 |
// leaving the rest for later processing. |
| 163 |
OPENSSL_memcpy(ctx->buf + num, data_ptr_copy, rem); |
| 164 |
data_ptr_copy += rem, len -= rem; |
| 165 |
if (Keccak1600_Absorb(ctx->A, ctx->buf, block_size, block_size) != 0 ) { |
| 166 |
return 0; |
| 167 |
} |
| 168 |
ctx->buf_load = 0; |
| 169 |
// ctx->buf is processed, ctx->buf_load is guaranteed to be zero |
| 170 |
} |
| 171 |
|
| 172 |
if (len >= block_size) { |
| 173 |
rem = Keccak1600_Absorb(ctx->A, data_ptr_copy, len, block_size); |
| 174 |
} |
| 175 |
else { |
| 176 |
rem = len; |
| 177 |
} |
| 178 |
|
| 179 |
if (rem != 0) { |
| 180 |
OPENSSL_memcpy(ctx->buf, data_ptr_copy + len - rem, rem); |
| 181 |
ctx->buf_load = rem; |
| 182 |
} |
| 183 |
|
| 184 |
return 1; |
| 185 |
} |
| 186 |
|
| 187 |
// FIPS202_Finalize processes padding and absorb of last input block |
| 188 |
// This function should be called once to finalize absorb and initiate |
| 189 |
// squeeze phase. FIPS202_Finalize fails if called from inappropriate |
| 190 |
// |ctx->state| or on |Keccak1600_Absorb| error. Otherwise, it returns 1. |
| 191 |
static int FIPS202_Finalize(uint8_t *md, KECCAK1600_CTX *ctx) { |
| 192 |
size_t block_size = ctx->block_size; |
| 193 |
size_t num = ctx->buf_load; |
| 194 |
|
| 195 |
if (ctx->state == KECCAK1600_STATE_SQUEEZE || |
| 196 |
ctx->state == KECCAK1600_STATE_FINAL ) { |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
// Pad the data with 10*1. Note that |num| can be |block_size - 1| |
| 201 |
// in which case both byte operations below are performed on |
| 202 |
// the same byte. |
| 203 |
OPENSSL_memset(ctx->buf + num, 0, block_size - num); |
| 204 |
ctx->buf[num] = ctx->pad; |
| 205 |
ctx->buf[block_size - 1] |= 0x80; |
| 206 |
|
| 207 |
if (Keccak1600_Absorb(ctx->A, ctx->buf, block_size, block_size) != 0) { |
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
// ctx->buf is processed, ctx->buf_load is guaranteed to be zero |
| 212 |
ctx->buf_load = 0; |
| 213 |
|
| 214 |
return 1; |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* SHA3 APIs implement SHA3 functionalities on top of FIPS202 API layer |
| 219 |
*/ |
| 220 |
int SHA3_Init(KECCAK1600_CTX *ctx, size_t bit_len) { |
| 221 |
if (ctx == NULL) { |
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
if (bit_len != SHA3_224_DIGEST_BITLENGTH && |
| 226 |
bit_len != SHA3_256_DIGEST_BITLENGTH && |
| 227 |
bit_len != SHA3_384_DIGEST_BITLENGTH && |
| 228 |
bit_len != SHA3_512_DIGEST_BITLENGTH) { |
| 229 |
return 0; |
| 230 |
} |
| 231 |
// |block_size| depends on the SHA3 |bit_len| output (digest) length |
| 232 |
return FIPS202_Init(ctx, SHA3_PAD_CHAR, SHA3_BLOCKSIZE(bit_len), bit_len); |
| 233 |
} |
| 234 |
|
| 235 |
int SHA3_Update(KECCAK1600_CTX *ctx, const void *data, size_t len) { |
| 236 |
if (ctx == NULL) { |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
if (data == NULL && len != 0) { |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
if (len == 0) { |
| 245 |
return 1; |
| 246 |
} |
| 247 |
|
| 248 |
return FIPS202_Update(ctx, data, len); |
| 249 |
} |
| 250 |
|
| 251 |
// SHA3_Final should be called once to process final digest value |
| 252 |
int SHA3_Final(uint8_t *md, KECCAK1600_CTX *ctx) { |
| 253 |
if (md == NULL || ctx == NULL) { |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
if (ctx->md_size == 0) { |
| 258 |
return 1; |
| 259 |
} |
| 260 |
|
| 261 |
if (FIPS202_Finalize(md, ctx) == 0) { |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
Keccak1600_Squeeze(ctx->A, md, ctx->md_size, ctx->block_size, ctx->state); |
| 266 |
ctx->state = KECCAK1600_STATE_FINAL; |
| 267 |
|
| 268 |
FIPS_service_indicator_update_state(); |
| 269 |
return 1; |
| 270 |
} |
| 271 |
|
| 272 |
int SHA3_224_Init(KECCAK1600_CTX *ctx) { |
| 273 |
return SHA3_Init(ctx, SHA3_224_DIGEST_BITLENGTH); |
| 274 |
} |
| 275 |
|
| 276 |
int SHA3_224_Update(KECCAK1600_CTX *ctx, const void *data, |
| 277 |
size_t len) { |
| 278 |
return SHA3_Update(ctx, data, len); |
| 279 |
} |
| 280 |
|
| 281 |
int SHA3_224_Final(uint8_t out[SHA3_224_DIGEST_LENGTH], |
| 282 |
KECCAK1600_CTX *ctx) { |
| 283 |
return SHA3_Final(&out[0], ctx); |
| 284 |
} |
| 285 |
|
| 286 |
int SHA3_256_Init(KECCAK1600_CTX *ctx) { |
| 287 |
return SHA3_Init(ctx, SHA3_256_DIGEST_BITLENGTH); |
| 288 |
} |
| 289 |
|
| 290 |
int SHA3_256_Update(KECCAK1600_CTX *ctx, const void *data, |
| 291 |
size_t len) { |
| 292 |
return SHA3_Update(ctx, data, len); |
| 293 |
} |
| 294 |
|
| 295 |
int SHA3_256_Final(uint8_t out[SHA3_256_DIGEST_LENGTH], |
| 296 |
KECCAK1600_CTX *ctx) { |
| 297 |
return SHA3_Final(&out[0], ctx); |
| 298 |
} |
| 299 |
|
| 300 |
int SHA3_384_Init(KECCAK1600_CTX *ctx) { |
| 301 |
return SHA3_Init(ctx, SHA3_384_DIGEST_BITLENGTH); |
| 302 |
} |
| 303 |
|
| 304 |
int SHA3_384_Update(KECCAK1600_CTX *ctx, const void *data, |
| 305 |
size_t len) { |
| 306 |
return SHA3_Update(ctx, data, len); |
| 307 |
} |
| 308 |
|
| 309 |
int SHA3_384_Final(uint8_t out[SHA3_384_DIGEST_LENGTH], |
| 310 |
KECCAK1600_CTX *ctx) { |
| 311 |
return SHA3_Final(&out[0], ctx); |
| 312 |
} |
| 313 |
|
| 314 |
int SHA3_512_Init(KECCAK1600_CTX *ctx) { |
| 315 |
return SHA3_Init(ctx, SHA3_512_DIGEST_BITLENGTH); |
| 316 |
} |
| 317 |
|
| 318 |
int SHA3_512_Update(KECCAK1600_CTX *ctx, const void *data, |
| 319 |
size_t len) { |
| 320 |
return SHA3_Update(ctx, data, len); |
| 321 |
} |
| 322 |
|
| 323 |
int SHA3_512_Final(uint8_t out[SHA3_512_DIGEST_LENGTH], |
| 324 |
KECCAK1600_CTX *ctx) { |
| 325 |
return SHA3_Final(&out[0], ctx); |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
* SHAKE APIs implement SHAKE functionalities on top of FIPS202 API layer |
| 330 |
*/ |
| 331 |
int SHAKE_Init(KECCAK1600_CTX *ctx, size_t block_size) { |
| 332 |
if (ctx == NULL) { |
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
if (block_size != SHAKE128_BLOCKSIZE && |
| 337 |
block_size != SHAKE256_BLOCKSIZE) { |
| 338 |
return 0; |
| 339 |
} |
| 340 |
// |block_size| depends on the SHAKE security level |
| 341 |
// The output length |bit_len| is initialized to 0 |
| 342 |
return FIPS202_Init(ctx, SHAKE_PAD_CHAR, block_size, 0); |
| 343 |
} |
| 344 |
|
| 345 |
int SHAKE_Absorb(KECCAK1600_CTX *ctx, const void *data, size_t len) { |
| 346 |
if (ctx == NULL) { |
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
if (data == NULL && len != 0) { |
| 351 |
return 0; |
| 352 |
} |
| 353 |
|
| 354 |
if (len == 0) { |
| 355 |
return 1; |
| 356 |
} |
| 357 |
|
| 358 |
return FIPS202_Update(ctx, data, len); |
| 359 |
} |
| 360 |
|
| 361 |
// SHAKE_Final is to be called once to finalize absorb and squeeze phases |
| 362 |
// |ctx->state| restricts consecutive calls to |FIPS202_Finalize|. |
| 363 |
// Function |SHAKE_Squeeze| should be used for incremental XOF output. |
| 364 |
int SHAKE_Final(uint8_t *md, KECCAK1600_CTX *ctx, size_t len) { |
| 365 |
if (ctx == NULL || md == NULL) { |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
ctx->md_size = len; |
| 370 |
if (ctx->md_size == 0) { |
| 371 |
return 1; |
| 372 |
} |
| 373 |
|
| 374 |
if (FIPS202_Finalize(md, ctx) == 0) { |
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
Keccak1600_Squeeze(ctx->A, md, ctx->md_size, ctx->block_size, ctx->state); |
| 379 |
ctx->state = KECCAK1600_STATE_FINAL; |
| 380 |
|
| 381 |
FIPS_service_indicator_update_state(); |
| 382 |
return 1; |
| 383 |
} |
| 384 |
|
| 385 |
// SHAKE_Squeeze can be called multiple time for incremental XOF output |
| 386 |
int SHAKE_Squeeze(uint8_t *md, KECCAK1600_CTX *ctx, size_t len) { |
| 387 |
size_t block_bytes; |
| 388 |
|
| 389 |
if (ctx == NULL || md == NULL) { |
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
ctx->md_size = len; |
| 394 |
|
| 395 |
if (ctx->md_size == 0) { |
| 396 |
return 1; |
| 397 |
} |
| 398 |
|
| 399 |
if (ctx->state == KECCAK1600_STATE_FINAL) { |
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
// Skip FIPS202_Finalize if the input has been padded and |
| 404 |
// the last block has been processed |
| 405 |
if (ctx->state == KECCAK1600_STATE_ABSORB) { |
| 406 |
if (FIPS202_Finalize(md, ctx) == 0) { |
| 407 |
return 0; |
| 408 |
} |
| 409 |
} |
| 410 |
// Process previous data from output buffer if any |
| 411 |
if (ctx->buf_load != 0) { |
| 412 |
if (len <= ctx->buf_load) { |
| 413 |
OPENSSL_memcpy(md, ctx->buf + ctx->block_size - ctx->buf_load, len); |
| 414 |
ctx->buf_load -= len; |
| 415 |
return 1; |
| 416 |
} else { |
| 417 |
OPENSSL_memcpy(md, ctx->buf + ctx->block_size - ctx->buf_load, ctx->buf_load); |
| 418 |
md += ctx->buf_load; |
| 419 |
len -= ctx->buf_load; |
| 420 |
ctx->buf_load = 0; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
// Process all full size output requested blocks |
| 425 |
if (len > ctx->block_size) { |
| 426 |
block_bytes = ctx->block_size * (len / ctx->block_size); |
| 427 |
Keccak1600_Squeeze(ctx->A, md, block_bytes, ctx->block_size, ctx->state); |
| 428 |
md += block_bytes; |
| 429 |
len -= block_bytes; |
| 430 |
ctx->state = KECCAK1600_STATE_SQUEEZE; |
| 431 |
} |
| 432 |
|
| 433 |
if (len > 0) { |
| 434 |
// Process an additional block if output length is not a multiple of block size. |
| 435 |
// Generated output is store in |ctx->buf|. Only requested bytes are transfered |
| 436 |
// to the output. The 'unused' output data is kept for processing in a sequenctual |
| 437 |
// call to SHAKE_Squeeze (incremental byte-wise SHAKE_Squeeze) |
| 438 |
Keccak1600_Squeeze(ctx->A, ctx->buf, ctx->block_size, ctx->block_size, ctx->state); |
| 439 |
OPENSSL_memcpy(md, ctx->buf, len); |
| 440 |
ctx->buf_load = ctx->block_size - len; // how much there is still in buffer to be consumed |
| 441 |
ctx->state = KECCAK1600_STATE_SQUEEZE; |
| 442 |
} |
| 443 |
|
| 444 |
//FIPS_service_indicator_update_state(); |
| 445 |
return 1; |
| 446 |
} |
| 447 |
|
| 448 |
/* |
| 449 |
* SHAKE batched (x4) APIs implement SHAKE functionalities in batches of four on top of SHAKE API layer |
| 450 |
*/ |
| 451 |
int SHAKE128_Init_x4(KECCAK1600_CTX_x4 *ctx) { |
| 452 |
OPENSSL_memset(ctx, 0, sizeof(KECCAK1600_CTX_x4)); |
| 453 |
return 1; |
| 454 |
} |
| 455 |
|
| 456 |
int SHAKE128_Absorb_once_x4(KECCAK1600_CTX_x4 *ctx, const void *data0, const void *data1, |
| 457 |
const void *data2, const void *data3, size_t len) { |
| 458 |
Keccak1600_Absorb_once_x4(ctx->A, data0, data1, data2, data3, len, |
| 459 |
SHAKE128_BLOCKSIZE, SHAKE_PAD_CHAR); |
| 460 |
return 1; |
| 461 |
} |
| 462 |
|
| 463 |
int SHAKE128_Squeezeblocks_x4(uint8_t *md0, uint8_t *md1, uint8_t *md2, uint8_t *md3, |
| 464 |
KECCAK1600_CTX_x4 *ctx, size_t blks) { |
| 465 |
Keccak1600_Squeezeblocks_x4(ctx->A, md0, md1, md2, md3, blks, SHAKE128_BLOCKSIZE); |
| 466 |
return 1; |
| 467 |
} |
| 468 |
|
| 469 |
static int SHAKE256_Absorb_once_x4(KECCAK1600_CTX_x4 *ctx, const void *data0, const void *data1, |
| 470 |
const void *data2, const void *data3, size_t len) { |
| 471 |
Keccak1600_Absorb_once_x4(ctx->A, data0, data1, data2, data3, |
| 472 |
len, SHAKE256_BLOCKSIZE, SHAKE_PAD_CHAR); |
| 473 |
return 1; |
| 474 |
} |
| 475 |
|
| 476 |
static int SHAKE256_Squeezeblocks_x4(uint8_t *md0, uint8_t *md1, uint8_t *md2, uint8_t *md3, |
| 477 |
KECCAK1600_CTX_x4 *ctx, size_t blks) { |
| 478 |
Keccak1600_Squeezeblocks_x4(ctx->A, md0, md1, md2, md3, blks, SHAKE256_BLOCKSIZE); |
| 479 |
return 1; |
| 480 |
} |
| 481 |
|
| 482 |
int SHAKE256_x4(const uint8_t *data0, const uint8_t *data1, const uint8_t *data2, |
| 483 |
const uint8_t *data3, const size_t in_len, |
| 484 |
uint8_t *out0, uint8_t *out1, uint8_t *out2, |
| 485 |
uint8_t *out3, size_t out_len) { |
| 486 |
KECCAK1600_CTX_x4 ctx; |
| 487 |
OPENSSL_memset(&ctx, 0, sizeof(ctx)); |
| 488 |
size_t nblocks = out_len / SHAKE256_BLOCKSIZE; |
| 489 |
uint8_t tmp0[SHAKE256_BLOCKSIZE]; |
| 490 |
uint8_t tmp1[SHAKE256_BLOCKSIZE]; |
| 491 |
uint8_t tmp2[SHAKE256_BLOCKSIZE]; |
| 492 |
uint8_t tmp3[SHAKE256_BLOCKSIZE]; |
| 493 |
|
| 494 |
SHAKE256_Absorb_once_x4(&ctx, data0, data1, data2, data3, in_len); |
| 495 |
SHAKE256_Squeezeblocks_x4(out0, out1, out2, out3, &ctx, nblocks); |
| 496 |
|
| 497 |
out0 += nblocks * SHAKE256_BLOCKSIZE; |
| 498 |
out1 += nblocks * SHAKE256_BLOCKSIZE; |
| 499 |
out2 += nblocks * SHAKE256_BLOCKSIZE; |
| 500 |
out3 += nblocks * SHAKE256_BLOCKSIZE; |
| 501 |
|
| 502 |
out_len -= nblocks * SHAKE256_BLOCKSIZE; |
| 503 |
|
| 504 |
if (out_len > 0) |
| 505 |
{ |
| 506 |
SHAKE256_Squeezeblocks_x4(tmp0, tmp1, tmp2, tmp3, &ctx, 1); |
| 507 |
OPENSSL_memcpy(out0, tmp0, out_len); |
| 508 |
OPENSSL_memcpy(out1, tmp1, out_len); |
| 509 |
OPENSSL_memcpy(out2, tmp2, out_len); |
| 510 |
OPENSSL_memcpy(out3, tmp3, out_len); |
| 511 |
} |
| 512 |
|
| 513 |
OPENSSL_cleanse(tmp0, sizeof(tmp0)); |
| 514 |
OPENSSL_cleanse(tmp1, sizeof(tmp1)); |
| 515 |
OPENSSL_cleanse(tmp2, sizeof(tmp2)); |
| 516 |
OPENSSL_cleanse(tmp3, sizeof(tmp3)); |
| 517 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 518 |
|
| 519 |
return 1; |
| 520 |
} |