Skip to content

Commit

Permalink
Failing AES
Browse files Browse the repository at this point in the history
  • Loading branch information
peteraba committed Jun 8, 2020
1 parent 5936c3d commit de058fa
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 135 deletions.
8 changes: 1 addition & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
57 changes: 0 additions & 57 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Aes128, Pkcs7>;
Expand All @@ -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 {
Expand Down
34 changes: 33 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Aes128, Pkcs7>;

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);
}
64 changes: 0 additions & 64 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, )
Expand All @@ -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:
Expand Down Expand Up @@ -172,13 +115,6 @@ def postXml(url, body):
</software>
</TokenExchangeRequest>"""

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"))
Expand Down

0 comments on commit de058fa

Please sign in to comment.