Expand description
Idiomatic Rust binding to the RNP OpenPGP C library (librnp).
RNP is the OpenPGP implementation that powers Mozilla Thunderbird. This
crate provides a thin, idiomatic Rust wrapper over the public C FFI
declared in <rnp/rnp.h>.
§Quick start
Generate a keypair, sign a message, verify the signature:
use rnp::{Algorithm, Context, Hash, KeyBuilder, KeyUsage};
let ctx = Context::new()?;
let key = KeyBuilder::new(Algorithm::Rsa)
.bits(2048)
.userid("alice <alice@example.com>")
.hash(Hash::Sha256)
.add_usage(KeyUsage::Sign)
.add_usage(KeyUsage::Certify)
.build(&ctx)?;
let message = b"hello, world";
let signed = rnp::sign(&ctx, message, &key)?;
let result = rnp::verify(&ctx, &signed)?;
assert!(result.any_valid()?);Encrypt and decrypt with a recipient key:
use rnp::{Algorithm, Context, Decryptor, Encryptor, KeyBuilder, KeyUsage, Output};
let ctx = Context::new()?;
let key = KeyBuilder::new(Algorithm::Rsa)
.bits(2048)
.userid("enc <enc@example.com>")
.add_usage(KeyUsage::EncryptComms)
.build(&ctx)?;
let mut ct = Output::to_memory()?;
Encryptor::new(&ctx, b"secret")?
.add_recipient(&key)
.build(&mut ct)?;
let ciphertext = ct.into_bytes()?;
let result = Decryptor::new(&ctx, &ciphertext).build()?;
assert_eq!(result.plaintext(), b"secret");Multi-signer detached signature via the Signer builder:
use rnp::{Algorithm, Context, Hash, KeyBuilder, KeyUsage, Mode, Signer};
let ctx = Context::new()?;
let k1 = KeyBuilder::new(Algorithm::Rsa).bits(2048).userid("a")
.add_usage(KeyUsage::Sign).build(&ctx)?;
let k2 = KeyBuilder::new(Algorithm::Rsa).bits(2048).userid("b")
.add_usage(KeyUsage::Sign).build(&ctx)?;
let sig = Signer::new(&ctx, b"doc", Mode::Detached)
.add_signer(&k1)
.add_signer_with_hash(&k2, Hash::Sha384)
.armor(true)
.build_to_memory()?;§Streaming
Any std::io::Read plugs in via Input::from_reader and any
std::io::Write via Output::to_writer, so large or non-seekable
data is processed without buffering it in memory:
let input = Input::from_reader(network_stream())?;
let mut output = Output::to_writer(tcp_sink())?;
Encryptor::new_with_input(&ctx, input)?.add_recipient(&key).build(&mut output)?;Readers and writers are read and written lazily while the operation
runs; a failed stream surfaces its original std::io::Error.
§Cargo features
| Feature | Description |
|---|---|
vendored | Compile librnp + Botan + json-c + zlib + bzip2 from source via the rnp-src crate and statically link the result. |
pqc | Expose PQC algorithm constants and Encryptor::prefer_pqc_enc_subkey. Requires librnp built with ENABLE_PQC=ON. |
crypto-refresh | Expose v6 keys, crypto-refresh algorithms, and v6 PKESK/SKESK. Requires librnp built with ENABLE_CRYPTO_REFRESH=ON. |
logging | Gate Context::set_log_fd / set_log_file. |
§Status
This crate exercises all 293 public functions librnp 0.18.1
declares in rnp.h (4 as documented equivalents; see
docs/PARITY.md
for the per-function mapping and the audit that keeps it true). The
per-phase TODO files in
TODO.roadmap
remain the build history.
§Linking
By default the crate links against a system-installed librnp
(-lrnp). Install via brew install rnp (macOS),
dnf install librnp-devel (Fedora), or build from source. To point
at a non-standard install location set RNP_INCLUDE_DIR and
RNP_LIB_DIR. Use --features vendored to build librnp from source.
Re-exports§
pub use algorithm::Algorithm;pub use algorithm::Cipher;pub use algorithm::Compression;pub use algorithm::Curve;pub use algorithm::Hash;pub use algorithm::KeyUsage;pub use algorithm::PqcAlgorithm;pub use algorithm::librnp_supports_pqc;pub use armor::ContentType;pub use armor::armor_bytes;pub use armor::dearmor;pub use armor::dearmor_bytes;pub use armor::enarmor;pub use armor::guess_contents;pub use callbacks::KeyProvider;pub use callbacks::KeyRequestOutcome;pub use callbacks::PasswordProvider;pub use callbacks::RequestedKeyType;pub use context::Context;pub use context::KeyringFormat;pub use dump::DumpFlags;pub use dump::JsonDumpFlags;pub use dump::JsonFlags;pub use dump::dump_packets_bytes_to_json;pub use dump::dump_packets_to_json;pub use dump::dump_packets_to_output;pub use encrypt::AddPasswordOptions;pub use encrypt::AeadType;pub use encrypt::DecryptResult;pub use encrypt::Decryptor;pub use encrypt::EncryptFlags;pub use encrypt::Encryptor;pub use encrypt::decrypt;pub use encrypt::decrypt_from_input;Deprecated pub use encrypt::decrypt_to;pub use error::Error;pub use error::ErrorKind;pub use error::Result;pub use error::from_rnp_code;pub use error::unknown_variant;pub use key::AddUidOptions;pub use key::ProtectOptions;pub use key::RevocationCode;pub use key::RevocationReason;pub use key::ExportFlags;pub use key::Key;pub use key::KeyIdentifier;pub use key::LoadSaveFlags;pub use key::RemoveFlags;pub use key::RemoveSignaturesFlags;pub use key::UnloadFlags;pub use key_signature_builder::CertificationBuilder;pub use key_signature_builder::ConfiguredBuilder;pub use key_signature_builder::DirectSignatureBuilder;pub use key_signature_builder::RevocationSignatureBuilder;pub use key_signature_builder::SignatureSetterOps;pub use keygen::KeyBuilder;pub use keygen::SubkeyBuilder;pub use keygen::generate_key_25519;pub use keygen::generate_key_dsa_eg;pub use keygen::generate_key_ec;pub use keygen::generate_key_ex;pub use keygen::generate_key_json;pub use keygen::generate_key_rsa;pub use keygen::generate_key_sm2;pub use keyring::IdentifierIterator;pub use keyring::IdentifierKind;pub use ops::ArmorType;pub use ops::Input;pub use ops::MessageSource;pub use ops::Output;pub use ops::OutputFileFlags;pub use ops::WriterOutcome;pub use ops::call_for_optional_string;pub use ops::call_for_string;pub use ops::cstr_to_optional_string;pub use ops::cstr_to_string;pub use secret::SecretString;pub use secret::zero_string_bytes;pub use security::FeatureType;pub use security::SecurityFlags;pub use security::SecurityLevel;pub use security::SecurityRule;pub use security::calculate_iterations;pub use security::request_password;pub use security::supported_features;pub use security::supports_feature;pub use signature::Mode;pub use signature::Signer;pub use signature::generate_revocation_certificate;pub use signature::generate_revocation_certificate_with;pub use signature::sign;pub use signature::sign_cleartext;pub use signature::sign_detached;pub use signature::verify;pub use signature::verify_detached;pub use signature_handle::Signature;pub use signature_handle::SignatureType;pub use signature_handle::Subpacket;pub use signature_handle::SubpacketType;pub use subkey::Subkey;pub use uid::Uid;pub use uid::UidType;pub use verify::FileInfo;pub use verify::Recipient;pub use verify::SignatureStatus;pub use verify::Symenc;pub use verify::VerifyFlags;pub use verify::VerifyOp;pub use verify::VerifyResult;pub use verify::VerifySignature;
Modules§
- algorithm
- Domain primitives: public-key algorithms, curves, hashes, ciphers, compression, key usage.
- armor
- ASCII-armor and dearmor wrappers, plus content-type sniffing.
- callbacks
- Callback installation for
Context. - context
- Top-level librnp FFI handle.
- dump
- Packet dumps and per-object JSON serialization.
- encrypt
- Encryption and decryption.
- error
- Error type for the RNP binding.
- ffi
- Raw FFI bindings to
librnp, re-exported from thernp_syscrate. - ffi_
safe - Centralized safety wrappers around the librnp C FFI.
- key
- OpenPGP key handles.
- key_
signature_ builder - Signature-creation builders: certification, direct, and revocation signatures.
- keygen
- Key generation.
- keyring
- Keyring-level operations on
Context: save, unload, import, homedir discovery, and key counts. - ops
- Operation-level wrappers: shared
Input/OutputRAII handles plus future op-builder types (sign, verify, encrypt, generate). - secret
- Password-hygienic string type.
- security
- Security profile, feature queries, and related utilities.
- signature
- Signing and verification.
- signature_
handle - Signature handle and subpacket types.
- strconv
FromStrandDisplayfor the crate’s model enums.- subkey
- Subkey handle.
- uid
- User-ID handle. Borrows the parent
Keyfor its lifetime. - verify
- Verify operation: the typed surface over
rnp_op_verify_*. - version
- Version helpers.
Functions§
- version_
string - librnp version string, e.g.
"0.18.1".