From 75a12694294adec7d684b10f406f71f37280abf9 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 8 May 2020 11:21:10 -0400 Subject: [PATCH] make id field in RSAIdentity private --- tor-bytes/src/impls.rs | 7 ++----- tor-llcrypto/src/pk/rsa.rs | 6 ++++-- tor-proto/src/crypto/handshake/ntor.rs | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/tor-bytes/src/impls.rs b/tor-bytes/src/impls.rs index ae9724169..a4f8d05db 100644 --- a/tor-bytes/src/impls.rs +++ b/tor-bytes/src/impls.rs @@ -191,20 +191,17 @@ mod curve25519_impls { /// Implement readable and writeable for the the RSAIdentity type. mod rsa_impls { use super::*; - use std::convert::TryInto; use tor_llcrypto::pk::rsa::*; impl Writeable for RSAIdentity { fn write_onto(&self, b: &mut B) { - b.write_all(&self.id[..]) + b.write_all(self.as_bytes()) } } impl Readable for RSAIdentity { fn take_from(b: &mut Reader<'_>) -> Result { let m = b.take(RSA_ID_LEN)?; - Ok(RSAIdentity { - id: m.try_into().expect("take is broken"), - }) + Ok(RSAIdentity::from_bytes(m).expect("take gave wrong length")) } } } diff --git a/tor-llcrypto/src/pk/rsa.rs b/tor-llcrypto/src/pk/rsa.rs index e19c75448..fbab93c75 100644 --- a/tor-llcrypto/src/pk/rsa.rs +++ b/tor-llcrypto/src/pk/rsa.rs @@ -21,7 +21,7 @@ pub const RSA_ID_LEN: usize = 20; /// identity key. These are used all over the Tor protocol. #[derive(Clone, Zeroize, Debug)] pub struct RSAIdentity { - pub id: [u8; RSA_ID_LEN], + id: [u8; RSA_ID_LEN], } impl PartialEq for RSAIdentity { @@ -42,8 +42,10 @@ impl RSAIdentity { /// Returns None if the input is not of the correct length. /// /// ``` + /// use tor_llcrypto::pk::rsa::RSAIdentity; + /// /// let bytes = b"xyzzyxyzzyxyzzyxyzzy"; - /// let id = RSAIdentity::from_bytes(&bytes); + /// let id = RSAIdentity::from_bytes(bytes); /// assert_eq!(id.unwrap().as_bytes(), bytes); /// /// let truncated = b"xyzzy"; diff --git a/tor-proto/src/crypto/handshake/ntor.rs b/tor-proto/src/crypto/handshake/ntor.rs index 17beb023b..257cbeb61 100644 --- a/tor-proto/src/crypto/handshake/ntor.rs +++ b/tor-proto/src/crypto/handshake/ntor.rs @@ -223,7 +223,7 @@ mod tests { let mut rng = rand_core::OsRng; let relay_secret = StaticSecret::new(&mut rng); let relay_public = PublicKey::from(&relay_secret); - let relay_identity = RSAIdentity { id: [12; 20] }; + let relay_identity = RSAIdentity::from_bytes(&[12; 20]).unwrap(); let relay_ntpk = NtorPublicKey { id: relay_identity, pk: relay_public.clone(), @@ -296,7 +296,7 @@ mod tests { let keys = hex!("0c62dee7f48893370d0ef896758d35729867beef1a5121df80e00f79ed349af39b51cae125719182f19d932a667dae1afbf2e336e6910e7822223e763afad0a13342157969dc6b79"); let relay_pk = NtorPublicKey { - id: RSAIdentity { id }, + id: RSAIdentity::from_bytes(&id).unwrap(), pk: b_pk.into(), }; let relay_sk = NtorSecretKey {