jwt_simple/algorithms/jwe/mod.rs
1//! JWE (JSON Web Encryption) key management algorithms.
2//!
3//! This module provides implementations of various JWE key management algorithms
4//! as specified in RFC 7518. Each key type is strongly typed to prevent misuse.
5//!
6//! # Supported Algorithms
7//!
8//! ## RSA Key Management
9//! - `RSA-OAEP` - RSA with OAEP using SHA-1
10//!
11//! Note: RSA-OAEP-256 (with SHA-256) is not currently supported because the underlying
12//! boring/superboring crates do not expose the API to specify the OAEP hash function.
13//!
14//! ## Symmetric Key Wrap
15//! - `A256KW` - AES-256 Key Wrap (recommended)
16//! - `A128KW` - AES-128 Key Wrap
17//!
18//! ## ECDH Key Agreement
19//! - `ECDH-ES+A256KW` - ECDH with AES-256 Key Wrap (recommended)
20//! - `ECDH-ES+A128KW` - ECDH with AES-128 Key Wrap
21//!
22//! # Content Encryption
23//!
24//! All key management algorithms support these content encryption algorithms:
25//! - `A256GCM` - AES-256-GCM (default, recommended)
26//! - `A128GCM` - AES-128-GCM
27//!
28//! # Sender Authentication
29//!
30//! Authenticated encryption authenticates the ciphertext, not the sender.
31//! With the asymmetric modes (`RSA-OAEP` and `ECDH-ES`), the encryption key is public,
32//! so anyone can produce a token that decrypts successfully, carrying any claims.
33//! Successful decryption in these modes proves nothing about who sent the token,
34//! and checking issuer or audience claims is no substitute, since the sender picked them too.
35//!
36//! The AES key wrap modes (`A128KW`, `A256KW`) are different: producing a decryptable
37//! token requires the shared secret, so decryption authenticates the sender as one of
38//! the key holders.
39//!
40//! When the sender's identity matters with the asymmetric modes, verify a signature
41//! after decryption, for example a signed token carried in a custom claim.
42//!
43//! # Examples
44//!
45//! ## RSA-OAEP
46//!
47//! ```rust
48//! use jwt_simple::prelude::*;
49//!
50//! // Generate a key pair
51//! let decryption_key = RsaOaepDecryptionKey::generate(2048).unwrap();
52//! let encryption_key = decryption_key.encryption_key();
53//!
54//! // Encrypt
55//! let claims = Claims::create(Duration::from_hours(1))
56//! .with_subject("user@example.com");
57//! let token = encryption_key.encrypt(claims).unwrap();
58//!
59//! // Decrypt
60//! let claims = decryption_key.decrypt_token::<NoCustomClaims>(&token, None).unwrap();
61//! ```
62//!
63//! ## AES Key Wrap
64//!
65//! ```rust
66//! use jwt_simple::prelude::*;
67//!
68//! // Generate a symmetric key
69//! let key = A256KWKey::generate();
70//!
71//! // Encrypt
72//! let claims = Claims::create(Duration::from_hours(1));
73//! let token = key.encrypt(claims).unwrap();
74//!
75//! // Decrypt
76//! let claims = key.decrypt_token::<NoCustomClaims>(&token, None).unwrap();
77//! ```
78//!
79//! ## ECDH-ES+A256KW
80//!
81//! ```rust
82//! use jwt_simple::prelude::*;
83//!
84//! // Generate a key pair
85//! let decryption_key = EcdhEsA256KWDecryptionKey::generate();
86//! let encryption_key = decryption_key.encryption_key();
87//!
88//! // Encrypt
89//! let claims = Claims::create(Duration::from_hours(1));
90//! let token = encryption_key.encrypt(claims).unwrap();
91//!
92//! // Decrypt
93//! let claims = decryption_key.decrypt_token::<NoCustomClaims>(&token, None).unwrap();
94//! ```
95
96pub mod aes_kw;
97pub mod content;
98pub mod ecdh_es;
99pub mod rsa_oaep;
100
101pub use aes_kw::{A128KWKey, A256KWKey};
102pub use content::ContentEncryption;
103pub use ecdh_es::{
104 EcdhEsA128KWDecryptionKey, EcdhEsA128KWEncryptionKey, EcdhEsA256KWDecryptionKey,
105 EcdhEsA256KWEncryptionKey,
106};
107pub use rsa_oaep::{RsaOaepDecryptionKey, RsaOaepEncryptionKey};