Skip to main content

sew_mls_crypto/
lib.rs

1//! Sew's OpenMLS crypto provider.
2//!
3//! [`MlsCrypto`] implements `openmls_traits`'
4//! [`OpenMlsCrypto`](openmls_traits::crypto::OpenMlsCrypto) and
5//! [`OpenMlsRand`](openmls_traits::random::OpenMlsRand) over RustCrypto crates, and does HPKE (RFC
6//! 9180, base mode) through the `hpke` crate (MIT OR Apache-2.0). It replaces
7//! `openmls_rust_crypto`'s `RustCrypto`, whose HPKE is the MPL-2.0 `hpke-rs`; [`MlsProvider`]
8//! replaces `OpenMlsRustCrypto` (crypto plus an in-memory `MemoryStorage`).
9//!
10//! The behaviour is `openmls_rust_crypto` 0.6.0's for every ciphersuite it advertises, which
11//! are the three this crate supports:
12//!
13//! - `MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519` (0x0001, the one Sew uses),
14//! - `MLS_128_DHKEMP256_AES128GCM_SHA256_P256` (0x0002),
15//! - `MLS_128_DHKEMX25519_CHACHA20POLY1305_SHA256_Ed25519` (0x0003).
16//!
17//! The same key formats (RFC 9180 `SerializePrivateKey`/`SerializePublicKey`, Ed25519 32-byte
18//! seeds, P-256 32-byte scalars, SEC1 uncompressed public keys, DER ECDSA signatures), the same
19//! errors, and the same RNG: one `ChaCha20Rng` per provider, seeded from the OS, behind a lock.
20//! HPKE's ephemeral keys come from that RNG too. One difference: `derive_hpke_keypair` returns
21//! X25519 private keys clamped (RFC 9180 ยง7.1.2), where `openmls_rust_crypto` returns them
22//! unclamped; the public key is the same and either form opens the same ciphertexts (see
23//! CHANGELOG). The post-quantum draft suites and the
24//! `targeted-messages-draft` / `virtual-clients-draft` methods are not built, and P-384
25//! signatures, which no supported suite uses, are refused. Those two openmls features add
26//! required methods to `OpenMlsCrypto` (through `openmls_traits`' features of the same names),
27//! so a build that turns either on does not compile with this crate: leave them off, or use a
28//! provider that implements those methods.
29//!
30//! [`SignatureKeyPair`] is the signature key pair of a basic credential, the drop-in for
31//! `openmls_basic_credential`'s (same key, signature, serde, TLS and storage formats; Ed25519
32//! and ECDSA P-256 only).
33//!
34//! Parts of this crate are derived from `openmls_rust_crypto` 0.6.0 and
35//! `openmls_basic_credential` 0.6.0 (Copyright (c) 2020 OpenMLS Authors, MIT); see NOTICE.
36
37mod credential;
38mod crypto;
39mod hpke_impl;
40mod rng;
41
42pub use credential::{SignatureKeyPair, StorageId};
43pub use crypto::{MlsCrypto, RandError};
44pub use openmls_memory_storage::{MemoryStorage, MemoryStorageError};
45
46use openmls_traits::OpenMlsProvider;
47
48/// An OpenMLS provider: [`MlsCrypto`] for crypto and randomness, `MemoryStorage` for storage.
49/// The drop-in for `openmls_rust_crypto::OpenMlsRustCrypto`.
50#[derive(Default, Debug)]
51pub struct MlsProvider {
52    crypto: MlsCrypto,
53    storage: MemoryStorage,
54}
55
56impl MlsProvider {
57    /// A provider with a fresh RNG and an empty store.
58    pub fn new() -> MlsProvider {
59        MlsProvider::default()
60    }
61}
62
63impl OpenMlsProvider for MlsProvider {
64    type CryptoProvider = MlsCrypto;
65    type RandProvider = MlsCrypto;
66    type StorageProvider = MemoryStorage;
67
68    fn storage(&self) -> &Self::StorageProvider {
69        &self.storage
70    }
71
72    fn crypto(&self) -> &Self::CryptoProvider {
73        &self.crypto
74    }
75
76    fn rand(&self) -> &Self::RandProvider {
77        &self.crypto
78    }
79}
80
81/// README.md's examples, compiled and run as doc tests.
82#[cfg(doctest)]
83#[doc = include_str!("../README.md")]
84struct ReadmeExamples;