Update to latest incoming (878a9b9)

This commit is contained in:
Kevin Ballard 2013-06-08 17:22:54 -07:00
parent 2eba04e579
commit 1a88757ca2
5 changed files with 32 additions and 32 deletions

View File

@ -62,16 +62,16 @@ pub fn Hasher(ht: HashType) -> Hasher {
} }
} }
pub impl Hasher { impl Hasher {
/// Initializes this hasher /// Initializes this hasher
fn init(&self) { pub fn init(&self) {
unsafe { unsafe {
EVP_DigestInit(self.ctx, self.evp); EVP_DigestInit(self.ctx, self.evp);
} }
} }
/// Update this hasher with more input bytes /// Update this hasher with more input bytes
fn update(&self, data: &[u8]) { pub fn update(&self, data: &[u8]) {
unsafe { unsafe {
do vec::as_imm_buf(data) |pdata, len| { do vec::as_imm_buf(data) |pdata, len| {
EVP_DigestUpdate(self.ctx, pdata, len as c_uint) EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
@ -83,7 +83,7 @@ pub impl Hasher {
* 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(&self) -> ~[u8] { pub fn final(&self) -> ~[u8] {
unsafe { 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| {

4
hex.rs
View File

@ -15,6 +15,7 @@
*/ */
use std::{str,uint,vec}; use std::{str,uint,vec};
use std::iterator::*;
pub trait ToHex { pub trait ToHex {
fn to_hex(&self) -> ~str; fn to_hex(&self) -> ~str;
@ -50,8 +51,7 @@ impl<'self> FromHex for &'self str {
fn from_hex(&self) -> ~[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 self.iter().enumerate().advance() |(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) }

View File

@ -65,8 +65,8 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC {
} }
} }
pub impl HMAC { impl HMAC {
fn update(&mut self, data: &[u8]) { pub fn update(&mut self, data: &[u8]) {
unsafe { unsafe {
do vec::as_imm_buf(data) |pdata, len| { do vec::as_imm_buf(data) |pdata, len| {
HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint) HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
@ -74,7 +74,7 @@ pub impl HMAC {
} }
} }
fn final(&mut self) -> ~[u8] { pub fn final(&mut self) -> ~[u8] {
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;

36
pkey.rs
View File

@ -99,7 +99,7 @@ pub fn PKey() -> PKey {
} }
///Represents a public key, optionally with a private key attached. ///Represents a public key, optionally with a private key attached.
priv impl PKey { impl PKey {
priv unsafe fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, &*mut u8) -> c_int) -> ~[u8] { priv unsafe fn _tostr(&self, f: extern "C" unsafe fn(*EVP_PKEY, &*mut u8) -> c_int) -> ~[u8] {
let buf = ptr::mut_null(); let buf = ptr::mut_null();
let len = f(self.evp, &buf); let len = f(self.evp, &buf);
@ -126,8 +126,8 @@ priv impl PKey {
} }
} }
pub impl PKey { impl PKey {
fn gen(&mut self, keysz: uint) { pub fn gen(&mut self, keysz: uint) {
unsafe { unsafe {
let rsa = RSA_generate_key( let rsa = RSA_generate_key(
keysz as c_uint, keysz as c_uint,
@ -146,7 +146,7 @@ pub impl PKey {
/** /**
* 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(&self) -> ~[u8] { pub fn save_pub(&self) -> ~[u8] {
unsafe { unsafe {
self._tostr(i2d_PublicKey) self._tostr(i2d_PublicKey)
} }
@ -155,7 +155,7 @@ pub impl PKey {
/** /**
* 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(&mut self, s: &[u8]) { pub fn load_pub(&mut self, s: &[u8]) {
unsafe { unsafe {
self._fromstr(s, d2i_PublicKey); self._fromstr(s, d2i_PublicKey);
self.parts = Public; self.parts = Public;
@ -166,7 +166,7 @@ pub impl PKey {
* 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(&self, ) -> ~[u8] { pub fn save_priv(&self, ) -> ~[u8] {
unsafe { unsafe {
self._tostr(i2d_PrivateKey) self._tostr(i2d_PrivateKey)
} }
@ -175,7 +175,7 @@ pub impl PKey {
* 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(&mut self, s: &[u8]) { pub fn load_priv(&mut self, s: &[u8]) {
unsafe { unsafe {
self._fromstr(s, d2i_PrivateKey); self._fromstr(s, d2i_PrivateKey);
self.parts = Both; self.parts = Both;
@ -185,7 +185,7 @@ pub impl PKey {
/** /**
* Returns the size of the public key modulus. * Returns the size of the public key modulus.
*/ */
fn size(&self) -> uint { pub fn size(&self) -> uint {
unsafe { unsafe {
RSA_size(EVP_PKEY_get1_RSA(self.evp)) as uint RSA_size(EVP_PKEY_get1_RSA(self.evp)) as uint
} }
@ -194,7 +194,7 @@ pub impl PKey {
/** /**
* Returns whether this pkey object can perform the specified role. * Returns whether this pkey object can perform the specified role.
*/ */
fn can(&self, r: Role) -> bool { pub fn can(&self, r: Role) -> bool {
match r { match r {
Encrypt => Encrypt =>
match self.parts { match self.parts {
@ -223,7 +223,7 @@ 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(&self) -> uint { pub fn max_data(&self) -> uint {
unsafe { unsafe {
let rsa = EVP_PKEY_get1_RSA(self.evp); let rsa = EVP_PKEY_get1_RSA(self.evp);
let len = RSA_size(rsa); let len = RSA_size(rsa);
@ -233,7 +233,7 @@ pub impl PKey {
} }
} }
fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { pub fn encrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] {
unsafe { unsafe {
let rsa = EVP_PKEY_get1_RSA(self.evp); let rsa = EVP_PKEY_get1_RSA(self.evp);
let len = RSA_size(rsa); let len = RSA_size(rsa);
@ -261,7 +261,7 @@ pub impl PKey {
} }
} }
fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] { pub fn decrypt_with_padding(&self, s: &[u8], padding: EncryptionPadding) -> ~[u8] {
unsafe { unsafe {
let rsa = EVP_PKEY_get1_RSA(self.evp); let rsa = EVP_PKEY_get1_RSA(self.evp);
let len = RSA_size(rsa); let len = RSA_size(rsa);
@ -294,26 +294,26 @@ 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(&self, s: &[u8]) -> ~[u8] { self.encrypt_with_padding(s, OAEP) } pub fn encrypt(&self, s: &[u8]) -> ~[u8] { 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(&self, s: &[u8]) -> ~[u8] { self.decrypt_with_padding(s, OAEP) } pub fn decrypt(&self, s: &[u8]) -> ~[u8] { 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(&self, s: &[u8]) -> ~[u8] { self.sign_with_hash(s, SHA256) } pub fn sign(&self, s: &[u8]) -> ~[u8] { 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(&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) }
fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[u8] { pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[u8] {
unsafe { unsafe {
let rsa = EVP_PKEY_get1_RSA(self.evp); let rsa = EVP_PKEY_get1_RSA(self.evp);
let len = RSA_size(rsa); let len = RSA_size(rsa);
@ -339,7 +339,7 @@ pub impl PKey {
} }
} }
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 {
unsafe { unsafe {
let rsa = EVP_PKEY_get1_RSA(self.evp); let rsa = EVP_PKEY_get1_RSA(self.evp);

10
symm.rs
View File

@ -86,12 +86,12 @@ pub fn Crypter(t: Type) -> Crypter {
} }
} }
pub impl Crypter { 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(&self, padding: bool) { pub fn pad(&self, padding: bool) {
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;
@ -103,7 +103,7 @@ pub impl Crypter {
/** /**
* Initializes this crypter. * Initializes this crypter.
*/ */
fn init(&self, mode: Mode, key: &[u8], iv: &[u8]) { pub fn init(&self, mode: Mode, key: &[u8], iv: &[u8]) {
unsafe { unsafe {
let mode = match mode { let mode = match mode {
Encrypt => 1 as c_int, Encrypt => 1 as c_int,
@ -129,7 +129,7 @@ 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(&self, data: &[u8]) -> ~[u8] { pub fn update(&self, data: &[u8]) -> ~[u8] {
unsafe { 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);
@ -156,7 +156,7 @@ pub impl Crypter {
/** /**
* Finish crypting. Returns the remaining partial block of output, if any. * Finish crypting. Returns the remaining partial block of output, if any.
*/ */
fn final(&self) -> ~[u8] { pub fn final(&self) -> ~[u8] {
unsafe { unsafe {
let mut res = vec::from_elem(self.blocksize, 0u8); let mut res = vec::from_elem(self.blocksize, 0u8);