Skip to content

Core\Jwe\Key::own

Makes a Core\Jwe\Key from your own key pair. Core\Jwe::decrypt uses this key to read messages that other people encrypted for your public key with Core\Jwe\Key::recipient. Only the key pair that belongs to that public key can read them.

The key pair must be a P256 or an X25519 key pair. An Ed25519 or RSA key pair throws a LogicError, because these keys cannot make a shared secret.

Core\Jwe::encrypt also accepts this key. The result is a message that only this same key pair can read.

Keep the key pair secret. $pair->write() returns its bytes, so a server can save the key pair and read it again with Core\Crypto\KeyPair::read.

The examples below decrypt a message sent to a key pair, show the error for a key pair that can only sign, and show a server that saves its key pair and reads it again for each request.

Parameters

  • $keyCore\Crypto\KeyPairA P256 or X25519 key pair, read with Core\Crypto\KeyPair::read.

Return value

Returns Core\Jwe\Key. A key that decrypt uses with the epk of the token. With this key, encrypt encrypts to the pair's own public key.

Errors

  • LogicError$key is an Ed25519 or RSA pair. Neither can be used for key agreement.

Examples

Basics

<?nvs
// Shows how a key pair decrypts messages that two senders encrypted for its public key.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
Core\Jwe\Key $forMe = Core\Jwe\Key::recipient($pair->publicKey());
string $fromAnna = Core\Jwe::encrypt('hi from Anna', $forMe);
string $fromBen = Core\Jwe::encrypt('hi from Ben', $forMe);
// One key made with `own` decrypts both messages.
// This prints `hi from Anna` and `hi from Ben`.
Core\Jwe\Key $mine = Core\Jwe\Key::own($pair);
echo Core\Jwe::decrypt($fromAnna, [$mine]), "\n";
echo Core\Jwe::decrypt($fromBen, [$mine]), "\n";
Output
hi from Anna
hi from Ben

A signing key pair is not allowed

<?nvs
// Shows the error for a key pair that can only sign.
// An `Ed25519` key pair makes signatures. It cannot make a shared secret,
// so `Core\Jwe\Key::own` throws a `LogicError`.
Core\Crypto\KeyPair $signing = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
try {
Core\Jwe\Key::own($signing);
} catch (LogicError $error) {
echo $error->message, "\n";
}
// An `X25519` key pair is allowed. This prints `ok`.
Core\Jwe\Key::own(Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519));
echo 'ok', "\n";
Output
Core\Jwe\Key::own(): ECDH-ES agrees over P-256 and X25519, and this key is neither. An Ed25519 key signs and an RSA key has no agreement at all, so there is no shared secret for a content key to come out of.
ok

A server reads its saved key pair

<?nvs
// Shows a server that saves its key pair once and reads it again for each request.
class Server
{
// Each request reads the saved key pair and decrypts the message in the request.
public static function handle(string $token, secret bytes $saved): tainted string
{
Core\Crypto\KeyPair $pair = Core\Crypto\KeyPair::read($saved, Core\Crypto\KeyKind::X25519);
return Core\Jwe::decrypt($token, [Core\Jwe\Key::own($pair)]);
}
}
// At setup, the server makes a key pair and saves its bytes, for example in a secret store.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
secret bytes $saved = $pair->write();
// A client encrypts two messages for the server's public key.
Core\Jwe\Key $server = Core\Jwe\Key::recipient($pair->publicKey());
string $first = Core\Jwe::encrypt('add 2 x red mug', $server);
string $second = Core\Jwe::encrypt('remove 1 x red mug', $server);
// This prints `add 2 x red mug` and `remove 1 x red mug`.
echo Server::handle($first, $saved), "\n";
echo Server::handle($second, $saved), "\n";
Output
add 2 x red mug
remove 1 x red mug