Sfoglia il codice sorgente

darkirc: Port to crypto_box 0.9 and use chacha20 instead of salsa.

parazyd 3 anni fa
parent
commit
33ed6921e7

+ 1 - 1
bin/darkirc/Cargo.toml

@@ -19,7 +19,7 @@ futures = "0.3.28"
 rustls-pemfile = "1.0.3"
 
 # Crypto
-crypto_box = "0.8.2"
+crypto_box = {version = "0.9.0-rc.1", features = ["std", "chacha20"]}
 rand = "0.8.5"
 
 # Misc

+ 8 - 11
bin/darkirc/src/crypto.rs

@@ -20,7 +20,7 @@ use std::{collections::HashMap, fmt};
 
 use crypto_box::{
     aead::{Aead, AeadCore},
-    SalsaBox,
+    ChaChaBox,
 };
 use rand::rngs::OsRng;
 
@@ -42,7 +42,7 @@ impl fmt::Display for KeyPair {
 }
 
 /// The format we're using is nonce+ciphertext, where nonce is 24 bytes.
-fn try_decrypt(salt_box: &SalsaBox, ciphertext: &str) -> Option<String> {
+fn try_decrypt(salt_box: &ChaChaBox, ciphertext: &str) -> Option<String> {
     let bytes = match bs58::decode(ciphertext).into_vec() {
         Ok(v) => v,
         Err(_) => return None,
@@ -69,8 +69,8 @@ fn try_decrypt(salt_box: &SalsaBox, ciphertext: &str) -> Option<String> {
 }
 
 /// The format we're using is nonce+ciphertext, where nonce is 24 bytes.
-pub fn encrypt(salt_box: &SalsaBox, plaintext: &[u8]) -> String {
-    let nonce = SalsaBox::generate_nonce(&mut OsRng);
+pub fn encrypt(salt_box: &ChaChaBox, plaintext: &[u8]) -> String {
+    let nonce = ChaChaBox::generate_nonce(&mut OsRng);
     let mut ciphertext = salt_box.encrypt(&nonce, plaintext).unwrap();
 
     let mut concat = vec![];
@@ -92,9 +92,7 @@ pub fn decrypt_target(
             continue
         }
 
-        let salt_box = chan_info.salt_box.clone();
-
-        if let Some(salt_box) = salt_box {
+        if let Some(salt_box) = &chan_info.salt_box {
             let decrypted_target = try_decrypt(&salt_box, &privmsg.target);
             if decrypted_target.is_none() {
                 continue
@@ -112,8 +110,7 @@ pub fn decrypt_target(
     for cnt_name in configured_contacts.keys() {
         let cnt_info = configured_contacts.get(cnt_name).unwrap();
 
-        let salt_box = cnt_info.salt_box.clone();
-        if let Some(salt_box) = salt_box {
+        if let Some(salt_box) = &cnt_info.salt_box {
             let decrypted_target = try_decrypt(&salt_box, &privmsg.target);
             if decrypted_target.is_none() {
                 continue
@@ -129,7 +126,7 @@ pub fn decrypt_target(
 }
 
 /// Decrypt PrivMsg nickname and message
-pub fn decrypt_privmsg(salt_box: &SalsaBox, privmsg: &mut PrivMsgEvent) {
+pub fn decrypt_privmsg(salt_box: &ChaChaBox, privmsg: &mut PrivMsgEvent) {
     let decrypted_nick = try_decrypt(salt_box, &privmsg.nick);
     let decrypted_msg = try_decrypt(salt_box, &privmsg.msg);
 
@@ -142,7 +139,7 @@ pub fn decrypt_privmsg(salt_box: &SalsaBox, privmsg: &mut PrivMsgEvent) {
 }
 
 /// Encrypt PrivMsg
-pub fn encrypt_privmsg(salt_box: &SalsaBox, privmsg: &mut PrivMsgEvent) {
+pub fn encrypt_privmsg(salt_box: &ChaChaBox, privmsg: &mut PrivMsgEvent) {
     privmsg.nick = encrypt(salt_box, &pad(privmsg.nick.clone().into()));
     privmsg.target = encrypt(salt_box, &pad(privmsg.target.clone().into()));
     privmsg.msg = encrypt(salt_box, privmsg.msg.as_bytes());

+ 1 - 1
bin/darkirc/src/main.rs

@@ -70,7 +70,7 @@ async fn realmain(settings: Args, executor: Arc<smol::Executor<'_>>) -> Result<(
     if settings.gen_keypair {
         let secret_key = crypto_box::SecretKey::generate(&mut OsRng);
         let pub_key = secret_key.public_key();
-        let prv_encoded = bs58::encode(secret_key.as_bytes()).into_string();
+        let prv_encoded = bs58::encode(secret_key.to_bytes()).into_string();
         let pub_encoded = bs58::encode(pub_key.as_bytes()).into_string();
 
         let kp = KeyPair { private_key: prv_encoded, public_key: pub_encoded };

+ 8 - 7
bin/darkirc/src/settings.rs

@@ -16,7 +16,8 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use crypto_box::SalsaBox;
+use async_std::sync::Arc;
+use crypto_box::ChaChaBox;
 use log::{info, warn};
 use serde::{self, Deserialize};
 use std::collections::HashMap;
@@ -105,7 +106,7 @@ pub struct Args {
 #[derive(Clone)]
 pub struct ContactInfo {
     /// Optional NaCl box for the channel, used for {en,de}cryption.
-    pub salt_box: Option<SalsaBox>,
+    pub salt_box: Option<Arc<ChaChaBox>>,
 }
 
 impl ContactInfo {
@@ -132,7 +133,7 @@ pub struct ChannelInfo {
     /// Optional topic for the channel
     pub topic: Option<String>,
     /// Optional NaCl box for the channel, used for {en,de}cryption.
-    pub salt_box: Option<SalsaBox>,
+    pub salt_box: Option<Arc<ChaChaBox>>,
     /// Flag indicates whether the user has joined the channel or not
     pub joined: bool,
     /// All nicknames which are visible on the channel
@@ -187,7 +188,7 @@ pub fn parse_configured_channels(data: &str) -> Result<HashMap<String, ChannelIn
             // Build the NaCl box
             if let Some(s) = chan.1["secret"].as_str() {
                 let salt_box = salt_box_from_shared_secret(s)?;
-                channel_info.salt_box = Some(salt_box);
+                channel_info.salt_box = Some(Arc::new(salt_box));
                 info!("Instantiated NaCl box for channel {}", chan.0);
             }
         }
@@ -296,7 +297,7 @@ pub fn parse_configured_contacts(data: &str) -> Result<HashMap<String, ContactIn
         };
 
         let public = crypto_box::PublicKey::from(bytes);
-        contact_info.salt_box = Some(SalsaBox::new(&public, &secret));
+        contact_info.salt_box = Some(Arc::new(ChaChaBox::new(&public, &secret)));
         ret.insert(cnt.0.to_string(), contact_info);
         info!("Instantiated NaCl box for contact {}", cnt.0);
     }
@@ -304,11 +305,11 @@ pub fn parse_configured_contacts(data: &str) -> Result<HashMap<String, ContactIn
     Ok(ret)
 }
 
-fn salt_box_from_shared_secret(s: &str) -> Result<SalsaBox> {
+fn salt_box_from_shared_secret(s: &str) -> Result<ChaChaBox> {
     let bytes: [u8; 32] = bs58::decode(s).into_vec()?.try_into().unwrap();
     let secret = crypto_box::SecretKey::from(bytes);
     let public = secret.public_key();
-    Ok(SalsaBox::new(&public, &secret))
+    Ok(ChaChaBox::new(&public, &secret))
 }
 
 fn parse_priv_key(data: &str) -> Result<String> {