~yujiri/libsufec

31f8438bef4535c03e4cda052795810adc5843ac — Yujiri 2 years ago 60d46a7
refactor
2 files changed, 9 insertions(+), 13 deletions(-)

M src/crypto.rs
M src/server.rs
M src/crypto.rs => src/crypto.rs +6 -9
@@ 57,15 57,12 @@ fn tripledh(
	let own_eph_their_eph = precompute(their_eph, own_eph);
	let own_id_their_eph = precompute(their_eph, own_id);
	let own_eph_their_id = precompute(their_id, own_eph);
	let shared_key = &own_eph_their_eph
		.0
		.iter()
		.zip(own_id_their_eph.0.iter())
		.map(|(a, b)| a ^ b)
		.zip(own_eph_their_id.0.iter())
		.map(|(a, b)| a ^ b)
		.collect::<Vec<u8>>();
	PrecomputedKey::from_slice(shared_key).unwrap()
	let mut shared_key = PrecomputedKey([0; PRECOMPUTEDKEYBYTES]);
	for i in 0..PRECOMPUTEDKEYBYTES {
		shared_key.0[i] = own_eph_their_eph.0[i] ^ own_id_their_eph.0[i] ^ own_eph_their_id.0[i];
	}
	println!("{:?}, {:?}, {:?}, {:?}", own_eph_their_eph.0, own_id_their_eph.0, own_eph_their_id.0, shared_key.0);
	shared_key
}

pub struct EncryptedStream {

M src/server.rs => src/server.rs +3 -4
@@ 41,13 41,12 @@ pub fn send(
	if keys_length == 0 {
		return Err(ServerError::RecipientNotFound);
	}
	let keys_buf = stream.receive(keys_length)?;
	// If we got an evil length, return error so we don't panic trying to convert the chunks to keys.
	if keys_buf.len() % PUBLICKEYBYTES != 0 {
	if keys_length % PUBLICKEYBYTES != 0 {
		return Err(ServerError::BadServer("invalid recipient keys"));
	}
	let keys = keys_buf.chunks(PUBLICKEYBYTES).map(|k| PublicKey::from_slice(k).unwrap());
	for key in keys {
	let keys_buf = stream.receive(keys_length)?;
	for key in keys_buf.chunks(PUBLICKEYBYTES).map(|k| PublicKey::from_slice(k).unwrap()) {
		let copy = encrypt_message(account, &recipient, &key, &message);
		stream.send(&(copy.len() as u32).to_be_bytes())?;
		stream.send(&copy)?;