Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93

Warning: Undefined array key "page_file_edit_line" in /home/sportsfever/public_html/filemanger/edit_text_line.php on line 32
/home/sportsfe.../httpdocs/clone/wp-inclu.../sodium_c.../src
File: Crypto.php
/** @var string $c - MAC || ciphertext */
[1000] Fix | Delete
$c = $state->finish() . $c;
[1001] Fix | Delete
unset($state);
[1002] Fix | Delete
[1003] Fix | Delete
return $c;
[1004] Fix | Delete
}
[1005] Fix | Delete
[1006] Fix | Delete
/**
[1007] Fix | Delete
* Decrypt a ciphertext generated via secretbox().
[1008] Fix | Delete
*
[1009] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[1010] Fix | Delete
*
[1011] Fix | Delete
* @param string $ciphertext
[1012] Fix | Delete
* @param string $nonce
[1013] Fix | Delete
* @param string $key
[1014] Fix | Delete
* @return string
[1015] Fix | Delete
* @throws SodiumException
[1016] Fix | Delete
* @throws TypeError
[1017] Fix | Delete
*/
[1018] Fix | Delete
public static function secretbox_open($ciphertext, $nonce, $key)
[1019] Fix | Delete
{
[1020] Fix | Delete
/** @var string $mac */
[1021] Fix | Delete
$mac = ParagonIE_Sodium_Core_Util::substr(
[1022] Fix | Delete
$ciphertext,
[1023] Fix | Delete
0,
[1024] Fix | Delete
self::secretbox_xsalsa20poly1305_MACBYTES
[1025] Fix | Delete
);
[1026] Fix | Delete
[1027] Fix | Delete
/** @var string $c */
[1028] Fix | Delete
$c = ParagonIE_Sodium_Core_Util::substr(
[1029] Fix | Delete
$ciphertext,
[1030] Fix | Delete
self::secretbox_xsalsa20poly1305_MACBYTES
[1031] Fix | Delete
);
[1032] Fix | Delete
[1033] Fix | Delete
/** @var int $clen */
[1034] Fix | Delete
$clen = ParagonIE_Sodium_Core_Util::strlen($c);
[1035] Fix | Delete
[1036] Fix | Delete
/** @var string $subkey */
[1037] Fix | Delete
$subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
[1038] Fix | Delete
[1039] Fix | Delete
/** @var string $block0 */
[1040] Fix | Delete
$block0 = ParagonIE_Sodium_Core_Salsa20::salsa20(
[1041] Fix | Delete
64,
[1042] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
[1043] Fix | Delete
$subkey
[1044] Fix | Delete
);
[1045] Fix | Delete
$verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify(
[1046] Fix | Delete
$mac,
[1047] Fix | Delete
$c,
[1048] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($block0, 0, 32)
[1049] Fix | Delete
);
[1050] Fix | Delete
if (!$verified) {
[1051] Fix | Delete
try {
[1052] Fix | Delete
ParagonIE_Sodium_Compat::memzero($subkey);
[1053] Fix | Delete
} catch (SodiumException $ex) {
[1054] Fix | Delete
$subkey = null;
[1055] Fix | Delete
}
[1056] Fix | Delete
throw new SodiumException('Invalid MAC');
[1057] Fix | Delete
}
[1058] Fix | Delete
[1059] Fix | Delete
/** @var string $m - Decrypted message */
[1060] Fix | Delete
$m = ParagonIE_Sodium_Core_Util::xorStrings(
[1061] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
[1062] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
[1063] Fix | Delete
);
[1064] Fix | Delete
if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
[1065] Fix | Delete
// We had more than 1 block, so let's continue to decrypt the rest.
[1066] Fix | Delete
$m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
[1067] Fix | Delete
ParagonIE_Sodium_Core_Util::substr(
[1068] Fix | Delete
$c,
[1069] Fix | Delete
self::secretbox_xsalsa20poly1305_ZEROBYTES
[1070] Fix | Delete
),
[1071] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
[1072] Fix | Delete
1,
[1073] Fix | Delete
(string) $subkey
[1074] Fix | Delete
);
[1075] Fix | Delete
}
[1076] Fix | Delete
return $m;
[1077] Fix | Delete
}
[1078] Fix | Delete
[1079] Fix | Delete
/**
[1080] Fix | Delete
* XChaCha20-Poly1305 authenticated symmetric-key encryption.
[1081] Fix | Delete
*
[1082] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[1083] Fix | Delete
*
[1084] Fix | Delete
* @param string $plaintext
[1085] Fix | Delete
* @param string $nonce
[1086] Fix | Delete
* @param string $key
[1087] Fix | Delete
* @return string
[1088] Fix | Delete
* @throws SodiumException
[1089] Fix | Delete
* @throws TypeError
[1090] Fix | Delete
*/
[1091] Fix | Delete
public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
[1092] Fix | Delete
{
[1093] Fix | Delete
/** @var string $subkey */
[1094] Fix | Delete
$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
[1095] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
[1096] Fix | Delete
$key
[1097] Fix | Delete
);
[1098] Fix | Delete
$nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
[1099] Fix | Delete
[1100] Fix | Delete
/** @var string $block0 */
[1101] Fix | Delete
$block0 = str_repeat("\x00", 32);
[1102] Fix | Delete
[1103] Fix | Delete
/** @var int $mlen - Length of the plaintext message */
[1104] Fix | Delete
$mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext);
[1105] Fix | Delete
$mlen0 = $mlen;
[1106] Fix | Delete
if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
[1107] Fix | Delete
$mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
[1108] Fix | Delete
}
[1109] Fix | Delete
$block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
[1110] Fix | Delete
[1111] Fix | Delete
/** @var string $block0 */
[1112] Fix | Delete
$block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
[1113] Fix | Delete
$block0,
[1114] Fix | Delete
$nonceLast,
[1115] Fix | Delete
$subkey
[1116] Fix | Delete
);
[1117] Fix | Delete
[1118] Fix | Delete
/** @var string $c */
[1119] Fix | Delete
$c = ParagonIE_Sodium_Core_Util::substr(
[1120] Fix | Delete
$block0,
[1121] Fix | Delete
self::secretbox_xchacha20poly1305_ZEROBYTES
[1122] Fix | Delete
);
[1123] Fix | Delete
if ($mlen > $mlen0) {
[1124] Fix | Delete
$c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
[1125] Fix | Delete
ParagonIE_Sodium_Core_Util::substr(
[1126] Fix | Delete
$plaintext,
[1127] Fix | Delete
self::secretbox_xchacha20poly1305_ZEROBYTES
[1128] Fix | Delete
),
[1129] Fix | Delete
$nonceLast,
[1130] Fix | Delete
$subkey,
[1131] Fix | Delete
ParagonIE_Sodium_Core_Util::store64_le(1)
[1132] Fix | Delete
);
[1133] Fix | Delete
}
[1134] Fix | Delete
$state = new ParagonIE_Sodium_Core_Poly1305_State(
[1135] Fix | Delete
ParagonIE_Sodium_Core_Util::substr(
[1136] Fix | Delete
$block0,
[1137] Fix | Delete
0,
[1138] Fix | Delete
self::onetimeauth_poly1305_KEYBYTES
[1139] Fix | Delete
)
[1140] Fix | Delete
);
[1141] Fix | Delete
try {
[1142] Fix | Delete
ParagonIE_Sodium_Compat::memzero($block0);
[1143] Fix | Delete
ParagonIE_Sodium_Compat::memzero($subkey);
[1144] Fix | Delete
} catch (SodiumException $ex) {
[1145] Fix | Delete
$block0 = null;
[1146] Fix | Delete
$subkey = null;
[1147] Fix | Delete
}
[1148] Fix | Delete
[1149] Fix | Delete
$state->update($c);
[1150] Fix | Delete
[1151] Fix | Delete
/** @var string $c - MAC || ciphertext */
[1152] Fix | Delete
$c = $state->finish() . $c;
[1153] Fix | Delete
unset($state);
[1154] Fix | Delete
[1155] Fix | Delete
return $c;
[1156] Fix | Delete
}
[1157] Fix | Delete
[1158] Fix | Delete
/**
[1159] Fix | Delete
* Decrypt a ciphertext generated via secretbox_xchacha20poly1305().
[1160] Fix | Delete
*
[1161] Fix | Delete
* @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
[1162] Fix | Delete
*
[1163] Fix | Delete
* @param string $ciphertext
[1164] Fix | Delete
* @param string $nonce
[1165] Fix | Delete
* @param string $key
[1166] Fix | Delete
* @return string
[1167] Fix | Delete
* @throws SodiumException
[1168] Fix | Delete
* @throws TypeError
[1169] Fix | Delete
*/
[1170] Fix | Delete
public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
[1171] Fix | Delete
{
[1172] Fix | Delete
/** @var string $mac */
[1173] Fix | Delete
$mac = ParagonIE_Sodium_Core_Util::substr(
[1174] Fix | Delete
$ciphertext,
[1175] Fix | Delete
0,
[1176] Fix | Delete
self::secretbox_xchacha20poly1305_MACBYTES
[1177] Fix | Delete
);
[1178] Fix | Delete
[1179] Fix | Delete
/** @var string $c */
[1180] Fix | Delete
$c = ParagonIE_Sodium_Core_Util::substr(
[1181] Fix | Delete
$ciphertext,
[1182] Fix | Delete
self::secretbox_xchacha20poly1305_MACBYTES
[1183] Fix | Delete
);
[1184] Fix | Delete
[1185] Fix | Delete
/** @var int $clen */
[1186] Fix | Delete
$clen = ParagonIE_Sodium_Core_Util::strlen($c);
[1187] Fix | Delete
[1188] Fix | Delete
/** @var string $subkey */
[1189] Fix | Delete
$subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key);
[1190] Fix | Delete
[1191] Fix | Delete
/** @var string $block0 */
[1192] Fix | Delete
$block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
[1193] Fix | Delete
64,
[1194] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
[1195] Fix | Delete
$subkey
[1196] Fix | Delete
);
[1197] Fix | Delete
$verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify(
[1198] Fix | Delete
$mac,
[1199] Fix | Delete
$c,
[1200] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($block0, 0, 32)
[1201] Fix | Delete
);
[1202] Fix | Delete
[1203] Fix | Delete
if (!$verified) {
[1204] Fix | Delete
try {
[1205] Fix | Delete
ParagonIE_Sodium_Compat::memzero($subkey);
[1206] Fix | Delete
} catch (SodiumException $ex) {
[1207] Fix | Delete
$subkey = null;
[1208] Fix | Delete
}
[1209] Fix | Delete
throw new SodiumException('Invalid MAC');
[1210] Fix | Delete
}
[1211] Fix | Delete
[1212] Fix | Delete
/** @var string $m - Decrypted message */
[1213] Fix | Delete
$m = ParagonIE_Sodium_Core_Util::xorStrings(
[1214] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
[1215] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
[1216] Fix | Delete
);
[1217] Fix | Delete
[1218] Fix | Delete
if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
[1219] Fix | Delete
// We had more than 1 block, so let's continue to decrypt the rest.
[1220] Fix | Delete
$m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
[1221] Fix | Delete
ParagonIE_Sodium_Core_Util::substr(
[1222] Fix | Delete
$c,
[1223] Fix | Delete
self::secretbox_xchacha20poly1305_ZEROBYTES
[1224] Fix | Delete
),
[1225] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
[1226] Fix | Delete
(string) $subkey,
[1227] Fix | Delete
ParagonIE_Sodium_Core_Util::store64_le(1)
[1228] Fix | Delete
);
[1229] Fix | Delete
}
[1230] Fix | Delete
return $m;
[1231] Fix | Delete
}
[1232] Fix | Delete
[1233] Fix | Delete
/**
[1234] Fix | Delete
* @param string $key
[1235] Fix | Delete
* @return array<int, string> Returns a state and a header.
[1236] Fix | Delete
* @throws Exception
[1237] Fix | Delete
* @throws SodiumException
[1238] Fix | Delete
*/
[1239] Fix | Delete
public static function secretstream_xchacha20poly1305_init_push($key)
[1240] Fix | Delete
{
[1241] Fix | Delete
# randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
[1242] Fix | Delete
$out = random_bytes(24);
[1243] Fix | Delete
[1244] Fix | Delete
# crypto_core_hchacha20(state->k, out, k, NULL);
[1245] Fix | Delete
$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20($out, $key);
[1246] Fix | Delete
$state = new ParagonIE_Sodium_Core_SecretStream_State(
[1247] Fix | Delete
$subkey,
[1248] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4)
[1249] Fix | Delete
);
[1250] Fix | Delete
[1251] Fix | Delete
# _crypto_secretstream_xchacha20poly1305_counter_reset(state);
[1252] Fix | Delete
$state->counterReset();
[1253] Fix | Delete
[1254] Fix | Delete
# memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES,
[1255] Fix | Delete
# crypto_secretstream_xchacha20poly1305_INONCEBYTES);
[1256] Fix | Delete
# memset(state->_pad, 0, sizeof state->_pad);
[1257] Fix | Delete
return array(
[1258] Fix | Delete
$state->toString(),
[1259] Fix | Delete
$out
[1260] Fix | Delete
);
[1261] Fix | Delete
}
[1262] Fix | Delete
[1263] Fix | Delete
/**
[1264] Fix | Delete
* @param string $key
[1265] Fix | Delete
* @param string $header
[1266] Fix | Delete
* @return string Returns a state.
[1267] Fix | Delete
* @throws Exception
[1268] Fix | Delete
*/
[1269] Fix | Delete
public static function secretstream_xchacha20poly1305_init_pull($key, $header)
[1270] Fix | Delete
{
[1271] Fix | Delete
# crypto_core_hchacha20(state->k, in, k, NULL);
[1272] Fix | Delete
$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
[1273] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($header, 0, 16),
[1274] Fix | Delete
$key
[1275] Fix | Delete
);
[1276] Fix | Delete
$state = new ParagonIE_Sodium_Core_SecretStream_State(
[1277] Fix | Delete
$subkey,
[1278] Fix | Delete
ParagonIE_Sodium_Core_Util::substr($header, 16)
[1279] Fix | Delete
);
[1280] Fix | Delete
$state->counterReset();
[1281] Fix | Delete
# memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES,
[1282] Fix | Delete
# crypto_secretstream_xchacha20poly1305_INONCEBYTES);
[1283] Fix | Delete
# memset(state->_pad, 0, sizeof state->_pad);
[1284] Fix | Delete
# return 0;
[1285] Fix | Delete
return $state->toString();
[1286] Fix | Delete
}
[1287] Fix | Delete
[1288] Fix | Delete
/**
[1289] Fix | Delete
* @param string $state
[1290] Fix | Delete
* @param string $msg
[1291] Fix | Delete
* @param string $aad
[1292] Fix | Delete
* @param int $tag
[1293] Fix | Delete
* @return string
[1294] Fix | Delete
* @throws SodiumException
[1295] Fix | Delete
*/
[1296] Fix | Delete
public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
[1297] Fix | Delete
{
[1298] Fix | Delete
$st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
[1299] Fix | Delete
# crypto_onetimeauth_poly1305_state poly1305_state;
[1300] Fix | Delete
# unsigned char block[64U];
[1301] Fix | Delete
# unsigned char slen[8U];
[1302] Fix | Delete
# unsigned char *c;
[1303] Fix | Delete
# unsigned char *mac;
[1304] Fix | Delete
[1305] Fix | Delete
$msglen = ParagonIE_Sodium_Core_Util::strlen($msg);
[1306] Fix | Delete
$aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
[1307] Fix | Delete
[1308] Fix | Delete
if ((($msglen + 63) >> 6) > 0xfffffffe) {
[1309] Fix | Delete
throw new SodiumException(
[1310] Fix | Delete
'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
[1311] Fix | Delete
);
[1312] Fix | Delete
}
[1313] Fix | Delete
[1314] Fix | Delete
# if (outlen_p != NULL) {
[1315] Fix | Delete
# *outlen_p = 0U;
[1316] Fix | Delete
# }
[1317] Fix | Delete
# if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
[1318] Fix | Delete
# sodium_misuse();
[1319] Fix | Delete
# }
[1320] Fix | Delete
[1321] Fix | Delete
# crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
[1322] Fix | Delete
# crypto_onetimeauth_poly1305_init(&poly1305_state, block);
[1323] Fix | Delete
# sodium_memzero(block, sizeof block);
[1324] Fix | Delete
$auth = new ParagonIE_Sodium_Core_Poly1305_State(
[1325] Fix | Delete
ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
[1326] Fix | Delete
);
[1327] Fix | Delete
[1328] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
[1329] Fix | Delete
$auth->update($aad);
[1330] Fix | Delete
[1331] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
[1332] Fix | Delete
# (0x10 - adlen) & 0xf);
[1333] Fix | Delete
$auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
[1334] Fix | Delete
[1335] Fix | Delete
# memset(block, 0, sizeof block);
[1336] Fix | Delete
# block[0] = tag;
[1337] Fix | Delete
# crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
[1338] Fix | Delete
# state->nonce, 1U, state->k);
[1339] Fix | Delete
$block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
[1340] Fix | Delete
ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63),
[1341] Fix | Delete
$st->getCombinedNonce(),
[1342] Fix | Delete
$st->getKey(),
[1343] Fix | Delete
ParagonIE_Sodium_Core_Util::store64_le(1)
[1344] Fix | Delete
);
[1345] Fix | Delete
[1346] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
[1347] Fix | Delete
$auth->update($block);
[1348] Fix | Delete
[1349] Fix | Delete
# out[0] = block[0];
[1350] Fix | Delete
$out = $block[0];
[1351] Fix | Delete
# c = out + (sizeof tag);
[1352] Fix | Delete
# crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k);
[1353] Fix | Delete
$cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
[1354] Fix | Delete
$msg,
[1355] Fix | Delete
$st->getCombinedNonce(),
[1356] Fix | Delete
$st->getKey(),
[1357] Fix | Delete
ParagonIE_Sodium_Core_Util::store64_le(2)
[1358] Fix | Delete
);
[1359] Fix | Delete
[1360] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
[1361] Fix | Delete
$auth->update($cipher);
[1362] Fix | Delete
[1363] Fix | Delete
$out .= $cipher;
[1364] Fix | Delete
unset($cipher);
[1365] Fix | Delete
[1366] Fix | Delete
# crypto_onetimeauth_poly1305_update
[1367] Fix | Delete
# (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
[1368] Fix | Delete
$auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
[1369] Fix | Delete
[1370] Fix | Delete
# STORE64_LE(slen, (uint64_t) adlen);
[1371] Fix | Delete
$slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
[1372] Fix | Delete
[1373] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
[1374] Fix | Delete
$auth->update($slen);
[1375] Fix | Delete
[1376] Fix | Delete
# STORE64_LE(slen, (sizeof block) + mlen);
[1377] Fix | Delete
$slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
[1378] Fix | Delete
[1379] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
[1380] Fix | Delete
$auth->update($slen);
[1381] Fix | Delete
[1382] Fix | Delete
# mac = c + mlen;
[1383] Fix | Delete
# crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
[1384] Fix | Delete
$mac = $auth->finish();
[1385] Fix | Delete
$out .= $mac;
[1386] Fix | Delete
[1387] Fix | Delete
# sodium_memzero(&poly1305_state, sizeof poly1305_state);
[1388] Fix | Delete
unset($auth);
[1389] Fix | Delete
[1390] Fix | Delete
[1391] Fix | Delete
# XOR_BUF(STATE_INONCE(state), mac,
[1392] Fix | Delete
# crypto_secretstream_xchacha20poly1305_INONCEBYTES);
[1393] Fix | Delete
$st->xorNonce($mac);
[1394] Fix | Delete
[1395] Fix | Delete
# sodium_increment(STATE_COUNTER(state),
[1396] Fix | Delete
# crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
[1397] Fix | Delete
$st->incrementCounter();
[1398] Fix | Delete
// Overwrite by reference:
[1399] Fix | Delete
$state = $st->toString();
[1400] Fix | Delete
[1401] Fix | Delete
/** @var bool $rekey */
[1402] Fix | Delete
$rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
[1403] Fix | Delete
# if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
[1404] Fix | Delete
# sodium_is_zero(STATE_COUNTER(state),
[1405] Fix | Delete
# crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
[1406] Fix | Delete
# crypto_secretstream_xchacha20poly1305_rekey(state);
[1407] Fix | Delete
# }
[1408] Fix | Delete
if ($rekey || $st->needsRekey()) {
[1409] Fix | Delete
// DO REKEY
[1410] Fix | Delete
self::secretstream_xchacha20poly1305_rekey($state);
[1411] Fix | Delete
}
[1412] Fix | Delete
# if (outlen_p != NULL) {
[1413] Fix | Delete
# *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen;
[1414] Fix | Delete
# }
[1415] Fix | Delete
return $out;
[1416] Fix | Delete
}
[1417] Fix | Delete
[1418] Fix | Delete
/**
[1419] Fix | Delete
* @param string $state
[1420] Fix | Delete
* @param string $cipher
[1421] Fix | Delete
* @param string $aad
[1422] Fix | Delete
* @return bool|array{0: string, 1: int}
[1423] Fix | Delete
* @throws SodiumException
[1424] Fix | Delete
*/
[1425] Fix | Delete
public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
[1426] Fix | Delete
{
[1427] Fix | Delete
$st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
[1428] Fix | Delete
[1429] Fix | Delete
$cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher);
[1430] Fix | Delete
# mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES;
[1431] Fix | Delete
$msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;
[1432] Fix | Delete
$aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
[1433] Fix | Delete
[1434] Fix | Delete
# if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
[1435] Fix | Delete
# sodium_misuse();
[1436] Fix | Delete
# }
[1437] Fix | Delete
if ((($msglen + 63) >> 6) > 0xfffffffe) {
[1438] Fix | Delete
throw new SodiumException(
[1439] Fix | Delete
'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
[1440] Fix | Delete
);
[1441] Fix | Delete
}
[1442] Fix | Delete
[1443] Fix | Delete
# crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
[1444] Fix | Delete
# crypto_onetimeauth_poly1305_init(&poly1305_state, block);
[1445] Fix | Delete
# sodium_memzero(block, sizeof block);
[1446] Fix | Delete
$auth = new ParagonIE_Sodium_Core_Poly1305_State(
[1447] Fix | Delete
ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
[1448] Fix | Delete
);
[1449] Fix | Delete
[1450] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
[1451] Fix | Delete
$auth->update($aad);
[1452] Fix | Delete
[1453] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
[1454] Fix | Delete
# (0x10 - adlen) & 0xf);
[1455] Fix | Delete
$auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
[1456] Fix | Delete
[1457] Fix | Delete
[1458] Fix | Delete
# memset(block, 0, sizeof block);
[1459] Fix | Delete
# block[0] = in[0];
[1460] Fix | Delete
# crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
[1461] Fix | Delete
# state->nonce, 1U, state->k);
[1462] Fix | Delete
$block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
[1463] Fix | Delete
$cipher[0] . str_repeat("\0", 63),
[1464] Fix | Delete
$st->getCombinedNonce(),
[1465] Fix | Delete
$st->getKey(),
[1466] Fix | Delete
ParagonIE_Sodium_Core_Util::store64_le(1)
[1467] Fix | Delete
);
[1468] Fix | Delete
# tag = block[0];
[1469] Fix | Delete
# block[0] = in[0];
[1470] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
[1471] Fix | Delete
$tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]);
[1472] Fix | Delete
$block[0] = $cipher[0];
[1473] Fix | Delete
$auth->update($block);
[1474] Fix | Delete
[1475] Fix | Delete
[1476] Fix | Delete
# c = in + (sizeof tag);
[1477] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
[1478] Fix | Delete
$auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen));
[1479] Fix | Delete
[1480] Fix | Delete
# crypto_onetimeauth_poly1305_update
[1481] Fix | Delete
# (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
[1482] Fix | Delete
$auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
[1483] Fix | Delete
[1484] Fix | Delete
# STORE64_LE(slen, (uint64_t) adlen);
[1485] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
[1486] Fix | Delete
$slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
[1487] Fix | Delete
$auth->update($slen);
[1488] Fix | Delete
[1489] Fix | Delete
# STORE64_LE(slen, (sizeof block) + mlen);
[1490] Fix | Delete
# crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
[1491] Fix | Delete
$slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
[1492] Fix | Delete
$auth->update($slen);
[1493] Fix | Delete
[1494] Fix | Delete
# crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
[1495] Fix | Delete
# sodium_memzero(&poly1305_state, sizeof poly1305_state);
[1496] Fix | Delete
$mac = $auth->finish();
[1497] Fix | Delete
[1498] Fix | Delete
# stored_mac = c + mlen;
[1499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function