| 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 |
use core::fmt::Debug; |
| 6 |
|
| 7 |
use mls_rs_core::{crypto::CipherSuite, error::IntoAnyError}; |
| 8 |
use mls_rs_crypto_traits::{AeadId, AeadType, AES_TAG_LEN}; |
| 9 |
|
| 10 |
use alloc::vec::Vec; |
| 11 |
|
| 12 |
#[derive(Debug)] |
| 13 |
#[cfg_attr(feature = "std", derive(thiserror::Error))] |
| 14 |
pub enum AeadError { |
| 15 |
#[cfg_attr(feature = "std", error("NSS Error"))] |
| 16 |
NssError(nss_gk_api::Error), |
| 17 |
#[cfg_attr( |
| 18 |
feature = "std", |
| 19 |
error("AEAD ciphertext of length {0} is too short to fit the tag") |
| 20 |
)] |
| 21 |
InvalidCipherLen(usize), |
| 22 |
#[cfg_attr(feature = "std", error("encrypted message cannot be empty"))] |
| 23 |
EmptyPlaintext, |
| 24 |
#[cfg_attr( |
| 25 |
feature = "std", |
| 26 |
error("AEAD key of invalid length {0}. Expected length {1}") |
| 27 |
)] |
| 28 |
InvalidKeyLen(usize, usize), |
| 29 |
#[cfg_attr(feature = "std", error("unsupported cipher suite"))] |
| 30 |
UnsupportedCipherSuite, |
| 31 |
} |
| 32 |
|
| 33 |
impl From<nss_gk_api::Error> for AeadError { |
| 34 |
fn from(value: nss_gk_api::Error) -> Self { |
| 35 |
AeadError::NssError(value) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
impl IntoAnyError for AeadError { |
| 40 |
#[cfg(feature = "std")] |
| 41 |
fn into_dyn_error(self) -> Result<Box<dyn std::error::Error + Send + Sync>, Self> { |
| 42 |
Ok(self.into()) |
| 43 |
} |
| 44 |
} |
| 45 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 46 |
pub struct Aead(AeadId); |
| 47 |
|
| 48 |
impl Aead { |
| 49 |
pub fn new(cipher_suite: CipherSuite) -> Option<Self> { |
| 50 |
AeadId::new(cipher_suite).map(Self) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 55 |
#[cfg_attr(all(target_arch = "wasm32", mls_build_async), maybe_async::must_be_async(?Send))] |
| 56 |
#[cfg_attr( |
| 57 |
all(not(target_arch = "wasm32"), mls_build_async), |
| 58 |
maybe_async::must_be_async |
| 59 |
)] |
| 60 |
impl AeadType for Aead { |
| 61 |
type Error = AeadError; |
| 62 |
|
| 63 |
#[allow(clippy::needless_lifetimes)] |
| 64 |
async fn seal<'a>( |
| 65 |
&self, |
| 66 |
key: &[u8], |
| 67 |
data: &[u8], |
| 68 |
aad: Option<&'a [u8]>, |
| 69 |
nonce: &[u8], |
| 70 |
) -> Result<Vec<u8>, AeadError> { |
| 71 |
nss_gk_api::init(); |
| 72 |
let mode = nss_gk_api::aead::Mode::Encrypt; |
| 73 |
|
| 74 |
(!data.is_empty()) |
| 75 |
.then_some(()) |
| 76 |
.ok_or(AeadError::EmptyPlaintext)?; |
| 77 |
|
| 78 |
(key.len() == self.key_size()) |
| 79 |
.then_some(()) |
| 80 |
.ok_or_else(|| AeadError::InvalidKeyLen(key.len(), self.key_size()))?; |
| 81 |
|
| 82 |
match self.0 { |
| 83 |
AeadId::Aes128Gcm => { |
| 84 |
let alg = nss_gk_api::aead::AeadAlgorithms::Aes128Gcm; |
| 85 |
let key = nss_gk_api::aead::Aead::import_key(alg, key)?; |
| 86 |
let nonce_array: [u8; 12] = |
| 87 |
nonce.try_into().expect("Nonce must be exactly 12 bytes"); |
| 88 |
let aad_array = aad.unwrap_or(&[0; 0]); |
| 89 |
|
| 90 |
let mut cipher = nss_gk_api::aead::Aead::new(mode, alg, &key, nonce_array)?; |
| 91 |
|
| 92 |
let ciphertext = cipher |
| 93 |
.seal(aad_array, data) |
| 94 |
.map_err(|_| AeadError::NssError(nss_gk_api::Error::AeadError))?; |
| 95 |
|
| 96 |
Ok(ciphertext) |
| 97 |
} |
| 98 |
AeadId::Aes256Gcm => { |
| 99 |
let alg = nss_gk_api::aead::AeadAlgorithms::Aes256Gcm; |
| 100 |
let key = nss_gk_api::aead::Aead::import_key(alg, key)?; |
| 101 |
let nonce_array: [u8; 12] = |
| 102 |
nonce.try_into().expect("Nonce must be exactly 12 bytes"); |
| 103 |
let aad_array = aad.unwrap_or(&[0; 0]); |
| 104 |
|
| 105 |
let mut cipher = nss_gk_api::aead::Aead::new(mode, alg, &key, nonce_array)?; |
| 106 |
|
| 107 |
let ciphertext = cipher |
| 108 |
.seal(aad_array, data) |
| 109 |
.map_err(|_| AeadError::NssError(nss_gk_api::Error::AeadError))?; |
| 110 |
|
| 111 |
Ok(ciphertext) |
| 112 |
} |
| 113 |
AeadId::Chacha20Poly1305 => { |
| 114 |
let alg = nss_gk_api::aead::AeadAlgorithms::ChaCha20Poly1305; |
| 115 |
let key = nss_gk_api::aead::Aead::import_key(alg, key)?; |
| 116 |
let nonce_array: [u8; 12] = |
| 117 |
nonce.try_into().expect("Nonce must be exactly 12 bytes"); |
| 118 |
let aad_array = aad.unwrap_or(&[0; 0]); |
| 119 |
|
| 120 |
let mut cipher = nss_gk_api::aead::Aead::new(mode, alg, &key, nonce_array)?; |
| 121 |
|
| 122 |
let ciphertext = cipher |
| 123 |
.seal(aad_array, data) |
| 124 |
.map_err(|_| AeadError::NssError(nss_gk_api::Error::AeadError))?; |
| 125 |
|
| 126 |
Ok(ciphertext) |
| 127 |
} |
| 128 |
_ => Err(AeadError::UnsupportedCipherSuite), |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
#[allow(clippy::needless_lifetimes)] |
| 133 |
async fn open<'a>( |
| 134 |
&self, |
| 135 |
key: &[u8], |
| 136 |
ciphertext: &[u8], |
| 137 |
aad: Option<&'a [u8]>, |
| 138 |
nonce: &[u8], |
| 139 |
) -> Result<Vec<u8>, AeadError> { |
| 140 |
nss_gk_api::init(); |
| 141 |
let mode = nss_gk_api::aead::Mode::Decrypt; |
| 142 |
|
| 143 |
(ciphertext.len() > AES_TAG_LEN) |
| 144 |
.then_some(()) |
| 145 |
.ok_or(AeadError::InvalidCipherLen(ciphertext.len()))?; |
| 146 |
|
| 147 |
(key.len() == self.key_size()) |
| 148 |
.then_some(()) |
| 149 |
.ok_or_else(|| AeadError::InvalidKeyLen(key.len(), self.key_size()))?; |
| 150 |
|
| 151 |
match self.0 { |
| 152 |
AeadId::Aes128Gcm => { |
| 153 |
let alg = nss_gk_api::aead::AeadAlgorithms::Aes128Gcm; |
| 154 |
let key = nss_gk_api::aead::Aead::import_key(alg, key)?; |
| 155 |
let nonce_array: [u8; 12] = |
| 156 |
nonce.try_into().expect("Nonce must be exactly 12 bytes"); |
| 157 |
let aad_array = aad.unwrap_or(&[0; 0]); |
| 158 |
|
| 159 |
let mut cipher = nss_gk_api::aead::Aead::new(mode, alg, &key, nonce_array)?; |
| 160 |
|
| 161 |
let plaintext = cipher |
| 162 |
.open(aad_array, 0, ciphertext) |
| 163 |
.map_err(|_| AeadError::NssError(nss_gk_api::Error::AeadError))?; |
| 164 |
|
| 165 |
Ok(plaintext) |
| 166 |
} |
| 167 |
AeadId::Aes256Gcm => { |
| 168 |
let alg = nss_gk_api::aead::AeadAlgorithms::Aes256Gcm; |
| 169 |
let key = nss_gk_api::aead::Aead::import_key(alg, key)?; |
| 170 |
let nonce_array: [u8; 12] = |
| 171 |
nonce.try_into().expect("Nonce must be exactly 12 bytes"); |
| 172 |
let aad_array = aad.unwrap_or(&[0; 0]); |
| 173 |
|
| 174 |
let mut cipher = nss_gk_api::aead::Aead::new(mode, alg, &key, nonce_array)?; |
| 175 |
|
| 176 |
let plaintext = cipher |
| 177 |
.open(aad_array, 0, ciphertext) |
| 178 |
.map_err(|_| AeadError::NssError(nss_gk_api::Error::AeadError))?; |
| 179 |
|
| 180 |
Ok(plaintext) |
| 181 |
} |
| 182 |
AeadId::Chacha20Poly1305 => { |
| 183 |
let alg = nss_gk_api::aead::AeadAlgorithms::ChaCha20Poly1305; |
| 184 |
let key = nss_gk_api::aead::Aead::import_key(alg, key)?; |
| 185 |
let nonce_array: [u8; 12] = |
| 186 |
nonce.try_into().expect("Nonce must be exactly 12 bytes"); |
| 187 |
let aad_array = aad.unwrap_or(&[0; 0]); |
| 188 |
|
| 189 |
let mut cipher = nss_gk_api::aead::Aead::new(mode, alg, &key, nonce_array)?; |
| 190 |
|
| 191 |
let plaintext = cipher |
| 192 |
.open(aad_array, 0, ciphertext) |
| 193 |
.map_err(|_| AeadError::NssError(nss_gk_api::Error::AeadError))?; |
| 194 |
|
| 195 |
Ok(plaintext) |
| 196 |
} |
| 197 |
_ => Err(AeadError::UnsupportedCipherSuite), |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
#[inline(always)] |
| 202 |
fn key_size(&self) -> usize { |
| 203 |
self.0.key_size() |
| 204 |
} |
| 205 |
|
| 206 |
fn nonce_size(&self) -> usize { |
| 207 |
self.0.nonce_size() |
| 208 |
} |
| 209 |
|
| 210 |
fn aead_id(&self) -> u16 { |
| 211 |
self.0 as u16 |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
#[cfg(all(not(mls_build_async), test))] |
| 216 |
mod test { |
| 217 |
use mls_rs_core::crypto::CipherSuite; |
| 218 |
use mls_rs_crypto_traits::{AeadType, AES_TAG_LEN}; |
| 219 |
|
| 220 |
use super::{Aead, AeadError}; |
| 221 |
|
| 222 |
use assert_matches::assert_matches; |
| 223 |
|
| 224 |
use alloc::vec; |
| 225 |
use alloc::vec::Vec; |
| 226 |
|
| 227 |
fn get_aeads() -> Vec<Aead> { |
| 228 |
[ |
| 229 |
CipherSuite::CURVE25519_AES128, |
| 230 |
CipherSuite::CURVE25519_CHACHA, |
| 231 |
CipherSuite::CURVE448_AES256, |
| 232 |
] |
| 233 |
.into_iter() |
| 234 |
.map(|cs| Aead::new(cs).unwrap()) |
| 235 |
.collect() |
| 236 |
} |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn invalid_key() { |
| 240 |
for aead in get_aeads() { |
| 241 |
let nonce = vec![42u8; aead.nonce_size()]; |
| 242 |
let data = b"top secret"; |
| 243 |
|
| 244 |
let too_short = vec![42u8; aead.key_size() - 1]; |
| 245 |
|
| 246 |
assert_matches!( |
| 247 |
aead.seal(&too_short, data, None, &nonce), |
| 248 |
Err(AeadError::InvalidKeyLen(_, _)) |
| 249 |
); |
| 250 |
|
| 251 |
let too_long = vec![42u8; aead.key_size() + 1]; |
| 252 |
|
| 253 |
assert_matches!( |
| 254 |
aead.seal(&too_long, data, None, &nonce), |
| 255 |
Err(AeadError::InvalidKeyLen(_, _)) |
| 256 |
); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn invalid_ciphertext() { |
| 262 |
for aead in get_aeads() { |
| 263 |
let key = vec![42u8; aead.key_size()]; |
| 264 |
let nonce = vec![42u8; aead.nonce_size()]; |
| 265 |
|
| 266 |
let too_short = [0u8; AES_TAG_LEN]; |
| 267 |
|
| 268 |
assert_matches!( |
| 269 |
aead.open(&key, &too_short, None, &nonce), |
| 270 |
Err(AeadError::InvalidCipherLen(_)) |
| 271 |
); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn aad_mismatch() { |
| 277 |
for aead in get_aeads() { |
| 278 |
let key = vec![42u8; aead.key_size()]; |
| 279 |
let nonce = vec![42u8; aead.nonce_size()]; |
| 280 |
|
| 281 |
let ciphertext = aead.seal(&key, b"message", Some(b"foo"), &nonce).unwrap(); |
| 282 |
|
| 283 |
assert_matches!( |
| 284 |
aead.open(&key, &ciphertext, Some(b"bar"), &nonce), |
| 285 |
Err(AeadError::NssError(_)) |
| 286 |
); |
| 287 |
|
| 288 |
assert_matches!( |
| 289 |
aead.open(&key, &ciphertext, None, &nonce), |
| 290 |
Err(AeadError::NssError(_)) |
| 291 |
); |
| 292 |
} |
| 293 |
} |
| 294 |
} |