aboutsummaryrefslogtreecommitdiff
path: root/src/crypto
diff options
context:
space:
mode:
authorValerii Hiora <[email protected]>2014-09-28 08:15:51 +0300
committerValerii Hiora <[email protected]>2014-09-30 08:21:31 +0300
commit02637ec7d451c38792c42c5c2cb4d59505e13ced (patch)
tree12a842f098c7cdbea5190097647666d34eabc093 /src/crypto
parentMerge pull request #53 from vhbit/cert-gen (diff)
downloadrust-openssl-02637ec7d451c38792c42c5c2cb4d59505e13ced.tar.xz
rust-openssl-02637ec7d451c38792c42c5c2cb4d59505e13ced.zip
single `ffi` module
Diffstat (limited to 'src/crypto')
-rw-r--r--src/crypto/hash.rs71
-rw-r--r--src/crypto/hmac.rs46
-rw-r--r--src/crypto/pkcs5.rs11
-rw-r--r--src/crypto/pkey.rs107
-rw-r--r--src/crypto/rand.rs8
-rw-r--r--src/crypto/symm.rs62
6 files changed, 83 insertions, 222 deletions
diff --git a/src/crypto/hash.rs b/src/crypto/hash.rs
index 4a4031e2..03d0f097 100644
--- a/src/crypto/hash.rs
+++ b/src/crypto/hash.rs
@@ -1,7 +1,8 @@
-use libc;
use libc::c_uint;
use std::ptr;
+use ffi;
+
pub enum HashType {
MD5,
SHA1,
@@ -12,71 +13,33 @@ pub enum HashType {
RIPEMD160
}
-#[allow(dead_code)]
-#[allow(non_camel_case_types)]
-#[repr(C)]
-pub struct EVP_MD_CTX {
- digest: *mut EVP_MD,
- engine: *mut libc::c_void,
- flags: libc::c_ulong,
- md_data: *mut libc::c_void,
- pctx: *mut EVP_PKEY_CTX,
- update: *mut libc::c_void
-}
-
-#[allow(non_camel_case_types)]
-#[repr(C)]
-pub struct EVP_MD;
-
-#[allow(non_camel_case_types)]
-#[repr(C)]
-pub struct EVP_PKEY_CTX;
-
-#[link(name = "crypto")]
-extern {
- fn EVP_MD_CTX_create() -> *mut EVP_MD_CTX;
- fn EVP_MD_CTX_destroy(ctx: *mut EVP_MD_CTX);
-
- fn EVP_md5() -> *const EVP_MD;
- fn EVP_sha1() -> *const EVP_MD;
- fn EVP_sha224() -> *const EVP_MD;
- fn EVP_sha256() -> *const EVP_MD;
- fn EVP_sha384() -> *const EVP_MD;
- fn EVP_sha512() -> *const EVP_MD;
- fn EVP_ripemd160() -> *const EVP_MD;
-
- fn EVP_DigestInit(ctx: *mut EVP_MD_CTX, typ: *const EVP_MD);
- fn EVP_DigestUpdate(ctx: *mut EVP_MD_CTX, data: *const u8, n: c_uint);
- fn EVP_DigestFinal(ctx: *mut EVP_MD_CTX, res: *mut u8, n: *mut u32);
-}
-
-pub fn evpmd(t: HashType) -> (*const EVP_MD, uint) {
+pub fn evpmd(t: HashType) -> (*const ffi::EVP_MD, uint) {
unsafe {
match t {
- MD5 => (EVP_md5(), 16u),
- SHA1 => (EVP_sha1(), 20u),
- SHA224 => (EVP_sha224(), 28u),
- SHA256 => (EVP_sha256(), 32u),
- SHA384 => (EVP_sha384(), 48u),
- SHA512 => (EVP_sha512(), 64u),
- RIPEMD160 => (EVP_ripemd160(), 20u),
+ MD5 => (ffi::EVP_md5(), 16u),
+ SHA1 => (ffi::EVP_sha1(), 20u),
+ SHA224 => (ffi::EVP_sha224(), 28u),
+ SHA256 => (ffi::EVP_sha256(), 32u),
+ SHA384 => (ffi::EVP_sha384(), 48u),
+ SHA512 => (ffi::EVP_sha512(), 64u),
+ RIPEMD160 => (ffi::EVP_ripemd160(), 20u),
}
}
}
#[allow(dead_code)]
pub struct Hasher {
- evp: *const EVP_MD,
- ctx: *mut EVP_MD_CTX,
+ evp: *const ffi::EVP_MD,
+ ctx: *mut ffi::EVP_MD_CTX,
len: uint,
}
impl Hasher {
pub fn new(ht: HashType) -> Hasher {
- let ctx = unsafe { EVP_MD_CTX_create() };
+ let ctx = unsafe { ffi::EVP_MD_CTX_create() };
let (evp, mdlen) = evpmd(ht);
unsafe {
- EVP_DigestInit(ctx, evp);
+ ffi::EVP_DigestInit(ctx, evp);
}
Hasher { evp: evp, ctx: ctx, len: mdlen }
@@ -85,7 +48,7 @@ impl Hasher {
/// Update this hasher with more input bytes
pub fn update(&self, data: &[u8]) {
unsafe {
- EVP_DigestUpdate(self.ctx, data.as_ptr(), data.len() as c_uint)
+ ffi::EVP_DigestUpdate(self.ctx, data.as_ptr(), data.len() as c_uint)
}
}
@@ -96,7 +59,7 @@ impl Hasher {
pub fn final(&self) -> Vec<u8> {
unsafe {
let mut res = Vec::from_elem(self.len, 0u8);
- EVP_DigestFinal(self.ctx, res.as_mut_ptr(), ptr::null_mut());
+ ffi::EVP_DigestFinal(self.ctx, res.as_mut_ptr(), ptr::null_mut());
res
}
}
@@ -105,7 +68,7 @@ impl Hasher {
impl Drop for Hasher {
fn drop(&mut self) {
unsafe {
- EVP_MD_CTX_destroy(self.ctx);
+ ffi::EVP_MD_CTX_destroy(self.ctx);
}
}
}
diff --git a/src/crypto/hmac.rs b/src/crypto/hmac.rs
index 5bf62586..0d945200 100644
--- a/src/crypto/hmac.rs
+++ b/src/crypto/hmac.rs
@@ -14,35 +14,13 @@
* limitations under the License.
*/
-use libc::{c_uchar, c_int, c_uint};
-use crypto::hash;
-
-#[allow(dead_code)]
-#[allow(non_camel_case_types)]
-#[repr(C)]
-pub struct HMAC_CTX {
- md: *mut hash::EVP_MD,
- md_ctx: hash::EVP_MD_CTX,
- i_ctx: hash::EVP_MD_CTX,
- o_ctx: hash::EVP_MD_CTX,
- key_length: c_uint,
- key: [c_uchar, ..128]
-}
+use libc::{c_int, c_uint};
-#[link(name = "crypto")]
-extern {
- fn HMAC_CTX_init(ctx: *mut HMAC_CTX);
- fn HMAC_Init_ex(ctx: *mut HMAC_CTX, key: *const u8, keylen: c_int, md: *const hash::EVP_MD, imple: *const ENGINE);
- fn HMAC_Update(ctx: *mut HMAC_CTX, input: *const u8, len: c_uint);
- fn HMAC_Final(ctx: *mut HMAC_CTX, output: *mut u8, len: *mut c_uint);
-}
-
-#[allow(non_camel_case_types)]
-#[repr(C)]
-struct ENGINE;
+use crypto::hash;
+use ffi;
pub struct HMAC {
- ctx: HMAC_CTX,
+ ctx: ffi::HMAC_CTX,
len: uint,
}
@@ -51,13 +29,13 @@ pub fn HMAC(ht: hash::HashType, key: &[u8]) -> HMAC {
unsafe {
let (evp, mdlen) = hash::evpmd(ht);
- let mut ctx : HMAC_CTX = ::std::mem::uninitialized();
+ let mut ctx : ffi::HMAC_CTX = ::std::mem::uninitialized();
- HMAC_CTX_init(&mut ctx);
- HMAC_Init_ex(&mut ctx,
- key.as_ptr(),
- key.len() as c_int,
- evp, 0 as *const _);
+ ffi::HMAC_CTX_init(&mut ctx);
+ ffi::HMAC_Init_ex(&mut ctx,
+ key.as_ptr(),
+ key.len() as c_int,
+ evp, 0 as *const _);
HMAC { ctx: ctx, len: mdlen }
}
@@ -66,7 +44,7 @@ pub fn HMAC(ht: hash::HashType, key: &[u8]) -> HMAC {
impl HMAC {
pub fn update(&mut self, data: &[u8]) {
unsafe {
- HMAC_Update(&mut self.ctx, data.as_ptr(), data.len() as c_uint)
+ ffi::HMAC_Update(&mut self.ctx, data.as_ptr(), data.len() as c_uint)
}
}
@@ -74,7 +52,7 @@ impl HMAC {
unsafe {
let mut res = Vec::from_elem(self.len, 0u8);
let mut outlen = 0;
- HMAC_Final(&mut self.ctx, res.as_mut_ptr(), &mut outlen);
+ ffi::HMAC_Final(&mut self.ctx, res.as_mut_ptr(), &mut outlen);
assert!(self.len == outlen as uint)
res
}
diff --git a/src/crypto/pkcs5.rs b/src/crypto/pkcs5.rs
index b795d84a..ec6e0cef 100644
--- a/src/crypto/pkcs5.rs
+++ b/src/crypto/pkcs5.rs
@@ -1,12 +1,5 @@
use libc::c_int;
-
-#[link(name = "crypto")]
-extern {
- fn PKCS5_PBKDF2_HMAC_SHA1(pass: *const u8, passlen: c_int,
- salt: *const u8, saltlen: c_int,
- iter: c_int, keylen: c_int,
- out: *mut u8) -> c_int;
-}
+use ffi;
/// 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, keylen: uint) -> Vec<u8> {
@@ -16,7 +9,7 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint, keylen: uint) -> Ve
let mut out = Vec::with_capacity(keylen);
- let r = PKCS5_PBKDF2_HMAC_SHA1(
+ let r = ffi::PKCS5_PBKDF2_HMAC_SHA1(
pass.as_ptr(), pass.len() as c_int,
salt.as_ptr(), salt.len() as c_int,
iter as c_int, keylen as c_int,
diff --git a/src/crypto/pkey.rs b/src/crypto/pkey.rs
index 5c4b108f..d95e7738 100644
--- a/src/crypto/pkey.rs
+++ b/src/crypto/pkey.rs
@@ -1,50 +1,11 @@
-use libc::{c_char, c_int, c_uint, c_void};
-use libc;
+use libc::{c_int, c_uint};
use std::mem;
use std::ptr;
-use bio::{mod, MemBio};
+use bio::{MemBio};
use crypto::hash::{HashType, MD5, SHA1, SHA224, SHA256, SHA384, SHA512, RIPEMD160};
-use crypto::symm::{EVP_CIPHER};
+use ffi;
use ssl::error::{SslError, StreamError};
-#[allow(non_camel_case_types)]
-pub type EVP_PKEY = *mut libc::c_void;
-
-#[allow(non_camel_case_types)]
-pub type RSA = *mut libc::c_void;
-
-pub type PrivateKeyWriteCallback = extern "C" fn(buf: *mut c_char, size: c_int, rwflag: c_int, user_data: *mut c_void) -> c_int;
-
-#[link(name = "crypto")]
-extern {
- fn EVP_PKEY_new() -> *mut EVP_PKEY;
- fn EVP_PKEY_free(k: *mut EVP_PKEY);
- fn EVP_PKEY_assign(pkey: *mut EVP_PKEY, typ: c_int, key: *const c_char) -> c_int;
- fn EVP_PKEY_get1_RSA(k: *mut EVP_PKEY) -> *mut RSA;
- fn EVP_PKEY_set1_RSA(k: *mut EVP_PKEY, r: *mut RSA) -> c_int;
-
- fn i2d_RSA_PUBKEY(k: *mut RSA, buf: *const *mut u8) -> c_int;
- fn d2i_RSA_PUBKEY(k: *const *mut RSA, buf: *const *const u8, len: c_uint) -> *mut RSA;
- fn i2d_RSAPrivateKey(k: *mut RSA, buf: *const *mut u8) -> c_int;
- fn d2i_RSAPrivateKey(k: *const *mut RSA, buf: *const *const u8, len: c_uint) -> *mut RSA;
-
- fn RSA_generate_key(modsz: c_uint, e: c_uint, cb: *const u8, cbarg: *const u8) -> *mut RSA;
- fn RSA_size(k: *mut RSA) -> c_uint;
-
- fn RSA_public_encrypt(flen: c_uint, from: *const u8, to: *mut u8, k: *mut RSA,
- pad: c_int) -> c_int;
- fn RSA_private_decrypt(flen: c_uint, from: *const u8, to: *mut u8, k: *mut RSA,
- pad: c_int) -> c_int;
- fn RSA_sign(t: c_int, m: *const u8, mlen: c_uint, sig: *mut u8, siglen: *mut c_uint,
- k: *mut RSA) -> c_int;
- fn RSA_verify(t: c_int, m: *const u8, mlen: c_uint, sig: *const u8, siglen: c_uint,
- k: *mut RSA) -> c_int;
-
- fn PEM_write_bio_PrivateKey(bio: *mut bio::ffi::BIO, pkey: *mut EVP_PKEY, cipher: *const EVP_CIPHER,
- kstr: *mut c_char, klen: c_int,
- callback: *mut c_void,
- user_data: *mut c_void) -> c_int;
-}
enum Parts {
Neither,
@@ -86,7 +47,7 @@ fn openssl_hash_nid(hash: HashType) -> c_int {
}
pub struct PKey {
- evp: *mut EVP_PKEY,
+ evp: *mut ffi::EVP_PKEY,
parts: Parts,
}
@@ -95,15 +56,15 @@ impl PKey {
pub fn new() -> PKey {
unsafe {
PKey {
- evp: EVP_PKEY_new(),
+ evp: ffi::EVP_PKEY_new(),
parts: Neither,
}
}
}
- fn _tostr(&self, f: unsafe extern "C" fn(*mut RSA, *const *mut u8) -> c_int) -> Vec<u8> {
+ fn _tostr(&self, f: unsafe extern "C" fn(*mut ffi::RSA, *const *mut u8) -> c_int) -> Vec<u8> {
unsafe {
- let rsa = EVP_PKEY_get1_RSA(self.evp);
+ let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
let len = f(rsa, ptr::null());
if len < 0 as c_int { return vec!(); }
let mut s = Vec::from_elem(len as uint, 0u8);
@@ -115,17 +76,17 @@ impl PKey {
}
}
- fn _fromstr(&mut self, s: &[u8], f: unsafe extern "C" fn(*const *mut RSA, *const *const u8, c_uint) -> *mut RSA) {
+ fn _fromstr(&mut self, s: &[u8], f: unsafe extern "C" fn(*const *mut ffi::RSA, *const *const u8, c_uint) -> *mut ffi::RSA) {
unsafe {
let rsa = ptr::null_mut();
f(&rsa, &s.as_ptr(), s.len() as c_uint);
- EVP_PKEY_set1_RSA(self.evp, rsa);
+ ffi::EVP_PKEY_set1_RSA(self.evp, rsa);
}
}
pub fn gen(&mut self, keysz: uint) {
unsafe {
- let rsa = RSA_generate_key(
+ let rsa = ffi::RSA_generate_key(
keysz as c_uint,
65537u as c_uint,
ptr::null(),
@@ -133,7 +94,7 @@ impl PKey {
);
// XXX: 6 == NID_rsaEncryption
- EVP_PKEY_assign(
+ ffi::EVP_PKEY_assign(
self.evp,
6 as c_int,
mem::transmute(rsa));
@@ -146,14 +107,14 @@ impl PKey {
* Returns a serialized form of the public key, suitable for load_pub().
*/
pub fn save_pub(&self) -> Vec<u8> {
- self._tostr(i2d_RSA_PUBKEY)
+ self._tostr(ffi::i2d_RSA_PUBKEY)
}
/**
* Loads a serialized form of the public key, as produced by save_pub().
*/
pub fn load_pub(&mut self, s: &[u8]) {
- self._fromstr(s, d2i_RSA_PUBKEY);
+ self._fromstr(s, ffi::d2i_RSA_PUBKEY);
self.parts = Public;
}
@@ -162,14 +123,14 @@ impl PKey {
* load_priv().
*/
pub fn save_priv(&self) -> Vec<u8> {
- self._tostr(i2d_RSAPrivateKey)
+ self._tostr(ffi::i2d_RSAPrivateKey)
}
/**
* Loads a serialized form of the public and private keys, as produced by
* save_priv().
*/
pub fn load_priv(&mut self, s: &[u8]) {
- self._fromstr(s, d2i_RSAPrivateKey);
+ self._fromstr(s, ffi::d2i_RSAPrivateKey);
self.parts = Both;
}
@@ -178,8 +139,8 @@ impl PKey {
pub fn write_pem(&self, writer: &mut Writer/*, password: Option<String>*/) -> Result<(), SslError> {
let mut mem_bio = try!(MemBio::new());
unsafe {
- try_ssl!(PEM_write_bio_PrivateKey(mem_bio.get_handle(), self.evp, ptr::null(),
- ptr::null_mut(), -1, ptr::null_mut(), ptr::null_mut()));
+ try_ssl!(ffi::PEM_write_bio_PrivateKey(mem_bio.get_handle(), self.evp, ptr::null(),
+ ptr::null_mut(), -1, ptr::null_mut(), ptr::null_mut()));
}
let buf = try!(mem_bio.read_to_end().map_err(StreamError));
@@ -191,7 +152,7 @@ impl PKey {
*/
pub fn size(&self) -> uint {
unsafe {
- RSA_size(EVP_PKEY_get1_RSA(self.evp)) as uint
+ ffi::RSA_size(ffi::EVP_PKEY_get1_RSA(self.evp)) as uint
}
}
@@ -229,8 +190,8 @@ impl PKey {
*/
pub fn max_data(&self) -> uint {
unsafe {
- let rsa = EVP_PKEY_get1_RSA(self.evp);
- let len = RSA_size(rsa);
+ let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
+ let len = ffi::RSA_size(rsa);
// 41 comes from RSA_public_encrypt(3) for OAEP
len as uint - 41u
@@ -239,14 +200,14 @@ impl PKey {
pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> {
unsafe {
- let rsa = EVP_PKEY_get1_RSA(self.evp);
- let len = RSA_size(rsa);
+ let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
+ let len = ffi::RSA_size(rsa);
assert!(s.len() < self.max_data());
let mut r = Vec::from_elem(len as uint + 1u, 0u8);
- let rv = RSA_public_encrypt(
+ let rv = ffi::RSA_public_encrypt(
s.len() as c_uint,
s.as_ptr(),
r.as_mut_ptr(),
@@ -264,14 +225,14 @@ impl PKey {
pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> Vec<u8> {
unsafe {
- let rsa = EVP_PKEY_get1_RSA(self.evp);
- let len = RSA_size(rsa);
+ let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
+ let len = ffi::RSA_size(rsa);
- assert_eq!(s.len() as c_uint, RSA_size(rsa));
+ assert_eq!(s.len() as c_uint, ffi::RSA_size(rsa));
let mut r = Vec::from_elem(len as uint + 1u, 0u8);
- let rv = RSA_private_decrypt(
+ let rv = ffi::RSA_private_decrypt(
s.len() as c_uint,
s.as_ptr(),
r.as_mut_ptr(),
@@ -312,11 +273,11 @@ impl PKey {
pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> Vec<u8> {
unsafe {
- let rsa = EVP_PKEY_get1_RSA(self.evp);
- let mut len = RSA_size(rsa);
+ let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
+ let mut len = ffi::RSA_size(rsa);
let mut r = Vec::from_elem(len as uint + 1u, 0u8);
- let rv = RSA_sign(
+ let rv = ffi::RSA_sign(
openssl_hash_nid(hash),
s.as_ptr(),
s.len() as c_uint,
@@ -335,9 +296,9 @@ impl PKey {
pub fn verify_with_hash(&self, m: &[u8], s: &[u8], hash: HashType) -> bool {
unsafe {
- let rsa = EVP_PKEY_get1_RSA(self.evp);
+ let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
- let rv = RSA_verify(
+ let rv = ffi::RSA_verify(
openssl_hash_nid(hash),
m.as_ptr(),
m.len() as c_uint,
@@ -350,7 +311,7 @@ impl PKey {
}
}
- pub unsafe fn get_handle(&self) -> *mut EVP_PKEY {
+ pub unsafe fn get_handle(&self) -> *mut ffi::EVP_PKEY {
return self.evp
}
}
@@ -358,7 +319,7 @@ impl PKey {
impl Drop for PKey {
fn drop(&mut self) {
unsafe {
- EVP_PKEY_free(self.evp);
+ ffi::EVP_PKEY_free(self.evp);
}
}
}
diff --git a/src/crypto/rand.rs b/src/crypto/rand.rs
index 9db87fcd..dffddee7 100644
--- a/src/crypto/rand.rs
+++ b/src/crypto/rand.rs
@@ -1,15 +1,11 @@
use libc::c_int;
-
-#[link(name = "crypto")]
-extern {
- fn RAND_bytes(buf: *mut u8, num: c_int) -> c_int;
-}
+use ffi;
pub fn rand_bytes(len: uint) -> Vec<u8> {
unsafe {
let mut out = Vec::with_capacity(len);
- let r = RAND_bytes(out.as_mut_ptr(), len as c_int);
+ let r = ffi::RAND_bytes(out.as_mut_ptr(), len as c_int);
if r != 1 as c_int { fail!() }
out.set_len(len);
diff --git a/src/crypto/symm.rs b/src/crypto/symm.rs
index 6bba7267..a3664bc0 100644
--- a/src/crypto/symm.rs
+++ b/src/crypto/symm.rs
@@ -1,36 +1,6 @@
-use libc::{c_int, c_uint};
-use libc;
+use libc::{c_int};
-#[allow(non_camel_case_types)]
-pub type EVP_CIPHER_CTX = *mut libc::c_void;
-
-#[allow(non_camel_case_types)]
-pub type EVP_CIPHER = *mut libc::c_void;
-
-#[link(name = "crypto")]
-extern {
- 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_free(ctx: EVP_CIPHER_CTX);
-
- fn EVP_aes_128_ecb() -> EVP_CIPHER;
- fn EVP_aes_128_cbc() -> EVP_CIPHER;
- // fn EVP_aes_128_ctr() -> EVP_CIPHER;
- // fn EVP_aes_128_gcm() -> EVP_CIPHER;
-
- fn EVP_aes_256_ecb() -> EVP_CIPHER;
- fn EVP_aes_256_cbc() -> EVP_CIPHER;
- // fn EVP_aes_256_ctr() -> EVP_CIPHER;
- // fn EVP_aes_256_gcm() -> EVP_CIPHER;
-
- fn EVP_rc4() -> EVP_CIPHER;
-
- fn EVP_CipherInit(ctx: EVP_CIPHER_CTX, evp: EVP_CIPHER,
- key: *const u8, iv: *const u8, mode: c_int);
- fn EVP_CipherUpdate(ctx: EVP_CIPHER_CTX, outbuf: *mut u8,
- outlen: &mut c_uint, inbuf: *const u8, inlen: c_int);
- fn EVP_CipherFinal(ctx: EVP_CIPHER_CTX, res: *mut u8, len: &mut c_int);
-}
+use ffi;
pub enum Mode {
Encrypt,
@@ -52,35 +22,35 @@ pub enum Type {
RC4_128,
}
-fn evpc(t: Type) -> (EVP_CIPHER, uint, uint) {
+fn evpc(t: Type) -> (ffi::EVP_CIPHER, uint, uint) {
unsafe {
match t {
- AES_128_ECB => (EVP_aes_128_ecb(), 16u, 16u),
- AES_128_CBC => (EVP_aes_128_cbc(), 16u, 16u),
+ AES_128_ECB => (ffi::EVP_aes_128_ecb(), 16u, 16u),
+ AES_128_CBC => (ffi::EVP_aes_128_cbc(), 16u, 16u),
// AES_128_CTR => (EVP_aes_128_ctr(), 16u, 0u),
//AES_128_GCM => (EVP_aes_128_gcm(), 16u, 16u),
- AES_256_ECB => (EVP_aes_256_ecb(), 32u, 16u),
- AES_256_CBC => (EVP_aes_256_cbc(), 32u, 16u),
+ AES_256_ECB => (ffi::EVP_aes_256_ecb(), 32u, 16u),
+ AES_256_CBC => (ffi::EVP_aes_256_cbc(), 32u, 16u),
// AES_256_CTR => (EVP_aes_256_ctr(), 32u, 0u),
//AES_256_GCM => (EVP_aes_256_gcm(), 32u, 16u),
- RC4_128 => (EVP_rc4(), 16u, 0u),
+ RC4_128 => (ffi::EVP_rc4(), 16u, 0u),
}
}
}
/// Represents a symmetric cipher context.
pub struct Crypter {
- evp: EVP_CIPHER,
- ctx: EVP_CIPHER_CTX,
+ evp: ffi::EVP_CIPHER,
+ ctx: ffi::EVP_CIPHER_CTX,
keylen: uint,
blocksize: uint
}
impl Crypter {
pub fn new(t: Type) -> Crypter {
- let ctx = unsafe { EVP_CIPHER_CTX_new() };
+ let ctx = unsafe { ffi::EVP_CIPHER_CTX_new() };
let (evp, keylen, blocksz) = evpc(t);
Crypter { evp: evp, ctx: ctx, keylen: keylen, blocksize: blocksz }
}
@@ -93,7 +63,7 @@ impl Crypter {
if self.blocksize > 0 {
unsafe {
let v = if padding { 1 as c_int } else { 0 };
- EVP_CIPHER_CTX_set_padding(self.ctx, v);
+ ffi::EVP_CIPHER_CTX_set_padding(self.ctx, v);
}
}
}
@@ -109,7 +79,7 @@ impl Crypter {
};
assert_eq!(key.len(), self.keylen);
- EVP_CipherInit(
+ ffi::EVP_CipherInit(
self.ctx,
self.evp,
key.as_ptr(),
@@ -128,7 +98,7 @@ impl Crypter {
let mut res = Vec::from_elem(data.len() + self.blocksize, 0u8);
let mut reslen = (data.len() + self.blocksize) as u32;
- EVP_CipherUpdate(
+ ffi::EVP_CipherUpdate(
self.ctx,
res.as_mut_ptr(),
&mut reslen,
@@ -149,7 +119,7 @@ impl Crypter {
let mut res = Vec::from_elem(self.blocksize, 0u8);
let mut reslen = self.blocksize as c_int;
- EVP_CipherFinal(self.ctx,
+ ffi::EVP_CipherFinal(self.ctx,
res.as_mut_ptr(),
&mut reslen);
@@ -162,7 +132,7 @@ impl Crypter {
impl Drop for Crypter {
fn drop(&mut self) {
unsafe {
- EVP_CIPHER_CTX_free(self.ctx);
+ ffi::EVP_CIPHER_CTX_free(self.ctx);
}
}
}