Edit File by line
/home/barbar84/www/wp-inclu.../sodium_c.../src
File: Crypto32.php
<?php
[0] Fix | Delete
[1] Fix | Delete
if (class_exists('ParagonIE_Sodium_Crypto32', false)) {
[2] Fix | Delete
return;
[3] Fix | Delete
}
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Class ParagonIE_Sodium_Crypto
[7] Fix | Delete
*
[8] Fix | Delete
* ATTENTION!
[9] Fix | Delete
*
[10] Fix | Delete
* If you are using this library, you should be using
[11] Fix | Delete
* ParagonIE_Sodium_Compat in your code, not this class.
[12] Fix | Delete
*/
[13] Fix | Delete
abstract class ParagonIE_Sodium_Crypto32
[14] Fix | Delete
{
[15] Fix | Delete
const aead_chacha20poly1305_KEYBYTES = 32;
[16] Fix | Delete
const aead_chacha20poly1305_NSECBYTES = 0;
[17] Fix | Delete
const aead_chacha20poly1305_NPUBBYTES = 8;
[18] Fix | Delete
const aead_chacha20poly1305_ABYTES = 16;
[19] Fix | Delete
[20] Fix | Delete
const aead_chacha20poly1305_IETF_KEYBYTES = 32;
[21] Fix | Delete
const aead_chacha20poly1305_IETF_NSECBYTES = 0;
[22] Fix | Delete
const aead_chacha20poly1305_IETF_NPUBBYTES = 12;
[23] Fix | Delete
const aead_chacha20poly1305_IETF_ABYTES = 16;
[24] Fix | Delete
[25] Fix | Delete
const aead_xchacha20poly1305_IETF_KEYBYTES = 32;
[26] Fix | Delete
const aead_xchacha20poly1305_IETF_NSECBYTES = 0;
[27] Fix | Delete
const aead_xchacha20poly1305_IETF_NPUBBYTES = 24;
[28] Fix | Delete
const aead_xchacha20poly1305_IETF_ABYTES = 16;
[29] Fix | Delete
[30] Fix | Delete
const box_curve25519xsalsa20poly1305_SEEDBYTES = 32;
[31] Fix | Delete
const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32;
[32] Fix | Delete
const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32;
[33] Fix | Delete
const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32;
[34] Fix | Delete
const box_curve25519xsalsa20poly1305_NONCEBYTES = 24;
[35] Fix | Delete
const box_curve25519xsalsa20poly1305_MACBYTES = 16;
[36] Fix | Delete
const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16;
[37] Fix | Delete
const box_curve25519xsalsa20poly1305_ZEROBYTES = 32;
[38] Fix | Delete
[39] Fix | Delete
const onetimeauth_poly1305_BYTES = 16;
[40] Fix | Delete
const onetimeauth_poly1305_KEYBYTES = 32;
[41] Fix | Delete
[42] Fix | Delete
const secretbox_xsalsa20poly1305_KEYBYTES = 32;
[43] Fix | Delete
const secretbox_xsalsa20poly1305_NONCEBYTES = 24;
[44] Fix | Delete
const secretbox_xsalsa20poly1305_MACBYTES = 16;
[45] Fix | Delete
const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16;
[46] Fix | Delete
const secretbox_xsalsa20poly1305_ZEROBYTES = 32;
[47] Fix | Delete
[48] Fix | Delete
const secretbox_xchacha20poly1305_KEYBYTES = 32;
[49] Fix | Delete
const secretbox_xchacha20poly1305_NONCEBYTES = 24;
[50] Fix | Delete
const secretbox_xchacha20poly1305_MACBYTES = 16;
[51] Fix | Delete
const secretbox_xchacha20poly1305_BOXZEROBYTES = 16;
[52] Fix | Delete
const secretbox_xchacha20poly1305_ZEROBYTES = 32;
[53] Fix | Delete
[54] Fix | Delete
const stream_salsa20_KEYBYTES = 32;
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* AEAD Decryption with ChaCha20-Poly1305
[58] Fix | Delete
*
[59] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[60] Fix | Delete
*
[61] Fix | Delete
* @param string $message
[62] Fix | Delete
* @param string $ad
[63] Fix | Delete
* @param string $nonce
[64] Fix | Delete
* @param string $key
[65] Fix | Delete
* @return string
[66] Fix | Delete
* @throws SodiumException
[67] Fix | Delete
* @throws TypeError
[68] Fix | Delete
*/
[69] Fix | Delete
public static function aead_chacha20poly1305_decrypt(
[70] Fix | Delete
$message = '',
[71] Fix | Delete
$ad = '',
[72] Fix | Delete
$nonce = '',
[73] Fix | Delete
$key = ''
[74] Fix | Delete
) {
[75] Fix | Delete
/** @var int $len - Length of message (ciphertext + MAC) */
[76] Fix | Delete
$len = ParagonIE_Sodium_Core32_Util::strlen($message);
[77] Fix | Delete
[78] Fix | Delete
/** @var int $clen - Length of ciphertext */
[79] Fix | Delete
$clen = $len - self::aead_chacha20poly1305_ABYTES;
[80] Fix | Delete
[81] Fix | Delete
/** @var int $adlen - Length of associated data */
[82] Fix | Delete
$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
[83] Fix | Delete
[84] Fix | Delete
/** @var string $mac - Message authentication code */
[85] Fix | Delete
$mac = ParagonIE_Sodium_Core32_Util::substr(
[86] Fix | Delete
$message,
[87] Fix | Delete
$clen,
[88] Fix | Delete
self::aead_chacha20poly1305_ABYTES
[89] Fix | Delete
);
[90] Fix | Delete
[91] Fix | Delete
/** @var string $ciphertext - The encrypted message (sans MAC) */
[92] Fix | Delete
$ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 0, $clen);
[93] Fix | Delete
[94] Fix | Delete
/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
[95] Fix | Delete
$block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
[96] Fix | Delete
32,
[97] Fix | Delete
$nonce,
[98] Fix | Delete
$key
[99] Fix | Delete
);
[100] Fix | Delete
[101] Fix | Delete
/* Recalculate the Poly1305 authentication tag (MAC): */
[102] Fix | Delete
$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
[103] Fix | Delete
try {
[104] Fix | Delete
ParagonIE_Sodium_Compat::memzero($block0);
[105] Fix | Delete
} catch (SodiumException $ex) {
[106] Fix | Delete
$block0 = null;
[107] Fix | Delete
}
[108] Fix | Delete
$state->update($ad);
[109] Fix | Delete
$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
[110] Fix | Delete
$state->update($ciphertext);
[111] Fix | Delete
$state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
[112] Fix | Delete
$computed_mac = $state->finish();
[113] Fix | Delete
[114] Fix | Delete
/* Compare the given MAC with the recalculated MAC: */
[115] Fix | Delete
if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
[116] Fix | Delete
throw new SodiumException('Invalid MAC');
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
// Here, we know that the MAC is valid, so we decrypt and return the plaintext
[120] Fix | Delete
return ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
[121] Fix | Delete
$ciphertext,
[122] Fix | Delete
$nonce,
[123] Fix | Delete
$key,
[124] Fix | Delete
ParagonIE_Sodium_Core32_Util::store64_le(1)
[125] Fix | Delete
);
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* AEAD Encryption with ChaCha20-Poly1305
[130] Fix | Delete
*
[131] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[132] Fix | Delete
*
[133] Fix | Delete
* @param string $message
[134] Fix | Delete
* @param string $ad
[135] Fix | Delete
* @param string $nonce
[136] Fix | Delete
* @param string $key
[137] Fix | Delete
* @return string
[138] Fix | Delete
* @throws SodiumException
[139] Fix | Delete
* @throws TypeError
[140] Fix | Delete
*/
[141] Fix | Delete
public static function aead_chacha20poly1305_encrypt(
[142] Fix | Delete
$message = '',
[143] Fix | Delete
$ad = '',
[144] Fix | Delete
$nonce = '',
[145] Fix | Delete
$key = ''
[146] Fix | Delete
) {
[147] Fix | Delete
/** @var int $len - Length of the plaintext message */
[148] Fix | Delete
$len = ParagonIE_Sodium_Core32_Util::strlen($message);
[149] Fix | Delete
[150] Fix | Delete
/** @var int $adlen - Length of the associated data */
[151] Fix | Delete
$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
[152] Fix | Delete
[153] Fix | Delete
/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
[154] Fix | Delete
$block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
[155] Fix | Delete
32,
[156] Fix | Delete
$nonce,
[157] Fix | Delete
$key
[158] Fix | Delete
);
[159] Fix | Delete
$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
[160] Fix | Delete
try {
[161] Fix | Delete
ParagonIE_Sodium_Compat::memzero($block0);
[162] Fix | Delete
} catch (SodiumException $ex) {
[163] Fix | Delete
$block0 = null;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
/** @var string $ciphertext - Raw encrypted data */
[167] Fix | Delete
$ciphertext = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
[168] Fix | Delete
$message,
[169] Fix | Delete
$nonce,
[170] Fix | Delete
$key,
[171] Fix | Delete
ParagonIE_Sodium_Core32_Util::store64_le(1)
[172] Fix | Delete
);
[173] Fix | Delete
[174] Fix | Delete
$state->update($ad);
[175] Fix | Delete
$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
[176] Fix | Delete
$state->update($ciphertext);
[177] Fix | Delete
$state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
[178] Fix | Delete
return $ciphertext . $state->finish();
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
/**
[182] Fix | Delete
* AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
[183] Fix | Delete
*
[184] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[185] Fix | Delete
*
[186] Fix | Delete
* @param string $message
[187] Fix | Delete
* @param string $ad
[188] Fix | Delete
* @param string $nonce
[189] Fix | Delete
* @param string $key
[190] Fix | Delete
* @return string
[191] Fix | Delete
* @throws SodiumException
[192] Fix | Delete
* @throws TypeError
[193] Fix | Delete
*/
[194] Fix | Delete
public static function aead_chacha20poly1305_ietf_decrypt(
[195] Fix | Delete
$message = '',
[196] Fix | Delete
$ad = '',
[197] Fix | Delete
$nonce = '',
[198] Fix | Delete
$key = ''
[199] Fix | Delete
) {
[200] Fix | Delete
/** @var int $adlen - Length of associated data */
[201] Fix | Delete
$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
[202] Fix | Delete
[203] Fix | Delete
/** @var int $len - Length of message (ciphertext + MAC) */
[204] Fix | Delete
$len = ParagonIE_Sodium_Core32_Util::strlen($message);
[205] Fix | Delete
[206] Fix | Delete
/** @var int $clen - Length of ciphertext */
[207] Fix | Delete
$clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
[208] Fix | Delete
[209] Fix | Delete
/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
[210] Fix | Delete
$block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
[211] Fix | Delete
32,
[212] Fix | Delete
$nonce,
[213] Fix | Delete
$key
[214] Fix | Delete
);
[215] Fix | Delete
[216] Fix | Delete
/** @var string $mac - Message authentication code */
[217] Fix | Delete
$mac = ParagonIE_Sodium_Core32_Util::substr(
[218] Fix | Delete
$message,
[219] Fix | Delete
$len - self::aead_chacha20poly1305_IETF_ABYTES,
[220] Fix | Delete
self::aead_chacha20poly1305_IETF_ABYTES
[221] Fix | Delete
);
[222] Fix | Delete
[223] Fix | Delete
/** @var string $ciphertext - The encrypted message (sans MAC) */
[224] Fix | Delete
$ciphertext = ParagonIE_Sodium_Core32_Util::substr(
[225] Fix | Delete
$message,
[226] Fix | Delete
0,
[227] Fix | Delete
$len - self::aead_chacha20poly1305_IETF_ABYTES
[228] Fix | Delete
);
[229] Fix | Delete
[230] Fix | Delete
/* Recalculate the Poly1305 authentication tag (MAC): */
[231] Fix | Delete
$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
[232] Fix | Delete
try {
[233] Fix | Delete
ParagonIE_Sodium_Compat::memzero($block0);
[234] Fix | Delete
} catch (SodiumException $ex) {
[235] Fix | Delete
$block0 = null;
[236] Fix | Delete
}
[237] Fix | Delete
$state->update($ad);
[238] Fix | Delete
$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
[239] Fix | Delete
$state->update($ciphertext);
[240] Fix | Delete
$state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
[241] Fix | Delete
$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
[242] Fix | Delete
$state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
[243] Fix | Delete
$computed_mac = $state->finish();
[244] Fix | Delete
[245] Fix | Delete
/* Compare the given MAC with the recalculated MAC: */
[246] Fix | Delete
if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
[247] Fix | Delete
throw new SodiumException('Invalid MAC');
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
// Here, we know that the MAC is valid, so we decrypt and return the plaintext
[251] Fix | Delete
return ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
[252] Fix | Delete
$ciphertext,
[253] Fix | Delete
$nonce,
[254] Fix | Delete
$key,
[255] Fix | Delete
ParagonIE_Sodium_Core32_Util::store64_le(1)
[256] Fix | Delete
);
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
/**
[260] Fix | Delete
* AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
[261] Fix | Delete
*
[262] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[263] Fix | Delete
*
[264] Fix | Delete
* @param string $message
[265] Fix | Delete
* @param string $ad
[266] Fix | Delete
* @param string $nonce
[267] Fix | Delete
* @param string $key
[268] Fix | Delete
* @return string
[269] Fix | Delete
* @throws SodiumException
[270] Fix | Delete
* @throws TypeError
[271] Fix | Delete
*/
[272] Fix | Delete
public static function aead_chacha20poly1305_ietf_encrypt(
[273] Fix | Delete
$message = '',
[274] Fix | Delete
$ad = '',
[275] Fix | Delete
$nonce = '',
[276] Fix | Delete
$key = ''
[277] Fix | Delete
) {
[278] Fix | Delete
/** @var int $len - Length of the plaintext message */
[279] Fix | Delete
$len = ParagonIE_Sodium_Core32_Util::strlen($message);
[280] Fix | Delete
[281] Fix | Delete
/** @var int $adlen - Length of the associated data */
[282] Fix | Delete
$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
[283] Fix | Delete
[284] Fix | Delete
/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
[285] Fix | Delete
$block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
[286] Fix | Delete
32,
[287] Fix | Delete
$nonce,
[288] Fix | Delete
$key
[289] Fix | Delete
);
[290] Fix | Delete
$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
[291] Fix | Delete
try {
[292] Fix | Delete
ParagonIE_Sodium_Compat::memzero($block0);
[293] Fix | Delete
} catch (SodiumException $ex) {
[294] Fix | Delete
$block0 = null;
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
/** @var string $ciphertext - Raw encrypted data */
[298] Fix | Delete
$ciphertext = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
[299] Fix | Delete
$message,
[300] Fix | Delete
$nonce,
[301] Fix | Delete
$key,
[302] Fix | Delete
ParagonIE_Sodium_Core32_Util::store64_le(1)
[303] Fix | Delete
);
[304] Fix | Delete
[305] Fix | Delete
$state->update($ad);
[306] Fix | Delete
$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
[307] Fix | Delete
$state->update($ciphertext);
[308] Fix | Delete
$state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
[309] Fix | Delete
$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
[310] Fix | Delete
$state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
[311] Fix | Delete
return $ciphertext . $state->finish();
[312] Fix | Delete
}
[313] Fix | Delete
[314] Fix | Delete
/**
[315] Fix | Delete
* AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
[316] Fix | Delete
*
[317] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[318] Fix | Delete
*
[319] Fix | Delete
* @param string $message
[320] Fix | Delete
* @param string $ad
[321] Fix | Delete
* @param string $nonce
[322] Fix | Delete
* @param string $key
[323] Fix | Delete
* @return string
[324] Fix | Delete
* @throws SodiumException
[325] Fix | Delete
* @throws TypeError
[326] Fix | Delete
*/
[327] Fix | Delete
public static function aead_xchacha20poly1305_ietf_decrypt(
[328] Fix | Delete
$message = '',
[329] Fix | Delete
$ad = '',
[330] Fix | Delete
$nonce = '',
[331] Fix | Delete
$key = ''
[332] Fix | Delete
) {
[333] Fix | Delete
$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
[334] Fix | Delete
ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
[335] Fix | Delete
$key
[336] Fix | Delete
);
[337] Fix | Delete
$nonceLast = "\x00\x00\x00\x00" .
[338] Fix | Delete
ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
[339] Fix | Delete
[340] Fix | Delete
return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
/**
[344] Fix | Delete
* AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
[345] Fix | Delete
*
[346] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[347] Fix | Delete
*
[348] Fix | Delete
* @param string $message
[349] Fix | Delete
* @param string $ad
[350] Fix | Delete
* @param string $nonce
[351] Fix | Delete
* @param string $key
[352] Fix | Delete
* @return string
[353] Fix | Delete
* @throws SodiumException
[354] Fix | Delete
* @throws TypeError
[355] Fix | Delete
*/
[356] Fix | Delete
public static function aead_xchacha20poly1305_ietf_encrypt(
[357] Fix | Delete
$message = '',
[358] Fix | Delete
$ad = '',
[359] Fix | Delete
$nonce = '',
[360] Fix | Delete
$key = ''
[361] Fix | Delete
) {
[362] Fix | Delete
$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
[363] Fix | Delete
ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
[364] Fix | Delete
$key
[365] Fix | Delete
);
[366] Fix | Delete
$nonceLast = "\x00\x00\x00\x00" .
[367] Fix | Delete
ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
[368] Fix | Delete
[369] Fix | Delete
return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
/**
[373] Fix | Delete
* HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512)
[374] Fix | Delete
*
[375] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[376] Fix | Delete
*
[377] Fix | Delete
* @param string $message
[378] Fix | Delete
* @param string $key
[379] Fix | Delete
* @return string
[380] Fix | Delete
* @throws TypeError
[381] Fix | Delete
*/
[382] Fix | Delete
public static function auth($message, $key)
[383] Fix | Delete
{
[384] Fix | Delete
return ParagonIE_Sodium_Core32_Util::substr(
[385] Fix | Delete
hash_hmac('sha512', $message, $key, true),
[386] Fix | Delete
0,
[387] Fix | Delete
32
[388] Fix | Delete
);
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
/**
[392] Fix | Delete
* HMAC-SHA-512-256 validation. Constant-time via hash_equals().
[393] Fix | Delete
*
[394] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[395] Fix | Delete
*
[396] Fix | Delete
* @param string $mac
[397] Fix | Delete
* @param string $message
[398] Fix | Delete
* @param string $key
[399] Fix | Delete
* @return bool
[400] Fix | Delete
* @throws SodiumException
[401] Fix | Delete
* @throws TypeError
[402] Fix | Delete
*/
[403] Fix | Delete
public static function auth_verify($mac, $message, $key)
[404] Fix | Delete
{
[405] Fix | Delete
return ParagonIE_Sodium_Core32_Util::hashEquals(
[406] Fix | Delete
$mac,
[407] Fix | Delete
self::auth($message, $key)
[408] Fix | Delete
);
[409] Fix | Delete
}
[410] Fix | Delete
[411] Fix | Delete
/**
[412] Fix | Delete
* X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption
[413] Fix | Delete
*
[414] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[415] Fix | Delete
*
[416] Fix | Delete
* @param string $plaintext
[417] Fix | Delete
* @param string $nonce
[418] Fix | Delete
* @param string $keypair
[419] Fix | Delete
* @return string
[420] Fix | Delete
* @throws SodiumException
[421] Fix | Delete
* @throws TypeError
[422] Fix | Delete
*/
[423] Fix | Delete
public static function box($plaintext, $nonce, $keypair)
[424] Fix | Delete
{
[425] Fix | Delete
return self::secretbox(
[426] Fix | Delete
$plaintext,
[427] Fix | Delete
$nonce,
[428] Fix | Delete
self::box_beforenm(
[429] Fix | Delete
self::box_secretkey($keypair),
[430] Fix | Delete
self::box_publickey($keypair)
[431] Fix | Delete
)
[432] Fix | Delete
);
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
/**
[436] Fix | Delete
* X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair.
[437] Fix | Delete
*
[438] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[439] Fix | Delete
*
[440] Fix | Delete
* @param string $message
[441] Fix | Delete
* @param string $publicKey
[442] Fix | Delete
* @return string
[443] Fix | Delete
* @throws SodiumException
[444] Fix | Delete
* @throws TypeError
[445] Fix | Delete
*/
[446] Fix | Delete
public static function box_seal($message, $publicKey)
[447] Fix | Delete
{
[448] Fix | Delete
/** @var string $ephemeralKeypair */
[449] Fix | Delete
$ephemeralKeypair = self::box_keypair();
[450] Fix | Delete
[451] Fix | Delete
/** @var string $ephemeralSK */
[452] Fix | Delete
$ephemeralSK = self::box_secretkey($ephemeralKeypair);
[453] Fix | Delete
[454] Fix | Delete
/** @var string $ephemeralPK */
[455] Fix | Delete
$ephemeralPK = self::box_publickey($ephemeralKeypair);
[456] Fix | Delete
[457] Fix | Delete
/** @var string $nonce */
[458] Fix | Delete
$nonce = self::generichash(
[459] Fix | Delete
$ephemeralPK . $publicKey,
[460] Fix | Delete
'',
[461] Fix | Delete
24
[462] Fix | Delete
);
[463] Fix | Delete
[464] Fix | Delete
/** @var string $keypair - The combined keypair used in crypto_box() */
[465] Fix | Delete
$keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
[466] Fix | Delete
[467] Fix | Delete
/** @var string $ciphertext Ciphertext + MAC from crypto_box */
[468] Fix | Delete
$ciphertext = self::box($message, $nonce, $keypair);
[469] Fix | Delete
try {
[470] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
[471] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephemeralSK);
[472] Fix | Delete
ParagonIE_Sodium_Compat::memzero($nonce);
[473] Fix | Delete
} catch (SodiumException $ex) {
[474] Fix | Delete
$ephemeralKeypair = null;
[475] Fix | Delete
$ephemeralSK = null;
[476] Fix | Delete
$nonce = null;
[477] Fix | Delete
}
[478] Fix | Delete
return $ephemeralPK . $ciphertext;
[479] Fix | Delete
}
[480] Fix | Delete
[481] Fix | Delete
/**
[482] Fix | Delete
* Opens a message encrypted via box_seal().
[483] Fix | Delete
*
[484] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[485] Fix | Delete
*
[486] Fix | Delete
* @param string $message
[487] Fix | Delete
* @param string $keypair
[488] Fix | Delete
* @return string
[489] Fix | Delete
* @throws SodiumException
[490] Fix | Delete
* @throws TypeError
[491] Fix | Delete
*/
[492] Fix | Delete
public static function box_seal_open($message, $keypair)
[493] Fix | Delete
{
[494] Fix | Delete
/** @var string $ephemeralPK */
[495] Fix | Delete
$ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32);
[496] Fix | Delete
[497] Fix | Delete
/** @var string $ciphertext (ciphertext + MAC) */
[498] Fix | Delete
$ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 32);
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function