Modernize crypto with new rust style and rustdoc support
This commit is contained in:
parent
c978e357d4
commit
cd4e72dc98
35
hash.rs
35
hash.rs
|
|
@ -1,40 +1,23 @@
|
|||
use std;
|
||||
|
||||
import core::ptr;
|
||||
import core::str;
|
||||
import core::vec;
|
||||
|
||||
import libc::c_uint;
|
||||
|
||||
export hasher;
|
||||
export hashtype;
|
||||
export mk_hasher;
|
||||
export hash;
|
||||
export _native;
|
||||
|
||||
export md5, sha1, sha224, sha256, sha384, sha512;
|
||||
|
||||
iface hasher {
|
||||
/*
|
||||
Method: init
|
||||
|
||||
Initializes this hasher
|
||||
*/
|
||||
#[doc = "Initializes this hasher"]
|
||||
fn init();
|
||||
|
||||
/*
|
||||
Method: update
|
||||
|
||||
Update this hasher with more input bytes
|
||||
*/
|
||||
#[doc = "Update this hasher with more input bytes"]
|
||||
fn update([u8]);
|
||||
|
||||
/*
|
||||
Method: final
|
||||
|
||||
#[doc = "
|
||||
Return the digest of all bytes added to this hasher since its last
|
||||
initialization
|
||||
*/
|
||||
"]
|
||||
fn final() -> [u8];
|
||||
}
|
||||
|
||||
|
|
@ -78,7 +61,7 @@ fn evpmd(t: hashtype) -> (EVP_MD, uint) {
|
|||
}
|
||||
}
|
||||
|
||||
fn mk_hasher(ht: hashtype) -> hasher {
|
||||
fn hasher(ht: hashtype) -> hasher {
|
||||
type hasherstate = {
|
||||
evp: EVP_MD,
|
||||
ctx: EVP_MD_CTX,
|
||||
|
|
@ -111,13 +94,11 @@ fn mk_hasher(ht: hashtype) -> hasher {
|
|||
ret h;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: hash
|
||||
|
||||
#[doc = "
|
||||
Hashes the supplied input data using hash t, returning the resulting hash value
|
||||
*/
|
||||
"]
|
||||
fn hash(t: hashtype, data: [u8]) -> [u8] unsafe {
|
||||
let h = mk_hasher(t);
|
||||
let h = hasher(t);
|
||||
h.init();
|
||||
h.update(data);
|
||||
ret h.final();
|
||||
|
|
|
|||
109
pkey.rs
109
pkey.rs
|
|
@ -1,12 +1,7 @@
|
|||
import core::ptr;
|
||||
import core::str;
|
||||
import core::unsafe;
|
||||
import core::vec;
|
||||
|
||||
import libc::{c_int, c_uint};
|
||||
|
||||
export pkeyrole, encrypt, decrypt, sign, verify;
|
||||
export pkey, mk_pkey;
|
||||
export pkey;
|
||||
export _native;
|
||||
|
||||
type EVP_PKEY = *libc::c_void;
|
||||
|
|
@ -45,11 +40,7 @@ enum pkeyparts {
|
|||
both
|
||||
}
|
||||
|
||||
/*
|
||||
Tag: pkeyrole
|
||||
|
||||
Represents a role an asymmetric key might be appropriate for.
|
||||
*/
|
||||
#[doc = "Represents a role an asymmetric key might be appropriate for."]
|
||||
enum pkeyrole {
|
||||
encrypt,
|
||||
decrypt,
|
||||
|
|
@ -57,100 +48,68 @@ enum pkeyrole {
|
|||
verify
|
||||
}
|
||||
|
||||
/*
|
||||
Object: pkey
|
||||
|
||||
Represents a public key, optionally with a private key attached.
|
||||
*/
|
||||
#[doc = "Represents a public key, optionally with a private key attached."]
|
||||
iface pkey {
|
||||
/*
|
||||
Method: save_pub
|
||||
|
||||
#[doc = "
|
||||
Returns a serialized form of the public key, suitable for load_pub().
|
||||
*/
|
||||
"]
|
||||
fn save_pub() -> [u8];
|
||||
|
||||
/*
|
||||
Method: load_pub
|
||||
|
||||
#[doc = "
|
||||
Loads a serialized form of the public key, as produced by save_pub().
|
||||
*/
|
||||
"]
|
||||
fn load_pub(s: [u8]);
|
||||
|
||||
/*
|
||||
Method: save_priv
|
||||
|
||||
#[doc = "
|
||||
Returns a serialized form of the public and private keys, suitable for
|
||||
load_priv().
|
||||
*/
|
||||
"]
|
||||
fn save_priv() -> [u8];
|
||||
|
||||
/*
|
||||
Method: load_priv
|
||||
|
||||
#[doc = "
|
||||
Loads a serialized form of the public and private keys, as produced by
|
||||
save_priv().
|
||||
*/
|
||||
"]
|
||||
fn load_priv(s: [u8]);
|
||||
|
||||
/*
|
||||
Method: size()
|
||||
|
||||
Returns the size of the public key modulus.
|
||||
*/
|
||||
#[doc = "Returns the size of the public key modulus."]
|
||||
fn size() -> uint;
|
||||
|
||||
/*
|
||||
Method: gen()
|
||||
|
||||
Generates a public/private keypair of the specified size.
|
||||
*/
|
||||
#[doc = "Generates a public/private keypair of the specified size."]
|
||||
fn gen(keysz: uint);
|
||||
|
||||
/*
|
||||
Method: can()
|
||||
|
||||
#[doc = "
|
||||
Returns whether this pkey object can perform the specified role.
|
||||
*/
|
||||
"]
|
||||
fn can(role: pkeyrole) -> bool;
|
||||
|
||||
/*
|
||||
Method: max_data()
|
||||
|
||||
#[doc = "
|
||||
Returns the maximum amount of data that can be encrypted by an encrypt()
|
||||
call.
|
||||
*/
|
||||
"]
|
||||
fn max_data() -> uint;
|
||||
|
||||
/*
|
||||
Method: encrypt()
|
||||
|
||||
#[doc = "
|
||||
Encrypts data using OAEP padding, returning the encrypted data. The supplied
|
||||
data must not be larger than max_data().
|
||||
*/
|
||||
"]
|
||||
fn encrypt(s: [u8]) -> [u8];
|
||||
|
||||
/*
|
||||
Method: decrypt()
|
||||
|
||||
#[doc = "
|
||||
Decrypts data, expecting OAEP padding, returning the decrypted data.
|
||||
*/
|
||||
"]
|
||||
fn decrypt(s: [u8]) -> [u8];
|
||||
|
||||
/*
|
||||
Method: sign()
|
||||
|
||||
#[doc = "
|
||||
Signs data, using OpenSSL's default scheme and sha256. Unlike encrypt(), can
|
||||
process an arbitrary amount of data; returns the signature.
|
||||
*/
|
||||
"]
|
||||
fn sign(s: [u8]) -> [u8];
|
||||
|
||||
/*
|
||||
Method: verify()
|
||||
|
||||
#[doc = "
|
||||
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.
|
||||
*/
|
||||
"]
|
||||
fn verify(m: [u8], s: [u8]) -> bool;
|
||||
}
|
||||
|
||||
|
|
@ -162,7 +121,7 @@ fn any_to_rsa(anykey: *ANYKEY) -> *RSA unsafe {
|
|||
unsafe::reinterpret_cast::<*ANYKEY, *RSA>(anykey)
|
||||
}
|
||||
|
||||
fn mk_pkey() -> pkey {
|
||||
fn pkey() -> pkey {
|
||||
type pkeystate = {
|
||||
mut evp: *EVP_PKEY,
|
||||
mut parts: pkeyparts
|
||||
|
|
@ -302,8 +261,8 @@ fn mk_pkey() -> pkey {
|
|||
mod tests {
|
||||
#[test]
|
||||
fn test_gen_pub() {
|
||||
let k0 = mk_pkey();
|
||||
let k1 = mk_pkey();
|
||||
let k0 = pkey();
|
||||
let k1 = pkey();
|
||||
k0.gen(512u);
|
||||
k1.load_pub(k0.save_pub());
|
||||
assert(k0.save_pub() == k1.save_pub());
|
||||
|
|
@ -320,8 +279,8 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_gen_priv() {
|
||||
let k0 = mk_pkey();
|
||||
let k1 = mk_pkey();
|
||||
let k0 = pkey();
|
||||
let k1 = pkey();
|
||||
k0.gen(512u);
|
||||
k1.load_priv(k0.save_priv());
|
||||
assert(k0.save_priv() == k1.save_priv());
|
||||
|
|
@ -338,8 +297,8 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_encrypt() {
|
||||
let k0 = mk_pkey();
|
||||
let k1 = mk_pkey();
|
||||
let k0 = pkey();
|
||||
let k1 = pkey();
|
||||
let msg: [u8] = [0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
||||
k0.gen(512u);
|
||||
k1.load_pub(k0.save_pub());
|
||||
|
|
@ -350,8 +309,8 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_sign() {
|
||||
let k0 = mk_pkey();
|
||||
let k1 = mk_pkey();
|
||||
let k0 = pkey();
|
||||
let k1 = pkey();
|
||||
let msg: [u8] = [0xdeu8, 0xadu8, 0xd0u8, 0x0du8];
|
||||
k0.gen(512u);
|
||||
k1.load_pub(k0.save_pub());
|
||||
|
|
|
|||
57
symm.rs
57
symm.rs
|
|
@ -1,9 +1,3 @@
|
|||
use std;
|
||||
|
||||
import core::ptr;
|
||||
import core::str;
|
||||
import core::vec;
|
||||
|
||||
import libc::c_int;
|
||||
|
||||
export crypter;
|
||||
|
|
@ -11,7 +5,6 @@ export cryptermode;
|
|||
export encryptmode, decryptmode;
|
||||
export cryptertype;
|
||||
export aes_256_ecb, aes_256_cbc;
|
||||
export mk_crypter;
|
||||
export encrypt, decrypt;
|
||||
export _native;
|
||||
|
||||
|
|
@ -38,40 +31,26 @@ native mod _native {
|
|||
fn EVP_CipherFinal(ctx: EVP_CIPHER_CTX, res: *u8, len: *u32);
|
||||
}
|
||||
|
||||
/*
|
||||
Object: crypter
|
||||
|
||||
Represents a symmetric cipher context.
|
||||
*/
|
||||
#[doc = "Represents a symmetric cipher context."]
|
||||
iface crypter {
|
||||
/*
|
||||
Method: pad
|
||||
|
||||
#[doc = "
|
||||
Enables or disables padding. If padding is disabled, total amount of data
|
||||
encrypted must be a multiple of block size.
|
||||
*/
|
||||
"]
|
||||
fn pad(padding: bool);
|
||||
|
||||
/*
|
||||
Method: init
|
||||
|
||||
Initializes this crypter.
|
||||
*/
|
||||
#[doc = "Initializes this crypter."]
|
||||
fn init(mode: cryptermode, key: [u8], iv: [u8]);
|
||||
|
||||
/*
|
||||
Method: update
|
||||
|
||||
#[doc = "
|
||||
Update this crypter with more data to encrypt or decrypt. Returns encrypted
|
||||
or decrypted bytes.
|
||||
*/
|
||||
"]
|
||||
fn update(data: [u8]) -> [u8];
|
||||
|
||||
/*
|
||||
Method: final
|
||||
|
||||
#[doc = "
|
||||
Finish crypting. Returns the remaining partial block of output, if any.
|
||||
*/
|
||||
"]
|
||||
fn final() -> [u8];
|
||||
}
|
||||
|
||||
|
|
@ -92,7 +71,7 @@ fn evpc(t: cryptertype) -> (EVP_CIPHER, uint, uint) {
|
|||
}
|
||||
}
|
||||
|
||||
fn mk_crypter(t: cryptertype) -> crypter {
|
||||
fn crypter(t: cryptertype) -> crypter {
|
||||
type crypterstate = {
|
||||
evp: EVP_CIPHER,
|
||||
ctx: EVP_CIPHER_CTX,
|
||||
|
|
@ -142,28 +121,24 @@ fn mk_crypter(t: cryptertype) -> crypter {
|
|||
ret h;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: encrypt
|
||||
|
||||
#[doc = "
|
||||
Encrypts data, using the specified crypter type in encrypt mode with the
|
||||
specified key and iv; returns the resulting (encrypted) data.
|
||||
*/
|
||||
"]
|
||||
fn encrypt(t: cryptertype, key: [u8], iv: [u8], data: [u8]) -> [u8] {
|
||||
let c = mk_crypter(t);
|
||||
let c = crypter(t);
|
||||
c.init(encryptmode, key, iv);
|
||||
let r = c.update(data);
|
||||
let rest = c.final();
|
||||
ret r + rest;
|
||||
}
|
||||
|
||||
/*
|
||||
Function: decrypt
|
||||
|
||||
#[doc = "
|
||||
Decrypts data, using the specified crypter type in decrypt mode with the
|
||||
specified key and iv; returns the resulting (decrypted) data.
|
||||
*/
|
||||
"]
|
||||
fn decrypt(t: cryptertype, key: [u8], iv: [u8], data: [u8]) -> [u8] {
|
||||
let c = mk_crypter(t);
|
||||
let c = crypter(t);
|
||||
c.init(decryptmode, key, iv);
|
||||
let r = c.update(data);
|
||||
let rest = c.final();
|
||||
|
|
@ -187,7 +162,7 @@ mod tests {
|
|||
let c0 =
|
||||
[ 0x8eu8, 0xa2u8, 0xb7u8, 0xcau8, 0x51u8, 0x67u8, 0x45u8, 0xbfu8,
|
||||
0xeau8, 0xfcu8, 0x49u8, 0x90u8, 0x4bu8, 0x49u8, 0x60u8, 0x89u8 ];
|
||||
let c = mk_crypter(aes_256_ecb);
|
||||
let c = crypter(aes_256_ecb);
|
||||
c.init(encryptmode, k0, []);
|
||||
c.pad(false);
|
||||
let r0 = c.update(p0) + c.final();
|
||||
|
|
|
|||
Loading…
Reference in New Issue