Added Header export when decrypting

This commit is contained in:
Hannes 2021-07-30 17:54:21 +02:00
parent 406afe08d3
commit de3e308d09
2 changed files with 20 additions and 5 deletions

View file

@ -6,7 +6,7 @@ homepage = "https://github.com/Dione-Software/double-ratchet-2"
repository = "https://github.com/Dione-Software/double-ratchet-2" repository = "https://github.com/Dione-Software/double-ratchet-2"
readme = "README.md" readme = "README.md"
keywords = ["double-ratchet", "crypto", "cryptography", "signal"] keywords = ["double-ratchet", "crypto", "cryptography", "signal"]
version = "0.3.2" version = "0.3.3"
edition = "2018" edition = "2018"
license = "MIT" license = "MIT"

View file

@ -257,7 +257,7 @@ impl RatchetEncHeader {
} }
fn try_skipped_message_keys(&mut self, enc_header: &(Vec<u8>, [u8; 12]), fn try_skipped_message_keys(&mut self, enc_header: &(Vec<u8>, [u8; 12]),
ciphertext: &[u8], nonce: &[u8; 12], ad: &[u8]) -> Option<Vec<u8>> { ciphertext: &[u8], nonce: &[u8; 12], ad: &[u8]) -> (Option<Vec<u8>>, Option<Header>) {
let ret_data = self.mkskipped.clone().into_iter().find(|e| { let ret_data = self.mkskipped.clone().into_iter().find(|e| {
let header = Header::decrypt(&e.0.0, &enc_header.0, &enc_header.1); let header = Header::decrypt(&e.0.0, &enc_header.0, &enc_header.1);
@ -267,12 +267,12 @@ impl RatchetEncHeader {
} }
}); });
match ret_data { match ret_data {
None => { None }, None => { (None, None) },
Some(data) => { Some(data) => {
let header = Header::decrypt(&data.0.0, &enc_header.0, &enc_header.1); let header = Header::decrypt(&data.0.0, &enc_header.0, &enc_header.1);
let mk = data.1; let mk = data.1;
self.mkskipped.remove(&(data.0.0, data.0.1)); self.mkskipped.remove(&(data.0.0, data.0.1));
Some(decrypt(&mk, ciphertext, &header.unwrap().concat(ad), nonce)) (Some(decrypt(&mk, ciphertext, &header.clone().unwrap().concat(ad), nonce)), header)
} }
} }
} }
@ -323,7 +323,7 @@ impl RatchetEncHeader {
} }
pub fn ratchet_decrypt(&mut self, enc_header: &(Vec<u8>, [u8; 12]), ciphertext: &[u8], nonce: &[u8; 12], ad: &[u8]) -> Vec<u8> { pub fn ratchet_decrypt(&mut self, enc_header: &(Vec<u8>, [u8; 12]), ciphertext: &[u8], nonce: &[u8; 12], ad: &[u8]) -> Vec<u8> {
let plaintext = self.try_skipped_message_keys(enc_header, ciphertext, nonce, ad); let (plaintext, _) = self.try_skipped_message_keys(enc_header, ciphertext, nonce, ad);
if let Some(d) = plaintext { return d }; if let Some(d) = plaintext { return d };
let (header, dh_ratchet) = self.decrypt_header(enc_header).unwrap(); let (header, dh_ratchet) = self.decrypt_header(enc_header).unwrap();
if dh_ratchet { if dh_ratchet {
@ -336,4 +336,19 @@ impl RatchetEncHeader {
self.nr += 1; self.nr += 1;
decrypt(&mk, ciphertext, &header.concat(ad), nonce) decrypt(&mk, ciphertext, &header.concat(ad), nonce)
} }
pub fn ratchet_decrypt_w_header(&mut self, enc_header: &(Vec<u8>, [u8; 12]), ciphertext: &[u8], nonce: &[u8; 12], ad: &[u8]) -> (Vec<u8>, Header) {
let (plaintext, header) = self.try_skipped_message_keys(enc_header, ciphertext, nonce, ad);
if let Some(d) = plaintext { return (d, header.unwrap()) };
let (header, dh_ratchet) = self.decrypt_header(enc_header).unwrap();
if dh_ratchet {
self.skip_message_keys(header.pn).unwrap();
self.dhratchet(&header);
}
self.skip_message_keys(header.n).unwrap();
let (ckr, mk) = kdf_ck(&self.ckr.unwrap());
self.ckr = Some(ckr);
self.nr += 1;
(decrypt(&mk, ciphertext, &header.concat(ad), nonce), header)
}
} }