keepsimple1 / libaes

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

Cipher::cbc_decrypt panics for invalid input lengths #16

Closed Nothing4You closed 2 years ago

Nothing4You commented 2 years ago

Hi,

according to the documentation, Cipher::cbc_decrypt should only panic when iv is less than 16 bytes. Other "unexpected errors" are supposed to return an empty Vec instead.

When providing an invalid input that is not aligned to 16 bytes it also panics:

#[test]
fn invalid_input_decrypt() {
    let key_128 = b"k123456789012345";
    let cipher = Cipher::new_128(key_128);
    let iv = b"v123456789012345";
    let bad_ciphertext = b"foo";
    cipher.cbc_decrypt(iv, &bad_ciphertext[..]);
}

---- invalid_input_decrypt stdout ---- thread 'invalid_input_decrypt' panicked at 'range end index 16 out of range for slice of length 3', library/core/src/slice/index.rs:73:5

keepsimple1 commented 2 years ago

Good catch. Thanks for opening the issue. I've open a PR to fix this. Let me know if that would work for you, or if you have any questions.

Nothing4You commented 2 years ago

Looks fine, thanks.

I'm curious though, is there a reason why you're not returning a std::result::Result?

keepsimple1 commented 2 years ago

Thanks! I didn't use Result for a couple of reasons: 1) even when the method returns Ok, it does not mean the msg decrypted correctly. 2) I felt sometimes returning an empty value is simpler in API than returning a Result. No unwrap(), no a new Error type, etc. When I used it at first myself, I felt it's easier, so I kept as is.