~kf5jwc/cryptopals-challenges

1d69748f36c7fb6547286968cf49b72ee2208d26 — Kyle Jones 1 year, 17 days ago 2c109bb
Implement CTR mode
5 files changed, 40 insertions(+), 3 deletions(-)

M aes/Cargo.lock
M aes/Cargo.toml
A aes/src/ctr.rs
A aes/src/ctr/round.rs
M aes/src/lib.rs
M aes/Cargo.lock => aes/Cargo.lock +4 -3
@@ 3,6 3,7 @@ name = "aes"
version = "0.1.0"
dependencies = [
 "base64 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "counter 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "input 0.1.0",


@@ 42,7 43,7 @@ name = "base64"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]


@@ 52,7 53,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "byteorder"
version = "1.3.0"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]


@@ 450,7 451,7 @@ dependencies = [
"checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799"
"checksum base64 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "621fc7ecb8008f86d7fb9b95356cd692ce9514b80a86d85b397f32a22da7b9e2"
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum byteorder 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60f0b0d4c0a382d2734228fd12b5a6b5dac185c60e938026fd31b265b94f9bd2"
"checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum counter 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d84b66ce02c964fa8047286289b36797ce48a52a44034e013ce3e5219b6cb360"

M aes/Cargo.toml => aes/Cargo.toml +1 -0
@@ 15,3 15,4 @@ log = "0.4.6"
env_logger = "0.6.0"
rand = "0.6.0"
counter = "0.4.3"
byteorder = "1.3.1"

A aes/src/ctr.rs => aes/src/ctr.rs +3 -0
@@ 0,0 1,3 @@
mod round;

pub use self::round::round;

A aes/src/ctr/round.rs => aes/src/ctr/round.rs +31 -0
@@ 0,0 1,31 @@
use crate::{
    BLOCK_SIZE,
    ecb,
};
use crypto::symmetriccipher::SymmetricCipherError;
use byteorder::{LittleEndian, WriteBytesExt};

pub fn round(key: &[u8], nonce: &u64, count: &u64, data: &[u8])
    -> Result<Vec<u8>, SymmetricCipherError>
{
    assert!(data.len() <= BLOCK_SIZE);
    let keystream: Vec<u8> = {
        let mut ret = vec![];
        ret.write_u64::<LittleEndian>(*nonce).unwrap();
        ret.write_u64::<LittleEndian>(*count).unwrap();
        eprintln!("nonce+ctr: {:?}", ret);
        // the encrypt function properly adds a block of padding
        // however this is never applied, so I won't trim it.
        ecb::encrypt(&ret, key)?
    };
    eprintln!("keystream: {:?}", keystream);
    let block = {
        keystream
        .iter()
        .zip(data)
        .map(|(n,m)| n^m)
        .collect()
    };
    eprintln!("round:     {:?}", block);
    Ok(block)
}

M aes/src/lib.rs => aes/src/lib.rs +1 -0
@@ 1,6 1,7 @@
pub use crypto::aes::KeySize;
pub mod ecb;
pub mod cbc;
pub mod ctr;
pub mod oracles;
pub mod mode_detection;