Module aws_lc_rs::aead

source ·
Expand description

Authenticated Encryption with Associated Data (AEAD).

See Authenticated encryption: relations among notions and analysis of the generic composition paradigm for an introduction to the concept of AEADs.

Examples

use aws_lc_rs::rand;
use aws_lc_rs::aead::{UnboundKey, AES_128_GCM, nonce_sequence, SealingKey,
    OpeningKey, Aad, BoundKey};
use aws_lc_rs::test::from_hex;

let plaintext = "plaintext value";

// Generate random bytes for secret key
let mut key_bytes = [0u8; 16];
rand::fill(&mut key_bytes).expect("Unable to generate key");

// Contextual information must match between encryption and decryption
let aad_content = "aws-lc-rs documentation";
let sequence_id = 0xabcdef01u32.to_be_bytes();

// Buffer containing plaintext. This will be modified to contain the ciphertext.
let mut in_out_buffer = Vec::from(plaintext);

// Construct a SealingKey for encryption
let unbound_key = UnboundKey::new(&AES_128_GCM, &key_bytes).unwrap();
let nonce_sequence = nonce_sequence::Counter64Builder::new()
    .identifier(sequence_id)
    .build();
let mut sealing_key = SealingKey::new(unbound_key, nonce_sequence);

// Encrypt a value using the SealingKey
let aad = Aad::from(aad_content);
sealing_key.seal_in_place_append_tag(aad, &mut in_out_buffer).expect("Encryption failed");

// The buffer now contains the ciphertext followed by a "tag" value.
let plaintext_len = in_out_buffer.len() - AES_128_GCM.tag_len();

// Construct an OpeningKey for decryption
let unbound_key = UnboundKey::new(&AES_128_GCM, &key_bytes).unwrap();
let nonce_sequence = nonce_sequence::Counter64Builder::new()
    .identifier(sequence_id)
    .build();
let mut opening_key = OpeningKey::new(unbound_key, nonce_sequence);

// Decrypt the value using the OpeningKey
let aad = Aad::from(aad_content);
opening_key.open_in_place(aad, &mut in_out_buffer).expect("Decryption failed");

let decrypted_plaintext = std::str::from_utf8(&in_out_buffer[0..plaintext_len]).unwrap();

assert_eq!(plaintext, decrypted_plaintext);

Modules

Structs

  • The additionally authenticated data (AAD) for an opening or sealing operation. This data is authenticated but is not encrypted.
  • An AEAD Algorithm.
  • Immutable keys for use in situations where OpeningKey/SealingKey and NonceSequence cannot reasonably be used.
  • A nonce for a single AEAD opening or sealing operation.
  • An AEAD key for authenticating and decrypting (“opening”), bound to a nonce sequence.
  • An AEAD key for encrypting and signing (“sealing”), bound to a nonce sequence.
  • An authentication tag.
  • An AEAD key without a designated role or nonce sequence.

Constants

  • The maximum length of a tag for the algorithms in this module.
  • All the AEADs we support use 96-bit nonces.

Statics

Traits