2022-05-09 01:04:15 +02:00
|
|
|
/*
|
|
|
|
* FIPS-202 compliant SHA3 implementation
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2022-05-09 01:04:15 +02:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The SHA-3 Secure Hash Standard was published by NIST in 2015.
|
|
|
|
*
|
|
|
|
* https://nvlpubs.nist.gov/nistpubs/fips/nist.fips.202.pdf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SHA3_C)
|
|
|
|
|
|
|
|
#include "mbedtls/sha3.h"
|
|
|
|
#include "mbedtls/platform_util.h"
|
|
|
|
#include "mbedtls/error.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
|
|
|
|
2023-05-29 23:05:19 +02:00
|
|
|
#define XOR_BYTE 0x6
|
|
|
|
|
2024-02-13 18:14:58 +01:00
|
|
|
/* Precomputed masks for the iota transform.
|
|
|
|
*
|
|
|
|
* Each round uses a 64-bit mask value. In each mask values, only
|
|
|
|
* bits whose position is of the form 2^k-1 can be set, thus only
|
|
|
|
* 7 of 64 bits of the mask need to be known for each mask value.
|
|
|
|
*
|
|
|
|
* We use a compressed encoding of the mask where bits 63, 31 and 15
|
|
|
|
* are moved to bits 4-6. This allows us to make each mask value
|
|
|
|
* 1 byte rather than 8 bytes, saving 7*24 = 168 bytes of data (with
|
|
|
|
* perhaps a little variation due to alignment). Decompressing this
|
|
|
|
* requires a little code, but much less than the savings on the table.
|
|
|
|
*
|
|
|
|
* The impact on performance depends on the platform and compiler.
|
|
|
|
* There's a bit more computation, but less memory bandwidth. A quick
|
|
|
|
* benchmark on x86_64 shows a 7% speed improvement with GCC and a
|
|
|
|
* 5% speed penalty with Clang, compared to the naive uint64_t[24] table.
|
|
|
|
* YMMV.
|
|
|
|
*/
|
|
|
|
/* Helper macro to set the values of the higher bits in unused low positions */
|
|
|
|
#define H(b63, b31, b15) (b63 << 6 | b31 << 5 | b15 << 4)
|
|
|
|
static const uint8_t iota_r_packed[24] = {
|
|
|
|
H(0, 0, 0) | 0x01, H(0, 0, 1) | 0x82, H(1, 0, 1) | 0x8a, H(1, 1, 1) | 0x00,
|
|
|
|
H(0, 0, 1) | 0x8b, H(0, 1, 0) | 0x01, H(1, 1, 1) | 0x81, H(1, 0, 1) | 0x09,
|
|
|
|
H(0, 0, 0) | 0x8a, H(0, 0, 0) | 0x88, H(0, 1, 1) | 0x09, H(0, 1, 0) | 0x0a,
|
|
|
|
H(0, 1, 1) | 0x8b, H(1, 0, 0) | 0x8b, H(1, 0, 1) | 0x89, H(1, 0, 1) | 0x03,
|
|
|
|
H(1, 0, 1) | 0x02, H(1, 0, 0) | 0x80, H(0, 0, 1) | 0x0a, H(1, 1, 0) | 0x0a,
|
|
|
|
H(1, 1, 1) | 0x81, H(1, 0, 1) | 0x80, H(0, 1, 0) | 0x01, H(1, 1, 1) | 0x08,
|
2022-05-09 01:04:15 +02:00
|
|
|
};
|
2024-02-13 18:14:58 +01:00
|
|
|
#undef H
|
2022-05-09 01:04:15 +02:00
|
|
|
|
|
|
|
static const uint8_t rho[24] = {
|
|
|
|
1, 62, 28, 27, 36, 44, 6, 55, 20,
|
|
|
|
3, 10, 43, 25, 39, 41, 45, 15,
|
|
|
|
21, 8, 18, 2, 61, 56, 14
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t pi[24] = {
|
|
|
|
10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
|
|
|
|
15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
|
|
|
|
};
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
#define ROT64(x, y) (((x) << (y)) | ((x) >> (64U - (y))))
|
|
|
|
#define ABSORB(ctx, idx, v) do { ctx->state[(idx) >> 3] ^= ((uint64_t) (v)) << (((idx) & 0x7) << 3); \
|
|
|
|
} while (0)
|
|
|
|
#define SQUEEZE(ctx, idx) ((uint8_t) (ctx->state[(idx) >> 3] >> (((idx) & 0x7) << 3)))
|
|
|
|
#define SWAP(x, y) do { uint64_t tmp = (x); (x) = (y); (y) = tmp; } while (0)
|
2022-05-09 01:04:15 +02:00
|
|
|
|
|
|
|
/* The permutation function. */
|
|
|
|
static void keccak_f1600(mbedtls_sha3_context *ctx)
|
|
|
|
{
|
|
|
|
uint64_t lane[5];
|
|
|
|
uint64_t *s = ctx->state;
|
|
|
|
int i;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
for (int round = 0; round < 24; round++) {
|
2022-05-09 01:04:15 +02:00
|
|
|
uint64_t t;
|
|
|
|
|
|
|
|
/* Theta */
|
|
|
|
lane[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20];
|
|
|
|
lane[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21];
|
|
|
|
lane[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22];
|
|
|
|
lane[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23];
|
|
|
|
lane[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24];
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
t = lane[4] ^ ROT64(lane[1], 1);
|
2022-05-09 01:04:15 +02:00
|
|
|
s[0] ^= t; s[5] ^= t; s[10] ^= t; s[15] ^= t; s[20] ^= t;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
t = lane[0] ^ ROT64(lane[2], 1);
|
2022-05-09 01:04:15 +02:00
|
|
|
s[1] ^= t; s[6] ^= t; s[11] ^= t; s[16] ^= t; s[21] ^= t;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
t = lane[1] ^ ROT64(lane[3], 1);
|
2022-05-09 01:04:15 +02:00
|
|
|
s[2] ^= t; s[7] ^= t; s[12] ^= t; s[17] ^= t; s[22] ^= t;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
t = lane[2] ^ ROT64(lane[4], 1);
|
2022-05-09 01:04:15 +02:00
|
|
|
s[3] ^= t; s[8] ^= t; s[13] ^= t; s[18] ^= t; s[23] ^= t;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
t = lane[3] ^ ROT64(lane[0], 1);
|
2022-05-09 01:04:15 +02:00
|
|
|
s[4] ^= t; s[9] ^= t; s[14] ^= t; s[19] ^= t; s[24] ^= t;
|
|
|
|
|
|
|
|
/* Rho */
|
2023-02-08 00:50:04 +01:00
|
|
|
for (i = 1; i < 25; i++) {
|
|
|
|
s[i] = ROT64(s[i], rho[i-1]);
|
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
|
|
|
|
/* Pi */
|
|
|
|
t = s[1];
|
2023-02-08 00:50:04 +01:00
|
|
|
for (i = 0; i < 24; i++) {
|
|
|
|
SWAP(s[pi[i]], t);
|
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
|
|
|
|
/* Chi */
|
|
|
|
lane[0] = s[0]; lane[1] = s[1]; lane[2] = s[2]; lane[3] = s[3]; lane[4] = s[4];
|
|
|
|
s[0] ^= (~lane[1]) & lane[2];
|
|
|
|
s[1] ^= (~lane[2]) & lane[3];
|
|
|
|
s[2] ^= (~lane[3]) & lane[4];
|
|
|
|
s[3] ^= (~lane[4]) & lane[0];
|
|
|
|
s[4] ^= (~lane[0]) & lane[1];
|
|
|
|
|
|
|
|
lane[0] = s[5]; lane[1] = s[6]; lane[2] = s[7]; lane[3] = s[8]; lane[4] = s[9];
|
|
|
|
s[5] ^= (~lane[1]) & lane[2];
|
|
|
|
s[6] ^= (~lane[2]) & lane[3];
|
|
|
|
s[7] ^= (~lane[3]) & lane[4];
|
|
|
|
s[8] ^= (~lane[4]) & lane[0];
|
|
|
|
s[9] ^= (~lane[0]) & lane[1];
|
|
|
|
|
|
|
|
lane[0] = s[10]; lane[1] = s[11]; lane[2] = s[12]; lane[3] = s[13]; lane[4] = s[14];
|
|
|
|
s[10] ^= (~lane[1]) & lane[2];
|
|
|
|
s[11] ^= (~lane[2]) & lane[3];
|
|
|
|
s[12] ^= (~lane[3]) & lane[4];
|
|
|
|
s[13] ^= (~lane[4]) & lane[0];
|
|
|
|
s[14] ^= (~lane[0]) & lane[1];
|
|
|
|
|
|
|
|
lane[0] = s[15]; lane[1] = s[16]; lane[2] = s[17]; lane[3] = s[18]; lane[4] = s[19];
|
|
|
|
s[15] ^= (~lane[1]) & lane[2];
|
|
|
|
s[16] ^= (~lane[2]) & lane[3];
|
|
|
|
s[17] ^= (~lane[3]) & lane[4];
|
|
|
|
s[18] ^= (~lane[4]) & lane[0];
|
|
|
|
s[19] ^= (~lane[0]) & lane[1];
|
|
|
|
|
|
|
|
lane[0] = s[20]; lane[1] = s[21]; lane[2] = s[22]; lane[3] = s[23]; lane[4] = s[24];
|
|
|
|
s[20] ^= (~lane[1]) & lane[2];
|
|
|
|
s[21] ^= (~lane[2]) & lane[3];
|
|
|
|
s[22] ^= (~lane[3]) & lane[4];
|
|
|
|
s[23] ^= (~lane[4]) & lane[0];
|
|
|
|
s[24] ^= (~lane[0]) & lane[1];
|
|
|
|
|
|
|
|
/* Iota */
|
2024-02-13 18:14:58 +01:00
|
|
|
/* Decompress the round masks (see definition of rc) */
|
|
|
|
s[0] ^= ((iota_r_packed[round] & 0x40ull) << 57 |
|
|
|
|
(iota_r_packed[round] & 0x20ull) << 26 |
|
|
|
|
(iota_r_packed[round] & 0x10ull) << 11 |
|
|
|
|
(iota_r_packed[round] & 0x8f));
|
2022-05-09 01:04:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
void mbedtls_sha3_init(mbedtls_sha3_context *ctx)
|
2022-05-09 01:04:15 +02:00
|
|
|
{
|
2023-02-08 00:50:04 +01:00
|
|
|
memset(ctx, 0, sizeof(mbedtls_sha3_context));
|
2022-05-09 01:04:15 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
void mbedtls_sha3_free(mbedtls_sha3_context *ctx)
|
2022-05-09 01:04:15 +02:00
|
|
|
{
|
2023-02-08 00:50:04 +01:00
|
|
|
if (ctx == NULL) {
|
2022-05-09 01:04:15 +02:00
|
|
|
return;
|
2023-02-08 00:50:04 +01:00
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha3_context));
|
2022-05-09 01:04:15 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
void mbedtls_sha3_clone(mbedtls_sha3_context *dst,
|
|
|
|
const mbedtls_sha3_context *src)
|
2022-05-09 01:04:15 +02:00
|
|
|
{
|
|
|
|
*dst = *src;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-3 context setup
|
|
|
|
*/
|
2023-02-08 00:50:04 +01:00
|
|
|
int mbedtls_sha3_starts(mbedtls_sha3_context *ctx, mbedtls_sha3_id id)
|
2022-05-09 01:04:15 +02:00
|
|
|
{
|
2024-02-08 10:47:08 +01:00
|
|
|
switch (id) {
|
|
|
|
case MBEDTLS_SHA3_224:
|
|
|
|
ctx->olen = 224 / 8;
|
|
|
|
ctx->max_block_size = 1152 / 8;
|
2022-05-09 01:04:15 +02:00
|
|
|
break;
|
2024-02-08 10:47:08 +01:00
|
|
|
case MBEDTLS_SHA3_256:
|
|
|
|
ctx->olen = 256 / 8;
|
|
|
|
ctx->max_block_size = 1088 / 8;
|
|
|
|
break;
|
|
|
|
case MBEDTLS_SHA3_384:
|
|
|
|
ctx->olen = 384 / 8;
|
|
|
|
ctx->max_block_size = 832 / 8;
|
|
|
|
break;
|
|
|
|
case MBEDTLS_SHA3_512:
|
|
|
|
ctx->olen = 512 / 8;
|
|
|
|
ctx->max_block_size = 576 / 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return MBEDTLS_ERR_SHA3_BAD_INPUT_DATA;
|
2023-02-08 00:50:04 +01:00
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
memset(ctx->state, 0, sizeof(ctx->state));
|
2022-05-20 16:01:07 +02:00
|
|
|
ctx->index = 0;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
return 0;
|
2022-05-09 01:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-3 process buffer
|
|
|
|
*/
|
2023-02-08 00:50:04 +01:00
|
|
|
int mbedtls_sha3_update(mbedtls_sha3_context *ctx,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t ilen)
|
2022-05-09 01:04:15 +02:00
|
|
|
{
|
2023-05-29 23:04:18 +02:00
|
|
|
if (ilen >= 8) {
|
|
|
|
// 8-byte align index
|
|
|
|
int align_bytes = 8 - (ctx->index % 8);
|
|
|
|
if (align_bytes) {
|
|
|
|
for (; align_bytes > 0; align_bytes--) {
|
|
|
|
ABSORB(ctx, ctx->index, *input++);
|
|
|
|
ilen--;
|
|
|
|
ctx->index++;
|
|
|
|
}
|
|
|
|
if ((ctx->index = ctx->index % ctx->max_block_size) == 0) {
|
|
|
|
keccak_f1600(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process input in 8-byte chunks
|
|
|
|
while (ilen >= 8) {
|
2023-06-07 20:59:05 +02:00
|
|
|
ABSORB(ctx, ctx->index, MBEDTLS_GET_UINT64_LE(input, 0));
|
2023-05-29 23:04:18 +02:00
|
|
|
input += 8;
|
|
|
|
ilen -= 8;
|
|
|
|
if ((ctx->index = (ctx->index + 8) % ctx->max_block_size) == 0) {
|
|
|
|
keccak_f1600(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle remaining bytes
|
2023-02-08 00:50:04 +01:00
|
|
|
while (ilen-- > 0) {
|
|
|
|
ABSORB(ctx, ctx->index, *input++);
|
|
|
|
if ((ctx->index = (ctx->index + 1) % ctx->max_block_size) == 0) {
|
|
|
|
keccak_f1600(ctx);
|
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
return 0;
|
2022-05-09 01:04:15 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
int mbedtls_sha3_finish(mbedtls_sha3_context *ctx,
|
|
|
|
uint8_t *output, size_t olen)
|
2022-05-09 01:04:15 +02:00
|
|
|
{
|
2023-08-30 19:43:23 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2022-05-17 12:53:30 +02:00
|
|
|
/* Catch SHA-3 families, with fixed output length */
|
2023-02-08 00:50:04 +01:00
|
|
|
if (ctx->olen > 0) {
|
|
|
|
if (ctx->olen > olen) {
|
2023-08-30 19:43:23 +02:00
|
|
|
ret = MBEDTLS_ERR_SHA3_BAD_INPUT_DATA;
|
|
|
|
goto exit;
|
2023-02-08 00:50:04 +01:00
|
|
|
}
|
2022-05-17 12:53:30 +02:00
|
|
|
olen = ctx->olen;
|
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
|
2023-05-29 23:05:19 +02:00
|
|
|
ABSORB(ctx, ctx->index, XOR_BYTE);
|
2023-02-08 00:50:04 +01:00
|
|
|
ABSORB(ctx, ctx->max_block_size - 1, 0x80);
|
|
|
|
keccak_f1600(ctx);
|
2022-05-09 01:04:15 +02:00
|
|
|
ctx->index = 0;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
while (olen-- > 0) {
|
|
|
|
*output++ = SQUEEZE(ctx, ctx->index);
|
2022-05-09 01:04:15 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if ((ctx->index = (ctx->index + 1) % ctx->max_block_size) == 0) {
|
|
|
|
keccak_f1600(ctx);
|
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
}
|
|
|
|
|
2023-08-30 19:43:23 +02:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
2023-08-30 20:22:28 +02:00
|
|
|
mbedtls_sha3_free(ctx);
|
2023-08-30 19:43:23 +02:00
|
|
|
return ret;
|
2022-05-09 01:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-06-07 18:08:09 +02:00
|
|
|
* output = SHA-3( input buffer )
|
2022-05-09 01:04:15 +02:00
|
|
|
*/
|
2023-02-08 00:50:04 +01:00
|
|
|
int mbedtls_sha3(mbedtls_sha3_id id, const uint8_t *input,
|
|
|
|
size_t ilen, uint8_t *output, size_t olen)
|
2022-05-09 01:04:15 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_sha3_context ctx;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
mbedtls_sha3_init(&ctx);
|
2022-05-09 01:04:15 +02:00
|
|
|
|
2022-05-17 11:43:15 +02:00
|
|
|
/* Sanity checks are performed in every mbedtls_sha3_xxx() */
|
2023-02-08 00:50:04 +01:00
|
|
|
if ((ret = mbedtls_sha3_starts(&ctx, id)) != 0) {
|
2022-05-09 01:04:15 +02:00
|
|
|
goto exit;
|
2023-02-08 00:50:04 +01:00
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if ((ret = mbedtls_sha3_update(&ctx, input, ilen)) != 0) {
|
2022-05-09 01:04:15 +02:00
|
|
|
goto exit;
|
2023-02-08 00:50:04 +01:00
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if ((ret = mbedtls_sha3_finish(&ctx, output, olen)) != 0) {
|
2022-05-09 01:04:15 +02:00
|
|
|
goto exit;
|
2023-02-08 00:50:04 +01:00
|
|
|
}
|
2022-05-09 01:04:15 +02:00
|
|
|
|
|
|
|
exit:
|
2023-02-08 00:50:04 +01:00
|
|
|
mbedtls_sha3_free(&ctx);
|
2022-05-09 01:04:15 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
return ret;
|
2022-05-09 01:04:15 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 20:42:33 +02:00
|
|
|
/**************** Self-tests ****************/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
|
|
|
|
static const unsigned char test_data[2][4] =
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
"abc",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const size_t test_data_len[2] =
|
|
|
|
{
|
|
|
|
0, /* "" */
|
|
|
|
3 /* "abc" */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char test_hash_sha3_224[2][28] =
|
|
|
|
{
|
|
|
|
{ /* "" */
|
|
|
|
0x6B, 0x4E, 0x03, 0x42, 0x36, 0x67, 0xDB, 0xB7,
|
|
|
|
0x3B, 0x6E, 0x15, 0x45, 0x4F, 0x0E, 0xB1, 0xAB,
|
|
|
|
0xD4, 0x59, 0x7F, 0x9A, 0x1B, 0x07, 0x8E, 0x3F,
|
|
|
|
0x5B, 0x5A, 0x6B, 0xC7
|
|
|
|
},
|
|
|
|
{ /* "abc" */
|
|
|
|
0xE6, 0x42, 0x82, 0x4C, 0x3F, 0x8C, 0xF2, 0x4A,
|
|
|
|
0xD0, 0x92, 0x34, 0xEE, 0x7D, 0x3C, 0x76, 0x6F,
|
|
|
|
0xC9, 0xA3, 0xA5, 0x16, 0x8D, 0x0C, 0x94, 0xAD,
|
|
|
|
0x73, 0xB4, 0x6F, 0xDF
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char test_hash_sha3_256[2][32] =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 0x47, 0xE3, 0x93, 0x40,
|
|
|
|
0x57, 0x34, 0x0B, 0x4C, 0xF4, 0x08, 0xD5, 0xA5,
|
|
|
|
0x65, 0x92, 0xF8, 0x27, 0x4E, 0xEC, 0x53, 0xF0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char long_kat_hash_sha3_224[28] =
|
|
|
|
{
|
|
|
|
0xD6, 0x93, 0x35, 0xB9, 0x33, 0x25, 0x19, 0x2E,
|
|
|
|
0x51, 0x6A, 0x91, 0x2E, 0x6D, 0x19, 0xA1, 0x5C,
|
|
|
|
0xB5, 0x1C, 0x6E, 0xD5, 0xC1, 0x52, 0x43, 0xE7,
|
|
|
|
0xA7, 0xFD, 0x65, 0x3C
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char long_kat_hash_sha3_256[32] =
|
|
|
|
{
|
|
|
|
0x5C, 0x88, 0x75, 0xAE, 0x47, 0x4A, 0x36, 0x34,
|
|
|
|
0xBA, 0x4F, 0xD5, 0x5E, 0xC8, 0x5B, 0xFF, 0xD6,
|
|
|
|
0x61, 0xF3, 0x2A, 0xCA, 0x75, 0xC6, 0xD6, 0x99,
|
|
|
|
0xD0, 0xCD, 0xCB, 0x6C, 0x11, 0x58, 0x91, 0xC1
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char long_kat_hash_sha3_384[48] =
|
|
|
|
{
|
|
|
|
0xEE, 0xE9, 0xE2, 0x4D, 0x78, 0xC1, 0x85, 0x53,
|
|
|
|
0x37, 0x98, 0x34, 0x51, 0xDF, 0x97, 0xC8, 0xAD,
|
|
|
|
0x9E, 0xED, 0xF2, 0x56, 0xC6, 0x33, 0x4F, 0x8E,
|
|
|
|
0x94, 0x8D, 0x25, 0x2D, 0x5E, 0x0E, 0x76, 0x84,
|
|
|
|
0x7A, 0xA0, 0x77, 0x4D, 0xDB, 0x90, 0xA8, 0x42,
|
|
|
|
0x19, 0x0D, 0x2C, 0x55, 0x8B, 0x4B, 0x83, 0x40
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char long_kat_hash_sha3_512[64] =
|
|
|
|
{
|
|
|
|
0x3C, 0x3A, 0x87, 0x6D, 0xA1, 0x40, 0x34, 0xAB,
|
|
|
|
0x60, 0x62, 0x7C, 0x07, 0x7B, 0xB9, 0x8F, 0x7E,
|
|
|
|
0x12, 0x0A, 0x2A, 0x53, 0x70, 0x21, 0x2D, 0xFF,
|
|
|
|
0xB3, 0x38, 0x5A, 0x18, 0xD4, 0xF3, 0x88, 0x59,
|
|
|
|
0xED, 0x31, 0x1D, 0x0A, 0x9D, 0x51, 0x41, 0xCE,
|
|
|
|
0x9C, 0xC5, 0xC6, 0x6E, 0xE6, 0x89, 0xB2, 0x66,
|
|
|
|
0xA8, 0xAA, 0x18, 0xAC, 0xE8, 0x28, 0x2A, 0x0E,
|
|
|
|
0x0D, 0xB5, 0x96, 0xC9, 0x0B, 0x0A, 0x7B, 0x87
|
|
|
|
};
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
static int mbedtls_sha3_kat_test(int verbose,
|
|
|
|
const char *type_name,
|
|
|
|
mbedtls_sha3_id id,
|
|
|
|
int test_num)
|
2022-05-20 20:42:33 +02:00
|
|
|
{
|
|
|
|
uint8_t hash[64];
|
|
|
|
int result;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
result = mbedtls_sha3(id,
|
|
|
|
test_data[test_num], test_data_len[test_num],
|
|
|
|
hash, sizeof(hash));
|
|
|
|
if (result != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" %s test %d error code: %d\n",
|
|
|
|
type_name, test_num, result);
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
return result;
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
switch (id) {
|
2022-05-20 20:42:33 +02:00
|
|
|
case MBEDTLS_SHA3_224:
|
2023-02-08 00:50:04 +01:00
|
|
|
result = memcmp(hash, test_hash_sha3_224[test_num], 28);
|
2022-05-20 20:42:33 +02:00
|
|
|
break;
|
|
|
|
case MBEDTLS_SHA3_256:
|
2023-02-08 00:50:04 +01:00
|
|
|
result = memcmp(hash, test_hash_sha3_256[test_num], 32);
|
2022-05-20 20:42:33 +02:00
|
|
|
break;
|
|
|
|
case MBEDTLS_SHA3_384:
|
2023-02-08 00:50:04 +01:00
|
|
|
result = memcmp(hash, test_hash_sha3_384[test_num], 48);
|
2022-05-20 20:42:33 +02:00
|
|
|
break;
|
|
|
|
case MBEDTLS_SHA3_512:
|
2023-02-08 00:50:04 +01:00
|
|
|
result = memcmp(hash, test_hash_sha3_512[test_num], 64);
|
2022-05-20 20:42:33 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (0 != result) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" %s test %d failed\n", type_name, test_num);
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
return -1;
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" %s test %d passed\n", type_name, test_num);
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
return 0;
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
static int mbedtls_sha3_long_kat_test(int verbose,
|
|
|
|
const char *type_name,
|
|
|
|
mbedtls_sha3_id id)
|
2022-05-20 20:42:33 +02:00
|
|
|
{
|
|
|
|
mbedtls_sha3_context ctx;
|
|
|
|
unsigned char buffer[1000];
|
|
|
|
unsigned char hash[64];
|
|
|
|
int result = 0;
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
memset(buffer, 'a', 1000);
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" %s long KAT test ", type_name);
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
mbedtls_sha3_init(&ctx);
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
result = mbedtls_sha3_starts(&ctx, id);
|
|
|
|
if (result != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("setup failed\n ");
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process 1,000,000 (one million) 'a' characters */
|
2023-06-07 18:09:47 +02:00
|
|
|
for (int i = 0; i < 1000; i++) {
|
2023-02-08 00:50:04 +01:00
|
|
|
result = mbedtls_sha3_update(&ctx, buffer, 1000);
|
|
|
|
if (result != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("update error code: %i\n", result);
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
result = mbedtls_sha3_finish(&ctx, hash, sizeof(hash));
|
|
|
|
if (result != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("finish error code: %d\n", result);
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
switch (id) {
|
2022-05-20 20:42:33 +02:00
|
|
|
case MBEDTLS_SHA3_224:
|
2023-02-08 00:50:04 +01:00
|
|
|
result = memcmp(hash, long_kat_hash_sha3_224, 28);
|
2022-05-20 20:42:33 +02:00
|
|
|
break;
|
|
|
|
case MBEDTLS_SHA3_256:
|
2023-02-08 00:50:04 +01:00
|
|
|
result = memcmp(hash, long_kat_hash_sha3_256, 32);
|
2022-05-20 20:42:33 +02:00
|
|
|
break;
|
|
|
|
case MBEDTLS_SHA3_384:
|
2023-02-08 00:50:04 +01:00
|
|
|
result = memcmp(hash, long_kat_hash_sha3_384, 48);
|
2022-05-20 20:42:33 +02:00
|
|
|
break;
|
|
|
|
case MBEDTLS_SHA3_512:
|
2023-02-08 00:50:04 +01:00
|
|
|
result = memcmp(hash, long_kat_hash_sha3_512, 64);
|
2022-05-20 20:42:33 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (result != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("failed\n");
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n");
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2023-02-08 00:50:04 +01:00
|
|
|
mbedtls_sha3_free(&ctx);
|
|
|
|
return result;
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
int mbedtls_sha3_self_test(int verbose)
|
2022-05-20 20:42:33 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2023-06-07 18:08:09 +02:00
|
|
|
/* SHA-3 Known Answer Tests (KAT) */
|
2023-02-08 00:50:04 +01:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (0 != mbedtls_sha3_kat_test(verbose,
|
|
|
|
"SHA3-224", MBEDTLS_SHA3_224, i)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (0 != mbedtls_sha3_kat_test(verbose,
|
|
|
|
"SHA3-256", MBEDTLS_SHA3_256, i)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (0 != mbedtls_sha3_kat_test(verbose,
|
|
|
|
"SHA3-384", MBEDTLS_SHA3_384, i)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (0 != mbedtls_sha3_kat_test(verbose,
|
|
|
|
"SHA3-512", MBEDTLS_SHA3_512, i)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 18:08:09 +02:00
|
|
|
/* SHA-3 long KAT tests */
|
2023-02-08 00:50:04 +01:00
|
|
|
if (0 != mbedtls_sha3_long_kat_test(verbose,
|
|
|
|
"SHA3-224", MBEDTLS_SHA3_224)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (0 != mbedtls_sha3_long_kat_test(verbose,
|
|
|
|
"SHA3-256", MBEDTLS_SHA3_256)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (0 != mbedtls_sha3_long_kat_test(verbose,
|
|
|
|
"SHA3-384", MBEDTLS_SHA3_384)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (0 != mbedtls_sha3_long_kat_test(verbose,
|
|
|
|
"SHA3-512", MBEDTLS_SHA3_512)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("\n");
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:50:04 +01:00
|
|
|
return 0;
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
|
|
|
|
2022-05-09 01:04:15 +02:00
|
|
|
#endif /* MBEDTLS_SHA3_C */
|