2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/sha1.h"
|
|
|
|
#include "mbedtls/sha256.h"
|
|
|
|
#include "mbedtls/sha512.h"
|
2022-05-09 01:04:34 +02:00
|
|
|
#include "mbedtls/sha3.h"
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_sha1(data_t *src_str, data_t *hash)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
unsigned char output[41];
|
|
|
|
|
|
|
|
memset(output, 0x00, 41);
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_sha1(src_str->x, src_str->len, output) == 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, 20, hash->len) == 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-29 16:44:38 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sha256_invalid_param()
|
2019-01-31 14:20:20 +01:00
|
|
|
{
|
|
|
|
mbedtls_sha256_context ctx;
|
|
|
|
unsigned char buf[64] = { 0 };
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t const buflen = sizeof(buf);
|
2019-01-31 14:20:20 +01:00
|
|
|
int invalid_type = 42;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
|
|
|
|
mbedtls_sha256_starts(&ctx, invalid_type));
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA,
|
|
|
|
mbedtls_sha256(buf, buflen,
|
|
|
|
buf, invalid_type));
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-19 16:46:28 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA224_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sha224(data_t *src_str, data_t *hash)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
unsigned char output[57];
|
|
|
|
|
|
|
|
memset(output, 0x00, 57);
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_sha256(src_str->x, src_str->len, output, 1), 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 28, hash->len), 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_sha256(data_t *src_str, data_t *hash)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
unsigned char output[65];
|
|
|
|
|
|
|
|
memset(output, 0x00, 65);
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_sha256(src_str->x, src_str->len, output, 0), 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 32, hash->len), 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-29 16:43:04 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sha512_invalid_param()
|
2019-01-31 14:20:20 +01:00
|
|
|
{
|
|
|
|
mbedtls_sha512_context ctx;
|
|
|
|
unsigned char buf[64] = { 0 };
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t const buflen = sizeof(buf);
|
2019-01-31 14:20:20 +01:00
|
|
|
int invalid_type = 42;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
|
|
|
|
mbedtls_sha512_starts(&ctx, invalid_type));
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA,
|
|
|
|
mbedtls_sha512(buf, buflen,
|
|
|
|
buf, invalid_type));
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-19 13:31:59 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA384_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sha384(data_t *src_str, data_t *hash)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
unsigned char output[97];
|
|
|
|
|
|
|
|
memset(output, 0x00, 97);
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_sha512(src_str->x, src_str->len, output, 1), 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 48, hash->len), 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_sha512(data_t *src_str, data_t *hash)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
unsigned char output[129];
|
|
|
|
|
|
|
|
memset(output, 0x00, 129);
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_sha512(src_str->x, src_str->len, output, 0), 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_hexcmp(output, hash->x, 64, hash->len), 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-05 13:30:16 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA1_C:MBEDTLS_SELF_TEST */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sha1_selftest()
|
2009-07-05 13:30:16 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_sha1_self_test(1) == 0);
|
2009-07-05 13:30:16 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-05 13:30:16 +02:00
|
|
|
|
2022-12-14 10:58:02 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA224_C:MBEDTLS_SELF_TEST */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sha224_selftest()
|
2022-12-14 10:58:02 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_sha224_self_test(1), 0);
|
2009-07-05 13:30:16 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-05 13:30:16 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_SELF_TEST */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sha256_selftest()
|
2009-07-05 13:30:16 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_sha256_self_test(1), 0);
|
2009-07-05 13:30:16 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-05 13:30:16 +02:00
|
|
|
|
2022-12-14 08:55:53 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA384_C:MBEDTLS_SELF_TEST */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sha384_selftest()
|
2009-07-05 13:30:16 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_sha384_self_test(1), 0);
|
2009-07-05 13:30:16 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-05 13:30:16 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA512_C:MBEDTLS_SELF_TEST */
|
2023-01-11 14:50:10 +01:00
|
|
|
void sha512_selftest()
|
2009-07-05 13:30:16 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_sha512_self_test(1), 0);
|
2009-07-05 13:30:16 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2022-05-09 01:04:34 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
|
2023-02-08 12:52:18 +01:00
|
|
|
void mbedtls_sha3(int family, data_t *in, data_t *hash)
|
2022-05-09 01:04:34 +02:00
|
|
|
{
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
|
2023-07-20 17:48:18 +02:00
|
|
|
TEST_CALLOC_OR_FAIL(output, hash->len);
|
2022-05-09 01:04:34 +02:00
|
|
|
|
2023-02-08 12:52:18 +01:00
|
|
|
TEST_ASSERT(mbedtls_sha3(family, in->x, in->len, output, hash->len) == 0);
|
2022-05-09 01:04:34 +02:00
|
|
|
|
2023-07-20 17:46:01 +02:00
|
|
|
TEST_BUFFERS_EQUAL(output, hash->len, hash->x, hash->len);
|
2022-05-09 01:04:34 +02:00
|
|
|
|
|
|
|
exit:
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_free(output);
|
2022-05-09 01:04:34 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-06-07 19:27:09 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
|
2023-06-07 18:06:13 +02:00
|
|
|
void sha3_invalid_param()
|
|
|
|
{
|
|
|
|
unsigned char output[32];
|
|
|
|
mbedtls_sha3_context ctx;
|
|
|
|
|
|
|
|
mbedtls_sha3_init(&ctx);
|
|
|
|
TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_NONE), MBEDTLS_ERR_SHA3_BAD_INPUT_DATA);
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_sha3_starts(&ctx, MBEDTLS_SHA3_256), 0);
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_sha3_finish(&ctx, output, 0), MBEDTLS_ERR_SHA3_BAD_INPUT_DATA);
|
|
|
|
TEST_EQUAL(mbedtls_sha3_finish(&ctx, output, 31), MBEDTLS_ERR_SHA3_BAD_INPUT_DATA);
|
|
|
|
TEST_EQUAL(mbedtls_sha3_finish(&ctx, output, 32), 0);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-05-09 01:04:34 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
|
2023-02-08 12:52:18 +01:00
|
|
|
void mbedtls_sha3_multi(int family, data_t *in, data_t *hash)
|
2022-05-09 01:04:34 +02:00
|
|
|
{
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
mbedtls_sha3_context ctx;
|
|
|
|
const unsigned int block_size = 256;
|
|
|
|
|
2023-07-20 17:48:18 +02:00
|
|
|
TEST_CALLOC_OR_FAIL(output, hash->len);
|
2022-05-09 01:04:34 +02:00
|
|
|
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_sha3_init(&ctx);
|
|
|
|
mbedtls_sha3_starts(&ctx, family);
|
2022-05-09 01:04:34 +02:00
|
|
|
|
2023-02-08 12:52:18 +01:00
|
|
|
for (size_t l = 0; l < in->len; l += block_size) {
|
|
|
|
TEST_ASSERT(mbedtls_sha3_update(&ctx, in->x + l, MIN(in->len - l, block_size)) == 0);
|
|
|
|
}
|
2022-05-09 01:04:34 +02:00
|
|
|
|
2023-02-08 12:52:18 +01:00
|
|
|
TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, hash->len) == 0);
|
2022-05-09 01:04:34 +02:00
|
|
|
|
2023-07-20 17:46:01 +02:00
|
|
|
TEST_BUFFERS_EQUAL(output, hash->len, hash->x, hash->len);
|
2022-05-09 01:04:34 +02:00
|
|
|
|
|
|
|
exit:
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_free(output);
|
2022-05-09 01:04:34 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-05-20 20:42:33 +02:00
|
|
|
|
2022-05-20 20:50:29 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
|
2023-02-08 12:52:18 +01:00
|
|
|
void sha3_streaming(int type, data_t *input)
|
2022-05-20 20:50:29 +02:00
|
|
|
{
|
|
|
|
mbedtls_sha3_context ctx;
|
|
|
|
unsigned char reference_hash[64];
|
|
|
|
unsigned char hash[64];
|
|
|
|
size_t chunk_size;
|
2023-02-08 12:52:18 +01:00
|
|
|
size_t hash_length = (type == MBEDTLS_SHA3_224 ? 28 :
|
|
|
|
type == MBEDTLS_SHA3_256 ? 32 :
|
|
|
|
type == MBEDTLS_SHA3_384 ? 48 :
|
|
|
|
type == MBEDTLS_SHA3_512 ? 64 :
|
|
|
|
0);
|
2022-05-20 20:50:29 +02:00
|
|
|
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_sha3_init(&ctx);
|
|
|
|
memset(reference_hash, 0, sizeof(reference_hash));
|
|
|
|
memset(hash, 0, sizeof(hash));
|
|
|
|
TEST_ASSERT(hash_length != 0);
|
2022-05-20 20:50:29 +02:00
|
|
|
|
|
|
|
/* Generate a reference hash */
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_sha3(type, input->x, input->len, reference_hash, hash_length);
|
2022-05-20 20:50:29 +02:00
|
|
|
|
|
|
|
/* Repeat each test with increasingly-sized data chunks
|
|
|
|
* E.g. start by processing bytes individual bytes, then 2-byte chunks,
|
|
|
|
* then 3-byte chunks, and so on...
|
|
|
|
* At each test ensure that the same hash is generated.
|
|
|
|
*/
|
2023-02-08 12:52:18 +01:00
|
|
|
for (chunk_size = 1; chunk_size < input->len; chunk_size++) {
|
2022-05-20 20:50:29 +02:00
|
|
|
size_t i;
|
|
|
|
size_t remaining = input->len;
|
|
|
|
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_sha3_init(&ctx);
|
|
|
|
TEST_ASSERT(mbedtls_sha3_starts(&ctx, type) == 0);
|
2022-05-20 20:50:29 +02:00
|
|
|
|
2023-02-08 12:52:18 +01:00
|
|
|
for (i = 0; i < input->len; i += chunk_size) {
|
2022-05-20 20:50:29 +02:00
|
|
|
size_t len = remaining >= chunk_size ? chunk_size : remaining;
|
2023-02-08 12:52:18 +01:00
|
|
|
TEST_ASSERT(mbedtls_sha3_update(&ctx, input->x + i, len) == 0);
|
2022-05-20 20:50:29 +02:00
|
|
|
remaining -= len;
|
|
|
|
}
|
|
|
|
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_sha3_finish(&ctx, hash, hash_length);
|
|
|
|
mbedtls_sha3_free(&ctx);
|
2022-05-20 20:50:29 +02:00
|
|
|
|
2023-07-20 17:46:01 +02:00
|
|
|
TEST_BUFFERS_EQUAL(hash, hash_length, reference_hash, hash_length);
|
2022-05-20 20:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_sha3_free(&ctx);
|
2022-05-20 20:50:29 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */
|
2023-02-08 12:52:18 +01:00
|
|
|
void sha3_reuse(data_t *input1, data_t *hash1,
|
|
|
|
data_t *input2, data_t *hash2)
|
2022-05-20 20:50:29 +02:00
|
|
|
{
|
|
|
|
unsigned char output[64];
|
|
|
|
mbedtls_sha3_context ctx;
|
|
|
|
mbedtls_sha3_id type1, type2;
|
|
|
|
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_sha3_init(&ctx);
|
|
|
|
switch (hash1->len) {
|
2022-05-20 20:50:29 +02:00
|
|
|
case 28: type1 = MBEDTLS_SHA3_224; break;
|
|
|
|
case 32: type1 = MBEDTLS_SHA3_256; break;
|
|
|
|
case 48: type1 = MBEDTLS_SHA3_384; break;
|
|
|
|
case 64: type1 = MBEDTLS_SHA3_512; break;
|
2023-02-08 12:52:18 +01:00
|
|
|
default: TEST_ASSERT(!"hash1->len validity"); break;
|
2022-05-20 20:50:29 +02:00
|
|
|
}
|
2023-02-08 12:52:18 +01:00
|
|
|
switch (hash2->len) {
|
2022-05-20 20:50:29 +02:00
|
|
|
case 28: type2 = MBEDTLS_SHA3_224; break;
|
|
|
|
case 32: type2 = MBEDTLS_SHA3_256; break;
|
|
|
|
case 48: type2 = MBEDTLS_SHA3_384; break;
|
|
|
|
case 64: type2 = MBEDTLS_SHA3_512; break;
|
2023-02-08 12:52:18 +01:00
|
|
|
default: TEST_ASSERT(!"hash2->len validity"); break;
|
2022-05-20 20:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Round 1 */
|
2023-02-08 12:52:18 +01:00
|
|
|
TEST_ASSERT(mbedtls_sha3_starts(&ctx, type1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_sha3_update(&ctx, input1->x, input1->len) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, sizeof(output)) == 0);
|
2023-07-20 17:46:01 +02:00
|
|
|
TEST_BUFFERS_EQUAL(output, hash1->len, hash1->x, hash1->len);
|
2022-05-20 20:50:29 +02:00
|
|
|
|
|
|
|
/* Round 2 */
|
2023-02-08 12:52:18 +01:00
|
|
|
TEST_ASSERT(mbedtls_sha3_starts(&ctx, type2) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_sha3_update(&ctx, input2->x, input2->len) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, sizeof(output)) == 0);
|
2023-07-20 17:46:01 +02:00
|
|
|
TEST_BUFFERS_EQUAL(output, hash2->len, hash2->x, hash2->len);
|
2022-05-20 20:50:29 +02:00
|
|
|
|
|
|
|
exit:
|
2023-02-08 12:52:18 +01:00
|
|
|
mbedtls_sha3_free(&ctx);
|
2022-05-20 20:50:29 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-05-20 20:42:33 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA3_C:MBEDTLS_SELF_TEST */
|
|
|
|
void sha3_selftest()
|
|
|
|
{
|
2023-02-08 12:52:18 +01:00
|
|
|
TEST_ASSERT(mbedtls_sha3_self_test(0) == 0);
|
2022-05-20 20:42:33 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|