| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// Copyright by contributors to this project. |
| 3 |
// SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 4 |
|
| 5 |
#![cfg_attr(not(feature = "std"), no_std)] |
| 6 |
extern crate alloc; |
| 7 |
|
| 8 |
pub mod aead; |
| 9 |
mod ec; |
| 10 |
pub mod ec_signer; |
| 11 |
pub mod ecdh; |
| 12 |
pub mod kdf; |
| 13 |
pub mod mac; |
| 14 |
|
| 15 |
use crate::aead::Aead; |
| 16 |
use ec_signer::{EcSigner, EcSignerError}; |
| 17 |
use ecdh::Ecdh; |
| 18 |
use kdf::Kdf; |
| 19 |
use mac::{Hash, HashError}; |
| 20 |
use mls_rs_crypto_hpke::{ |
| 21 |
context::{ContextR, ContextS}, |
| 22 |
dhkem::DhKem, |
| 23 |
hpke::{Hpke, HpkeError}, |
| 24 |
}; |
| 25 |
use mls_rs_crypto_traits::{AeadType, KdfType, KemId, KemType}; |
| 26 |
use rand_core::{OsRng, RngCore}; |
| 27 |
|
| 28 |
use mls_rs_core::{ |
| 29 |
crypto::{ |
| 30 |
CipherSuite, CipherSuiteProvider, CryptoProvider, HpkeCiphertext, HpkePublicKey, |
| 31 |
HpkeSecretKey, SignaturePublicKey, SignatureSecretKey, |
| 32 |
}, |
| 33 |
error::{AnyError, IntoAnyError}, |
| 34 |
}; |
| 35 |
use zeroize::Zeroizing; |
| 36 |
|
| 37 |
use alloc::vec; |
| 38 |
use alloc::vec::Vec; |
| 39 |
|
| 40 |
#[derive(Debug)] |
| 41 |
#[cfg_attr(feature = "std", derive(thiserror::Error))] |
| 42 |
pub enum NssCryptoError { |
| 43 |
#[cfg_attr(feature = "std", error(transparent))] |
| 44 |
AeadError(AnyError), |
| 45 |
#[cfg_attr(feature = "std", error(transparent))] |
| 46 |
HpkeError(HpkeError), |
| 47 |
#[cfg_attr(feature = "std", error(transparent))] |
| 48 |
KdfError(AnyError), |
| 49 |
#[cfg_attr(feature = "std", error(transparent))] |
| 50 |
HashError(HashError), |
| 51 |
#[cfg_attr(feature = "std", error("rand core error: {0:?}"))] |
| 52 |
RandError(rand_core::Error), |
| 53 |
#[cfg_attr(feature = "std", error(transparent))] |
| 54 |
EcSignerError(EcSignerError), |
| 55 |
} |
| 56 |
|
| 57 |
impl From<rand_core::Error> for NssCryptoError { |
| 58 |
fn from(value: rand_core::Error) -> Self { |
| 59 |
NssCryptoError::RandError(value) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl From<HpkeError> for NssCryptoError { |
| 64 |
fn from(e: HpkeError) -> Self { |
| 65 |
NssCryptoError::HpkeError(e) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
impl From<HashError> for NssCryptoError { |
| 70 |
fn from(e: HashError) -> Self { |
| 71 |
NssCryptoError::HashError(e) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
impl From<EcSignerError> for NssCryptoError { |
| 76 |
fn from(e: EcSignerError) -> Self { |
| 77 |
NssCryptoError::EcSignerError(e) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl IntoAnyError for NssCryptoError { |
| 82 |
#[cfg(feature = "std")] |
| 83 |
fn into_dyn_error(self) -> Result<Box<dyn std::error::Error + Send + Sync>, Self> { |
| 84 |
Ok(self.into()) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[derive(Debug, Clone)] |
| 89 |
#[non_exhaustive] |
| 90 |
pub struct NssCryptoProvider { |
| 91 |
pub enabled_cipher_suites: Vec<CipherSuite>, |
| 92 |
} |
| 93 |
|
| 94 |
impl NssCryptoProvider { |
| 95 |
pub fn new() -> Self { |
| 96 |
Self::default() |
| 97 |
} |
| 98 |
|
| 99 |
pub fn with_enabled_cipher_suites(enabled_cipher_suites: Vec<CipherSuite>) -> Self { |
| 100 |
Self { |
| 101 |
enabled_cipher_suites, |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
pub fn all_supported_cipher_suites() -> Vec<CipherSuite> { |
| 106 |
vec![ |
| 107 |
CipherSuite::P256_AES128, |
| 108 |
CipherSuite::CURVE25519_AES128, |
| 109 |
CipherSuite::CURVE25519_CHACHA, |
| 110 |
] |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
impl Default for NssCryptoProvider { |
| 115 |
fn default() -> Self { |
| 116 |
Self { |
| 117 |
enabled_cipher_suites: Self::all_supported_cipher_suites(), |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
impl CryptoProvider for NssCryptoProvider { |
| 123 |
type CipherSuiteProvider = NssCryptoCipherSuite<DhKem<Ecdh, Kdf>, Kdf, Aead>; |
| 124 |
|
| 125 |
fn supported_cipher_suites(&self) -> Vec<CipherSuite> { |
| 126 |
self.enabled_cipher_suites.clone() |
| 127 |
} |
| 128 |
|
| 129 |
fn cipher_suite_provider( |
| 130 |
&self, |
| 131 |
cipher_suite: CipherSuite, |
| 132 |
) -> Option<Self::CipherSuiteProvider> { |
| 133 |
if !self.enabled_cipher_suites.contains(&cipher_suite) { |
| 134 |
return None; |
| 135 |
} |
| 136 |
|
| 137 |
let kdf = Kdf::new(cipher_suite)?; |
| 138 |
let ecdh = Ecdh::new(cipher_suite)?; |
| 139 |
let kem_id = KemId::new(cipher_suite)?; |
| 140 |
let kem = DhKem::new(ecdh, kdf, kem_id as u16, kem_id.n_secret()); |
| 141 |
let aead = Aead::new(cipher_suite)?; |
| 142 |
|
| 143 |
NssCryptoCipherSuite::new(cipher_suite, kem, kdf, aead) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
#[derive(Clone)] |
| 148 |
pub struct NssCryptoCipherSuite<KEM, KDF, AEAD> |
| 149 |
where |
| 150 |
KEM: KemType + Clone, |
| 151 |
KDF: KdfType + Clone, |
| 152 |
AEAD: AeadType + Clone, |
| 153 |
{ |
| 154 |
cipher_suite: CipherSuite, |
| 155 |
aead: AEAD, |
| 156 |
kdf: KDF, |
| 157 |
hash: Hash, |
| 158 |
hpke: Hpke<KEM, KDF, AEAD>, |
| 159 |
ec_signer: EcSigner, |
| 160 |
} |
| 161 |
|
| 162 |
impl<KEM, KDF, AEAD> NssCryptoCipherSuite<KEM, KDF, AEAD> |
| 163 |
where |
| 164 |
KEM: KemType + Clone, |
| 165 |
KDF: KdfType + Clone, |
| 166 |
AEAD: AeadType + Clone, |
| 167 |
{ |
| 168 |
pub fn new(cipher_suite: CipherSuite, kem: KEM, kdf: KDF, aead: AEAD) -> Option<Self> { |
| 169 |
let hpke = Hpke::new(kem, kdf.clone(), Some(aead.clone())); |
| 170 |
|
| 171 |
Some(Self { |
| 172 |
cipher_suite, |
| 173 |
kdf, |
| 174 |
aead, |
| 175 |
hash: Hash::new(cipher_suite).ok()?, |
| 176 |
hpke, |
| 177 |
ec_signer: EcSigner::new(cipher_suite)?, |
| 178 |
}) |
| 179 |
} |
| 180 |
|
| 181 |
pub fn random_bytes(&self, out: &mut [u8]) -> Result<(), NssCryptoError> { |
| 182 |
OsRng.try_fill_bytes(out).map_err(Into::into) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 187 |
#[cfg_attr(all(target_arch = "wasm32", mls_build_async), maybe_async::must_be_async(?Send))] |
| 188 |
#[cfg_attr( |
| 189 |
all(not(target_arch = "wasm32"), mls_build_async), |
| 190 |
maybe_async::must_be_async |
| 191 |
)] |
| 192 |
impl<KEM, KDF, AEAD> CipherSuiteProvider for NssCryptoCipherSuite<KEM, KDF, AEAD> |
| 193 |
where |
| 194 |
KEM: KemType + Clone + Send + Sync, |
| 195 |
KDF: KdfType + Clone + Send + Sync, |
| 196 |
AEAD: AeadType + Clone + Send + Sync, |
| 197 |
{ |
| 198 |
type Error = NssCryptoError; |
| 199 |
// TODO exporter_secret in this struct is not zeroized |
| 200 |
type HpkeContextR = ContextR<KDF, AEAD>; |
| 201 |
type HpkeContextS = ContextS<KDF, AEAD>; |
| 202 |
|
| 203 |
async fn hash(&self, data: &[u8]) -> Result<Vec<u8>, Self::Error> { |
| 204 |
Ok(self.hash.hash(data)) |
| 205 |
} |
| 206 |
|
| 207 |
async fn mac(&self, key: &[u8], data: &[u8]) -> Result<Vec<u8>, Self::Error> { |
| 208 |
Ok(self.hash.mac(key, data)?) |
| 209 |
} |
| 210 |
|
| 211 |
async fn aead_seal( |
| 212 |
&self, |
| 213 |
key: &[u8], |
| 214 |
data: &[u8], |
| 215 |
aad: Option<&[u8]>, |
| 216 |
nonce: &[u8], |
| 217 |
) -> Result<Vec<u8>, Self::Error> { |
| 218 |
self.aead |
| 219 |
.seal(key, data, aad, nonce) |
| 220 |
.await |
| 221 |
.map_err(|e| NssCryptoError::AeadError(e.into_any_error())) |
| 222 |
} |
| 223 |
|
| 224 |
async fn aead_open( |
| 225 |
&self, |
| 226 |
key: &[u8], |
| 227 |
cipher_text: &[u8], |
| 228 |
aad: Option<&[u8]>, |
| 229 |
nonce: &[u8], |
| 230 |
) -> Result<Zeroizing<Vec<u8>>, Self::Error> { |
| 231 |
self.aead |
| 232 |
.open(key, cipher_text, aad, nonce) |
| 233 |
.await |
| 234 |
.map_err(|e| NssCryptoError::AeadError(e.into_any_error())) |
| 235 |
.map(Zeroizing::new) |
| 236 |
} |
| 237 |
|
| 238 |
fn aead_key_size(&self) -> usize { |
| 239 |
self.aead.key_size() |
| 240 |
} |
| 241 |
|
| 242 |
fn aead_nonce_size(&self) -> usize { |
| 243 |
self.aead.nonce_size() |
| 244 |
} |
| 245 |
|
| 246 |
async fn kdf_expand( |
| 247 |
&self, |
| 248 |
prk: &[u8], |
| 249 |
info: &[u8], |
| 250 |
len: usize, |
| 251 |
) -> Result<Zeroizing<Vec<u8>>, Self::Error> { |
| 252 |
self.kdf |
| 253 |
.expand(prk, info, len) |
| 254 |
.await |
| 255 |
.map_err(|e| NssCryptoError::KdfError(e.into_any_error())) |
| 256 |
.map(Zeroizing::new) |
| 257 |
} |
| 258 |
|
| 259 |
async fn kdf_extract( |
| 260 |
&self, |
| 261 |
salt: &[u8], |
| 262 |
ikm: &[u8], |
| 263 |
) -> Result<Zeroizing<Vec<u8>>, Self::Error> { |
| 264 |
self.kdf |
| 265 |
.extract(salt, ikm) |
| 266 |
.await |
| 267 |
.map_err(|e| NssCryptoError::KdfError(e.into_any_error())) |
| 268 |
.map(Zeroizing::new) |
| 269 |
} |
| 270 |
|
| 271 |
fn kdf_extract_size(&self) -> usize { |
| 272 |
self.kdf.extract_size() |
| 273 |
} |
| 274 |
|
| 275 |
async fn hpke_seal( |
| 276 |
&self, |
| 277 |
remote_key: &HpkePublicKey, |
| 278 |
info: &[u8], |
| 279 |
aad: Option<&[u8]>, |
| 280 |
pt: &[u8], |
| 281 |
) -> Result<HpkeCiphertext, Self::Error> { |
| 282 |
Ok(self.hpke.seal(remote_key, info, None, aad, pt).await?) |
| 283 |
} |
| 284 |
|
| 285 |
async fn hpke_open( |
| 286 |
&self, |
| 287 |
ciphertext: &HpkeCiphertext, |
| 288 |
local_secret: &HpkeSecretKey, |
| 289 |
local_public: &HpkePublicKey, |
| 290 |
info: &[u8], |
| 291 |
aad: Option<&[u8]>, |
| 292 |
) -> Result<Vec<u8>, Self::Error> { |
| 293 |
Ok(self |
| 294 |
.hpke |
| 295 |
.open(ciphertext, local_secret, local_public, info, None, aad) |
| 296 |
.await?) |
| 297 |
} |
| 298 |
|
| 299 |
async fn hpke_setup_r( |
| 300 |
&self, |
| 301 |
enc: &[u8], |
| 302 |
local_secret: &HpkeSecretKey, |
| 303 |
local_public: &HpkePublicKey, |
| 304 |
info: &[u8], |
| 305 |
) -> Result<Self::HpkeContextR, Self::Error> { |
| 306 |
Ok(self |
| 307 |
.hpke |
| 308 |
.setup_receiver(enc, local_secret, local_public, info, None) |
| 309 |
.await?) |
| 310 |
} |
| 311 |
|
| 312 |
async fn hpke_setup_s( |
| 313 |
&self, |
| 314 |
remote_key: &HpkePublicKey, |
| 315 |
info: &[u8], |
| 316 |
) -> Result<(Vec<u8>, Self::HpkeContextS), Self::Error> { |
| 317 |
Ok(self.hpke.setup_sender(remote_key, info, None).await?) |
| 318 |
} |
| 319 |
|
| 320 |
async fn kem_derive(&self, ikm: &[u8]) -> Result<(HpkeSecretKey, HpkePublicKey), Self::Error> { |
| 321 |
Ok(self.hpke.derive(ikm).await?) |
| 322 |
} |
| 323 |
|
| 324 |
async fn kem_generate(&self) -> Result<(HpkeSecretKey, HpkePublicKey), Self::Error> { |
| 325 |
Ok(self.hpke.generate().await?) |
| 326 |
} |
| 327 |
|
| 328 |
fn kem_public_key_validate(&self, key: &HpkePublicKey) -> Result<(), Self::Error> { |
| 329 |
Ok(self.hpke.public_key_validate(key)?) |
| 330 |
} |
| 331 |
|
| 332 |
fn random_bytes(&self, out: &mut [u8]) -> Result<(), Self::Error> { |
| 333 |
self.random_bytes(out) |
| 334 |
} |
| 335 |
|
| 336 |
fn cipher_suite(&self) -> CipherSuite { |
| 337 |
self.cipher_suite |
| 338 |
} |
| 339 |
|
| 340 |
async fn sign( |
| 341 |
&self, |
| 342 |
secret_key: &SignatureSecretKey, |
| 343 |
data: &[u8], |
| 344 |
) -> Result<Vec<u8>, Self::Error> { |
| 345 |
Ok(self.ec_signer.sign(secret_key, data)?) |
| 346 |
} |
| 347 |
|
| 348 |
async fn verify( |
| 349 |
&self, |
| 350 |
public_key: &SignaturePublicKey, |
| 351 |
signature: &[u8], |
| 352 |
data: &[u8], |
| 353 |
) -> Result<(), Self::Error> { |
| 354 |
Ok(self.ec_signer.verify(public_key, signature, data)?) |
| 355 |
} |
| 356 |
|
| 357 |
async fn signature_key_generate( |
| 358 |
&self, |
| 359 |
) -> Result<(SignatureSecretKey, SignaturePublicKey), Self::Error> { |
| 360 |
Ok(self.ec_signer.signature_key_generate()?) |
| 361 |
} |
| 362 |
|
| 363 |
async fn signature_key_derive_public( |
| 364 |
&self, |
| 365 |
secret_key: &SignatureSecretKey, |
| 366 |
) -> Result<SignaturePublicKey, Self::Error> { |
| 367 |
Ok(self.ec_signer.signature_key_derive_public(secret_key)?) |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
#[cfg(not(mls_build_async))] |
| 372 |
#[test] |
| 373 |
fn mls_core_tests() { |
| 374 |
let provider = NssCryptoProvider::new(); |
| 375 |
mls_rs_core::crypto::test_suite::verify_tests(&provider, true); |
| 376 |
|
| 377 |
for cs in NssCryptoProvider::all_supported_cipher_suites() { |
| 378 |
let mut hpke = provider.cipher_suite_provider(cs).unwrap().hpke; |
| 379 |
|
| 380 |
mls_rs_core::crypto::test_suite::verify_hpke_context_tests(&hpke, cs); |
| 381 |
mls_rs_core::crypto::test_suite::verify_hpke_encap_tests(&mut hpke, cs); |
| 382 |
} |
| 383 |
} |