tokio-1.0
Avril 4 years ago
parent 3cff2e90f1
commit 590fcedf81
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,6 +1,6 @@
[package]
name = "cryptohelpers"
version = "0.1.1"
version = "1.1.0"
license= "MIT"
description = "Collection of helpers and simplifying functions for cryptography things"
authors = ["Avril <flanchan@cumallover.me>"]
@ -18,8 +18,11 @@ crc = {version = "1.8", optional = true }
hex-literal = {version = "0.3", optional = true }
libc = "0.2"
tokio = {version = "0.2", features=["io-util"], optional=true}
serde_derive = {version = "1.0", optional = true}
serde = {version = "1.0", optional = true}
[features]
#default = ["full", "async", "serialise"]
async = ["tokio"]
# Actual things
@ -31,6 +34,8 @@ full = [
"rsa"
]
serialise = ["serde_derive"]
sha256 = ["sha2"]
password = ["sha256", "pbkdf2", "hex-literal", "hmac", "getrandom"]
aes = ["openssl", "getrandom"]

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

@ -24,6 +24,11 @@ mod bytes;
#[allow(unused_imports)]
mod error;
#[cfg(feature="serde")]
use serde_derive::{
Serialize, Deserialize,
};
// Actual things
#[cfg(feature="sha256")]

@ -26,7 +26,7 @@ pub trait PublicKey
fn get_pkey_pub(&self) -> Result<Cow<'_, PKey<Self::KeyType>>, Self::Error>;
/// Get or create an `Rsa` from this public key if possible
fn get_rsa_pub(&self) -> Result<Option<Cow<'_, Rsa<Self::KeyType>>>, Self::Error>
#[inline] fn get_rsa_pub(&self) -> Result<Option<Cow<'_, Rsa<Self::KeyType>>>, Self::Error>
{
Ok(self.get_pkey_pub()?.rsa().ok().map(|x| Cow::Owned(x)))
}

@ -188,7 +188,7 @@ impl RsaPrivateKey
return Err(Error::Binary(BinaryErrorKind::Length{expected: Some(OFF_SIZE), got: Some(bytes.len())}));
}
let offset: &PrivateOffsetGroup = bytes::derefer(&bytes[..OFF_SIZE]);
let offset: &PrivateOffsetGroup = unsafe{bytes::derefer_unchecked(&bytes[..OFF_SIZE])};
let bytes = &bytes[OFF_SIZE..];
let sz = offset.body_len();
@ -250,7 +250,7 @@ impl RsaPrivateKey
if buffer.len() != from.read_exact(&mut buffer[..]).await? {
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read offsets"));
} else {
*bytes::derefer(&buffer[..])
*unsafe{bytes::derefer_unchecked(&buffer[..])}
}
};
@ -275,7 +275,7 @@ impl RsaPrivateKey
let mut buffer = [0u8; size_of::<PrivateOffsetGroup>()];
from.read_exact(&mut buffer[..])?;
*bytes::derefer(&buffer[..])
*unsafe{bytes::derefer_unchecked(&buffer[..])}
};
let mut data = vec![0u8; offset.body_len()];

@ -129,7 +129,7 @@ impl RsaPublicKey
return Err(Error::Binary(BinaryErrorKind::Length{expected: Some(size_of::<PublicOffsetGroup>()), got: Some(bytes.len())}));
}
let offset: &PublicOffsetGroup = bytes::derefer(&bytes[..size_of::<PublicOffsetGroup>()]);
let offset: &PublicOffsetGroup = unsafe {bytes::derefer_unchecked(&bytes[..size_of::<PublicOffsetGroup>()])};
let bytes = &bytes[size_of::<PublicOffsetGroup>()..];
let sz = offset.body_len();
@ -190,7 +190,7 @@ impl RsaPublicKey
if buffer.len() != from.read_exact(&mut buffer[..]).await? {
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read offsets"));
} else {
*bytes::derefer(&buffer[..])
*unsafe{bytes::derefer_unchecked(&buffer[..])}
}
};
@ -215,7 +215,7 @@ impl RsaPublicKey
let mut buffer = [0u8; size_of::<PublicOffsetGroup>()];
from.read_exact(&mut buffer[..])?;
*bytes::derefer(&buffer[..])
*unsafe{bytes::derefer_unchecked(&buffer[..])}
};
let mut data = vec![0u8; offset.body_len()];

@ -20,6 +20,7 @@ const SIZE: usize = consts::SHA256_SIZE;
/// Represents a SHA256 hash
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, Debug)]
#[repr(C, packed)]
#[cfg_attr(feature="serde", derive(Serialize,Deserialize))]
pub struct Sha256Hash
{
hash: [u8; SIZE],

Loading…
Cancel
Save