Core\Crypto::expandKey
Makes a key from material that is already random, such as a shared secret or a root key held by a service.
You give it three things: the material, a salt, and a short text that names what the key is for. The salt may be empty and is not a secret. The text is what keeps two keys apart: the same material with the text "messages" and with the text "file names" gives two keys that have nothing to do with each other. That is how one secret can protect several things at once, and it is better than using the secret itself twice.
The result is 32 bytes, ready for Core\Crypto::seal. The same three arguments always give the same key.
Good to know: the material has to be random already. A password is not, however long it is. Use Core\Crypto::deriveKey for that, which stretches it first.
Parameters
$materialsecret bytesThe secret to expand. It must already be uniformly random. For a password, usederiveKey.$saltbytesThe salt of the extract step. It is not a secret, and it can be empty. An empty salt is the default of RFC 5869, a salt of zero bytes.$infostringThe context string. Two calls with the same$materialand a different$infogive unrelated keys. So one secret can give keys for two uses.
Return value
Returns secret bytes. 32 bytes as secret bytes, the key size that seal and open use. The same three arguments always give the same key.
Examples
Make a key from a shared secret
<?nvs// Shows the usual job: two sides reach a shared secret, then each side turns it// into a key and uses that key to send a message.
Core\Crypto\KeyPair $mine = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);Core\Crypto\KeyPair $theirs = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
// The salt may be empty. The text names what the key is for.secret bytes $key = Core\Crypto::expandKey( Core\Crypto::agree($mine, $theirs->publicKey()), '' as bytes, 'chat v1',);bytes $sealed = Core\Crypto::seal('see you at six' as bytes, $key, Core\Crypto::Cipher::XChaCha20Poly1305);
// The other side reaches the same secret and uses the same text, so it builds// the same key. This prints the message.secret bytes $peerKey = Core\Crypto::expandKey( Core\Crypto::agree($theirs, $mine->publicKey()), '' as bytes, 'chat v1',);echo Core\Crypto::open($sealed, $peerKey, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";see you at sixOne secret two keys
<?nvs// Shows how one secret gives several keys. A connection often wants one key for// each direction, so a message the client sent cannot be replayed back at it.
secret bytes $shared = Core\Crypto::generateKey();bytes $salt = Core\Random::bytes(16);
secret bytes $toServer = Core\Crypto::expandKey($shared, $salt, 'client to server');secret bytes $toClient = Core\Crypto::expandKey($shared, $salt, 'server to client');
// The two keys have nothing to do with each other. This prints// `two keys from one secret: yes`.echo 'two keys from one secret: ', ($toServer != $toClient ? 'yes' : 'no'), "\n";
bytes $sealed = Core\Crypto::seal('get /orders' as bytes, $toServer, Core\Crypto::Cipher::XChaCha20Poly1305);
// The key for the other direction does not open it, and `Core\Crypto::open`// throws instead of returning something wrong.try { Core\Crypto::open($sealed, $toClient, Core\Crypto::Cipher::XChaCha20Poly1305); echo 'the wrong key opened it', "\n";} catch (RuntimeError $e) { echo 'the wrong key did not open it', "\n";}two keys from one secret: yesthe wrong key did not open itOne root key per customer
<?nvs// Shows a service that holds one root key and gives every customer a key of// their own. Nothing extra is stored: a customer's key is made again from the// root key and their name whenever it is needed.
secret bytes $root = Core\Crypto::generateKey();bytes $salt = Core\Random::bytes(16);
array<string> $customers = ['acme', 'globex'];foreach ($customers as string $customer) { secret bytes $key = Core\Crypto::expandKey($root, $salt, 'customer ' . $customer); bytes $sealed = Core\Crypto::seal( ('invoice for ' . $customer) as bytes, $key, Core\Crypto::Cipher::XChaCha20Poly1305, );
// Later, with only the root key and the customer's name, the same key comes // back. This prints one line per customer. secret bytes $again = Core\Crypto::expandKey($root, $salt, 'customer ' . $customer); echo Core\Crypto::open($sealed, $again, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";}
// One customer's key does not open another customer's data, so a key that leaks// costs you that customer and no other.secret bytes $acme = Core\Crypto::expandKey($root, $salt, 'customer acme');bytes $forGlobex = Core\Crypto::seal( 'invoice for globex' as bytes, Core\Crypto::expandKey($root, $salt, 'customer globex'), Core\Crypto::Cipher::XChaCha20Poly1305,);try { Core\Crypto::open($forGlobex, $acme, Core\Crypto::Cipher::XChaCha20Poly1305); echo "one key opened another customer's data", "\n";} catch (RuntimeError $e) { echo "one key opens one customer's data only", "\n";}invoice for acmeinvoice for globexone key opens one customer's data only