fmt
This commit is contained in:
parent
f2bb938fa4
commit
de14367ce0
@ -84,7 +84,7 @@ impl Aes256Decryption {
|
|||||||
if encrypted_aes_key.len() <= 12 {
|
if encrypted_aes_key.len() <= 12 {
|
||||||
return Err(Error::msg("encrypted_aes_key is shorter than nonce length"));
|
return Err(Error::msg("encrypted_aes_key is shorter than nonce length"));
|
||||||
} // Actually we could probably test that if the remnant is not a multiple of 32, something's wrong
|
} // Actually we could probably test that if the remnant is not a multiple of 32, something's wrong
|
||||||
// take the first 12 bytes form encrypted_aes_key as nonce
|
// take the first 12 bytes form encrypted_aes_key as nonce
|
||||||
let (decrypt_key_nonce, encrypted_key) = encrypted_aes_key.split_at(12);
|
let (decrypt_key_nonce, encrypted_key) = encrypted_aes_key.split_at(12);
|
||||||
// decrypt key with shared_secret obtained from transaction
|
// decrypt key with shared_secret obtained from transaction
|
||||||
let decrypt_key_cipher = Aes256Gcm::new_from_slice(shared_secret.as_ref())
|
let decrypt_key_cipher = Aes256Gcm::new_from_slice(shared_secret.as_ref())
|
||||||
@ -121,7 +121,7 @@ impl Aes256Decryption {
|
|||||||
Purpose::Login => {
|
Purpose::Login => {
|
||||||
let half_key = self.decrypt_login()?;
|
let half_key = self.decrypt_login()?;
|
||||||
Ok(half_key.to_inner())
|
Ok(half_key.to_inner())
|
||||||
},
|
}
|
||||||
Purpose::ThirtyTwoBytes => {
|
Purpose::ThirtyTwoBytes => {
|
||||||
let thirty_two_buf = self.decrypt_thirty_two()?;
|
let thirty_two_buf = self.decrypt_thirty_two()?;
|
||||||
Ok(thirty_two_buf.to_vec())
|
Ok(thirty_two_buf.to_vec())
|
||||||
@ -225,7 +225,7 @@ impl Aes256Encryption {
|
|||||||
pub fn encrypt_with_aes_key(&self) -> Result<CipherText> {
|
pub fn encrypt_with_aes_key(&self) -> Result<CipherText> {
|
||||||
match self.purpose {
|
match self.purpose {
|
||||||
Purpose::Login => self.encrypt_login(),
|
Purpose::Login => self.encrypt_login(),
|
||||||
Purpose::ThirtyTwoBytes => self.encrypt_thirty_two()
|
Purpose::ThirtyTwoBytes => self.encrypt_thirty_two(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,13 +245,14 @@ impl Aes256Encryption {
|
|||||||
if self.plaintext.len() != 32 {
|
if self.plaintext.len() != 32 {
|
||||||
return Err(Error::msg("Invalid length, should be 32"));
|
return Err(Error::msg("Invalid length, should be 32"));
|
||||||
}
|
}
|
||||||
let mut thirty_two = [0u8;32];
|
let mut thirty_two = [0u8; 32];
|
||||||
thirty_two.copy_from_slice(&self.plaintext);
|
thirty_two.copy_from_slice(&self.plaintext);
|
||||||
let cipher = Aes256Gcm::new(&self.aes_key.into());
|
let cipher = Aes256Gcm::new(&self.aes_key.into());
|
||||||
let cipher_text = cipher
|
let cipher_text = cipher
|
||||||
.encrypt(&self.nonce.into(), thirty_two.as_slice())
|
.encrypt(&self.nonce.into(), thirty_two.as_slice())
|
||||||
.map_err(|e| Error::msg(format!("{}", e)))?;
|
.map_err(|e| Error::msg(format!("{}", e)))?;
|
||||||
let mut res = Vec::with_capacity(self.nonce.len() + cipher_text.len());
|
let mut res = Vec::with_capacity(self.nonce.len() + cipher_text.len());
|
||||||
|
log::info!("{}", cipher_text.len());
|
||||||
res.extend_from_slice(&self.nonce);
|
res.extend_from_slice(&self.nonce);
|
||||||
res.extend_from_slice(&cipher_text);
|
res.extend_from_slice(&cipher_text);
|
||||||
Ok(res)
|
Ok(res)
|
||||||
@ -267,8 +268,10 @@ mod tests {
|
|||||||
const ALICE_SP_ADDRESS: &str = "tsp1qqw3lqr6xravz9nf8ntazgwwl0fqv47kfjdxsnxs6eutavqfwyv5q6qk97mmyf6dtkdyzqlu2zv6h9j2ggclk7vn705q5u2phglpq7yw3dg5rwpdz";
|
const ALICE_SP_ADDRESS: &str = "tsp1qqw3lqr6xravz9nf8ntazgwwl0fqv47kfjdxsnxs6eutavqfwyv5q6qk97mmyf6dtkdyzqlu2zv6h9j2ggclk7vn705q5u2phglpq7yw3dg5rwpdz";
|
||||||
const BOB_SP_ADDRESS: &str = "tsp1qq2hlsgrj0gz8kcfkf9flqw5llz0u2vr04telqndku9mcqm6dl4fhvq60t8r78srrf56w9yr7w9e9dusc2wjqc30up6fjwnh9mw3e3veqegdmtf08";
|
const BOB_SP_ADDRESS: &str = "tsp1qq2hlsgrj0gz8kcfkf9flqw5llz0u2vr04telqndku9mcqm6dl4fhvq60t8r78srrf56w9yr7w9e9dusc2wjqc30up6fjwnh9mw3e3veqegdmtf08";
|
||||||
const TRANSACTION: &str = "4e6d03dec558e1b6624f813bf2da7cd8d8fb1c2296684c08cf38724dcfd8d10b";
|
const TRANSACTION: &str = "4e6d03dec558e1b6624f813bf2da7cd8d8fb1c2296684c08cf38724dcfd8d10b";
|
||||||
const ALICE_SHARED_SECRET: &str = "ccf02d364c2641ca129a3fdf49de57b705896e233f7ba6d738991993ea7e2106";
|
const ALICE_SHARED_SECRET: &str =
|
||||||
const BOB_SHARED_SECRET: &str = "15ef3e377fb842e81de52dbaaea8ba30aeb051a81043ee19264afd27353da521";
|
"ccf02d364c2641ca129a3fdf49de57b705896e233f7ba6d738991993ea7e2106";
|
||||||
|
const BOB_SHARED_SECRET: &str =
|
||||||
|
"15ef3e377fb842e81de52dbaaea8ba30aeb051a81043ee19264afd27353da521";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn new_aes_empty_plaintext() {
|
fn new_aes_empty_plaintext() {
|
||||||
@ -304,7 +307,12 @@ mod tests {
|
|||||||
let plaintext = [1u8; HALFKEYSIZE];
|
let plaintext = [1u8; HALFKEYSIZE];
|
||||||
let aes_key = Aes256Gcm::generate_key(&mut thread_rng());
|
let aes_key = Aes256Gcm::generate_key(&mut thread_rng());
|
||||||
let nonce = Aes256Gcm::generate_nonce(&mut thread_rng());
|
let nonce = Aes256Gcm::generate_nonce(&mut thread_rng());
|
||||||
let aes_enc = Aes256Encryption::import_key(Purpose::Login, plaintext.to_vec(), aes_key.into(), nonce.into());
|
let aes_enc = Aes256Encryption::import_key(
|
||||||
|
Purpose::Login,
|
||||||
|
plaintext.to_vec(),
|
||||||
|
aes_key.into(),
|
||||||
|
nonce.into(),
|
||||||
|
);
|
||||||
|
|
||||||
assert!(aes_enc.is_ok());
|
assert!(aes_enc.is_ok());
|
||||||
|
|
||||||
@ -312,10 +320,11 @@ mod tests {
|
|||||||
|
|
||||||
assert!(cipher.is_ok());
|
assert!(cipher.is_ok());
|
||||||
|
|
||||||
let mut plain_key = [0u8;32];
|
let mut plain_key = [0u8; 32];
|
||||||
plain_key.copy_from_slice(&aes_key.to_vec());
|
plain_key.copy_from_slice(&aes_key.to_vec());
|
||||||
|
|
||||||
let aes_dec = Aes256Decryption::new(Purpose::Login, cipher.unwrap(), plain_key.to_vec(), None);
|
let aes_dec =
|
||||||
|
Aes256Decryption::new(Purpose::Login, cipher.unwrap(), plain_key.to_vec(), None);
|
||||||
|
|
||||||
assert!(aes_dec.is_ok());
|
assert!(aes_dec.is_ok());
|
||||||
}
|
}
|
||||||
@ -370,8 +379,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn aes_encrypt_key_many() {
|
fn aes_encrypt_key_many() {
|
||||||
let plaintext = [1u8; HALFKEYSIZE];
|
let plaintext = [1u8; THIRTYTWO];
|
||||||
let mut aes_enc = Aes256Encryption::new(Purpose::Login, plaintext.to_vec()).unwrap();
|
let mut aes_enc =
|
||||||
|
Aes256Encryption::new(Purpose::ThirtyTwoBytes, plaintext.to_vec()).unwrap();
|
||||||
|
|
||||||
let mut shared_secrets: HashMap<Txid, _> = HashMap::new();
|
let mut shared_secrets: HashMap<Txid, _> = HashMap::new();
|
||||||
let mut sp_address2shared_secrets: HashMap<SilentPaymentAddress, SharedSecret> =
|
let mut sp_address2shared_secrets: HashMap<SilentPaymentAddress, SharedSecret> =
|
||||||
@ -396,31 +406,26 @@ mod tests {
|
|||||||
assert!(sp_address2encrypted_keys.is_ok());
|
assert!(sp_address2encrypted_keys.is_ok());
|
||||||
|
|
||||||
// Alice
|
// Alice
|
||||||
let encrypted_key = sp_address2encrypted_keys.as_mut()
|
let encrypted_key = sp_address2encrypted_keys
|
||||||
|
.as_mut()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get(&ALICE_SP_ADDRESS.try_into().unwrap())
|
.get(&ALICE_SP_ADDRESS.try_into().unwrap())
|
||||||
.cloned();
|
.cloned();
|
||||||
|
|
||||||
let ciphertext = aes_enc.encrypt_with_aes_key();
|
let ciphertext = aes_enc.encrypt_with_aes_key();
|
||||||
|
|
||||||
assert!(ciphertext.is_ok());
|
|
||||||
|
|
||||||
let aes_dec = Aes256Decryption::new(
|
let aes_dec = Aes256Decryption::new(
|
||||||
Purpose::Login,
|
Purpose::ThirtyTwoBytes,
|
||||||
ciphertext.unwrap(),
|
ciphertext.unwrap(),
|
||||||
encrypted_key.unwrap(),
|
encrypted_key.unwrap(),
|
||||||
Some(SharedSecret::from_str(ALICE_SHARED_SECRET).unwrap()),
|
Some(SharedSecret::from_str(ALICE_SHARED_SECRET).unwrap()),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(aes_dec.is_ok());
|
|
||||||
|
|
||||||
let retrieved_plain = aes_dec.unwrap().decrypt_with_key();
|
let retrieved_plain = aes_dec.unwrap().decrypt_with_key();
|
||||||
|
|
||||||
assert!(retrieved_plain.is_ok());
|
|
||||||
|
|
||||||
assert!(retrieved_plain.unwrap() == plaintext);
|
assert!(retrieved_plain.unwrap() == plaintext);
|
||||||
|
|
||||||
// Bob
|
// Bob
|
||||||
let encrypted_key = sp_address2encrypted_keys
|
let encrypted_key = sp_address2encrypted_keys
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get(&BOB_SP_ADDRESS.try_into().unwrap())
|
.get(&BOB_SP_ADDRESS.try_into().unwrap())
|
||||||
@ -428,21 +433,15 @@ mod tests {
|
|||||||
|
|
||||||
let ciphertext = aes_enc.encrypt_with_aes_key();
|
let ciphertext = aes_enc.encrypt_with_aes_key();
|
||||||
|
|
||||||
assert!(ciphertext.is_ok());
|
|
||||||
|
|
||||||
let aes_dec = Aes256Decryption::new(
|
let aes_dec = Aes256Decryption::new(
|
||||||
Purpose::Login,
|
Purpose::ThirtyTwoBytes,
|
||||||
ciphertext.unwrap(),
|
ciphertext.unwrap(),
|
||||||
encrypted_key.unwrap(),
|
encrypted_key.unwrap(),
|
||||||
Some(SharedSecret::from_str(BOB_SHARED_SECRET).unwrap()),
|
Some(SharedSecret::from_str(BOB_SHARED_SECRET).unwrap()),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(aes_dec.is_ok());
|
|
||||||
|
|
||||||
let retrieved_plain = aes_dec.unwrap().decrypt_with_key();
|
let retrieved_plain = aes_dec.unwrap().decrypt_with_key();
|
||||||
|
|
||||||
assert!(retrieved_plain.is_ok());
|
|
||||||
|
|
||||||
assert!(retrieved_plain.unwrap() == plaintext);
|
assert!(retrieved_plain.unwrap() == plaintext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user