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

Decrypt columns in BigQuery SQL or with Tink

An analyst needs a column that the application encrypted, in a BigQuery query, or a service in Java or Python needs to read values a Rust service wrote. Values the library writes are in the formats of Tink, Google’s cryptography library, so Tink in another language and BigQuery’s SQL read them, given the keys. Three routes lead there, and they differ in what the reader needs and in whether the key rotates:

Envelope valuesOne fixed keyKeyring
Written byKmsAeadEnvelope::encrypt, per call or store-lessAeadCipher::encrypt or DeterministicCipher::encrypt, with a key the KMS encrypts oncean encryptor’s encrypt or encrypt_deterministic
A value carriesits encrypted DEKa 4-byte key ida 4-byte key id
Tink reads withKmsEnvelopeAead, on a Cloud KMS or AWS KMS keya keyset of the key, as TinkKeyset builds itexport_tink_keyset, on a Cloud KMS key, an AWS KMS key named by its ARN, or a Vault Transit key for Tink for Go
BigQuery reads withnothing: SQL has no KMS envelopeKEYS.KEYSET_CHAIN over the keyset encrypted by a Cloud KMS keyKEYS.KEYSET_CHAIN over export_bigquery_keyset, on a Cloud KMS key
Algorithms BigQuery readsnoneAES-256-GCM and AES-SIVAES-256-GCM and AES-SIV
Rotationthe KEK in the KMSnone: a second key needs a second key id and a keyset of bothcentral, with a new export after each rotation
SeeTink’s KMS envelopeOne fixed keyBigQuery and Tink interop

The AAD

The reader decrypts a value only with the AAD it was encrypted with, so the AAD is one the reader can build:

  • envelope values and one fixed key: the application’s own bytes, which a Tink reader passes as they are, and SQL builds from the row when they are text, see AAD;
  • a keyring: the field’s default AAD, <kind>/<id>/<field> for a randomised field, which SQL rebuilds from the row as UTF-8 text, see AAD in SQL. A custom AAD fails on a field declared for BigQuery.

With a keyring

  1. Declare each field the reader decrypts with FieldSpec::with_interop(Interop::BigQuery) or Interop::Tink: the keyring then refuses to encrypt it under an algorithm or a KEK the reader does not read, see Fields BigQuery reads and Fields Tink reads;

  2. the administrative job exports the keyset, with export_bigquery_keyset under the bigquery-export feature or export_tink_keyset under admin, and writes its bytes where the query or the reader takes them;

  3. the job exports again. A Readable keyset holds every version of a purpose that decrypts and is not compromised, and a Single keyset one version, as its primary key:

    • the Readable keysets after every rotate, before the promotion, and after every retire, every mark_compromised and every KEK rotation;
    • the Single keyset of a new deterministic version after its promotion, since a pending version has none. Until then, an equality lookup in SQL computes no probe under the new version and misses the rows encrypted under it, with no error.

    See Rotation cadence, Retiring the old version and Encrypted keysets.

Full example of a Tink export available here, and of a BigQuery sweep in SQL here.

Query keyring values in BigQuery

Every value reaches the query as a parameter: @kms_key is the export’s key_uri(), each keyset parameter its value(), and @email the address an equality query looks for. The requirements of a query are in Queries:

-- Randomised values, with a Readable(Randomized) keyset
SELECT AEAD.DECRYPT_STRING(
  KEYS.KEYSET_CHAIN(@kms_key, @keyset), u.email_enc, CONCAT('user/', u.id, '/email'))
FROM users u;

-- Deterministic values, with a Readable(Deterministic) or Single keyset
SELECT DETERMINISTIC_DECRYPT_STRING(KEYS.KEYSET_CHAIN(@kms_key, @keyset), u.email_det, 'user/email')
FROM users u;

-- An equality query: one probe per deterministic version, each with its Single keyset
SELECT u.id
FROM users u
WHERE u.email_det IN (
  DETERMINISTIC_ENCRYPT(KEYS.KEYSET_CHAIN(@kms_key, @keyset_d2), @email, 'user/email'),
  DETERMINISTIC_ENCRYPT(KEYS.KEYSET_CHAIN(@kms_key, @keyset_d1), @email, 'user/email')
);

Read a keyring export with Tink for Python

Tink for Python reads the keyset that export_tink_keyset wrote, such as tenant-42.keyset of Exporting a keyset to Tink, with the KMS client of the key URI the export returned, and decrypts a randomised value with the value’s default AAD as UTF-8:

import tink
from tink import aead
from tink.integration import gcpkms

aead.register()
key_uri = 'gcp-kms://projects/my-project/locations/europe-north1/keyRings/my-key-ring/cryptoKeys/my-key'
with open('tenant-42.keyset', 'rb') as keyset_file:
  keyset_handle = tink.read_keyset_handle(
      tink.BinaryKeysetReader(keyset_file.read()),
      gcpkms.GcpKmsClient(key_uri).get_aead(key_uri),
  )
email_aead = keyset_handle.primitive(aead.Aead)


def decrypt_email(value: bytes, user_id: str) -> str:
  return email_aead.decrypt(value, f'user/{user_id}/email'.encode()).decode()

What to watch for

  • An exported keyset holds the keys of the versions it selects, so treat it as a copy of the record’s readable versions, see Keyring limits;
  • BigQuery cannot calculate a beacon: a beacon lookup in BigQuery matches beacons the application calculated, see BigQuery.