I am trying to implement an AES 128 encryption algorithm using gcrypt in Eclipse; however, I am getting nowhere fast. Below is the code I am using, which I got from another thread.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <gcrypt.h>
static void aesTest(int gcry_mode, char * iniVector)
{
#define GCRY_CIPHER GCRY_CIPHER_AES128 // Pick the cipher here
gcry_error_t gcryError;
gcry_cipher_hd_t gcryCipherHd;
size_t index;
size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER);
char * txtBuffer = "123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ";
size_t txtLength = strlen(txtBuffer)+1; // string plus termination
char * encBuffer = malloc(txtLength);
char * outBuffer = malloc(txtLength);
char * aesSymKey = "one test AES key"; // 16 bytes
gcryError = gcry_cipher_open(
&gcryCipherHd, // gcry_cipher_hd_t *
GCRY_CIPHER, // int
gcry_mode, // int
0); // unsigned int
if (gcryError)
{
printf("gcry_cipher_open failed: %s/%s\n",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
gcryError = gcry_cipher_setkey(gcryCipherHd, aesSymKey, keyLength);
if (gcryError)
{
printf("gcry_cipher_setkey failed: %s/%s\n",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
gcryError = gcry_cipher_setiv(gcryCipherHd, iniVector, blkLength);
if (gcryError)
{
printf("gcry_cipher_setiv failed: %s/%s\n",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
gcryError = gcry_cipher_encrypt(
gcryCipherHd, // gcry_cipher_hd_t
encBuffer, // void *
txtLength, // size_t
txtBuffer, // const void *
txtLength); // size_t
if (gcryError)
{
printf("gcry_cipher_encrypt failed: %s/%s\n",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
gcryError = gcry_cipher_setiv(gcryCipherHd, iniVector, blkLength);
if (gcryError)
{
printf("gcry_cipher_setiv failed: %s/%s\n",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
gcryError = gcry_cipher_decrypt(
gcryCipherHd, // gcry_cipher_hd_t
outBuffer, // void *
txtLength, // size_t
encBuffer, // const void *
txtLength); // size_t
if (gcryError)
{
printf("gcry_cipher_decrypt failed: %s/%s\n",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_mode = %s\n", gcry_mode == GCRY_CIPHER_MODE_ECB ? "ECB" : "CBC");
printf("keyLength = %d\n", keyLength);
printf("blkLength = %d\n", blkLength);
printf("txtLength = %d\n", txtLength);
printf("aesSymKey = %s\n", aesSymKey);
printf("iniVector = %s\n", iniVector);
printf("txtBuffer = %s\n", txtBuffer);
printf("encBuffer = ");
for (index = 0; index<txtLength; index++)
printf("%02X", (unsigned char)encBuffer[index]);
printf("\n");
printf("outBuffer = %s\n", outBuffer);
// clean up after ourselves
gcry_cipher_close(gcryCipherHd);
free(encBuffer);
free(outBuffer);
}
int main(void) {
aesTest(GCRY_CIPHER_MODE_ECB, "a test ini value");
aesTest(GCRY_CIPHER_MODE_ECB, "different value!");
aesTest(GCRY_CIPHER_MODE_CBC, "a test ini value");
aesTest(GCRY_CIPHER_MODE_CBC, "different value!");
return EXIT_SUCCESS;
}
When I try to build the above, I get the below errors/warnings. I have a feeling I might not being including the gcrypt library correctly - I added the libgcrypt directory to "Project Properties"/"C/C++ General"/"Paths and Symbols"/"Includes" - but after playing around with different ways to include the library, I kept getting similar results. Although it shouldn't matter, I'm running A MacBook with OS Lion.
Here is my console after building:
Code:
**** Build of configuration Debug for project aesTest ****
make all
Building file: ../src/aesTest.c
Invoking: GCC C Compiler
gcc -I"/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src" -I"/Applications/eclipse C++/Includes/libgcrypt-1.5.0/cipher" -I"/Applications/eclipse C++/Includes/libgpg-error-1.10/src" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/aesTest.d" -MT"src/aesTest.d" -o "src/aesTest.o" "../src/aesTest.c"
In file included from ../src/aesTest.c:13:
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1336: warning: 'gcry_ac_io_mode_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1337: warning: 'gcry_ac_io_type_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1344: warning: 'gcry_ac_data_read_cb_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1358: warning: 'gcry_ac_data_write_cb_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1393: warning: 'gcry_md_algo_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1401: warning: 'gcry_md_algo_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1407: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1411: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1415: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1416: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1421: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1425: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1433: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1440: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1448: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1456: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1463: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1470: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1470: warning: 'gcry_ac_io_mode_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1471: warning: 'gcry_ac_io_type_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1477: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1477: warning: 'gcry_ac_io_mode_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1478: warning: 'gcry_ac_io_type_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1482: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1483: warning: 'gcry_ac_id_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1487: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1491: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1491: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1492: warning: 'gcry_ac_key_type_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1492: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1500: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1502: warning: 'gcry_ac_key_pair_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1507: warning: 'gcry_ac_key_pair_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1508: warning: 'gcry_ac_key_type_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1512: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1516: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1516: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1520: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1521: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1526: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1526: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1531: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1535: warning: 'gcry_ac_key_pair_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1541: warning: 'gcry_ac_em_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1543: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1544: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1550: warning: 'gcry_ac_em_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1552: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1553: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1559: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1561: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1563: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1569: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1571: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1573: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1578: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1579: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1581: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1587: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1588: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1590: warning: 'gcry_ac_data_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1598: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1599: warning: 'gcry_ac_scheme_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1601: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1602: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1603: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1611: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1612: warning: 'gcry_ac_scheme_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1614: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1615: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1616: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1624: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1625: warning: 'gcry_ac_scheme_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1627: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1628: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1629: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1638: warning: 'gcry_ac_handle_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1639: warning: 'gcry_ac_scheme_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1641: warning: 'gcry_ac_key_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1642: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1643: warning: 'gcry_ac_io_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1649: warning: 'gcry_ac_id_t' is deprecated
/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src/gcrypt.h:1656: warning: 'gcry_ac_id_t' is deprecated
../src/aesTest.c: In function 'aesTest':
../src/aesTest.c:100: warning: format '%d' expects type 'int', but argument 2 has type 'size_t'
../src/aesTest.c:100: warning: format '%d' expects type 'int', but argument 2 has type 'size_t'
../src/aesTest.c:101: warning: format '%d' expects type 'int', but argument 2 has type 'size_t'
../src/aesTest.c:101: warning: format '%d' expects type 'int', but argument 2 has type 'size_t'
../src/aesTest.c:102: warning: format '%d' expects type 'int', but argument 2 has type 'size_t'
../src/aesTest.c:102: warning: format '%d' expects type 'int', but argument 2 has type 'size_t'
Finished building: ../src/aesTest.c
Building target: aesTest
Invoking: MacOS X C Linker
gcc -L"/Applications/eclipse C++/Includes/libgcrypt-1.5.0/src" -L"/Applications/eclipse C++/Includes/libgcrypt-1.5.0/cipher" -L"/Applications/eclipse C++/Includes/libgpg-error-1.10/src" -o "aesTest" ./src/aesTest.o
Undefined symbols for architecture x86_64:
"_gcry_cipher_get_algo_keylen", referenced from:
_aesTest in aesTest.o
"_gcry_cipher_get_algo_blklen", referenced from:
_aesTest in aesTest.o
"_gcry_cipher_open", referenced from:
_aesTest in aesTest.o
"_gcry_strerror", referenced from:
_aesTest in aesTest.o
"_gcry_strsource", referenced from:
_aesTest in aesTest.o
"_gcry_cipher_setkey", referenced from:
_aesTest in aesTest.o
"_gcry_cipher_setiv", referenced from:
_aesTest in aesTest.o
"_gcry_cipher_encrypt", referenced from:
_aesTest in aesTest.o
"_gcry_cipher_decrypt", referenced from:
_aesTest in aesTest.o
"_gcry_cipher_close", referenced from:
_aesTest in aesTest.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [aesTest] Error 1
**** Build Finished ****