Update for latest master (0.9-pre b42c438)
This commit is contained in:
parent
25e18fab13
commit
9ea9c195e2
|
|
@ -16,6 +16,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#[link(name = "crypto",
|
#[link(name = "crypto",
|
||||||
|
package_id = "crypto",
|
||||||
vers = "0.3",
|
vers = "0.3",
|
||||||
uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")];
|
uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")];
|
||||||
#[crate_type = "lib"];
|
#[crate_type = "lib"];
|
||||||
|
|
|
||||||
5
hash.rs
5
hash.rs
|
|
@ -41,7 +41,6 @@ mod libcrypto {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn evpmd(t: HashType) -> (EVP_MD, uint) {
|
pub fn evpmd(t: HashType) -> (EVP_MD, uint) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
match t {
|
match t {
|
||||||
MD5 => (libcrypto::EVP_md5(), 16u),
|
MD5 => (libcrypto::EVP_md5(), 16u),
|
||||||
|
|
@ -62,7 +61,6 @@ pub struct Hasher {
|
||||||
|
|
||||||
impl Hasher {
|
impl Hasher {
|
||||||
pub fn new(ht: HashType) -> Hasher {
|
pub fn new(ht: HashType) -> Hasher {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
let ctx = unsafe { libcrypto::EVP_MD_CTX_create() };
|
let ctx = unsafe { libcrypto::EVP_MD_CTX_create() };
|
||||||
let (evp, mdlen) = evpmd(ht);
|
let (evp, mdlen) = evpmd(ht);
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -74,7 +72,6 @@ impl Hasher {
|
||||||
|
|
||||||
/// Update this hasher with more input bytes
|
/// Update this hasher with more input bytes
|
||||||
pub fn update(&self, data: &[u8]) {
|
pub fn update(&self, data: &[u8]) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
do data.as_imm_buf |pdata, len| {
|
do data.as_imm_buf |pdata, len| {
|
||||||
unsafe {
|
unsafe {
|
||||||
libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
|
libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
|
||||||
|
|
@ -87,7 +84,6 @@ impl Hasher {
|
||||||
* initialization
|
* initialization
|
||||||
*/
|
*/
|
||||||
pub fn final(&self) -> ~[u8] {
|
pub fn final(&self) -> ~[u8] {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
let mut res = vec::from_elem(self.len, 0u8);
|
let mut res = vec::from_elem(self.len, 0u8);
|
||||||
do res.as_mut_buf |pres, _len| {
|
do res.as_mut_buf |pres, _len| {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -100,7 +96,6 @@ impl Hasher {
|
||||||
|
|
||||||
impl Drop for Hasher {
|
impl Drop for Hasher {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
libcrypto::EVP_MD_CTX_destroy(self.ctx);
|
libcrypto::EVP_MD_CTX_destroy(self.ctx);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
4
hmac.rs
4
hmac.rs
|
|
@ -28,7 +28,6 @@ pub struct HMAC_CTX {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[link_args = "-lcrypto"]
|
#[link_args = "-lcrypto"]
|
||||||
#[abi = "cdecl"]
|
|
||||||
extern {
|
extern {
|
||||||
fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: libc::c_int, md: EVP_MD);
|
fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: libc::c_int, md: EVP_MD);
|
||||||
|
|
||||||
|
|
@ -43,7 +42,6 @@ pub struct HMAC {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC {
|
pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
||||||
let (evp, mdlen) = evpmd(ht);
|
let (evp, mdlen) = evpmd(ht);
|
||||||
|
|
@ -68,7 +66,6 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC {
|
||||||
|
|
||||||
impl HMAC {
|
impl HMAC {
|
||||||
pub fn update(&mut self, data: &[u8]) {
|
pub fn update(&mut self, data: &[u8]) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
do data.as_imm_buf |pdata, len| {
|
do data.as_imm_buf |pdata, len| {
|
||||||
HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
|
HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
|
||||||
|
|
@ -77,7 +74,6 @@ impl HMAC {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn final(&mut self) -> ~[u8] {
|
pub fn final(&mut self) -> ~[u8] {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
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;
|
||||||
|
|
|
||||||
2
pkcs5.rs
2
pkcs5.rs
|
|
@ -14,8 +14,6 @@ mod libcrypto {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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.
|
||||||
#[fixed_stack_segment]
|
|
||||||
#[inline(never)]
|
|
||||||
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);
|
||||||
|
|
|
||||||
11
pkey.rs
11
pkey.rs
|
|
@ -87,7 +87,6 @@ pub struct PKey {
|
||||||
/// Represents a public key, optionally with a private key attached.
|
/// Represents a public key, optionally with a private key attached.
|
||||||
impl PKey {
|
impl PKey {
|
||||||
pub fn new() -> PKey {
|
pub fn new() -> PKey {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
PKey {
|
PKey {
|
||||||
evp: unsafe { libcrypto::EVP_PKEY_new() },
|
evp: unsafe { libcrypto::EVP_PKEY_new() },
|
||||||
parts: Neither,
|
parts: Neither,
|
||||||
|
|
@ -95,7 +94,6 @@ impl PKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, **mut u8) -> c_int) -> ~[u8] {
|
fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, **mut u8) -> c_int) -> ~[u8] {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let len = f(self.evp, ptr::null());
|
let len = f(self.evp, ptr::null());
|
||||||
if len < 0 as c_int { return ~[]; }
|
if len < 0 as c_int { return ~[]; }
|
||||||
|
|
@ -111,7 +109,6 @@ impl PKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _fromstr(&mut self, s: &[u8], f: extern "C" unsafe fn(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY) {
|
fn _fromstr(&mut self, s: &[u8], f: extern "C" unsafe fn(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
do s.as_imm_buf |ps, len| {
|
do s.as_imm_buf |ps, len| {
|
||||||
let evp = ptr::null();
|
let evp = ptr::null();
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -122,7 +119,6 @@ impl PKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gen(&mut self, keysz: uint) {
|
pub fn gen(&mut self, keysz: uint) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let rsa = libcrypto::RSA_generate_key(
|
let rsa = libcrypto::RSA_generate_key(
|
||||||
keysz as c_uint,
|
keysz as c_uint,
|
||||||
|
|
@ -176,7 +172,6 @@ impl PKey {
|
||||||
* Returns the size of the public key modulus.
|
* Returns the size of the public key modulus.
|
||||||
*/
|
*/
|
||||||
pub fn size(&self) -> uint {
|
pub fn size(&self) -> uint {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
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
|
||||||
}
|
}
|
||||||
|
|
@ -215,7 +210,6 @@ impl PKey {
|
||||||
* call.
|
* call.
|
||||||
*/
|
*/
|
||||||
pub fn max_data(&self) -> uint {
|
pub fn max_data(&self) -> uint {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
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);
|
||||||
|
|
@ -226,7 +220,6 @@ impl PKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] {
|
pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
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);
|
||||||
|
|
@ -256,7 +249,6 @@ impl PKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] {
|
pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
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);
|
||||||
|
|
@ -310,7 +302,6 @@ impl PKey {
|
||||||
pub fn verify(&self, m: &[u8], s: &[u8]) -> bool { self.verify_with_hash(m, s, SHA256) }
|
pub fn verify(&self, m: &[u8], s: &[u8]) -> bool { self.verify_with_hash(m, s, SHA256) }
|
||||||
|
|
||||||
pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[u8] {
|
pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[u8] {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
||||||
let mut len = libcrypto::RSA_size(rsa);
|
let mut len = libcrypto::RSA_size(rsa);
|
||||||
|
|
@ -338,7 +329,6 @@ impl PKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify_with_hash(&self, m: &[u8], s: &[u8], hash: HashType) -> bool {
|
pub fn verify_with_hash(&self, m: &[u8], s: &[u8], hash: HashType) -> bool {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
|
||||||
|
|
||||||
|
|
@ -362,7 +352,6 @@ impl PKey {
|
||||||
|
|
||||||
impl Drop for PKey {
|
impl Drop for PKey {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
libcrypto::EVP_PKEY_free(self.evp);
|
libcrypto::EVP_PKEY_free(self.evp);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
1
rand.rs
1
rand.rs
|
|
@ -11,7 +11,6 @@ mod libcrypto {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rand_bytes(len: uint) -> ~[u8] {
|
pub fn rand_bytes(len: uint) -> ~[u8] {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
let mut out = vec::with_capacity(len);
|
let mut out = vec::with_capacity(len);
|
||||||
|
|
||||||
do out.as_mut_buf |out_buf, len| {
|
do out.as_mut_buf |out_buf, len| {
|
||||||
|
|
|
||||||
7
symm.rs
7
symm.rs
|
|
@ -59,7 +59,6 @@ pub enum Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evpc(t: Type) -> (EVP_CIPHER, uint, uint) {
|
fn evpc(t: Type) -> (EVP_CIPHER, uint, uint) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
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),
|
||||||
|
|
@ -87,7 +86,6 @@ pub struct Crypter {
|
||||||
|
|
||||||
impl Crypter {
|
impl Crypter {
|
||||||
pub fn new(t: Type) -> Crypter {
|
pub fn new(t: Type) -> Crypter {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
let ctx = unsafe { libcrypto::EVP_CIPHER_CTX_new() };
|
let ctx = unsafe { 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 }
|
||||||
|
|
@ -98,7 +96,6 @@ impl Crypter {
|
||||||
* data encrypted must be a multiple of block size.
|
* data encrypted must be a multiple of block size.
|
||||||
*/
|
*/
|
||||||
pub fn pad(&self, padding: bool) {
|
pub fn pad(&self, padding: bool) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
if self.blocksize > 0 {
|
if self.blocksize > 0 {
|
||||||
unsafe {
|
unsafe {
|
||||||
let v = if padding { 1 } else { 0 } as c_int;
|
let v = if padding { 1 } else { 0 } as c_int;
|
||||||
|
|
@ -111,7 +108,6 @@ impl Crypter {
|
||||||
* Initializes this crypter.
|
* Initializes this crypter.
|
||||||
*/
|
*/
|
||||||
pub fn init(&self, mode: Mode, key: &[u8], iv: &[u8]) {
|
pub fn init(&self, mode: Mode, key: &[u8], iv: &[u8]) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let mode = match mode {
|
let mode = match mode {
|
||||||
Encrypt => 1 as c_int,
|
Encrypt => 1 as c_int,
|
||||||
|
|
@ -138,7 +134,6 @@ impl Crypter {
|
||||||
* encrypted or decrypted bytes.
|
* encrypted or decrypted bytes.
|
||||||
*/
|
*/
|
||||||
pub fn update(&self, data: &[u8]) -> ~[u8] {
|
pub fn update(&self, data: &[u8]) -> ~[u8] {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
do data.as_imm_buf |pdata, len| {
|
do data.as_imm_buf |pdata, len| {
|
||||||
let mut res = vec::from_elem(len + self.blocksize, 0u8);
|
let mut res = vec::from_elem(len + self.blocksize, 0u8);
|
||||||
|
|
@ -167,7 +162,6 @@ impl Crypter {
|
||||||
* Finish crypting. Returns the remaining partial block of output, if any.
|
* Finish crypting. Returns the remaining partial block of output, if any.
|
||||||
*/
|
*/
|
||||||
pub fn final(&self) -> ~[u8] {
|
pub fn final(&self) -> ~[u8] {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut res = vec::from_elem(self.blocksize, 0u8);
|
let mut res = vec::from_elem(self.blocksize, 0u8);
|
||||||
|
|
||||||
|
|
@ -185,7 +179,6 @@ impl Crypter {
|
||||||
|
|
||||||
impl Drop for Crypter {
|
impl Drop for Crypter {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
#[fixed_stack_segment]; #[inline(never)];
|
|
||||||
unsafe {
|
unsafe {
|
||||||
libcrypto::EVP_CIPHER_CTX_free(self.ctx);
|
libcrypto::EVP_CIPHER_CTX_free(self.ctx);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue