Update for Rust 0.6
Also disable AES_128_CTR and AES_256_CTR because they cause link failures on OS X.
This commit is contained in:
parent
1cb2f63ad4
commit
9d09a98664
31
hash.rs
31
hash.rs
|
|
@ -1,4 +1,4 @@
|
||||||
use libc::c_uint;
|
use core::libc::c_uint;
|
||||||
|
|
||||||
pub enum HashType {
|
pub enum HashType {
|
||||||
MD5,
|
MD5,
|
||||||
|
|
@ -10,10 +10,10 @@ pub enum HashType {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
type EVP_MD_CTX = *libc::c_void;
|
pub type EVP_MD_CTX = *libc::c_void;
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
type EVP_MD = *libc::c_void;
|
pub type EVP_MD = *libc::c_void;
|
||||||
|
|
||||||
#[link_name = "crypto"]
|
#[link_name = "crypto"]
|
||||||
#[abi = "cdecl"]
|
#[abi = "cdecl"]
|
||||||
|
|
@ -32,7 +32,8 @@ extern mod libcrypto {
|
||||||
fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32);
|
fn EVP_DigestFinal(ctx: EVP_MD_CTX, res: *mut u8, n: *u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evpmd(t: HashType) -> (EVP_MD, uint) {
|
pub fn evpmd(t: HashType) -> (EVP_MD, uint) {
|
||||||
|
unsafe {
|
||||||
match t {
|
match t {
|
||||||
MD5 => (libcrypto::EVP_md5(), 16u),
|
MD5 => (libcrypto::EVP_md5(), 16u),
|
||||||
SHA1 => (libcrypto::EVP_sha1(), 20u),
|
SHA1 => (libcrypto::EVP_sha1(), 20u),
|
||||||
|
|
@ -41,6 +42,7 @@ fn evpmd(t: HashType) -> (EVP_MD, uint) {
|
||||||
SHA384 => (libcrypto::EVP_sha384(), 48u),
|
SHA384 => (libcrypto::EVP_sha384(), 48u),
|
||||||
SHA512 => (libcrypto::EVP_sha512(), 64u),
|
SHA512 => (libcrypto::EVP_sha512(), 64u),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Hasher {
|
pub struct Hasher {
|
||||||
|
|
@ -50,51 +52,62 @@ pub struct Hasher {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Hasher(ht: HashType) -> Hasher {
|
pub fn Hasher(ht: HashType) -> Hasher {
|
||||||
|
unsafe {
|
||||||
let ctx = libcrypto::EVP_MD_CTX_create();
|
let ctx = libcrypto::EVP_MD_CTX_create();
|
||||||
let (evp, mdlen) = evpmd(ht);
|
let (evp, mdlen) = evpmd(ht);
|
||||||
let h = Hasher { evp: evp, ctx: ctx, len: mdlen };
|
let h = Hasher { evp: evp, ctx: ctx, len: mdlen };
|
||||||
h.init();
|
h.init();
|
||||||
h
|
h
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl Hasher {
|
pub impl Hasher {
|
||||||
/// Initializes this hasher
|
/// Initializes this hasher
|
||||||
fn init() unsafe {
|
fn init(&self) {
|
||||||
|
unsafe {
|
||||||
libcrypto::EVP_DigestInit(self.ctx, self.evp);
|
libcrypto::EVP_DigestInit(self.ctx, self.evp);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Update this hasher with more input bytes
|
/// Update this hasher with more input bytes
|
||||||
fn update(data: &[u8]) unsafe {
|
fn update(&self, data: &[u8]) {
|
||||||
|
unsafe {
|
||||||
do vec::as_imm_buf(data) |pdata, len| {
|
do vec::as_imm_buf(data) |pdata, len| {
|
||||||
libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
|
libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the digest of all bytes added to this hasher since its last
|
* Return the digest of all bytes added to this hasher since its last
|
||||||
* initialization
|
* initialization
|
||||||
*/
|
*/
|
||||||
fn final() -> ~[u8] unsafe {
|
fn final(&self) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
let mut res = vec::from_elem(self.len, 0u8);
|
let mut res = vec::from_elem(self.len, 0u8);
|
||||||
do vec::as_mut_buf(res) |pres, _len| {
|
do vec::as_mut_buf(res) |pres, _len| {
|
||||||
libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null());
|
libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null());
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hashes the supplied input data using hash t, returning the resulting hash
|
* Hashes the supplied input data using hash t, returning the resulting hash
|
||||||
* value
|
* value
|
||||||
*/
|
*/
|
||||||
pub fn hash(t: HashType, data: &[u8]) -> ~[u8] unsafe {
|
pub fn hash(t: HashType, data: &[u8]) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
let h = Hasher(t);
|
let h = Hasher(t);
|
||||||
h.update(data);
|
h.update(data);
|
||||||
h.final()
|
h.final()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
use hex::FromHex;
|
use hex::FromHex;
|
||||||
use hex::ToHex;
|
use hex::ToHex;
|
||||||
|
|
||||||
|
|
@ -117,7 +130,7 @@ mod tests {
|
||||||
io::println(fmt!("Test failed - %s != %s", calced, hashtest.expected_output));
|
io::println(fmt!("Test failed - %s != %s", calced, hashtest.expected_output));
|
||||||
}
|
}
|
||||||
|
|
||||||
assert calced == hashtest.expected_output;
|
assert!(calced == hashtest.expected_output);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test vectors from http://www.nsrl.nist.gov/testdata/
|
// Test vectors from http://www.nsrl.nist.gov/testdata/
|
||||||
|
|
|
||||||
25
hex.rs
25
hex.rs
|
|
@ -17,13 +17,13 @@
|
||||||
extern mod std;
|
extern mod std;
|
||||||
|
|
||||||
pub trait ToHex {
|
pub trait ToHex {
|
||||||
pure fn to_hex() -> ~str;
|
fn to_hex(&self) -> ~str;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl &[u8]: ToHex {
|
impl<'self> ToHex for &'self [u8] {
|
||||||
pure fn to_hex() -> ~str {
|
fn to_hex(&self) -> ~str {
|
||||||
|
|
||||||
let chars = str::chars(~"0123456789ABCDEF");
|
let chars = str::to_chars(~"0123456789ABCDEF");
|
||||||
|
|
||||||
let mut s = ~"";
|
let mut s = ~"";
|
||||||
|
|
||||||
|
|
@ -45,20 +45,20 @@ impl &[u8]: ToHex {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait FromHex {
|
pub trait FromHex {
|
||||||
pure fn from_hex() -> ~[u8];
|
fn from_hex(&self) -> ~[u8];
|
||||||
}
|
}
|
||||||
|
|
||||||
impl &str: FromHex {
|
impl<'self> FromHex for &'self str {
|
||||||
pure fn from_hex() -> ~[u8] {
|
fn from_hex(&self) -> ~[u8] {
|
||||||
let mut vec = vec::with_capacity(self.len() / 2);
|
let mut vec = vec::with_capacity(self.len() / 2);
|
||||||
|
|
||||||
for str::each_chari(self) |i,c| {
|
for str::each_chari(*self) |i,c| {
|
||||||
|
|
||||||
let nibble =
|
let nibble =
|
||||||
if c >= '0' && c <= '9' { (c as u8) - 0x30 }
|
if c >= '0' && c <= '9' { (c as u8) - 0x30 }
|
||||||
else if c >= 'a' && c <= 'f' { (c as u8) - (0x61 - 10) }
|
else if c >= 'a' && c <= 'f' { (c as u8) - (0x61 - 10) }
|
||||||
else if c >= 'A' && c <= 'F' { (c as u8) - (0x41 - 10) }
|
else if c >= 'A' && c <= 'F' { (c as u8) - (0x41 - 10) }
|
||||||
else { fail ~"bad hex character"; };
|
else { fail!(~"bad hex character"); };
|
||||||
|
|
||||||
if i % 2 == 0 {
|
if i % 2 == 0 {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -76,15 +76,16 @@ impl &str: FromHex {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test() {
|
pub fn test() {
|
||||||
|
|
||||||
assert [05u8, 0xffu8, 0x00u8, 0x59u8].to_hex() == ~"05FF0059";
|
assert!([05u8, 0xffu8, 0x00u8, 0x59u8].to_hex() == ~"05FF0059");
|
||||||
|
|
||||||
assert "00FFA9D1F5".from_hex() == ~[0, 0xff, 0xa9, 0xd1, 0xf5];
|
assert!("00FFA9D1F5".from_hex() == ~[0, 0xff, 0xa9, 0xd1, 0xf5]);
|
||||||
|
|
||||||
assert "00FFA9D1F5".from_hex().to_hex() == ~"00FFA9D1F5";
|
assert!("00FFA9D1F5".from_hex().to_hex() == ~"00FFA9D1F5");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
28
hmac.rs
28
hmac.rs
|
|
@ -17,13 +17,13 @@
|
||||||
use hash::*;
|
use hash::*;
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
struct HMAC_CTX {
|
pub struct HMAC_CTX {
|
||||||
mut md: EVP_MD,
|
md: EVP_MD,
|
||||||
mut md_ctx: EVP_MD_CTX,
|
md_ctx: EVP_MD_CTX,
|
||||||
mut i_ctx: EVP_MD_CTX,
|
i_ctx: EVP_MD_CTX,
|
||||||
mut o_ctx: EVP_MD_CTX,
|
o_ctx: EVP_MD_CTX,
|
||||||
mut key_length: libc::c_uint,
|
key_length: libc::c_uint,
|
||||||
mut key: [libc::c_uchar * 128]
|
key: [libc::c_uchar, ..128]
|
||||||
}
|
}
|
||||||
|
|
||||||
#[link_name = "crypto"]
|
#[link_name = "crypto"]
|
||||||
|
|
@ -38,7 +38,7 @@ extern mod libcrypto {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct HMAC {
|
pub struct HMAC {
|
||||||
priv mut ctx: HMAC_CTX,
|
priv ctx: HMAC_CTX,
|
||||||
priv len: uint,
|
priv len: uint,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,25 +66,29 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl HMAC {
|
pub impl HMAC {
|
||||||
fn update(data: &[u8]) unsafe {
|
fn update(&mut self, data: &[u8]) {
|
||||||
|
unsafe {
|
||||||
do vec::as_imm_buf(data) |pdata, len| {
|
do vec::as_imm_buf(data) |pdata, len| {
|
||||||
libcrypto::HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
|
libcrypto::HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn final() -> ~[u8] unsafe {
|
fn final(&mut self) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
let mut res = vec::from_elem(self.len, 0u8);
|
let mut res = vec::from_elem(self.len, 0u8);
|
||||||
let mut outlen: libc::c_uint = 0;
|
let mut outlen: libc::c_uint = 0;
|
||||||
do vec::as_mut_buf(res) |pres, _len| {
|
do vec::as_mut_buf(res) |pres, _len| {
|
||||||
libcrypto::HMAC_Final(&mut self.ctx, pres, &mut outlen);
|
libcrypto::HMAC_Final(&mut self.ctx, pres, &mut outlen);
|
||||||
assert self.len == outlen as uint
|
assert!(self.len == outlen as uint)
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let h = HMAC(SHA512, ~[00u8]);
|
let mut h = HMAC(SHA512, ~[00u8]);
|
||||||
|
|
||||||
h.update(~[00u8]);
|
h.update(~[00u8]);
|
||||||
|
|
||||||
|
|
|
||||||
36
pkcs5.rs
36
pkcs5.rs
|
|
@ -1,4 +1,4 @@
|
||||||
use libc::{c_char, c_uchar, c_int};
|
use core::libc::c_int;
|
||||||
|
|
||||||
#[link_name = "crypto"]
|
#[link_name = "crypto"]
|
||||||
#[abi = "cdecl"]
|
#[abi = "cdecl"]
|
||||||
|
|
@ -14,21 +14,23 @@ 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,
|
pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint,
|
||||||
keylen: uint) -> ~[u8] {
|
keylen: uint) -> ~[u8] {
|
||||||
assert iter >= 1u;
|
assert!(iter >= 1u);
|
||||||
assert keylen >= 1u;
|
assert!(keylen >= 1u);
|
||||||
|
|
||||||
do str::as_buf(pass) |pass_buf, pass_len| {
|
do str::as_buf(pass) |pass_buf, pass_len| {
|
||||||
do vec::as_imm_buf(salt) |salt_buf, salt_len| {
|
do vec::as_imm_buf(salt) |salt_buf, salt_len| {
|
||||||
let mut out = vec::with_capacity(keylen);
|
let mut out = vec::with_capacity(keylen);
|
||||||
|
|
||||||
do vec::as_mut_buf(out) |out_buf, _out_len| {
|
do vec::as_mut_buf(out) |out_buf, _out_len| {
|
||||||
|
unsafe {
|
||||||
let r = libcrypto::PKCS5_PBKDF2_HMAC_SHA1(
|
let r = libcrypto::PKCS5_PBKDF2_HMAC_SHA1(
|
||||||
pass_buf, pass_len as c_int,
|
pass_buf, pass_len as c_int,
|
||||||
salt_buf, salt_len as c_int,
|
salt_buf, salt_len as c_int,
|
||||||
iter as c_int, keylen as c_int,
|
iter as c_int, keylen as c_int,
|
||||||
out_buf);
|
out_buf);
|
||||||
|
|
||||||
if r != 1 as c_int { fail; }
|
if r != 1 as c_int { fail!(); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { vec::raw::set_len(&mut out, keylen); }
|
unsafe { vec::raw::set_len(&mut out, keylen); }
|
||||||
|
|
@ -40,11 +42,13 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint,
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
// Test vectors from
|
// Test vectors from
|
||||||
// http://tools.ietf.org/html/draft-josefsson-pbkdf2-test-vectors-06
|
// http://tools.ietf.org/html/draft-josefsson-pbkdf2-test-vectors-06
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pbkdf2_hmac_sha1() {
|
fn test_pbkdf2_hmac_sha1() {
|
||||||
assert pbkdf2_hmac_sha1(
|
assert!(pbkdf2_hmac_sha1(
|
||||||
"password",
|
"password",
|
||||||
str::to_bytes("salt"),
|
str::to_bytes("salt"),
|
||||||
1u,
|
1u,
|
||||||
|
|
@ -53,9 +57,9 @@ mod tests {
|
||||||
0x0c_u8, 0x60_u8, 0xc8_u8, 0x0f_u8, 0x96_u8, 0x1f_u8, 0x0e_u8,
|
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,
|
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
|
0x12_u8, 0x06_u8, 0x2f_u8, 0xe0_u8, 0x37_u8, 0xa6_u8
|
||||||
];
|
]);
|
||||||
|
|
||||||
assert pbkdf2_hmac_sha1(
|
assert!(pbkdf2_hmac_sha1(
|
||||||
"password",
|
"password",
|
||||||
str::to_bytes("salt"),
|
str::to_bytes("salt"),
|
||||||
2u,
|
2u,
|
||||||
|
|
@ -64,9 +68,9 @@ mod tests {
|
||||||
0xea_u8, 0x6c_u8, 0x01_u8, 0x4d_u8, 0xc7_u8, 0x2d_u8, 0x6f_u8,
|
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,
|
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
|
0x41_u8, 0xf0_u8, 0xd8_u8, 0xde_u8, 0x89_u8, 0x57_u8
|
||||||
];
|
]);
|
||||||
|
|
||||||
assert pbkdf2_hmac_sha1(
|
assert!(pbkdf2_hmac_sha1(
|
||||||
"password",
|
"password",
|
||||||
str::to_bytes("salt"),
|
str::to_bytes("salt"),
|
||||||
4096u,
|
4096u,
|
||||||
|
|
@ -75,9 +79,9 @@ mod tests {
|
||||||
0x4b_u8, 0x00_u8, 0x79_u8, 0x01_u8, 0xb7_u8, 0x65_u8, 0x48_u8,
|
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,
|
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
|
0x21_u8, 0xd0_u8, 0x65_u8, 0xa4_u8, 0x29_u8, 0xc1_u8
|
||||||
];
|
]);
|
||||||
|
|
||||||
assert pbkdf2_hmac_sha1(
|
assert!(pbkdf2_hmac_sha1(
|
||||||
"password",
|
"password",
|
||||||
str::to_bytes("salt"),
|
str::to_bytes("salt"),
|
||||||
16777216u,
|
16777216u,
|
||||||
|
|
@ -86,9 +90,9 @@ mod tests {
|
||||||
0xee_u8, 0xfe_u8, 0x3d_u8, 0x61_u8, 0xcd_u8, 0x4d_u8, 0xa4_u8,
|
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,
|
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
|
0x15_u8, 0x8c_u8, 0x26_u8, 0x34_u8, 0xe9_u8, 0x84_u8
|
||||||
];
|
]);
|
||||||
|
|
||||||
assert pbkdf2_hmac_sha1(
|
assert!(pbkdf2_hmac_sha1(
|
||||||
"passwordPASSWORDpassword",
|
"passwordPASSWORDpassword",
|
||||||
str::to_bytes("saltSALTsaltSALTsaltSALTsaltSALTsalt"),
|
str::to_bytes("saltSALTsaltSALTsaltSALTsaltSALTsalt"),
|
||||||
4096u,
|
4096u,
|
||||||
|
|
@ -98,9 +102,9 @@ mod tests {
|
||||||
0x9b_u8, 0x80_u8, 0xc8_u8, 0xd8_u8, 0x36_u8, 0x62_u8, 0xc0_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,
|
0xe4_u8, 0x4a_u8, 0x8b_u8, 0x29_u8, 0x1a_u8, 0x96_u8, 0x4c_u8,
|
||||||
0xf2_u8, 0xf0_u8, 0x70_u8, 0x38_u8
|
0xf2_u8, 0xf0_u8, 0x70_u8, 0x38_u8
|
||||||
];
|
]);
|
||||||
|
|
||||||
assert pbkdf2_hmac_sha1(
|
assert!(pbkdf2_hmac_sha1(
|
||||||
"pass\x00word",
|
"pass\x00word",
|
||||||
str::to_bytes("sa\x00lt"),
|
str::to_bytes("sa\x00lt"),
|
||||||
4096u,
|
4096u,
|
||||||
|
|
@ -109,6 +113,6 @@ mod tests {
|
||||||
0x56_u8, 0xfa_u8, 0x6a_u8, 0xa7_u8, 0x55_u8, 0x48_u8, 0x09_u8,
|
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,
|
0x9d_u8, 0xcc_u8, 0x37_u8, 0xd7_u8, 0xf0_u8, 0x34_u8, 0x25_u8,
|
||||||
0xe0_u8, 0xc3_u8
|
0xe0_u8, 0xc3_u8
|
||||||
];
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
174
pkey.rs
174
pkey.rs
|
|
@ -1,4 +1,4 @@
|
||||||
use libc::{c_int, c_uint};
|
use core::libc::{c_int, c_uint};
|
||||||
use hash::{HashType, MD5, SHA1, SHA224, SHA256, SHA384, SHA512};
|
use hash::{HashType, MD5, SHA1, SHA224, SHA256, SHA384, SHA512};
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
|
|
@ -74,25 +74,33 @@ fn openssl_hash_nid(hash: HashType) -> c_int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rsa_to_any(rsa: *RSA) -> *ANYKEY unsafe {
|
fn rsa_to_any(rsa: *RSA) -> *ANYKEY {
|
||||||
|
unsafe {
|
||||||
cast::reinterpret_cast(&rsa)
|
cast::reinterpret_cast(&rsa)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn any_to_rsa(anykey: *ANYKEY) -> *RSA unsafe {
|
fn any_to_rsa(anykey: *ANYKEY) -> *RSA {
|
||||||
|
unsafe {
|
||||||
cast::reinterpret_cast(&anykey)
|
cast::reinterpret_cast(&anykey)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PKey {
|
pub struct PKey {
|
||||||
priv mut evp: *EVP_PKEY,
|
priv evp: *EVP_PKEY,
|
||||||
priv mut parts: Parts,
|
priv parts: Parts,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn PKey() -> PKey {
|
pub fn PKey() -> PKey {
|
||||||
|
unsafe {
|
||||||
PKey { evp: libcrypto::EVP_PKEY_new(), parts: Neither }
|
PKey { evp: libcrypto::EVP_PKEY_new(), parts: Neither }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///Represents a public key, optionally with a private key attached.
|
||||||
priv impl PKey {
|
priv impl PKey {
|
||||||
fn _tostr(f: fn@(*EVP_PKEY, &*mut u8) -> c_int) -> ~[u8] unsafe {
|
priv fn _tostr(&self, f: @fn(*EVP_PKEY, &*mut u8) -> c_int) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
let buf = ptr::mut_null();
|
let buf = ptr::mut_null();
|
||||||
let len = f(self.evp, &buf);
|
let len = f(self.evp, &buf);
|
||||||
if len < 0 as c_int { return ~[]; }
|
if len < 0 as c_int { return ~[]; }
|
||||||
|
|
@ -102,24 +110,28 @@ priv impl PKey {
|
||||||
f(self.evp, &ps)
|
f(self.evp, &ps)
|
||||||
};
|
};
|
||||||
|
|
||||||
vec::slice(s, 0u, r as uint)
|
vec::slice(s, 0u, r as uint).to_owned()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _fromstr(
|
priv fn _fromstr(
|
||||||
|
&mut self,
|
||||||
s: &[u8],
|
s: &[u8],
|
||||||
f: fn@(c_int, &*EVP_PKEY, &*u8, c_uint) -> *EVP_PKEY
|
f: @fn(c_int, &*EVP_PKEY, &*u8, c_uint) -> *EVP_PKEY
|
||||||
) unsafe {
|
) {
|
||||||
|
unsafe {
|
||||||
do vec::as_imm_buf(s) |ps, len| {
|
do vec::as_imm_buf(s) |ps, len| {
|
||||||
let evp = ptr::null();
|
let evp = ptr::null();
|
||||||
f(6 as c_int, &evp, &ps, len as c_uint);
|
f(6 as c_int, &evp, &ps, len as c_uint);
|
||||||
self.evp = evp;
|
self.evp = evp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///Represents a public key, optionally with a private key attached.
|
|
||||||
pub impl PKey {
|
pub impl PKey {
|
||||||
fn gen(keysz: uint) unsafe {
|
fn gen(&mut self, keysz: uint) {
|
||||||
|
unsafe {
|
||||||
let rsa = libcrypto::RSA_generate_key(
|
let rsa = libcrypto::RSA_generate_key(
|
||||||
keysz as c_uint,
|
keysz as c_uint,
|
||||||
65537u as c_uint,
|
65537u as c_uint,
|
||||||
|
|
@ -132,49 +144,60 @@ pub impl PKey {
|
||||||
libcrypto::EVP_PKEY_assign(self.evp, 6 as c_int, rsa_);
|
libcrypto::EVP_PKEY_assign(self.evp, 6 as c_int, rsa_);
|
||||||
self.parts = Both;
|
self.parts = Both;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a serialized form of the public key, suitable for load_pub().
|
* Returns a serialized form of the public key, suitable for load_pub().
|
||||||
*/
|
*/
|
||||||
fn save_pub() -> ~[u8] {
|
fn save_pub(&self) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
self._tostr(libcrypto::i2d_PublicKey)
|
self._tostr(libcrypto::i2d_PublicKey)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a serialized form of the public key, as produced by save_pub().
|
* Loads a serialized form of the public key, as produced by save_pub().
|
||||||
*/
|
*/
|
||||||
fn load_pub(s: &[u8]) {
|
fn load_pub(&mut self, s: &[u8]) {
|
||||||
|
unsafe {
|
||||||
self._fromstr(s, libcrypto::d2i_PublicKey);
|
self._fromstr(s, libcrypto::d2i_PublicKey);
|
||||||
self.parts = Public;
|
self.parts = Public;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a serialized form of the public and private keys, suitable for
|
* Returns a serialized form of the public and private keys, suitable for
|
||||||
* load_priv().
|
* load_priv().
|
||||||
*/
|
*/
|
||||||
fn save_priv() -> ~[u8] {
|
fn save_priv(&self, ) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
self._tostr(libcrypto::i2d_PrivateKey)
|
self._tostr(libcrypto::i2d_PrivateKey)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Loads a serialized form of the public and private keys, as produced by
|
* Loads a serialized form of the public and private keys, as produced by
|
||||||
* save_priv().
|
* save_priv().
|
||||||
*/
|
*/
|
||||||
fn load_priv(s: &[u8]) {
|
fn load_priv(&mut self, s: &[u8]) {
|
||||||
|
unsafe {
|
||||||
self._fromstr(s, libcrypto::d2i_PrivateKey);
|
self._fromstr(s, libcrypto::d2i_PrivateKey);
|
||||||
self.parts = Both;
|
self.parts = Both;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the size of the public key modulus.
|
* Returns the size of the public key modulus.
|
||||||
*/
|
*/
|
||||||
fn size() -> uint {
|
fn size(&self) -> uint {
|
||||||
|
unsafe {
|
||||||
libcrypto::RSA_size(libcrypto::EVP_PKEY_get1_RSA(self.evp)) as uint
|
libcrypto::RSA_size(libcrypto::EVP_PKEY_get1_RSA(self.evp)) as uint
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether this pkey object can perform the specified role.
|
* Returns whether this pkey object can perform the specified role.
|
||||||
*/
|
*/
|
||||||
fn can(r: Role) -> bool {
|
fn can(&self, r: Role) -> bool {
|
||||||
match r {
|
match r {
|
||||||
Encrypt =>
|
Encrypt =>
|
||||||
match self.parts {
|
match self.parts {
|
||||||
|
|
@ -203,19 +226,22 @@ pub impl PKey {
|
||||||
* Returns the maximum amount of data that can be encrypted by an encrypt()
|
* Returns the maximum amount of data that can be encrypted by an encrypt()
|
||||||
* call.
|
* call.
|
||||||
*/
|
*/
|
||||||
fn max_data() -> uint unsafe {
|
fn max_data(&self) -> uint {
|
||||||
|
unsafe {
|
||||||
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
||||||
let len = libcrypto::RSA_size(rsa);
|
let len = libcrypto::RSA_size(rsa);
|
||||||
|
|
||||||
// 41 comes from RSA_public_encrypt(3) for OAEP
|
// 41 comes from RSA_public_encrypt(3) for OAEP
|
||||||
len as uint - 41u
|
len as uint - 41u
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn encrypt_with_padding(s: &[u8], padding: EncryptionPadding) -> ~[u8] unsafe {
|
fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
||||||
let len = libcrypto::RSA_size(rsa);
|
let len = libcrypto::RSA_size(rsa);
|
||||||
|
|
||||||
assert s.len() < self.max_data();
|
assert!(s.len() < self.max_data());
|
||||||
|
|
||||||
let mut r = vec::from_elem(len as uint + 1u, 0u8);
|
let mut r = vec::from_elem(len as uint + 1u, 0u8);
|
||||||
|
|
||||||
|
|
@ -232,17 +258,19 @@ pub impl PKey {
|
||||||
if rv < 0 as c_int {
|
if rv < 0 as c_int {
|
||||||
~[]
|
~[]
|
||||||
} else {
|
} else {
|
||||||
vec::slice(r, 0u, rv as uint)
|
vec::const_slice(r, 0u, rv as uint).to_owned()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decrypt_with_padding(s: &[u8], padding: EncryptionPadding) -> ~[u8] unsafe {
|
fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
||||||
let len = libcrypto::RSA_size(rsa);
|
let len = libcrypto::RSA_size(rsa);
|
||||||
|
|
||||||
assert s.len() as c_uint == libcrypto::RSA_size(rsa);
|
assert!(s.len() as c_uint == libcrypto::RSA_size(rsa));
|
||||||
|
|
||||||
let mut r = vec::from_elem(len as uint + 1u, 0u8);
|
let mut r = vec::from_elem(len as uint + 1u, 0u8);
|
||||||
|
|
||||||
|
|
@ -259,7 +287,8 @@ pub impl PKey {
|
||||||
if rv < 0 as c_int {
|
if rv < 0 as c_int {
|
||||||
~[]
|
~[]
|
||||||
} else {
|
} else {
|
||||||
vec::slice(r, 0u, rv as uint)
|
vec::const_slice(r, 0u, rv as uint).to_owned()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -269,26 +298,27 @@ pub impl PKey {
|
||||||
* Encrypts data using OAEP padding, returning the encrypted data. The
|
* Encrypts data using OAEP padding, returning the encrypted data. The
|
||||||
* supplied data must not be larger than max_data().
|
* supplied data must not be larger than max_data().
|
||||||
*/
|
*/
|
||||||
fn encrypt(s: &[u8]) -> ~[u8] unsafe { self.encrypt_with_padding(s, OAEP) }
|
fn encrypt(&self, s: &[u8]) -> ~[u8] { unsafe { self.encrypt_with_padding(s, OAEP) } }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypts data, expecting OAEP padding, returning the decrypted data.
|
* Decrypts data, expecting OAEP padding, returning the decrypted data.
|
||||||
*/
|
*/
|
||||||
fn decrypt(s: &[u8]) -> ~[u8] unsafe { self.decrypt_with_padding(s, OAEP) }
|
fn decrypt(&self, s: &[u8]) -> ~[u8] { unsafe { self.decrypt_with_padding(s, OAEP) } }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Signs data, using OpenSSL's default scheme and sha256. Unlike encrypt(),
|
* Signs data, using OpenSSL's default scheme and sha256. Unlike encrypt(),
|
||||||
* can process an arbitrary amount of data; returns the signature.
|
* can process an arbitrary amount of data; returns the signature.
|
||||||
*/
|
*/
|
||||||
fn sign(s: &[u8]) -> ~[u8] unsafe { self.sign_with_hash(s, SHA256) }
|
fn sign(&self, s: &[u8]) -> ~[u8] { unsafe { self.sign_with_hash(s, SHA256) } }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies a signature s (using OpenSSL's default scheme and sha256) on a
|
* Verifies a signature s (using OpenSSL's default scheme and sha256) on a
|
||||||
* message m. Returns true if the signature is valid, and false otherwise.
|
* message m. Returns true if the signature is valid, and false otherwise.
|
||||||
*/
|
*/
|
||||||
fn verify(m: &[u8], s: &[u8]) -> bool unsafe { self.verify_with_hash(m, s, SHA256) }
|
fn verify(&self, m: &[u8], s: &[u8]) -> bool { unsafe { self.verify_with_hash(m, s, SHA256) } }
|
||||||
|
|
||||||
fn sign_with_hash(s: &[u8], hash: HashType) -> ~[u8] unsafe {
|
fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
||||||
let len = libcrypto::RSA_size(rsa);
|
let len = libcrypto::RSA_size(rsa);
|
||||||
let mut r = vec::from_elem(len as uint + 1u, 0u8);
|
let mut r = vec::from_elem(len as uint + 1u, 0u8);
|
||||||
|
|
@ -308,13 +338,15 @@ pub impl PKey {
|
||||||
if rv < 0 as c_int {
|
if rv < 0 as c_int {
|
||||||
~[]
|
~[]
|
||||||
} else {
|
} else {
|
||||||
vec::slice(r, 0u, *plen as uint)
|
vec::const_slice(r, 0u, *plen as uint).to_owned()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn verify_with_hash(m: &[u8], s: &[u8], hash: HashType) -> bool unsafe {
|
fn verify_with_hash(&self, m: &[u8], s: &[u8], hash: HashType) -> bool {
|
||||||
|
unsafe {
|
||||||
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
||||||
|
|
||||||
do vec::as_imm_buf(m) |pm, m_len| {
|
do vec::as_imm_buf(m) |pm, m_len| {
|
||||||
|
|
@ -332,94 +364,98 @@ pub impl PKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use hash::{MD5, SHA1};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_gen_pub() {
|
fn test_gen_pub() {
|
||||||
let k0 = PKey();
|
let mut k0 = PKey();
|
||||||
let k1 = PKey();
|
let mut k1 = PKey();
|
||||||
k0.gen(512u);
|
k0.gen(512u);
|
||||||
k1.load_pub(k0.save_pub());
|
k1.load_pub(k0.save_pub());
|
||||||
assert(k0.save_pub() == k1.save_pub());
|
assert!(k0.save_pub() == k1.save_pub());
|
||||||
assert(k0.size() == k1.size());
|
assert!(k0.size() == k1.size());
|
||||||
assert(k0.can(Encrypt));
|
assert!(k0.can(Encrypt));
|
||||||
assert(k0.can(Decrypt));
|
assert!(k0.can(Decrypt));
|
||||||
assert(k0.can(Verify));
|
assert!(k0.can(Verify));
|
||||||
assert(k0.can(Sign));
|
assert!(k0.can(Sign));
|
||||||
assert(k1.can(Encrypt));
|
assert!(k1.can(Encrypt));
|
||||||
assert(!k1.can(Decrypt));
|
assert!(!k1.can(Decrypt));
|
||||||
assert(k1.can(Verify));
|
assert!(k1.can(Verify));
|
||||||
assert(!k1.can(Sign));
|
assert!(!k1.can(Sign));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_gen_priv() {
|
fn test_gen_priv() {
|
||||||
let k0 = PKey();
|
let mut k0 = PKey();
|
||||||
let k1 = PKey();
|
let mut k1 = PKey();
|
||||||
k0.gen(512u);
|
k0.gen(512u);
|
||||||
k1.load_priv(k0.save_priv());
|
k1.load_priv(k0.save_priv());
|
||||||
assert(k0.save_priv() == k1.save_priv());
|
assert!(k0.save_priv() == k1.save_priv());
|
||||||
assert(k0.size() == k1.size());
|
assert!(k0.size() == k1.size());
|
||||||
assert(k0.can(Encrypt));
|
assert!(k0.can(Encrypt));
|
||||||
assert(k0.can(Decrypt));
|
assert!(k0.can(Decrypt));
|
||||||
assert(k0.can(Verify));
|
assert!(k0.can(Verify));
|
||||||
assert(k0.can(Sign));
|
assert!(k0.can(Sign));
|
||||||
assert(k1.can(Encrypt));
|
assert!(k1.can(Encrypt));
|
||||||
assert(k1.can(Decrypt));
|
assert!(k1.can(Decrypt));
|
||||||
assert(k1.can(Verify));
|
assert!(k1.can(Verify));
|
||||||
assert(k1.can(Sign));
|
assert!(k1.can(Sign));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_encrypt() {
|
fn test_encrypt() {
|
||||||
let k0 = PKey();
|
let mut k0 = PKey();
|
||||||
let k1 = PKey();
|
let mut k1 = PKey();
|
||||||
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
||||||
k0.gen(512u);
|
k0.gen(512u);
|
||||||
k1.load_pub(k0.save_pub());
|
k1.load_pub(k0.save_pub());
|
||||||
let emsg = k1.encrypt(msg);
|
let emsg = k1.encrypt(msg);
|
||||||
let dmsg = k0.decrypt(emsg);
|
let dmsg = k0.decrypt(emsg);
|
||||||
assert(msg == dmsg);
|
assert!(msg == dmsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_encrypt_pkcs() {
|
fn test_encrypt_pkcs() {
|
||||||
let k0 = PKey();
|
let mut k0 = PKey();
|
||||||
let k1 = PKey();
|
let mut k1 = PKey();
|
||||||
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
||||||
k0.gen(512u);
|
k0.gen(512u);
|
||||||
k1.load_pub(k0.save_pub());
|
k1.load_pub(k0.save_pub());
|
||||||
let emsg = k1.encrypt_with_padding(msg, PKCS1v15);
|
let emsg = k1.encrypt_with_padding(msg, PKCS1v15);
|
||||||
let dmsg = k0.decrypt_with_padding(emsg, PKCS1v15);
|
let dmsg = k0.decrypt_with_padding(emsg, PKCS1v15);
|
||||||
assert(msg == dmsg);
|
assert!(msg == dmsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sign() {
|
fn test_sign() {
|
||||||
let k0 = PKey();
|
let mut k0 = PKey();
|
||||||
let k1 = PKey();
|
let mut k1 = PKey();
|
||||||
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
||||||
k0.gen(512u);
|
k0.gen(512u);
|
||||||
k1.load_pub(k0.save_pub());
|
k1.load_pub(k0.save_pub());
|
||||||
let sig = k0.sign(msg);
|
let sig = k0.sign(msg);
|
||||||
let rv = k1.verify(msg, sig);
|
let rv = k1.verify(msg, sig);
|
||||||
assert(rv == true);
|
assert!(rv == true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sign_hashes() {
|
fn test_sign_hashes() {
|
||||||
let k0 = PKey();
|
let mut k0 = PKey();
|
||||||
let k1 = PKey();
|
let mut k1 = PKey();
|
||||||
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
let msg = ~[0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
||||||
k0.gen(512u);
|
k0.gen(512u);
|
||||||
k1.load_pub(k0.save_pub());
|
k1.load_pub(k0.save_pub());
|
||||||
|
|
||||||
let sig = k0.sign_with_hash(msg, MD5);
|
let sig = k0.sign_with_hash(msg, MD5);
|
||||||
|
|
||||||
assert k1.verify_with_hash(msg, sig, MD5);
|
assert!(k1.verify_with_hash(msg, sig, MD5));
|
||||||
assert !k1.verify_with_hash(msg, sig, SHA1);
|
assert!(!k1.verify_with_hash(msg, sig, SHA1));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
rand.rs
8
rand.rs
|
|
@ -1,4 +1,4 @@
|
||||||
use libc::{c_uchar, c_int};
|
use core::libc::c_int;
|
||||||
|
|
||||||
#[link_name = "crypto"]
|
#[link_name = "crypto"]
|
||||||
#[abi = "cdecl"]
|
#[abi = "cdecl"]
|
||||||
|
|
@ -10,8 +10,10 @@ pub fn rand_bytes(len: uint) -> ~[u8] {
|
||||||
let mut out = vec::with_capacity(len);
|
let mut out = vec::with_capacity(len);
|
||||||
|
|
||||||
do vec::as_mut_buf(out) |out_buf, len| {
|
do vec::as_mut_buf(out) |out_buf, len| {
|
||||||
|
unsafe {
|
||||||
let r = libcrypto::RAND_bytes(out_buf, len as c_int);
|
let r = libcrypto::RAND_bytes(out_buf, len as c_int);
|
||||||
if r != 1 as c_int { fail }
|
if r != 1 as c_int { fail!() }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { vec::raw::set_len(&mut out, len); }
|
unsafe { vec::raw::set_len(&mut out, len); }
|
||||||
|
|
@ -21,6 +23,8 @@ pub fn rand_bytes(len: uint) -> ~[u8] {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rand_bytes() {
|
fn test_rand_bytes() {
|
||||||
let bytes = rand_bytes(32u);
|
let bytes = rand_bytes(32u);
|
||||||
|
|
|
||||||
70
symm.rs
70
symm.rs
|
|
@ -1,8 +1,4 @@
|
||||||
use libc::{c_int, c_uint};
|
use core::libc::{c_int, c_uint};
|
||||||
|
|
||||||
export encryptmode, decryptmode;
|
|
||||||
export encrypt, decrypt;
|
|
||||||
export libcrypto;
|
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
type EVP_CIPHER_CTX = *libc::c_void;
|
type EVP_CIPHER_CTX = *libc::c_void;
|
||||||
|
|
@ -18,13 +14,13 @@ extern mod libcrypto {
|
||||||
|
|
||||||
fn EVP_aes_128_ecb() -> EVP_CIPHER;
|
fn EVP_aes_128_ecb() -> EVP_CIPHER;
|
||||||
fn EVP_aes_128_cbc() -> EVP_CIPHER;
|
fn EVP_aes_128_cbc() -> EVP_CIPHER;
|
||||||
fn EVP_aes_128_ctr() -> EVP_CIPHER;
|
// fn EVP_aes_128_ctr() -> EVP_CIPHER;
|
||||||
fn EVP_aes_128_gcm() -> EVP_CIPHER;
|
// fn EVP_aes_128_gcm() -> EVP_CIPHER;
|
||||||
|
|
||||||
fn EVP_aes_256_ecb() -> EVP_CIPHER;
|
fn EVP_aes_256_ecb() -> EVP_CIPHER;
|
||||||
fn EVP_aes_256_cbc() -> EVP_CIPHER;
|
fn EVP_aes_256_cbc() -> EVP_CIPHER;
|
||||||
fn EVP_aes_256_ctr() -> EVP_CIPHER;
|
// fn EVP_aes_256_ctr() -> EVP_CIPHER;
|
||||||
fn EVP_aes_256_gcm() -> EVP_CIPHER;
|
// fn EVP_aes_256_gcm() -> EVP_CIPHER;
|
||||||
|
|
||||||
fn EVP_rc4() -> EVP_CIPHER;
|
fn EVP_rc4() -> EVP_CIPHER;
|
||||||
|
|
||||||
|
|
@ -44,31 +40,33 @@ pub enum Mode {
|
||||||
pub enum Type {
|
pub enum Type {
|
||||||
AES_128_ECB,
|
AES_128_ECB,
|
||||||
AES_128_CBC,
|
AES_128_CBC,
|
||||||
AES_128_CTR,
|
// AES_128_CTR,
|
||||||
//AES_128_GCM,
|
//AES_128_GCM,
|
||||||
|
|
||||||
AES_256_ECB,
|
AES_256_ECB,
|
||||||
AES_256_CBC,
|
AES_256_CBC,
|
||||||
AES_256_CTR,
|
// AES_256_CTR,
|
||||||
//AES_256_GCM,
|
//AES_256_GCM,
|
||||||
|
|
||||||
RC4_128,
|
RC4_128,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evpc(t: Type) -> (EVP_CIPHER, uint, uint) {
|
fn evpc(t: Type) -> (EVP_CIPHER, uint, uint) {
|
||||||
|
unsafe {
|
||||||
match t {
|
match t {
|
||||||
AES_128_ECB => (libcrypto::EVP_aes_128_ecb(), 16u, 16u),
|
AES_128_ECB => (libcrypto::EVP_aes_128_ecb(), 16u, 16u),
|
||||||
AES_128_CBC => (libcrypto::EVP_aes_128_cbc(), 16u, 16u),
|
AES_128_CBC => (libcrypto::EVP_aes_128_cbc(), 16u, 16u),
|
||||||
AES_128_CTR => (libcrypto::EVP_aes_128_ctr(), 16u, 0u),
|
// AES_128_CTR => (libcrypto::EVP_aes_128_ctr(), 16u, 0u),
|
||||||
//AES_128_GCM => (libcrypto::EVP_aes_128_gcm(), 16u, 16u),
|
//AES_128_GCM => (libcrypto::EVP_aes_128_gcm(), 16u, 16u),
|
||||||
|
|
||||||
AES_256_ECB => (libcrypto::EVP_aes_256_ecb(), 32u, 16u),
|
AES_256_ECB => (libcrypto::EVP_aes_256_ecb(), 32u, 16u),
|
||||||
AES_256_CBC => (libcrypto::EVP_aes_256_cbc(), 32u, 16u),
|
AES_256_CBC => (libcrypto::EVP_aes_256_cbc(), 32u, 16u),
|
||||||
AES_256_CTR => (libcrypto::EVP_aes_256_ctr(), 32u, 0u),
|
// AES_256_CTR => (libcrypto::EVP_aes_256_ctr(), 32u, 0u),
|
||||||
//AES_256_GCM => (libcrypto::EVP_aes_256_gcm(), 32u, 16u),
|
//AES_256_GCM => (libcrypto::EVP_aes_256_gcm(), 32u, 16u),
|
||||||
|
|
||||||
RC4_128 => (libcrypto::EVP_rc4(), 16u, 0u),
|
RC4_128 => (libcrypto::EVP_rc4(), 16u, 0u),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a symmetric cipher context.
|
/// Represents a symmetric cipher context.
|
||||||
|
|
@ -80,9 +78,11 @@ pub struct Crypter {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn Crypter(t: Type) -> Crypter {
|
pub fn Crypter(t: Type) -> Crypter {
|
||||||
|
unsafe {
|
||||||
let ctx = libcrypto::EVP_CIPHER_CTX_new();
|
let ctx = libcrypto::EVP_CIPHER_CTX_new();
|
||||||
let (evp, keylen, blocksz) = evpc(t);
|
let (evp, keylen, blocksz) = evpc(t);
|
||||||
Crypter { evp: evp, ctx: ctx, keylen: keylen, blocksize: blocksz }
|
Crypter { evp: evp, ctx: ctx, keylen: keylen, blocksize: blocksz }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl Crypter {
|
pub impl Crypter {
|
||||||
|
|
@ -90,22 +90,25 @@ pub impl Crypter {
|
||||||
* Enables or disables padding. If padding is disabled, total amount of
|
* Enables or disables padding. If padding is disabled, total amount of
|
||||||
* data encrypted must be a multiple of block size.
|
* data encrypted must be a multiple of block size.
|
||||||
*/
|
*/
|
||||||
fn pad(padding: bool) {
|
fn pad(&self, padding: bool) {
|
||||||
if self.blocksize > 0 {
|
if self.blocksize > 0 {
|
||||||
|
unsafe {
|
||||||
let v = if padding { 1 } else { 0 } as c_int;
|
let v = if padding { 1 } else { 0 } as c_int;
|
||||||
libcrypto::EVP_CIPHER_CTX_set_padding(self.ctx, v);
|
libcrypto::EVP_CIPHER_CTX_set_padding(self.ctx, v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes this crypter.
|
* Initializes this crypter.
|
||||||
*/
|
*/
|
||||||
fn init(mode: Mode, key: &[u8], iv: &[u8]) unsafe {
|
fn init(&self, mode: Mode, key: &[u8], iv: &[u8]) {
|
||||||
|
unsafe {
|
||||||
let mode = match mode {
|
let mode = match mode {
|
||||||
Encrypt => 1 as c_int,
|
Encrypt => 1 as c_int,
|
||||||
Decrypt => 0 as c_int,
|
Decrypt => 0 as c_int,
|
||||||
};
|
};
|
||||||
assert key.len() == self.keylen;
|
assert!(key.len() == self.keylen);
|
||||||
|
|
||||||
do vec::as_imm_buf(key) |pkey, _len| {
|
do vec::as_imm_buf(key) |pkey, _len| {
|
||||||
do vec::as_imm_buf(iv) |piv, _len| {
|
do vec::as_imm_buf(iv) |piv, _len| {
|
||||||
|
|
@ -119,12 +122,14 @@ pub impl Crypter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update this crypter with more data to encrypt or decrypt. Returns
|
* Update this crypter with more data to encrypt or decrypt. Returns
|
||||||
* encrypted or decrypted bytes.
|
* encrypted or decrypted bytes.
|
||||||
*/
|
*/
|
||||||
fn update(data: &[u8]) -> ~[u8] unsafe {
|
fn update(&self, data: &[u8]) -> ~[u8] {
|
||||||
|
unsafe {
|
||||||
do vec::as_imm_buf(data) |pdata, len| {
|
do vec::as_imm_buf(data) |pdata, len| {
|
||||||
let mut res = vec::from_elem(len + self.blocksize, 0u8);
|
let mut res = vec::from_elem(len + self.blocksize, 0u8);
|
||||||
|
|
||||||
|
|
@ -142,15 +147,17 @@ pub impl Crypter {
|
||||||
reslen
|
reslen
|
||||||
};
|
};
|
||||||
|
|
||||||
vec::slice(res, 0u, reslen as uint)
|
vec::slice(res, 0u, reslen as uint).to_owned()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finish crypting. Returns the remaining partial block of output, if any.
|
* Finish crypting. Returns the remaining partial block of output, if any.
|
||||||
*/
|
*/
|
||||||
fn final() -> ~[u8] unsafe {
|
fn final(&self) -> ~[u8] {
|
||||||
let res = vec::to_mut(vec::from_elem(self.blocksize, 0u8));
|
unsafe {
|
||||||
|
let mut res = vec::from_elem(self.blocksize, 0u8);
|
||||||
|
|
||||||
io::println(fmt!("final, res %? long", res.len()));
|
io::println(fmt!("final, res %? long", res.len()));
|
||||||
|
|
||||||
|
|
@ -162,7 +169,8 @@ pub impl Crypter {
|
||||||
|
|
||||||
io::println(fmt!("openssl says %? bytes", reslen));
|
io::println(fmt!("openssl says %? bytes", reslen));
|
||||||
|
|
||||||
vec::slice(res, 0u, reslen as uint)
|
vec::slice(res, 0u, reslen as uint).to_owned()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -170,7 +178,7 @@ pub impl Crypter {
|
||||||
* Encrypts data, using the specified crypter type in encrypt mode with the
|
* Encrypts data, using the specified crypter type in encrypt mode with the
|
||||||
* specified key and iv; returns the resulting (encrypted) data.
|
* specified key and iv; returns the resulting (encrypted) data.
|
||||||
*/
|
*/
|
||||||
fn encrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] {
|
pub fn encrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] {
|
||||||
let c = Crypter(t);
|
let c = Crypter(t);
|
||||||
c.init(Encrypt, key, iv);
|
c.init(Encrypt, key, iv);
|
||||||
let r = c.update(data);
|
let r = c.update(data);
|
||||||
|
|
@ -182,7 +190,7 @@ fn encrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] {
|
||||||
* Decrypts data, using the specified crypter type in decrypt mode with the
|
* Decrypts data, using the specified crypter type in decrypt mode with the
|
||||||
* specified key and iv; returns the resulting (decrypted) data.
|
* specified key and iv; returns the resulting (decrypted) data.
|
||||||
*/
|
*/
|
||||||
fn decrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] {
|
pub fn decrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] {
|
||||||
let c = Crypter(t);
|
let c = Crypter(t);
|
||||||
c.init(Decrypt, key, iv);
|
c.init(Decrypt, key, iv);
|
||||||
let r = c.update(data);
|
let r = c.update(data);
|
||||||
|
|
@ -192,6 +200,8 @@ fn decrypt(t: Type, key: &[u8], iv: ~[u8], data: &[u8]) -> ~[u8] {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
use hex::FromHex;
|
use hex::FromHex;
|
||||||
|
|
||||||
// Test vectors from FIPS-197:
|
// Test vectors from FIPS-197:
|
||||||
|
|
@ -213,11 +223,11 @@ mod tests {
|
||||||
c.init(Encrypt, k0, ~[]);
|
c.init(Encrypt, k0, ~[]);
|
||||||
c.pad(false);
|
c.pad(false);
|
||||||
let r0 = c.update(p0) + c.final();
|
let r0 = c.update(p0) + c.final();
|
||||||
assert(r0 == c0);
|
assert!(r0 == c0);
|
||||||
c.init(Decrypt, k0, ~[]);
|
c.init(Decrypt, k0, ~[]);
|
||||||
c.pad(false);
|
c.pad(false);
|
||||||
let p1 = c.update(r0) + c.final();
|
let p1 = c.update(r0) + c.final();
|
||||||
assert(p1 == p0);
|
assert!(p1 == p0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cipher_test(ciphertype: Type, pt: ~str, ct: ~str, key: ~str, iv: ~str) {
|
fn cipher_test(ciphertype: Type, pt: ~str, ct: ~str, key: ~str, iv: ~str) {
|
||||||
|
|
@ -236,7 +246,7 @@ mod tests {
|
||||||
io::println(fmt!("Lengths differ: %u in computed vs %u expected",
|
io::println(fmt!("Lengths differ: %u in computed vs %u expected",
|
||||||
computed.len(), expected.len()));
|
computed.len(), expected.len()));
|
||||||
}
|
}
|
||||||
fail ~"test failure";
|
fail!(~"test failure");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -251,7 +261,7 @@ mod tests {
|
||||||
cipher_test(RC4_128, pt, ct, key, iv);
|
cipher_test(RC4_128, pt, ct, key, iv);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
/*#[test]
|
||||||
fn test_aes128_ctr() {
|
fn test_aes128_ctr() {
|
||||||
|
|
||||||
let pt = ~"6BC1BEE22E409F96E93D7E117393172AAE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411E5FBC1191A0A52EFF69F2445DF4F9B17AD2B417BE66C3710";
|
let pt = ~"6BC1BEE22E409F96E93D7E117393172AAE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411E5FBC1191A0A52EFF69F2445DF4F9B17AD2B417BE66C3710";
|
||||||
|
|
@ -260,9 +270,9 @@ mod tests {
|
||||||
let iv = ~"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF";
|
let iv = ~"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF";
|
||||||
|
|
||||||
cipher_test(AES_128_CTR, pt, ct, key, iv);
|
cipher_test(AES_128_CTR, pt, ct, key, iv);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
#[test]
|
/*#[test]
|
||||||
fn test_aes128_gcm() {
|
fn test_aes128_gcm() {
|
||||||
// Test case 3 in GCM spec
|
// Test case 3 in GCM spec
|
||||||
let pt = ~"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255";
|
let pt = ~"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255";
|
||||||
|
|
@ -271,6 +281,6 @@ mod tests {
|
||||||
let iv = ~"cafebabefacedbaddecaf888";
|
let iv = ~"cafebabefacedbaddecaf888";
|
||||||
|
|
||||||
cipher_test(AES_128_GCM, pt, ct, key, iv);
|
cipher_test(AES_128_GCM, pt, ct, key, iv);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue