note.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. use crypto_api_chachapoly::ChachaPolyIetf;
  2. use darkfi_serial::{Decodable, Encodable, SerialDecodable, SerialEncodable};
  3. use rand::rngs::OsRng;
  4. use darkfi::{
  5. crypto::{
  6. diffie_hellman::{kdf_sapling, sapling_ka_agree},
  7. keypair::{PublicKey, SecretKey},
  8. },
  9. Error, Result,
  10. };
  11. pub const AEAD_TAG_SIZE: usize = 16;
  12. pub fn encrypt<T: Encodable>(note: &T, public: &PublicKey) -> Result<EncryptedNote2> {
  13. let ephem_secret = SecretKey::random(&mut OsRng);
  14. let ephem_public = PublicKey::from_secret(ephem_secret);
  15. let shared_secret = sapling_ka_agree(&ephem_secret, public);
  16. let key = kdf_sapling(&shared_secret, &ephem_public);
  17. let mut input = Vec::new();
  18. note.encode(&mut input)?;
  19. let mut ciphertext = vec![0; input.len() + AEAD_TAG_SIZE];
  20. assert_eq!(
  21. ChachaPolyIetf::aead_cipher()
  22. .seal_to(&mut ciphertext, &input, &[], key.as_ref(), &[0u8; 12])
  23. .unwrap(),
  24. input.len() + AEAD_TAG_SIZE
  25. );
  26. Ok(EncryptedNote2 { ciphertext, ephem_public })
  27. }
  28. #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  29. pub struct EncryptedNote2 {
  30. ciphertext: Vec<u8>,
  31. ephem_public: PublicKey,
  32. }
  33. impl EncryptedNote2 {
  34. pub fn decrypt<T: Decodable>(&self, secret: &SecretKey) -> Result<T> {
  35. let shared_secret = sapling_ka_agree(secret, &self.ephem_public);
  36. let key = kdf_sapling(&shared_secret, &self.ephem_public);
  37. let mut plaintext = vec![0; self.ciphertext.len()];
  38. assert_eq!(
  39. ChachaPolyIetf::aead_cipher()
  40. .open_to(&mut plaintext, &self.ciphertext, &[], key.as_ref(), &[0u8; 12])
  41. .map_err(|_| Error::NoteDecryptionFailed)?,
  42. self.ciphertext.len() - AEAD_TAG_SIZE
  43. );
  44. let t = T::decode(&plaintext[..])?;
  45. Ok(t)
  46. }
  47. }
  48. #[cfg(test)]
  49. mod tests {
  50. use super::*;
  51. use darkfi::crypto::{
  52. keypair::Keypair,
  53. types::{DrkCoinBlind, DrkSerial, DrkTokenId, DrkValueBlind},
  54. };
  55. use group::ff::Field;
  56. #[test]
  57. fn test_note_encdec() {
  58. #[derive(SerialEncodable, SerialDecodable)]
  59. struct MyNote {
  60. serial: DrkSerial,
  61. value: u64,
  62. token_id: DrkTokenId,
  63. coin_blind: DrkCoinBlind,
  64. value_blind: DrkValueBlind,
  65. token_blind: DrkValueBlind,
  66. memo: Vec<u8>,
  67. }
  68. let note = MyNote {
  69. serial: DrkSerial::random(&mut OsRng),
  70. value: 110,
  71. token_id: DrkTokenId::random(&mut OsRng),
  72. coin_blind: DrkCoinBlind::random(&mut OsRng),
  73. value_blind: DrkValueBlind::random(&mut OsRng),
  74. token_blind: DrkValueBlind::random(&mut OsRng),
  75. memo: vec![32, 223, 231, 3, 1, 1],
  76. };
  77. let keypair = Keypair::random(&mut OsRng);
  78. let encrypted_note = encrypt(&note, &keypair.public).unwrap();
  79. let note2: MyNote = encrypted_note.decrypt(&keypair.secret).unwrap();
  80. assert_eq!(note.value, note2.value);
  81. assert_eq!(note.token_id, note2.token_id);
  82. assert_eq!(note.token_blind, note2.token_blind);
  83. assert_eq!(note.memo, note2.memo);
  84. }
  85. }