GCM mode and GCRY_CIPHER_AES256
David Dahl
ddahl at nulltxt.se
Fri May 30 21:02:10 CEST 2014
Here is the code:
-----------------
#include "pbkdf2.h"
#define GCRY_CIPHER GCRY_CIPHER_AES256
#define GCRY_CIPHER_MODE GCRY_CIPHER_MODE_GCM
#define RNDM_BYTES_LENGTH 32
#define SALT_LENGTH 16
int wrapKeyItem (char* privateKey, struct keyItem key,
unsigned char name, struct wrappedKeyItem* out)
{
size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER);
size_t privateKeyLength = strlen(privateKey) + 1;
char *encBuffer = malloc(privateKeyLength);
gcry_error_t err = 0;
// Create a handle
gcry_cipher_hd_t handle;
err = gcry_cipher_open(&handle, GCRY_CIPHER, GCRY_CIPHER_MODE, 0);
if (!handle) {
printf("GCM algo %d, gcry_cipher_open failed: %s\n",
GCRY_CIPHER, gpg_strerror (err));
printf("Error no: %d and message: %s\n ", err, gcry_strerror(err));
return 1;
}
// Set the key
err = gcry_cipher_setkey(handle,
key.key,
keyLength);
if (err) {
printf("gcry_cipher_setkey failed.");
printf("Error no: %d and message: %s\n ", err, gcry_strerror(err));
return 1;
}
// set the IV
unsigned int *iv;
iv = gcry_random_bytes(RNDM_BYTES_LENGTH, GCRY_STRONG_RANDOM);
err = gcry_cipher_setiv(handle, iv, blkLength);
if (err) {
printf("gcry_cipher_setiv failed.");
printf("Error no: %d and message: %s\n ", err, gcry_strerror(err));
return 1;
}
// Do encrypt
err = gcry_cipher_encrypt(handle, encBuffer,
privateKeyLength, privateKey,
privateKeyLength);
if (err) {
printf("gcry_cipher_encrypt failed.");
printf("Error no: %d and message: %s\n ", err, gcry_strerror(err));
return 1;
}
printf("encBuffer: %s\n ", encBuffer);
out->ciphertext = encBuffer;
out->iv = iv;
out->name = name;
// Free memory
gcry_cipher_close(handle);
free(encBuffer);
return 0;
}
On 05/30/2014 12:38 PM, Werner Koch wrote:
> On Fri, 30 May 2014 01:23, ddahl at nulltxt.se said:
>
>> err = gcry_cipher_open(&handle, GCRY_CIPHER, GCRY_CIPHER_MODE, 0);
>>
>> returns with: "GCM algo 9, gcry_cipher_open failed: Invalid cipher mode"
>
> Please paste the actual code so that we can understand and replicate the
> case.
>
More information about the Gcrypt-devel
mailing list