Skip to content

Core\Crypto::open

Checks a sealed message and gives back what was sealed, or throws an error.

Core\Crypto::open is the other half of Core\Crypto::seal. You give it the sealed bytes, the same 32 byte key, and the same cipher. There is no IV to pass back in, because the nonce travels in front of the sealed bytes.

The check comes first. If one byte of the sealed message has been changed, if it is too short, if the key is not the one that sealed it, or if the cipher is not the one that sealed it, the member throws a RuntimeError. It never gives back a half-decrypted message, and the error is the same sentence in all four cases. Telling them apart would tell a forger which part of the guess was right.

So a sealed message can travel anywhere. A program that opens one knows the bytes are exactly the bytes somebody sealed under that key.

Parameters

  • $sealedbytesA message seal produced.
  • $keysecret bytesThe 32-byte key that $sealed was sealed with.
  • $cipherCore\Crypto::CipherThe cipher that $sealed was sealed with. With the other cipher, the message throws the same error as a forged message. You never get wrong bytes.

Return value

Returns bytes. The original plaintext, byte for byte.

Errors

  • LogicError$key is not 32 bytes long, so it is not a key.
  • RuntimeError$sealed is not an authentic message for $key and $cipher. It was changed, it is too short, or the key or the cipher is wrong. All these cases have the same message. A different message would tell an attacker which part of a forgery worked.

Examples

A changed byte is refused

<?nvs
// Shows what `Core\Crypto::open` does when the sealed bytes have been changed
// on the way. It throws, and no part of the message comes back.
secret bytes $key = Core\Crypto::generateKey();
bytes $sealed = Core\Crypto::seal(
'pay 100 to Ada' as bytes,
$key,
Core\Crypto::Cipher::XChaCha20Poly1305,
);
// Somebody adds 1 to the last byte. `% 256` keeps it a byte, so 255 becomes 0.
int $length = Core\Bytes::length($sealed) as int;
uint $last = Core\Bytes::at($sealed, -1);
bytes $changed = Core\Bytes::join(
[Core\Bytes::slice($sealed, 0, $length - 1), Core\Bytes::fill(1, ($last + 1) % 256)],
'' as bytes,
);
// The message is now 1 byte different, and it is refused. This prints
// `the message was changed`.
try {
echo Core\Crypto::open($changed, $key, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";
} catch (RuntimeError $e) {
echo 'the message was changed', "\n";
}
// The untouched bytes still open. This prints the message.
echo Core\Crypto::open($sealed, $key, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";
Output
the message was changed
pay 100 to Ada

The cipher must be the one that sealed it

<?nvs
// Shows that the key and the cipher used to open a message must be the ones
// that sealed it. Both mistakes give the same error, so an attacker learns
// nothing from it.
secret bytes $key = Core\Crypto::generateKey();
secret bytes $otherKey = Core\Crypto::generateKey();
bytes $sealed = Core\Crypto::seal(
'pay 100 to Ada' as bytes,
$key,
Core\Crypto::Cipher::Aes256Gcm,
);
// The right key with the other cipher. This prints `no`.
try {
Core\Crypto::open($sealed, $key, Core\Crypto::Cipher::XChaCha20Poly1305);
echo 'yes', "\n";
} catch (RuntimeError $e) {
echo 'no', "\n";
}
// The right cipher with the other key. This prints `no` as well.
try {
Core\Crypto::open($sealed, $otherKey, Core\Crypto::Cipher::Aes256Gcm);
echo 'yes', "\n";
} catch (RuntimeError $e) {
echo 'no', "\n";
}
// Both of them together. This prints the message.
echo Core\Crypto::open($sealed, $key, Core\Crypto::Cipher::Aes256Gcm) as string, "\n";
Output
no
no
pay 100 to Ada

Read a session cookie

<?nvs
// Shows how a program reads a session cookie that a browser sent back. The
// program sealed that cookie earlier. A cookie that does not open is treated
// as no cookie at all.
secret bytes $key = Core\Crypto::generateKey();
// The cookie the program handed out, as text a browser can carry.
string $cookie = Core\Encoding::toHex(Core\Crypto::seal(
'user=ada;until=2026-12-31' as bytes,
$key,
Core\Crypto::Cipher::XChaCha20Poly1305,
));
// A second cookie, made up by somebody who wants to be Ada.
string $forged = Core\Encoding::toHex(Core\Bytes::fill(65, 88));
// The first prints `signed in: user=ada;until=2026-12-31`, the second prints
// `not signed in`.
array<string> $arriving = [$cookie, $forged];
foreach ($arriving as string $sent) {
try {
bytes $opened = Core\Crypto::open(
Core\Encoding::fromHex($sent),
$key,
Core\Crypto::Cipher::XChaCha20Poly1305,
);
echo 'signed in: ', $opened as string, "\n";
} catch (RuntimeError $e) {
echo 'not signed in', "\n";
}
}
Output
signed in: user=ada;until=2026-12-31
not signed in