note.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. use std::io;
  2. use crypto_api_chachapoly::ChachaPolyIetf;
  3. use rand::rngs::OsRng;
  4. use crate::{
  5. crypto::{
  6. diffie_hellman::{kdf_sapling, sapling_ka_agree},
  7. keypair::{PublicKey, SecretKey},
  8. },
  9. serial::{Decodable, Encodable, ReadExt, WriteExt},
  10. types::*,
  11. Error, Result,
  12. };
  13. pub const NOTE_PLAINTEXT_SIZE: usize = 32 + // serial
  14. 8 + // value
  15. 32 + // token_id
  16. 32 + // coin_blind
  17. 32; // value_blind
  18. pub const AEAD_TAG_SIZE: usize = 16;
  19. pub const ENC_CIPHERTEXT_SIZE: usize = NOTE_PLAINTEXT_SIZE + AEAD_TAG_SIZE;
  20. #[derive(Clone)]
  21. pub struct Note {
  22. pub serial: DrkSerial,
  23. pub value: u64,
  24. pub token_id: DrkTokenId,
  25. pub coin_blind: DrkCoinBlind,
  26. pub value_blind: DrkValueBlind,
  27. }
  28. impl Encodable for Note {
  29. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  30. let mut len = 0;
  31. len += self.serial.encode(&mut s)?;
  32. len += self.value.encode(&mut s)?;
  33. len += self.token_id.encode(&mut s)?;
  34. len += self.coin_blind.encode(&mut s)?;
  35. len += self.value_blind.encode(&mut s)?;
  36. Ok(len)
  37. }
  38. }
  39. impl Decodable for Note {
  40. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  41. Ok(Self {
  42. serial: Decodable::decode(&mut d)?,
  43. value: Decodable::decode(&mut d)?,
  44. token_id: Decodable::decode(&mut d)?,
  45. coin_blind: Decodable::decode(&mut d)?,
  46. value_blind: Decodable::decode(d)?,
  47. })
  48. }
  49. }
  50. impl Note {
  51. pub fn encrypt(&self, public: &PublicKey) -> Result<EncryptedNote> {
  52. let ephem_secret = SecretKey::random(&mut OsRng);
  53. let ephem_public = PublicKey::from_secret(ephem_secret);
  54. let shared_secret = sapling_ka_agree(&ephem_secret, public);
  55. let key = kdf_sapling(&shared_secret, &ephem_public);
  56. let mut input = Vec::new();
  57. self.encode(&mut input)?;
  58. let mut ciphertext = [0u8; ENC_CIPHERTEXT_SIZE];
  59. assert_eq!(
  60. ChachaPolyIetf::aead_cipher()
  61. .seal_to(&mut ciphertext, &input, &[], key.as_ref(), &[0u8; 12])
  62. .unwrap(),
  63. ENC_CIPHERTEXT_SIZE
  64. );
  65. Ok(EncryptedNote { ciphertext, ephem_public })
  66. }
  67. }
  68. pub struct EncryptedNote {
  69. ciphertext: [u8; ENC_CIPHERTEXT_SIZE],
  70. ephem_public: PublicKey,
  71. }
  72. impl Encodable for EncryptedNote {
  73. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  74. let mut len = 0;
  75. s.write_slice(&self.ciphertext)?;
  76. len += ENC_CIPHERTEXT_SIZE;
  77. len += self.ephem_public.encode(&mut s)?;
  78. Ok(len)
  79. }
  80. }
  81. impl Decodable for EncryptedNote {
  82. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  83. let mut ciphertext = [0u8; ENC_CIPHERTEXT_SIZE];
  84. d.read_slice(&mut ciphertext[..])?;
  85. Ok(Self { ciphertext, ephem_public: Decodable::decode(d)? })
  86. }
  87. }
  88. impl EncryptedNote {
  89. pub fn decrypt(&self, secret: &SecretKey) -> Result<Note> {
  90. let shared_secret = sapling_ka_agree(&secret, &self.ephem_public);
  91. let key = kdf_sapling(&shared_secret, &self.ephem_public);
  92. let mut plaintext = [0; ENC_CIPHERTEXT_SIZE];
  93. assert_eq!(
  94. ChachaPolyIetf::aead_cipher()
  95. .open_to(&mut plaintext, &self.ciphertext, &[], key.as_ref(), &[0u8; 12])
  96. .map_err(|_| Error::NoteDecryptionFailed)?,
  97. NOTE_PLAINTEXT_SIZE
  98. );
  99. Note::decode(&plaintext[..])
  100. }
  101. }
  102. #[test]
  103. fn test_note_encdec() {
  104. use crate::types::*;
  105. let note = Note {
  106. serial: DrkSerial::random(&mut OsRng),
  107. value: 110,
  108. token_id: DrkTokenId::random(&mut OsRng),
  109. coin_blind: DrkCoinBlind::random(&mut OsRng),
  110. value_blind: DrkValueBlind::random(&mut OsRng),
  111. };
  112. let secret = DrkSecretKey::random(&mut OsRng);
  113. let public = derive_public_key(secret);
  114. let encrypted_note = note.encrypt(&public).unwrap();
  115. let note2 = encrypted_note.decrypt(&secret).unwrap();
  116. assert_eq!(note.value, note2.value);
  117. assert_eq!(note.token_id, note2.token_id);
  118. }