Gcm

Mojo struct 🡭

Gcm

@memory_only
struct Gcm[Cipher: BlockCipherDecryptable & BlockCipherEncryptable & Copyable & Deinitable, G: UniversalHashable & Copyable & Deinitable, NONCE_SIZE: Int]

Galois/Counter Mode (GCM) authenticated encryption.

GCM combines counter (CTR) mode for confidentiality with GHASH for authentication.

Note: GCM is defined only for block ciphers with a 128-bit block size.

NIST SP 800-38D: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf

Aliases

  • BLOCK_SIZE = Cipher.BLOCK_SIZE

Parameters

  • Cipher (BlockCipherDecryptable & BlockCipherEncryptable & Copyable & Deinitable)
  • G (UniversalHashable & Copyable & Deinitable)
  • NONCE_SIZE (Int)

Implemented traits

AeadDecryptable, AeadEncryptable, AnyType, Copyable, Deinitable, Movable

Methods

__init__

fn def __init__(out self, var cipher: Cipher, nonce: Array[UInt8, NONCE_SIZE])

Initialize the GCM mode with the given block cipher and nonce.

Args:

  • cipher (Cipher)
  • nonce (Array[UInt8, NONCE_SIZE])
  • self (Self)

Returns:

Self

Raises:

encrypt

fn def encrypt[TAG_SIZE: Int, aad_o: Origin[mut=aad_o.mut], o: MutOrigin](mut self, aad: Span[UInt8, aad_o], data: Span[UInt8, o]) -> Array[UInt8, TAG_SIZE]

Encrypt data in place and return the TAG_SIZE-byte tag.

The counter starts at inc32(J0); GHASH then authenticates aad together with the freshly produced ciphertext.

Parameters:

  • TAG_SIZE (Int)
  • aad_o (Origin[mut=mut])
  • o (MutOrigin)

Args:

  • self (Self)
  • aad (Span[UInt8, aad_o])
  • data (Span[UInt8, o])

Returns:

Array[UInt8, TAG_SIZE]

Raises:

decrypt

fn def decrypt[TAG_SIZE: Int, aad_o: Origin[mut=aad_o.mut], o: MutOrigin](mut self, aad: Span[UInt8, aad_o], data: Span[UInt8, o], tag: Array[UInt8, TAG_SIZE])

Verify tag, then decrypt data in place.

The tag is recomputed over aad and the input ciphertext and compared in constant time. On mismatch this raises and data is left untouched.

tag_size satisfies the generic Aead.decrypt signature but is pinned to this instance’s TAG_SIZE; it is inferred from tag.

Parameters:

  • TAG_SIZE (Int)
  • aad_o (Origin[mut=mut])
  • o (MutOrigin)

Args:

  • self (Self)
  • aad (Span[UInt8, aad_o])
  • data (Span[UInt8, o])
  • tag (Array[UInt8, TAG_SIZE])

Raises: