Merge pull request #58 from cypherbits/new-encryption
New generation AES256-GCM encryption by libsodium. PHP >= 7.2 needed
This commit is contained in:
36
chat.php
36
chat.php
@@ -768,7 +768,7 @@ function restore_backup($C){
|
||||
$note['type']=1;
|
||||
}
|
||||
if(MSGENCRYPTED){
|
||||
$note['text']=openssl_encrypt($note['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$note['text']=base64_encode(sodium_crypto_aead_aes256gcm_encrypt($note['text'], '', AES_IV, ENCRYPTKEY));
|
||||
}
|
||||
$stmt->execute([$note['type'], $note['lastedited'], $note['editedby'], $note['text']]);
|
||||
}
|
||||
@@ -804,7 +804,7 @@ function send_backup($C){
|
||||
$result=$db->query('SELECT * FROM ' . PREFIX . "notes;");
|
||||
while($note=$result->fetch(PDO::FETCH_ASSOC)){
|
||||
if(MSGENCRYPTED){
|
||||
$note['text']=openssl_decrypt($note['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$note['text']=sodium_crypto_aead_aes256gcm_decrypt(base64_decode($note['text']), null, AES_IV, ENCRYPTKEY);
|
||||
}
|
||||
$code['notes'][]=$note;
|
||||
}
|
||||
@@ -1539,7 +1539,7 @@ function send_notes($type){
|
||||
}
|
||||
if(isset($_REQUEST['text'])){
|
||||
if(MSGENCRYPTED){
|
||||
$_REQUEST['text']=openssl_encrypt($_REQUEST['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$_REQUEST['text']=base64_encode(sodium_crypto_aead_aes256gcm_encrypt($_REQUEST['text'], '', AES_IV, ENCRYPTKEY));
|
||||
}
|
||||
$time=time();
|
||||
$stmt=$db->prepare('INSERT INTO ' . PREFIX . 'notes (type, lastedited, editedby, text) VALUES (?, ?, ?, ?);');
|
||||
@@ -1573,7 +1573,7 @@ function send_notes($type){
|
||||
$note['text']='';
|
||||
}
|
||||
if(MSGENCRYPTED){
|
||||
$note['text']=openssl_decrypt($note['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$note['text']=sodium_crypto_aead_aes256gcm_decrypt(base64_decode($note['text']), null, AES_IV, ENCRYPTKEY);
|
||||
}
|
||||
echo "</p>".form('notes');
|
||||
echo "$hiddendo<textarea name=\"text\">".htmlspecialchars($note['text']).'</textarea><br>';
|
||||
@@ -2932,7 +2932,7 @@ function validate_input(){
|
||||
'text' =>"<span class=\"usermsg\">$displaysend".style_this($message, $U['style']).'</span>'
|
||||
];
|
||||
if(MSGENCRYPTED){
|
||||
$newmessage['text']=openssl_encrypt($newmessage['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$newmessage['text']=base64_encode(sodium_crypto_aead_aes256gcm_encrypt($newmessage['text'], '', AES_IV, ENCRYPTKEY));
|
||||
}
|
||||
$stmt=$db->prepare('INSERT INTO ' . PREFIX . 'inbox (postdate, postid, poster, recipient, text) VALUES(?, ?, ?, ?, ?)');
|
||||
$stmt->execute([$newmessage['postdate'], $id[0], $newmessage['poster'], $newmessage['recipient'], $newmessage['text']]);
|
||||
@@ -3120,7 +3120,7 @@ function add_system_message($mes){
|
||||
function write_message($message){
|
||||
global $db;
|
||||
if(MSGENCRYPTED){
|
||||
$message['text']=openssl_encrypt($message['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$message['text']=base64_encode(sodium_crypto_aead_aes256gcm_encrypt($message['text'], '', AES_IV, ENCRYPTKEY));
|
||||
}
|
||||
$stmt=$db->prepare('INSERT INTO ' . PREFIX . 'messages (postdate, poststatus, poster, recipient, text, delstatus) VALUES (?, ?, ?, ?, ?, ?);');
|
||||
$stmt->execute([$message['postdate'], $message['poststatus'], $message['poster'], $message['recipient'], $message['text'], $message['delstatus']]);
|
||||
@@ -3241,7 +3241,7 @@ function print_messages($delstatus=0){
|
||||
|
||||
function prepare_message_print(&$message, $removeEmbed){
|
||||
if(MSGENCRYPTED){
|
||||
$message['text']=openssl_decrypt($message['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$message['text']=sodium_crypto_aead_aes256gcm_decrypt(base64_decode($message['text']), null, AES_IV, ENCRYPTKEY);
|
||||
}
|
||||
if($removeEmbed){
|
||||
$message['text']=preg_replace_callback('/<img src="([^"]+)"><\/a>/u',
|
||||
@@ -4002,16 +4002,16 @@ function update_db(){
|
||||
}
|
||||
update_setting('dbversion', DBVERSION);
|
||||
if($msgencrypted!==MSGENCRYPTED){
|
||||
if(!extension_loaded('openssl')){
|
||||
send_fatal_error($I['opensslextrequired']);
|
||||
if(!extension_loaded('sodium')){
|
||||
send_fatal_error($I['sodiumextrequired']);
|
||||
}
|
||||
$result=$db->query('SELECT id, text FROM ' . PREFIX . 'messages;');
|
||||
$stmt=$db->prepare('UPDATE ' . PREFIX . 'messages SET text=? WHERE id=?;');
|
||||
while($message=$result->fetch(PDO::FETCH_ASSOC)){
|
||||
if(MSGENCRYPTED){
|
||||
$message['text']=openssl_encrypt($message['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$message['text']=base64_encode(sodium_crypto_aead_aes256gcm_encrypt($message['text'], '', AES_IV, ENCRYPTKEY));
|
||||
}else{
|
||||
$message['text']=openssl_decrypt($message['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$message['text']=sodium_crypto_aead_aes256gcm_decrypt(base64_decode($message['text']), null, AES_IV, ENCRYPTKEY);
|
||||
}
|
||||
$stmt->execute([$message['text'], $message['id']]);
|
||||
}
|
||||
@@ -4019,9 +4019,9 @@ function update_db(){
|
||||
$stmt=$db->prepare('UPDATE ' . PREFIX . 'notes SET text=? WHERE id=?;');
|
||||
while($message=$result->fetch(PDO::FETCH_ASSOC)){
|
||||
if(MSGENCRYPTED){
|
||||
$message['text']=openssl_encrypt($message['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$message['text']=base64_encode(sodium_crypto_aead_aes256gcm_encrypt($message['text'], '', AES_IV, ENCRYPTKEY));
|
||||
}else{
|
||||
$message['text']=openssl_decrypt($message['text'], 'aes-256-cbc', ENCRYPTKEY, 0, '1234567890123456');
|
||||
$message['text']=sodium_crypto_aead_aes256gcm_decrypt(base64_decode($message['text']), null, AES_IV, ENCRYPTKEY);
|
||||
}
|
||||
$stmt->execute([$message['text'], $message['id']]);
|
||||
}
|
||||
@@ -4180,10 +4180,11 @@ function load_lang(){
|
||||
|
||||
function load_config(){
|
||||
mb_internal_encoding('UTF-8');
|
||||
define('VERSION', '1.23.7'); // Script version
|
||||
define('VERSION', '1.24'); // Script version
|
||||
define('DBVERSION', 42); // Database layout version
|
||||
define('MSGENCRYPTED', false); // Store messages encrypted in the database to prevent other database users from reading them - true/false - visit the setup page after editing!
|
||||
define('ENCRYPTKEY', 'MY_KEY'); // Encryption key for messages
|
||||
define('ENCRYPTKEY_PASS', 'MY_SECRET_KEY'); // Encryption key for messages
|
||||
define('AES_IV_PASS', '1234567890123456'); //AES Encryption IV
|
||||
define('DBHOST', 'localhost'); // Database host
|
||||
define('DBUSER', 'www-data'); // Database user
|
||||
define('DBPASS', 'YOUR_DB_PASS'); // Database password
|
||||
@@ -4201,4 +4202,9 @@ function load_config(){
|
||||
}
|
||||
define('COOKIENAME', PREFIX . 'chat_session'); // Cookie name storing the session information
|
||||
define('LANG', 'en'); // Default language
|
||||
if (MSGENCRYPTED){
|
||||
//Do not touch: Compute real keys needed by encryption functions
|
||||
define('ENCRYPTKEY', substr(hash("sha512/256",ENCRYPTKEY_PASS),0, SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES));
|
||||
define('AES_IV', substr(hash("sha512/256",AES_IV_PASS), 0, SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user