lun*_*man 5 c linux kernel cryptography aes
我需要与 Solaris 加密机制 SUN_CKM_AES_CCM 兼容。在 Linux 中,我相信我应该设置一个 AEAD 请求来获取“ccm(aes)”机械。Linux Crypto 的文档似乎相当糟糕,最好的例子似乎是 tcrypt.c 测试和内核源代码。
在 Solaris 上,我对 512 字节的块进行了测试加密,其中包含 16 字节的 hmac 和 12 字节的 iv。这需要保持不变,希望结果是相同的。
但是,我认为应该有效的方法无效;
struct crypto_aead *tfm = NULL;
struct aead_request *req;
unsigned char key[16] = {
0x5c, 0x95, 0x64, 0x42, 0x00, 0x82, 0x1c, 0x9e,
0xd4, 0xac, 0x01, 0x83, 0xc4, 0x9c, 0x14, 0x97
};
unsigned int ivsize;
int ret;
struct scatterlist plaintext[1];
struct scatterlist ciphertext[1];
struct scatterlist hmactext[1];
unsigned char *plaindata = NULL;
unsigned char *cipherdata = NULL;
unsigned char *hmacdata = NULL;
unsigned char *ivp = NULL;
int i;
unsigned char d;
struct tcrypt_result result;
tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
init_completion(&result.completion);
req = aead_request_alloc(tfm, GFP_KERNEL);
aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
cipher_work_done, &result);
crypto_aead_clear_flags(tfm, ~0);
ret = crypto_aead_setkey(tfm, key, sizeof(key));
ret = crypto_aead_setauthsize(tfm, 16); // authsize is hmac?
ivsize = crypto_aead_ivsize(tfm);
if (ivsize != 12) {
printk("ivsize is not 12 %d - this needs to be fixed\n", ivsize);
}
plaindata = kmalloc(512, GFP_KERNEL);
cipherdata = kmalloc(512, GFP_KERNEL);
hmacdata = kmalloc(16, GFP_KERNEL);
ivp = kmalloc(ivsize, GFP_KERNEL);
if (!plaindata || !cipherdata || !hmacdata || !ivp) goto out;
// put 00 01 02 03 ... in the input buffer...
for (i = 0, d = 0; i < 512; i++, d++)
plaindata[i] = d;
memset(cipherdata, 0, 512);
memset(hmacdata, 0, 16);
memset(ivp, 0, ivsize);
// Put a8 a9 aa .... in iv
for (i = 0,d=0xa8; i < 12; i++, d++)
ivp[i] = d;
sg_init_one(&plaintext[0], plaindata, 512);
sg_init_one(&ciphertext[0], cipherdata, 512);
sg_init_one(&hmactext[0], hmacdata, 16);
aead_request_set_crypt(req, plaintext, ciphertext, 512, ivp);
aead_request_set_assoc(req, hmactext, 16);
ret = crypto_aead_encrypt(req);
printk("cipher call returns %d \n", ret);
Run Code Online (Sandbox Code Playgroud)
我们得到的结果是 ivsize 是 16(我认为无法将其设置为 12),并且加密失败并显示“-22”或 EINVAL。代码中有很多错误检查,在这里删除,确认所有先前的调用返回成功。
据我所知,我非常接近 tcrypt.c 来源。但是,我想知道强制ivsize = 16是否意味着我无论如何都不能使用提供的算法。除此之外,很高兴看到加密调用成功以及密码数据输出中的内容。
代码被放入内核模块,并在 _init() 时间运行。最初我使用 blkcipher "aes",它有效,但不是 ccm-aes 变体。这让我改用 aead,但我无法开始工作。
好的,这就是我学到的。
1)让我们调用应用程序的 iv nonce。让我们调用内部加密的 iv iv。事实证明,Solaris 代码正在使用nonce-len=12,但 CCM-AES 算法仍然使用iv-len=16.
来自 Solaris 内核源代码,iv由:
iv[0] = 1..7, based on ivlen 16 - noncelen 12 = 2.
iv[1] = the nonce data (12 bytes).
iv[14] = 0
iv[15] = 1
Run Code Online (Sandbox Code Playgroud)
因此,在 Linux 上,我想要使用 ivlen 16 的“ccm(aes)”,并正确准备ivfrom nonce。
2)crypto_aead_encrypt()调用时aead_request_set_assoc()忽略前面的调用,将 HMAC 放在密码缓冲区的末尾。就我而言,在密文 [512] 处,为 16 个字节。所以输入的长度需要为 +16。
使用分散列表,如果设置正确,“最后”的 HMAC 可能会有所不同。
3)调用crypto_aead_decrypt()cryptlen时应为+16(cipherinputlen + maclen)。MAC 从输入缓冲区的末尾读取,即 16 字节的密文[512]。这也可以是使用 scatterlist 的单独缓冲区。
4)
crypto_aead_setauthsize()检查给定的 len 是否正确,然后不做任何处理。不要认为这实际上设置了大小!
5)aead_request_set_assoc()必须设置,即使它只是一个零缓冲区。
| 归档时间: |
|
| 查看次数: |
2637 次 |
| 最近记录: |