Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
BlockBlazeDev committed Apr 3, 2015
1 parent fc8ec5b commit ca5a652
Show file tree
Hide file tree
Showing 24 changed files with 165 additions and 144 deletions.
2 changes: 1 addition & 1 deletion src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use symmetriccipher::{Encryptor, Decryptor, SynchronousStreamCipher};
use util;

/// AES key size
#[derive(Copy)]
#[derive(Clone, Copy)]
pub enum KeySize {
KeySize128,
KeySize192,
Expand Down
4 changes: 4 additions & 0 deletions src/aesni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ pub struct AesNiEncryptor {
round_keys: [u8; 240]
}

impl Clone for AesNiEncryptor { fn clone(&self) -> AesNiEncryptor { *self } }

#[derive(Copy)]
pub struct AesNiDecryptor {
rounds: u8,
round_keys: [u8; 240]
}

impl Clone for AesNiDecryptor { fn clone(&self) -> AesNiDecryptor { *self } }

/// The number of rounds as well as a function to setup an appropriately sized key.
type RoundSetupInfo = (u8, fn(&[u8], KeyType, &mut [u8]));

Expand Down
10 changes: 5 additions & 5 deletions src/aessafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ macro_rules! define_aes_struct(
$name:ident,
$rounds:expr
) => (
#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct $name {
sk: [Bs8State<u16>; ($rounds + 1)]
}
Expand Down Expand Up @@ -227,7 +227,7 @@ macro_rules! define_aes_struct_x8(
$name:ident,
$rounds:expr
) => (
#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct $name {
sk: [Bs8State<u32x4>; ($rounds + 1)]
}
Expand Down Expand Up @@ -453,7 +453,7 @@ fn decrypt_core<S: AesOps + Copy>(state: &S, sk: &[S]) -> S {
tmp
}

#[derive(Copy)]
#[derive(Clone, Copy)]
struct Bs8State<T>(T, T, T, T, T, T, T, T);

impl <T: Copy> Bs8State<T> {
Expand Down Expand Up @@ -634,7 +634,7 @@ impl <T: Not<Output = T> + Copy> Bs8State<T> {
}
}

#[derive(Copy)]
#[derive(Clone, Copy)]
struct Bs4State<T>(T, T, T, T);

impl <T: Copy> Bs4State<T> {
Expand All @@ -658,7 +658,7 @@ impl <T: BitXor<Output = T> + Copy> Bs4State<T> {
}
}

#[derive(Copy)]
#[derive(Clone, Copy)]
struct Bs2State<T>(T, T);

impl <T> Bs2State<T> {
Expand Down
2 changes: 2 additions & 0 deletions src/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub struct Blake2b {
computed: bool, // whether the final digest has been computed
}

impl Clone for Blake2b { fn clone(&self) -> Blake2b { *self } }

struct Blake2bParam {
digest_length: u8,
key_length: u8,
Expand Down
6 changes: 3 additions & 3 deletions src/blockmodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait PaddingProcessor {

/// The BlockEngine is implemented as a state machine with the following states. See comments in the
/// BlockEngine code for more information on the states.
#[derive(Copy)]
#[derive(Clone, Copy)]
enum BlockEngineState {
FastMode,
NeedInput,
Expand Down Expand Up @@ -417,7 +417,7 @@ impl <P: BlockProcessor, X: PaddingProcessor> BlockEngine<P, X> {
}

/// No padding mode for ECB and CBC encryption
#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct NoPadding;

impl PaddingProcessor for NoPadding {
Expand All @@ -426,7 +426,7 @@ impl PaddingProcessor for NoPadding {
}

/// PKCS padding mode for ECB and CBC encryption
#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct PkcsPadding;

// This class implements both encryption padding, where padding is added, and decryption padding,
Expand Down
10 changes: 5 additions & 5 deletions src/blowfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cryptoutil::{read_u32v_be, write_u32_be};
use symmetriccipher::{BlockEncryptor, BlockDecryptor};
use step_by::RangeExt;

#[derive(Copy)]
#[derive(Clone,Copy)]
pub struct Blowfish {
s: [[u32; 256]; 4],
p: [u32; 18]
Expand Down Expand Up @@ -240,7 +240,7 @@ impl Blowfish {
}
}
}

// Bcrypt key schedule.
pub fn salted_expand_key(&mut self, salt: &[u8], key: &[u8]) {
let mut key_pos = 0;
Expand All @@ -264,7 +264,7 @@ impl Blowfish {
r = new_r;
self.s[i][j] = l;
self.s[i][j+1] = r;

let (new_l, new_r) = self.encrypt(l ^ next_u32_wrap(salt, &mut salt_pos), r ^ next_u32_wrap(salt, &mut salt_pos));
l = new_l;
r = new_r;
Expand Down Expand Up @@ -533,7 +533,7 @@ mod test {
assert!(test.ciphertext[..] == output[..]);
}
}

#[test]
fn decrypt_eay_test_vectors() {
let tests = eay_test_vectors();
Expand All @@ -558,7 +558,7 @@ mod bench {
let plaintext = [1u8; 8];
let state = Blowfish::new(&key);
let mut ciphertext = [0u8; 8];

bh.iter(|| {
state.encrypt_block(&plaintext, &mut ciphertext);
});
Expand Down
2 changes: 1 addition & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::cmp;

use cryptoutil;

#[derive(Copy)]
#[derive(Clone,Copy)]
pub enum BufferResult {
BufferUnderflow,
BufferOverflow
Expand Down
8 changes: 5 additions & 3 deletions src/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use symmetriccipher::{Encryptor, Decryptor, SynchronousStreamCipher, SymmetricCi
use cryptoutil::{read_u32_le, symm_enc_or_dec, write_u32_le, xor_keystream};
use simd::u32x4;

#[derive(Copy)]
#[derive(Clone,Copy)]
struct ChaChaState {
a: u32x4,
b: u32x4,
Expand All @@ -25,6 +25,8 @@ pub struct ChaCha20 {
offset : usize,
}

impl Clone for ChaCha20 { fn clone(&self) -> ChaCha20 { *self } }

macro_rules! swizzle{
($b: expr, $c: expr, $d: expr) => {{
let u32x4(b10, b11, b12, b13) = $b;
Expand Down Expand Up @@ -69,7 +71,7 @@ macro_rules! round{

macro_rules! rotate {
($a: expr, $b: expr, $c:expr) => {{
let v = $a ^ $b;
let v = $a ^ $b;
let r = S32 - $c;
let right = v >> r;
$a = (v << $c) ^ right
Expand Down Expand Up @@ -112,7 +114,7 @@ impl ChaCha20 {
}

fn expand(key: &[u8], nonce: &[u8]) -> ChaChaState {

let constant = match key.len() {
16 => b"expand 16-byte k",
32 => b"expand 32-byte k",
Expand Down
24 changes: 12 additions & 12 deletions src/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use poly1305::Poly1305;
use mac::Mac;
use cryptoutil::{write_u64_le};
use util::fixed_time_eq;
#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct ChaCha20Poly1305 {
cipher : ChaCha20,
mac: Poly1305,
Expand Down Expand Up @@ -99,7 +99,7 @@ mod test {
aad: Vec<u8>,
tag: Vec<u8>
}

#[test]
fn test_chacha20_256_poly1305_boringssl_vectors_encrypt() {

Expand Down Expand Up @@ -748,17 +748,17 @@ mod bench {
bh.iter( || {
let mut cipher = ChaCha20Poly1305::new(&[0; 32], &[0; 8], &aad);
let mut decipher = ChaCha20Poly1305::new(&[0; 32], &[0; 8], &aad);

let mut output = [0u8; 10];
let mut tag = [0u8; 16];
let mut output2 = [0u8; 10];
cipher.encrypt(&input, &mut output, &mut tag);
decipher.decrypt(&output, &mut output2, &tag);

});
bh.bytes = 10u64;
}


#[bench]
pub fn chacha20poly1305_1k(bh: & mut Bencher) {
Expand All @@ -767,16 +767,16 @@ mod bench {
bh.iter( || {
let mut cipher = ChaCha20Poly1305::new(&[0; 32], &[0; 8], &aad);
let mut decipher = ChaCha20Poly1305::new(&[0; 32], &[0; 8], &aad);

let mut output = [0u8; 1024];
let mut tag = [0u8; 16];
let mut output2 = [0u8; 1024];

cipher.encrypt(&input, &mut output, &mut tag);
decipher.decrypt(&output, &mut output2, &tag);
});
bh.bytes = 1024u64;

}

#[bench]
Expand All @@ -786,16 +786,16 @@ mod bench {
bh.iter( || {
let mut cipher = ChaCha20Poly1305::new(&[0; 32], &[0; 8], &aad);
let mut decipher = ChaCha20Poly1305::new(&[0; 32], &[0; 8], &aad);

let mut output = [0u8; 65536];
let mut tag = [0u8; 16];
let mut output2 = [0u8; 65536];

cipher.encrypt(&input, &mut output, &mut tag);
decipher.decrypt(&output, &mut output2, &tag);

});
bh.bytes = 65536u64;

}
}
}
2 changes: 2 additions & 0 deletions src/cryptoutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ pub struct FixedBuffer64 {
buffer_idx: usize,
}

impl Clone for FixedBuffer64 { fn clone(&self) -> FixedBuffer64 { *self } }

impl FixedBuffer64 {
/// Create a new buffer
pub fn new() -> FixedBuffer64 {
Expand Down
12 changes: 6 additions & 6 deletions src/curve25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9].
Bounds on each t[i] vary depending on context.
*/

#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct Fe(pub [i32; 10]);

impl PartialEq for Fe {
Expand Down Expand Up @@ -1062,37 +1062,37 @@ impl Fe {
}
}

#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct GeP2 {
x: Fe,
y: Fe,
z: Fe,
}

#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct GeP3 {
x: Fe,
y: Fe,
z: Fe,
t: Fe,
}

#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct GeP1P1 {
x: Fe,
y: Fe,
z: Fe,
t: Fe,
}

#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct GePrecomp {
y_plus_x: Fe,
y_minus_x: Fe,
xy2d: Fe,
}

#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct GeCached {
y_plus_x: Fe,
y_minus_x: Fe,
Expand Down
13 changes: 6 additions & 7 deletions src/fortuna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* is designed to be timing-attack resistant. The speed hit from this
* is in line with a "safety first" API, but be aware of it.
*
* Fortuna was originally described in
* Fortuna was originally described in
* Practical Cryptography, Niels Ferguson and Bruce Schneier.
* John Wiley & Sons, 2003.
*
Expand Down Expand Up @@ -147,7 +147,7 @@ impl FortunaGenerator {


/// A single entropy pool (not public)
#[derive(Copy)]
#[derive(Clone, Copy)]
struct Pool {
state: Sha256,
count: usize
Expand Down Expand Up @@ -194,7 +194,7 @@ impl Fortuna {
}
}

/// Adds a random event `e` from source `s` to entropy pool `i` (PC 9.5.6)
/// Adds a random event `e` from source `s` to entropy pool `i` (PC 9.5.6)
pub fn add_random_event(&mut self, s: u8, i: usize, e: &[u8]) {
assert!(i <= NUM_POOLS);
// These restrictions (and `s` in [0, 255]) are part of the Fortuna spec.
Expand Down Expand Up @@ -352,7 +352,7 @@ mod tests {
50, 68, 236, 107, 133, 18, 217, 219, 46, 134,
169, 156, 211, 74, 163, 17, 100, 173, 26, 70,
246, 193, 57, 164, 167, 175, 233, 220, 160, 114,
2, 200, 215, 80, 207, 218, 85, 58, 235, 117,
2, 200, 215, 80, 207, 218, 85, 58, 235, 117,
177, 223, 87, 192, 50, 251, 61, 65, 141, 100,
59, 228, 23, 215, 58, 107, 248, 248, 103, 57,
127, 31, 241, 91, 230, 33, 0, 164, 77, 46];
Expand Down Expand Up @@ -411,7 +411,7 @@ mod tests {

// from Crypto.Random.Fortuna import FortunaAccumulator
// x = FortunaAccumulator.FortunaAccumulator()
// x.add_random_event(0, 0, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
// x.add_random_event(0, 0, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
// x.add_random_event(0, 0, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
// x.add_random_event(1, 0, "\1\2")
// x.add_random_event(1, 1, "\1\2")
Expand All @@ -433,7 +433,7 @@ mod tests {
f.add_random_event(0, 0, &[0; 32]);
f.add_random_event(0, 0, &[0; 32]);

// x.add_random_event(0, 0, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
// x.add_random_event(0, 0, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
// x.add_random_event(0, 0, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")
// print list(bytearray(x.random_data(100)))
let expected = [101, 123, 175, 157, 142, 202, 211, 47, 149, 214,
Expand Down Expand Up @@ -513,4 +513,3 @@ mod bench {
bh.bytes = bytes.len() as u64;
}
}

Loading

0 comments on commit ca5a652

Please sign in to comment.