This directory contains Rust examples using the libzupt library through its C API (zupt_cxx.h, zupt.h and zupt_keccak.h), called via FFI using bindgen.
- Rust toolchain (1.70+ recommended)
- CMake 3.15 or higher
- A C/C++ compiler (gcc/clang)
- The built
libzupt.soshared library (in../build/)
If Rust is not installed:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/envcd ..
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DLIBZUPT_BUILD_TESTS=OFF -DLIBZUPT_BUILD_PYTHON=OFF
make -j$(nproc)cd examples_rust
./build.shOr manually with cargo:
cd examples_rust
cargo build --releaseRun a specific example:
cargo run --release --example example_basic
cargo run --release --example example_file
cargo run --release --example example_keygen
cargo run --release --example example_random
cargo run --release --example example_secure_bufferOr run all examples at once:
for ex in example_basic example_file example_keygen example_random example_secure_buffer; do
cargo run --release --example $ex
echo
done- example_basic - Basic encryption/decryption with in-memory data, including wrong-key rejection test.
- example_file - Encrypting and decrypting files (write plaintext to disk, encrypt, save ciphertext, decrypt back).
- example_keygen - Key pair generation, saving/loading keys from disk, and exporting public keys.
- example_random - Generating random bytes, computing SHA-256 and SHA3-512 hashes, and key derivation.
- example_secure_buffer - Using
SecureBufferfor automatic memory zeroization of sensitive data.
generate_keypair() -> Result<KeyPair, ZuptError>- Generate a new hybrid key pairexport_public_key(priv_key, pub_key) -> Result<(), ZuptError>- Export public key from secret keysave_keypair(keypair, path) -> Result<(), ZuptError>- Save key pair to fileload_keypair(path) -> Result<KeyPair, ZuptError>- Load key pair from fileload_public_key(path) -> Result<Vec<u8>, ZuptError>- Load public key from file
encrypt(pub_key, plaintext) -> Result<(Vec<u8>, Vec<u8>), ZuptError>- Encrypt data in memorydecrypt(priv_key, ciphertext, enc_header) -> Result<Vec<u8>, ZuptError>- Decrypt data in memoryencrypt_file(pub_key, path) -> Result<(Vec<u8>, Vec<u8>), ZuptError>- Encrypt a filedecrypt_file(priv_key, path, enc_header) -> Result<Vec<u8>, ZuptError>- Decrypt a file
random_bytes(count) -> Vec<u8>- Generate cryptographically secure random bytessha256(data) -> Vec<u8>- Compute SHA-256 hashsha3_512(data) -> Vec<u8>- Compute SHA3-512 hashsecure_wipe(buf)- Securely wipe memory (zeroing with volatile writes)read_file(path) -> Result<Vec<u8>, ZuptError>- Read file contents via libzuptwrite_file(path, data) -> Result<(), ZuptError>- Write data to file via libzupt
SecureBuffer::from_bytes(data) -> Self- Create from byte sliceSecureBuffer::new(size) -> Self- Create empty buffer of given sizezeroize()- Manually zero out buffer contentsas_slice()/as_mut_slice()- Access underlying data- Memory is automatically zeroed on drop
| Constant | Value |
|---|---|
MLKEM_PUBLICKEYBYTES |
1184 |
MLKEM_SECRETKEYBYTES |
2400 |
MLKEM_CIPHERTEXTBYTES |
1088 |
MLKEM_SSBYTES |
32 |
X25519_KEYBYTES |
32 |
HYBRID_PUB_KEY_SIZE |
1224 |
HYBRID_PRIV_KEY_SIZE |
3656 |
HYBRID_ENC_HEADER_SIZE |
1137 |
AES_KEY_SIZE |
32 |
AES_NONCE_SIZE |
16 |
HMAC_SIZE |
32 |
examples_rust/
Cargo.toml - Package manifest and dependencies
build.rs - Build script (generates FFI bindings via bindgen)
build.sh - Convenience build script (builds libzupt + Rust examples)
src/
lib.rs - Safe Rust wrapper around libzupt C API
examples/
example_basic.rs
example_file.rs
example_keygen.rs
example_random.rs
example_secure_buffer.rs
The build.rs script uses bindgen to automatically generate Rust FFI bindings from the libzupt C header files at compile time. The src/lib.rs wraps the raw FFI calls in safe Rust functions with proper error handling (Result<T, ZuptError>).
The build process:
bindgenreadszupt_cxx.h,zupt.h, andzupt_keccak.hfrom../include/- Generated bindings are written to
$OUT_DIR/bindings.rs - The crate links against
libzupt.so(shared) orlibzupt_static.a(static) from../build/
SPDX-License-Identifier: MIT