Edit File by line
/home/zeestwma/richards.../wp-inclu.../sodium_c.../src
File: Compat.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Libsodium compatibility layer
[3] Fix | Delete
*
[4] Fix | Delete
* This is the only class you should be interfacing with, as a user of
[5] Fix | Delete
* sodium_compat.
[6] Fix | Delete
*
[7] Fix | Delete
* If the PHP extension for libsodium is installed, it will always use that
[8] Fix | Delete
* instead of our implementations. You get better performance and stronger
[9] Fix | Delete
* guarantees against side-channels that way.
[10] Fix | Delete
*
[11] Fix | Delete
* However, if your users don't have the PHP extension installed, we offer a
[12] Fix | Delete
* compatible interface here. It will give you the correct results as if the
[13] Fix | Delete
* PHP extension was installed. It won't be as fast, of course.
[14] Fix | Delete
*
[15] Fix | Delete
* CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION *
[16] Fix | Delete
* *
[17] Fix | Delete
* Until audited, this is probably not safe to use! DANGER WILL ROBINSON *
[18] Fix | Delete
* *
[19] Fix | Delete
* CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION *
[20] Fix | Delete
*/
[21] Fix | Delete
[22] Fix | Delete
if (class_exists('ParagonIE_Sodium_Compat', false)) {
[23] Fix | Delete
return;
[24] Fix | Delete
}
[25] Fix | Delete
[26] Fix | Delete
class ParagonIE_Sodium_Compat
[27] Fix | Delete
{
[28] Fix | Delete
/**
[29] Fix | Delete
* This parameter prevents the use of the PECL extension.
[30] Fix | Delete
* It should only be used for unit testing.
[31] Fix | Delete
*
[32] Fix | Delete
* @var bool
[33] Fix | Delete
*/
[34] Fix | Delete
public static $disableFallbackForUnitTests = false;
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Use fast multiplication rather than our constant-time multiplication
[38] Fix | Delete
* implementation. Can be enabled at runtime. Only enable this if you
[39] Fix | Delete
* are absolutely certain that there is no timing leak on your platform.
[40] Fix | Delete
*
[41] Fix | Delete
* @var bool
[42] Fix | Delete
*/
[43] Fix | Delete
public static $fastMult = false;
[44] Fix | Delete
[45] Fix | Delete
const LIBRARY_MAJOR_VERSION = 9;
[46] Fix | Delete
const LIBRARY_MINOR_VERSION = 1;
[47] Fix | Delete
const LIBRARY_VERSION_MAJOR = 9;
[48] Fix | Delete
const LIBRARY_VERSION_MINOR = 1;
[49] Fix | Delete
const VERSION_STRING = 'polyfill-1.0.8';
[50] Fix | Delete
[51] Fix | Delete
// From libsodium
[52] Fix | Delete
const BASE64_VARIANT_ORIGINAL = 1;
[53] Fix | Delete
const BASE64_VARIANT_ORIGINAL_NO_PADDING = 3;
[54] Fix | Delete
const BASE64_VARIANT_URLSAFE = 5;
[55] Fix | Delete
const BASE64_VARIANT_URLSAFE_NO_PADDING = 7;
[56] Fix | Delete
const CRYPTO_AEAD_AES256GCM_KEYBYTES = 32;
[57] Fix | Delete
const CRYPTO_AEAD_AES256GCM_NSECBYTES = 0;
[58] Fix | Delete
const CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12;
[59] Fix | Delete
const CRYPTO_AEAD_AES256GCM_ABYTES = 16;
[60] Fix | Delete
const CRYPTO_AEAD_AEGIS128L_KEYBYTES = 16;
[61] Fix | Delete
const CRYPTO_AEAD_AEGIS128L_NSECBYTES = 0;
[62] Fix | Delete
const CRYPTO_AEAD_AEGIS128L_NPUBBYTES = 16;
[63] Fix | Delete
const CRYPTO_AEAD_AEGIS128L_ABYTES = 32;
[64] Fix | Delete
const CRYPTO_AEAD_AEGIS256_KEYBYTES = 32;
[65] Fix | Delete
const CRYPTO_AEAD_AEGIS256_NSECBYTES = 0;
[66] Fix | Delete
const CRYPTO_AEAD_AEGIS256_NPUBBYTES = 32;
[67] Fix | Delete
const CRYPTO_AEAD_AEGIS256_ABYTES = 32;
[68] Fix | Delete
const CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32;
[69] Fix | Delete
const CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0;
[70] Fix | Delete
const CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8;
[71] Fix | Delete
const CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16;
[72] Fix | Delete
const CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32;
[73] Fix | Delete
const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = 0;
[74] Fix | Delete
const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12;
[75] Fix | Delete
const CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = 16;
[76] Fix | Delete
const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES = 32;
[77] Fix | Delete
const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES = 0;
[78] Fix | Delete
const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES = 24;
[79] Fix | Delete
const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES = 16;
[80] Fix | Delete
const CRYPTO_AUTH_BYTES = 32;
[81] Fix | Delete
const CRYPTO_AUTH_KEYBYTES = 32;
[82] Fix | Delete
const CRYPTO_BOX_SEALBYTES = 16;
[83] Fix | Delete
const CRYPTO_BOX_SECRETKEYBYTES = 32;
[84] Fix | Delete
const CRYPTO_BOX_PUBLICKEYBYTES = 32;
[85] Fix | Delete
const CRYPTO_BOX_KEYPAIRBYTES = 64;
[86] Fix | Delete
const CRYPTO_BOX_MACBYTES = 16;
[87] Fix | Delete
const CRYPTO_BOX_NONCEBYTES = 24;
[88] Fix | Delete
const CRYPTO_BOX_SEEDBYTES = 32;
[89] Fix | Delete
const CRYPTO_CORE_RISTRETTO255_BYTES = 32;
[90] Fix | Delete
const CRYPTO_CORE_RISTRETTO255_SCALARBYTES = 32;
[91] Fix | Delete
const CRYPTO_CORE_RISTRETTO255_HASHBYTES = 64;
[92] Fix | Delete
const CRYPTO_CORE_RISTRETTO255_NONREDUCEDSCALARBYTES = 64;
[93] Fix | Delete
const CRYPTO_KDF_BYTES_MIN = 16;
[94] Fix | Delete
const CRYPTO_KDF_BYTES_MAX = 64;
[95] Fix | Delete
const CRYPTO_KDF_CONTEXTBYTES = 8;
[96] Fix | Delete
const CRYPTO_KDF_KEYBYTES = 32;
[97] Fix | Delete
const CRYPTO_KX_BYTES = 32;
[98] Fix | Delete
const CRYPTO_KX_PRIMITIVE = 'x25519blake2b';
[99] Fix | Delete
const CRYPTO_KX_SEEDBYTES = 32;
[100] Fix | Delete
const CRYPTO_KX_KEYPAIRBYTES = 64;
[101] Fix | Delete
const CRYPTO_KX_PUBLICKEYBYTES = 32;
[102] Fix | Delete
const CRYPTO_KX_SECRETKEYBYTES = 32;
[103] Fix | Delete
const CRYPTO_KX_SESSIONKEYBYTES = 32;
[104] Fix | Delete
const CRYPTO_GENERICHASH_BYTES = 32;
[105] Fix | Delete
const CRYPTO_GENERICHASH_BYTES_MIN = 16;
[106] Fix | Delete
const CRYPTO_GENERICHASH_BYTES_MAX = 64;
[107] Fix | Delete
const CRYPTO_GENERICHASH_KEYBYTES = 32;
[108] Fix | Delete
const CRYPTO_GENERICHASH_KEYBYTES_MIN = 16;
[109] Fix | Delete
const CRYPTO_GENERICHASH_KEYBYTES_MAX = 64;
[110] Fix | Delete
const CRYPTO_PWHASH_SALTBYTES = 16;
[111] Fix | Delete
const CRYPTO_PWHASH_STRPREFIX = '$argon2id$';
[112] Fix | Delete
const CRYPTO_PWHASH_ALG_ARGON2I13 = 1;
[113] Fix | Delete
const CRYPTO_PWHASH_ALG_ARGON2ID13 = 2;
[114] Fix | Delete
const CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 33554432;
[115] Fix | Delete
const CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 4;
[116] Fix | Delete
const CRYPTO_PWHASH_MEMLIMIT_MODERATE = 134217728;
[117] Fix | Delete
const CRYPTO_PWHASH_OPSLIMIT_MODERATE = 6;
[118] Fix | Delete
const CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 536870912;
[119] Fix | Delete
const CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 8;
[120] Fix | Delete
const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES = 32;
[121] Fix | Delete
const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX = '$7$';
[122] Fix | Delete
const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE = 534288;
[123] Fix | Delete
const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE = 16777216;
[124] Fix | Delete
const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE = 33554432;
[125] Fix | Delete
const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE = 1073741824;
[126] Fix | Delete
const CRYPTO_SCALARMULT_BYTES = 32;
[127] Fix | Delete
const CRYPTO_SCALARMULT_SCALARBYTES = 32;
[128] Fix | Delete
const CRYPTO_SCALARMULT_RISTRETTO255_BYTES = 32;
[129] Fix | Delete
const CRYPTO_SCALARMULT_RISTRETTO255_SCALARBYTES = 32;
[130] Fix | Delete
const CRYPTO_SHORTHASH_BYTES = 8;
[131] Fix | Delete
const CRYPTO_SHORTHASH_KEYBYTES = 16;
[132] Fix | Delete
const CRYPTO_SECRETBOX_KEYBYTES = 32;
[133] Fix | Delete
const CRYPTO_SECRETBOX_MACBYTES = 16;
[134] Fix | Delete
const CRYPTO_SECRETBOX_NONCEBYTES = 24;
[135] Fix | Delete
const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES = 17;
[136] Fix | Delete
const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES = 24;
[137] Fix | Delete
const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES = 32;
[138] Fix | Delete
const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH = 0;
[139] Fix | Delete
const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL = 1;
[140] Fix | Delete
const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY = 2;
[141] Fix | Delete
const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL = 3;
[142] Fix | Delete
const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX = 0x3fffffff80;
[143] Fix | Delete
const CRYPTO_SIGN_BYTES = 64;
[144] Fix | Delete
const CRYPTO_SIGN_SEEDBYTES = 32;
[145] Fix | Delete
const CRYPTO_SIGN_PUBLICKEYBYTES = 32;
[146] Fix | Delete
const CRYPTO_SIGN_SECRETKEYBYTES = 64;
[147] Fix | Delete
const CRYPTO_SIGN_KEYPAIRBYTES = 96;
[148] Fix | Delete
const CRYPTO_STREAM_KEYBYTES = 32;
[149] Fix | Delete
const CRYPTO_STREAM_NONCEBYTES = 24;
[150] Fix | Delete
const CRYPTO_STREAM_XCHACHA20_KEYBYTES = 32;
[151] Fix | Delete
const CRYPTO_STREAM_XCHACHA20_NONCEBYTES = 24;
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Add two numbers (little-endian unsigned), storing the value in the first
[155] Fix | Delete
* parameter.
[156] Fix | Delete
*
[157] Fix | Delete
* This mutates $val.
[158] Fix | Delete
*
[159] Fix | Delete
* @param string $val
[160] Fix | Delete
* @param string $addv
[161] Fix | Delete
* @return void
[162] Fix | Delete
* @throws SodiumException
[163] Fix | Delete
*/
[164] Fix | Delete
public static function add(
[165] Fix | Delete
#[\SensitiveParameter]
[166] Fix | Delete
&$val,
[167] Fix | Delete
#[\SensitiveParameter]
[168] Fix | Delete
$addv
[169] Fix | Delete
) {
[170] Fix | Delete
$val_len = ParagonIE_Sodium_Core_Util::strlen($val);
[171] Fix | Delete
$addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
[172] Fix | Delete
if ($val_len !== $addv_len) {
[173] Fix | Delete
throw new SodiumException('values must have the same length');
[174] Fix | Delete
}
[175] Fix | Delete
$A = ParagonIE_Sodium_Core_Util::stringToIntArray($val);
[176] Fix | Delete
$B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv);
[177] Fix | Delete
[178] Fix | Delete
$c = 0;
[179] Fix | Delete
for ($i = 0; $i < $val_len; $i++) {
[180] Fix | Delete
$c += ($A[$i] + $B[$i]);
[181] Fix | Delete
$A[$i] = ($c & 0xff);
[182] Fix | Delete
$c >>= 8;
[183] Fix | Delete
}
[184] Fix | Delete
$val = ParagonIE_Sodium_Core_Util::intArrayToString($A);
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* @param string $encoded
[189] Fix | Delete
* @param int $variant
[190] Fix | Delete
* @param string $ignore
[191] Fix | Delete
* @return string
[192] Fix | Delete
* @throws SodiumException
[193] Fix | Delete
*/
[194] Fix | Delete
public static function base642bin(
[195] Fix | Delete
#[\SensitiveParameter]
[196] Fix | Delete
$encoded,
[197] Fix | Delete
$variant,
[198] Fix | Delete
$ignore = ''
[199] Fix | Delete
) {
[200] Fix | Delete
/* Type checks: */
[201] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($encoded, 'string', 1);
[202] Fix | Delete
[203] Fix | Delete
/** @var string $encoded */
[204] Fix | Delete
$encoded = (string) $encoded;
[205] Fix | Delete
if (ParagonIE_Sodium_Core_Util::strlen($encoded) === 0) {
[206] Fix | Delete
return '';
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
// Just strip before decoding
[210] Fix | Delete
if (!empty($ignore)) {
[211] Fix | Delete
$encoded = str_replace($ignore, '', $encoded);
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
try {
[215] Fix | Delete
switch ($variant) {
[216] Fix | Delete
case self::BASE64_VARIANT_ORIGINAL:
[217] Fix | Delete
return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, true);
[218] Fix | Delete
case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
[219] Fix | Delete
return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, false);
[220] Fix | Delete
case self::BASE64_VARIANT_URLSAFE:
[221] Fix | Delete
return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, true);
[222] Fix | Delete
case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
[223] Fix | Delete
return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, false);
[224] Fix | Delete
default:
[225] Fix | Delete
throw new SodiumException('invalid base64 variant identifier');
[226] Fix | Delete
}
[227] Fix | Delete
} catch (Exception $ex) {
[228] Fix | Delete
if ($ex instanceof SodiumException) {
[229] Fix | Delete
throw $ex;
[230] Fix | Delete
}
[231] Fix | Delete
throw new SodiumException('invalid base64 string');
[232] Fix | Delete
}
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
/**
[236] Fix | Delete
* @param string $decoded
[237] Fix | Delete
* @param int $variant
[238] Fix | Delete
* @return string
[239] Fix | Delete
* @throws SodiumException
[240] Fix | Delete
*/
[241] Fix | Delete
public static function bin2base64(
[242] Fix | Delete
#[\SensitiveParameter]
[243] Fix | Delete
$decoded,
[244] Fix | Delete
$variant
[245] Fix | Delete
) {
[246] Fix | Delete
/* Type checks: */
[247] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($decoded, 'string', 1);
[248] Fix | Delete
/** @var string $decoded */
[249] Fix | Delete
$decoded = (string) $decoded;
[250] Fix | Delete
if (ParagonIE_Sodium_Core_Util::strlen($decoded) === 0) {
[251] Fix | Delete
return '';
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
switch ($variant) {
[255] Fix | Delete
case self::BASE64_VARIANT_ORIGINAL:
[256] Fix | Delete
return ParagonIE_Sodium_Core_Base64_Original::encode($decoded);
[257] Fix | Delete
case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
[258] Fix | Delete
return ParagonIE_Sodium_Core_Base64_Original::encodeUnpadded($decoded);
[259] Fix | Delete
case self::BASE64_VARIANT_URLSAFE:
[260] Fix | Delete
return ParagonIE_Sodium_Core_Base64_UrlSafe::encode($decoded);
[261] Fix | Delete
case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
[262] Fix | Delete
return ParagonIE_Sodium_Core_Base64_UrlSafe::encodeUnpadded($decoded);
[263] Fix | Delete
default:
[264] Fix | Delete
throw new SodiumException('invalid base64 variant identifier');
[265] Fix | Delete
}
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
/**
[269] Fix | Delete
* Cache-timing-safe implementation of bin2hex().
[270] Fix | Delete
*
[271] Fix | Delete
* @param string $string A string (probably raw binary)
[272] Fix | Delete
* @return string A hexadecimal-encoded string
[273] Fix | Delete
* @throws SodiumException
[274] Fix | Delete
* @throws TypeError
[275] Fix | Delete
* @psalm-suppress MixedArgument
[276] Fix | Delete
*/
[277] Fix | Delete
public static function bin2hex(
[278] Fix | Delete
#[\SensitiveParameter]
[279] Fix | Delete
$string
[280] Fix | Delete
) {
[281] Fix | Delete
/* Type checks: */
[282] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
[283] Fix | Delete
[284] Fix | Delete
if (self::useNewSodiumAPI()) {
[285] Fix | Delete
return (string) sodium_bin2hex($string);
[286] Fix | Delete
}
[287] Fix | Delete
if (self::use_fallback('bin2hex')) {
[288] Fix | Delete
return (string) call_user_func('\\Sodium\\bin2hex', $string);
[289] Fix | Delete
}
[290] Fix | Delete
return ParagonIE_Sodium_Core_Util::bin2hex($string);
[291] Fix | Delete
}
[292] Fix | Delete
[293] Fix | Delete
/**
[294] Fix | Delete
* Compare two strings, in constant-time.
[295] Fix | Delete
* Compared to memcmp(), compare() is more useful for sorting.
[296] Fix | Delete
*
[297] Fix | Delete
* @param string $left The left operand; must be a string
[298] Fix | Delete
* @param string $right The right operand; must be a string
[299] Fix | Delete
* @return int If < 0 if the left operand is less than the right
[300] Fix | Delete
* If = 0 if both strings are equal
[301] Fix | Delete
* If > 0 if the right operand is less than the left
[302] Fix | Delete
* @throws SodiumException
[303] Fix | Delete
* @throws TypeError
[304] Fix | Delete
* @psalm-suppress MixedArgument
[305] Fix | Delete
*/
[306] Fix | Delete
public static function compare(
[307] Fix | Delete
#[\SensitiveParameter]
[308] Fix | Delete
$left,
[309] Fix | Delete
#[\SensitiveParameter]
[310] Fix | Delete
$right
[311] Fix | Delete
) {
[312] Fix | Delete
/* Type checks: */
[313] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
[314] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
[315] Fix | Delete
[316] Fix | Delete
if (self::useNewSodiumAPI()) {
[317] Fix | Delete
return (int) sodium_compare($left, $right);
[318] Fix | Delete
}
[319] Fix | Delete
if (self::use_fallback('compare')) {
[320] Fix | Delete
return (int) call_user_func('\\Sodium\\compare', $left, $right);
[321] Fix | Delete
}
[322] Fix | Delete
return ParagonIE_Sodium_Core_Util::compare($left, $right);
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
/**
[326] Fix | Delete
* Authenticated Encryption with Associated Data: Decryption
[327] Fix | Delete
*
[328] Fix | Delete
* Algorithm:
[329] Fix | Delete
* AEGIS-128L
[330] Fix | Delete
*
[331] Fix | Delete
* @param string $ciphertext Encrypted message (with MAC appended)
[332] Fix | Delete
* @param string $assocData Authenticated Associated Data (unencrypted)
[333] Fix | Delete
* @param string $nonce Number to be used only Once; must be 32 bytes
[334] Fix | Delete
* @param string $key Encryption key
[335] Fix | Delete
*
[336] Fix | Delete
* @return string The original plaintext message
[337] Fix | Delete
* @throws SodiumException
[338] Fix | Delete
* @throws TypeError
[339] Fix | Delete
* @psalm-suppress MixedArgument
[340] Fix | Delete
* @psalm-suppress MixedInferredReturnType
[341] Fix | Delete
* @psalm-suppress MixedReturnStatement
[342] Fix | Delete
*/
[343] Fix | Delete
public static function crypto_aead_aegis128l_decrypt(
[344] Fix | Delete
$ciphertext = '',
[345] Fix | Delete
$assocData = '',
[346] Fix | Delete
$nonce = '',
[347] Fix | Delete
#[\SensitiveParameter]
[348] Fix | Delete
$key = ''
[349] Fix | Delete
) {
[350] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
[351] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
[352] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
[353] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
[354] Fix | Delete
[355] Fix | Delete
/* Input validation: */
[356] Fix | Delete
if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS128L_NPUBBYTES) {
[357] Fix | Delete
throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS_128L_NPUBBYTES long');
[358] Fix | Delete
}
[359] Fix | Delete
if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS128L_KEYBYTES) {
[360] Fix | Delete
throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long');
[361] Fix | Delete
}
[362] Fix | Delete
$ct_length = ParagonIE_Sodium_Core_Util::strlen($ciphertext);
[363] Fix | Delete
if ($ct_length < self::CRYPTO_AEAD_AEGIS128L_ABYTES) {
[364] Fix | Delete
throw new SodiumException('Message must be at least CRYPTO_AEAD_AEGIS128L_ABYTES long');
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
$ct = ParagonIE_Sodium_Core_Util::substr(
[368] Fix | Delete
$ciphertext,
[369] Fix | Delete
0,
[370] Fix | Delete
$ct_length - self::CRYPTO_AEAD_AEGIS128L_ABYTES
[371] Fix | Delete
);
[372] Fix | Delete
$tag = ParagonIE_Sodium_Core_Util::substr(
[373] Fix | Delete
$ciphertext,
[374] Fix | Delete
$ct_length - self::CRYPTO_AEAD_AEGIS128L_ABYTES,
[375] Fix | Delete
self::CRYPTO_AEAD_AEGIS128L_ABYTES
[376] Fix | Delete
);
[377] Fix | Delete
return ParagonIE_Sodium_Core_AEGIS128L::decrypt($ct, $tag, $assocData, $key, $nonce);
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
/**
[381] Fix | Delete
* Authenticated Encryption with Associated Data: Encryption
[382] Fix | Delete
*
[383] Fix | Delete
* Algorithm:
[384] Fix | Delete
* AEGIS-128L
[385] Fix | Delete
*
[386] Fix | Delete
* @param string $plaintext Message to be encrypted
[387] Fix | Delete
* @param string $assocData Authenticated Associated Data (unencrypted)
[388] Fix | Delete
* @param string $nonce Number to be used only Once; must be 32 bytes
[389] Fix | Delete
* @param string $key Encryption key
[390] Fix | Delete
*
[391] Fix | Delete
* @return string Ciphertext with 32-byte authentication tag appended
[392] Fix | Delete
* @throws SodiumException
[393] Fix | Delete
* @throws TypeError
[394] Fix | Delete
* @psalm-suppress MixedArgument
[395] Fix | Delete
*/
[396] Fix | Delete
public static function crypto_aead_aegis128l_encrypt(
[397] Fix | Delete
#[\SensitiveParameter]
[398] Fix | Delete
$plaintext = '',
[399] Fix | Delete
$assocData = '',
[400] Fix | Delete
$nonce = '',
[401] Fix | Delete
#[\SensitiveParameter]
[402] Fix | Delete
$key = ''
[403] Fix | Delete
) {
[404] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
[405] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
[406] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
[407] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
[408] Fix | Delete
[409] Fix | Delete
/* Input validation: */
[410] Fix | Delete
if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS128L_NPUBBYTES) {
[411] Fix | Delete
throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long');
[412] Fix | Delete
}
[413] Fix | Delete
if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS128L_KEYBYTES) {
[414] Fix | Delete
throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long');
[415] Fix | Delete
}
[416] Fix | Delete
[417] Fix | Delete
list($ct, $tag) = ParagonIE_Sodium_Core_AEGIS128L::encrypt($plaintext, $assocData, $key, $nonce);
[418] Fix | Delete
return $ct . $tag;
[419] Fix | Delete
}
[420] Fix | Delete
[421] Fix | Delete
/**
[422] Fix | Delete
* Return a secure random key for use with the AEGIS-128L
[423] Fix | Delete
* symmetric AEAD interface.
[424] Fix | Delete
*
[425] Fix | Delete
* @return string
[426] Fix | Delete
* @throws Exception
[427] Fix | Delete
* @throws Error
[428] Fix | Delete
*/
[429] Fix | Delete
public static function crypto_aead_aegis128l_keygen()
[430] Fix | Delete
{
[431] Fix | Delete
return random_bytes(self::CRYPTO_AEAD_AEGIS128L_KEYBYTES);
[432] Fix | Delete
}
[433] Fix | Delete
[434] Fix | Delete
/**
[435] Fix | Delete
* Authenticated Encryption with Associated Data: Decryption
[436] Fix | Delete
*
[437] Fix | Delete
* Algorithm:
[438] Fix | Delete
* AEGIS-256
[439] Fix | Delete
*
[440] Fix | Delete
* @param string $ciphertext Encrypted message (with MAC appended)
[441] Fix | Delete
* @param string $assocData Authenticated Associated Data (unencrypted)
[442] Fix | Delete
* @param string $nonce Number to be used only Once; must be 32 bytes
[443] Fix | Delete
* @param string $key Encryption key
[444] Fix | Delete
*
[445] Fix | Delete
* @return string The original plaintext message
[446] Fix | Delete
* @throws SodiumException
[447] Fix | Delete
* @throws TypeError
[448] Fix | Delete
* @psalm-suppress MixedArgument
[449] Fix | Delete
* @psalm-suppress MixedInferredReturnType
[450] Fix | Delete
* @psalm-suppress MixedReturnStatement
[451] Fix | Delete
*/
[452] Fix | Delete
public static function crypto_aead_aegis256_decrypt(
[453] Fix | Delete
$ciphertext = '',
[454] Fix | Delete
$assocData = '',
[455] Fix | Delete
$nonce = '',
[456] Fix | Delete
#[\SensitiveParameter]
[457] Fix | Delete
$key = ''
[458] Fix | Delete
) {
[459] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
[460] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
[461] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
[462] Fix | Delete
ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
[463] Fix | Delete
[464] Fix | Delete
/* Input validation: */
[465] Fix | Delete
if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS256_NPUBBYTES) {
[466] Fix | Delete
throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS256_NPUBBYTES long');
[467] Fix | Delete
}
[468] Fix | Delete
if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS256_KEYBYTES) {
[469] Fix | Delete
throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS256_KEYBYTES long');
[470] Fix | Delete
}
[471] Fix | Delete
$ct_length = ParagonIE_Sodium_Core_Util::strlen($ciphertext);
[472] Fix | Delete
if ($ct_length < self::CRYPTO_AEAD_AEGIS256_ABYTES) {
[473] Fix | Delete
throw new SodiumException('Message must be at least CRYPTO_AEAD_AEGIS256_ABYTES long');
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
$ct = ParagonIE_Sodium_Core_Util::substr(
[477] Fix | Delete
$ciphertext,
[478] Fix | Delete
0,
[479] Fix | Delete
$ct_length - self::CRYPTO_AEAD_AEGIS256_ABYTES
[480] Fix | Delete
);
[481] Fix | Delete
$tag = ParagonIE_Sodium_Core_Util::substr(
[482] Fix | Delete
$ciphertext,
[483] Fix | Delete
$ct_length - self::CRYPTO_AEAD_AEGIS256_ABYTES,
[484] Fix | Delete
self::CRYPTO_AEAD_AEGIS256_ABYTES
[485] Fix | Delete
);
[486] Fix | Delete
return ParagonIE_Sodium_Core_AEGIS256::decrypt($ct, $tag, $assocData, $key, $nonce);
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
/**
[490] Fix | Delete
* Authenticated Encryption with Associated Data: Encryption
[491] Fix | Delete
*
[492] Fix | Delete
* Algorithm:
[493] Fix | Delete
* AEGIS-256
[494] Fix | Delete
*
[495] Fix | Delete
* @param string $plaintext Message to be encrypted
[496] Fix | Delete
* @param string $assocData Authenticated Associated Data (unencrypted)
[497] Fix | Delete
* @param string $nonce Number to be used only Once; must be 32 bytes
[498] Fix | Delete
* @param string $key Encryption key
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function