/* Crypt file if option is enabled. */
$crypted = false;
$crypt_key = '';
- if ($crypt == true && !(extension_loaded('mcrypt') == true)) {
- error_log("PHP extension mcrypt not loaded, won't encrypt in Jirafeau");
+ if ($crypt == true && !(extension_loaded('sodium') == true)) {
+ error_log("PHP extension sodium not loaded, won't encrypt in Jirafeau");
}
- if ($crypt == true && extension_loaded('mcrypt') == true) {
- $crypt_key = jirafeau_encrypt_file($file['tmp_name'], $file['tmp_name']);
+ if ($crypt == true && extension_loaded('sodium') == true) {
+ $crypt_key = jirafeau_encrypt_file($file['tmp_name'], $file['tmp_name'].'crypt');
if (strlen($crypt_key) > 0) {
- $crypted = true;
+ if (rename($file['tmp_name'].'crypt', $file['tmp_name']) === true) {
+ $crypted = true;
+ }
}
}
$handle,
$name . NL. $mime_type . NL. $size . NL. $password . NL. $time .
NL . $hash. NL . ($one_time_download ? 'O' : 'R') . NL . time() .
- NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C' : 'O')
+ NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C2' : 'O')
);
fclose($handle);
$hash_link = substr(base_16_to_64(md5_file($link_tmp_name)), 0, $link_name_length);
$out['upload_date'] = trim($c[7]);
$out['ip'] = trim($c[8]);
$out['link_code'] = trim($c[9]);
- $out['crypted'] = trim($c[10]) == 'C';
+ $out['crypted'] = trim($c[10]) == 'C2';
+ $out['crypted_legacy'] = trim($c[10]) == 'C';
return $out;
}
$out .= "# PHP options<br/>";
$out .= "- php version: " . phpversion() . "<br/>";
+ $out .= "- sodium version: " . phpversion('sodium') . "<br/>";
$out .= "- mcrypt version: " . phpversion('mcrypt') . "<br/>";
$php_options = [
'post_max_size',
$crypted = false;
$crypt_key = '';
- if ($crypt == true && extension_loaded('mcrypt') == true) {
- $crypt_key = jirafeau_encrypt_file($p, $p);
+ if ($crypt == true && extension_loaded('sodium') == true) {
+ $crypt_key = jirafeau_encrypt_file($p, $p.'.crypt');
if (strlen($crypt_key) > 0) {
- $crypted = true;
+ if (rename($p.'.crypt', $p) === true) {
+ $crypted = true;
+ }
}
}
$handle,
$a['file_name'] . NL . $a['mime_type'] . NL . $size . NL .
$a['key'] . NL . $a['time'] . NL . $hash . NL . $a['onetime'] . NL .
- time() . NL . $a['ip'] . NL . $delete_link_code . NL . ($crypted ? 'C' : 'O')
+ time() . NL . $a['ip'] . NL . $delete_link_code . NL . ($crypted ? 'C2' : 'O')
);
fclose($handle);
$hash_link = substr(base_16_to_64(md5_file($link_tmp_name)), 0, $link_name_length);
}
/**
- * Crypt file and returns decrypt key.
+ * Crypt file using Sodium and returns decrypt key.
* @param $fp_src file path to the file to crypt.
- * @param $fp_dst file path to the file to write crypted file (could be the same).
+ * @param $fp_dst file path to the file to write crypted file (must not be the same).
* @return decrypt key composed of the key and the iv separated by a point ('.')
*/
function jirafeau_encrypt_file($fp_src, $fp_dst)
{
$fs = filesize($fp_src);
- if ($fs === false || $fs == 0 || !(extension_loaded('mcrypt') == true)) {
+ if ($fs === false || $fs == 0 || extension_loaded('sodium') == false || $fp_src == $fp_dst) {
return '';
}
- /* Prepare module. */
- $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
/* Generate key. */
- $crypt_key = jirafeau_gen_random(10);
- $hash_key = md5($crypt_key);
- $iv = jirafeau_crypt_create_iv($hash_key, mcrypt_enc_get_iv_size($m));
+ $crypt_key = bin2hex(random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES / 2));
/* Init module. */
- mcrypt_generic_init($m, $hash_key, $iv);
+ [$crypt_state, $crypt_header] = sodium_crypto_secretstream_xchacha20poly1305_init_push($crypt_key);
/* Crypt file. */
- $r = fopen($fp_src, 'r');
- $w = fopen($fp_dst, 'c');
- while (!feof($r)) {
- $to_enc = fread($r, 1024);
- if (strlen($to_enc) > 0) {
- $enc = mcrypt_generic($m, $to_enc);
- if (fwrite($w, $enc) === false) {
- return '';
- }
+ $r = fopen($fp_src, 'rb');
+ $w = fopen($fp_dst, 'wb');
+ fwrite($w, $crypt_header);
+
+ for ($i = 0; $i < $fs; $i += JIRAFEAU_SODIUM_CHUNKSIZE) {
+ $to_enc = fread($r, JIRAFEAU_SODIUM_CHUNKSIZE);
+ $enc = sodium_crypto_secretstream_xchacha20poly1305_push($crypt_state, $to_enc);
+
+ if (fwrite($w, $enc) === false) {
+ return '';
}
}
+
fclose($r);
fclose($w);
+
/* Cleanup. */
- mcrypt_generic_deinit($m);
- mcrypt_module_close($m);
+ sodium_memzero($crypt_state);
+
return $crypt_key;
}
/**
- * Decrypt file.
+ * Decrypt file using Sodium.
* @param $fp_src file path to the file to decrypt.
- * @param $fp_dst file path to the file to write decrypted file (could be the same).
+ * @param $fp_dst file path to the file to write decrypted file (must not be the same).
* @param $k string composed of the key and the iv separated by a point ('.')
* @return key used to decrypt. a string of length 0 is returned if failed.
*/
function jirafeau_decrypt_file($fp_src, $fp_dst, $k)
+{
+ $fs = filesize($fp_src);
+ if ($fs === false || $fs == 0 || extension_loaded('sodium') == false || $fp_src == $fp_dst) {
+ return false;
+ }
+
+ /* Decrypt file. */
+ $r = fopen($fp_src, 'rb');
+ $w = fopen($fp_dst, 'wb');
+
+ $crypt_header = fread($r, SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES);
+
+ /* Init module. */
+ $crypt_state = sodium_crypto_secretstream_xchacha20poly1305_init_pull($crypt_header, $k);
+
+ /* Decrypt file. */
+
+ for ($i = SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES; $i < $fs; $i += JIRAFEAU_SODIUM_CHUNKSIZE + SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES) {
+ $to_dec = fread($r, JIRAFEAU_SODIUM_CHUNKSIZE + SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES);
+ [$dec, $crypt_tag] = sodium_crypto_secretstream_xchacha20poly1305_pull($crypt_state, $to_dec);
+
+ if (fwrite($w, $dec) === false) {
+ return false;
+ }
+ }
+
+ fclose($r);
+ fclose($w);
+
+ /* Cleanup. */
+ sodium_memzero($crypt_state);
+
+ return true;
+}
+
+/**
+ * Decrypt file using mcrypt.
+ * @param $fp_src file path to the file to decrypt.
+ * @param $fp_dst file path to the file to write decrypted file (could be the same).
+ * @param $k string composed of the key and the iv separated by a point ('.')
+ * @return key used to decrypt. a string of length 0 is returned if failed.
+ */
+function jirafeau_decrypt_file_legacy($fp_src, $fp_dst, $k)
{
$fs = filesize($fp_src);
if ($fs === false || $fs == 0 || extension_loaded('mcrypt') == false) {
function jirafeau_default_web_root()
{
- return $_SERVER['HTTP_HOST'] . str_replace('install.php', '', $_SERVER['REQUEST_URI']);
+ $url_scheme = (isset($_SERVER['HTTPS'])) ? 'https://' : 'http://';
+ return $url_scheme . $_SERVER['HTTP_HOST'] . str_replace('install.php', '', $_SERVER['REQUEST_URI']);
}
function jirafeau_get_download_stats($hash)