Skip to content

Core\Crypto::verify

Checks that a signature belongs to a public key and covers a message. It returns nothing when the check holds, and throws an error when it does not.

There is no true or false here, so there is no answer a program can forget to look at. A call that returns means this message is the one that was signed, by whoever holds the private half of this key. Put the call in a try block, and refuse the message in the catch block.

The algorithm is the kind of the key, exactly as it is for Core\Crypto::sign. Nothing that arrived with the message chooses how the message is checked.

Every way a check can fail gives the same error: a changed message, a changed signature, a signature of the wrong length, and a signature made under a different key. The error says nothing about which of them happened, because that would tell a forger where to try next.

Good to know: an X25519 key verifies nothing. That kind is for Core\Crypto::agree.

Parameters

  • $messagebytesThe bytes that the signature should cover. A tainted value is allowed, and it stays tainted. A valid signature tells you who sent the bytes. It does not tell you that their content is safe.
  • $signaturebytesThe signature as sign returns it: r ‖ s for P256, never DER.
  • $keyCore\Crypto\PublicKeyThe public key to check against, of any kind but X25519.

Return value

Returns void. Nothing. For a valid signature, it returns. For a wrong one, it throws an error. So there is no false result that a == check could read wrongly.

Errors

  • RuntimeErrorThe signature is not by this key for this message. It was changed, it has the wrong length for the algorithm of the key, or another key made it. All these cases have the same message, so the error does not say which part was wrong.
  • LogicError$key is an X25519 key, which cannot verify. It is your program's own key, not data you received, so this is a bug in your program.

Examples

A check that holds and one that does not

<?nvs
// Shows what this member does when a signature is right and when it is wrong.
// It returns nothing, so the error is the whole answer.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
bytes $message = 'meeting at 14:00' as bytes;
bytes $signature = Core\Crypto::sign($message, $pair);
// The check holds, so this line returns and the next one runs.
Core\Crypto::verify($message, $signature, $pair->publicKey());
echo 'the message is the one that was signed', "\n";
// A message somebody changed on the way. Handle the error in a `catch` block
// and refuse the message there.
try {
Core\Crypto::verify('meeting at 15:00' as bytes, $signature, $pair->publicKey());
echo 'the changed message passed', "\n";
} catch (Throwable $e) {
echo 'the changed message was refused', "\n";
}
// A signature made under a different key. This is the same error, because the
// error never says which part was wrong.
Core\Crypto\KeyPair $stranger = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
try {
Core\Crypto::verify($message, Core\Crypto::sign($message, $stranger), $pair->publicKey());
echo 'the other key passed', "\n";
} catch (Throwable $e) {
echo 'the other key was refused', "\n";
}
Output
the message is the one that was signed
the changed message was refused
the other key was refused

Check with a key that arrived

<?nvs
// Shows how to check a signature with a public key that came from somewhere
// else. A key travels as bytes, and you name the kind and the format you
// expect.
Core\Crypto\KeyPair $signer = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
bytes $message = 'price list 2026-04' as bytes;
bytes $signature = Core\Crypto::sign($message, $signer);
// The two formats other programs send a public key in. `Spki` is bytes, and
// `Jwk` is JSON text that browsers and web services use.
bytes $spki = $signer->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);
bytes $jwk = $signer->publicKey()->write(Core\Crypto\PublicKey::Format::Jwk);
array<Core\Crypto\PublicKey::Format> $formats = [Core\Crypto\PublicKey::Format::Spki, Core\Crypto\PublicKey::Format::Jwk];
array<bytes> $arrived = [$spki, $jwk];
// Both are the same key, so the same signature checks out under both. This
// prints `checked out` twice.
uint $i = 0;
while ($i < 2) {
Core\Crypto\PublicKey $key = Core\Crypto\PublicKey::read(
$arrived[$i as int],
Core\Crypto\KeyKind::P256,
$formats[$i as int],
);
Core\Crypto::verify($message, $signature, $key);
echo 'checked out', "\n";
$i = $i + 1;
}
Output
checked out
checked out

Install an update only from its publisher

<?nvs
// Shows a program that accepts a file only from the one publisher it knows.
// The file and its signature arrive together, and the key was built into the
// program beforehand.
// What the publisher does, once, when it releases the file.
Core\Crypto\KeyPair $publisher = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
bytes $published = $publisher->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);
bytes $update = "print('version 3')" as bytes;
bytes $signature = Core\Crypto::sign($update, $publisher);
// What the program does with every file it downloads. The key it trusts is the
// one it was shipped with.
Core\Crypto\PublicKey $trusted = Core\Crypto\PublicKey::read(
$published,
Core\Crypto\KeyKind::Ed25519,
Core\Crypto\PublicKey::Format::Spki,
);
// Check first, use afterwards. A file that does not check out is never used.
try {
Core\Crypto::verify($update, $signature, $trusted);
echo 'installing the update', "\n";
} catch (Throwable $e) {
echo 'this update did not come from the publisher', "\n";
}
// The same program, with a file somebody replaced on the way. The signature is
// still the publisher's, but it does not cover these bytes.
bytes $swapped = "print('version 3'); steal()" as bytes;
try {
Core\Crypto::verify($swapped, $signature, $trusted);
echo 'installing the update', "\n";
} catch (Throwable $e) {
echo 'this update did not come from the publisher', "\n";
}
Output
installing the update
this update did not come from the publisher