Skip to content

Add SHA3 mechanism support - #894

Open
AlexLaroche wants to merge 10 commits into
softhsm:mainfrom
AlexLaroche:add-sha3-support
Open

Add SHA3 mechanism support#894
AlexLaroche wants to merge 10 commits into
softhsm:mainfrom
AlexLaroche:add-sha3-support

Conversation

@AlexLaroche

@AlexLaroche AlexLaroche commented Jul 22, 2026

Copy link
Copy Markdown

Summary

Implements the full PKCS#11 v3.2 SHA3 (FIPS 202) mechanism family for both the OpenSSL and Botan crypto backends:

  • Plain digest (CKM_SHA3_224/256/384/512)
  • HMAC (CKM_SHA3_*_HMAC)
  • RSA combined signing (CKM_SHA3_*_RSA_PKCS, CKM_SHA3_*_RSA_PKCS_PSS), including the raw/precomputed-digest CKM_RSA_PKCS_PSS path
  • DSA combined signing (CKM_DSA_SHA3_*)
  • ECDSA combined signing (CKM_ECDSA_SHA3_*)
  • SHA3 as an RSA-OAEP hash parameter

Not included: CKM_SHA3_*_KEY_GEN and CKM_SHA3_*_HMAC_GENERAL — neither is implemented for any existing hash family today, so neither is added here either.

Closes #603.

Test plan

  • HashTests: FIPS 202 known-answer vectors for SHA3-224/256/384/512, both backends
  • DigestTests: digest round-trip for all four sizes
  • SignVerifyTests: HMAC-SHA3 round-trip; RSA-PKCS and RSA-PKCS-PSS round-trip (named and raw/precomputed-digest); ECDSA round-trip across P-256/P-384/P-521; DSA wrong-key-type negative cases extended (no positive DSA+hash round-trip exists for any size today, so none was added here either)
  • AsymEncryptDecryptTests: RSA-OAEP round-trip with a SHA3 hash parameter, with and without a label
  • Full test suite green on both the OpenSSL and Botan crypto backends

Summary by CodeRabbit

  • New Features
    • Added SHA-3 support for hashing, HMAC, and SHA-3–based PKCS#11 operations including RSA (PKCS, RSA-PSS), RSA-OAEP, DSA, and ECDSA.
    • Added SHA3-224/256/384/512 mechanism recognition (including SHA3-based MGF1) and improved mechanism info reporting and secret-key check handling.
  • Tests
    • Expanded automated coverage for SHA-3 digests, HMAC, sign/verify (RSA/DSA/ECDSA), and RSA-OAEP.
  • Documentation
    • Added --enable-sha3 configure option.

Implement the full PKCS#11 v3.2 SHA3 mechanism family for both the
OpenSSL and Botan crypto backends: plain digest (CKM_SHA3_*), HMAC
(CKM_SHA3_*_HMAC), and RSA/DSA/ECDSA combined signing
(CKM_SHA3_*_RSA_PKCS[_PSS], CKM_DSA_SHA3_*, CKM_ECDSA_SHA3_*). Also
accepts a SHA3 hash parameter in CKM_RSA_PKCS_OAEP and in raw
CKM_RSA_PKCS_PSS signing with a precomputed digest.

Not implemented: CKM_SHA3_*_KEY_GEN and CKM_SHA3_*_HMAC_GENERAL,
neither of which is implemented for any existing hash family today.

Closes softhsm#603.
@AlexLaroche
AlexLaroche requested a review from a team as a code owner July 22, 2026 23:16
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

SoftHSMv2 adds conditional SHA-3-224/256/384/512 support for hashing, HMAC, RSA, DSA, ECDSA, and RSA-OAEP through OpenSSL and Botan, including configuration detection, PKCS#11 routing, backend implementations, build integration, and tests.

Changes

SHA-3 crypto foundation

Layer / File(s) Summary
Algorithm contracts and backend adapters
src/lib/crypto/*
Adds SHA-3 hash, HMAC, and asymmetric mechanism types, OpenSSL and Botan implementations, factory mappings, and build-source registration.
Configuration and PKCS#11 routing
m4/*, src/lib/SoftHSM.cpp, src/lib/P11Attributes.cpp, src/bin/dump/tables.h
Adds --enable-sha3, OpenSSL detection, mechanism registration and metadata, digest/HMAC routing, RSA parameter handling, and SHA-3 HMAC key handling.
Asymmetric backend operations
src/lib/crypto/OSSLRSA.cpp, src/lib/crypto/BotanRSA.cpp, src/lib/crypto/*DSA.cpp, src/lib/crypto/*ECDSA.cpp
Maps SHA-3 RSA, DSA, and ECDSA mechanisms to signing and verification implementations, including RSA-PSS and OAEP validation.
Validation coverage
src/lib/crypto/test/*, src/lib/test/*
Adds SHA-3 hash, digest, HMAC, RSA, ECDSA, OAEP, and wrong-key-type tests.

Estimated code review effort: 4 (Complex) | ~60 minutes

Possibly related PRs

Suggested labels: enhancement

Suggested reviewers: bukka

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR adds substantial HMAC, RSA, DSA, OAEP, build, and test work beyond the linked issue's SHA3 digest/ECDSA scope. Either split the extra SHA3 HMAC/RSA/DSA/OAEP work into separate changes or add linked issues covering that broader scope.
Docstring Coverage ⚠️ Warning Docstring coverage is 17.98% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding SHA3 mechanism support.
Linked Issues check ✅ Passed The changes implement CKM_SHA3_* and CKM_ECDSA_SHA3_* support requested by #603 across both backends and tests.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lib/crypto/BotanRSA.cpp`:
- Around line 1478-1493: Update the sLen upper-bound check in the hash-parameter
handling near the SHA3 cases to use the computed allowedLen instead of the
hardcoded SHA-1 digest size. Preserve the existing PSS bound logic while
applying the selected hash algorithm’s digest length for all supported
algorithms.

In `@src/lib/crypto/OSSLRSA.cpp`:
- Around line 180-195: Update the OpenSSL/LibreSSL dependency validation in
configure.ac to require OpenSSL 1.1.1 or newer and LibreSSL 3.8.0 or newer,
matching the direct EVP_sha3_* calls in the HashAlgo SHA3 cases of OSSLRSA.cpp.
Ensure unsupported versions are rejected during configuration rather than
reaching compilation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 35e1972a-5cb0-4820-8504-0a2cf3fe700e

📥 Commits

Reviewing files that changed from the base of the PR and between f12916e and 4c583bf.

📒 Files selected for processing (41)
  • src/bin/dump/tables.h
  • src/lib/P11Attributes.cpp
  • src/lib/SoftHSM.cpp
  • src/lib/crypto/AsymmetricAlgorithm.h
  • src/lib/crypto/BotanCryptoFactory.cpp
  • src/lib/crypto/BotanDSA.cpp
  • src/lib/crypto/BotanECDSA.cpp
  • src/lib/crypto/BotanMAC.cpp
  • src/lib/crypto/BotanMAC.h
  • src/lib/crypto/BotanRSA.cpp
  • src/lib/crypto/BotanSHA3_224.cpp
  • src/lib/crypto/BotanSHA3_224.h
  • src/lib/crypto/BotanSHA3_256.cpp
  • src/lib/crypto/BotanSHA3_256.h
  • src/lib/crypto/BotanSHA3_384.cpp
  • src/lib/crypto/BotanSHA3_384.h
  • src/lib/crypto/BotanSHA3_512.cpp
  • src/lib/crypto/BotanSHA3_512.h
  • src/lib/crypto/CMakeLists.txt
  • src/lib/crypto/HashAlgorithm.h
  • src/lib/crypto/MacAlgorithm.h
  • src/lib/crypto/Makefile.am
  • src/lib/crypto/OSSLCryptoFactory.cpp
  • src/lib/crypto/OSSLDSA.cpp
  • src/lib/crypto/OSSLECDSA.cpp
  • src/lib/crypto/OSSLHMAC.cpp
  • src/lib/crypto/OSSLHMAC.h
  • src/lib/crypto/OSSLRSA.cpp
  • src/lib/crypto/OSSLSHA3_224.cpp
  • src/lib/crypto/OSSLSHA3_224.h
  • src/lib/crypto/OSSLSHA3_256.cpp
  • src/lib/crypto/OSSLSHA3_256.h
  • src/lib/crypto/OSSLSHA3_384.cpp
  • src/lib/crypto/OSSLSHA3_384.h
  • src/lib/crypto/OSSLSHA3_512.cpp
  • src/lib/crypto/OSSLSHA3_512.h
  • src/lib/crypto/test/HashTests.cpp
  • src/lib/crypto/test/HashTests.h
  • src/lib/test/AsymEncryptDecryptTests.cpp
  • src/lib/test/DigestTests.cpp
  • src/lib/test/SignVerifyTests.cpp

Comment thread src/lib/crypto/BotanRSA.cpp
Comment thread src/lib/crypto/OSSLRSA.cpp
EVP_sha3_*() requires OpenSSL >= 1.1.1 or LibreSSL >= 3.8.0, newer than
SoftHSM's OpenSSL 1.0.0 baseline. Add a --enable-sha3 configure option
(default detect, mirroring --enable-ecc/--enable-eddsa/--enable-mldsa)
that probes for real EVP_sha3_224/256/384/512 support via a compile/link
check rather than a version-number comparison, since LibreSSL pins
OPENSSL_VERSION_NUMBER to a fixed value and can't be version-sniffed.
Botan always resolves to supported, since it has had SHA3 since well
before its own minimum required version.

Every SHA3 code path is gated behind the resulting WITH_SHA3 define:
the OSSLSHA3_*/BotanSHA3_* digest classes, the HMAC-SHA3 implementations,
crypto factory dispatch, every EVP_sha3_*() call site in OSSLRSA.cpp, the
PKCS#11 mechanism registration/dispatch in SoftHSM.cpp, and the
corresponding unit tests -- mirroring how WITH_ECC/WITH_EDDSA are already
handled throughout the codebase. This keeps the OpenSSL floor at 1.0.0
while avoiding build failures against old OpenSSL or LibreSSL.
getCipherRawPss() hardcoded the salt-length upper bound
(bitLength+6)/8-2-20 assuming a 20-byte SHA-1 digest, even though the
function already computes allowedLen per hash algorithm just above.
For any non-SHA1 hash (SHA-224/256/384/512, SHA3-224/256/384/512) this
let sLen exceed the true PKCS#1 v2.1 RSASSA-PSS bound
(emLen - hLen - 2), miscomputing the maximum allowed salt length. Use
allowedLen instead of the hardcoded 20.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@m4/acx_openssl_sha3.m4`:
- Around line 11-34: Update the OpenSSL SHA3 capability check using
AC_CACHE_CHECK, or move AC_MSG_RESULT reporting outside AC_CACHE_VAL, so the
cached value still produces a configure result on every run. Preserve the
existing acx_cv_lib_openssl_sha3_support detection and yes/no value assignments.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0e2bcbde-32a0-4a05-b744-43ce6bdcc5ec

📥 Commits

Reviewing files that changed from the base of the PR and between 4c583bf and 41cd8d2.

📒 Files selected for processing (23)
  • README.md
  • m4/acx_crypto_backend.m4
  • m4/acx_openssl_sha3.m4
  • src/lib/SoftHSM.cpp
  • src/lib/crypto/BotanCryptoFactory.cpp
  • src/lib/crypto/BotanMAC.cpp
  • src/lib/crypto/BotanRSA.cpp
  • src/lib/crypto/BotanSHA3_224.cpp
  • src/lib/crypto/BotanSHA3_256.cpp
  • src/lib/crypto/BotanSHA3_384.cpp
  • src/lib/crypto/BotanSHA3_512.cpp
  • src/lib/crypto/OSSLCryptoFactory.cpp
  • src/lib/crypto/OSSLHMAC.cpp
  • src/lib/crypto/OSSLRSA.cpp
  • src/lib/crypto/OSSLSHA3_224.cpp
  • src/lib/crypto/OSSLSHA3_256.cpp
  • src/lib/crypto/OSSLSHA3_384.cpp
  • src/lib/crypto/OSSLSHA3_512.cpp
  • src/lib/crypto/test/HashTests.cpp
  • src/lib/crypto/test/HashTests.h
  • src/lib/test/AsymEncryptDecryptTests.cpp
  • src/lib/test/DigestTests.cpp
  • src/lib/test/SignVerifyTests.cpp
🚧 Files skipped from review as they are similar to previous changes (19)
  • src/lib/crypto/BotanSHA3_224.cpp
  • src/lib/crypto/BotanSHA3_256.cpp
  • src/lib/crypto/BotanSHA3_512.cpp
  • src/lib/crypto/OSSLSHA3_512.cpp
  • src/lib/crypto/OSSLSHA3_384.cpp
  • src/lib/test/DigestTests.cpp
  • src/lib/crypto/OSSLSHA3_224.cpp
  • src/lib/crypto/OSSLCryptoFactory.cpp
  • src/lib/crypto/BotanCryptoFactory.cpp
  • src/lib/crypto/BotanSHA3_384.cpp
  • src/lib/crypto/BotanMAC.cpp
  • src/lib/crypto/test/HashTests.h
  • src/lib/crypto/test/HashTests.cpp
  • src/lib/crypto/OSSLSHA3_256.cpp
  • src/lib/crypto/OSSLHMAC.cpp
  • src/lib/crypto/BotanRSA.cpp
  • src/lib/crypto/OSSLRSA.cpp
  • src/lib/test/SignVerifyTests.cpp
  • src/lib/SoftHSM.cpp

Comment thread m4/acx_openssl_sha3.m4 Outdated
AC_CACHE_VAL skips its body once acx_cv_lib_openssl_sha3_support is
cached, so the AC_MSG_RESULT calls inside it never ran on repeat
configure invocations, leaving "checking for OpenSSL SHA3 support..."
without a yes/no. Switch to AC_CACHE_CHECK, which prints the cached
result unconditionally instead of only on a cold cache.
Every other optional crypto feature added alongside SHA3 (ECC, EDDSA,
ML-DSA, ML-KEM) has a #cmakedefine entry here; SHA3 was missing one.
Comment thread src/lib/crypto/BotanRSA.cpp Outdated
Addresses review comment on PR softhsm#894: the mechanism-param type/hash/MGF
validation and salt-length bound check were duplicated across all 10
PSS hash variants in both signInit and verifyInit.
…SLRSA)

Antoine pointed out on PR softhsm#894 that the same duplication factored out
of BotanRSA in the previous commit exists in OSSLRSA's signInit and
verifyInit. Applies the same checkPssParams() helper there.
Comment thread src/lib/crypto/OSSLRSA.cpp Outdated
Antoine noted on PR softhsm#894 that BotanRSA::checkPssParams and
OSSLRSA::checkPssParams were identical, and suggested factoring it
into a dedicated utility class the way MLDSAUtil/MLKEMUtil already
do for their algorithms. Adds RSAUtil::checkPssParams and has both
backends call it instead of keeping their own copy.
@antoinelochet

Copy link
Copy Markdown
Contributor

I think this one is ready for merging.

@bukka bukka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good, just some minor things.

Comment thread src/lib/crypto/BotanSHA3_224.cpp Outdated
Comment thread config.h.in.cmake
bukka pointed out on PR softhsm#894 that new files should use the short
"Copyright (c) 2026 SoftHSMv2 contributors / SPDX-License-Identifier:
BSD-2-Clause" header (as used by RSAUtil.h etc.) instead of the long
boilerplate BSD license text copied from the file each was based on.
bukka asked on PR softhsm#894 how WITH_SHA3 gets enabled via CMake — it
didn't; config.h.in.cmake referenced @WITH_SHA3@ but nothing ever set
it, so CMake builds silently compiled out all SHA3 support. Mirrors
the autotools logic (m4/acx_crypto_backend.m4, acx_openssl_sha3.m4):
Botan always has SHA3 so it's set unconditionally, OpenSSL gets a
try_run sanity check requiring OpenSSL >= 1.1.1 / LibreSSL >= 3.8.0.

Verified with a real CMake configure+build: OpenSSL backend builds
clean and cryptotest/p11test (81 tests, SHA3 PSS included) pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for SHA3 digest mechanisms

3 participants