Skip to content

Commit

Permalink
Aes128Cbc works
Browse files Browse the repository at this point in the history
  • Loading branch information
peteraba committed Jun 8, 2020
1 parent de058fa commit 01ae93f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 60 deletions.
24 changes: 11 additions & 13 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 12 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,7 +23,7 @@ use std::ffi::CString;
use std::ffi::CStr;

// create an alias for convinience
type Aes128Ebc = Ecb<Aes128, Pkcs7>;
type Aes128Cbc = Cbc<Aes128, Pkcs7>;

fn c_str_ptr_to_rust(s: *const c_char) -> &'static str {
let c_str = unsafe {
Expand Down Expand Up @@ -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]
Expand Down
33 changes: 0 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Aes128, Pkcs7>;

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);
}

0 comments on commit 01ae93f

Please sign in to comment.