aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2017-08-14 17:12:55 -0700
committerGitHub <[email protected]>2017-08-14 17:12:55 -0700
commitdfdf4e4d3867ef18c32e4c691023c3978537acd2 (patch)
tree4178ad5d4cea70a016f40d3dabbe66cadd37ab9c
parentMerge pull request #679 from mcgoo/vcpkg (diff)
parentAdd a stateful SHA256 hasher (diff)
downloadrust-openssl-dfdf4e4d3867ef18c32e4c691023c3978537acd2.tar.xz
rust-openssl-dfdf4e4d3867ef18c32e4c691023c3978537acd2.zip
Merge pull request #680 from sfackler/sha256-state
Add a stateful SHA256 hasher
-rw-r--r--openssl-sys/src/lib.rs18
-rw-r--r--openssl/src/sha.rs46
2 files changed, 64 insertions, 0 deletions
diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs
index fe50dcad..6f33678f 100644
--- a/openssl-sys/src/lib.rs
+++ b/openssl-sys/src/lib.rs
@@ -128,6 +128,16 @@ pub struct X509V3_CTX {
// 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")]
pub type BN_ULONG = libc::c_ulonglong;
#[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)
-> c_int;
+pub type SHA_LONG = c_uint;
+
pub const AES_ENCRYPT: c_int = 1;
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_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_ECDH: c_int = 4;
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 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_pending(ssl: *const SSL) -> c_int;
pub fn SSL_free(ssl: *mut SSL);
diff --git a/openssl/src/sha.rs b/openssl/src/sha.rs
index 949e7307..c4141094 100644
--- a/openssl/src/sha.rs
+++ b/openssl/src/sha.rs
@@ -1,4 +1,5 @@
//! The SHA family of hashes.
+use libc::c_void;
use ffi;
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)]
mod test {
use hex::ToHex;
@@ -88,6 +124,16 @@ mod test {
}
#[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]
fn standalone_384() {
let data = b"abc";
let expected = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e\