AwsEncryptionSdk.Format.Body (AWS Encryption SDK v1.2.2)

View Source

Message body serialization and deserialization.

Supports both framed and non-framed body formats.

Non-Framed Format

| Field           | Size      |
|-----------------|-----------|
| IV              | 12 bytes  |
| Content Length  | 8 bytes   | Uint64
| Ciphertext      | Variable  |
| Auth Tag        | 16 bytes  |

Framed Format

Regular frames:

| Field           | Size      |
|-----------------|-----------|
| Sequence Number | 4 bytes   | Uint32 (1, 2, 3, ...)
| IV              | 12 bytes  |
| Ciphertext      | frame_length bytes |
| Auth Tag        | 16 bytes  |

Final frame:

| Field           | Size      |
|-----------------|-----------|
| Seq Number End  | 4 bytes   | 0xFFFFFFFF
| Sequence Number | 4 bytes   | Actual sequence number
| IV              | 12 bytes  |
| Content Length  | 4 bytes   | Uint32
| Ciphertext      | Variable  |
| Auth Tag        | 16 bytes  |

Summary

Types

Final frame structure

Any frame type

Non-framed body structure

Regular frame structure

Functions

Checks a message's frame count against the specification's limit of 2^32 - 1 frames.

Deserializes all frames from a framed body.

Deserializes a frame (regular or final).

Deserializes a non-framed body.

Returns the number of frames a framed body holds for plaintext_length bytes split into frames of frame_length bytes.

Serializes a non-framed body.

Types

final_frame()

@type final_frame() :: %{
  sequence_number: pos_integer(),
  iv: binary(),
  ciphertext: binary(),
  auth_tag: binary(),
  final: true
}

Final frame structure

frame()

@type frame() :: regular_frame() | final_frame()

Any frame type

non_framed()

@type non_framed() :: %{iv: binary(), ciphertext: binary(), auth_tag: binary()}

Non-framed body structure

regular_frame()

@type regular_frame() :: %{
  sequence_number: pos_integer(),
  iv: binary(),
  ciphertext: binary(),
  auth_tag: binary()
}

Regular frame structure

Functions

check_frame_count(frame_count)

@spec check_frame_count(pos_integer()) :: :ok | {:error, :max_frames_exceeded}

Checks a message's frame count against the specification's limit of 2^32 - 1 frames.

Returns :ok when a message of frame_count frames may be written, and {:error, :max_frames_exceeded} when it would exceed the limit.

deserialize_all_frames(data, frame_length)

@spec deserialize_all_frames(binary(), pos_integer()) ::
  {:ok, [frame()], binary()} | {:error, term()}

Deserializes all frames from a framed body.

Returns {:ok, frames, rest} where frames is a list ordered by sequence number.

deserialize_frame(arg1, frame_length)

@spec deserialize_frame(binary(), pos_integer()) ::
  {:ok, frame(), binary()} | {:error, term()}

Deserializes a frame (regular or final).

Returns {:ok, frame_map, rest} where frame_map includes :final key for final frames.

deserialize_non_framed(arg1)

@spec deserialize_non_framed(binary()) ::
  {:ok, non_framed(), binary()} | {:error, term()}

Deserializes a non-framed body.

Returns {:ok, body_map, rest} on success.

frame_count(plaintext_length, frame_length)

@spec frame_count(non_neg_integer(), pos_integer()) :: pos_integer()

Returns the number of frames a framed body holds for plaintext_length bytes split into frames of frame_length bytes.

Every full frame but the last is a regular frame, and the remainder (or the last full frame) is the final frame: an empty plaintext is one empty final frame, and a plaintext of exactly n * frame_length bytes is n frames.

serialize_final_frame(sequence_number, iv, ciphertext, auth_tag)

@spec serialize_final_frame(pos_integer(), binary(), binary(), binary()) :: binary()

Serializes a final frame.

Parameters

  • sequence_number - Frame sequence number, the message's frame count (at most 2^32 - 1)
  • iv - 12-byte initialization vector
  • ciphertext - Encrypted content (may be shorter than frame_length)
  • auth_tag - 16-byte authentication tag

serialize_non_framed(iv, ciphertext, auth_tag)

@spec serialize_non_framed(binary(), binary(), binary()) ::
  {:ok, binary()} | {:error, :content_too_large}

Serializes a non-framed body.

Parameters

  • iv - 12-byte initialization vector
  • ciphertext - Encrypted content
  • auth_tag - 16-byte authentication tag

Returns

{:ok, binary} on success, {:error, reason} if content exceeds 64 GiB limit.

serialize_regular_frame(sequence_number, iv, ciphertext, auth_tag)

@spec serialize_regular_frame(pos_integer(), binary(), binary(), binary()) :: binary()

Serializes a regular frame.

Parameters

  • sequence_number - Frame sequence (1, 2, 3, ...), below 2^32 - 1: a final frame follows every regular frame
  • iv - 12-byte initialization vector
  • ciphertext - Encrypted content (must be exactly frame_length bytes)
  • auth_tag - 16-byte authentication tag