Merge pull request #645 from dgarske/c2000_sha_examples
TI C2000 example: add standalone SHA-2 and SHA-3 examplespull/646/head
commit
7f29c50efa
|
|
@ -60,10 +60,16 @@ SECTIONS
|
|||
* Ramfuncs memcpy). Listed before .text so these subsections are claimed
|
||||
* here, not by the generic .text below. --gen_func_subsections=on makes
|
||||
* each function its own .text:<name> input section. */
|
||||
.TI.ramfunc : { sha3.obj(.text:BlockSha3)
|
||||
.TI.ramfunc : {
|
||||
#ifndef WOLF_SHA2_EXAMPLE
|
||||
sha3.obj(.text:BlockSha3)
|
||||
#endif
|
||||
#if !defined(WOLF_SHA2_EXAMPLE) && !defined(WOLF_SHA3_EXAMPLE)
|
||||
wc_mldsa.obj(.text:mldsa_ntt_c)
|
||||
wc_mldsa.obj(.text:mldsa_invntt_c)
|
||||
wc_mldsa.obj(.text:mldsa_ntt_small_c) }
|
||||
wc_mldsa.obj(.text:mldsa_ntt_small_c)
|
||||
#endif
|
||||
}
|
||||
LOAD = FLASH_BANK0,
|
||||
RUN = RAMGS_RAMCODE,
|
||||
LOAD_START(RamfuncsLoadStart),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
/* c2000_board.h
|
||||
*
|
||||
* Copyright (C) 2006-2026 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
/* Board bring-up shared by the standalone SHA-2 / SHA-3 examples
|
||||
* (Source/test_sha2.c, Source/test_sha3.c). */
|
||||
|
||||
#ifndef C2000_BOARD_H
|
||||
#define C2000_BOARD_H
|
||||
|
||||
/* PLL to 150 MHz, GPIO, SCIA console (unless built with CIO=1) and the
|
||||
* free-running CPU Timer 0 used by current_time(). */
|
||||
void c2000_board_init(void);
|
||||
|
||||
/* Elapsed seconds since the last current_time(1) call. */
|
||||
double current_time(int reset);
|
||||
|
||||
#endif /* C2000_BOARD_H */
|
||||
|
|
@ -141,6 +141,28 @@ extern "C" {
|
|||
#undef WOLFSSL_SHAKE256
|
||||
#define WOLFSSL_SHAKE256
|
||||
|
||||
/* Standalone hash examples (make SHA2=1 / SHA3=1): one hash family only, no
|
||||
* RNG and no ML-DSA. */
|
||||
#if defined(WOLF_SHA2_EXAMPLE) || defined(WOLF_SHA3_EXAMPLE)
|
||||
#define WOLF_HASH_EXAMPLE
|
||||
#undef WC_NO_RNG
|
||||
#define WC_NO_RNG
|
||||
#endif
|
||||
#ifdef WOLF_SHA2_EXAMPLE
|
||||
#undef WOLFSSL_SHA224
|
||||
#define WOLFSSL_SHA224
|
||||
#undef WOLFSSL_SHA3
|
||||
#undef WOLFSSL_SHAKE128
|
||||
#undef WOLFSSL_SHAKE256
|
||||
#endif
|
||||
#ifdef WOLF_SHA3_EXAMPLE
|
||||
#undef NO_SHA256
|
||||
#define NO_SHA256
|
||||
#undef WOLFSSL_SHA512
|
||||
#undef WOLFSSL_SHA384
|
||||
#endif
|
||||
|
||||
#ifndef WOLF_HASH_EXAMPLE
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* ML-DSA-87 (Dilithium L5) - VERIFY ONLY */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
|
@ -222,6 +244,7 @@ extern "C" {
|
|||
* ~10.7 KB total RAM. Left off by default because the caller must keep the
|
||||
* public-key buffer valid for the key's lifetime. */
|
||||
#endif
|
||||
#endif /* !WOLF_HASH_EXAMPLE */
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* Disabled: big-integer / asymmetric (keeps TFM/ECC/integer out of build) */
|
||||
|
|
|
|||
|
|
@ -348,6 +348,49 @@ endif
|
|||
HARNESS_EXTRA += $(CURDIR)/Source/entropy_probe.c
|
||||
endif
|
||||
|
||||
# SHA2=1 / SHA3=1 build a small standalone example (Source/test_sha2.c or
|
||||
# Source/test_sha3.c) with only that hash family: KATs and a throughput loop,
|
||||
# in place of wolf_main.c and the wolfcrypt test/benchmark harness.
|
||||
SHA2 ?= 0
|
||||
SHA3 ?= 0
|
||||
ifneq ($(SHA2)$(SHA3),00)
|
||||
ifeq ($(SHA2)$(SHA3),11)
|
||||
$(error SHA2=1 and SHA3=1 are separate images - build them one at a time)
|
||||
endif
|
||||
SHA_EXAMPLE_CONFLICTS := ECC MLKEM FASTVERIFY SECUREBOOT PRECOMPA MLDSA AES \
|
||||
X25519 X448 HKDF CHACHA AESEXTRA HWAES RSA SHA1 RSASIGN DH SIGN BENCH \
|
||||
MEMPROF ENTROPY ENTROPY_PROBE
|
||||
ifneq ($(strip $(foreach t,$(SHA_EXAMPLE_CONFLICTS),$(filter-out 0,$($(t))))),)
|
||||
$(error SHA2=1 / SHA3=1 are standalone images - build them without other toggles)
|
||||
endif
|
||||
WC_SRCS := \
|
||||
$(WOLFROOT)/wolfcrypt/src/error.c \
|
||||
$(WOLFROOT)/wolfcrypt/src/wc_port.c \
|
||||
$(WOLFROOT)/wolfcrypt/src/memory.c \
|
||||
$(WOLFROOT)/wolfcrypt/src/logging.c \
|
||||
$(WOLFROOT)/wolfcrypt/src/misc.c \
|
||||
$(WOLFROOT)/wolfcrypt/src/hash.c
|
||||
ifeq ($(SHA2),1)
|
||||
CFLAGS += --define=WOLF_SHA2_EXAMPLE
|
||||
LFLAGS += --define=WOLF_SHA2_EXAMPLE
|
||||
WC_SRCS += \
|
||||
$(WOLFROOT)/wolfcrypt/src/sha256.c \
|
||||
$(WOLFROOT)/wolfcrypt/src/sha512.c
|
||||
BSP_SRCS := $(CURDIR)/Source/test_sha2.c
|
||||
else
|
||||
CFLAGS += --define=WOLF_SHA3_EXAMPLE
|
||||
LFLAGS += --define=WOLF_SHA3_EXAMPLE
|
||||
WC_SRCS += $(WOLFROOT)/wolfcrypt/src/sha3.c
|
||||
BSP_SRCS := $(CURDIR)/Source/test_sha3.c
|
||||
endif
|
||||
BSP_SRCS += \
|
||||
$(CURDIR)/Source/c2000_board.c \
|
||||
$(DEV)/common/source/device.c
|
||||
CFLAGS += --define=NO_CRYPT_TEST --define=NO_CRYPT_BENCHMARK
|
||||
HARNESS_SRCS :=
|
||||
HARNESS_EXTRA :=
|
||||
endif
|
||||
|
||||
# Append the shared AES core once, after every toggle has had its say.
|
||||
ifeq ($(NEED_AES_CORE),1)
|
||||
WC_SRCS += \
|
||||
|
|
|
|||
|
|
@ -18,6 +18,24 @@ The default build runs a KAT suite plus `wolfcrypt_test` and (optionally) `bench
|
|||
- ECDSA + ECDH over SECP256R1 via SP math (`ECC=1`)
|
||||
- RSA-2048 PKCS#1 v1.5 verify via SP math (`RSA=1`)
|
||||
|
||||
## Standalone SHA-2 / SHA-3 examples (`SHA2=1`, `SHA3=1`)
|
||||
|
||||
The default image is driven by `Source/wolf_main.c`, which runs every algorithm's KATs and then the upstream `wolfcrypt_test()` and `benchmark_test()`. Those two come from `wolfcrypt/test/test.c` and `wolfcrypt/benchmark/benchmark.c` in the wolfSSL tree (`WOLFROOT`), not from this directory.
|
||||
|
||||
For a single hash family, two small standalone examples show the whole flow in one file each:
|
||||
|
||||
```
|
||||
make CGT_ROOT=/path/to/ti-cgt-c2000_xx.y.z SHA2=1 # Source/test_sha2.c
|
||||
make CGT_ROOT=/path/to/ti-cgt-c2000_xx.y.z SHA3=1 # Source/test_sha3.c
|
||||
```
|
||||
|
||||
- `Source/test_sha2.c`: SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256.
|
||||
- `Source/test_sha3.c`: SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, SHAKE256.
|
||||
|
||||
Each `main()` calls `c2000_board_init()` (`Source/c2000_board.c`: clocks, SCIA console, CPU Timer 0) and runs one function per variant that shows the complete API sequence, `wc_InitXxx()` -> `wc_XxxUpdate()` -> `wc_XxxFinal()` -> `wc_XxxFree()`, on the FIPS 180-4 / FIPS 202 "abc" vector, printing the digest and `PASS`/`FAIL`. A streaming case then feeds one message through several `Update` calls, and a throughput loop hashes 64 KiB per variant through the generic `wc_HashInit()` / `wc_HashUpdate()` / `wc_HashFinal()` API. Output ends with `SHA-2 example: PASS` (or `SHA-3 ...`) and `=== done ===`.
|
||||
|
||||
Only that family's wolfCrypt source is built (`sha256.c` + `sha512.c`, or `sha3.c`), with no RNG, ML-DSA or test/benchmark harness. `Header/user_settings.h` selects the algorithms under `WOLF_SHA2_EXAMPLE` / `WOLF_SHA3_EXAMPLE`. Both toggles build alone: combining one with the other or with any other toggle is a build error. The output image is `out/wolfcrypt_f28p55x.out` as for every other build, flashed the same way (see Flash and run).
|
||||
|
||||
## Octet representation at the API boundary (`MLDSA=1`)
|
||||
|
||||
Every wolfCrypt `byte*` buffer holds one octet per `byte` cell. On this part a cell is 16 bits, so a buffer costs twice the RAM and each cell reads as `0x00nn` -- an ML-DSA-65 signature is 3309 octets = 3309 cells = 6618 bytes of RAM. The octet values are exact, so this is a footprint property, not a signature-integrity one, and `sigLen` stays an octet count.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,199 @@
|
|||
/* c2000_board.c
|
||||
*
|
||||
* Copyright (C) 2006-2026 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
/* Minimal board support for the standalone SHA-2 / SHA-3 examples on the TI
|
||||
* LAUNCHXL-F28P55X: clocks, SCIA console and a CPU Timer 0 time base. The
|
||||
* same code lives in wolf_main.c for the full test/benchmark image.
|
||||
*
|
||||
* Output goes to SCIA (GPIO28/29, XDS110 virtual COM, 115200 8N1) when built
|
||||
* with WOLF_C2000_SCI_STDOUT (the Makefile default), otherwise to the CCS
|
||||
* console over JTAG (CIO=1).
|
||||
*/
|
||||
|
||||
#include "driverlib.h"
|
||||
#include "device.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#ifdef WOLF_C2000_SCI_STDOUT
|
||||
#include <file.h>
|
||||
#endif
|
||||
|
||||
#include "c2000_board.h"
|
||||
|
||||
#ifdef WOLF_C2000_SCI_STDOUT
|
||||
static void c2000_sciInit(void)
|
||||
{
|
||||
/* RX pin */
|
||||
GPIO_setPinConfig(DEVICE_GPIO_CFG_SCIRXDA);
|
||||
GPIO_setDirectionMode(DEVICE_GPIO_PIN_SCIRXDA, GPIO_DIR_MODE_IN);
|
||||
GPIO_setPadConfig(DEVICE_GPIO_PIN_SCIRXDA, GPIO_PIN_TYPE_STD);
|
||||
GPIO_setQualificationMode(DEVICE_GPIO_PIN_SCIRXDA, GPIO_QUAL_ASYNC);
|
||||
|
||||
/* TX pin */
|
||||
GPIO_setPinConfig(DEVICE_GPIO_CFG_SCITXDA);
|
||||
GPIO_setDirectionMode(DEVICE_GPIO_PIN_SCITXDA, GPIO_DIR_MODE_OUT);
|
||||
GPIO_setPadConfig(DEVICE_GPIO_PIN_SCITXDA, GPIO_PIN_TYPE_STD);
|
||||
GPIO_setQualificationMode(DEVICE_GPIO_PIN_SCITXDA, GPIO_QUAL_ASYNC);
|
||||
|
||||
SCI_performSoftwareReset(SCIA_BASE);
|
||||
SCI_setConfig(SCIA_BASE, DEVICE_LSPCLK_FREQ, 115200,
|
||||
(SCI_CONFIG_WLEN_8 | SCI_CONFIG_STOP_ONE |
|
||||
SCI_CONFIG_PAR_NONE));
|
||||
SCI_resetChannels(SCIA_BASE);
|
||||
SCI_resetRxFIFO(SCIA_BASE);
|
||||
SCI_resetTxFIFO(SCIA_BASE);
|
||||
SCI_enableFIFO(SCIA_BASE);
|
||||
SCI_enableModule(SCIA_BASE);
|
||||
SCI_performSoftwareReset(SCIA_BASE);
|
||||
}
|
||||
|
||||
/* Minimal CGT low-level I/O device that writes to SCIA, so stdout can be
|
||||
* redirected with add_device()/freopen(). Only write() does real work. */
|
||||
int WOLF_open(const char *path, unsigned flags, int llv_fd);
|
||||
int WOLF_close(int dev_fd);
|
||||
int WOLF_read(int dev_fd, char *buf, unsigned count);
|
||||
int WOLF_write(int dev_fd, const char *buf, unsigned count);
|
||||
fpos_t WOLF_lseek(int dev_fd, fpos_t offset, int origin);
|
||||
int WOLF_unlink(const char *path);
|
||||
int WOLF_rename(const char *old_name, const char *new_name);
|
||||
|
||||
int WOLF_open(const char *path, unsigned flags, int llv_fd)
|
||||
{
|
||||
(void)path;
|
||||
(void)flags;
|
||||
(void)llv_fd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WOLF_close(int dev_fd)
|
||||
{
|
||||
(void)dev_fd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WOLF_read(int dev_fd, char *buf, unsigned count)
|
||||
{
|
||||
(void)dev_fd;
|
||||
(void)buf;
|
||||
(void)count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WOLF_write(int dev_fd, const char *buf, unsigned count)
|
||||
{
|
||||
unsigned i;
|
||||
(void)dev_fd;
|
||||
for (i = 0; i < count; i++) {
|
||||
if (buf[i] == '\n') {
|
||||
SCI_writeCharBlockingFIFO(SCIA_BASE, (uint16_t)'\r');
|
||||
}
|
||||
SCI_writeCharBlockingFIFO(SCIA_BASE, (uint16_t)(buf[i] & 0xFF));
|
||||
}
|
||||
return (int)count;
|
||||
}
|
||||
|
||||
fpos_t WOLF_lseek(int dev_fd, fpos_t offset, int origin)
|
||||
{
|
||||
(void)dev_fd;
|
||||
(void)offset;
|
||||
(void)origin;
|
||||
return (fpos_t)0;
|
||||
}
|
||||
|
||||
int WOLF_unlink(const char *path)
|
||||
{
|
||||
(void)path;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WOLF_rename(const char *old_name, const char *new_name)
|
||||
{
|
||||
(void)old_name;
|
||||
(void)new_name;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void c2000_stdoutToSci(void)
|
||||
{
|
||||
add_device("wolf", _SSA, WOLF_open, WOLF_close, WOLF_read, WOLF_write,
|
||||
WOLF_lseek, WOLF_unlink, WOLF_rename);
|
||||
freopen("wolf:", "w", stdout);
|
||||
}
|
||||
#endif /* WOLF_C2000_SCI_STDOUT */
|
||||
|
||||
/* CPU Timer 0: free-running 32-bit down-counter at SYSCLK. */
|
||||
static void c2000_timerInit(void)
|
||||
{
|
||||
CPUTimer_stopTimer(CPUTIMER0_BASE);
|
||||
CPUTimer_setPeriod(CPUTIMER0_BASE, 0xFFFFFFFFUL);
|
||||
CPUTimer_setPreScaler(CPUTIMER0_BASE, 0U);
|
||||
CPUTimer_setEmulationMode(CPUTIMER0_BASE,
|
||||
CPUTIMER_EMULATIONMODE_RUNFREE);
|
||||
CPUTimer_reloadTimerCounter(CPUTIMER0_BASE);
|
||||
CPUTimer_startTimer(CPUTIMER0_BASE);
|
||||
}
|
||||
|
||||
/* A single measured interval must be shorter than 2^32 / SYSCLK (~28.6 s at
|
||||
* 150 MHz) for the wrap handling to be exact. */
|
||||
double current_time(int reset)
|
||||
{
|
||||
static unsigned long long accum = 0;
|
||||
static uint32_t last = 0;
|
||||
static int started = 0;
|
||||
uint32_t now;
|
||||
|
||||
now = CPUTimer_getTimerCount(CPUTIMER0_BASE);
|
||||
|
||||
if (!started || reset) {
|
||||
accum = 0;
|
||||
last = now;
|
||||
started = 1;
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
if (last >= now) {
|
||||
accum += (unsigned long long)(last - now);
|
||||
}
|
||||
else {
|
||||
/* counter reloaded once since the last sample */
|
||||
accum += (unsigned long long)last +
|
||||
(0xFFFFFFFFULL - (unsigned long long)now) + 1ULL;
|
||||
}
|
||||
last = now;
|
||||
|
||||
return (double)accum / (double)DEVICE_SYSCLK_FREQ;
|
||||
}
|
||||
|
||||
void c2000_board_init(void)
|
||||
{
|
||||
/* PLL to 150 MHz, flash wait states, disable watchdog, enable clocks. */
|
||||
Device_init();
|
||||
Device_initGPIO();
|
||||
#ifdef WOLF_C2000_SCI_STDOUT
|
||||
c2000_sciInit();
|
||||
c2000_stdoutToSci();
|
||||
#endif
|
||||
c2000_timerInit();
|
||||
|
||||
/* Unbuffered stdout so output appears immediately and survives a crash. */
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
}
|
||||
|
|
@ -0,0 +1,328 @@
|
|||
/* test_sha2.c
|
||||
*
|
||||
* Copyright (C) 2006-2026 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
/* Standalone SHA-2 example for the TI LAUNCHXL-F28P55X (C2000 C28x).
|
||||
* Build with `make SHA2=1`.
|
||||
*
|
||||
* Flow:
|
||||
* 1. c2000_board_init() - clocks, SCIA console, timer.
|
||||
* 2. One known-answer test (KAT) per SHA-2 variant on "abc" (FIPS 180-4).
|
||||
* Each shows the full API sequence: Init -> Update -> Final -> Free.
|
||||
* 3. A streaming KAT: one message fed through several Update calls.
|
||||
* 4. A throughput loop per variant using the generic wc_Hash* API.
|
||||
*
|
||||
* On the C28x a byte is 16 bits (CHAR_BIT == 16). Every wolfCrypt byte
|
||||
* buffer holds one octet per cell, so the message and digest arrays below are
|
||||
* written exactly as on an 8-bit-byte target.
|
||||
*/
|
||||
|
||||
#include "driverlib.h"
|
||||
#include "device.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/sha256.h>
|
||||
#include <wolfssl/wolfcrypt/sha512.h>
|
||||
#include <wolfssl/wolfcrypt/hash.h>
|
||||
|
||||
#include "c2000_board.h"
|
||||
|
||||
#define BENCH_BLOCK_SZ 1024 /* 1 KiB per Update */
|
||||
#define BENCH_BLOCKS 64
|
||||
|
||||
static const byte msgAbc[] = { 'a', 'b', 'c' };
|
||||
|
||||
static int g_failures = 0;
|
||||
|
||||
/* Print the digest, compare it to the expected value and report PASS/FAIL. */
|
||||
static void check_digest(const char* name, int ret, const byte* digest,
|
||||
const byte* expected, word32 len)
|
||||
{
|
||||
word32 i;
|
||||
|
||||
if (ret != 0) {
|
||||
printf("%-12s KAT: FAIL (ret %d)\r\n", name, ret);
|
||||
g_failures++;
|
||||
return;
|
||||
}
|
||||
printf("%-12s ", name);
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%02x", (unsigned)(digest[i] & 0xFF));
|
||||
}
|
||||
printf("\r\n");
|
||||
if (XMEMCMP(digest, expected, len) == 0) {
|
||||
printf("%-12s KAT: PASS\r\n", name);
|
||||
}
|
||||
else {
|
||||
printf("%-12s KAT: FAIL\r\n", name);
|
||||
g_failures++;
|
||||
}
|
||||
}
|
||||
|
||||
static void sha224_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA224_DIGEST_SIZE] = {
|
||||
0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22,
|
||||
0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3,
|
||||
0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7,
|
||||
0xe3, 0x6c, 0x9d, 0xa7
|
||||
};
|
||||
wc_Sha224 sha;
|
||||
byte digest[WC_SHA224_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha224(&sha);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha224Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha224Final(&sha, digest);
|
||||
}
|
||||
wc_Sha224Free(&sha);
|
||||
}
|
||||
check_digest("SHA-224", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
static void sha256_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA256_DIGEST_SIZE] = {
|
||||
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
|
||||
0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
|
||||
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
|
||||
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
|
||||
};
|
||||
wc_Sha256 sha;
|
||||
byte digest[WC_SHA256_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha256(&sha);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha256Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha256Final(&sha, digest);
|
||||
}
|
||||
wc_Sha256Free(&sha);
|
||||
}
|
||||
check_digest("SHA-256", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
/* The same hash state can absorb a message in any number of Update calls.
|
||||
* The 56-octet FIPS 180-4 message is split 20 + 20 + 16. */
|
||||
static void sha256_stream_example(void)
|
||||
{
|
||||
static const byte msg[] = {
|
||||
'a','b','c','d','b','c','d','e','c','d','e','f','d','e','f','g',
|
||||
'e','f','g','h','f','g','h','i','g','h','i','j','h','i','j','k',
|
||||
'i','j','k','l','j','k','l','m','k','l','m','n','l','m','n','o',
|
||||
'm','n','o','p','n','o','p','q'
|
||||
};
|
||||
static const byte expected[WC_SHA256_DIGEST_SIZE] = {
|
||||
0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
|
||||
0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
|
||||
0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
|
||||
0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
|
||||
};
|
||||
wc_Sha256 sha;
|
||||
byte digest[WC_SHA256_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha256(&sha);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha256Update(&sha, msg, 20);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha256Update(&sha, msg + 20, 20);
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha256Update(&sha, msg + 40, (word32)sizeof(msg) - 40);
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha256Final(&sha, digest);
|
||||
}
|
||||
wc_Sha256Free(&sha);
|
||||
}
|
||||
check_digest("SHA-256 3x", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
static void sha384_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA384_DIGEST_SIZE] = {
|
||||
0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
|
||||
0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
|
||||
0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
|
||||
0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
|
||||
0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
|
||||
0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
|
||||
};
|
||||
wc_Sha384 sha;
|
||||
byte digest[WC_SHA384_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha384(&sha);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha384Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha384Final(&sha, digest);
|
||||
}
|
||||
wc_Sha384Free(&sha);
|
||||
}
|
||||
check_digest("SHA-384", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
static void sha512_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA512_DIGEST_SIZE] = {
|
||||
0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
|
||||
0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
|
||||
0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
|
||||
0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
|
||||
0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
|
||||
0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
|
||||
0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
|
||||
0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
|
||||
};
|
||||
wc_Sha512 sha;
|
||||
byte digest[WC_SHA512_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha512(&sha);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha512Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha512Final(&sha, digest);
|
||||
}
|
||||
wc_Sha512Free(&sha);
|
||||
}
|
||||
check_digest("SHA-512", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
static void sha512_224_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA512_224_DIGEST_SIZE] = {
|
||||
0x46, 0x34, 0x27, 0x0f, 0x70, 0x7b, 0x6a, 0x54,
|
||||
0xda, 0xae, 0x75, 0x30, 0x46, 0x08, 0x42, 0xe2,
|
||||
0x0e, 0x37, 0xed, 0x26, 0x5c, 0xee, 0xe9, 0xa4,
|
||||
0x3e, 0x89, 0x24, 0xaa
|
||||
};
|
||||
wc_Sha512 sha;
|
||||
byte digest[WC_SHA512_224_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha512_224(&sha);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha512_224Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha512_224Final(&sha, digest);
|
||||
}
|
||||
wc_Sha512_224Free(&sha);
|
||||
}
|
||||
check_digest("SHA-512/224", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
static void sha512_256_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA512_256_DIGEST_SIZE] = {
|
||||
0x53, 0x04, 0x8e, 0x26, 0x81, 0x94, 0x1e, 0xf9,
|
||||
0x9b, 0x2e, 0x29, 0xb7, 0x6b, 0x4c, 0x7d, 0xab,
|
||||
0xe4, 0xc2, 0xd0, 0xc6, 0x34, 0xfc, 0x6d, 0x46,
|
||||
0xe0, 0xe2, 0xf1, 0x31, 0x07, 0xe7, 0xaf, 0x23
|
||||
};
|
||||
wc_Sha512 sha;
|
||||
byte digest[WC_SHA512_256_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha512_256(&sha);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha512_256Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha512_256Final(&sha, digest);
|
||||
}
|
||||
wc_Sha512_256Free(&sha);
|
||||
}
|
||||
check_digest("SHA-512/256", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
/* Hash BENCH_BLOCKS KiB and print the throughput. */
|
||||
static void hash_bench(const char* name, enum wc_HashType type)
|
||||
{
|
||||
static byte data[BENCH_BLOCK_SZ];
|
||||
static wc_HashAlg hash;
|
||||
byte digest[WC_MAX_DIGEST_SIZE];
|
||||
double secs;
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < BENCH_BLOCK_SZ; i++) {
|
||||
data[i] = (byte)(i & 0xFF);
|
||||
}
|
||||
|
||||
(void)current_time(1);
|
||||
ret = wc_HashInit(&hash, type);
|
||||
for (i = 0; i < BENCH_BLOCKS && ret == 0; i++) {
|
||||
ret = wc_HashUpdate(&hash, type, data, BENCH_BLOCK_SZ);
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = wc_HashFinal(&hash, type, digest);
|
||||
}
|
||||
secs = current_time(0);
|
||||
(void)wc_HashFree(&hash, type);
|
||||
|
||||
if (ret != 0 || secs <= 0.0) {
|
||||
printf("%-12s bench: FAIL (ret %d)\r\n", name, ret);
|
||||
g_failures++;
|
||||
return;
|
||||
}
|
||||
printf("%-12s %d KiB in %.3f s = %.1f KiB/s\r\n", name,
|
||||
BENCH_BLOCKS, secs, (double)BENCH_BLOCKS / secs);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
c2000_board_init();
|
||||
|
||||
printf("\r\n=== wolfCrypt SHA-2 example on TI C2000 "
|
||||
"LAUNCHXL-F28P55X ===\r\n");
|
||||
|
||||
printf("\r\n--- Known-answer tests ---\r\n");
|
||||
sha224_example();
|
||||
sha256_example();
|
||||
sha256_stream_example();
|
||||
sha384_example();
|
||||
sha512_example();
|
||||
sha512_224_example();
|
||||
sha512_256_example();
|
||||
|
||||
printf("\r\n--- Throughput (%d MHz) ---\r\n",
|
||||
(int)(DEVICE_SYSCLK_FREQ / 1000000UL));
|
||||
hash_bench("SHA-224", WC_HASH_TYPE_SHA224);
|
||||
hash_bench("SHA-256", WC_HASH_TYPE_SHA256);
|
||||
hash_bench("SHA-384", WC_HASH_TYPE_SHA384);
|
||||
hash_bench("SHA-512", WC_HASH_TYPE_SHA512);
|
||||
hash_bench("SHA-512/224", WC_HASH_TYPE_SHA512_224);
|
||||
hash_bench("SHA-512/256", WC_HASH_TYPE_SHA512_256);
|
||||
|
||||
printf("\r\nSHA-2 example: %s (%d failure(s))\r\n",
|
||||
(g_failures == 0) ? "PASS" : "FAIL", g_failures);
|
||||
printf("=== done ===\r\n");
|
||||
|
||||
for (;;) {
|
||||
/* spin */
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,339 @@
|
|||
/* test_sha3.c
|
||||
*
|
||||
* Copyright (C) 2006-2026 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
/* Standalone SHA-3 example for the TI LAUNCHXL-F28P55X (C2000 C28x).
|
||||
* Build with `make SHA3=1`.
|
||||
*
|
||||
* Flow:
|
||||
* 1. c2000_board_init() - clocks, SCIA console, timer.
|
||||
* 2. One known-answer test (KAT) per SHA-3 variant on "abc" (FIPS 202).
|
||||
* Each shows the full API sequence: Init -> Update -> Final -> Free.
|
||||
* 3. SHAKE128 / SHAKE256 (extendable output) KATs, then a streaming KAT
|
||||
* that feeds one message through several Update calls.
|
||||
* 4. A throughput loop per variant using the generic wc_Hash* API.
|
||||
*
|
||||
* On the C28x a byte is 16 bits (CHAR_BIT == 16). Every wolfCrypt byte
|
||||
* buffer holds one octet per cell, so the message and digest arrays below are
|
||||
* written exactly as on an 8-bit-byte target.
|
||||
*/
|
||||
|
||||
#include "driverlib.h"
|
||||
#include "device.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/sha3.h>
|
||||
#include <wolfssl/wolfcrypt/hash.h>
|
||||
|
||||
#include "c2000_board.h"
|
||||
|
||||
#define BENCH_BLOCK_SZ 1024 /* 1 KiB per Update */
|
||||
#define BENCH_BLOCKS 64
|
||||
|
||||
static const byte msgAbc[] = { 'a', 'b', 'c' };
|
||||
|
||||
static int g_failures = 0;
|
||||
|
||||
/* Print the digest, compare it to the expected value and report PASS/FAIL. */
|
||||
static void check_digest(const char* name, int ret, const byte* digest,
|
||||
const byte* expected, word32 len)
|
||||
{
|
||||
word32 i;
|
||||
|
||||
if (ret != 0) {
|
||||
printf("%-12s KAT: FAIL (ret %d)\r\n", name, ret);
|
||||
g_failures++;
|
||||
return;
|
||||
}
|
||||
printf("%-12s ", name);
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%02x", (unsigned)(digest[i] & 0xFF));
|
||||
}
|
||||
printf("\r\n");
|
||||
if (XMEMCMP(digest, expected, len) == 0) {
|
||||
printf("%-12s KAT: PASS\r\n", name);
|
||||
}
|
||||
else {
|
||||
printf("%-12s KAT: FAIL\r\n", name);
|
||||
g_failures++;
|
||||
}
|
||||
}
|
||||
|
||||
static void sha3_224_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA3_224_DIGEST_SIZE] = {
|
||||
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
|
||||
};
|
||||
wc_Sha3 sha;
|
||||
byte digest[WC_SHA3_224_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha3_224(&sha, NULL, INVALID_DEVID);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha3_224_Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha3_224_Final(&sha, digest);
|
||||
}
|
||||
wc_Sha3_224_Free(&sha);
|
||||
}
|
||||
check_digest("SHA3-224", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
static void sha3_256_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA3_256_DIGEST_SIZE] = {
|
||||
0x3a, 0x98, 0x5d, 0xa7, 0x4f, 0xe2, 0x25, 0xb2,
|
||||
0x04, 0x5c, 0x17, 0x2d, 0x6b, 0xd3, 0x90, 0xbd,
|
||||
0x85, 0x5f, 0x08, 0x6e, 0x3e, 0x9d, 0x52, 0x5b,
|
||||
0x46, 0xbf, 0xe2, 0x45, 0x11, 0x43, 0x15, 0x32
|
||||
};
|
||||
wc_Sha3 sha;
|
||||
byte digest[WC_SHA3_256_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha3_256(&sha, NULL, INVALID_DEVID);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha3_256_Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha3_256_Final(&sha, digest);
|
||||
}
|
||||
wc_Sha3_256_Free(&sha);
|
||||
}
|
||||
check_digest("SHA3-256", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
static void sha3_384_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA3_384_DIGEST_SIZE] = {
|
||||
0xec, 0x01, 0x49, 0x82, 0x88, 0x51, 0x6f, 0xc9,
|
||||
0x26, 0x45, 0x9f, 0x58, 0xe2, 0xc6, 0xad, 0x8d,
|
||||
0xf9, 0xb4, 0x73, 0xcb, 0x0f, 0xc0, 0x8c, 0x25,
|
||||
0x96, 0xda, 0x7c, 0xf0, 0xe4, 0x9b, 0xe4, 0xb2,
|
||||
0x98, 0xd8, 0x8c, 0xea, 0x92, 0x7a, 0xc7, 0xf5,
|
||||
0x39, 0xf1, 0xed, 0xf2, 0x28, 0x37, 0x6d, 0x25
|
||||
};
|
||||
wc_Sha3 sha;
|
||||
byte digest[WC_SHA3_384_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha3_384(&sha, NULL, INVALID_DEVID);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha3_384_Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha3_384_Final(&sha, digest);
|
||||
}
|
||||
wc_Sha3_384_Free(&sha);
|
||||
}
|
||||
check_digest("SHA3-384", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
static void sha3_512_example(void)
|
||||
{
|
||||
static const byte expected[WC_SHA3_512_DIGEST_SIZE] = {
|
||||
0xb7, 0x51, 0x85, 0x0b, 0x1a, 0x57, 0x16, 0x8a,
|
||||
0x56, 0x93, 0xcd, 0x92, 0x4b, 0x6b, 0x09, 0x6e,
|
||||
0x08, 0xf6, 0x21, 0x82, 0x74, 0x44, 0xf7, 0x0d,
|
||||
0x88, 0x4f, 0x5d, 0x02, 0x40, 0xd2, 0x71, 0x2e,
|
||||
0x10, 0xe1, 0x16, 0xe9, 0x19, 0x2a, 0xf3, 0xc9,
|
||||
0x1a, 0x7e, 0xc5, 0x76, 0x47, 0xe3, 0x93, 0x40,
|
||||
0x57, 0x34, 0x0b, 0x4c, 0xf4, 0x08, 0xd5, 0xa5,
|
||||
0x65, 0x92, 0xf8, 0x27, 0x4e, 0xec, 0x53, 0xf0
|
||||
};
|
||||
wc_Sha3 sha;
|
||||
byte digest[WC_SHA3_512_DIGEST_SIZE];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitSha3_512(&sha, NULL, INVALID_DEVID);
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha3_512_Update(&sha, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha3_512_Final(&sha, digest);
|
||||
}
|
||||
wc_Sha3_512_Free(&sha);
|
||||
}
|
||||
check_digest("SHA3-512", ret, digest, expected, sizeof(digest));
|
||||
}
|
||||
|
||||
/* SHAKE128 is an XOF: the caller picks the output length at Final. */
|
||||
static void shake128_example(void)
|
||||
{
|
||||
static const byte expected[32] = {
|
||||
0x58, 0x81, 0x09, 0x2d, 0xd8, 0x18, 0xbf, 0x5c,
|
||||
0xf8, 0xa3, 0xdd, 0xb7, 0x93, 0xfb, 0xcb, 0xa7,
|
||||
0x40, 0x97, 0xd5, 0xc5, 0x26, 0xa6, 0xd3, 0x5f,
|
||||
0x97, 0xb8, 0x33, 0x51, 0x94, 0x0f, 0x2c, 0xc8
|
||||
};
|
||||
wc_Shake shake;
|
||||
byte out[32];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitShake128(&shake, NULL, INVALID_DEVID);
|
||||
if (ret == 0) {
|
||||
ret = wc_Shake128_Update(&shake, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Shake128_Final(&shake, out, (word32)sizeof(out));
|
||||
}
|
||||
wc_Shake128_Free(&shake);
|
||||
}
|
||||
check_digest("SHAKE128", ret, out, expected, sizeof(out));
|
||||
}
|
||||
|
||||
/* SHAKE256 is an XOF: the caller picks the output length at Final. */
|
||||
static void shake256_example(void)
|
||||
{
|
||||
static const byte expected[64] = {
|
||||
0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
|
||||
0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
|
||||
0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
|
||||
0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
|
||||
0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
|
||||
0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
|
||||
0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
|
||||
0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
|
||||
};
|
||||
wc_Shake shake;
|
||||
byte out[64];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitShake256(&shake, NULL, INVALID_DEVID);
|
||||
if (ret == 0) {
|
||||
ret = wc_Shake256_Update(&shake, msgAbc, (word32)sizeof(msgAbc));
|
||||
if (ret == 0) {
|
||||
ret = wc_Shake256_Final(&shake, out, (word32)sizeof(out));
|
||||
}
|
||||
wc_Shake256_Free(&shake);
|
||||
}
|
||||
check_digest("SHAKE256", ret, out, expected, sizeof(out));
|
||||
}
|
||||
|
||||
/* The same state can absorb a message in any number of Update calls.
|
||||
* The 56-octet FIPS message is split 20 + 20 + 16. */
|
||||
static void shake256_stream_example(void)
|
||||
{
|
||||
static const byte msg[] = {
|
||||
'a','b','c','d','b','c','d','e','c','d','e','f','d','e','f','g',
|
||||
'e','f','g','h','f','g','h','i','g','h','i','j','h','i','j','k',
|
||||
'i','j','k','l','j','k','l','m','k','l','m','n','l','m','n','o',
|
||||
'm','n','o','p','n','o','p','q'
|
||||
};
|
||||
static const byte expected[64] = {
|
||||
0x4d, 0x8c, 0x2d, 0xd2, 0x43, 0x5a, 0x01, 0x28,
|
||||
0xee, 0xfb, 0xb8, 0xc3, 0x6f, 0x6f, 0x87, 0x13,
|
||||
0x3a, 0x79, 0x11, 0xe1, 0x8d, 0x97, 0x9e, 0xe1,
|
||||
0xae, 0x6b, 0xe5, 0xd4, 0xfd, 0x2e, 0x33, 0x29,
|
||||
0x40, 0xd8, 0x68, 0x8a, 0x4e, 0x6a, 0x59, 0xaa,
|
||||
0x80, 0x60, 0xf1, 0xf9, 0xbc, 0x99, 0x6c, 0x05,
|
||||
0xac, 0xa3, 0xc6, 0x96, 0xa8, 0xb6, 0x62, 0x79,
|
||||
0xdc, 0x67, 0x2c, 0x74, 0x0b, 0xb2, 0x24, 0xec
|
||||
};
|
||||
wc_Shake shake;
|
||||
byte out[64];
|
||||
int ret;
|
||||
|
||||
ret = wc_InitShake256(&shake, NULL, INVALID_DEVID);
|
||||
if (ret == 0) {
|
||||
ret = wc_Shake256_Update(&shake, msg, 20);
|
||||
if (ret == 0) {
|
||||
ret = wc_Shake256_Update(&shake, msg + 20, 20);
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = wc_Shake256_Update(&shake, msg + 40,
|
||||
(word32)sizeof(msg) - 40);
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = wc_Shake256_Final(&shake, out, (word32)sizeof(out));
|
||||
}
|
||||
wc_Shake256_Free(&shake);
|
||||
}
|
||||
check_digest("SHAKE256 3x", ret, out, expected, sizeof(out));
|
||||
}
|
||||
|
||||
/* Hash BENCH_BLOCKS KiB and print the throughput. */
|
||||
static void hash_bench(const char* name, enum wc_HashType type)
|
||||
{
|
||||
static byte data[BENCH_BLOCK_SZ];
|
||||
static wc_HashAlg hash;
|
||||
byte digest[WC_MAX_DIGEST_SIZE];
|
||||
double secs;
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < BENCH_BLOCK_SZ; i++) {
|
||||
data[i] = (byte)(i & 0xFF);
|
||||
}
|
||||
|
||||
(void)current_time(1);
|
||||
ret = wc_HashInit(&hash, type);
|
||||
for (i = 0; i < BENCH_BLOCKS && ret == 0; i++) {
|
||||
ret = wc_HashUpdate(&hash, type, data, BENCH_BLOCK_SZ);
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = wc_HashFinal(&hash, type, digest);
|
||||
}
|
||||
secs = current_time(0);
|
||||
(void)wc_HashFree(&hash, type);
|
||||
|
||||
if (ret != 0 || secs <= 0.0) {
|
||||
printf("%-12s bench: FAIL (ret %d)\r\n", name, ret);
|
||||
g_failures++;
|
||||
return;
|
||||
}
|
||||
printf("%-12s %d KiB in %.3f s = %.1f KiB/s\r\n", name,
|
||||
BENCH_BLOCKS, secs, (double)BENCH_BLOCKS / secs);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
c2000_board_init();
|
||||
|
||||
printf("\r\n=== wolfCrypt SHA-3 example on TI C2000 "
|
||||
"LAUNCHXL-F28P55X ===\r\n");
|
||||
|
||||
printf("\r\n--- Known-answer tests ---\r\n");
|
||||
sha3_224_example();
|
||||
sha3_256_example();
|
||||
sha3_384_example();
|
||||
sha3_512_example();
|
||||
shake128_example();
|
||||
shake256_example();
|
||||
shake256_stream_example();
|
||||
|
||||
printf("\r\n--- Throughput (%d MHz) ---\r\n",
|
||||
(int)(DEVICE_SYSCLK_FREQ / 1000000UL));
|
||||
hash_bench("SHA3-224", WC_HASH_TYPE_SHA3_224);
|
||||
hash_bench("SHA3-256", WC_HASH_TYPE_SHA3_256);
|
||||
hash_bench("SHA3-384", WC_HASH_TYPE_SHA3_384);
|
||||
hash_bench("SHA3-512", WC_HASH_TYPE_SHA3_512);
|
||||
hash_bench("SHAKE128", WC_HASH_TYPE_SHAKE128);
|
||||
hash_bench("SHAKE256", WC_HASH_TYPE_SHAKE256);
|
||||
|
||||
printf("\r\nSHA-3 example: %s (%d failure(s))\r\n",
|
||||
(g_failures == 0) ? "PASS" : "FAIL", g_failures);
|
||||
printf("=== done ===\r\n");
|
||||
|
||||
for (;;) {
|
||||
/* spin */
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue