From de058fae4fe4a0b55f88f22aa723565b6e36df38 Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 9 Jun 2020 00:21:16 +0200 Subject: [PATCH] Failing AES --- Cargo.lock | 8 +------ Cargo.toml | 12 +++++----- src/lib.rs | 57 ----------------------------------------------- src/main.rs | 34 +++++++++++++++++++++++++++- test.py | 64 ----------------------------------------------------- 5 files changed, 40 insertions(+), 135 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9ed3fa8..2c6f589 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,11 +15,6 @@ name = "autocfg" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "base64" -version = "0.12.0" -source = "git+https://github.com/marshallpierce/rust-base64.git#5e41403e63177b97c1e52e5697b3f67d85109b86" - [[package]] name = "base64" version = "0.12.1" @@ -701,7 +696,7 @@ name = "rhttp" version = "0.1.0" dependencies = [ "aes-soft 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "base64 0.12.0 (git+https://github.com/marshallpierce/rust-base64.git)", + "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)", "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)", @@ -1085,7 +1080,6 @@ dependencies = [ [metadata] "checksum aes-soft 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4925647ee64e5056cf231608957ce7c81e12d6d6e316b9ce1404778cc1d35fa7" "checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" -"checksum base64 0.12.0 (git+https://github.com/marshallpierce/rust-base64.git)" = "" "checksum base64 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "53d1ccbaf7d9ec9537465a97bf19edc1a4e158ecb49fc16178202238c569cc42" "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" diff --git a/Cargo.toml b/Cargo.toml index e932270..28fa939 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,12 +9,12 @@ name = "rhttp" crate-type = ["cdylib"] [dependencies] -base64 = { git = "https://github.com/marshallpierce/rust-base64.git" } +aes-soft = "0.4.0" +base64 = "0.12.1" +block-modes = "0.3.3" +hex-literal = "0.2.1" +http = "0.2.1" libc = "0.2.71" +reqwest = { version = "0.10.6", features = ["blocking"] } sha2 = "0.8.2" sha3 = "0.8.2" -reqwest = { version = "0.10.6", features = ["blocking"] } -http = "0.2.1" -aes-soft = "0.4.0" -block-modes = "0.3.3" -hex-literal = "0.2.1" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 044c2d4..694326b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,6 @@ use sha2::{Sha512, Digest}; use sha3::{Sha3_512}; use std::ffi::CString; use std::ffi::CStr; -use std::iter; // create an alias for convinience type Aes128Ebc = Ecb; @@ -40,62 +39,6 @@ fn rust_to_c_str_ptr(s: String) -> *mut c_char { return c_str.into_raw() } - -#[no_mangle] -pub extern "C" fn add_numbers(number1: i32, number2: i32) -> i32 { - return number1 + number2; -} - -#[no_mangle] -pub extern "C" fn sub_numbers(number1: i32, number2: i32) -> i32 { - return number1 - number2; -} - -#[no_mangle] -pub extern "C" fn sq32(number1: f32) -> f32 { - return number1 * number1; -} - -#[no_mangle] -pub extern "C" fn sq64(number1: f64) -> f64 { - return number1 * number1; -} - -#[no_mangle] -pub extern "C" fn gen(length: i32) -> *mut c_char { - let repeated = "a".repeat(length as usize); - - return rust_to_c_str_ptr(repeated); -} - -#[no_mangle] -pub extern "C" fn how_many_characters(s: *const c_char) -> i32 { - let r_str = c_str_ptr_to_rust(s); - - return r_str.chars().count() as i32; -} - -#[no_mangle] -pub extern "C" fn szia() -> *mut c_char { - let r_str = String::from("Szia, Apu!"); - - return rust_to_c_str_ptr(r_str); -} - -#[no_mangle] -pub extern "C" fn tizenhet() -> i32 { - return 17; -} - -#[no_mangle] -pub extern "C" fn theme_song_generate(length: i32) -> *mut c_char { - let mut song = String::from("ő "); - song.extend(iter::repeat("na ").take(length as usize)); - song.push_str("Batman! ő"); - - return rust_to_c_str_ptr(song); -} - #[no_mangle] pub extern "C" fn free_string(s: *mut c_char) { unsafe { diff --git a/src/main.rs b/src/main.rs index 83f16dc..76cc236 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,35 @@ +#[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() { - //println!("Hello World!"); + + 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); } diff --git a/test.py b/test.py index 0aefbf0..1064680 100644 --- a/test.py +++ b/test.py @@ -11,29 +11,6 @@ lib = ctypes.cdll.LoadLibrary(dll) # Definitions -lib.tizenhet.argtypes = () - -lib.add_numbers.argtypes = (c_int32, c_int32, ) -lib.add_numbers.restype = c_int32 - -lib.how_many_characters.argtypes = (c_void_p, ) -lib.how_many_characters.restype = c_int32 - -lib.szia.argtypes = () -lib.szia.restype = c_void_p - -lib.gen.argtypes = (c_int32, ) -lib.gen.restype = c_void_p - -lib.sq32.argtypes = (c_float, ) -lib.sq32.restype = c_float - -lib.sq64.argtypes = (c_double, ) -lib.sq64.restype = c_double - -lib.theme_song_generate.argtypes = (c_int32, ) -lib.theme_song_generate.restype = c_void_p - lib.free_string.argtypes = (c_void_p, ) lib.base64_encode.argtypes = (c_void_p, ) @@ -54,40 +31,6 @@ lib.post_xml.argtypes = (c_void_p, c_void_p, ) lib.post_xml.restype = c_void_p -# Example calls -def addTo17Minus3(count): - return lib.add_numbers(count, lib.sub_numbers(lib.tizenhet(), 3)) - -def sq32(n): - return lib.sq32(n) - -def sq64(n): - return lib.sq64(n) - -def howMany(text): - return lib.how_many_characters(text.encode('utf-8')) - -def szia(): - ptr = lib.szia() - try: - return ctypes.cast(ptr, ctypes.c_char_p).value.decode('utf-8') - finally: - lib.free_string(ptr) - -def multiplA(count): - ptr = lib.gen(count) - try: - return ctypes.cast(ptr, ctypes.c_char_p).value.decode('utf-8') - finally: - lib.free_string(ptr) - -def themeSongGenerate(count): - ptr = lib.theme_song_generate(count) - try: - return ctypes.cast(ptr, ctypes.c_char_p).value.decode('utf-8') - finally: - lib.free_string(ptr) - def base64(text): ptr1 = lib.base64_encode(text.encode('utf-8')) try: @@ -172,13 +115,6 @@ def postXml(url, body): """ -print(addTo17Minus3(2)) -print(sq32(1.5)) -print(sq64(2.5)) -print(howMany("12345")) -print(szia()) -print(multiplA(5)) -print(themeSongGenerate(5)) print(base64("any carnal pleasure.")) print(sha512("mysecret", "7b6f7690ae2a5ecdf66b3db2adf91340a680da1ab82561796b8504db942476967369814aa35050dd86838848c1ba703450f2f5e21b0a8e4cff690b855ae5bd8c")) print(sha3_512("mysecret", "ef846feafed891792553756277b48e90784eca281f683920551f36b359833b10aab4897765050e398232e3f213fe49c7c50271f339d4797c25dc58c3d7f33f81"))