Hi I am working on a stackable filesystem WrapFS and trying to implement AES-GCM cipher on it to encrypt the data stored in the underlying file system. I am having major troubles in handling the GCM mode for AES at kernel level. However I was successful in implementing AES in CTR mode and the code for the same is attached below. Can someone point me in the right direction as to how to modify it to AES-GCM ?
I looked at this link, but that didn't help much. I am looking for two things: 1.) How to store authentication tags alongside encrypted data in files written and 2.) How to handle IV of AES-GCM by sending to and receiving from lower filesystem.
Thanks
int decrypt_encrypt_page(struct page *src_page,
struct page *dst_page,
char *key,
int key_len,
int encrypt)
{
int ret = 0;
struct scatterlist src_sg, dst_sg;
struct crypto_blkcipher *tfm;
struct blkcipher_desc desc;
sg_init_table(&src_sg, 1);
sg_init_table(&dst_sg, 1);
sg_set_page(&src_sg, src_page, PAGE_SIZE, 0);
sg_set_page(&dst_sg, dst_page, PAGE_SIZE, 0);
tfm = crypto_alloc_blkcipher(default_algo, 0, 0);
if (IS_ERR(tfm)) {
printk(KERN_ERR "failed to load transform for %s: %ld\n",
default_algo,
PTR_ERR(tfm));
ret = IS_ERR(tfm);
goto out;
}
desc.tfm = tfm;
desc.flags = 0;
ret = crypto_blkcipher_setkey(tfm, key, key_len);
if (ret) {
printk(KERN_ERR "setkey() failed flags=%x\n",
crypto_blkcipher_get_flags(tfm));
goto out;
}
if (encrypt)
ret = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg, PAGE_SIZE);
else
ret = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg, PAGE_SIZE);
if (ret)
printk(KERN_INFO "Some error occured while encrypting.\n");
out:
crypto_free_blkcipher(tfm);
return ret;
}