keepsimple1 / libaes

A small and fast AES cipher in safe Rust
Apache License 2.0
25 stars 1 forks source link

This use case does not work #29

Closed shijimasoft closed 11 months ago

shijimasoft commented 11 months ago

I open this issue because I ran into a somewhat strange situation: I am rewriting a python code in rust using libaes for AES decryption, below is the code I use:

    let data: [u8; 16] = hex!("9f2707bc98bb5781d4e7b461bffe6270");
    let key: [u8; 16] = hex!("64c5fd55dd3ad988325baaec5243db98");
    let iv: [u8; 16] = hex!("0004008c001658000000000000000000");

    let aes: Cipher = Cipher::new_128(&key);
    let dec: Vec<u8> = aes.cbc_decrypt(&iv, &data);

    println!("{:?}", dec); // This will print an empty vec

Using pycrypto instead works correctly, returning the expected result: 59f96218d8eccab277ed477a33dcb7f3 (expected byte array translated into an hex string)

Maybe I'm doing something wrong or just forgetting some padding things or the data format is incorrect (literal hex! returns a decimal format byte array i.e: [159, 39, 7, 188, 152, 187, 87, 129, 212, 231, 180, 97, 191, 254, 98, 112])

shijimasoft commented 11 months ago

I tried the reverse (encryption) process and the data I got are slightly different from what I expected in fact the encrypted data are: 9f2707bc98bb5781d4e7b461bffe6270b419f11edb920f94f6a1b6ec8e469926 (32 bytes) and not the expected 9f2707bc98bb5781d4e7b461bffe6270 (16 bytes).

keepsimple1 commented 11 months ago

This is because padding (PKCS7) is always included, hence for a 16-byte input, its padding will be another 16-byte padding, leading to total 32 bytes.

pycrypto is different in a way that padding is handled separately and require the user to add padding when needed.

shijimasoft commented 11 months ago

Is it possible to implement a flag for manual padding management? (For encryption as well as decryption)

keepsimple1 commented 11 months ago

Is it possible to implement a flag for manual padding management? (For encryption as well as decryption)

Yes it's possible. I've opened a PR #30 to do that. Would you have time to try out the PR's branch and see it works for you?

shijimasoft commented 11 months ago

I just checked the PR, everything seems to be working correctly (I also tried other use cases and they are all correct). Thank you for being so quick to help me!

keepsimple1 commented 11 months ago

Thanks for verifying! I've merged the PR. Will post a new release soon.

shijimasoft commented 11 months ago

Okk! Thank you for your work!