Repository navigation
Pq fs 2026 part4 lab prep sha512 fixes #11983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e6a25ba
e83e2f0
cca0142
5b3d893
5167dfd
1979864
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2716,9 +2716,12 @@ static int Sha512FinalRaw(wc_Sha512* sha512, byte* hash, word32 digestSz) | |
| if ((digestSz & 0x7) == 0) | ||
| ByteReverseWords64((word64 *)hash, sha512->digest, digestSz); | ||
| else { | ||
| /* Reverse for the copy, then reverse back to keep the state. */ | ||
| ByteReverseWords64(sha512->digest, sha512->digest, | ||
| WC_SHA512_DIGEST_SIZE); | ||
| XMEMCPY(hash, sha512->digest, digestSz); | ||
| ByteReverseWords64(sha512->digest, sha512->digest, | ||
| WC_SHA512_DIGEST_SIZE); | ||
| } | ||
| #else | ||
| XMEMCPY(hash, sha512->digest, digestSz); | ||
|
|
@@ -2907,28 +2910,33 @@ int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data) | |
| wc_MemZero_Add("wc_Sha512Transform buffer", buffer, WC_SHA512_BLOCK_SIZE); | ||
| #endif | ||
|
|
||
| /* data is const: never modify the caller's block. */ | ||
| #if defined(WOLFSSL_ARMASM) && defined(WOLFSSL_ARM_ARCH) && \ | ||
| (WOLFSSL_ARM_ARCH < 6) | ||
| /* ARMv5 asm loads words with LDR, so it needs an aligned copy. */ | ||
| XMEMCPY(buffer, data, WC_SHA512_BLOCK_SIZE); | ||
| ret = Transform_Sha512(sha, (const byte*)buffer); | ||
| #elif defined(WOLFSSL_ARMASM) && defined(__aarch64__) | ||
| /* The C CPU step writes sha->buffer, which may hold pending input. */ | ||
| XMEMCPY(buffer, sha->buffer, WC_SHA512_BLOCK_SIZE); | ||
| ret = Transform_Sha512(sha, data); | ||
| XMEMCPY(sha->buffer, buffer, WC_SHA512_BLOCK_SIZE); | ||
| #elif defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM) || \ | ||
| defined(WOLFSSL_PPC64_ASM) || defined(WOLFSSL_PPC32_ASM) | ||
| /* The asm reads data as Sha512Update passes it: unreversed. */ | ||
| ret = Transform_Sha512(sha, data); | ||
|
Comment on lines
+2924
to
+2927
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 (optional) RISC-V vector-crypto builds can now fault in wc_Sha512Transform on a misaligned caller block. The base code gave the asm an aligned word64 copy. The shared branch at sha512.c:2924-2927 passes Why this was flaggedThe trigger is a build with --enable-riscv-asm and WOLFSSL_RISCV_VECTOR_CRYPTO_ASM, running on a core or kernel that does not handle misaligned vector element accesses. The caller passes a block at an address that is not 8-byte aligned, through wc_Sha512Transform or wolfSSL_SHA512_Transform. The new branch at sha512.c:2924-2927 calls Transform_Sha512(sha, data). Under WOLFSSL_RISCV_VECTOR_CRYPTO_ASM, sha512.c:1915-1918 sends that to Transform_Sha512_Len_riscv_vector, which loads the block with Verification: In the base, the RISC-V branch ran
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed, and fixed in b6111e438d. The vector-crypto lane loads the block with vl8re64.v (riscv-64-sha512-asm.S:117), and RVV 1.0 §7.9 lets whole-register loads raise an address-misaligned exception when the base is not aligned to the element size (8 bytes here). Transform now copies the block into the aligned local buffer first for WOLFSSL_RISCV_VECTOR_CRYPTO_ASM, the same as ARMv5. The base and scalar-crypto RISC-V lanes still pass data through: they load with lbu, or with ld on Zbb builds, which have their own WOLFSSL_RISCV_ASM_NO_UNALIGNED opt-out. Tested under qemu-riscv64 -cpu max with --enable-riscv-asm=zv,zvkb,zvkned: the SHA-512 tests pass, and an aligned and a misaligned Transform of the same block give the same digest. qemu does not fault on misaligned vector loads, so the trap itself was not reproduced. Thanks! |
||
| #else | ||
| XMEMCPY(buffer, sha->buffer, WC_SHA512_BLOCK_SIZE); | ||
| XMEMCPY(sha->buffer, data, WC_SHA512_BLOCK_SIZE); | ||
| #if defined(LITTLE_ENDIAN_ORDER) && !defined(WC_SHA512_RAW_BE_BUFFER) | ||
| #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \ | ||
| (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)) | ||
| if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags)) | ||
| #endif | ||
| { | ||
| ByteReverseWords64((word64*)data, (word64*)data, | ||
| WC_SHA512_BLOCK_SIZE); | ||
| ByteReverseWords64(sha->buffer, sha->buffer, WC_SHA512_BLOCK_SIZE); | ||
| } | ||
| #endif /* LITTLE_ENDIAN_ORDER && !WC_SHA512_RAW_BE_BUFFER */ | ||
|
|
||
| #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM) | ||
| ByteReverseWords64(buffer, (word64*)data, WC_SHA512_BLOCK_SIZE); | ||
| ret = Transform_Sha512(sha, (const byte*)buffer); | ||
| #elif defined(WOLFSSL_PPC64_ASM) || defined(WOLFSSL_PPC32_ASM) | ||
| /* PPC assembly uses the (sha, data) form and reads the block directly | ||
| * (big-endian native - any little-endian reversal was done above). */ | ||
| (void)buffer; | ||
| ret = Transform_Sha512(sha, data); | ||
| #else | ||
| XMEMCPY(buffer, sha->buffer, WC_SHA512_BLOCK_SIZE); | ||
| XMEMCPY(sha->buffer, data, WC_SHA512_BLOCK_SIZE); | ||
| #endif | ||
|
|
||
| ret = Transform_Sha512(sha); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.