Commit 4e0257ab authored by Erick Tryzelaar's avatar Erick Tryzelaar
Browse files

Merge branch 'master' of https://github.com/kballard/rustcrypto

parents 5bd84e63 bcdc23c3
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -16,10 +16,13 @@
 */

#[link(name = "crypto",
       package_id = "crypto",
       vers = "0.3",
       uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")];
#[crate_type = "lib"];

#[feature(globs)];

pub mod hash;
pub mod hex;
pub mod hmac;
+20 −20
Original line number Diff line number Diff line
@@ -22,21 +22,21 @@ mod libcrypto {
    use super::*;
    use std::libc::c_uint;

    #[link_args = "-lcrypto"]
    #[link(name = "crypto")]
    extern {
        fn EVP_MD_CTX_create() -> EVP_MD_CTX;
        fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX);
        pub fn EVP_MD_CTX_create() -> EVP_MD_CTX;
        pub fn EVP_MD_CTX_destroy(ctx: EVP_MD_CTX);

        fn EVP_md5() -> EVP_MD;
        fn EVP_sha1() -> EVP_MD;
        fn EVP_sha224() -> EVP_MD;
        fn EVP_sha256() -> EVP_MD;
        fn EVP_sha384() -> EVP_MD;
        fn EVP_sha512() -> EVP_MD;
        pub fn EVP_md5() -> EVP_MD;
        pub fn EVP_sha1() -> EVP_MD;
        pub fn EVP_sha224() -> EVP_MD;
        pub fn EVP_sha256() -> EVP_MD;
        pub fn EVP_sha384() -> EVP_MD;
        pub fn EVP_sha512() -> EVP_MD;

        fn EVP_DigestInit(ctx: EVP_MD_CTX, typ: EVP_MD);
        fn EVP_DigestUpdate(ctx: EVP_MD_CTX, data: *u8, n: c_uint);
        fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32);
        pub fn EVP_DigestInit(ctx: EVP_MD_CTX, typ: EVP_MD);
        pub fn EVP_DigestUpdate(ctx: EVP_MD_CTX, data: *u8, n: c_uint);
        pub fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32);
    }
}

@@ -72,11 +72,11 @@ impl Hasher {

    /// Update this hasher with more input bytes
    pub fn update(&self, data: &[u8]) {
        do data.as_imm_buf |pdata, len| {
        data.as_imm_buf(|pdata, len| {
            unsafe {
                libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
            }
        }
        });
    }

    /**
@@ -85,17 +85,17 @@ impl Hasher {
     */
    pub fn final(&self) -> ~[u8] {
        let mut res = vec::from_elem(self.len, 0u8);
        do res.as_mut_buf |pres, _len| {
        res.as_mut_buf(|pres, _len| {
            unsafe {
                libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null());
            }
        }
        });
        res
    }
}

impl Drop for Hasher {
    fn drop(&self) {
    fn drop(&mut self) {
        unsafe {
            libcrypto::EVP_MD_CTX_destroy(self.ctx);
        }
@@ -134,7 +134,7 @@ mod tests {
        let calced = calced_raw.to_hex();

        if calced != hashtest.expected_output {
            println(fmt!("Test failed - %s != %s", calced, hashtest.expected_output));
            println!("Test failed - {} != {}", calced, hashtest.expected_output);
        }

        assert!(calced == hashtest.expected_output);
+4 −4
Original line number Diff line number Diff line
@@ -20,10 +20,10 @@ pub trait ToHex {
    fn to_hex(&self) -> ~str;
}

impl<'self> ToHex for &'self [u8] {
impl<'a> ToHex for &'a [u8] {
    fn to_hex(&self) -> ~str {

        let chars = "0123456789ABCDEF".iter().collect::<~[char]>();
        let chars = "0123456789ABCDEF".chars().collect::<~[char]>();

        let mut s = ~"";

@@ -46,11 +46,11 @@ pub trait FromHex {
    fn from_hex(&self) -> ~[u8];
}

impl<'self> FromHex for &'self str {
impl<'a> FromHex for &'a str {
    fn from_hex(&self) -> ~[u8] {
        let mut vec = vec::with_capacity(self.len() / 2);

        for (i,c) in self.iter().enumerate() {
        for (i,c) in self.chars().enumerate() {
            let nibble =
                if c >= '0' && c <= '9' { (c as u8) - 0x30 }
                else if c >= 'a' && c <= 'f' { (c as u8) - (0x61 - 10) }
+5 −14
Original line number Diff line number Diff line
@@ -27,8 +27,7 @@ pub struct HMAC_CTX {
    key: [libc::c_uchar, ..128]
}

#[link_args = "-lcrypto"]
#[abi = "cdecl"]
#[link(name = "crypto")]
extern {
    fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: libc::c_int, md: EVP_MD);

@@ -68,9 +67,9 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC {
impl HMAC {
    pub fn update(&mut self, data: &[u8]) {
        unsafe {
            do data.as_imm_buf |pdata, len| {
            data.as_imm_buf(|pdata, len| {
                HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
            }
            });
        }
    }

@@ -78,19 +77,11 @@ impl HMAC {
        unsafe {
            let mut res = vec::from_elem(self.len, 0u8);
            let mut outlen: libc::c_uint = 0;
            do res.as_mut_buf |pres, _len| {
            res.as_mut_buf(|pres, _len| {
                HMAC_Final(&mut self.ctx, pres, &mut outlen);
                assert!(self.len == outlen as uint)
            }
            });
            res
        }
    }
}

fn main() {
    let mut h = HMAC(SHA512, ~[00u8]);

    h.update([00u8]);

    println(fmt!("%?", h.final()))
}
+12 −14
Original line number Diff line number Diff line
@@ -4,28 +4,26 @@ use std::vec;
mod libcrypto {
    use std::libc::c_int;

    #[link_args = "-lcrypto"]
    #[link(name = "crypto")]
    extern {
        fn PKCS5_PBKDF2_HMAC_SHA1(pass: *u8, passlen: c_int,
        pub fn PKCS5_PBKDF2_HMAC_SHA1(pass: *u8, passlen: c_int,
                                      salt: *u8, saltlen: c_int,
                                      iter: c_int, keylen: c_int,
                                      out: *mut u8) -> c_int;
    }
}

#[doc = "
Derives a key from a password and salt using the PBKDF2-HMAC-SHA1 algorithm.
"]
/// Derives a key from a password and salt using the PBKDF2-HMAC-SHA1 algorithm.
pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint,
                        keylen: uint) -> ~[u8] {
    assert!(iter >= 1u);
    assert!(keylen >= 1u);

    do pass.as_imm_buf |pass_buf, pass_len| {
        do salt.as_imm_buf |salt_buf, salt_len| {
    pass.as_imm_buf(|pass_buf, pass_len| {
        salt.as_imm_buf(|salt_buf, salt_len| {
            let mut out = vec::with_capacity(keylen);

            do out.as_mut_buf |out_buf, _out_len| {
            out.as_mut_buf(|out_buf, _out_len| {
                let r = unsafe {
                    libcrypto::PKCS5_PBKDF2_HMAC_SHA1(
                        pass_buf, pass_len as c_int,
@@ -35,13 +33,13 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint,
                };

                if r != 1 as c_int { fail!(); }
            }
            });

            unsafe { vec::raw::set_len(&mut out, keylen); }

            out
        }
    }
        })
    })
}

#[cfg(test)]
Loading