kokke / tiny-AES-c

Small portable AES128/192/256 in C
The Unlicense
4.3k stars 1.3k forks source link

AES 256 output is inverted #187

Closed esimunds closed 3 years ago

esimunds commented 3 years ago

I've been using this project to send encrypted messages over Bluetooth to a peripheral device. The encryption part works well and the device understands me and sends replies back.

However, I've just decrypted my first message and after a bit of head scratching I noticed that if I invert the bytes of the output, I get the expected message. I am using AES 256 in CBC mode.

Has anyone else seen this behavior? Thanks

kokke commented 3 years ago

Hi @esimunds and thanks for your interest

First let me note that this project has been tested thoroughly and against official "golden vectors", meaning input + output pairs which have been certified by NIST. I find it highly unlikely that an error in this project is causing you problems.

What you are describing sounds more like a case of "misuse" where you are doing something wrong or unintended.

Opening an issue here is not a very good way to get help with usage-related problems - I suggest you open a question on stackoverflow.com instead.

If you can post the code you are using on both sides here, maybe I can take a look and try and see if I can spot the error :)

You mention that you exchange data with a peripheral unit via Bluetooth - I would expect the errors to be contained in that part of the code.

esimunds commented 3 years ago

I figured it was some sort of user error on my part, I just thought maybe someone else had made the same mistakes and could help out. It turns out I hadn't zeroed out the IV when starting the decryption and it caused the decrypted response message to look quite similar to the inverse of what I expected, when in reality it wasn't a perfect inversion and I was just getting lucky to get somewhat close, if that makes sense.

So "misuse" is definitely the cause and I have sorted it out. Probably should have done a bit more testing before posting here.

kokke commented 3 years ago

@esimunds no problem at all - glad you got it working :)

I am working on a FAQ and some sort of implementation-guide. +90% of all issues here are asking for help on how to use the library correctly, or have problems because they aren't.

The focus on small size means there is less hand-holding and fewer meaningful error messages to guide you.

That makes the library less user-friendly than it could be with better documentation...