Skip to main content

Encrypted Callbacks

When encryption is switched on for an API key, the data in every callback sent for that key is unreadable until you decrypt it. Encryption always comes with signing, so verify the signature before you decrypt.

What an encrypted callback looks like​

With encryption on, the body is an envelope in place of the usual JSON. The headers are the same as on a signed callback, and the signature is computed over this envelope exactly as received.

{
"encrypted": true,
"algorithm": "AES-256-GCM",
"iv": "<base64, 12 bytes>",
"tag": "<base64, 16 bytes>",
"data": "<base64 ciphertext>"
}
FieldMeaning
encryptedAlways true on an encrypted callback
algorithmAlways AES-256-GCM
ivThe initialisation vector, new for every callback
tagThe authentication tag; decryption fails if the envelope was altered
dataThe encrypted callback JSON

Handle it​

  1. Verify the signature and timestamp against the raw body, as in Signed Callbacks. Reject on failure without decrypting.
  2. Parse the envelope and base64-decode iv, tag and data.
  3. Decrypt data with AES-256-GCM, using the key from your encryption_secret (hex-decoded to 32 bytes), the iv and the tag. Reject the callback if decryption fails.
  4. The result is the usual callback JSON. Parse it and carry on as before, including the transactionnumber check.
Important

Verify the signature before you decrypt. If the signature check fails, reject the callback without decrypting it.

Important

Hex-decode the encryption_secret to 32 bytes before you use it as the key. Do not use it as text.

// after the signature check
$envelope = json_decode($raw, true);

$json = openssl_decrypt(
base64_decode($envelope['data']),
'aes-256-gcm',
hex2bin($encryptionSecret),
OPENSSL_RAW_DATA,
base64_decode($envelope['iv']),
base64_decode($envelope['tag'])
);

if ($json === false) {
http_response_code(400);
exit;
}

$payload = json_decode($json, true);

Where the tag goes​

The envelope carries the tag on its own. Libraries differ in how they want it:

LanguageTag handling
PHP, Node, C#Passed separately
Python (cryptography), Java, GoAppended to the end of the ciphertext
tip

If decryption fails on a callback whose signature passed, first check that you are passing the tag the way your library expects.