Skip to content

Core\Crypto\KeyKind

Names which asymmetric key a call is talking about — the curve, or the RSA signing scheme.

Every member that makes, reads or uses a key pair takes one of these five cases, and there is no default. The kinds do not stand in for one another: X25519 agrees a shared secret with a peer and signs nothing, Ed25519 signs and agrees nothing, and P256 does both, which is why it is the curve an interop key is usually on. The two RSA cases are one key type under two schemes — RsaPkcs1 is JWS's RS256 and RsaPss is PS256 — and an RSA key is read from whoever issued it rather than drawn here.

In plain words: a key is not a general-purpose thing you point at any operation. What it is for is part of what it is, and the kind is how a program says so. Asking a key to do the other job is a mistake in the program, reported as one. The examples put the five cases side by side, agree a secret across two ends of one curve, and verify a partner's signature against the key on file.

Examples

What each kind is for

<?nvs
// `Core\Crypto\KeyKind` has five kinds of key. Two of them agree a shared secret
// with another program, four of them sign, and `P256` does both. Every call
// that creates or reads a key needs its kind.
class Keys
{
public static function purpose(Core\Crypto\KeyKind $kind): string
{
return match ($kind) {
Core\Crypto\KeyKind::P256 => 'P256 agrees and signs',
Core\Crypto\KeyKind::X25519 => 'X25519 agrees only',
Core\Crypto\KeyKind::Ed25519 => 'Ed25519 signs only',
Core\Crypto\KeyKind::RsaPkcs1 => 'RsaPkcs1 signs, the scheme JWS calls RS256',
Core\Crypto\KeyKind::RsaPss => 'RsaPss signs, the scheme JWS calls PS256',
};
}
}
array<Core\Crypto\KeyKind> $roster = [
Core\Crypto\KeyKind::P256,
Core\Crypto\KeyKind::X25519,
Core\Crypto\KeyKind::Ed25519,
Core\Crypto\KeyKind::RsaPkcs1,
Core\Crypto\KeyKind::RsaPss,
];
foreach ($roster as Core\Crypto\KeyKind $kind) {
echo Keys::purpose($kind), "\n";
}
// `generateKeyPair` creates a new key pair for the three curve kinds. There is
// no key size to pass, because each curve has one size.
Core\Crypto\KeyPair $signing = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
echo 'drew an Ed25519 pair', "\n";
// You get an RSA key from whoever issued it. `generateKeyPair` with an RSA
// kind throws a `LogicError`.
try {
Core\Crypto\KeyPair $never = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::RsaPkcs1);
echo 'drew an RSA pair', "\n";
} catch (LogicError $notDrawn) {
echo $notDrawn->message, "\n";
}
Output
P256 agrees and signs
X25519 agrees only
Ed25519 signs only
RsaPkcs1 signs, the scheme JWS calls RS256
RsaPss signs, the scheme JWS calls PS256
drew an Ed25519 pair
Core\Crypto::generateKeyPair(): an RSA key is never generated here. A program is handed its RSA key by whoever issued it, so read that file with `Core\Crypto\KeyPair::read`.

Two ends of one agreement

<?nvs
// Two programs agree on a shared secret. Each creates a key pair of the same
// kind and sends its public key to the other. Each then combines its own
// private key with the other public key, and both get the same secret.
Core\Crypto\KeyPair $ours = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
Core\Crypto\KeyPair $theirs = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
secret bytes $weAgreed = Core\Crypto::agree($ours, $theirs->publicKey());
secret bytes $theyAgreed = Core\Crypto::agree($theirs, $ours->publicKey());
// The shared secret is not a key yet. `expandKey` with a context string turns it
// into a key. A different context string gives a different key.
secret bytes $ourKey = Core\Crypto::expandKey($weAgreed, '' as bytes, 'orders v1');
secret bytes $theirKey = Core\Crypto::expandKey($theyAgreed, '' as bytes, 'orders v1');
// We seal with our key, and they open with theirs. This works because both
// keys are the same.
bytes $sealed = Core\Crypto::seal('order 1042 accepted' as bytes, $ourKey, Core\Crypto::Cipher::XChaCha20Poly1305);
echo Core\Encoding::decodeText(
Core\Crypto::open($sealed, $theirKey, Core\Crypto::Cipher::XChaCha20Poly1305),
Core\Charset::Utf8,
), "\n";
// An Ed25519 pair is for signing. `agree` with an Ed25519 key throws a
// `LogicError`, because both keys must be of the same curve.
Core\Crypto\KeyPair $forSigning = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
try {
secret bytes $nothing = Core\Crypto::agree($forSigning, $theirs->publicKey());
echo 'agreed with a signing key', "\n";
} catch (LogicError $wrongKind) {
echo $wrongKind->message, "\n";
}
Output
order 1042 accepted
Core\Crypto::agree(): these two keys are not two ends of one agreement. Both have to be of one curve, `P256` or `X25519` — `Ed25519` signs and agrees nothing, and neither RSA kind agrees at all.

Verifying a partner webhook

<?nvs
// A partner signs the webhooks it sends. This program checks the signature
// before it uses the body. The key kind is stored with the key in the
// configuration, because the same RSA key can be RS256 or PS256.
// The partner's side. In a real system, this is another program on another machine.
Core\Crypto\KeyPair $partner = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
bytes $body = "{\"event\":\"invoice.paid\",\"id\":\"in_88\"}" as bytes;
bytes $signature = Core\Crypto::sign($body, $partner);
// Our side. The key bytes and the key kind come from the configuration.
// `PublicKey::read` checks that the bytes are a valid key of that kind.
Core\Crypto\KeyKind $kind = Core\Crypto\KeyKind::Ed25519;
Core\Crypto\PublicKey $onFile = Core\Crypto\PublicKey::read(
$partner->publicKey()->write(Core\Crypto\PublicKey::Format::Spki),
$kind,
Core\Crypto\PublicKey::Format::Spki,
);
try {
Core\Crypto::verify($body, $signature, $onFile);
echo 'the webhook is from the partner', "\n";
} catch (RuntimeError $notTheirs) {
echo 'not from the partner', "\n";
}
// The same signature with a changed body. `verify` throws a `RuntimeError`.
bytes $altered = "{\"event\":\"invoice.paid\",\"id\":\"in_99\"}" as bytes;
try {
Core\Crypto::verify($altered, $signature, $onFile);
echo 'accepted an altered body', "\n";
} catch (RuntimeError $refused) {
echo 'refused an altered body', "\n";
}
// `kind()` returns the kind the key was read as. A program with keys of several
// kinds can use it to pick the right operation for each key.
if ($onFile->kind() == Core\Crypto\KeyKind::Ed25519) {
echo 'the key on file signs, and agrees nothing', "\n";
}
Output
the webhook is from the partner
refused an altered body
the key on file signs, and agrees nothing