Skip to content

Core\Crypto::agree

Produces a secret that you and another program share, from your own key pair and the public key that program sent you.

Both sides do this. You use your own private key with their public key, and they use their private key with your public key. Both sides end up with the same secret, and the secret is never sent over the network. Someone who reads both public keys on the way still cannot work out the secret.

The result is not ready to be used as a key. Give it to Core\Crypto::expandKey together with a short text that names what the key is for, such as "chat v1". That gives you a key for Core\Crypto::seal.

Good to know: both keys must be of the same kind, P256 or X25519. An Ed25519 key signs and agrees nothing, and an RSA key cannot agree at all.

Parameters

  • $mineCore\Crypto\KeyPairYour own key pair, of kind P256 or X25519. The other kinds can only sign.
  • $theirsCore\Crypto\PublicKeyThe public key of the other side. It must be of the same kind, because two keys on different curves cannot agree.

Return value

Returns secret bytes. 32 bytes as secret bytes. This is the x-coordinate for P-256 and the u-coordinate for X25519. WebCrypto's deriveBits returns the same value for the same two keys.

Errors

  • LogicErrorThe two keys cannot agree. One key is of a kind that cannot agree: Ed25519 or an RSA kind. Or the two keys are on different curves.
  • RuntimeErrorThe X25519 point of the other side adds nothing, so the secret would be all zeros for any private key. The sender chose that point. So the error is about the sender, and it is not a bug in your program.

Examples

Both sides reach one secret

<?nvs
// Shows two sides reaching the same shared secret. Both key pairs are in this
// one program. In real code each side has its own pair and sends only the
// public half of it.
Core\Crypto\KeyPair $mine = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
Core\Crypto\KeyPair $theirs = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
// Each side uses its own pair together with the other side's public key.
secret bytes $ours = Core\Crypto::agree($mine, $theirs->publicKey());
secret bytes $yours = Core\Crypto::agree($theirs, $mine->publicKey());
// The two secrets are the same. This prints `both sides reached one secret: yes`.
echo 'both sides reached one secret: ', ($ours == $yours ? 'yes' : 'no'), "\n";
Output
both sides reached one secret: yes

Read the public key a peer sent

<?nvs
// Shows how to send your public key and how to read the one a peer sent. A
// public key travels as bytes in the `Spki` format, which browsers and most
// other languages read and write too.
Core\Crypto\KeyPair $mine = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
Core\Crypto\KeyPair $theirs = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
// These are the bytes you send. A public key is safe to publish.
bytes $sent = $mine->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);
// This prints `sent 91 bytes`.
echo 'sent ', Core\Bytes::length($sent), ' bytes', "\n";
// This is what the peer does with them. You name the kind and the format,
// because the bytes alone do not say which curve you expect.
Core\Crypto\PublicKey $read = Core\Crypto\PublicKey::read(
$sent,
Core\Crypto\KeyKind::P256,
Core\Crypto\PublicKey::Format::Spki,
);
// The key the peer read reaches the same secret as the key you hold.
secret bytes $peerSide = Core\Crypto::agree($theirs, $read);
secret bytes $mySide = Core\Crypto::agree($mine, $theirs->publicKey());
echo 'one secret from both keys: ', ($peerSide == $mySide ? 'yes' : 'no'), "\n";
Output
sent 91 bytes
one secret from both keys: yes

Give every client its own key

<?nvs
// Shows a server that gives every client its own key. The server has one key
// pair. It reaches a separate secret with each client, turns that secret into a
// key, and seals a message only that client can open.
Core\Crypto\KeyPair $server = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
array<string> $clients = ['alice', 'bob'];
foreach ($clients as string $name) {
// In a real program this key arrives from the client over the network.
Core\Crypto\KeyPair $client = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
// The client's name is part of the text, so two clients never end up with
// the same key.
secret bytes $key = Core\Crypto::expandKey(
Core\Crypto::agree($server, $client->publicKey()),
'' as bytes,
'session for ' . $name,
);
bytes $sealed = Core\Crypto::seal(
('hello ' . $name) as bytes,
$key,
Core\Crypto::Cipher::XChaCha20Poly1305,
);
// The client reaches the same secret from its own side, builds the same key
// and opens the message. This prints `hello alice` and then `hello bob`.
secret bytes $clientKey = Core\Crypto::expandKey(
Core\Crypto::agree($client, $server->publicKey()),
'' as bytes,
'session for ' . $name,
);
echo Core\Crypto::open($sealed, $clientKey, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";
}
Output
hello alice
hello bob