Commit fb8f201b authored by Erick Tryzelaar's avatar Erick Tryzelaar
Browse files

Update to rust HEAD

parent f9e4b7ab
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -15,11 +15,11 @@
 */

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

use std; // FIXME https://github.com/mozilla/rust/issues/1127
extern mod std; // FIXME https://github.com/mozilla/rust/issues/1127

mod hash;
mod pkey;
+63 −74
Original line number Diff line number Diff line
import libc::c_uint;

export hasher;
export hashtype;
export hash;
export libcrypto;

export md5, sha1, sha224, sha256, sha384, sha512;

iface hasher {
    #[doc = "Initializes this hasher"]
    fn init();

    #[doc = "Update this hasher with more input bytes"]
    fn update(~[u8]);

    #[doc = "
    Return the digest of all bytes added to this hasher since its last
    initialization
    "]
    fn final() -> ~[u8];
}

enum hashtype {
    md5,
    sha1,
    sha224,
    sha256,
    sha384,
    sha512
use libc::c_uint;

pub enum HashType {
    MD5,
    SHA1,
    SHA224,
    SHA256,
    SHA384,
    SHA512
}

#[allow(non_camel_case_types)]
type EVP_MD_CTX = *libc::c_void;

#[allow(non_camel_case_types)]
type EVP_MD = *libc::c_void;

#[link_name = "crypto"]
@@ -47,61 +29,68 @@ extern mod libcrypto {

    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: *u8, n: *u32);
    fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32);
}

fn evpmd(t: hashtype) -> (EVP_MD, uint) {
    alt t {
        md5 { (libcrypto::EVP_md5(), 16u) }
        sha1 { (libcrypto::EVP_sha1(), 20u) }
        sha224 { (libcrypto::EVP_sha224(), 28u) }
        sha256 { (libcrypto::EVP_sha256(), 32u) }
        sha384 { (libcrypto::EVP_sha384(), 48u) }
        sha512 { (libcrypto::EVP_sha512(), 64u) }
fn evpmd(t: HashType) -> (EVP_MD, uint) {
    match t {
        MD5 => (libcrypto::EVP_md5(), 16u),
        SHA1 => (libcrypto::EVP_sha1(), 20u),
        SHA224 => (libcrypto::EVP_sha224(), 28u),
        SHA256 => (libcrypto::EVP_sha256(), 32u),
        SHA384 => (libcrypto::EVP_sha384(), 48u),
        SHA512 => (libcrypto::EVP_sha512(), 64u),
    }
}

fn hasher(ht: hashtype) -> hasher {
    type hasherstate = {
        evp: EVP_MD,
        ctx: EVP_MD_CTX,
        len: uint
    };
pub struct Hasher {
    priv evp: EVP_MD,
    priv ctx: EVP_MD_CTX,
    priv len: uint,
}

pub fn Hasher(ht: HashType) -> Hasher {
    let ctx = libcrypto::EVP_MD_CTX_create();
    let (evp, mdlen) = evpmd(ht);
    let h = Hasher { evp: evp, ctx: ctx, len: mdlen };
    h.init();
    h
}

    impl of hasher for hasherstate {
pub impl Hasher {
    /// Initializes this hasher
    fn init() unsafe {
        libcrypto::EVP_DigestInit(self.ctx, self.evp);
    }

        fn update(data: ~[u8]) unsafe {
            let pdata: *u8 = vec::unsafe::to_ptr::<u8>(data);
            libcrypto::EVP_DigestUpdate(self.ctx, pdata, vec::len(data) as c_uint);
    /// Update this hasher with more input bytes
    fn update(data: &[u8]) unsafe {
        do vec::as_imm_buf(data) |pdata, len| {
            libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
        }
    }

    /**
     * Return the digest of all bytes added to this hasher since its last
     * initialization
     */
    fn final() -> ~[u8] unsafe {
            let res = vec::to_mut(vec::from_elem::<u8>(self.len, 0u8));
            let pres = vec::unsafe::to_ptr::<u8>(res);
            libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null::<u32>());
            vec::from_mut::<u8>(res)
        let mut res = vec::from_elem(self.len, 0u8);
        do vec::as_mut_buf(res) |pres, _len| {
            libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null());
        }
        res
    }

    let ctx = libcrypto::EVP_MD_CTX_create();
    let (evp, mdlen) = evpmd(ht);
    let st = { evp: evp, ctx: ctx, len: mdlen };
    let h = st as hasher;
    h.init();
    ret h;
}

#[doc = "
Hashes the supplied input data using hash t, returning the resulting hash value
"]
fn hash(t: hashtype, data: ~[u8]) -> ~[u8] unsafe {
    let h = hasher(t);
    h.init();
/**
 * Hashes the supplied input data using hash t, returning the resulting hash
 * value
 */
pub fn hash(t: HashType, data: &[u8]) -> ~[u8] unsafe {
    let h = Hasher(t);
    h.update(data);
    ret h.final();
    h.final()
}

#[cfg(test)]
@@ -113,7 +102,7 @@ mod tests {
        let d0 = 
           ~[0x90u8, 0x01u8, 0x50u8, 0x98u8, 0x3cu8, 0xd2u8, 0x4fu8, 0xb0u8,
             0xd6u8, 0x96u8, 0x3fu8, 0x7du8, 0x28u8, 0xe1u8, 0x7fu8, 0x72u8];
        assert(hash(md5, s0) == d0);
        assert(hash(MD5, s0) == d0);
    }

    #[test]
@@ -123,7 +112,7 @@ mod tests {
           ~[0xa9u8, 0x99u8, 0x3eu8, 0x36u8, 0x47u8, 0x06u8, 0x81u8, 0x6au8,
             0xbau8, 0x3eu8, 0x25u8, 0x71u8, 0x78u8, 0x50u8, 0xc2u8, 0x6cu8,
             0x9cu8, 0xd0u8, 0xd8u8, 0x9du8];
        assert(hash(sha1, s0) == d0);
        assert(hash(SHA1, s0) == d0);
    }

    #[test]
@@ -134,6 +123,6 @@ mod tests {
             0x41u8, 0x41u8, 0x40u8, 0xdeu8, 0x5du8, 0xaeu8, 0x22u8, 0x23u8,
             0xb0u8, 0x03u8, 0x61u8, 0xa3u8, 0x96u8, 0x17u8, 0x7au8, 0x9cu8,
             0xb4u8, 0x10u8, 0xffu8, 0x61u8, 0xf2u8, 0x00u8, 0x15u8, 0xadu8];
        assert(hash(sha256, s0) == d0);
        assert(hash(SHA256, s0) == d0);
    }
}
+48 −24
Original line number Diff line number Diff line
import libc::{c_char, c_uchar, c_int};
use libc::{c_char, c_uchar, c_int};

#[link_name = "crypto"]
#[abi = "cdecl"]
extern mod libcrypto {
    fn PKCS5_PBKDF2_HMAC_SHA1(pass: *c_char, passlen: c_int,
                              salt: *c_uchar, saltlen: c_int,
    fn PKCS5_PBKDF2_HMAC_SHA1(pass: *u8, passlen: c_int,
                              salt: *u8, saltlen: c_int,
                              iter: c_int, keylen: c_int,
                              out: *c_uchar) -> c_int;
                              out: *mut u8) -> c_int;
}

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

    do str::as_c_str(pass) |pass_buf| {
        do vec::as_buf(salt) |salt_buf| {
            let mut out = ~[];
            vec::reserve(out, keylen);
    do str::as_buf(pass) |pass_buf, pass_len| {
        do vec::as_imm_buf(salt) |salt_buf, salt_len| {
            let mut out = vec::with_capacity(keylen);

            do vec::as_buf(out) |out_buf| {
            do vec::as_mut_buf(out) |out_buf, _out_len| {
                let r = libcrypto::PKCS5_PBKDF2_HMAC_SHA1(
                    pass_buf, str::len(pass) as c_int,
                    salt_buf, vec::len(salt) as c_int,
                    pass_buf, pass_len as c_int,
                    salt_buf, salt_len as c_int,
                    iter as c_int, keylen as c_int,
                    out_buf);

                if r != 1 as c_int { fail; }

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

            out
@@ -44,27 +44,45 @@ mod tests {
    // http://tools.ietf.org/html/draft-josefsson-pbkdf2-test-vectors-06
    #[test]
    fn test_pbkdf2_hmac_sha1() {
        assert pbkdf2_hmac_sha1("password", str::bytes("salt"), 1u, 20u) == ~[
        assert pbkdf2_hmac_sha1(
            "password",
            str::to_bytes("salt"),
            1u,
            20u
        ) == ~[
            0x0c_u8, 0x60_u8, 0xc8_u8, 0x0f_u8, 0x96_u8, 0x1f_u8, 0x0e_u8,
            0x71_u8, 0xf3_u8, 0xa9_u8, 0xb5_u8, 0x24_u8, 0xaf_u8, 0x60_u8,
            0x12_u8, 0x06_u8, 0x2f_u8, 0xe0_u8, 0x37_u8, 0xa6_u8
        ];

        assert pbkdf2_hmac_sha1("password", str::bytes("salt"), 2u, 20u) == ~[
        assert pbkdf2_hmac_sha1(
            "password",
            str::to_bytes("salt"),
            2u,
            20u
        ) == ~[
            0xea_u8, 0x6c_u8, 0x01_u8, 0x4d_u8, 0xc7_u8, 0x2d_u8, 0x6f_u8,
            0x8c_u8, 0xcd_u8, 0x1e_u8, 0xd9_u8, 0x2a_u8, 0xce_u8, 0x1d_u8,
            0x41_u8, 0xf0_u8, 0xd8_u8, 0xde_u8, 0x89_u8, 0x57_u8
        ];

        assert pbkdf2_hmac_sha1("password", str::bytes("salt"), 4096u,
                                20u) == ~[
        assert pbkdf2_hmac_sha1(
            "password",
            str::to_bytes("salt"),
            4096u,
            20u
        ) == ~[
            0x4b_u8, 0x00_u8, 0x79_u8, 0x01_u8, 0xb7_u8, 0x65_u8, 0x48_u8,
            0x9a_u8, 0xbe_u8, 0xad_u8, 0x49_u8, 0xd9_u8, 0x26_u8, 0xf7_u8,
            0x21_u8, 0xd0_u8, 0x65_u8, 0xa4_u8, 0x29_u8, 0xc1_u8
        ];

        assert pbkdf2_hmac_sha1("password", str::bytes("salt"), 16777216u,
                                20u) == ~[
        assert pbkdf2_hmac_sha1(
            "password",
            str::to_bytes("salt"),
            16777216u,
            20u
        ) == ~[
            0xee_u8, 0xfe_u8, 0x3d_u8, 0x61_u8, 0xcd_u8, 0x4d_u8, 0xa4_u8,
            0xe4_u8, 0xe9_u8, 0x94_u8, 0x5b_u8, 0x3d_u8, 0x6b_u8, 0xa2_u8,
            0x15_u8, 0x8c_u8, 0x26_u8, 0x34_u8, 0xe9_u8, 0x84_u8
@@ -72,16 +90,22 @@ mod tests {

        assert pbkdf2_hmac_sha1(
            "passwordPASSWORDpassword",
                str::bytes("saltSALTsaltSALTsaltSALTsaltSALTsalt"),
                4096u, 25u) == ~[
            str::to_bytes("saltSALTsaltSALTsaltSALTsaltSALTsalt"),
            4096u,
            25u
        ) == ~[
            0x3d_u8, 0x2e_u8, 0xec_u8, 0x4f_u8, 0xe4_u8, 0x1c_u8, 0x84_u8,
            0x9b_u8, 0x80_u8, 0xc8_u8, 0xd8_u8, 0x36_u8, 0x62_u8, 0xc0_u8,
            0xe4_u8, 0x4a_u8, 0x8b_u8, 0x29_u8, 0x1a_u8, 0x96_u8, 0x4c_u8,
            0xf2_u8, 0xf0_u8, 0x70_u8, 0x38_u8
        ];

       assert pbkdf2_hmac_sha1("pass\x00word", str::bytes("sa\x00lt"), 4096u,
                               16u) == ~[
        assert pbkdf2_hmac_sha1(
            "pass\x00word",
            str::to_bytes("sa\x00lt"),
            4096u,
            16u
        ) == ~[
            0x56_u8, 0xfa_u8, 0x6a_u8, 0xa7_u8, 0x55_u8, 0x48_u8, 0x09_u8,
            0x9d_u8, 0xcc_u8, 0x37_u8, 0xd7_u8, 0xf0_u8, 0x34_u8, 0x25_u8,
            0xe0_u8, 0xc3_u8
+282 −226

File changed.

Preview size limit exceeded, changes collapsed.

+7 −8
Original line number Diff line number Diff line
import libc::{c_uchar, c_int};
use libc::{c_uchar, c_int};

#[link_name = "crypto"]
#[abi = "cdecl"]
extern mod libcrypto {
    fn RAND_bytes(buf: *c_uchar, num: c_int) -> c_int;
    fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int;
}

fn rand_bytes(len: uint) -> ~[u8] {
    let mut out = ~[];
    vec::reserve(out, len);
pub fn rand_bytes(len: uint) -> ~[u8] {
    let mut out = vec::with_capacity(len);

    do vec::as_buf(out) |out_buf| {
    do vec::as_mut_buf(out) |out_buf, len| {
        let r = libcrypto::RAND_bytes(out_buf, len as c_int);
        if r != 1 as c_int { fail }

        unsafe { vec::unsafe::set_len(out, len); }
    }

    unsafe { vec::raw::set_len(out, len); }

    out
}

Loading