pub struct AesGcm { /* private fields */ }Expand description
Represents an AES-GCM (Galois/Counter Mode) instance.
Implementations§
Source§impl AesGcm
impl AesGcm
Sourcepub fn new(key: &Key) -> Result<Self, Unspecified>
pub fn new(key: &Key) -> Result<Self, Unspecified>
Sourcepub fn encrypt_sized<const C: usize, N: GenericIv, A: Aad>(
&mut self,
nonce: N,
input: &[u8; C],
output: &mut [u8; C],
aad: A,
) -> Result<Tag, Unspecified>
pub fn encrypt_sized<const C: usize, N: GenericIv, A: Aad>( &mut self, nonce: N, input: &[u8; C], output: &mut [u8; C], aad: A, ) -> Result<Tag, Unspecified>
Encrypt data using AES-GCM with compile-time known sizes.
§Arguments
nonce- The nonce (IV) to use for encryption.input- The input data to encrypt.output- The output buffer to store the encrypted data.aad- Additional Authenticated Data.
§Errors
§Returns
The associated authentication Tag.
§Example
use wolf_crypto::{aead::{AesGcm, AadSlice}, aes::Key, buf::Nonce};
let key = Key::Aes256([1u8; 32]);
let nonce: Nonce = [2u8; 12].into();;
let input = [3u8; 32];
let mut output = [0u8; 32];
let mut gcm = AesGcm::new(&key).unwrap();
let tag = gcm.encrypt_sized(nonce, &input, &mut output, AadSlice::EMPTY).unwrap();
assert_ne!(input, output);Sourcepub fn try_encrypt<N: GenericIv, A: Aad>(
&mut self,
nonce: N,
input: &[u8],
output: &mut [u8],
aad: A,
) -> Result<Tag, Unspecified>
pub fn try_encrypt<N: GenericIv, A: Aad>( &mut self, nonce: N, input: &[u8], output: &mut [u8], aad: A, ) -> Result<Tag, Unspecified>
Try to encrypt data using AES-GCM.
§Arguments
nonce- The nonce (IV) to use for encryption.input- The input data to encrypt.output- The output buffer to store the encrypted data.aad- Additional Authenticated Data.
§Returns
The authentication tag on success, or an error.
§Errors
- If the input buffer is larger than the output buffer.
- If the input or AAD size is greater than what can be represented by a u32.
§Example
use wolf_crypto::{aead::{AesGcm, AadSlice}, aes::Key, buf::Nonce};
let key = Key::Aes256([1u8; 32]);
let nonce: Nonce = [2u8; 12].into();
let mut output = [0u8; 32];
let input = [3u8; 32];
let mut gcm = AesGcm::new(&key).unwrap();
let tag = gcm.try_encrypt(nonce, &input, &mut output, AadSlice::EMPTY).unwrap();
assert_ne!(input, output);Sourcepub fn encrypt<N: GenericIv, A: Aad>(
&mut self,
nonce: N,
input: &[u8],
output: &mut [u8],
aad: A,
) -> Tag
Available on crate feature can-panic only.
pub fn encrypt<N: GenericIv, A: Aad>( &mut self, nonce: N, input: &[u8], output: &mut [u8], aad: A, ) -> Tag
can-panic only.Encrypt data using AES-GCM, panicking on failure.
§Arguments
nonce- The nonce (IV) to use for encryption.input- The input data to encrypt.output- The output buffer to store the encrypted data.aad- Additional Authenticated Data.
§Returns
The authentication tag.
§Panics
- If the input buffer is larger than the output buffer.
- If the input or AAD size is greater than what can be represented by a u32.
- If the encryption operation fails.
§Example
use wolf_crypto::{aead::{AesGcm, AadSlice}, aes::Key, buf::Nonce};
let key = Key::Aes256([1u8; 32]);
let nonce: Nonce = [2u8; 12].into();
let mut output = [0u8; 32];
let input = [3u8; 32];
let mut gcm = AesGcm::new(&key).unwrap();
let tag = gcm.encrypt(nonce, &input, &mut output, AadSlice::EMPTY);
assert_ne!(input, output);Sourcepub fn decrypt_sized<const C: usize, N: GenericIv, A: Aad>(
&mut self,
nonce: N,
input: &[u8; C],
output: &mut [u8; C],
aad: A,
tag: &Tag,
) -> Res
pub fn decrypt_sized<const C: usize, N: GenericIv, A: Aad>( &mut self, nonce: N, input: &[u8; C], output: &mut [u8; C], aad: A, tag: &Tag, ) -> Res
Decrypt data using AES-GCM with compile-time known sizes.
§Arguments
nonce- The nonce (IV) used for encryption.input- The input data to decrypt.output- The output buffer to store the decrypted data.aad- Additional Authenticated Data.tag- The authentication tag from encryption.
§Returns
A Res indicating success or failure.
§Example
use wolf_crypto::{aead::{AesGcm, AadSlice}, aes::Key, buf::Nonce};
let key = Key::Aes256([1u8; 32]);
let nonce: Nonce = [2u8; 12].into();
let mut ciphertext = [0u8; 32];
let plaintext = [3u8; 32];
let aad = AadSlice::EMPTY;
let mut gcm = AesGcm::new(&key).unwrap();
let tag = gcm.encrypt_sized(nonce.copy(), &plaintext, &mut ciphertext, aad).unwrap();
let mut decrypted = [0u8; 32];
let result = gcm.decrypt_sized(nonce, &ciphertext, &mut decrypted, aad, &tag);
assert!(result.is_ok());
assert_eq!(plaintext, decrypted);Sourcepub fn try_decrypt<N: GenericIv, A: Aad>(
&mut self,
nonce: N,
input: &[u8],
output: &mut [u8],
aad: A,
tag: &Tag,
) -> Res
pub fn try_decrypt<N: GenericIv, A: Aad>( &mut self, nonce: N, input: &[u8], output: &mut [u8], aad: A, tag: &Tag, ) -> Res
Try to decrypt data using AES-GCM.
§Arguments
nonce- The nonce (IV) used for encryption.input- The input data to decrypt.output- The output buffer to store the decrypted data.aad- Additional Authenticated Data.tag- The authentication tag from encryption.
§Returns
A Res indicating success or failure.
§Errors
- If the input buffer is larger than the output buffer.
- If the input or AAD size is greater than what can be represented by a u32.
- If the decryption operation fails (including authentication failure).
§Example
use wolf_crypto::{aead::AesGcm, aes::Key, buf::Nonce};
let key = Key::Aes256([1u8; 32]);
let nonce: Nonce = [2u8; 12].into();
let mut ciphertext = [0u8; 32];
let plaintext = [3u8; 32];
let mut gcm = AesGcm::new(&key).unwrap();
let tag = gcm.try_encrypt(nonce.copy(), &plaintext, &mut ciphertext, ()).unwrap();
let mut decrypted = [0u8; 32];
let result = gcm.try_decrypt(nonce, &ciphertext, &mut decrypted, (), &tag);
assert!(result.is_ok());
assert_eq!(plaintext, decrypted);Sourcepub fn decrypt<N: GenericIv, A: Aad>(
&mut self,
nonce: N,
input: &[u8],
output: &mut [u8],
aad: A,
tag: &Tag,
)
Available on crate feature can-panic only.
pub fn decrypt<N: GenericIv, A: Aad>( &mut self, nonce: N, input: &[u8], output: &mut [u8], aad: A, tag: &Tag, )
can-panic only.Decrypt data using AES-GCM, panicking on failure.
§Arguments
nonce- The nonce (IV) used for encryption.input- The input data to decrypt.output- The output buffer to store the decrypted data.aad- Additional Authenticated Data.tag- The authentication tag from encryption.
§Panics
- If the input buffer is larger than the output buffer.
- If the input or AAD size is greater than what can be represented by a u32.
- If the decryption operation fails (including authentication failure).
§Example
use wolf_crypto::{aead::AesGcm, aes::Key, buf::Nonce};
let key = Key::Aes256([1u8; 32]);
let nonce: Nonce = [2u8; 12].into();
let mut ciphertext = [0u8; 32];
let plaintext = [3u8; 32];
let mut gcm = AesGcm::new(&key).unwrap();
let tag = gcm.encrypt(nonce.copy(), &plaintext, &mut ciphertext, ());
let mut decrypted = [0u8; 32];
gcm.decrypt(nonce, &ciphertext, &mut decrypted, (), &tag);
assert_eq!(plaintext, decrypted);Trait Implementations§
impl Fips for AesGcm
impl Send for AesGcm
impl Sync for AesGcm
Auto Trait Implementations§
impl Freeze for AesGcm
impl RefUnwindSafe for AesGcm
impl Unpin for AesGcm
impl UnwindSafe for AesGcm
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more