From 01ae93fc9ede0d5955b615e950f75812b1bb28ca Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 9 Jun 2020 01:40:50 +0200 Subject: [PATCH] Aes128Cbc works --- Cargo.lock | 24 +++++++++++------------- Cargo.toml | 3 ++- src/lib.rs | 25 ++++++++++++------------- src/main.rs | 33 --------------------------------- 4 files changed, 25 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2c6f589..d607768 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,20 +44,12 @@ dependencies = [ "generic-array 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "block-cipher-trait" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "block-modes" -version = "0.3.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "block-cipher 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -268,6 +260,11 @@ dependencies = [ "libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "hex-literal" version = "0.2.1" @@ -697,7 +694,8 @@ version = "0.1.0" dependencies = [ "aes-soft 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", - "block-modes 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "block-modes 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "hex-literal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1084,8 +1082,7 @@ dependencies = [ "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" "checksum block-cipher 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f837aab23b44bf3be7e24411b599fda76b1788792e765fa077af268292e156d1" -"checksum block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1c924d49bd09e7c06003acda26cd9742e796e34282ec6c1189404dee0c1f4774" -"checksum block-modes 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "31aa8410095e39fdb732909fb5730a48d5bd7c2e3cd76bd1b07b3dbea130c529" +"checksum block-modes 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "538b66bc25a51ce985544067a9c3e17d426447f468c495dd6cb00040e5953692" "checksum block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" "checksum bumpalo 3.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" @@ -1115,6 +1112,7 @@ dependencies = [ "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" "checksum h2 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "79b7246d7e4b979c03fa093da39cfb3617a96bbeee6310af63991668d7e843ff" "checksum hermit-abi 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71" +"checksum hex 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" "checksum hex-literal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "961de220ec9a91af2e1e5bd80d02109155695e516771762381ef8581317066e0" "checksum hex-literal-impl 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "853f769599eb31de176303197b7ba4973299c38c7a7604a6bc88c3eef05b9b46" "checksum http 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" diff --git a/Cargo.toml b/Cargo.toml index 28fa939..fbda261 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,8 @@ crate-type = ["cdylib"] [dependencies] aes-soft = "0.4.0" base64 = "0.12.1" -block-modes = "0.3.3" +block-modes = "0.4.0" +hex = "0.4.2" hex-literal = "0.2.1" http = "0.2.1" libc = "0.2.71" diff --git a/src/lib.rs b/src/lib.rs index 694326b..b4d3abd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,18 @@ -#[macro_use] extern crate hex_literal; extern crate base64; +extern crate hex; extern crate libc; extern crate sha2; extern crate sha3; extern crate reqwest; extern crate aes_soft as aes; extern crate block_modes; +extern crate hex_literal; //external crates use aes::Aes128; -use block_modes::{Ecb}; +use block_modes::{BlockMode, Cbc}; use block_modes::block_padding::Pkcs7; +use hex_literal::hex; use http::HeaderMap; use http::header::{CONTENT_TYPE, ACCEPT}; use libc::c_char; @@ -21,7 +23,7 @@ use std::ffi::CString; use std::ffi::CStr; // create an alias for convinience -type Aes128Ebc = Ecb; +type Aes128Cbc = Cbc; fn c_str_ptr_to_rust(s: *const c_char) -> &'static str { let c_str = unsafe { @@ -101,27 +103,24 @@ pub extern "C" fn aes_encode() -> *mut c_char { let key = hex!("000102030405060708090a0b0c0d0e0f"); let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"); let plaintext = b"Hello world!"; - let cipher = Aes128Ebc::new_var(&key, &iv).unwrap(); - let ciphertext = cipher.encrypt_vec(plaintext); + let cipher = Aes128Cbc::new_var(&key, &iv).unwrap(); - let s_str = format!("{:x}", ciphertext); + let ciphertext = cipher.encrypt_vec(plaintext); - return rust_to_c_str_ptr(s_str.to_string()); + return rust_to_c_str_ptr(hex::encode(ciphertext)); } -#[no_mangle] +// #[no_mangle] pub extern "C" fn aes_decode() -> *mut c_char { let ciphertext = hex!("1b7a4c403124ae2fb52bedc534d82fa8"); let key = hex!("000102030405060708090a0b0c0d0e0f"); let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"); - let plaintext = b"Hello world!"; - let cipher = Aes128Ebc::new_var(&key, &iv).unwrap(); + let cipher = Aes128Cbc::new_var(&key, &iv).unwrap(); + let decrypted_ciphertext = cipher.decrypt_vec(&ciphertext).unwrap(); - let s_str = format!("{:x}", decrypted_ciphertext); - - return rust_to_c_str_ptr(s_str.to_string()); + return rust_to_c_str_ptr(hex::encode(decrypted_ciphertext)); } #[no_mangle] diff --git a/src/main.rs b/src/main.rs index 76cc236..f79c691 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,35 +1,2 @@ -#[macro_use] extern crate hex_literal; -extern crate aes_soft as aes; -extern crate block_modes; - -use aes::Aes128; -use block_modes::{BlockMode, Cbc}; -use block_modes::block_padding::Pkcs7; -use hex_literal::hex; - -// create an alias for convenience -type Aes128Cbc = Cbc; - fn main() { - - let key = hex!("000102030405060708090a0b0c0d0e0f"); - let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"); - let plaintext = b"Hello world!"; - let cipher = Aes128Cbc::new_var(&key, &iv).unwrap(); - - // buffer must have enough space for message+padding - let mut buffer = [0u8; 32]; - // copy message to the buffer - let pos = plaintext.len(); - buffer[..pos].copy_from_slice(plaintext); - let ciphertext = cipher.encrypt(&mut buffer, pos).unwrap(); - - assert_eq!(ciphertext, hex!("1b7a4c403124ae2fb52bedc534d82fa8")); - - // re-create cipher mode instance and decrypt the message - let cipher = Aes128Cbc::new_var(&key, &iv).unwrap(); - let mut buf = ciphertext.to_vec(); - let decrypted_ciphertext = cipher.decrypt(&mut buf).unwrap(); - - assert_eq!(decrypted_ciphertext, plaintext); }