Skip to content

Commit a3f5260

Browse files
authored
Merge pull request #10500 from rizlik/sha224_only
crpytocb: support SHA224 under WOLF_CRYPTO_CB_ONLY_SHA256
2 parents 52620e3 + 408ea84 commit a3f5260

5 files changed

Lines changed: 187 additions & 11 deletions

File tree

.github/workflows/cryptocb-only.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ jobs:
3030
- name: RSA
3131
cppflags: -DWOLF_CRYPTO_CB_ONLY_RSA
3232
# WOLF_CRYPTO_CB_ONLY_SHA256: strips software SHA-256; swdev provides
33-
# the software path via cryptocb. SHA-224 not yet supported.
33+
# the software path via cryptocb.
3434
- name: SHA256
35-
extra_config: --disable-sha224
3635
cppflags: -DWOLF_CRYPTO_CB_ONLY_SHA256
3736
# WOLF_CRYPTO_CB_ONLY_AES: strips software AES; swdev provides the
3837
# software path via cryptocb.
@@ -50,7 +49,6 @@ jobs:
5049
# algorithm call that a single-strip entry would still resolve via
5150
# the remaining software paths.
5251
- name: ALL
53-
extra_config: --disable-sha224
5452
cppflags: >-
5553
-DWOLF_CRYPTO_CB_ONLY_ECC -DWOLF_CRYPTO_CB_ONLY_RSA
5654
-DWOLF_CRYPTO_CB_ONLY_SHA256 -DWOLF_CRYPTO_CB_ONLY_AES

tests/swdev/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ struct layouts, so flipping them between the two compiles is safe.
119119
CPPFLAGS="-DWOLF_CRYPTO_CB_ONLY_ECC \
120120
-DWOLF_CRYPTO_CB_ONLY_RSA \
121121
-DWOLF_CRYPTO_CB_ONLY_SHA256 \
122-
-DWOLF_CRYPTO_CB_ONLY_AES" \
123-
--disable-sha224
122+
-DWOLF_CRYPTO_CB_ONLY_AES"
124123
make
125124
make check
126125
```
@@ -135,8 +134,6 @@ Notes:
135134
Out-of-tree (VPATH) builds fail at configure time. swdev is built
136135
from `wolfcrypt/test/include.am` and inherits `PARENT_SRCS`,
137136
`PARENT_BUILD_CFLAGS`, etc., from the parent build.
138-
- `--disable-sha224` is required when `WOLF_CRYPTO_CB_ONLY_SHA256` is
139-
set: SHA-224 is unsupported for now.
140137

141138
For the full CI matrix that exercises each `_ONLY_*` macro, see
142139
`.github/workflows/cryptocb-only.yml`.

tests/swdev/swdev.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,44 @@ static int swdev_sha256(wc_CryptoInfo* info)
209209
wc_Sha256Free(&shadow);
210210
return ret;
211211
}
212+
213+
#ifdef WOLFSSL_SHA224
214+
/* SHA-224 is SHA-256 with a different IV/truncation; wc_Sha224 is a typedef
215+
* of wc_Sha256, so the same shadow/copy-state dance applies. */
216+
static int swdev_sha224(wc_CryptoInfo* info)
217+
{
218+
wc_Sha224* sha224 = info->hash.sha224;
219+
wc_Sha224 shadow;
220+
int ret;
221+
222+
if (sha224 == NULL)
223+
return BAD_FUNC_ARG;
224+
225+
ret = wc_InitSha224(&shadow);
226+
if (ret != 0)
227+
return ret;
228+
229+
swdev_sha256_copy_state((wc_Sha256*)&shadow, (const wc_Sha256*)sha224);
230+
231+
if (info->hash.in != NULL) {
232+
ret = wc_Sha224Update(&shadow, info->hash.in, info->hash.inSz);
233+
if (ret != 0)
234+
goto out;
235+
}
236+
237+
if (info->hash.digest != NULL) {
238+
ret = wc_Sha224Final(&shadow, info->hash.digest);
239+
if (ret != 0)
240+
goto out;
241+
}
242+
243+
swdev_sha256_copy_state((wc_Sha256*)sha224, (const wc_Sha256*)&shadow);
244+
245+
out:
246+
wc_Sha224Free(&shadow);
247+
return ret;
248+
}
249+
#endif /* WOLFSSL_SHA224 */
212250
#endif /* !NO_SHA256 */
213251

214252
#ifndef NO_AES
@@ -513,6 +551,10 @@ WC_SWDEV_EXPORT int wc_SwDev_Callback(int devId, wc_CryptoInfo* info,
513551
switch (info->hash.type) {
514552
case WC_HASH_TYPE_SHA256:
515553
return swdev_sha256(info);
554+
#ifdef WOLFSSL_SHA224
555+
case WC_HASH_TYPE_SHA224:
556+
return swdev_sha224(info);
557+
#endif
516558
default:
517559
return CRYPTOCB_UNAVAILABLE;
518560
}

wolfcrypt/src/sha256.c

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ on the specific device platform.
6060

6161
#if !defined(NO_SHA256) && !defined(WOLFSSL_RISCV_ASM)
6262

63-
#if defined(WOLF_CRYPTO_CB_ONLY_SHA256) && defined(WOLFSSL_SHA224)
64-
#error "WOLF_CRYPTO_CB_ONLY_SHA256 is incompatible with WOLFSSL_SHA224"
65-
#endif
66-
6763
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
6864
/* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
6965
#define FIPS_NO_WRAPPERS
@@ -101,6 +97,42 @@ on the specific device platform.
10197
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
10298
#endif
10399

100+
/* WOLF_CRYPTO_CB_ONLY_SHA256 strips the software SHA-256 implementation and
101+
* routes every operation through the crypto callback. It is mutually exclusive
102+
* with any in-tree SHA-256 hardware/asm backend below: keep this list in sync
103+
* with the #elif chain at the start of the "Hardware Acceleration" section. */
104+
#if defined(WOLF_CRYPTO_CB_ONLY_SHA256) && ( \
105+
defined(WOLFSSL_TI_HASH) || \
106+
defined(WOLFSSL_CRYPTOCELL) || \
107+
defined(MAX3266X_SHA) || \
108+
defined(FREESCALE_LTC_SHA) || \
109+
defined(FREESCALE_MMCAU_SHA) || \
110+
defined(WOLFSSL_PIC32MZ_HASH) || \
111+
defined(STM32_HASH_SHA2) || \
112+
(defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)) || \
113+
(defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)) || \
114+
defined(WOLFSSL_AFALG_HASH) || \
115+
defined(WOLFSSL_DEVCRYPTO_HASH) || \
116+
(defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_HASH)) || \
117+
defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) || \
118+
defined(WOLFSSL_RENESAS_TSIP_TLS) || \
119+
defined(WOLFSSL_RENESAS_SCEPROTECT) || \
120+
defined(WOLFSSL_RENESAS_RSIP) || \
121+
defined(PSOC6_HASH_SHA2) || \
122+
defined(WOLFSSL_IMXRT_DCP) || \
123+
defined(WOLFSSL_NXP_HASHCRYPT_SHA) || \
124+
defined(WOLFSSL_SILABS_SE_ACCEL) || \
125+
defined(WOLFSSL_KCAPI_HASH) || \
126+
(defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)) || \
127+
defined(WOLFSSL_RENESAS_RX64_HASH) || \
128+
defined(WOLFSSL_PPC32_ASM) || \
129+
defined(WOLFSSL_ARMASM) || \
130+
(defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
131+
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))))
132+
#error "WOLF_CRYPTO_CB_ONLY_SHA256 is incompatible with SHA-256 hardware" \
133+
" acceleration backends"
134+
#endif
135+
104136
#ifdef WOLFSSL_ESPIDF
105137
/* Define the ESP_LOGx(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE value for output messages here.
106138
**
@@ -2148,6 +2180,35 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
21482180
#elif defined(PSOC6_HASH_SHA2)
21492181
/* Implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
21502182

2183+
#elif defined(WOLF_CRYPTO_CB_ONLY_SHA256)
2184+
int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
2185+
{
2186+
int ret;
2187+
if (sha224 == NULL)
2188+
return BAD_FUNC_ARG;
2189+
ret = InitSha256((wc_Sha256*)sha224);
2190+
if (ret != 0)
2191+
return ret;
2192+
sha224->digest[0] = 0xc1059ed8;
2193+
sha224->digest[1] = 0x367cd507;
2194+
sha224->digest[2] = 0x3070dd17;
2195+
sha224->digest[3] = 0xf70e5939;
2196+
sha224->digest[4] = 0xffc00b31;
2197+
sha224->digest[5] = 0x68581511;
2198+
sha224->digest[6] = 0x64f98fa7;
2199+
sha224->digest[7] = 0xbefa4fa4;
2200+
sha224->heap = heap;
2201+
sha224->devId = devId;
2202+
sha224->devCtx = NULL;
2203+
#ifdef WOLFSSL_SMALL_STACK_CACHE
2204+
sha224->W = NULL;
2205+
#endif
2206+
#ifdef WOLFSSL_ASYNC_CRYPT
2207+
XMEMSET(&sha224->asyncDev, 0, sizeof(sha224->asyncDev));
2208+
#endif
2209+
return ret;
2210+
}
2211+
21512212
#else
21522213

21532214
#define NEED_SOFT_SHA224
@@ -2369,6 +2430,50 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
23692430
}
23702431
#endif /* end of SHA224 software implementation */
23712432

2433+
#ifdef WOLF_CRYPTO_CB_ONLY_SHA256
2434+
2435+
int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
2436+
{
2437+
if (sha224 == NULL)
2438+
return BAD_FUNC_ARG;
2439+
if (data == NULL && len == 0)
2440+
return 0;
2441+
if (data == NULL)
2442+
return BAD_FUNC_ARG;
2443+
2444+
#ifndef WOLF_CRYPTO_CB_FIND
2445+
if (sha224->devId != INVALID_DEVID)
2446+
#endif
2447+
{
2448+
int ret = wc_CryptoCb_Sha224Hash(sha224, data, len, NULL);
2449+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2450+
return ret;
2451+
}
2452+
2453+
return NO_VALID_DEVID;
2454+
}
2455+
2456+
int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
2457+
{
2458+
int ret;
2459+
2460+
if (sha224 == NULL || hash == NULL)
2461+
return BAD_FUNC_ARG;
2462+
2463+
#ifndef WOLF_CRYPTO_CB_FIND
2464+
if (sha224->devId != INVALID_DEVID)
2465+
#endif
2466+
{
2467+
ret = wc_CryptoCb_Sha224Hash(sha224, NULL, 0, hash);
2468+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2469+
return ret;
2470+
}
2471+
2472+
return NO_VALID_DEVID;
2473+
}
2474+
2475+
#endif /* WOLF_CRYPTO_CB_ONLY_SHA256 */
2476+
23722477
int wc_InitSha224(wc_Sha224* sha224)
23732478
{
23742479
int devId = INVALID_DEVID;

wolfcrypt/test/test.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72593,6 +72593,40 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
7259372593
}
7259472594
else
7259572595
#endif
72596+
#ifdef WOLFSSL_SHA224
72597+
if (info->hash.type == WC_HASH_TYPE_SHA224) {
72598+
if (info->hash.sha224 == NULL)
72599+
return NOT_COMPILED_IN;
72600+
72601+
/* set devId to invalid, so software is used */
72602+
info->hash.sha224->devId = INVALID_DEVID;
72603+
#if defined(WOLF_CRYPTO_CB_ONLY_SHA256)
72604+
#ifdef DEBUG_WOLFSSL
72605+
printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
72606+
#endif
72607+
if (myCtx->exampleVar == 99) {
72608+
info->hash.sha224->devId = devIdArg;
72609+
return 0;
72610+
}
72611+
#endif
72612+
72613+
if (info->hash.in != NULL) {
72614+
ret = wc_Sha224Update(
72615+
info->hash.sha224,
72616+
info->hash.in,
72617+
info->hash.inSz);
72618+
}
72619+
if (info->hash.digest != NULL) {
72620+
ret = wc_Sha224Final(
72621+
info->hash.sha224,
72622+
info->hash.digest);
72623+
}
72624+
72625+
/* reset devId */
72626+
info->hash.sha224->devId = devIdArg;
72627+
}
72628+
else
72629+
#endif
7259672630
#ifdef WOLFSSL_SHA384
7259772631
if (info->hash.type == WC_HASH_TYPE_SHA384) {
7259872632
if (info->hash.sha384 == NULL)

0 commit comments

Comments
 (0)