Rust crate updates 2026-05-05#10402
Open
holtrop-wolfssl wants to merge 6 commits intowolfSSL:masterfrom
Open
Conversation
- store pointer to WC_RNG instead of full struct - enforce RNG is not dropped before consumer structs The C library stores a pointer via the set_rng() methods on a few structs (e.g. RSA). This change holds a reference (or instance) of RNG within the consumer structs to ensure it is kept alive if set_rng (or now set_shared_rng) is used.
This fixes internal pointers breaking if Rust moves the ECC struct (with some build configurations).
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the wolfssl-wolfcrypt Rust wrapper to (1) improve RNG lifetime safety across FFI consumers, (2) add RustCrypto trait integrations for BLAKE2 digest/MAC, and (3) extend AEAD support with AES-192 CCM/GCM wrappers, along with corresponding test updates.
Changes:
- Refactors
RNGto own a C-heapWC_RNG*and updates RNG-taking APIs to accept&RNG(plus newset_shared_rng(Arc<RNG>)for consumers that store an RNG pointer internally). - Adds RustCrypto
digest::Digestwrappers (blake2_digest) anddigest::Macwrappers (blake2_mac) for BLAKE2b/BLAKE2s. - Adds AEAD wrappers and tests for AES-192-GCM and AES-192-CCM, and adds
Clonesupport for HMAC MAC types.
Reviewed changes
Copilot reviewed 28 out of 28 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| wrapper/rust/wolfssl-wolfcrypt/tests/test_rsa.rs | Updates tests to new RNG borrowing/sharing patterns (&RNG, set_shared_rng). |
| wrapper/rust/wolfssl-wolfcrypt/tests/test_random.rs | Updates RNG tests to match RNG methods taking &self (no mut). |
| wrapper/rust/wolfssl-wolfcrypt/tests/test_hmac_mac.rs | Adds a clone/forking test to validate cloned HMAC MAC state equivalence. |
| wrapper/rust/wolfssl-wolfcrypt/tests/test_ecc.rs | Updates ECC tests for RNG ownership and shared RNG binding. |
| wrapper/rust/wolfssl-wolfcrypt/tests/test_curve25519.rs | Updates Curve25519 tests for conditional RNG sharing when blinding is enabled. |
| wrapper/rust/wolfssl-wolfcrypt/tests/test_blake2_mac.rs | Adds MAC trait tests for BLAKE2b/BLAKE2s keyed constructions. |
| wrapper/rust/wolfssl-wolfcrypt/tests/test_blake2_digest.rs | Adds Digest trait tests for typed BLAKE2b/BLAKE2s hashers. |
| wrapper/rust/wolfssl-wolfcrypt/tests/test_aes.rs | Adds AES-192-GCM/CCM AEAD roundtrip tests. |
| wrapper/rust/wolfssl-wolfcrypt/src/rsa.rs | Refactors RSA RNG usage (&RNG params, owned/shared RNG binding stored to ensure lifetime). |
| wrapper/rust/wolfssl-wolfcrypt/src/rsa_pkcs1v15.rs | Updates RSA PKCS#1v1.5 signing wrapper to new RNG pointer model. |
| wrapper/rust/wolfssl-wolfcrypt/src/random.rs | Refactors RNG to own WC_RNG* allocated via wc_rng_new_ex, updates methods to take &self. |
| wrapper/rust/wolfssl-wolfcrypt/src/mlkem.rs | Updates ML-KEM APIs to accept &RNG and pass WC_RNG* through FFI. |
| wrapper/rust/wolfssl-wolfcrypt/src/lms.rs | Updates LMS keygen to accept &RNG. |
| wrapper/rust/wolfssl-wolfcrypt/src/lib.rs | Adds alloc support and conditionally exports new BLAKE2 digest/MAC modules. |
| wrapper/rust/wolfssl-wolfcrypt/src/hmac.rs | Implements deep Clone for HMAC via wc_HmacCopy. |
| wrapper/rust/wolfssl-wolfcrypt/src/hmac_mac.rs | Derives Clone for HMAC MAC wrapper types. |
| wrapper/rust/wolfssl-wolfcrypt/src/ed448.rs | Updates Ed448 key generation to accept &RNG. |
| wrapper/rust/wolfssl-wolfcrypt/src/ed25519.rs | Updates Ed25519 key generation to accept &RNG. |
| wrapper/rust/wolfssl-wolfcrypt/src/ecdsa.rs | Adapts ECDSA wrapper FFI calls to ECC key pointer storage changes. |
| wrapper/rust/wolfssl-wolfcrypt/src/ecc.rs | Refactors ECC to store a C-heap ecc_key* and adds owned/shared RNG binding. |
| wrapper/rust/wolfssl-wolfcrypt/src/dilithium.rs | Updates Dilithium APIs to accept &RNG. |
| wrapper/rust/wolfssl-wolfcrypt/src/dh.rs | Updates DH APIs to accept &RNG. |
| wrapper/rust/wolfssl-wolfcrypt/src/curve25519.rs | Updates Curve25519 APIs to accept &RNG, adds RNG ownership/sharing for blinding. |
| wrapper/rust/wolfssl-wolfcrypt/src/blake2_mac.rs | Adds RustCrypto Mac trait wrappers for keyed BLAKE2b/BLAKE2s. |
| wrapper/rust/wolfssl-wolfcrypt/src/blake2_digest.rs | Adds RustCrypto Digest trait wrappers for typed BLAKE2b/BLAKE2s hashers. |
| wrapper/rust/wolfssl-wolfcrypt/src/aes.rs | Adds AES-192 CCM/GCM AEAD wrappers. |
| wrapper/rust/wolfssl-wolfcrypt/Makefile | Enables the new alloc feature in the Makefile feature set. |
| wrapper/rust/wolfssl-wolfcrypt/Cargo.toml | Replaces std feature with alloc and keeps feature list in sync with new APIs/modules. |
Comments suppressed due to low confidence (1)
wrapper/rust/wolfssl-wolfcrypt/Cargo.toml:22
- This change removes the previously exported
stdfeature and adds a newallocfeature. Together with the public API signature changes in this PR (e.g.,set_rng/generatenow takingRNGor&RNGinstead of&mut RNG), this is a semver-breaking change for a 1.x crate. Consider either (a) bumping the crate major version, or (b) keepingstdas a backwards-compatible feature alias toallocand providing compatibility shims where practical.
[features]
alloc = []
rand_core = ["dep:rand_core"]
aead = ["dep:aead"]
cipher = ["dep:cipher"]
mac = ["digest/mac"]
digest = ["dep:digest"]
signature = ["dep:signature"]
password-hash = ["dep:password-hash", "password-hash/phc"]
kem = ["dep:kem", "hybrid-array/extra-sizes"]
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+400
to
+409
| fn new_ecc_key(heap: *mut core::ffi::c_void, dev_id: i32) -> Result<*mut sys::ecc_key, i32> { | ||
| let key = unsafe { sys::wc_ecc_key_new(heap) }; | ||
| if key.is_null() { | ||
| return Err(sys::wolfCrypt_ErrorCodes_MEMORY_E); | ||
| } | ||
| let rc = unsafe { sys::wc_ecc_init_ex(key, heap, dev_id) }; | ||
| if rc != 0 { | ||
| unsafe { sys::wc_ecc_key_free(key); } | ||
| return Err(rc); | ||
| } |
Comment on lines
+1686
to
+1693
| /// instance. | ||
| /// | ||
| /// # Safety contract | ||
| /// | ||
| /// The caller must ensure that the `RNG` instance is not dropped before | ||
| /// this `ECC` instance. The `ECC` struct holds an internal pointer to the | ||
| /// `RNG`'s underlying `WC_RNG` context, and dropping the `RNG` first | ||
| /// would result in a dangling pointer. |
Comment on lines
+1188
to
+1195
| /// instance. | ||
| /// | ||
| /// # Safety contract | ||
| /// | ||
| /// The caller must ensure that the `RNG` instance is not dropped before | ||
| /// this `RSA` instance. The `RSA` struct holds an internal pointer to the | ||
| /// `RNG`'s underlying `WC_RNG` context, and dropping the `RNG` first | ||
| /// would result in a dangling pointer. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Testing
Unit/CI tests.
Checklist