1

I'm writing simple aes decoder/encoder using libtomcrypt. I need to story symmetric keys used in encoding, to decode data later. The problem is that the value of key which I access from "storage" struct changes comparing to the key that was used in encoder.

I've tried to assign key value to element of global symmetric_key array elem, but the value is still different from original

#include <tomcrypt.h>
typedef struct {
  unsigned char * data;
  unsigned char * crc_value;
  symmetric_key skey;
}
aes_info_set;
aes_info_set aes_encrypter(unsigned char * data, unsigned char * key, int buf_size, int keysize, int rounds);
void main() {
  unsigned char * data = "hallo world";
  aes_info_set test;
  test = aes_encrypter(data, key, 80, 32, 14);
  printf("struct skey: %x \n", test.skey); 
}

aes_info_set aes_encrypter(unsigned char * data, unsigned char * key, int buf_size, int keysize, int rounds) {
  aes_info_set info_pack;
  unsigned char * text = data;
  unsigned char enc_out[buf_size];
  unsigned char * crc_value = (unsigned char * ) malloc(4 * sizeof(unsigned char));
  symmetric_key skey;
  crc_value = crc_check(text, strlen(text));
  aes_keysize( & keysize);
  aes_setup(key, keysize, rounds, & skey);
  aes_ecb_encrypt(text, enc_out, & skey);
  printf("FROM FUNC\n");
  info_pack.data = enc_out;
  info_pack.crc_value = crc_value;
  info_pack.skey = skey;
  printf("func skey: %x \n", skey);
  return info_pack;
}

//Output
>func skey: a15b56e0
>
>struct skey: a15b7890 

I expected them to be the same

astaria
  • 19
  • 1
  • (a) Turn on compiler warnings, at least `-Wmost` if you are using GCC or Clang. Then see whether there is a warning on the line `printf("func skey: %x \n", skey);`. (b) It may be that `symmetric_key` is a pointer, and you cannot access its value merely with `skey` (where `skey` is defined with `symmetric_key skey`) but would need to use some access function to get its value. (c) Failing that, your question fails to provide a [mcve]. Where did you get `tomcrypt.h`?There is one in the [libtomcrypt](https://github.com/libtom/libtomcrypt.git) project, but it defines `symmetric_key` as a union. – Eric Postpischil Sep 22 '19 at 09:56

1 Answers1

0

If I understand correctly you're confusing the key you need to encrypt/decrypt your file and the "key" that is used internally by the library to do what you want.

The key that you want to store is the one that is passed as first argument to aes_setup(), whereas the instance of a symmetric_key is not important.

See the examples in e.g. the AES tests aes.c:647 on how rijndael_setup() is used (rijndael_setup() is the same as aes_setup()).

jaeckel
  • 36
  • 1