Files for running aes only from Polarssl

786 views Asked by At

I am trying to use only aes in my program. I have copied the files

  1. config.h
  2. aes.h
  3. havege.h

to the folder polarssl. But when I run the program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "polarssl/aes.h"
#include "polarssl/havege.h"
int main()
{
    char buff[2][64] = {"ABCDEFGHIJKLMN", ""};
    havege_state hs;
    int retval;
    unsigned char IV[16];
    unsigned char IV2[16];
    unsigned char key[32];

    aes_context enc_ctx;
    aes_context dec_ctx;

    havege_init(&hs);
    havege_random(&hs, IV, 16);
    havege_random(&hs, key, 32);
    strncpy(IV, IV2, 16);           //copy IV

    aes_setkey_enc(&enc_ctx, key, 256);
    aes_setkey_dec(&dec_ctx, key, 256);


    //encrypt
    aes_crypt_cbc(&enc_ctx, AES_ENCRYPT, 64, IV, buff[0], buff[1]);
    printf("Before encrypt:%s\n", buff[0]);

    //decrypt
    aes_crypt_cbc(&dec_ctx, AES_DECRYPT, 64, IV2, buff[1],buff[0]);
    printf("After decrypt:%s\n", buff[0]);
    return 0;
}

I am getting the error

In function `main':
ex.c:(.text+0x68): undefined reference to `havege_init'
ex.c:(.text+0x86): undefined reference to `havege_random'
ex.c:(.text+0xa4): undefined reference to `havege_random'
ex.c:(.text+0xe0): undefined reference to `aes_setkey_enc'
ex.c:(.text+0xfe): undefined reference to `aes_setkey_dec'
ex.c:(.text+0x133): undefined reference to `aes_crypt_cbc'
ex.c:(.text+0x17e): undefined reference to `aes_crypt_cbc'
collect2: error: ld returned 1 exit status
2

There are 2 answers

0
David R. On

Next to the header files, you will also need the .c files! (aes.c, havege.c) and compile those in your code.

On the implementation side: * Are you sure you want to use HAVEGE? There are lots of doubts on its effectiveness (depending on the system you run on), the standardized CTR-DRBG seems to be a much better choice..

0
Timmay On

I think your error has something to do with linking to the Aes and Havege files. Your compiler is not recognizing them! Are they in the same folder as your main? If they are in the same folder, then remove the "polarssl/" from the headerfile name at the top.

Or maybe, when compiling be sure to include aes.c and aes.h too. I found that I was getting the same error due to this. I was only including aes.h in compiling. Example

$terminal: gcc main.c aes.h aes.c -o encrypt

Just wondering? If you want to use only aes, why are you trying to use havege.h?