Files
2026-08-24 10:03:24 +08:00

675 lines
24 KiB
C

// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
* Copyright (c) 2019 Amlogic, Inc. All rights reserved.
*/
#include <crypto_internal.h>
#include <amlogic/aml_crypto.h>
#include <string.h>
#include <vsprintf.h>
#include <stdio.h>
int32_t aml_hw_sha2_init(sha2_ctx *ctx, uint32_t is224)
{
if (!ctx)
return CRYPTO_ERROR_BAD_PARAMETERS;
memset(ctx, 0, sizeof(*ctx));
ctx->digest_len = is224 ? 224 : 256;
return CRYPTO_ERROR_NO_ERROR;
}
int32_t aml_hw_sha2_update(sha2_ctx *ctx, const uint8_t *input, uint32_t ilen)
{
uint32_t to_fill = 0;
int32_t ret = CRYPTO_ERROR_NO_ERROR;
uint32_t residue = 0;
if (!ctx || !input || !ilen)
return CRYPTO_ERROR_BAD_PARAMETERS;
if (ctx->buf_len > 0) {
to_fill = (SHA256_BLOCK_SIZE - ctx->buf_len) <= ilen ?
(SHA256_BLOCK_SIZE - ctx->buf_len) : ilen;
memcpy(ctx->buf + ctx->buf_len, input, to_fill);
input += to_fill;
ctx->buf_len += to_fill;
ilen -= to_fill;
if (ctx->buf_len == SHA256_BLOCK_SIZE) {
ret = sha2_update_internal(ctx, ctx->buf, ctx->buf_len, NULL, 0);
if (ret != CRYPTO_ERROR_NO_ERROR)
return ret;
ctx->buf_len = 0;
} else
return CRYPTO_ERROR_NO_ERROR;
}
residue = ilen % SHA256_BLOCK_SIZE;
ret = sha2_update_internal(ctx, input, ilen - residue, NULL, 0);
if (ret != CRYPTO_ERROR_NO_ERROR)
return ret;
input += ilen - residue;
memcpy(ctx->buf, input, residue);
ctx->buf_len = residue;
return ret;
}
int32_t aml_hw_sha2_final(sha2_ctx *ctx, uint8_t *hash)
{
return sha2_update_internal(ctx, ctx->buf, ctx->buf_len, hash, 1);
}
int32_t aml_hw_sha3_init(sha3_ctx *ctx, uint32_t mode)
{
if (!ctx || mode >= SHA3_MAX)
return CRYPTO_ERROR_BAD_PARAMETERS;
memset(ctx, 0, sizeof(*ctx));
ctx->mode = mode;
switch (ctx->mode) {
case SHA3_224:
ctx->digest_len = SHA3_224_DIGEST_SIZE;
ctx->block_size = SHA3_224_RATE;
break;
case SHA3_256:
ctx->digest_len = SHA3_256_DIGEST_SIZE;
ctx->block_size = SHA3_256_RATE;
break;
case SHA3_384:
ctx->digest_len = SHA3_384_DIGEST_SIZE;
ctx->block_size = SHA3_384_RATE;
break;
case SHA3_512:
ctx->digest_len = SHA3_512_DIGEST_SIZE;
ctx->block_size = SHA3_512_RATE;
break;
case SHA3_SHAKE_128:
ctx->digest_len = 16;
ctx->block_size = SHAKE_128_RATE;
break;
case SHA3_SHAKE_256:
ctx->digest_len = 32;
ctx->block_size = SHAKE_256_RATE;
break;
default:
ctx->digest_len = 0;
break;
}
return CRYPTO_ERROR_NO_ERROR;
}
int32_t aml_hw_sha3_update(sha3_ctx *ctx, const uint8_t *input, uint32_t ilen)
{
int32_t ret = CRYPTO_ERROR_NO_ERROR;
#ifndef NO_BLOCK_BUFFERING_FOR_SHA3
uint32_t to_fill = 0;
#endif
uint32_t residue = 0;
if (!ctx || !input || !ilen)
return CRYPTO_ERROR_BAD_PARAMETERS;
#ifndef NO_BLOCK_BUFFERING_FOR_SHA3
if (ctx->buf_len > 0) {
to_fill = (ctx->block_size - ctx->buf_len) <= ilen ?
(ctx->block_size - ctx->buf_len) : ilen;
memcpy(ctx->buf + ctx->buf_len, input, to_fill);
input += to_fill;
ctx->buf_len += to_fill;
ilen -= to_fill;
if (ctx->buf_len == ctx->block_size) {
ret = sha3_update_internal(ctx, ctx->buf, ctx->buf_len, NULL, 0, 0);
if (ret != CRYPTO_ERROR_NO_ERROR)
return ret;
ctx->buf_len = 0;
} else
return CRYPTO_ERROR_NO_ERROR;
}
#endif
#ifndef NO_BLOCK_BUFFERING_FOR_SHA3
residue = ilen % ctx->block_size;
#endif
ret = sha3_update_internal(ctx, input, ilen - residue, NULL, 0, 0);
if (ret != CRYPTO_ERROR_NO_ERROR)
return ret;
#ifndef NO_BLOCK_BUFFERING_FOR_SHA3
input += ilen - residue;
memcpy(ctx->buf, input, residue);
ctx->buf_len = residue;
#endif
return ret;
}
int32_t aml_hw_sha3_final(sha3_ctx *ctx, uint8_t *digest, uint32_t digest_size)
{
int32_t ret = CRYPTO_ERROR_NO_ERROR;
ret = sha3_update_internal(ctx, ctx->buf, ctx->buf_len, digest, digest_size, 1);
if (ret != CRYPTO_ERROR_NO_ERROR) {
printf("sha3 final failed\n");
return ret;
}
if (ctx->mode >= SHA3_SHAKE_128)
return sha3_shake_squeeze_internal(ctx, digest, digest_size);
return ret;
}
//#define SHA_TEST_CASE
#ifdef SHA_TEST_CASE
#include <command.h>
static uint8_t sha2_256_361byte_data[361] = {
0xdd, 0x5b, 0x66, 0x93, 0x10, 0x50, 0x8a, 0x14, 0xd4,
0x6d, 0xdb, 0x5e, 0x37, 0xc6, 0x1f, 0xa3, 0x96, 0x72, 0x75, 0xc8, 0xd4, 0x72, 0x5f, 0x54, 0x87,
0x2a, 0x0d, 0xaf, 0xd6, 0x35, 0x14, 0x4a, 0x4b, 0x15, 0x08, 0xfc, 0x7c, 0x1d, 0x39, 0x75, 0x36,
0x15, 0x3f, 0x94, 0x30, 0xad, 0x2e, 0xce, 0x9d, 0x02, 0x69, 0x80, 0xeb, 0x35, 0x47, 0xd8, 0x54,
0xd7, 0x9a, 0x8b, 0xea, 0xff, 0x42, 0x4f, 0x99, 0xeb, 0xc3, 0x07, 0xe7, 0x24, 0x36, 0xa1, 0xe5,
0x91, 0x66, 0x62, 0x10, 0x2b, 0x64, 0x1d, 0x17, 0x34, 0x39, 0x12, 0xe1, 0x6f, 0x3e, 0xc8, 0xed,
0x9b, 0x49, 0x9b, 0x66, 0x56, 0x8c, 0x92, 0x04, 0x9f, 0xd0, 0x45, 0x3f, 0xa5, 0x3b, 0x83, 0x81,
0x3b, 0xfd, 0xf3, 0x14, 0x30, 0xe3, 0x05, 0x53, 0xca, 0xd4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0x0d, 0xe5, 0xbb, 0xf5, 0x73, 0x08, 0xfb, 0x8b, 0x4f, 0x27, 0x95, 0x5d, 0x46,
0x64, 0xdf, 0x7c, 0x40, 0xd3, 0x03, 0xee, 0x5b, 0x8f, 0xe9, 0x1a, 0x6f, 0x33, 0x3a, 0x7f, 0x63,
0xae, 0xef, 0x17, 0x72, 0xb3, 0xfa, 0xe8, 0xb8, 0xd3, 0xf6, 0xf2, 0xe1, 0x31, 0x7e, 0xc9, 0x24,
0x0c, 0x79, 0x51, 0x78, 0x74, 0x05, 0x41, 0x20, 0xb5, 0xe0, 0xf0, 0xd0, 0x8c, 0x90, 0x4b, 0x3c,
};
static const uint32_t sha2_256_361byte_data_sz = sizeof(sha2_256_361byte_data) / sizeof(uint8_t);
static uint8_t sha256_361byte_digest[SHA256_DIGEST_SIZE] = {
0xaf, 0x6e, 0x73, 0x8f, 0x80, 0x62, 0x2d, 0xc5, 0xf9, 0xf6, 0x3c, 0x02, 0x40, 0x39, 0x37, 0x15,
0x1e, 0x87, 0x03, 0xdc, 0x4e, 0xdc, 0xac, 0x1e, 0xdb, 0xfa, 0x3e, 0x51, 0x0a, 0x8e, 0x50, 0xec,
};
static uint8_t sha224_361byte_digest[SHA224_DIGEST_SIZE] = {
0x6c, 0x33, 0x8d, 0xbf, 0x05, 0x63, 0x02, 0x4d, 0x7d, 0x1f, 0xca, 0xe1, 0x17, 0x66, 0x4b, 0xc9,
0x2e, 0xc4, 0xf7, 0x1d, 0xfc, 0x00, 0x80, 0x14, 0xda, 0x3c, 0x98, 0x64
};
static void test_sha256(void)
{
uint8_t sha2_256_361byte_data_little[sha2_256_361byte_data_sz];
uint8_t dst[SHA256_DIGEST_SIZE];
int i = 0;
sha2_ctx ctx;
printf("SHA 256:");
for (i = 0; i < sizeof(sha2_256_361byte_data_little); i++)
sha2_256_361byte_data_little[i] = sha2_256_361byte_data[sha2_256_361byte_data_sz - 1 - i];
memset(dst, 0, SHA256_DIGEST_SIZE);
aml_hw_sha2_init(&ctx, 0);
for (i = 0; i < sizeof(sha2_256_361byte_data_little); i++)
aml_hw_sha2_update(&ctx, &sha2_256_361byte_data_little[i], 1);
aml_hw_sha2_final(&ctx, dst);
if (!memcmp(dst, sha256_361byte_digest, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
static void test_sha224(void)
{
/* Reuse vector */
uint8_t sha2_256_361byte_data_little[sha2_256_361byte_data_sz];
uint8_t dst[SHA224_DIGEST_SIZE];
int i = 0;
uint32_t consumed = 0;
uint32_t remaining = 0;
uint8_t *ptr = sha2_256_361byte_data_little;
sha2_ctx ctx;
printf("SHA 224:");
for (i = 0; i < sizeof(sha2_256_361byte_data_little); i++)
sha2_256_361byte_data_little[i] = sha2_256_361byte_data[sha2_256_361byte_data_sz - 1 - i];
memset(dst, 0, sizeof(dst));
aml_hw_sha2_init(&ctx, 1);
remaining = sizeof(sha2_256_361byte_data_little);
while (remaining > 0) {
consumed = (remaining >= SHA224_BLOCK_SIZE)? SHA224_BLOCK_SIZE: remaining;
remaining -= consumed;
aml_hw_sha2_update(&ctx, ptr, consumed);
ptr += consumed;
}
aml_hw_sha2_final(&ctx, dst);
if (!memcmp(dst, sha224_361byte_digest, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
const uint8_t sha3_224_msg[] = {
0xc7, 0xc5, 0x78, 0x4b, 0x44, 0x70, 0x52, 0x67, 0x2c, 0x41, 0x81, 0x38, 0xe3, 0xe2, 0x95, 0x59,
0xa5, 0x68, 0xde, 0x2c, 0x61, 0xd7, 0xdf, 0x79, 0xcb, 0xe9, 0x00, 0x57, 0xad, 0x0b, 0x83, 0x50,
0x7d, 0xa9, 0xb9, 0xc0, 0x35, 0xab, 0x76, 0x7e, 0x5f, 0x40, 0xc2, 0xbe, 0x6f, 0xda, 0xd1, 0x36,
0x56, 0x7a, 0x36, 0x80, 0x54, 0x2d, 0x53, 0xc0, 0x0e, 0xd6, 0x14, 0x86, 0xe0, 0x2f, 0xdd, 0xf6,
0x74, 0x0b, 0xc0, 0x26, 0x94, 0xde, 0xf4, 0xc7, 0x3c, 0x7e, 0x82, 0x08, 0xf4, 0x2b, 0x42, 0xb7,
0x5c, 0xce, 0x06, 0xa9, 0x09, 0x7e, 0x15, 0x5d, 0x8f, 0x48, 0xdb, 0xdc, 0xdf, 0x30, 0xde, 0xe3,
0xd4, 0x73, 0xf4, 0x44, 0x08, 0x0f, 0xb4, 0x8a, 0xec, 0x85, 0x2a, 0xdc, 0x18, 0xde, 0xcf, 0x24,
0xdf, 0xec, 0xb7, 0x70, 0x27, 0xd2, 0x0d, 0x98, 0x77, 0xc7, 0xbd, 0x21, 0x52, 0x16, 0x70, 0x61,
0xc4, 0x69, 0xbd, 0xe4, 0x3a, 0x48, 0x9d, 0x0f, 0x97, 0xdd, 0x20, 0x03, 0x83, 0xfa, 0x5f, 0xc4,
};
const uint8_t sha3_224_md[] = {
0x2c, 0x91, 0x6f, 0x82, 0x7d, 0xd3, 0x04, 0x89, 0x70, 0xb5, 0x67, 0xb2, 0x0d, 0x24, 0xe1, 0xc0,
0x46, 0x51, 0xe7, 0x69, 0x1b, 0xb5, 0xeb, 0xe7, 0xca, 0x87, 0x8c, 0x75,
};
static void test_sha3_224(void)
{
uint8_t dst[SHA224_DIGEST_SIZE];
sha3_ctx ctx;
int i = 0;
printf("SHA3 224:");
memset(dst, 0, sizeof(dst));
aml_hw_sha3_init(&ctx, SHA3_224);
for (i = 0; i < sizeof(sha3_224_msg); i++)
aml_hw_sha3_update(&ctx, &sha3_224_msg[i], 1);
aml_hw_sha3_final(&ctx, dst, SHA224_DIGEST_SIZE);
if (!memcmp(dst, sha3_224_md, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
const uint8_t sha3_256_msg[] = {
0x35, 0x5f, 0x6e, 0xcd, 0x76, 0xdb, 0x7f, 0x49, 0x37, 0x3a, 0xba, 0xd5, 0x20, 0x17, 0x9a, 0x0e,
0x2b, 0x15, 0x88, 0xca, 0xa7, 0xd1, 0xfe, 0xa9, 0x83, 0x15, 0xed, 0xed, 0xc9, 0x4f, 0x5c, 0x8b,
0x69, 0x41, 0x52, 0x81, 0x38, 0xf9, 0xf7, 0x6b, 0x38, 0x04, 0xf6, 0x51, 0x1a, 0xdf, 0x25, 0xc4,
0x01, 0xae, 0x68, 0x29, 0x89, 0x30, 0xd3, 0x54, 0x08, 0xf3, 0x0e, 0x59, 0x14, 0xc4, 0xfe, 0x9e,
0x45, 0x6a, 0xa1, 0x35, 0x3b, 0x94, 0x3a, 0x69, 0xe3, 0x0f, 0xaf, 0x97, 0xf7, 0x3c, 0x2f, 0x40,
0xc4, 0xc2, 0x17, 0x5b, 0x30, 0x80, 0x17, 0xf0, 0xfb, 0xc6, 0x91, 0xe2, 0x45, 0xc4, 0x41, 0x0d,
0x51, 0x42, 0x23, 0x90, 0x88, 0x92, 0x91, 0x6c, 0xc1, 0xc3, 0xac, 0x4f, 0xbf, 0x18, 0xfe, 0xaa,
0xfb, 0xb8, 0x5e, 0x09, 0xa4, 0xa7, 0x13, 0xa4, 0xe5, 0xf5, 0x3c, 0x31, 0x66, 0x36, 0x6f, 0xfd,
0x15, 0x32, 0xa0, 0x3b, 0x26, 0xdd, 0x44, 0x9c,
};
const uint8_t sha3_256_md[] = {
0x8c, 0xd9, 0x57, 0x8b, 0xad, 0xf0, 0x24, 0x09, 0xcc, 0x7d, 0x33, 0x66, 0x00, 0xeb, 0x79, 0x63,
0x40, 0xa3, 0x13, 0x22, 0x36, 0xcb, 0x3a, 0xf7, 0xbe, 0xa7, 0x68, 0x6d, 0xdd, 0xdb, 0x7f, 0xa2,
};
static void test_sha3_256(void)
{
uint8_t dst[SHA256_DIGEST_SIZE];
sha3_ctx ctx;
printf("SHA3 256:");
memset(dst, 0, sizeof(dst));
aml_hw_sha3_init(&ctx, SHA3_256);
aml_hw_sha3_update(&ctx, sha3_256_msg, sizeof(sha3_256_msg));
aml_hw_sha3_final(&ctx, dst, SHA256_DIGEST_SIZE);
if (!memcmp(dst, sha3_256_md, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
const uint8_t sha3_384_msg[] = {
0x0b, 0x40, 0x05, 0x3b, 0xfc, 0xbe, 0xd5, 0x29, 0x56, 0x7e, 0x6c, 0x27, 0x55, 0xde, 0x79, 0xb7,
0xbb, 0xa3, 0x7e, 0xe5, 0xba, 0x4c, 0xd4, 0x35, 0x14, 0xa0, 0xf0, 0xda, 0x81, 0x8a, 0xe4, 0x28,
0x51, 0x26, 0x28, 0x4c, 0x52, 0x1c, 0x9b, 0x3b, 0xf4, 0x2d, 0xc5, 0xe8, 0xab, 0x0b, 0x5f, 0xba,
0x95, 0x6e, 0x15, 0x3d, 0x5d, 0x64, 0x8a, 0x18, 0x5f, 0xe8, 0x9d, 0x3f, 0x0d, 0xb7, 0xeb, 0x69,
0x91, 0x5f, 0x94, 0xc5, 0xb4, 0x99, 0x43, 0xe8, 0xfe, 0xd8, 0x68, 0x0a, 0x1e, 0x22, 0xc8, 0x73,
0x8d, 0x1f, 0x38, 0x30, 0x6f, 0x70, 0xa5, 0x0a, 0xba, 0x41, 0x58, 0xb9, 0x99, 0x22, 0x78, 0x54,
0x13, 0x12, 0xd1, 0x0c, 0xe8, 0xdd, 0xd3, 0x1a,
};
const uint8_t sha3_384_md[] = {
0xc6, 0x8b, 0x40, 0x52, 0x78, 0x52, 0x85, 0xf6, 0x85, 0xd9, 0xc5, 0x19, 0x91, 0xcd, 0x30, 0xb3,
0xe9, 0x2b, 0x3d, 0x7f, 0xf5, 0x23, 0x4f, 0xeb, 0x77, 0xa7, 0xae, 0x1a, 0x6a, 0xf0, 0x8e, 0xe6,
0xac, 0x8d, 0xdc, 0x58, 0x5d, 0x2a, 0x0a, 0x30, 0xda, 0xf2, 0xda, 0x04, 0xd6, 0x6e, 0xb5, 0xe4,
};
static void test_sha3_384(void)
{
uint8_t dst[SHA384_DIGEST_SIZE];
sha3_ctx ctx;
printf("SHA3 384:");
memset(dst, 0, sizeof(dst));
aml_hw_sha3_init(&ctx, SHA3_384);
aml_hw_sha3_update(&ctx, sha3_384_msg, sizeof(sha3_384_msg));
aml_hw_sha3_final(&ctx, dst, SHA384_DIGEST_SIZE);
if (!memcmp(dst, sha3_384_md, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
const uint8_t sha3_512_msg[] = {
0xf0, 0xe0, 0x39, 0x62, 0x39, 0xf1, 0x7d, 0x6f, 0xc2, 0x83, 0x88, 0x50, 0x62, 0x19, 0x9d, 0xc3,
0x2d, 0x8c, 0x08, 0x7d, 0x54, 0x5b, 0x17, 0x1e, 0x46, 0x42, 0x66, 0x64, 0x1b, 0x0e, 0x5f, 0x9f,
0x33, 0xaf, 0x02, 0x65, 0x1a, 0x8f, 0xe2, 0x86, 0xaf, 0x22, 0x70, 0x42, 0x86, 0xbf, 0x7c, 0xba,
0x8a, 0xae, 0xf9, 0xa5, 0xe7, 0x82, 0x01, 0x44, 0x27, 0xa8, 0x19, 0x98, 0x9a, 0x41, 0xb5, 0xd2,
0xfc, 0x2d, 0xff, 0x0b, 0x52, 0x68, 0xd4, 0x35,
};
const uint8_t sha3_512_md[] = {
0xb6, 0xec, 0x54, 0x39, 0x7e, 0xba, 0x3f, 0x9e, 0x92, 0x04, 0x96, 0x99, 0xf3, 0x49, 0x20, 0xff,
0x17, 0xe3, 0x3c, 0x7d, 0xab, 0xad, 0xb3, 0x2b, 0xdd, 0xca, 0xf2, 0xe5, 0x5a, 0x6b, 0x82, 0x40,
0x0e, 0x7c, 0xef, 0x3a, 0xfa, 0x25, 0x1c, 0xe9, 0x2b, 0xde, 0xd3, 0x00, 0x14, 0xe9, 0x92, 0xf6,
0xc8, 0x92, 0x7f, 0xd8, 0x0d, 0x18, 0xf3, 0x46, 0x68, 0x1a, 0x84, 0xab, 0x3b, 0xe5, 0x4e, 0x16,
};
static void test_sha3_512(void)
{
uint8_t dst[SHA512_DIGEST_SIZE];
sha3_ctx ctx;
printf("SHA3 512:");
memset(dst, 0, sizeof(dst));
aml_hw_sha3_init(&ctx, SHA3_512);
aml_hw_sha3_update(&ctx, sha3_512_msg, sizeof(sha3_512_msg) - 1);
aml_hw_sha3_update(&ctx, sha3_512_msg + sizeof(sha3_512_msg) - 1, 1);
aml_hw_sha3_final(&ctx, dst, SHA512_DIGEST_SIZE);
if (!memcmp(dst, sha3_512_md, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
const uint8_t shake_128_msg[] = {
0x5d, 0x8f, 0x84, 0xb2, 0xf2, 0x08, 0xb5, 0x8a, 0x68, 0xe8, 0x8c, 0xe8, 0xef, 0xb5, 0x43, 0xa8,
0x40, 0x4f, 0x0e, 0xc0, 0xc9, 0x80, 0x5c, 0x76, 0x0a, 0xd3, 0x59, 0xd1, 0x3f, 0xaa, 0xb8, 0x4d,
0x3f, 0x8b, 0xb1, 0xd2, 0xa4, 0xbb, 0x45, 0xe7, 0x2c, 0x0e, 0xc9, 0x24, 0x5f, 0xfd, 0xa2, 0xe5,
0x72, 0xf9, 0x4e, 0x46, 0x6c, 0xff, 0xa4, 0x4b, 0x87, 0x6d, 0x5c, 0x5e, 0xd9, 0x14, 0xd1, 0xff,
0x33, 0x8e, 0x06, 0xb7, 0x4a, 0xd1, 0xe7, 0x4d, 0x14, 0x05, 0xd2, 0x3d, 0x75, 0x93, 0x56, 0x66,
0x1b, 0x7f, 0x3b, 0x03, 0xa7, 0xf7, 0xc2, 0xdc, 0x0d, 0x2c, 0x2d, 0xbe, 0x3d, 0x60, 0x82, 0x28,
0x03, 0x40, 0x8d, 0x47, 0x2b, 0x75, 0x24, 0x24, 0xea, 0x76, 0xaf, 0x1d, 0x79, 0xa0, 0xe7, 0x92,
0x03, 0x88, 0xdd, 0xe0, 0xc1, 0x90, 0x3e, 0x93, 0x64, 0xb8, 0xd6, 0xd7, 0xb3, 0xb7, 0x54, 0x30,
0x75, 0x4b, 0x4d, 0x6b, 0x91, 0xcd, 0x83, 0xd5, 0x74, 0x08, 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41, 0x2a, 0x82, 0x6c, 0x4c, 0x84, 0x45, 0xf7, 0x72, 0x03, 0x37, 0x11, 0x91, 0x98,
};
const uint8_t shake_128_md[] = {
0xb4, 0x81, 0x38, 0x95, 0xae, 0x01, 0xb4, 0x3c, 0x9d, 0x9e, 0xd8, 0x5a, 0x8b, 0x03, 0xaa, 0xf4,
};
static void test_shake_128(void)
{
uint8_t dst[16];
sha3_ctx ctx;
printf("SHAKE 128:");
memset(dst, 0, sizeof(dst));
aml_hw_sha3_init(&ctx, SHA3_SHAKE_128);
aml_hw_sha3_update(&ctx, shake_128_msg, sizeof(shake_128_msg));
aml_hw_sha3_final(&ctx, dst, 16);
if (!memcmp(dst, shake_128_md, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
const uint8_t shake_128_variable_msg[] = {
0x0a, 0x13, 0xad, 0x2c, 0x7a, 0x23, 0x9b, 0x4b, 0xa7, 0x3e, 0xa6, 0x59, 0x2a, 0xe8, 0x4e, 0xa9,
};
const uint8_t shake_128_variable_md[140] = {
0x5f, 0xea, 0xf9, 0x9c, 0x15, 0xf4, 0x88, 0x51, 0x94, 0x3f, 0xf9, 0xba, 0xa6, 0xe5, 0x05, 0x5d,
0x83, 0x77, 0xf0, 0xdd, 0x34, 0x7a, 0xa4, 0xdb, 0xec, 0xe5, 0x1a, 0xd3, 0xa6, 0xd9, 0xce, 0x0c,
0x01, 0xae, 0xe9, 0xfe, 0x22, 0x60, 0xb8, 0x0a, 0x46, 0x73, 0xa9, 0x09, 0xb5, 0x32, 0xad, 0xcd,
0xd1, 0xe4, 0x21, 0xc3, 0x2d, 0x64, 0x60, 0x53, 0x5b, 0x5f, 0xe3, 0x92, 0xa5, 0x8d, 0x26, 0x34,
0x97, 0x9a, 0x5a, 0x10, 0x4d, 0x6c, 0x47, 0x0a, 0xa3, 0x30, 0x6c, 0x40, 0x0b, 0x06, 0x1d, 0xb9,
0x1c, 0x46, 0x3b, 0x28, 0x48, 0x29, 0x7b, 0xca, 0x2b, 0xc2, 0x6d, 0x18, 0x64, 0xba, 0x49, 0xd7,
0xff, 0x94, 0x9e, 0xbc, 0xa5, 0x0f, 0xbf, 0x79, 0xa5, 0xe6, 0x37, 0x16, 0xdc, 0x82, 0xb6, 0x00,
0xbd, 0x52, 0xca, 0x74, 0x37, 0xed, 0x77, 0x4d, 0x16, 0x9f, 0x6b, 0xf0, 0x2e, 0x46, 0x48, 0x79,
0x56, 0xfb, 0xa2, 0x23, 0x0f, 0x34, 0xcd, 0x2a, 0x04, 0x85, 0x48, 0x4d,
};
static void test_shake_128_variable(void)
{
uint8_t dst[140];
sha3_ctx ctx;
printf("SHAKE 128 variable:");
memset(dst, 0, sizeof(dst));
aml_hw_sha3_init(&ctx, SHA3_SHAKE_128);
aml_hw_sha3_update(&ctx, shake_128_variable_msg, sizeof(shake_128_variable_msg));
aml_hw_sha3_final(&ctx, dst, 140);
if (!memcmp(dst, shake_128_variable_md, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
const uint8_t shake_256_msg[] = {
0x10, 0x4f, 0xef, 0xe8, 0x9f, 0x08, 0xd1, 0x5d, 0x36, 0xa2, 0x23, 0x3f, 0x42, 0xa7, 0xde, 0xfa,
0x91, 0x7c, 0x5a, 0xd2, 0x64, 0x2e, 0x06, 0xca, 0xc5, 0x6d, 0x5c, 0xc5, 0x1a, 0xd9, 0x14, 0xec,
0xfb, 0x7d, 0x98, 0x4f, 0x41, 0x99, 0xb9, 0xcf, 0x5f, 0xa5, 0xa0, 0x3b, 0xf6, 0x92, 0x07, 0xb9,
0xa3, 0x53, 0xa9, 0x68, 0x1c, 0x9c, 0xf6, 0x43, 0x7b, 0xea, 0x0c, 0x49, 0xd9, 0xc3, 0xe3, 0xdb,
0x1f, 0x3f, 0xc7, 0x65, 0x19, 0xc7, 0x0c, 0x40, 0xcc, 0x1d, 0xfd, 0xd7, 0x0a, 0x9c, 0x15, 0x09,
0x43, 0xc2, 0x72, 0xcf, 0x9e, 0xeb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x8b,
};
const uint8_t shake_256_md[] = {
0x46, 0x29, 0x3a, 0x63, 0xc2, 0x35, 0x75, 0x0d, 0x58, 0xa2, 0x4e, 0xdc, 0xa5, 0xba, 0x63, 0x7b,
0x96, 0xca, 0xe7, 0x43, 0x25, 0xc6, 0xc8, 0x12, 0x2c, 0x41, 0x55, 0xc0, 0xd1, 0x58, 0x05, 0xe6,
};
static void test_shake_256(void)
{
uint8_t dst[SHA256_DIGEST_SIZE];
sha3_ctx ctx;
printf("SHAKE 256:");
memset(dst, 0, sizeof(dst));
aml_hw_sha3_init(&ctx, SHA3_SHAKE_256);
aml_hw_sha3_update(&ctx, shake_256_msg, sizeof(shake_256_msg));
aml_hw_sha3_final(&ctx, dst, SHA256_DIGEST_SIZE);
if (!memcmp(dst, shake_256_md, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
const uint8_t shake_256_variable_msg[] = {
0x8d, 0x80, 0x01, 0xe2, 0xc0, 0x96, 0xf1, 0xb8, 0x8e, 0x7c, 0x92, 0x24, 0xa0, 0x86, 0xef, 0xd4,
0x79, 0x7f, 0xbf, 0x74, 0xa8, 0x03, 0x3a, 0x2d, 0x42, 0x2a, 0x2b, 0x6b, 0x8f, 0x67, 0x47, 0xe4,
};
const uint8_t shake_256_variable_md[250] = {
0x2e, 0x97, 0x5f, 0x6a, 0x8a, 0x14, 0xf0, 0x70, 0x4d, 0x51, 0xb1, 0x36, 0x67, 0xd8, 0x19, 0x5c,
0x21, 0x9f, 0x71, 0xe6, 0x34, 0x56, 0x96, 0xc4, 0x9f, 0xa4, 0xb9, 0xd0, 0x8e, 0x92, 0x25, 0xd3,
0xd3, 0x93, 0x93, 0x42, 0x51, 0x52, 0xc9, 0x7e, 0x71, 0xdd, 0x24, 0x60, 0x1c, 0x11, 0xab, 0xcf,
0xa0, 0xf1, 0x2f, 0x53, 0xc6, 0x80, 0xbd, 0x3a, 0xe7, 0x57, 0xb8, 0x13, 0x4a, 0x9c, 0x10, 0xd4,
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
};
static void test_shake_256_variable(void)
{
uint8_t dst[250];
sha3_ctx ctx;
int i = 0;
printf("SHAKE 256 variable:");
memset(dst, 0, sizeof(dst));
aml_hw_sha3_init(&ctx, SHA3_SHAKE_256);
for (i = 0; i < sizeof(shake_256_variable_msg); i++)
aml_hw_sha3_update(&ctx, &shake_256_variable_msg[i], 1);
aml_hw_sha3_final(&ctx, dst, 250);
if (!memcmp(dst, shake_256_variable_md, sizeof(dst)))
printf("PASS\n");
else
printf("FAIL\n");
return;
}
static int do_sha_test(uint32_t test_case)
{
switch (test_case) {
case 0:
test_sha256();
test_sha224();
return 0;
case 1:
test_sha3_224();
test_sha3_256();
test_sha3_384();
test_sha3_512();
test_shake_128();
test_shake_256();
test_shake_128_variable();
test_shake_256_variable();
return 0;
case 2:
test_sha256();
return 0;
case 3:
test_sha224();
return 0;
case 4:
test_sha3_224();
return 0;
case 5:
test_sha3_256();
return 0;
case 6:
test_sha3_384();
return 0;
case 7:
test_sha3_512();
return 0;
case 8:
test_shake_128();
return 0;
case 9:
test_shake_128_variable();
return 0;
case 10:
test_shake_256();
return 0;
case 11:
test_shake_256_variable();
return 0;
default:
printf("Unknown test case\n");
return 0;
}
}
static int do_hw_sha_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
uint32_t test_case = 0;
char *endp = 0;
if (argc != 2) {
cmd_usage(cmdtp);
return 1;
}
test_case = ustrtoul(argv[1], &endp, 0);
do_sha_test(test_case);
return 0;
}
U_BOOT_CMD(sha_test, 2, 0, do_hw_sha_test,
"sha_test <test case>",
"run small sha test cases, if case=0, run all\n"
);
#endif