Refactor lib.rs and add PKCS#11 API-level logging#427
Merged
simo5 merged 18 commits intolatchset:mainfrom Mar 6, 2026
Merged
Conversation
178e0c5 to
735f029
Compare
Jakuje
reviewed
Mar 6, 2026
Extract general management functions (Initialize, Finalize, GetInfo, GetFunctionList, etc.) from `src/lib.rs` into a new `src/fns` module to improve code organization. Update visibility of internal structures (State, Config) and global statics to `pub(crate)` to share them with the new module. Move global locking macros to the new module and update cdylib imports. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Move slot and token management functions (such as C_GetSlotList, C_InitToken, etc.) from src/lib.rs into a new dedicated module at src/fns/stmgmt.rs. Additionally, move internal helper macros (res_or_ret, cast_or_ret, etc.) to src/fns/mod.rs to facilitate code reuse across modules and clean up the main library file. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Move session management functions (such as OpenSession, CloseSession, Login, Logout, and SessionCancel) from lib.rs into a new dedicated module. This improves code organization by reducing the size of the main library file and grouping related functionality together. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Move the implementation of object management functions (create, copy, destroy, get/set attributes, and find objects) from src/lib.rs to a new module src/fns/objmgmt.rs. This improves code organization by reducing the size of the main library file and grouping related logic. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
This refactors the encryption, decryption, and message encryption/decryption implementations out of `src/lib.rs` and into a new `src/fns/encryption.rs` module. This reduces the size of the main library file and groups related cryptographic operations together. Several helper functions (FIPS approval and mechanism checks) are made crate-public to support this move. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Refactor the codebase by extracting all signing, verification, and related signature handling functions (C_Sign*, C_Verify*) from src/lib.rs into a new dedicated module src/fns/signing.rs. This improves code organization and reduces the file size of the main library entry point. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Refactor the implementation of key generation, derivation, wrapping, and encapsulation functions out of `src/lib.rs` into a new dedicated module `src/fns/keymgmt.rs`. This improves code organization and reduces the size of the main library file. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Relocate the implementation of dual-function cryptographic operations (DigestEncrypt, DecryptDigest, SignEncrypt, and DecryptVerify) from lib.rs to a new src/fns/dual.rs module. This improves code organization and aligns with the structure of other cryptographic functions. Additionally, make internal_digest_update public within the crate so it can be utilized by the new module. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Extract the message digest functions (DigestInit, Digest, DigestUpdate, DigestKey, DigestFinal) from lib.rs into a new dedicated module at src/fns/digest.rs. This refactoring reduces the complexity of the main library file and improves code organization by grouping digest operations together. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Move the implementations for random number generation, legacy control functions, and async placeholders from src/lib.rs to src/fns/mod.rs. This improves code organization by grouping function definitions and decluttering the main library file. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Reorder module declarations and static definitions in lib.rs for better organization. Move the check_test_slot_busy helper function to the tests module to reduce clutter in the main library entry point. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Remove `global_rlock!` and `global_wlock!` macros in favor of encapsulation within `GlobalState` and `GlobalConfig` structs. These types now manage the internal `RwLock`s and expose `rlock()` and `wlock()` methods to handle lock acquisition and initialization checks, resulting in cleaner usage sites and improved type safety. Signed-off-by: Simo Sorce <simo@redhat.com>
Refactor C-API entry points to separate FFI wrappers from implementation logic. Move the logic to inner functions returning `Result`, allowing idiomatic error handling with the `?` operator. Add debug logging to the wrappers to trace function arguments and return codes. This improves the ability to debug the module at the boundary. Remove obsolete macros `res_or_ret`, `ret_to_rv`, and `cast_or_ret`. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Refactor the `bytes_to_vec!` and `bytes_to_slice!` macros into proper generic functions. This change improves type safety and requires explicit `unsafe` blocks at call sites when creating slices from raw pointers, making the code's safety requirements more visible. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Refactor the mechanism parameter casting logic by replacing the `cast_params!` macro with a generic `cast_params` function in `src/misc.rs`. This change improves code clarity by removing the macro and placing the length validation and pointer casting logic into a single function. Call sites have been updated to use the new function and explicitly wrap the invocation in `unsafe` blocks, as the function involves dereferencing raw pointers. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Remove the custom `map_err!` macro definition from `src/error.rs` and replace all usages with the standard `Result::map_err` combinator. This simplifies the code by removing unnecessary macro indirection and utilizing standard Rust idioms for handling integer conversion errors. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Delete `general_error`, `device_error`, `arg_bad`, and the `some_or_err` macro from the error module. Update call sites to use `Error::ck_rv_from_error` directly or map errors to specific PKCS#11 return values, simplifying the error handling code. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Dereferencing `CK_MECHANISM` pointers directly can lead to unaligned memory access, causing undefined behavior or crashes on certain architectures. This introduces `CK_MECHANISM::from_ptr` to safely read the struct by value using `std::ptr::read_unaligned`. All cryptographic function initialization routines have been updated to use this method to ensure memory safety. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Simo Sorce <simo@redhat.com>
Member
Author
|
@Jakuje pleae re-check, I have:
|
Jakuje
approved these changes
Mar 6, 2026
Contributor
Jakuje
left a comment
There was a problem hiding this comment.
I think this looks good now! Thanks for addressing the reported issues!
Member
Author
No, thank you for patiently going through such a big PR and uncovering these issues! |
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
This patchset is mainly refactoring of the lib.rs with no change in behavior except for the added debug logging functionality.
It transitions the code from a monolithic
lib.rsarchitecture into a modular design (src/fns/*) that improves code organization and maintainability.Key changes include:
digest.rs,signing.rs,keymgmt.rs).map_err!,res_or_ret!) are replaced with standard RustResultcombinators (?,.map_err()).log_debug!macro is introduced and applied to all C-API entry points to trace input arguments and return codes.cast_params!andbytes_to_vec!macros are replaced with generic functions, enforcing stricter type checking and requiring explicitunsafeblocks at call sites.The changes generally follow Rust best practices and improve the safety posture of the FFI boundary.
Fixes #425
Reviewer's checklist: