You are here

public static function ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_encrypt in Automatic Updates 7

Same name and namespace in other branches
  1. 8 vendor/paragonie/sodium_compat/src/Crypto.php \ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_encrypt()

AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)

@internal Do not use this directly. Use ParagonIE_Sodium_Compat.

Parameters

string $message:

string $ad:

string $nonce:

string $key:

Return value

string

Throws

SodiumException

TypeError

2 calls to ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_encrypt()
ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt in vendor/paragonie/sodium_compat/src/Compat.php
Authenticated Encryption with Associated Data
ParagonIE_Sodium_Crypto::aead_xchacha20poly1305_ietf_encrypt in vendor/paragonie/sodium_compat/src/Crypto.php
AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)

File

vendor/paragonie/sodium_compat/src/Crypto.php, line 274

Class

ParagonIE_Sodium_Crypto
Class ParagonIE_Sodium_Crypto

Code

public static function aead_chacha20poly1305_ietf_encrypt($message = '', $ad = '', $nonce = '', $key = '') {

  /** @var int $len - Length of the plaintext message */
  $len = ParagonIE_Sodium_Core_Util::strlen($message);

  /** @var int $adlen - Length of the associated data */
  $adlen = ParagonIE_Sodium_Core_Util::strlen($ad);

  /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
  $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $nonce, $key);
  $state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
  try {
    ParagonIE_Sodium_Compat::memzero($block0);
  } catch (SodiumException $ex) {
    $block0 = null;
  }

  /** @var string $ciphertext - Raw encrypted data */
  $ciphertext = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc($message, $nonce, $key, ParagonIE_Sodium_Core_Util::store64_le(1));
  $state
    ->update($ad);
  $state
    ->update(str_repeat("\0", 0x10 - $adlen & 0xf));
  $state
    ->update($ciphertext);
  $state
    ->update(str_repeat("\0", 0x10 - $len & 0xf));
  $state
    ->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
  $state
    ->update(ParagonIE_Sodium_Core_Util::store64_le($len));
  return $ciphertext . $state
    ->finish();
}