Commit c978e357 authored by Brian Anderson's avatar Brian Anderson
Browse files

Update for 0.2

parent ee4c285a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@
       uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")];
#[crate_type = "lib"];

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

mod hash;
mod pkey;
mod symm;
+21 −21
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import core::ptr;
import core::str;
import core::vec;

import ctypes::c_uint;
import libc::c_uint;

export hasher;
export hashtype;
@@ -38,21 +38,21 @@ iface hasher {
    fn final() -> [u8];
}

tag hashtype {
    md5;
    sha1;
    sha224;
    sha256;
    sha384;
    sha512;
enum hashtype {
    md5,
    sha1,
    sha224,
    sha256,
    sha384,
    sha512
}

type EVP_MD_CTX = *libc::c_void;
type EVP_MD = *libc::c_void;

#[link_name = "crypto"]
#[abi = "cdecl"]
native mod _native {
    type EVP_MD_CTX;
    type EVP_MD;

    fn EVP_MD_CTX_create() -> EVP_MD_CTX;

    fn EVP_md5() -> EVP_MD;
@@ -67,21 +67,21 @@ native mod _native {
    fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *u8, n: *u32);
}

fn evpmd(t: hashtype) -> (_native::EVP_MD, uint) {
fn evpmd(t: hashtype) -> (EVP_MD, uint) {
    alt t {
        md5. { (_native::EVP_md5(), 16u) }
        sha1. { (_native::EVP_sha1(), 20u) }
        sha224. { (_native::EVP_sha224(), 28u) }
        sha256. { (_native::EVP_sha256(), 32u) }
        sha384. { (_native::EVP_sha384(), 48u) }
        sha512. { (_native::EVP_sha512(), 64u) }
        md5 { (_native::EVP_md5(), 16u) }
        sha1 { (_native::EVP_sha1(), 20u) }
        sha224 { (_native::EVP_sha224(), 28u) }
        sha256 { (_native::EVP_sha256(), 32u) }
        sha384 { (_native::EVP_sha384(), 48u) }
        sha512 { (_native::EVP_sha512(), 64u) }
    }
}

fn mk_hasher(ht: hashtype) -> hasher {
    type hasherstate = {
        evp: _native::EVP_MD,
        ctx: _native::EVP_MD_CTX,
        evp: EVP_MD,
        ctx: EVP_MD_CTX,
        len: uint
    };

@@ -96,7 +96,7 @@ fn mk_hasher(ht: hashtype) -> hasher {
        }

        fn final() -> [u8] unsafe {
            let res: [mutable u8] = vec::init_elt_mut::<u8>(0u8, self.len);
            let res: [mut u8] = vec::to_mut(vec::from_elem::<u8>(self.len, 0u8));
            let pres: *u8 = vec::unsafe::to_ptr::<u8>(res);
            _native::EVP_DigestFinal(self.ctx, pres, ptr::null::<u32>());
            vec::from_mut::<u8>(res)
+33 −35
Original line number Diff line number Diff line
use std; // FIXME https://github.com/graydon/rust/issues/1127

import core::ptr;
import core::str;
import core::unsafe;
import core::vec;

import ctypes::{c_int, c_uint};
import libc::{c_int, c_uint};

export pkeyrole, encrypt, decrypt, sign, verify;
export pkey, mk_pkey;
export _native;

type EVP_PKEY = *libc::c_void;
type ANYKEY = *libc::c_void;
type RSA = *libc::c_void;

#[link_name = "crypto"]
#[abi = "cdecl"]
native mod _native {
    type EVP_PKEY;
    type ANYKEY;
    type RSA;

    fn EVP_PKEY_new() -> *EVP_PKEY;
    fn EVP_PKEY_free(k: *EVP_PKEY);
    fn EVP_PKEY_assign(k: *EVP_PKEY, t: c_int, inner: *ANYKEY);
@@ -41,10 +39,10 @@ native mod _native {
                  k: *RSA) -> c_int;
}

tag pkeyparts {
    neither;
    public;
    both;
enum pkeyparts {
    neither,
    public,
    both
}

/*
@@ -52,11 +50,11 @@ Tag: pkeyrole

Represents a role an asymmetric key might be appropriate for.
*/
tag pkeyrole {
    encrypt;
    decrypt;
    sign;
    verify;
enum pkeyrole {
    encrypt,
    decrypt,
    sign,
    verify
}

/*
@@ -156,25 +154,25 @@ iface pkey {
    fn verify(m: [u8], s: [u8]) -> bool;
}

fn rsa_to_any(rsa: *_native::RSA) -> *_native::ANYKEY unsafe {
    unsafe::reinterpret_cast::<*_native::RSA, *_native::ANYKEY>(rsa)
fn rsa_to_any(rsa: *RSA) -> *ANYKEY unsafe {
    unsafe::reinterpret_cast::<*RSA, *ANYKEY>(rsa)
}

fn any_to_rsa(anykey: *_native::ANYKEY) -> *_native::RSA unsafe {
    unsafe::reinterpret_cast::<*_native::ANYKEY, *_native::RSA>(anykey)
fn any_to_rsa(anykey: *ANYKEY) -> *RSA unsafe {
    unsafe::reinterpret_cast::<*ANYKEY, *RSA>(anykey)
}

fn mk_pkey() -> pkey {
    type pkeystate = {
        mutable evp: *_native::EVP_PKEY,
        mutable parts: pkeyparts
        mut evp: *EVP_PKEY,
        mut parts: pkeyparts
    };

    fn _tostr(st: pkeystate,
              f: fn@(*_native::EVP_PKEY, **u8) -> c_int) -> [u8] unsafe {
              f: fn@(*EVP_PKEY, **u8) -> c_int) -> [u8] unsafe {
        let len = f(st.evp, ptr::null());
        if len < 0 as c_int { ret []; }
        let s: [mutable u8] = vec::init_elt_mut::<u8>(0u8, len as uint);
        let s: [mut u8] = vec::to_mut(vec::from_elem::<u8>(len as uint, 0u8));
        let ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
        let pps: **u8 = ptr::addr_of(ps);
        let r = f(st.evp, pps);
@@ -183,12 +181,12 @@ fn mk_pkey() -> pkey {
    }

    fn _fromstr(st: pkeystate,
                f: fn@(c_int, **_native::EVP_PKEY, **u8, c_uint) -> *_native::EVP_PKEY,
                f: fn@(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY,
                s: [u8]) unsafe {
            let ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
            let pps: **u8 = ptr::addr_of(ps);
            let evp: *_native::EVP_PKEY = ptr::null();
            let pevp: **_native::EVP_PKEY = ptr::addr_of(evp);
            let evp: *EVP_PKEY = ptr::null();
            let pevp: **EVP_PKEY = ptr::addr_of(evp);
            f(6 as c_int, pevp, pps, vec::len(s) as c_uint);
            st.evp = *pevp;
    }
@@ -230,10 +228,10 @@ fn mk_pkey() -> pkey {
        }
        fn can(r: pkeyrole) -> bool {
            alt r {
                encrypt. { self.parts != neither }
                verify. { self.parts != neither }
                decrypt. { self.parts == both }
                sign. { self.parts == both }
                encrypt { self.parts != neither }
                verify { self.parts != neither }
                decrypt { self.parts == both }
                sign { self.parts == both }
            }
        }
        fn max_data() -> uint unsafe {
@@ -247,7 +245,7 @@ fn mk_pkey() -> pkey {
            let len = _native::RSA_size(rsa);
            // 41 comes from RSA_public_encrypt(3) for OAEP
            assert(vec::len(s) < _native::RSA_size(rsa) as uint - 41u);
            let r: [mutable u8] = vec::init_elt_mut::<u8>(0u8, len as uint + 1u);
            let r: [mut u8] = vec::to_mut(vec::from_elem::<u8>(len as uint + 1u, 0u8));
            let pr: *u8 = vec::unsafe::to_ptr::<u8>(r);
            let ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
            // XXX: 4 == RSA_PKCS1_OAEP_PADDING
@@ -260,7 +258,7 @@ fn mk_pkey() -> pkey {
            let rsa = _native::EVP_PKEY_get1_RSA(self.evp);
            let len = _native::RSA_size(rsa);
            assert(vec::len(s) as c_uint == _native::RSA_size(rsa));
            let r: [mutable u8] = vec::init_elt_mut::<u8>(0u8, len as uint + 1u);
            let r: [mut u8] = vec::to_mut(vec::from_elem::<u8>(len as uint + 1u, 0u8));
            let pr: *u8 = vec::unsafe::to_ptr::<u8>(r);
            let ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
            // XXX: 4 == RSA_PKCS1_OAEP_PADDING
@@ -272,7 +270,7 @@ fn mk_pkey() -> pkey {
        fn sign(s: [u8]) -> [u8] unsafe {
            let rsa = _native::EVP_PKEY_get1_RSA(self.evp);
            let len = _native::RSA_size(rsa);
            let r: [mutable u8] = vec::init_elt_mut::<u8>(0u8, len as uint + 1u);
            let r: [mut u8] = vec::to_mut(vec::from_elem::<u8>(len as uint + 1u, 0u8));
            let pr: *u8 = vec::unsafe::to_ptr::<u8>(r);
            let ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
            let plen: *c_uint = ptr::addr_of(len);
@@ -295,7 +293,7 @@ fn mk_pkey() -> pkey {
        }
    }

    let st = { mutable evp: _native::EVP_PKEY_new(), mutable parts: neither };
    let st = { mut evp: _native::EVP_PKEY_new(), mut parts: neither };
    let p = st as pkey;
    ret p;
}
+19 −19
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import core::ptr;
import core::str;
import core::vec;

import ctypes::c_int;
import libc::c_int;

export crypter;
export cryptermode;
@@ -15,12 +15,12 @@ export mk_crypter;
export encrypt, decrypt;
export _native;

type EVP_CIPHER_CTX = *libc::c_void;
type EVP_CIPHER = *libc::c_void;

#[link_name = "crypto"]
#[abi = "cdecl"]
native mod _native {
    type EVP_CIPHER_CTX;
    type EVP_CIPHER;

    fn EVP_CIPHER_CTX_new() -> EVP_CIPHER_CTX;
    fn EVP_CIPHER_CTX_set_padding(ctx: EVP_CIPHER_CTX, padding: c_int);

@@ -75,39 +75,39 @@ iface crypter {
    fn final() -> [u8];
}

tag cryptermode {
    encryptmode;
    decryptmode;
enum cryptermode {
    encryptmode,
    decryptmode
}

tag cryptertype {
    aes_256_ecb;
    aes_256_cbc;
enum cryptertype {
    aes_256_ecb,
    aes_256_cbc
}

fn evpc(t: cryptertype) -> (_native::EVP_CIPHER, uint, uint) {
fn evpc(t: cryptertype) -> (EVP_CIPHER, uint, uint) {
    alt t {
        aes_256_ecb. { (_native::EVP_aes_256_ecb(), 32u, 16u) }
        aes_256_cbc. { (_native::EVP_aes_256_cbc(), 32u, 16u) }
        aes_256_ecb { (_native::EVP_aes_256_ecb(), 32u, 16u) }
        aes_256_cbc { (_native::EVP_aes_256_cbc(), 32u, 16u) }
    }
}

fn mk_crypter(t: cryptertype) -> crypter {
    type crypterstate = {
        evp: _native::EVP_CIPHER,
        ctx: _native::EVP_CIPHER_CTX,
        evp: EVP_CIPHER,
        ctx: EVP_CIPHER_CTX,
        keylen: uint,
        blocksize: uint
    };

    impl of crypter for crypterstate {
        fn pad(padding: bool) {
            let v = (padding ? 1 : 0) as c_int;
            let v = if padding { 1 } else { 0} as c_int;
            _native::EVP_CIPHER_CTX_set_padding(self.ctx, v);
        }

        fn init (mode: cryptermode, key: [u8], iv: [u8]) unsafe {
            let m = alt mode { encryptmode. { 1 } decryptmode. { 0 } } as c_int;
            let m = alt mode { encryptmode { 1 } decryptmode { 0 } } as c_int;
            assert(vec::len(key) == self.keylen);
            let pkey: *u8 = vec::unsafe::to_ptr::<u8>(key);
            let piv: *u8 = vec::unsafe::to_ptr::<u8>(iv);
@@ -119,7 +119,7 @@ fn mk_crypter(t: cryptertype) -> crypter {
            let datalen: u32 = vec::len(data) as u32;
            let reslen: u32 = datalen + (self.blocksize as u32);
            let preslen: *u32 = ptr::addr_of(reslen);
            let res: [mutable u8] = vec::init_elt_mut::<u8>(0u8, reslen as uint);
            let res: [mut u8] = vec::to_mut(vec::from_elem::<u8>(reslen as uint, 0u8));
            let pres: *u8 = vec::unsafe::to_ptr::<u8>(res);
            _native::EVP_CipherUpdate(self.ctx, pres, preslen, pdata, datalen);
            ret vec::slice::<u8>(res, 0u, *preslen as uint);
@@ -128,7 +128,7 @@ fn mk_crypter(t: cryptertype) -> crypter {
        fn final() -> [u8] unsafe {
            let reslen: u32 = self.blocksize as u32;
            let preslen: *u32 = ptr::addr_of(reslen);
            let res: [mutable u8] = vec::init_elt_mut::<u8>(0u8, reslen as uint);
            let res: [mut u8] = vec::to_mut(vec::from_elem::<u8>(reslen as uint, 0u8));
            let pres: *u8 = vec::unsafe::to_ptr::<u8>(res);
            _native::EVP_CipherFinal(self.ctx, pres, preslen);
            ret vec::slice::<u8>(res, 0u, *preslen as uint);