Encryption GCM-AEAD对运行linux内核3.10的ubuntu系统的支持
我正在尝试使用GCM加密实现用于加密的AEAD示例代码。但是我在设置键时总是出现无效参数错误Encryption GCM-AEAD对运行linux内核3.10的ubuntu系统的支持,encryption,linux-kernel,cryptography,aes-gcm,Encryption,Linux Kernel,Cryptography,Aes Gcm,我正在尝试使用GCM加密实现用于加密的AEAD示例代码。但是我在设置键时总是出现无效参数错误 static int init_aead(void) { printk("Starting encryption\n"); struct crypto_aead *tfm = NULL; struct aead_request *req; struct tcrypt_result tresult; struct s
static int init_aead(void)
{
printk("Starting encryption\n");
struct crypto_aead *tfm = NULL;
struct aead_request *req;
struct tcrypt_result tresult;
struct scatterlist plaintext[1] ;
struct scatterlist ciphertext[1];
struct scatterlist gmactext[1];
unsigned char *plaindata = NULL;
unsigned char *cipherdata = NULL;
unsigned char *gmacdata = NULL;
const u8 *key = kmalloc(16, GFP_KERNEL);
char *algo = "rfc4106(gcm(aes))";
unsigned char *ivp = NULL;
int ret, i, d;
unsigned int iv_len;
unsigned int keylen = 16;
/* Allocating a cipher handle for AEAD */
tfm = crypto_alloc_aead(algo, 0, 0);
init_completion(&tresult.completion);
if(IS_ERR(tfm)) {
pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
PTR_ERR(tfm));
return PTR_ERR(tfm);
}
/* Allocating request data structure to be used with AEAD data structure */
req = aead_request_alloc(tfm, GFP_KERNEL);
if(IS_ERR(req)) {
pr_err("Couldn't allocate request handle for %s:\n", algo);
return PTR_ERR(req);
}
/* Allocting a callback function to be used , when the request completes */
aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, aead_work_done,&tresult);
crypto_aead_clear_flags(tfm, ~0);
/* Set key */
get_random_bytes((void*)key, keylen);
if((ret = crypto_aead_setkey(tfm, key, 16) != 0)) {
pr_err("Return value for setkey is %d\n", ret);
pr_info("key could not be set\n");
ret = -EAGAIN;
return ret;
}
/* Set authentication tag length */
if(crypto_aead_setauthsize(tfm, 16)) {
pr_info("Tag size could not be authenticated\n");
ret = -EAGAIN;
return ret;
}
/* Set IV size */
iv_len = crypto_aead_ivsize(tfm);
if (!(iv_len)){
pr_info("IV size could not be authenticated\n");
ret = -EAGAIN;
return ret;
}
plaindata = kmalloc(16, GFP_KERNEL);
cipherdata = kmalloc(16, GFP_KERNEL);
gmacdata = kmalloc(16, GFP_KERNEL);
ivp = kmalloc(iv_len, GFP_KERNEL);
if(!plaindata || !cipherdata || !gmacdata || !ivp) {
printk("Memory not availaible\n");
ret = -ENOMEM;
return ret;
}
for (i = 0, d = 0; i < 16; i++, d++)
plaindata[i] = d;
memset(cipherdata, 0, 16);
memset(gmacdata, 0, 16);
for (i = 0,d=0xa8; i < 16; i++, d++)
ivp[i] = d;
sg_init_one(&plaintext[0], plaindata, 16);
sg_init_one(&ciphertext[0], cipherdata, 16);
sg_init_one(&gmactext[0], gmacdata, 128);
aead_request_set_crypt(req, plaintext, ciphertext, 16, ivp);
aead_request_set_assoc(req, gmactext, 16);
ret = crypto_aead_encrypt(req);
if (ret)
printk("cipher call returns %d \n", ret);
else
printk("Failure \n");
return 0;
}
module_init(init_aead);
module_exit(exit_aead);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("My code for aead encryption test");
}
是否由于块大小而引发无效参数错误。如果是这样,我该怎么做才能使它工作呢?AES的块大小实际上总是128位。不过,GCM的块大小是另一回事。顾名思义,GCM(伽罗瓦计数器模式)是建立在CTR(计数器)操作模式之上的,有时也称为SIC(分段整数计数器)操作模式。这将AES转换为流密码。根据定义,流密码的块大小为一个字节(更准确地说,是一位,但位级操作通常不受API支持) 但是,块大小与调用中显示的键大小几乎没有关系,而且参数似乎需要字节而不是位(通常定义键长度)
IV的大小应为12字节(默认值)。否则,GCM实现可能需要额外的计算(如果存在的话)。对于Aes GCM RFC 4106,密钥必须为20字节。我还不知道为什么。我已经查看了ipsec源代码,以了解如何在那里进行加密 你能检查一下上面的评论是否有助于你解决这个问题吗?谢谢你的帮助。我还有一个疑问。blocksize:1是指定输入块大小的长度还是指定输出块大小的长度。根据最新主线内核中的crypto_aead_setkey定义,“为AES密码句柄提供16字节密钥时,执行AES-128”[.如果块大小是输入数据大小,即1字节,我们提供16字节的密钥,它会工作吗?如果不是这样,我会得到一个-EINVAL错误eror的原因是什么?块大小和密钥大小彼此没有直接关系。但输入和输出块大小始终相同。这取决于实现的字节数不过处理得很及时;不幸的是,我只剩下30分钟吃东西和准备皮艇了:)再次感谢:)。你能告诉我我做错了什么,我该怎么做才能让它工作吗?
name : rfc4106(gcm(aes))
driver : rfc4106-gcm-aesni
module : aesni_intel
priority : 400
refcnt : 1
selftest : passed
type : nivaead
async : yes
blocksize : 1
ivsize : 8
maxauthsize : 16
geniv : seqiv