crypto.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::collections::HashMap;
  19. use crypto_box::{
  20. aead::{Aead, AeadCore},
  21. SalsaBox,
  22. };
  23. use rand::rngs::OsRng;
  24. use crate::{
  25. settings::{ChannelInfo, ContactInfo},
  26. Privmsg,
  27. };
  28. /// The format we're using is nonce+ciphertext, where nonce is 24 bytes.
  29. fn try_decrypt(salt_box: &SalsaBox, ciphertext: &str) -> Option<String> {
  30. let bytes = match bs58::decode(ciphertext).into_vec() {
  31. Ok(v) => v,
  32. Err(_) => return None,
  33. };
  34. if bytes.len() < 25 {
  35. return None
  36. }
  37. // Try extracting the nonce
  38. let nonce = match bytes[0..24].try_into() {
  39. Ok(v) => v,
  40. Err(_) => return None,
  41. };
  42. // Take the remaining ciphertext
  43. let message = &bytes[24..];
  44. // Try decrypting the message
  45. match salt_box.decrypt(nonce, message) {
  46. Ok(v) => Some(String::from_utf8_lossy(&v).to_string()),
  47. Err(_) => None,
  48. }
  49. }
  50. /// The format we're using is nonce+ciphertext, where nonce is 24 bytes.
  51. pub fn encrypt(salt_box: &SalsaBox, plaintext: &str) -> String {
  52. let nonce = SalsaBox::generate_nonce(&mut OsRng);
  53. let mut ciphertext = salt_box.encrypt(&nonce, plaintext.as_bytes()).unwrap();
  54. let mut concat = vec![];
  55. concat.append(&mut nonce.as_slice().to_vec());
  56. concat.append(&mut ciphertext);
  57. bs58::encode(concat).into_string()
  58. }
  59. /// Decrypt PrivMsg target
  60. pub fn decrypt_target(
  61. contact: &mut String,
  62. privmsg: &mut Privmsg,
  63. configured_chans: HashMap<String, ChannelInfo>,
  64. configured_contacts: HashMap<String, ContactInfo>,
  65. ) {
  66. for chan_name in configured_chans.keys() {
  67. let chan_info = configured_chans.get(chan_name).unwrap();
  68. if !chan_info.joined {
  69. continue
  70. }
  71. let salt_box = chan_info.salt_box.clone();
  72. if let Some(salt_box) = salt_box {
  73. let decrypted_target = try_decrypt(&salt_box, &privmsg.target);
  74. if decrypted_target.is_none() {
  75. continue
  76. }
  77. let target = decrypted_target.unwrap();
  78. if *chan_name == target {
  79. privmsg.target = target;
  80. return
  81. }
  82. }
  83. }
  84. for cnt_name in configured_contacts.keys() {
  85. let cnt_info = configured_contacts.get(cnt_name).unwrap();
  86. let salt_box = cnt_info.salt_box.clone();
  87. if let Some(salt_box) = salt_box {
  88. let decrypted_target = try_decrypt(&salt_box, &privmsg.target);
  89. if decrypted_target.is_none() {
  90. continue
  91. }
  92. let target = decrypted_target.unwrap();
  93. privmsg.target = target;
  94. *contact = cnt_name.into();
  95. return
  96. }
  97. }
  98. }
  99. /// Decrypt PrivMsg nickname and message
  100. pub fn decrypt_privmsg(salt_box: &SalsaBox, privmsg: &mut Privmsg) {
  101. let decrypted_nick = try_decrypt(&salt_box.clone(), &privmsg.nickname);
  102. let decrypted_msg = try_decrypt(&salt_box.clone(), &privmsg.message);
  103. if decrypted_nick.is_none() && decrypted_msg.is_none() {
  104. return
  105. }
  106. privmsg.nickname = decrypted_nick.unwrap();
  107. privmsg.message = decrypted_msg.unwrap();
  108. }
  109. /// Encrypt PrivMsg
  110. pub fn encrypt_privmsg(salt_box: &SalsaBox, privmsg: &mut Privmsg) {
  111. privmsg.nickname = encrypt(salt_box, &privmsg.nickname);
  112. privmsg.target = encrypt(salt_box, &privmsg.target);
  113. privmsg.message = encrypt(salt_box, &privmsg.message);
  114. }