From 673bcfaf5a79d5a1612b2e326c22a174e9edae95 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 16 Aug 2017 20:25:35 -0700 Subject: [PATCH] Add SHA1 an SHA224 hashers --- openssl-sys/src/lib.rs | 19 +++++++++ openssl/src/sha.rs | 95 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index 6f33678f..9103715a 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -128,6 +128,19 @@ pub struct X509V3_CTX { // Maybe more here } +#[repr(C)] +pub struct SHA_CTX { + pub h0: SHA_LONG, + pub h1: SHA_LONG, + pub h2: SHA_LONG, + pub h3: SHA_LONG, + pub h4: SHA_LONG, + pub Nl: SHA_LONG, + pub Nh: SHA_LONG, + pub data: [SHA_LONG; SHA_LBLOCK as usize], + pub num: c_uint, +} + #[repr(C)] pub struct SHA256_CTX { pub h: [SHA_LONG; 8], @@ -2235,9 +2248,15 @@ 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 SHA1_Init(c: *mut SHA_CTX) -> c_int; + pub fn SHA1_Update(c: *mut SHA_CTX, data: *const c_void, len: size_t) -> c_int; + pub fn SHA1_Final(md: *mut c_uchar, c: *mut SHA_CTX) -> c_int; 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 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 SSL_new(ctx: *mut SSL_CTX) -> *mut SSL; pub fn SSL_pending(ssl: *const SSL) -> c_int; diff --git a/openssl/src/sha.rs b/openssl/src/sha.rs index c4141094..fe08a2d3 100644 --- a/openssl/src/sha.rs +++ b/openssl/src/sha.rs @@ -58,6 +58,81 @@ pub fn sha512(data: &[u8]) -> [u8; 64] { } } +/// An object which calculates a SHA1 hash of some data. +/// +/// # Warning +/// +/// SHA1 is known to be insecure - it should not be used unless required for +/// compatibility with existing systems. +pub struct Sha1(ffi::SHA_CTX); + +impl Sha1 { + /// Creates a new hasher. + #[inline] + pub fn new() -> Sha1 { + unsafe { + let mut ctx = mem::uninitialized(); + ffi::SHA1_Init(&mut ctx); + Sha1(ctx) + } + } + + /// Feeds some data into the hasher. + /// + /// This can be called multiple times. + #[inline] + pub fn update(&mut self, buf: &[u8]) { + unsafe { + ffi::SHA1_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; 20] { + unsafe { + let mut hash: [u8; 20] = mem::uninitialized(); + ffi::SHA1_Final(hash.as_mut_ptr(), &mut self.0); + hash + } + } +} + +/// An object which calculates a SHA224 hash of some data. +pub struct Sha224(ffi::SHA256_CTX); + +impl Sha224 { + /// Creates a new hasher. + #[inline] + pub fn new() -> Sha224 { + unsafe { + let mut ctx = mem::uninitialized(); + ffi::SHA224_Init(&mut ctx); + Sha224(ctx) + } + } + + /// Feeds some data into the hasher. + /// + /// This can be called multiple times. + #[inline] + pub fn update(&mut self, buf: &[u8]) { + unsafe { + ffi::SHA224_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; 28] { + unsafe { + let mut hash: [u8; 28] = mem::uninitialized(); + ffi::SHA224_Final(hash.as_mut_ptr(), &mut self.0); + hash + } + } +} + /// An object which calculates a SHA256 hash of some data. pub struct Sha256(ffi::SHA256_CTX); @@ -107,6 +182,16 @@ mod test { assert_eq!(sha1(data).to_hex(), expected); } + #[test] + fn struct_1() { + let expected = "a9993e364706816aba3e25717850c26c9cd0d89d"; + + let mut hasher = Sha1::new(); + hasher.update(b"a"); + hasher.update(b"bc"); + assert_eq!(hasher.finish().to_hex(), expected); + } + #[test] fn standalone_224() { let data = b"abc"; @@ -115,6 +200,16 @@ mod test { assert_eq!(sha224(data).to_hex(), expected); } + #[test] + fn struct_224() { + let expected = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"; + + let mut hasher = Sha224::new(); + hasher.update(b"a"); + hasher.update(b"bc"); + assert_eq!(hasher.finish().to_hex(), expected); + } + #[test] fn standalone_256() { let data = b"abc";