You are here

public static function ParagonIE_Sodium_Compat::crypto_kx in Automatic Updates 7

Same name and namespace in other branches
  1. 8 vendor/paragonie/sodium_compat/src/Compat.php \ParagonIE_Sodium_Compat::crypto_kx()

Perform a key exchange, between a designated client and a server.

Typically, you would designate one machine to be the client and the other to be the server. The first two keys are what you'd expect for scalarmult() below, but the latter two public keys don't swap places.

| ALICE | BOB | | Client | Server | |--------------------------------|-------------------------------------| | shared = crypto_kx( | shared = crypto_kx( | | alice_sk, | bob_sk, | <- contextual | bob_pk, | alice_pk, | <- contextual | alice_pk, | alice_pk, | <----- static | bob_pk | bob_pk | <----- static | ) | ) |

They are used along with the scalarmult product to generate a 256-bit BLAKE2b hash unique to the client and server keys.

@psalm-suppress MixedArgument

Parameters

string $my_secret:

string $their_public:

string $client_public:

string $server_public:

Return value

string

Throws

SodiumException

TypeError

2 calls to ParagonIE_Sodium_Compat::crypto_kx()
php72compat.php in vendor/paragonie/sodium_compat/lib/php72compat.php
sodium_compat.php in vendor/paragonie/sodium_compat/lib/sodium_compat.php

File

vendor/paragonie/sodium_compat/src/Compat.php, line 1696

Class

ParagonIE_Sodium_Compat

Code

public static function crypto_kx($my_secret, $their_public, $client_public, $server_public) {

  /* Type checks: */
  ParagonIE_Sodium_Core_Util::declareScalarType($my_secret, 'string', 1);
  ParagonIE_Sodium_Core_Util::declareScalarType($their_public, 'string', 2);
  ParagonIE_Sodium_Core_Util::declareScalarType($client_public, 'string', 3);
  ParagonIE_Sodium_Core_Util::declareScalarType($server_public, 'string', 4);

  /* Input validation: */
  if (ParagonIE_Sodium_Core_Util::strlen($my_secret) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
    throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
  }
  if (ParagonIE_Sodium_Core_Util::strlen($their_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
    throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
  }
  if (ParagonIE_Sodium_Core_Util::strlen($client_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
    throw new SodiumException('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
  }
  if (ParagonIE_Sodium_Core_Util::strlen($server_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
    throw new SodiumException('Argument 4 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
  }
  if (self::useNewSodiumAPI()) {
    if (is_callable('sodium_crypto_kx')) {
      return (string) sodium_crypto_kx($my_secret, $their_public, $client_public, $server_public);
    }
  }
  if (self::use_fallback('crypto_kx')) {
    return (string) call_user_func('\\Sodium\\crypto_kx', $my_secret, $their_public, $client_public, $server_public);
  }
  if (PHP_INT_SIZE === 4) {
    return ParagonIE_Sodium_Crypto32::keyExchange($my_secret, $their_public, $client_public, $server_public);
  }
  return ParagonIE_Sodium_Crypto::keyExchange($my_secret, $their_public, $client_public, $server_public);
}