// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // Copyright by contributors to this project. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
use core::fmt::Debug;
use mls_rs_core::{crypto::CipherSuite, error::IntoAnyError}; use mls_rs_crypto_traits::{AeadId, AeadType, AES_TAG_LEN};
use alloc::vec::Vec;
#[derive(Debug)] #[cfg_attr(feature = "std", derive(thiserror::Error))] pubenum AeadError { #[cfg_attr(feature = "std", error("NSS Error"))]
NssError(nss_rs::Error), #[cfg_attr(
feature = "std",
error("AEAD ciphertext of length {0} is too short to fit the tag")
)]
InvalidCipherLen(usize), #[cfg_attr(feature = "std", error("encrypted message cannot be empty"))]
EmptyPlaintext, #[cfg_attr(
feature = "std",
error("AEAD key of invalid length {0}. Expected length {1}")
)]
InvalidKeyLen(usize, usize), #[cfg_attr(feature = "std", error("unsupported cipher suite"))]
UnsupportedCipherSuite,
}
matchself.0 {
AeadId::Aes128Gcm => { let alg = nss_rs::aead::AeadAlgorithms::Aes128Gcm; let key = nss_rs::aead::Aead::import_key(alg, key)?; let nonce_array: [u8; 12] =
nonce.try_into().expect("Nonce must be exactly 12 bytes"); let aad_array = aad.unwrap_or(&[0; 0]);
let ciphertext = cipher
.encrypt(aad_array, data)
.map_err(|_| AeadError::NssError(nss_rs::Error::Aead))?;
Ok(ciphertext)
}
AeadId::Aes256Gcm => { let alg = nss_rs::aead::AeadAlgorithms::Aes256Gcm; let key = nss_rs::aead::Aead::import_key(alg, key)?; let nonce_array: [u8; 12] =
nonce.try_into().expect("Nonce must be exactly 12 bytes"); let aad_array = aad.unwrap_or(&[0; 0]);
let ciphertext = cipher
.encrypt(aad_array, data)
.map_err(|_| AeadError::NssError(nss_rs::Error::Aead))?;
Ok(ciphertext)
}
AeadId::Chacha20Poly1305 => { let alg = nss_rs::aead::AeadAlgorithms::ChaCha20Poly1305; let key = nss_rs::aead::Aead::import_key(alg, key)?; let nonce_array: [u8; 12] =
nonce.try_into().expect("Nonce must be exactly 12 bytes"); let aad_array = aad.unwrap_or(&[0; 0]);
matchself.0 {
AeadId::Aes128Gcm => { let alg = nss_rs::aead::AeadAlgorithms::Aes128Gcm; let key = nss_rs::aead::Aead::import_key(alg, key)?; let nonce_array: [u8; 12] =
nonce.try_into().expect("Nonce must be exactly 12 bytes"); let aad_array = aad.unwrap_or(&[0; 0]);
let plaintext = cipher
.decrypt(aad_array, 0, ciphertext)
.map_err(|_| AeadError::NssError(nss_rs::Error::Aead))?;
Ok(plaintext)
}
AeadId::Aes256Gcm => { let alg = nss_rs::aead::AeadAlgorithms::Aes256Gcm; let key = nss_rs::aead::Aead::import_key(alg, key)?; let nonce_array: [u8; 12] =
nonce.try_into().expect("Nonce must be exactly 12 bytes"); let aad_array = aad.unwrap_or(&[0; 0]);
let plaintext = cipher
.decrypt(aad_array, 0, ciphertext)
.map_err(|_| AeadError::NssError(nss_rs::Error::Aead))?;
Ok(plaintext)
}
AeadId::Chacha20Poly1305 => { let alg = nss_rs::aead::AeadAlgorithms::ChaCha20Poly1305; let key = nss_rs::aead::Aead::import_key(alg, key)?; let nonce_array: [u8; 12] =
nonce.try_into().expect("Nonce must be exactly 12 bytes"); let aad_array = aad.unwrap_or(&[0; 0]);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.