Linux 内核加密 API:“crypto_alloc_skcipher”未找到 skcipher 算法名称

问题描述

我正在尝试使用加密 API 制作 Linux 内核驱动程序。

所以首先我有我自己开发的 skcipher 算法,我在加密 API 上成功注册,我可以在注册良好的加密列表中看到它。

.base = {

/* Name used by the framework to find who is implementing what. */
    .cra_name = "cbc(aes)stackOverFlow",/* Driver name. Can be used to request a specific implementation of an algorithm. */
    .cra_driver_name = "stackOverFlow-cbc-aes",/* Priority is used when implementation auto-selection takes place:
* if there are several implementers,the one with the highest priority is chosen.
* By convention: HW engine > ASM/arch-optimized > plain C 
* */
    .cra_priority = 300,/* Driver module */
    .cra_module = THIS_MODULE,/* Size of the data blocks this algo operates on. */
    .cra_blocksize = AES_BLOCK_SIZE,.cra_flags = CRYPTO_ALG_INTERNAL | CRYPTO_ALG_TYPE_SKCIPHER,/* Size of the context attached to an algorithm instance. 
* This value informs the kernel crypto API about the memory size
* needed to be allocated for the transformation context.
*/
    .cra_ctxsize = sizeof(struct crypto_aes_ctx),/* Alignment mask for the input and output data buffer. */
    .cra_alignmask = 15,},/* constructor/destructor methods called every time an alg instance is created/destroyed. */
    .min_keysize = AES_MIN_KEY_SIZE,.max_keysize = AES_MAX_KEY_SIZE,.ivsize     = AES_BLOCK_SIZE,.init = test_skcipher_cra_init,.exit = test_skcipher_cra_exit,.setkey = test_aes_setkey,.encrypt = test_cbc_aes_encrypt,.decrypt = test_cbc_aes_decrypt,};

这是我的初始化功能模块:

static int __init test_skcipher_cra_init(struct crypto_skcipher *tfm){

int ret;

ret = crypto_register_skcipher(&test_cbc_aes_alg);
if (ret < 0){
    printk(KERN_ALERT "register Failed %d",ret);
}
    
else{
    printk(KERN_INFO "SUCCESS crypto_register\n");

}

return ret;

}

为了确保我的驱动程序正常工作,我使用实现用户代码(我从链接中获得)来加密一些数据:https://www.kernel.org/doc/html/v4.17/crypto/api-samples.html

但是,当我编译所有内容并查看内核日志消息时,我收到一条错误消息“无法分配 skcipher 句柄”,该消息来自部分实现代码

skcipher = crypto_alloc_skcipher("stackOverFlow-cbc-aes",0);
    if (IS_ERR(skcipher)) {
        pr_info("Could not allocate skcipher handle\n");
        return PTR_ERR(skcipher);
    }

但是在加密 API 中,我可以看到驱动程序:

name         : cbc(aes)stackOverFlow
driver       : stackOverFlow-cbc-aes
module       : kernel
priority     : 300
refcnt       : 1
selftest     : passed
internal     : yes
type         : skcipher
async        : no
blocksize    : 16
min keysize  : 16
max keysize  : 32
ivsize       : 16
chunksize    : 16
walksize     : 16

我真的多次尝试修改算法中的标志和其他内容,但我不明白它一直向我显示此消息。所以我的问题是为什么它给我这个错误并且我的加密驱动程序已经在加密 API 上注册

请注意,当我将名称更改为 crypto_alloc_skcipher("cbc-aes-aesni",0) 这是 API 中已存在的名称之一时,一切正常。

解决方法

我设法解决了这个问题,这是一个愚蠢的错误,因为我把 Init 算法函数与 Init 函数模块混淆了。