Merge pull request #682 from sfackler/sha-struct

Add SHA384 and SHA512
This commit is contained in:
Steven Fackler 2017-08-16 21:07:50 -07:00 committed by GitHub
commit 51471c7c8a
2 changed files with 110 additions and 0 deletions

View File

@ -151,6 +151,17 @@ pub struct SHA256_CTX {
pub md_len: c_uint,
}
#[repr(C)]
pub struct SHA512_CTX {
pub h: [SHA_LONG64; 8],
pub Nl: SHA_LONG64,
pub Nh: SHA_LONG64,
// this is a union but we don't want to require 1.19
u: [SHA_LONG64; SHA_LBLOCK as usize],
pub num: c_uint,
pub md_len: c_uint,
}
#[cfg(target_pointer_width = "64")]
pub type BN_ULONG = libc::c_ulonglong;
#[cfg(target_pointer_width = "32")]
@ -183,6 +194,7 @@ pub type PasswordCallback = unsafe extern "C" fn(buf: *mut c_char,
-> c_int;
pub type SHA_LONG = c_uint;
pub type SHA_LONG64 = u64;
pub const AES_ENCRYPT: c_int = 1;
pub const AES_DECRYPT: c_int = 0;
@ -2257,6 +2269,12 @@ extern "C" {
pub fn SHA224_Init(c: *mut SHA256_CTX) -> c_int;
pub fn SHA224_Update(c: *mut SHA256_CTX, data: *const c_void, len: size_t) -> c_int;
pub fn SHA224_Final(md: *mut c_uchar, c: *mut SHA256_CTX) -> c_int;
pub fn SHA384_Init(c: *mut SHA512_CTX) -> c_int;
pub fn SHA384_Update(c: *mut SHA512_CTX, data: *const c_void, len: size_t) -> c_int;
pub fn SHA384_Final(md: *mut c_uchar, c: *mut SHA512_CTX) -> c_int;
pub fn SHA512_Init(c: *mut SHA512_CTX) -> c_int;
pub fn SHA512_Update(c: *mut SHA512_CTX, data: *const c_void, len: size_t) -> c_int;
pub fn SHA512_Final(md: *mut c_uchar, c: *mut SHA512_CTX) -> c_int;
pub fn SSL_new(ctx: *mut SSL_CTX) -> *mut SSL;
pub fn SSL_pending(ssl: *const SSL) -> c_int;

View File

@ -168,6 +168,76 @@ impl Sha256 {
}
}
/// An object which calculates a SHA384 hash of some data.
pub struct Sha384(ffi::SHA512_CTX);
impl Sha384 {
/// Creates a new hasher.
#[inline]
pub fn new() -> Sha384 {
unsafe {
let mut ctx = mem::uninitialized();
ffi::SHA384_Init(&mut ctx);
Sha384(ctx)
}
}
/// Feeds some data into the hasher.
///
/// This can be called multiple times.
#[inline]
pub fn update(&mut self, buf: &[u8]) {
unsafe {
ffi::SHA384_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len());
}
}
/// Returns the hash of the data.
#[inline]
pub fn finish(mut self) -> [u8; 48] {
unsafe {
let mut hash: [u8; 48] = mem::uninitialized();
ffi::SHA384_Final(hash.as_mut_ptr(), &mut self.0);
hash
}
}
}
/// An object which calculates a SHA512 hash of some data.
pub struct Sha512(ffi::SHA512_CTX);
impl Sha512 {
/// Creates a new hasher.
#[inline]
pub fn new() -> Sha512 {
unsafe {
let mut ctx = mem::uninitialized();
ffi::SHA512_Init(&mut ctx);
Sha512(ctx)
}
}
/// Feeds some data into the hasher.
///
/// This can be called multiple times.
#[inline]
pub fn update(&mut self, buf: &[u8]) {
unsafe {
ffi::SHA512_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len());
}
}
/// Returns the hash of the data.
#[inline]
pub fn finish(mut self) -> [u8; 64] {
unsafe {
let mut hash: [u8; 64] = mem::uninitialized();
ffi::SHA512_Final(hash.as_mut_ptr(), &mut self.0);
hash
}
}
}
#[cfg(test)]
mod test {
use hex::ToHex;
@ -237,6 +307,17 @@ mod test {
assert_eq!((&sha384(data)[..]).to_hex(), expected);
}
#[test]
fn struct_384() {
let expected = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e\
7cc2358baeca134c825a7";
let mut hasher = Sha384::new();
hasher.update(b"a");
hasher.update(b"bc");
assert_eq!((&hasher.finish()[..]).to_hex(), expected);
}
#[test]
fn standalone_512() {
let data = b"abc";
@ -245,4 +326,15 @@ mod test {
assert_eq!((&sha512(data)[..]).to_hex(), expected);
}
#[test]
fn struct_512() {
let expected = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274\
fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
let mut hasher = Sha512::new();
hasher.update(b"a");
hasher.update(b"bc");
assert_eq!((&hasher.finish()[..]).to_hex(), expected);
}
}