Add a stateful SHA256 hasher

This commit is contained in:
Steven Fackler 2017-08-14 17:07:44 -07:00
parent 8bde3af884
commit 1d92ff290e
2 changed files with 64 additions and 0 deletions

View File

@ -128,6 +128,16 @@ pub struct X509V3_CTX {
// Maybe more here // Maybe more here
} }
#[repr(C)]
pub struct SHA256_CTX {
pub h: [SHA_LONG; 8],
pub Nl: SHA_LONG,
pub Nh: SHA_LONG,
pub data: [SHA_LONG; SHA_LBLOCK as usize],
pub num: c_uint,
pub md_len: c_uint,
}
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]
pub type BN_ULONG = libc::c_ulonglong; pub type BN_ULONG = libc::c_ulonglong;
#[cfg(target_pointer_width = "32")] #[cfg(target_pointer_width = "32")]
@ -159,6 +169,8 @@ pub type PasswordCallback = unsafe extern "C" fn(buf: *mut c_char,
user_data: *mut c_void) user_data: *mut c_void)
-> c_int; -> c_int;
pub type SHA_LONG = c_uint;
pub const AES_ENCRYPT: c_int = 1; pub const AES_ENCRYPT: c_int = 1;
pub const AES_DECRYPT: c_int = 0; pub const AES_DECRYPT: c_int = 0;
@ -1169,6 +1181,8 @@ pub const RSA_NO_PADDING: c_int = 3;
pub const RSA_PKCS1_OAEP_PADDING: c_int = 4; pub const RSA_PKCS1_OAEP_PADDING: c_int = 4;
pub const RSA_X931_PADDING: c_int = 5; pub const RSA_X931_PADDING: c_int = 5;
pub const SHA_LBLOCK: c_int = 16;
pub const SSL_CTRL_SET_TMP_DH: c_int = 3; pub const SSL_CTRL_SET_TMP_DH: c_int = 3;
pub const SSL_CTRL_SET_TMP_ECDH: c_int = 4; pub const SSL_CTRL_SET_TMP_ECDH: c_int = 4;
pub const SSL_CTRL_EXTRA_CHAIN_CERT: c_int = 14; pub const SSL_CTRL_EXTRA_CHAIN_CERT: c_int = 14;
@ -2221,6 +2235,10 @@ extern "C" {
pub fn SHA384(d: *const c_uchar, n: size_t, md: *mut c_uchar) -> *mut c_uchar; pub fn SHA384(d: *const c_uchar, n: size_t, md: *mut c_uchar) -> *mut c_uchar;
pub fn SHA512(d: *const c_uchar, n: size_t, md: *mut c_uchar) -> *mut c_uchar; pub fn SHA512(d: *const c_uchar, n: size_t, md: *mut c_uchar) -> *mut c_uchar;
pub fn SHA256_Init(c: *mut SHA256_CTX) -> c_int;
pub fn SHA256_Update(c: *mut SHA256_CTX, data: *const c_void, len: size_t) -> c_int;
pub fn SHA256_Final(md: *mut c_uchar, c: *mut SHA256_CTX) -> c_int;
pub fn SSL_new(ctx: *mut SSL_CTX) -> *mut SSL; pub fn SSL_new(ctx: *mut SSL_CTX) -> *mut SSL;
pub fn SSL_pending(ssl: *const SSL) -> c_int; pub fn SSL_pending(ssl: *const SSL) -> c_int;
pub fn SSL_free(ssl: *mut SSL); pub fn SSL_free(ssl: *mut SSL);

View File

@ -1,4 +1,5 @@
//! The SHA family of hashes. //! The SHA family of hashes.
use libc::c_void;
use ffi; use ffi;
use std::mem; use std::mem;
@ -57,6 +58,41 @@ pub fn sha512(data: &[u8]) -> [u8; 64] {
} }
} }
/// An object which calculates a SHA256 hash of some data.
pub struct Sha256(ffi::SHA256_CTX);
impl Sha256 {
/// Creates a new hasher.
#[inline]
pub fn new() -> Sha256 {
unsafe {
let mut ctx = mem::uninitialized();
ffi::SHA256_Init(&mut ctx);
Sha256(ctx)
}
}
/// Feeds some data into the hasher.
///
/// This can be called multiple times.
#[inline]
pub fn update(&mut self, buf: &[u8]) {
unsafe {
ffi::SHA256_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; 32] {
unsafe {
let mut hash: [u8; 32] = mem::uninitialized();
ffi::SHA256_Final(hash.as_mut_ptr(), &mut self.0);
hash
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use hex::ToHex; use hex::ToHex;
@ -87,6 +123,16 @@ mod test {
assert_eq!(sha256(data).to_hex(), expected); assert_eq!(sha256(data).to_hex(), expected);
} }
#[test]
fn struct_256() {
let expected = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
let mut hasher = Sha256::new();
hasher.update(b"a");
hasher.update(b"bc");
assert_eq!(hasher.finish().to_hex(), expected);
}
#[test] #[test]
fn standalone_384() { fn standalone_384() {
let data = b"abc"; let data = b"abc";