Update for 0.2

This commit is contained in:
Brian Anderson 2012-03-28 11:43:56 -07:00
parent ee4c285a79
commit c978e357d4
4 changed files with 75 additions and 75 deletions

View File

@ -19,6 +19,8 @@
uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")]; uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")];
#[crate_type = "lib"]; #[crate_type = "lib"];
use std; // FIXME https://github.com/mozilla/rust/issues/1127
mod hash; mod hash;
mod pkey; mod pkey;
mod symm; mod symm;

42
hash.rs
View File

@ -4,7 +4,7 @@ import core::ptr;
import core::str; import core::str;
import core::vec; import core::vec;
import ctypes::c_uint; import libc::c_uint;
export hasher; export hasher;
export hashtype; export hashtype;
@ -38,21 +38,21 @@ iface hasher {
fn final() -> [u8]; fn final() -> [u8];
} }
tag hashtype { enum hashtype {
md5; md5,
sha1; sha1,
sha224; sha224,
sha256; sha256,
sha384; sha384,
sha512; sha512
} }
type EVP_MD_CTX = *libc::c_void;
type EVP_MD = *libc::c_void;
#[link_name = "crypto"] #[link_name = "crypto"]
#[abi = "cdecl"] #[abi = "cdecl"]
native mod _native { native mod _native {
type EVP_MD_CTX;
type EVP_MD;
fn EVP_MD_CTX_create() -> EVP_MD_CTX; fn EVP_MD_CTX_create() -> EVP_MD_CTX;
fn EVP_md5() -> EVP_MD; fn EVP_md5() -> EVP_MD;
@ -67,21 +67,21 @@ native mod _native {
fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *u8, n: *u32); 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 { alt t {
md5. { (_native::EVP_md5(), 16u) } md5 { (_native::EVP_md5(), 16u) }
sha1. { (_native::EVP_sha1(), 20u) } sha1 { (_native::EVP_sha1(), 20u) }
sha224. { (_native::EVP_sha224(), 28u) } sha224 { (_native::EVP_sha224(), 28u) }
sha256. { (_native::EVP_sha256(), 32u) } sha256 { (_native::EVP_sha256(), 32u) }
sha384. { (_native::EVP_sha384(), 48u) } sha384 { (_native::EVP_sha384(), 48u) }
sha512. { (_native::EVP_sha512(), 64u) } sha512 { (_native::EVP_sha512(), 64u) }
} }
} }
fn mk_hasher(ht: hashtype) -> hasher { fn mk_hasher(ht: hashtype) -> hasher {
type hasherstate = { type hasherstate = {
evp: _native::EVP_MD, evp: EVP_MD,
ctx: _native::EVP_MD_CTX, ctx: EVP_MD_CTX,
len: uint len: uint
}; };
@ -96,7 +96,7 @@ fn mk_hasher(ht: hashtype) -> hasher {
} }
fn final() -> [u8] unsafe { 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); let pres: *u8 = vec::unsafe::to_ptr::<u8>(res);
_native::EVP_DigestFinal(self.ctx, pres, ptr::null::<u32>()); _native::EVP_DigestFinal(self.ctx, pres, ptr::null::<u32>());
vec::from_mut::<u8>(res) vec::from_mut::<u8>(res)

68
pkey.rs
View File

@ -1,23 +1,21 @@
use std; // FIXME https://github.com/graydon/rust/issues/1127
import core::ptr; import core::ptr;
import core::str; import core::str;
import core::unsafe; import core::unsafe;
import core::vec; import core::vec;
import ctypes::{c_int, c_uint}; import libc::{c_int, c_uint};
export pkeyrole, encrypt, decrypt, sign, verify; export pkeyrole, encrypt, decrypt, sign, verify;
export pkey, mk_pkey; export pkey, mk_pkey;
export _native; export _native;
type EVP_PKEY = *libc::c_void;
type ANYKEY = *libc::c_void;
type RSA = *libc::c_void;
#[link_name = "crypto"] #[link_name = "crypto"]
#[abi = "cdecl"] #[abi = "cdecl"]
native mod _native { native mod _native {
type EVP_PKEY;
type ANYKEY;
type RSA;
fn EVP_PKEY_new() -> *EVP_PKEY; fn EVP_PKEY_new() -> *EVP_PKEY;
fn EVP_PKEY_free(k: *EVP_PKEY); fn EVP_PKEY_free(k: *EVP_PKEY);
fn EVP_PKEY_assign(k: *EVP_PKEY, t: c_int, inner: *ANYKEY); fn EVP_PKEY_assign(k: *EVP_PKEY, t: c_int, inner: *ANYKEY);
@ -41,10 +39,10 @@ native mod _native {
k: *RSA) -> c_int; k: *RSA) -> c_int;
} }
tag pkeyparts { enum pkeyparts {
neither; neither,
public; public,
both; both
} }
/* /*
@ -52,11 +50,11 @@ Tag: pkeyrole
Represents a role an asymmetric key might be appropriate for. Represents a role an asymmetric key might be appropriate for.
*/ */
tag pkeyrole { enum pkeyrole {
encrypt; encrypt,
decrypt; decrypt,
sign; sign,
verify; verify
} }
/* /*
@ -156,25 +154,25 @@ iface pkey {
fn verify(m: [u8], s: [u8]) -> bool; fn verify(m: [u8], s: [u8]) -> bool;
} }
fn rsa_to_any(rsa: *_native::RSA) -> *_native::ANYKEY unsafe { fn rsa_to_any(rsa: *RSA) -> *ANYKEY unsafe {
unsafe::reinterpret_cast::<*_native::RSA, *_native::ANYKEY>(rsa) unsafe::reinterpret_cast::<*RSA, *ANYKEY>(rsa)
} }
fn any_to_rsa(anykey: *_native::ANYKEY) -> *_native::RSA unsafe { fn any_to_rsa(anykey: *ANYKEY) -> *RSA unsafe {
unsafe::reinterpret_cast::<*_native::ANYKEY, *_native::RSA>(anykey) unsafe::reinterpret_cast::<*ANYKEY, *RSA>(anykey)
} }
fn mk_pkey() -> pkey { fn mk_pkey() -> pkey {
type pkeystate = { type pkeystate = {
mutable evp: *_native::EVP_PKEY, mut evp: *EVP_PKEY,
mutable parts: pkeyparts mut parts: pkeyparts
}; };
fn _tostr(st: pkeystate, 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()); let len = f(st.evp, ptr::null());
if len < 0 as c_int { ret []; } 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 ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
let pps: **u8 = ptr::addr_of(ps); let pps: **u8 = ptr::addr_of(ps);
let r = f(st.evp, pps); let r = f(st.evp, pps);
@ -183,12 +181,12 @@ fn mk_pkey() -> pkey {
} }
fn _fromstr(st: pkeystate, 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 { s: [u8]) unsafe {
let ps: *u8 = vec::unsafe::to_ptr::<u8>(s); let ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
let pps: **u8 = ptr::addr_of(ps); let pps: **u8 = ptr::addr_of(ps);
let evp: *_native::EVP_PKEY = ptr::null(); let evp: *EVP_PKEY = ptr::null();
let pevp: **_native::EVP_PKEY = ptr::addr_of(evp); let pevp: **EVP_PKEY = ptr::addr_of(evp);
f(6 as c_int, pevp, pps, vec::len(s) as c_uint); f(6 as c_int, pevp, pps, vec::len(s) as c_uint);
st.evp = *pevp; st.evp = *pevp;
} }
@ -230,10 +228,10 @@ fn mk_pkey() -> pkey {
} }
fn can(r: pkeyrole) -> bool { fn can(r: pkeyrole) -> bool {
alt r { alt r {
encrypt. { self.parts != neither } encrypt { self.parts != neither }
verify. { self.parts != neither } verify { self.parts != neither }
decrypt. { self.parts == both } decrypt { self.parts == both }
sign. { self.parts == both } sign { self.parts == both }
} }
} }
fn max_data() -> uint unsafe { fn max_data() -> uint unsafe {
@ -247,7 +245,7 @@ fn mk_pkey() -> pkey {
let len = _native::RSA_size(rsa); let len = _native::RSA_size(rsa);
// 41 comes from RSA_public_encrypt(3) for OAEP // 41 comes from RSA_public_encrypt(3) for OAEP
assert(vec::len(s) < _native::RSA_size(rsa) as uint - 41u); 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 pr: *u8 = vec::unsafe::to_ptr::<u8>(r);
let ps: *u8 = vec::unsafe::to_ptr::<u8>(s); let ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
// XXX: 4 == RSA_PKCS1_OAEP_PADDING // XXX: 4 == RSA_PKCS1_OAEP_PADDING
@ -260,7 +258,7 @@ fn mk_pkey() -> pkey {
let rsa = _native::EVP_PKEY_get1_RSA(self.evp); let rsa = _native::EVP_PKEY_get1_RSA(self.evp);
let len = _native::RSA_size(rsa); let len = _native::RSA_size(rsa);
assert(vec::len(s) as c_uint == _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 pr: *u8 = vec::unsafe::to_ptr::<u8>(r);
let ps: *u8 = vec::unsafe::to_ptr::<u8>(s); let ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
// XXX: 4 == RSA_PKCS1_OAEP_PADDING // XXX: 4 == RSA_PKCS1_OAEP_PADDING
@ -272,7 +270,7 @@ fn mk_pkey() -> pkey {
fn sign(s: [u8]) -> [u8] unsafe { fn sign(s: [u8]) -> [u8] unsafe {
let rsa = _native::EVP_PKEY_get1_RSA(self.evp); let rsa = _native::EVP_PKEY_get1_RSA(self.evp);
let len = _native::RSA_size(rsa); 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 pr: *u8 = vec::unsafe::to_ptr::<u8>(r);
let ps: *u8 = vec::unsafe::to_ptr::<u8>(s); let ps: *u8 = vec::unsafe::to_ptr::<u8>(s);
let plen: *c_uint = ptr::addr_of(len); 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; let p = st as pkey;
ret p; ret p;
} }

38
symm.rs
View File

@ -4,7 +4,7 @@ import core::ptr;
import core::str; import core::str;
import core::vec; import core::vec;
import ctypes::c_int; import libc::c_int;
export crypter; export crypter;
export cryptermode; export cryptermode;
@ -15,12 +15,12 @@ export mk_crypter;
export encrypt, decrypt; export encrypt, decrypt;
export _native; export _native;
type EVP_CIPHER_CTX = *libc::c_void;
type EVP_CIPHER = *libc::c_void;
#[link_name = "crypto"] #[link_name = "crypto"]
#[abi = "cdecl"] #[abi = "cdecl"]
native mod _native { native mod _native {
type EVP_CIPHER_CTX;
type EVP_CIPHER;
fn EVP_CIPHER_CTX_new() -> EVP_CIPHER_CTX; fn EVP_CIPHER_CTX_new() -> EVP_CIPHER_CTX;
fn EVP_CIPHER_CTX_set_padding(ctx: EVP_CIPHER_CTX, padding: c_int); fn EVP_CIPHER_CTX_set_padding(ctx: EVP_CIPHER_CTX, padding: c_int);
@ -75,39 +75,39 @@ iface crypter {
fn final() -> [u8]; fn final() -> [u8];
} }
tag cryptermode { enum cryptermode {
encryptmode; encryptmode,
decryptmode; decryptmode
} }
tag cryptertype { enum cryptertype {
aes_256_ecb; aes_256_ecb,
aes_256_cbc; aes_256_cbc
} }
fn evpc(t: cryptertype) -> (_native::EVP_CIPHER, uint, uint) { fn evpc(t: cryptertype) -> (EVP_CIPHER, uint, uint) {
alt t { alt t {
aes_256_ecb. { (_native::EVP_aes_256_ecb(), 32u, 16u) } aes_256_ecb { (_native::EVP_aes_256_ecb(), 32u, 16u) }
aes_256_cbc. { (_native::EVP_aes_256_cbc(), 32u, 16u) } aes_256_cbc { (_native::EVP_aes_256_cbc(), 32u, 16u) }
} }
} }
fn mk_crypter(t: cryptertype) -> crypter { fn mk_crypter(t: cryptertype) -> crypter {
type crypterstate = { type crypterstate = {
evp: _native::EVP_CIPHER, evp: EVP_CIPHER,
ctx: _native::EVP_CIPHER_CTX, ctx: EVP_CIPHER_CTX,
keylen: uint, keylen: uint,
blocksize: uint blocksize: uint
}; };
impl of crypter for crypterstate { impl of crypter for crypterstate {
fn pad(padding: bool) { 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); _native::EVP_CIPHER_CTX_set_padding(self.ctx, v);
} }
fn init (mode: cryptermode, key: [u8], iv: [u8]) unsafe { 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); assert(vec::len(key) == self.keylen);
let pkey: *u8 = vec::unsafe::to_ptr::<u8>(key); let pkey: *u8 = vec::unsafe::to_ptr::<u8>(key);
let piv: *u8 = vec::unsafe::to_ptr::<u8>(iv); 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 datalen: u32 = vec::len(data) as u32;
let reslen: u32 = datalen + (self.blocksize as u32); let reslen: u32 = datalen + (self.blocksize as u32);
let preslen: *u32 = ptr::addr_of(reslen); 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); let pres: *u8 = vec::unsafe::to_ptr::<u8>(res);
_native::EVP_CipherUpdate(self.ctx, pres, preslen, pdata, datalen); _native::EVP_CipherUpdate(self.ctx, pres, preslen, pdata, datalen);
ret vec::slice::<u8>(res, 0u, *preslen as uint); ret vec::slice::<u8>(res, 0u, *preslen as uint);
@ -128,7 +128,7 @@ fn mk_crypter(t: cryptertype) -> crypter {
fn final() -> [u8] unsafe { fn final() -> [u8] unsafe {
let reslen: u32 = self.blocksize as u32; let reslen: u32 = self.blocksize as u32;
let preslen: *u32 = ptr::addr_of(reslen); 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); let pres: *u8 = vec::unsafe::to_ptr::<u8>(res);
_native::EVP_CipherFinal(self.ctx, pres, preslen); _native::EVP_CipherFinal(self.ctx, pres, preslen);
ret vec::slice::<u8>(res, 0u, *preslen as uint); ret vec::slice::<u8>(res, 0u, *preslen as uint);