Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Formats

Every byte layout the library reads or writes is listed on this page and described on the pages below it. Read it to find the layout of a stored value, to tell two layouts apart, or to decide which pages to read to parse a value outside the library:

  • Values and DEKs: Encrypted<T, F> and its formats, Tink’s KMS envelope, encrypted DEKs and the serialised DEKs inside them, DEKs encrypted with a public key, and beacons;
  • Streamed values: the header and chunks of streamed values, in the prefixed, bare and envelope forms, and how a range read maps to chunks;
  • Signature and MAC formats: detached and attached signatures and MACs, what they are computed over, the key ids of signing keys and the test vectors;
  • Keyring records and keysets: the key ids of a keyring, the JSON record every store keeps, encryption markers, and Tink keysets.

The library writes one envelope format for every provider, Tink’s KMS envelope. A value encrypted by KmsAeadEnvelope::encrypt decrypts with Tink’s KmsEnvelopeAead on the same KMS key and algorithm, and a value Tink encrypts decrypts with KmsAeadEnvelope::decrypt.

LayoutTypeBytesRead byStatus
Tink KMS envelopeEncrypted<T, Envelope>[w: u32 BE][encrypted DEK: w][nonce: 12][ciphertext][tag: 16]kms-aead, Tinkcurrent
Bare valueEncrypted<T, Bare>[nonce: 12][ciphertext][tag: 16]kms-aead, Tinkcurrent
Encrypted DEKEncryptedDataEncryptionKeythe provider’s byteskms-aead, Tink for GCP and AWS keyscurrent
Serialised DEKthe plaintext of an encrypted DEK, but Azure’sTink’s key messagekms-aead, Tinkcurrent
TINK-prefixed valueEncrypted<T, Randomized>, Encrypted<T, Deterministic>, named by a KeyId[0x01][key id: u32 BE][value]kms-aead, Tink, BigQuery; kms-aead-keyring writes it toocurrent
Streamed valueStreamEncryptor and StreamDecryptor of AeadCipher::stream_encryptor and stream_decryptor, and of the keyring’s Encryptor::stream_encryptor and Keyring::stream_decryptorsee its pagekms-aead with stream; a C2SP chunked-encryption reader from byte 5; kms-aead-keyring writes it toocurrent
Envelope streamed valueStreamEncryptor and StreamDecryptor of KmsAeadEnvelope::stream_encryptor and stream_decryptorsee its pagekms-aead with stream; a C2SP chunked-encryption reader from byte 4 + wcurrent
Bare streamed valueStreamEncryptor and StreamDecryptor of the stream_encryptor_bare and stream_decryptor_bare calls of AeadCipher and KmsAeadEnvelopesee its pagekms-aead with stream, any C2SP chunked-encryption readercurrent
Detached signatureSignature<T>[0x01][key id: u32 BE][signature]kms-aeadcurrent
Attached signatureSigned<T>[0x01][key id: u32 BE][w: u16 BE][signature: w][payload]kms-aeadcurrent
Detached MACMac<T>[0x01][key id: u32 BE][tag]kms-aead, and kms-aead-keyring for its MAC versionscurrent
Attached MACWithMac<T>[0x01][key id: u32 BE][w: u16 BE][tag: w][payload]kms-aead, and kms-aead-keyring for its MAC versionscurrent
Tink keysetTinkKeysetgoogle.crypto.tink.KeysetTink, BigQuery; kms-aead-keyring writes it toocurrent
Tink encrypted keysetTinkKeyset::into_encrypted_bytesgoogle.crypto.tink.EncryptedKeyset, [0x12][w: varint][encrypted keyset: w]Tink with a GCP, AWS or Vault Transit key; kms-aead-keyring writes it toocurrent
Envelope layout v1none[0x01][k: u8][key version: k][w: u16 BE][encrypted DEK: w][cipher text]kms-aead 0.33 and earlierlegacy
Encrypted DEK layout v1none[0xFF][0x01][k: u8][key version: k][encrypted DEK]kms-aead 0.33 and earlierlegacy
Length-prefixed envelopenone[w: u64 BE][encrypted DEK: w][cipher text], and [w: u32 BE] from 32-bit targets in early releaseskms-aead 0.33 and earlierlegacy
Raw encrypted DEKEncryptedDataEncryptionKeythe KMS ciphertext of the DEK’s 16 or 32 raw byteskms-aead 0.33 and earlier, and kms-aead under Azurelegacy, except under Azure, which still writes it

k = 0 records no key version. A key version is 1 to 255 ASCII alphanumeric characters. The Azure provider encrypts the raw DEK, see Encrypted DEKs.

The library refuses the legacy layouts with LEGACY_FORMAT. How kms-aead 0.33 rewrites stored values in them is in Migrating from 0.29.

Telling layouts apart

The first bytes of a stored value:

First bytesLayout
00 00 00 00the legacy length-prefixed envelope
00, then a length of 1 to 4,096Tink’s KMS envelope, an envelope streamed value, or the 4-byte length prefix of the legacy envelope on 32-bit targets
01the legacy envelope layout v1, or a TINK-prefixed value
02a DEK encrypted with the public half of an asymmetric KEK
FF 01the legacy encrypted DEK layout v1

Only the decrypted DEK tells a Tink envelope from the legacy 32-bit layout: a raw key of 16 or 32 bytes against a serialised Tink key, whose lengths differ. An encrypted DEK of AWS, Azure or the local KMS also starts with 0x01, and Tink’s LEGACY and CRUNCHY output prefixes with 0x00.

A column that holds two layouts:

  • Tink envelopes and TINK-prefixed values: the first byte, 0x00 or 0x01;
  • Tink envelopes and legacy envelopes of layout v1: the first byte, 0x00 or 0x01;
  • Tink envelopes and legacy length-prefixed envelopes: the first four bytes;
  • legacy envelopes of layout v1 and TINK-prefixed values: no byte tells them apart, so the layout is recorded outside the value, such as in another column;
  • bare values and any other layout: a bare value has no framing, and its random nonce can start with any byte, so a column of bare values holds no other layout;
  • streamed values and bare streamed values: no byte tells them apart, since a bare one starts with its random salt, so the call names the form, as decrypt_bare does for a bare value, see Streamed values;
  • envelope streamed values and Tink envelopes: both start with [w][encrypted DEK], so the layout is recorded outside the value, as for bare values;
  • legacy encrypted DEKs of layout v1 and current ones: 0xFF 0x01 starts no encrypted DEK of a built-in provider, so the library tells them apart for those and refuses the legacy ones before the KMS call. It never inspects the bytes of an application’s own provider.