wolfssl/wrapper/rust/wolfssl-wolfcrypt
Reda Chouk de592ef252 Rust wrapper: build and pass tests against the FIPS v6 module, and run doc tests in FIPS builds 2026-11-23 10:24:17 +01:00
..
src Rust wrapper: build and pass tests against the FIPS v6 module, and run doc tests in FIPS builds 2026-11-23 10:24:17 +01:00
tests Rust wrapper: build and pass tests against the FIPS v6 module, and run doc tests in FIPS builds 2026-11-23 10:24:17 +01:00
CHANGELOG.md
…
Cargo.lock
…
Cargo.toml
…
Makefile
…
README.md
…
build.rs Rust wrapper: build and pass tests against the FIPS v6 module, and run doc tests in FIPS builds 2026-11-23 10:24:17 +01:00
headers.h
…

README.md

wolfssl-wolfcrypt crate

The wolfssl-wolfcrypt crate is a Rust wrapper for the wolfCrypt cryptographic algorithms portion of the wolfSSL C library.

This crate requires wolfSSL version 5.9.0 or newer, or the wolfSSL FIPS v5.2.3 module or newer.

The crate uses no_std so that no Rust standard library is required. This makes it well-suited for embedded/bare-metal environments.

There is an optional alloc feature that enables APIs which require heap allocation.

Installation

The wolfssl C library must be installed to be used by the Rust crate.

The wolfssl-wolfcrypt crate can be used by including it as a dependency in your project's Cargo.toml file.

For example:

[dependencies]
wolfssl-wolfcrypt = "3.0"

When upgrading from an earlier major version, see the "Breaking changes" sections of CHANGELOG.md.

API Coverage

This crate provides a wrapper API for the following wolfCrypt C library functionality:

  • AES
    • CBC, CCM, CFB, CTR, EAX, ECB, GCM, OFB, XTS
  • BLAKE2
  • CMAC
  • ChaCha20-Poly1305
  • Curve25519
  • DH
  • ECC
  • Ed25519
  • Ed448
  • HKDF
  • HMAC
  • LMS
  • ML-DSA
  • ML-KEM
  • PBKDF2
  • PKCS #12 PBKDF
  • PRF
  • RNG
  • RSA
  • scrypt
  • SHA
    • SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, SHAKE256
  • SRTP/SRTCP KDF
  • SSH KDF
  • TLSv1.3 HKDF

RustCrypto Trait Support

In addition to its native API, this crate can implement the common RustCrypto traits for wolfCrypt-backed types. Each set of trait implementations is gated behind a Cargo feature so that projects only pull in the dependencies they need. All features are off by default.

Feature RustCrypto crate wolfCrypt types
digest digest SHA (sha_digest), BLAKE2 (blake2_digest)
mac digest (mac) HMAC (hmac_mac), CMAC (cmac_mac), BLAKE2 (blake2_mac)
signature signature ECDSA (ecdsa), RSA PKCS#1 v1.5 (rsa_pkcs1v15), Ed25519 (ed25519), Ed448 (ed448)
cipher cipher AES (aes)
aead aead AES-GCM/CCM (aes), ChaCha20-Poly1305/XChaCha20-Poly1305 (chacha20_poly1305)
rand_core rand_core RNG (random)
rand_core_0_9 rand_core 0.9 RNG (random)
kem kem ML-KEM (mlkem_kem)
password-hash password-hash PBKDF2 (pbkdf2_password_hash), scrypt (scrypt_password_hash)

The BLAKE2, CMAC, and HMAC trait modules additionally require the corresponding algorithm support to be enabled in the wolfSSL C library.

RNG implements the fallible rand_core::TryRng and rand_core::TryCryptoRng traits: RNG errors are returned to the caller as a random::RngError carrying the wolfSSL error code. For APIs requiring the infallible rand_core::Rng and rand_core::CryptoRng traits, wrap the RNG in rand_core::UnwrapErr, which panics on RNG failure:

use rand_core::{Rng, UnwrapErr};
use wolfssl_wolfcrypt::random::RNG;

let mut rng = UnwrapErr(RNG::new().expect("Failed to create RNG"));
let mut buf = [0u8; 32];
rng.fill_bytes(&mut buf);

For projects still on the rand 0.9 ecosystem, the rand_core_0_9 feature implements the equivalent rand_core 0.9 traits, TryRngCore and TryCryptoRng, with the same RngError. Wrap the RNG in the 0.9 UnwrapErr to get RngCore and CryptoRng. Both features can be enabled at once.

Enable features in your Cargo.toml, for example:

[dependencies]
wolfssl-wolfcrypt = { version = "3.0", features = ["digest", "signature"] }

FIPS

When the crate is built against a wolfSSL FIPS module, the fips module provides FIPS-specific APIs:

  • fips::run_all_self_tests() runs all FIPS self tests (CASTs) once. Calling it at startup ensures that concurrent first use of an algorithm cannot fail while that algorithm's self test is running.
  • fips::PrivateKeyRead::enable() returns a guard that allows private key data to be read on the current thread until the guard is dropped. Guards can be nested.
  • fips::set_private_key_read_enable() enables or disables private key reads directly.

Build Notes

WOLFSSL_PREFIX

If the wolfSSL C library is not installed in a default location, you can specify the installation prefix with the WOLFSSL_PREFIX environment variable when building the wolfssl-wolfcrypt crate.

For example:

WOLFSSL_PREFIX=/opt/my-wolfssl-build cargo build

The library may be under either lib or lib64 in the prefix. If WOLFSSL_PREFIX is set but does not point at a usable wolfSSL installation, the build fails rather than falling back to another wolfSSL library.

When WOLFSSL_PREFIX is unset and the crate is built from within a wolfSSL source repository that has been configured, it builds against the library in that repository, and the build fails if that library has not been built yet.

Unsupported wolfSSL Configurations

The crate build fails if the wolfSSL library was built with asynchronous crypto support (WOLFSSL_ASYNC_CRYPT), which the wrapper does not support.

Metadata for Dependent Crates

The crate declares links = "wolfssl" and publishes the following values to the build scripts of crates that depend on it directly:

Variable Value
DEP_WOLFSSL_LIBDIRS Directory containing a shared wolfSSL library (e.g. to set an rpath)
DEP_WOLFSSL_FIPS 1 when wolfSSL is a FIPS build
DEP_WOLFSSL_FIPS_V5 1 when wolfSSL is the FIPS v5 module

Cross-Compiling

Ensure that the target you want to build for is installed for Rust. For example:

rustup target add riscv64imac-unknown-none-elf

Build with the --target option if building manually:

export WOLFSSL_PREFIX=/opt/wolfssl-riscv64
cargo build --target riscv64imac-unknown-none-elf

To specify the linker for the target:

export CARGO_TARGET_RISCV64IMAC_UNKNOWN_NONE_ELF_LINKER=riscv64-elf-gcc