Skip to content

earlgrey/cryptolib: Generate Rust FFI and safe bindings for libotcrypto using bindgen and syn crate - #452

Open
lchiawei wants to merge 3 commits into
OpenPRoT:earlgrey-hwefrom
lchiawei:feature/libotcrypto
Open

earlgrey/cryptolib: Generate Rust FFI and safe bindings for libotcrypto using bindgen and syn crate#452
lchiawei wants to merge 3 commits into
OpenPRoT:earlgrey-hwefrom
lchiawei:feature/libotcrypto

Conversation

@lchiawei

Copy link
Copy Markdown

This PR introduces the earlgrey_cryptolib crate for OpenTitan Earlgrey:

  • Adds a host tool (tools/otcrypto_bindgen) using syn to sanitize bindgen FFI and derive zerocopy for IPC.
  • Provides safe Rust abstractions for libotcrypto.a (SHA-2 hashing, status conversion, buffer checksums).
  • Adds microkernel QEMU and FPGA smoke tests.

…crypto

Add a dedicated host tool to transform OpenTitan libotcrypto C bindings into idiomatic Rust FFI.

Key changes in this commit:
* Patch rules_rust_bindgen in MODULE.bazel to support static library binding workflows.
* Parse raw Rust bindings with syn to inspect AST syntax nodes.
* Convert type and enum names to PascalCase and field names to snake_case.
* Strip C prefixes from enum variants and type identifiers.
* Rewrite raw pointer fields (*mut u32) in key structs to u32 integers for IPC safety.
* Generate inline accessor methods to safely retrieve pointer values.
* Derive zerocopy traits (IntoBytes, FromBytes, Immutable, KnownLayout) on POD types.
* Add Bazel unit tests to verify AST transformations and layout preservation.
Add the earlgrey_cryptolib crate with dynamic Bazel FFI bindings and safe wrappers for Earlgrey.

Key changes in this commit:
* Link precompiled libotcrypto.a via cc_import.
* Generate raw Rust bindings dynamically from @lowrisc_opentitan using rust_bindgen.
* Apply otcrypto_bindgen to generate the sanitized otcrypto_sys FFI module.
* Implement CryptoStatus and CryptoError types for hardened status code conversion.
* Add safe wrappers for initialization (init) and buffer checksum calculation.
* Add safe one-shot hashing APIs for SHA-256, SHA-384, and SHA-512.
* Configure presubmit license checks for cryptolib source files.
Add integration smoke tests for earlgrey_cryptolib on the Earlgrey target.

Key changes in this commit:
* Configure system memory mappings for crypto peripherals (HMAC, AES, OTBN, CSRNG, EDN).
* Add alert handler and clkmgr peripheral mappings to system.json5.
* Enable transactional crypto peripheral clocks in test runtime setup.
* Add smoke test cases for otcrypto_init and NIST test vectors for SHA-256, SHA-384, and SHA-512.
* Verify empty-string hash vectors and zerocopy key structure transformations.
* Define opentitan_test targets for QEMU and FPGA (cw340, hyper310, hyper340).
@lchiawei
lchiawei marked this pull request as ready for review September 2, 2026 03:14
@lchiawei
lchiawei requested a review from cfrantz September 2, 2026 18:49
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.

1 participant