From 9512f7cf408a51be0545bd63eccd30f7b6a730b9 Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 13 Sep 2020 09:12:19 +0100 Subject: [PATCH] fixed dumbshit --- Cargo.toml | 4 ++-- src/aes.rs | 1 - src/bytes.rs | 20 +++++++----------- src/password.rs | 2 +- src/rsa/private.rs | 43 ++++++++++++++++++++++++++++---------- src/rsa/private_offsets.rs | 1 - src/rsa/public.rs | 42 ++++++++++++++++++++++++++----------- src/rsa/public_offsets.rs | 1 - src/rsa/sign.rs | 1 + src/sha256.rs | 3 +-- 10 files changed, 75 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f61ddcf..f5f2abe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptohelpers" -version = "1.1.1" +version = "1.1.2" license= "MIT" description = "Collection of helpers and simplifying functions for cryptography things" authors = ["Avril "] @@ -22,7 +22,7 @@ serde_derive = {version = "1.0", optional = true} serde = {version = "1.0", optional = true} [features] -#default = ["full", "async", "serialise"] +default = ["full", "async", "serialise"] async = ["tokio"] # Actual things diff --git a/src/aes.rs b/src/aes.rs index b006dea..48cd68a 100644 --- a/src/aes.rs +++ b/src/aes.rs @@ -30,7 +30,6 @@ const BLOCKSIZE: usize = 16; /// A key and IV for the AES algorithm #[derive(Debug, PartialEq, Eq, Clone, Hash, Default)] -#[repr(C, packed)] pub struct AesKey { key: [u8; KEYSIZE], iv: [u8; IVSIZE], diff --git a/src/bytes.rs b/src/bytes.rs index e51203d..166bffd 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -51,39 +51,35 @@ pub fn refer_mut(value: &mut T) -> &mut [u8] /// /// # Notes /// This function omits bounds checks in production builds -pub unsafe fn derefer_unchecked(bytes: &[u8]) -> &T +pub unsafe fn derefer_unchecked(bytes: &[u8]) -> *const T { #[cfg(debug_assertions)] assert!(bytes.len() >= mem::size_of::(), "not enough bytes "); - &*(&bytes[0] as *const u8 as *const T) + &bytes[0] as *const u8 as *const T } /// Get a mutable reference to a type from its bytes /// /// # Notes /// This function omits bounds checks in production builds -pub unsafe fn derefer_unchecked_mut(bytes: &mut [u8]) -> &mut T +pub unsafe fn derefer_unchecked_mut(bytes: &mut [u8]) -> *mut T { #[cfg(debug_assertions)] assert!(bytes.len() >= mem::size_of::(), "not enough bytes "); - &mut *(&mut bytes[0] as *mut u8 as *mut T) + &mut bytes[0] as *mut u8 as *mut T } /// Get a type from its bytes -pub fn derefer(bytes: &[u8]) -> &T +pub fn derefer(bytes: &[u8]) -> *const T { assert!(bytes.len() >= mem::size_of::(), "not enough bytes "); - unsafe { - &*(&bytes[0] as *const u8 as *const T) - } + &bytes[0] as *const u8 as *const T } /// Get a mutable reference to a type from its bytes /// /// # Notes /// This function omits bounds checks in production builds -pub fn derefer_mut(bytes: &mut [u8]) -> &mut T +pub fn derefer_mut(bytes: &mut [u8]) -> *mut T { assert!(bytes.len() >= mem::size_of::(), "not enough bytes "); - unsafe { - &mut *(&mut bytes[0] as *mut u8 as *mut T) - } + &mut bytes[0] as *mut u8 as *mut T } diff --git a/src/password.rs b/src/password.rs index e289dd2..3fd4023 100644 --- a/src/password.rs +++ b/src/password.rs @@ -19,7 +19,7 @@ pub const ROUNDS: u32 = consts::PASSWORD_ROUNDS; /// Represents a password hash #[derive(Clone, Copy, PartialEq, Eq, Hash, Default)] -#[repr(C, packed)] +#[repr(transparent)] pub struct Password { derived: [u8; KEYSIZE], } diff --git a/src/rsa/private.rs b/src/rsa/private.rs index a7ac3e4..ad4851f 100644 --- a/src/rsa/private.rs +++ b/src/rsa/private.rs @@ -188,7 +188,10 @@ impl RsaPrivateKey return Err(Error::Binary(BinaryErrorKind::Length{expected: Some(OFF_SIZE), got: Some(bytes.len())})); } - let offset: &PrivateOffsetGroup = unsafe{bytes::derefer_unchecked(&bytes[..OFF_SIZE])}; + let offset = unsafe{ + bytes::derefer_unchecked::(&bytes[..OFF_SIZE]) + .read_unaligned() + }; let bytes = &bytes[OFF_SIZE..]; let sz = offset.body_len(); @@ -199,7 +202,7 @@ impl RsaPrivateKey Ok(Self{ data: Vec::from(&bytes[..]), offset_starts: offset.starts(), - offset: *offset, + offset, }) } @@ -245,12 +248,20 @@ impl RsaPrivateKey const OFF_SIZE: usize = size_of::(); let offset: PrivateOffsetGroup = { - let mut buffer = [0u8; OFF_SIZE]; + union BufHack { + buffer: [u8; OFF_SIZE], + res: PrivateOffsetGroup, + } + let mut offsets = BufHack{buffer: [0u8; OFF_SIZE]}; + unsafe { + let buffer = &mut offsets.buffer; - if buffer.len() != from.read_exact(&mut buffer[..]).await? { - return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read offsets")); - } else { - *unsafe{bytes::derefer_unchecked(&buffer[..])} + if buffer.len() != from.read_exact(&mut buffer[..]).await? { + return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read offsets")); + } + } + unsafe{ + offsets.res } }; @@ -271,11 +282,21 @@ impl RsaPrivateKey pub fn read_from_sync(&self, from: &mut T) -> io::Result where T: Read + ?Sized { + const OFF_SIZE: usize = size_of::(); + let offset: PrivateOffsetGroup = { - let mut buffer = [0u8; size_of::()]; - - from.read_exact(&mut buffer[..])?; - *unsafe{bytes::derefer_unchecked(&buffer[..])} + union BufHack { + buffer: [u8; OFF_SIZE], + res: PrivateOffsetGroup, + } + let mut offsets = BufHack{buffer: [0u8; OFF_SIZE]}; + unsafe { + let buffer = &mut offsets.buffer; + from.read_exact(&mut buffer[..])?; + } + unsafe { + offsets.res + } }; let mut data = vec![0u8; offset.body_len()]; diff --git a/src/rsa/private_offsets.rs b/src/rsa/private_offsets.rs index 1c9534d..bb8f885 100644 --- a/src/rsa/private_offsets.rs +++ b/src/rsa/private_offsets.rs @@ -1,7 +1,6 @@ //! Private offsets use super::offsets::*; -#[repr(C, packed)] #[derive(Clone,Copy,Debug,Eq,PartialEq,Hash,Default)] pub struct PrivateOffsetGroup { diff --git a/src/rsa/public.rs b/src/rsa/public.rs index 8d07a8d..5edc205 100644 --- a/src/rsa/public.rs +++ b/src/rsa/public.rs @@ -129,7 +129,10 @@ impl RsaPublicKey return Err(Error::Binary(BinaryErrorKind::Length{expected: Some(size_of::()), got: Some(bytes.len())})); } - let offset: &PublicOffsetGroup = unsafe {bytes::derefer_unchecked(&bytes[..size_of::()])}; + let offset = unsafe { + bytes::derefer_unchecked::(&bytes[..size_of::()]) + .read_unaligned() + }; let bytes = &bytes[size_of::()..]; let sz = offset.body_len(); @@ -140,7 +143,7 @@ impl RsaPublicKey Ok(Self { data: Vec::from(&bytes[..]), offset_starts: offset.starts(), - offset: *offset, + offset, }) } @@ -185,12 +188,19 @@ impl RsaPublicKey where T: AsyncRead + Unpin + ?Sized { let offset: PublicOffsetGroup = { - let mut buffer = [0u8; size_of::()]; - - if buffer.len() != from.read_exact(&mut buffer[..]).await? { - return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read offsets")); - } else { - *unsafe{bytes::derefer_unchecked(&buffer[..])} + union BufHack { + buffer: [u8; size_of::()], + offsets: PublicOffsetGroup, + } + let mut offsets = BufHack{buffer: [0u8; size_of::()]}; + unsafe { + let buffer = &mut offsets.buffer; + if buffer.len() != from.read_exact(&mut buffer[..]).await? { + return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read offsets")); + } + } + unsafe { + offsets.offsets } }; @@ -212,10 +222,18 @@ impl RsaPublicKey where T: Read + ?Sized { let offset: PublicOffsetGroup = { - let mut buffer = [0u8; size_of::()]; - - from.read_exact(&mut buffer[..])?; - *unsafe{bytes::derefer_unchecked(&buffer[..])} + union BufHack { + buffer: [u8; size_of::()], + offsets: PublicOffsetGroup, + } + let mut offsets = BufHack{buffer: [0u8; size_of::()]}; + unsafe { + let buffer = &mut offsets.buffer; + from.read_exact(&mut buffer[..])?; + } + unsafe { + offsets.offsets + } }; let mut data = vec![0u8; offset.body_len()]; diff --git a/src/rsa/public_offsets.rs b/src/rsa/public_offsets.rs index 1f3f9f0..b413f6c 100644 --- a/src/rsa/public_offsets.rs +++ b/src/rsa/public_offsets.rs @@ -1,7 +1,6 @@ //! Offsets for a public key container use super::offsets::*; -#[repr(C, packed)] #[derive(Clone,Copy,Debug,Eq,PartialEq,Hash,Default)] pub struct PublicOffsetGroup { diff --git a/src/rsa/sign.rs b/src/rsa/sign.rs index e23b280..cd8950a 100644 --- a/src/rsa/sign.rs +++ b/src/rsa/sign.rs @@ -38,6 +38,7 @@ use consts::BUFFER_SIZE; /// Represents an RSA signature #[derive(Copy, Clone)] +#[repr(transparent)] pub struct Signature([u8; SIZE]); impl Signature diff --git a/src/sha256.rs b/src/sha256.rs index 54159f4..91111b9 100644 --- a/src/sha256.rs +++ b/src/sha256.rs @@ -19,9 +19,8 @@ const SIZE: usize = consts::SHA256_SIZE; /// Represents a SHA256 hash #[derive(Clone, Copy, Default, PartialEq, Eq, Hash, Debug)] -#[repr(C, packed)] +#[repr(transparent)] #[cfg_attr(feature="serialise", derive(Serialize,Deserialize))] - pub struct Sha256Hash { hash: [u8; SIZE],