I have a problem encrypting/decrypting a simple 16 byte message in Libgcrypt while using CCM mode of operation and AES algorithm. In the documentation of Libgcrypt I cannot find which parameters to set for CCM (should I set IV or counter?).
I am stuck with the following code:
gcry_error_t err;
gcry_cipher_hd_t hd;
char * key = "1234567890123456";
char * plainText = "VNiJkPzAWPFm1234";
size_t messageSize = strlen(plainText);
char * cipherText = malloc(messageSize);
char * recoveredText = malloc(messageSize);
err = gcry_cipher_open(
&hd,
GCRY_CIPHER_AES128,
GCRY_CIPHER_MODE_CCM,
0);
err = gcry_cipher_setkey(hd, key, 16);
/* What to do here? */
err = gcry_cipher_encrypt(
hd,
cipherText,
messageSize,
plainText,
messageSize);
err = gcry_cipher_decrypt(
hd,
recoveredText,
messageSize,
cipherText,
messageSize);
How do I perform simple encryption/decryption using AES128 and CCM in Libgcrypt?
Example
The example is without additional non-encrypted data, therefore param[1] of gcry_cipher_ctl setting the GCRYCTL_SET_CCM_LENGTHS is 0. But it could be easily adjusted if required.
The functionality is divided into three parts:
If you run it, it will output the following text to the console:
Code