kokke/tiny-AES-c

Looking for debugging help: Running tiny AES on an embedded dsp

dconz13 opened this issue · 8 comments

I'm trying to use the tinyAES library on a TMS320VC5510 DSP chip with a 192bit key. The code compiles fine but when I try to run the test.c, none of the test cases pass. I'm not sure what to change to fix this so that I am able to use the library for encryption.

My current suspicion is that maybe the lowest space the chip can allocate is 16bits so the uint8_t allocations and bit manipulations get messed up? Any ideas / leads would be much appreciated.

kokke commented

Hi @dconz13 and thanks for your interest in the project :)

I would start by checking sizeof(uint8_t) and verifying that it's 1.

You can usually verify by either printf("%u", sizeof(uint8_t)); or trying to compile something like this:

#include <stdint.h>

typedef int static_assertion [sizeof(uint8_t) == 1 ? 1 : -1];

Can you post your code ?

EDIT:

My current suspicion is that maybe the lowest space the chip can allocate is 16bits so the uint8_t allocations and bit manipulations get messed up?

I think I've seen something similar before with a TI DSP, so yeah - definitely verify that the CPU handles byte-wise arithmetic well, by checking the size of uint8_t.

Hey thanks for the quick response. I ran printf("%u", sizeof(unsigned char)); and did get 1. So that weeds out the uint8_t from being the issue. One other notable thing is that I couldn't get the library to compile using the stdint.h for this compiler. To get around it I defined the following typedefs in aes.h and removed the stdint.h include:

typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef unsigned int uintptr_t;

Other than that, the aes.c, aes.h and test.c are unmodified. I'm currently trying to run the main function in the test.c file that runs the various test functions.

kokke commented

Okay so the size of uint8_t is correct.

Next I would ensure that test.c and aes.h + aes.c are compiled with AES192=1 defined, since it's different from the default, which is 128 bit keys.

You could check that by inserting this piece of code into test.c, aes.h and aes.c:

#if !defined(AES192) || (AES192 != 1)
  #error AES192 is not defined == 1
#endif

If you compile for instance aes.c with AES192=1 but compile test.c with AES128=1, there will be trouble.

I added that code and confirmed that AES192 is set to 1. I decided to try checking the size of uint32_t and uintptr_t on the chip and noticed that printf("%u", sizeof(unsigned int)); returns 1 and prinf("%u", sizeof(unsigned long int)); returns 2. This doesn't match up with the values returned when I run it on my intel cpu or the values listed on tutorialspoint. Maybe I need to figure out how to allocate larger blocks of memory for these typedefs?

C47D commented

Maybe you can try checking the size of unsigned long long.

unsigned long long returns 4. I tried changing uint32_t and uintptr_t to unsigned long long but still no luck. I also realized that the ecb mode (what I'm trying to run) doesn't use these types.

I'm going to close the issue since it has to do with my particular architecture and not the library itself.

For my chip, since the uint8_t is using 16bits of memory, there is a carry bit that is propogating during the MixColumns function. Doing ... (*state)[i][x] ^= (Tm ^ Tmp) & 0xFF; drops the carry bit and produces the correct encryption result based off the nist aes expected values. Thanks for the help guys!

kokke commented

@C47D thanks for the help :)

@dconz13 I re-read your post and noticed that you didn't actually test the sizeof(uint8_t) but of unsigned char which is slightly different, in that uint8_t must be EXACTLY 1 byte wide, if the type exists.

For my chip, since the uint8_t is using 16bits of memory

Do I understand correctly that you're saying sizeof(unsigned char) is actually 2 on this chip?

I thought about adding assertions to the test-code, to make it fail faster, when assumptions aren't met - like so:

assert(sizeof(uint8_t) == 1);
assert(sizeof(uint32_t) == 4);

... which might have given you a hint faster.

I know that the compile didn't work with stdint.h out of the box, but then you made your own typedef and THAT would have been caught by the assertion.