fixed dumbshit

tokio-1.0
Avril 4 years ago
parent 66d9ff4774
commit 9512f7cf40
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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 <flanchan@cumallover.me>"]
@ -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

@ -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],

@ -51,39 +51,35 @@ pub fn refer_mut<T: ?Sized>(value: &mut T) -> &mut [u8]
///
/// # Notes
/// This function omits bounds checks in production builds
pub unsafe fn derefer_unchecked<T>(bytes: &[u8]) -> &T
pub unsafe fn derefer_unchecked<T>(bytes: &[u8]) -> *const T
{
#[cfg(debug_assertions)] assert!(bytes.len() >= mem::size_of::<T>(), "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<T>(bytes: &mut [u8]) -> &mut T
pub unsafe fn derefer_unchecked_mut<T>(bytes: &mut [u8]) -> *mut T
{
#[cfg(debug_assertions)] assert!(bytes.len() >= mem::size_of::<T>(), "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<T>(bytes: &[u8]) -> &T
pub fn derefer<T>(bytes: &[u8]) -> *const T
{
assert!(bytes.len() >= mem::size_of::<T>(), "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<T>(bytes: &mut [u8]) -> &mut T
pub fn derefer_mut<T>(bytes: &mut [u8]) -> *mut T
{
assert!(bytes.len() >= mem::size_of::<T>(), "not enough bytes ");
unsafe {
&mut *(&mut bytes[0] as *mut u8 as *mut T)
}
&mut bytes[0] as *mut u8 as *mut T
}

@ -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],
}

@ -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::<PrivateOffsetGroup>(&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::<PrivateOffsetGroup>();
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[..])}
}
}
unsafe{
offsets.res
}
};
@ -271,11 +282,21 @@ impl RsaPrivateKey
pub fn read_from_sync<T>(&self, from: &mut T) -> io::Result<Self>
where T: Read + ?Sized
{
let offset: PrivateOffsetGroup = {
let mut buffer = [0u8; size_of::<PrivateOffsetGroup>()];
const OFF_SIZE: usize = size_of::<PrivateOffsetGroup>();
let offset: PrivateOffsetGroup = {
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{bytes::derefer_unchecked(&buffer[..])}
}
unsafe {
offsets.res
}
};
let mut data = vec![0u8; offset.body_len()];

@ -1,7 +1,6 @@
//! Private offsets
use super::offsets::*;
#[repr(C, packed)]
#[derive(Clone,Copy,Debug,Eq,PartialEq,Hash,Default)]
pub struct PrivateOffsetGroup
{

@ -129,7 +129,10 @@ impl RsaPublicKey
return Err(Error::Binary(BinaryErrorKind::Length{expected: Some(size_of::<PublicOffsetGroup>()), got: Some(bytes.len())}));
}
let offset: &PublicOffsetGroup = unsafe {bytes::derefer_unchecked(&bytes[..size_of::<PublicOffsetGroup>()])};
let offset = unsafe {
bytes::derefer_unchecked::<PublicOffsetGroup>(&bytes[..size_of::<PublicOffsetGroup>()])
.read_unaligned()
};
let bytes = &bytes[size_of::<PublicOffsetGroup>()..];
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::<PublicOffsetGroup>()];
union BufHack {
buffer: [u8; size_of::<PublicOffsetGroup>()],
offsets: PublicOffsetGroup,
}
let mut offsets = BufHack{buffer: [0u8; size_of::<PublicOffsetGroup>()]};
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[..])}
}
}
unsafe {
offsets.offsets
}
};
@ -212,10 +222,18 @@ impl RsaPublicKey
where T: Read + ?Sized
{
let offset: PublicOffsetGroup = {
let mut buffer = [0u8; size_of::<PublicOffsetGroup>()];
union BufHack {
buffer: [u8; size_of::<PublicOffsetGroup>()],
offsets: PublicOffsetGroup,
}
let mut offsets = BufHack{buffer: [0u8; size_of::<PublicOffsetGroup>()]};
unsafe {
let buffer = &mut offsets.buffer;
from.read_exact(&mut buffer[..])?;
*unsafe{bytes::derefer_unchecked(&buffer[..])}
}
unsafe {
offsets.offsets
}
};
let mut data = vec![0u8; offset.body_len()];

@ -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
{

@ -38,6 +38,7 @@ use consts::BUFFER_SIZE;
/// Represents an RSA signature
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Signature([u8; SIZE]);
impl Signature

@ -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],

Loading…
Cancel
Save