Skip to main content

Gcm

Struct Gcm 

Source
pub struct Gcm<C> { /* private fields */ }
Expand description

Galois/Counter Mode (GCM) with a full 128-bit authentication tag.

Per NIST SP 800-38D, this implementation enforces a per-call payload limit of (2^32 - 2) counter blocks (68_719_476_704 bytes) so the 32-bit counter field cannot wrap.

Callers must still ensure nonce uniqueness per key; this API is stateless and cannot enforce global (key, nonce) uniqueness.

§Examples

use cryptography::{Aes256, Gcm};

let key = [0x11u8; 32];
let nonce = [0x22u8; 12];
let aad = b"hdr";
let mut ciphertext = *b"payload";

let aead = Gcm::new(Aes256::new(&key));
let tag = aead.encrypt(&nonce, aad, &mut ciphertext);

let mut recovered = ciphertext;
assert!(aead.decrypt(&nonce, aad, &mut recovered, &tag));
assert_eq!(recovered, *b"payload");

Implementations§

Source§

impl<C> Gcm<C>

Source

pub fn new(cipher: C) -> Self

Wrap a 128-bit block cipher in SP 800-38D GCM mode.

Source

pub fn cipher(&self) -> &C

Borrow the wrapped block cipher.

Source§

impl<C: BlockCipher> Gcm<C>

Source

pub fn compute_tag( &self, nonce: &[u8], aad: &[u8], ciphertext: &[u8], ) -> [u8; 16]

Compute the GCM authentication tag over aad and ciphertext.

Panics if ciphertext.len() exceeds the SP 800-38D per-call bound of 68_719_476_704 bytes.

Source

pub fn encrypt(&self, nonce: &[u8], aad: &[u8], data: &mut [u8]) -> [u8; 16]

Encrypt in place and return the 128-bit authentication tag.

Panics if data.len() exceeds the SP 800-38D per-call bound of 68_719_476_704 bytes.

Source

pub fn decrypt( &self, nonce: &[u8], aad: &[u8], data: &mut [u8], tag: &[u8], ) -> bool

Verify the tag and, if valid, decrypt in place.

Returns false and leaves data unchanged if tag verification fails.

Panics if data.len() exceeds the SP 800-38D per-call bound of 68_719_476_704 bytes.

Trait Implementations§

Source§

impl<C: BlockCipher> Aead for Gcm<C>

Source§

type Tag = [u8; 16]

Detached authentication tag type.
Source§

fn encrypt_in_place( &self, nonce: &[u8], aad: &[u8], data: &mut [u8], ) -> Self::Tag

Encrypt data in place and return its authentication tag.
Source§

fn decrypt_in_place( &self, nonce: &[u8], aad: &[u8], data: &mut [u8], tag: &Self::Tag, ) -> bool

Decrypt data in place after authenticating tag.
Source§

fn encrypt( &self, nonce: &[u8], aad: &[u8], plaintext: &[u8], ) -> (Vec<u8>, Self::Tag)

Encrypt plaintext and return (ciphertext, tag).
Source§

fn decrypt( &self, nonce: &[u8], aad: &[u8], ciphertext: &[u8], tag: &Self::Tag, ) -> Option<Vec<u8>>

Decrypt ciphertext and return plaintext on successful authentication.

Auto Trait Implementations§

§

impl<C> Freeze for Gcm<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for Gcm<C>
where C: RefUnwindSafe,

§

impl<C> Send for Gcm<C>
where C: Send,

§

impl<C> Sync for Gcm<C>
where C: Sync,

§

impl<C> Unpin for Gcm<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for Gcm<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for Gcm<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.