paseto_v5_aws_lc/lib.rs
1//! PASETO v5 using AWS-LC for ML-DSA-87 signatures and symmetric encryption.
2//!
3//! Enable `fips` to use the AWS-LC FIPS module, or `non-fips` to require the
4//! non-FIPS backend. These features are mutually exclusive. Without either,
5//! AWS-LC uses its default backend.
6//!
7//! New signing keys are generated inside AWS-LC. Secret-key encodings contain
8//! the 32-byte ML-DSA seed; importing a seed preserves its existing provenance.
9//!
10//! Plaintext key encodings follow the
11//! [post-quantum PASERK draft](https://github.com/paseto-standard/paserk/pull/22).
12//! Key IDs, wrapping and sealing are not implemented by this backend.
13//!
14//! # PASERK plaintext keys
15//!
16//! Format a public key with `to_string()`. For local and secret keys,
17//! `expose_key().to_string()` exports the secret material in plaintext.
18//! Parse that text into the corresponding key type. Use [`KeyText`] for raw bytes:
19//!
20//! ```
21//! # #[cfg(feature = "signing")] {
22//! use paseto_v5_aws_lc::{KeyText, SecretKey};
23//! let seed = [7; 32];
24//! let key: SecretKey = KeyText::from_raw_bytes(&seed).try_into()?;
25//! let text = key.expose_key().to_string();
26//! let restored: SecretKey = text.parse()?;
27//! assert_eq!(restored.expose_key().as_raw_bytes(), &seed);
28//! # }
29//! # Ok::<(), paseto_v5_aws_lc::PasetoError>(())
30//! ```
31//!
32//! # Sign and verify a public token
33//!
34//! ```
35//! # #[cfg(feature = "signing")] {
36//! use paseto_v5_aws_lc::{SecretKey, SignedToken, UnsignedToken};
37//! use paseto_json::{ForAudience, HasExpiry, RegisteredClaims, Time, Validate};
38//! use std::time::Duration;
39//!
40//! let key = SecretKey::random()?;
41//! let claims = RegisteredClaims::now(Duration::from_secs(300)).for_audience("inbox".into());
42//! let token = UnsignedToken::new(claims).sign(&key)?.to_string();
43//!
44//! let received: SignedToken<RegisteredClaims> = token.parse()?;
45//! let policy = Time::valid_now().and_then(HasExpiry).and_then(ForAudience("inbox"));
46//! let accepted = received.verify(&key.public_key(), &policy)?;
47//! assert_eq!(accepted.claims.aud.as_deref(), Some("inbox"));
48//! # }
49//! # Ok::<(), paseto_v5_aws_lc::PasetoError>(())
50//! ```
51//!
52//! # Encrypt and decrypt a local token
53//!
54//! ```
55//! # #[cfg(feature = "encrypting")] {
56//! use paseto_v5_aws_lc::{EncryptedToken, LocalKey, UnencryptedToken};
57//! use paseto_json::{ForAudience, HasExpiry, RegisteredClaims, Time, Validate};
58//! use std::time::Duration;
59//!
60//! let key = LocalKey::random()?;
61//! let claims = RegisteredClaims::now(Duration::from_secs(300)).for_audience("inbox".into());
62//! let token = UnencryptedToken::new(claims).encrypt(&key)?.to_string();
63//!
64//! let received: EncryptedToken<RegisteredClaims> = token.parse()?;
65//! let policy = Time::valid_now().and_then(HasExpiry).and_then(ForAudience("inbox"));
66//! let accepted = received.decrypt(&key, &policy)?;
67//! assert_eq!(accepted.claims.aud.as_deref(), Some("inbox"));
68//! # }
69//! # Ok::<(), paseto_v5_aws_lc::PasetoError>(())
70//! ```
71#![forbid(unsafe_code)]
72
73#[cfg(all(feature = "decrypting", feature = "zeroize"))]
74macro_rules! secret {
75 ($value:expr) => {
76 zeroize::Zeroizing::new($value)
77 };
78}
79#[cfg(all(feature = "decrypting", not(feature = "zeroize")))]
80macro_rules! secret {
81 ($value:expr) => {
82 $value
83 };
84}
85
86/// Low level implementation primitives.
87pub mod core;
88
89pub use paseto_core::PasetoError;
90
91/// A token with publicly readable data, but not yet verified.
92#[cfg(feature = "verifying")]
93pub type SignedToken<M, F = ()> = paseto_core::SignedToken<core::V5, M, F>;
94/// A token with secret data.
95#[cfg(feature = "decrypting")]
96pub type EncryptedToken<M, F = ()> = paseto_core::EncryptedToken<core::V5, M, F>;
97/// A [`SignedToken`] that has been verified.
98#[cfg(feature = "verifying")]
99pub type UnsignedToken<M, F = ()> = paseto_core::UnsignedToken<core::V5, M, F>;
100/// An [`EncryptedToken`] that has been decrypted.
101#[cfg(feature = "decrypting")]
102pub type UnencryptedToken<M, F = ()> = paseto_core::UnencryptedToken<core::V5, M, F>;
103
104/// Private key used for token encryption and decryption.
105#[cfg(feature = "decrypting")]
106pub type LocalKey = paseto_core::LocalKey<core::V5>;
107/// Public key used for signature verification.
108#[cfg(feature = "verifying")]
109pub type PublicKey = paseto_core::PublicKey<core::V5>;
110/// Private key used for token signing.
111#[cfg(feature = "signing")]
112pub type SecretKey = paseto_core::SecretKey<core::V5>;
113
114/// A plaintext encoding of a key.
115pub type KeyText<K> = paseto_core::paserk::KeyText<core::V5, K>;
116
117#[cfg(all(test, feature = "fips"))]
118#[test]
119fn fips_backend_is_enabled() {
120 aws_lc_rs::try_fips_mode().expect("the fips feature must select the FIPS module");
121}
122
123#[cfg(all(test, feature = "non-fips"))]
124#[test]
125fn non_fips_backend_is_enabled() {
126 assert!(aws_lc_rs::try_fips_mode().is_err());
127}