Skip to content

Commit 01bdd66

Browse files
authored
Auto merge of #2945 - QuiltOS:openssl, r=alexcrichton
Use openssl on Unix rather than C functions directly for SHA256 Takes on an extra dependency, but removes unsafe code in the process. @alexcrichton I was a little confused by what you were saying on IRC, so I went ahead and just speculatively made this. No worries if it is no good.
2 parents 9ef9bd7 + 2a66dfb commit 01bdd66

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

Cargo.lock

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ toml = "0.1.29"
4545
url = "1.1"
4646
winapi = "0.2"
4747

48+
[target.'cfg(unix)'.dependencies]
49+
openssl = "0.7"
50+
4851
[dev-dependencies]
4952
hamcrest = "0.1"
5053
bufstream = "0.1"

src/cargo/util/sha256.rs

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,26 @@ pub use self::imp::Sha256;
66
// allow improper ctypes because size_t falls under that in old compilers
77
#[allow(bad_style, improper_ctypes)]
88
mod imp {
9-
use libc;
9+
extern crate openssl;
1010

11-
enum EVP_MD_CTX {}
12-
enum EVP_MD {}
13-
enum ENGINE {}
11+
use std::io::Write;
12+
use self::openssl::crypto::hash::{Hasher, Type};
1413

15-
extern {
16-
fn EVP_DigestInit_ex(ctx: *mut EVP_MD_CTX,
17-
kind: *const EVP_MD,
18-
imp: *mut ENGINE) -> libc::c_int;
19-
fn EVP_DigestUpdate(ctx: *mut EVP_MD_CTX,
20-
d: *const libc::c_void,
21-
cnt: libc::size_t) -> libc::c_int;
22-
fn EVP_DigestFinal_ex(ctx: *mut EVP_MD_CTX, md: *mut libc::c_uchar,
23-
s: *mut libc::c_uint) -> libc::c_int;
24-
fn EVP_MD_CTX_create() -> *mut EVP_MD_CTX;
25-
fn EVP_MD_CTX_destroy(ctx: *mut EVP_MD_CTX);
26-
fn EVP_sha256() -> *const EVP_MD;
27-
}
28-
29-
pub struct Sha256 { ctx: *mut EVP_MD_CTX }
14+
pub struct Sha256(Hasher);
3015

3116
impl Sha256 {
3217
pub fn new() -> Sha256 {
33-
unsafe {
34-
let ctx = EVP_MD_CTX_create();
35-
assert!(!ctx.is_null());
36-
let ret = Sha256 { ctx: ctx };
37-
let n = EVP_DigestInit_ex(ret.ctx, EVP_sha256(), 0 as *mut _);
38-
assert_eq!(n, 1);
39-
ret
40-
}
18+
Sha256(Hasher::new(Type::SHA256))
4119
}
4220

4321
pub fn update(&mut self, bytes: &[u8]) {
44-
unsafe {
45-
let n = EVP_DigestUpdate(self.ctx, bytes.as_ptr() as *const _,
46-
bytes.len() as libc::size_t);
47-
assert_eq!(n, 1);
48-
}
22+
let _ = self.0.write_all(bytes);
4923
}
5024

5125
pub fn finish(&mut self) -> [u8; 32] {
52-
unsafe {
53-
let mut ret = [0u8; 32];
54-
let mut out = 0;
55-
let n = EVP_DigestFinal_ex(self.ctx, ret.as_mut_ptr(), &mut out);
56-
assert_eq!(n, 1);
57-
assert_eq!(out, 32);
58-
ret
59-
}
60-
}
61-
}
62-
63-
impl Drop for Sha256 {
64-
fn drop(&mut self) {
65-
unsafe { EVP_MD_CTX_destroy(self.ctx) }
26+
let mut ret = [0u8; 32];
27+
ret.copy_from_slice(&self.0.finish()[..]);
28+
ret
6629
}
6730
}
6831
}

0 commit comments

Comments
 (0)