You are here

public static function ParagonIE_Sodium_Compat::crypto_scalarmult in Automatic Updates 7

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

Calculate the shared secret between your secret key and your recipient's public key.

Algorithm: X25519 (ECDH over Curve25519)

@psalm-suppress MixedArgument

Parameters

string $secretKey:

string $publicKey:

Return value

string

Throws

SodiumException

TypeError

5 calls to ParagonIE_Sodium_Compat::crypto_scalarmult()
ParagonIE_Sodium_Compat::crypto_kx_client_session_keys in vendor/paragonie/sodium_compat/src/Compat.php
ParagonIE_Sodium_Compat::crypto_kx_server_session_keys in vendor/paragonie/sodium_compat/src/Compat.php
ParagonIE_Sodium_Crypto::keyExchange in vendor/paragonie/sodium_compat/src/Crypto.php
Libsodium's 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 2174

Class

ParagonIE_Sodium_Compat

Code

public static function crypto_scalarmult($secretKey, $publicKey) {

  /* Type checks: */
  ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
  ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);

  /* Input validation: */
  if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
    throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
  }
  if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
    throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
  }
  if (self::useNewSodiumAPI()) {
    return sodium_crypto_scalarmult($secretKey, $publicKey);
  }
  if (self::use_fallback('crypto_scalarmult')) {
    return (string) call_user_func('\\Sodium\\crypto_scalarmult', $secretKey, $publicKey);
  }

  /* Output validation: Forbid all-zero keys */
  if (ParagonIE_Sodium_Core_Util::hashEquals($secretKey, str_repeat("\0", self::CRYPTO_BOX_SECRETKEYBYTES))) {
    throw new SodiumException('Zero secret key is not allowed');
  }
  if (ParagonIE_Sodium_Core_Util::hashEquals($publicKey, str_repeat("\0", self::CRYPTO_BOX_PUBLICKEYBYTES))) {
    throw new SodiumException('Zero public key is not allowed');
  }
  if (PHP_INT_SIZE === 4) {
    return ParagonIE_Sodium_Crypto32::scalarmult($secretKey, $publicKey);
  }
  return ParagonIE_Sodium_Crypto::scalarmult($secretKey, $publicKey);
}