Skip to content

Core\Crypto::Cipher

Names the construction a sealed message is written and read under.

Core\Crypto::Cipher is declared in the class Core\Crypto. The methods seal and open take it as an argument.

Core\Crypto::seal and Core\Crypto::open each take a cipher, and there is no default: every call says which of the two it is under. Both cases authenticate, so neither can produce a message somebody could alter without the open being refused. What they differ in is who else can read the result. XChaCha20Poly1305 is the one to reach for when both ends are your own programs — its nonce is wide enough that one is never drawn twice, and it is fast on a machine with no AES instructions. Aes256Gcm is the one a browser can open, because it is what WebCrypto encrypts with.

Good to know: sealed bytes carry no label saying which case wrote them, so a program that stores a message stores the case beside it. Opening under the other one is refused exactly as an altered message is. The examples seal under both cases, show that refusal, and keep the case beside the ciphertext in a small store.

Examples

The two constructions

<?nvs
// `Core\Crypto::seal` needs a `Core\Crypto::Cipher` case, and there is no default.
// Both ciphers detect a changed message, so both are safe. Pick `Aes256Gcm`
// when another system must read the message.
secret bytes $key = Core\Crypto::generateKey();
bytes $note = 'ship the order on friday' as bytes;
bytes $fast = Core\Crypto::seal($note, $key, Core\Crypto::Cipher::XChaCha20Poly1305);
bytes $interop = Core\Crypto::seal($note, $key, Core\Crypto::Cipher::Aes256Gcm);
// Each cipher puts a nonce (a random value used once) before the message. It is
// 24 bytes for XChaCha20Poly1305 and 12 for Aes256Gcm. Both add a 16-byte tag.
echo 'message ', Core\Bytes::length($note), ' octets', "\n";
echo 'XChaCha20Poly1305 ', Core\Bytes::length($fast), ' octets', "\n";
echo 'Aes256Gcm ', Core\Bytes::length($interop), ' octets', "\n";
// `Core\Crypto::open` needs the same case again. It returns the original note.
echo Core\Encoding::decodeText(
Core\Crypto::open($fast, $key, Core\Crypto::Cipher::XChaCha20Poly1305),
Core\Charset::Utf8,
), "\n";
echo Core\Encoding::decodeText(
Core\Crypto::open($interop, $key, Core\Crypto::Cipher::Aes256Gcm),
Core\Charset::Utf8,
), "\n";
Output
message 24 octets
XChaCha20Poly1305 64 octets
Aes256Gcm 52 octets
ship the order on friday
ship the order on friday

A sealed message carries no label

<?nvs
// A sealed message does not say which cipher sealed it. Opening it with the other
// cipher throws the same error as a changed message. The program that seals a
// message must remember its cipher.
secret bytes $key = Core\Crypto::generateKey();
bytes $sealed = Core\Crypto::seal('card ending 4242' as bytes, $key, Core\Crypto::Cipher::Aes256Gcm);
try {
bytes $opened = Core\Crypto::open($sealed, $key, Core\Crypto::Cipher::XChaCha20Poly1305);
echo 'opened under the other case', "\n";
} catch (RuntimeError $refused) {
echo $refused->message, "\n";
}
// A wrong key gives the same error, so an attacker cannot tell which check
// failed. Opening with the right cipher returns the original text.
echo Core\Encoding::decodeText(
Core\Crypto::open($sealed, $key, Core\Crypto::Cipher::Aes256Gcm),
Core\Charset::Utf8,
), "\n";
Output
Core\Crypto::open(): $sealed is not an authentic message under $key — it has been altered, it is too short to be one, or the key is not the one it was sealed under
card ending 4242

Keeping the case beside the ciphertext

<?nvs
// A store keeps the case beside each row it seals. Values a browser has to
// read are sealed under the interop case; everything else takes the wider
// nonce. Because the case is stored, a row sealed today still opens after the
// program starts sealing new rows under the other one.
class Vault
{
// The one place the program picks a case. If that choice ever moves, this
// is the line that moves.
public static function cipherFor(string $reader): Core\Crypto::Cipher
{
return $reader == 'browser'
? Core\Crypto::Cipher::Aes256Gcm
: Core\Crypto::Cipher::XChaCha20Poly1305;
}
}
secret bytes $key = Core\Crypto::generateKey();
array<string> $readers = ['browser', 'job runner', 'browser'];
array<string> $values = ['session=a41f', 'retry after 30s', 'cart holds 8 items'];
array<bytes> $bodies = [];
array<Core\Crypto::Cipher> $cases = [];
foreach (Core\Arr::range(0, 2) as int $row) {
Core\Crypto::Cipher $cipher = Vault::cipherFor($readers[$row]);
$bodies[] = Core\Crypto::seal($values[$row] as bytes, $key, $cipher);
$cases[] = $cipher;
}
// Reading them back, in another process and with nothing to guess: the row
// says which case opens it.
foreach (Core\Arr::range(0, 2) as int $row) {
echo $readers[$row], ' -> ', Core\Encoding::decodeText(
Core\Crypto::open($bodies[$row], $key, $cases[$row]),
Core\Charset::Utf8,
), "\n";
}
Output
browser -> session=a41f
job runner -> retry after 30s
browser -> cart holds 8 items