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

Linux Kernel Crypto API : skcipher algorithm name not found by "crypto_alloc_skcipher"

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

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

.base = {

/* Name used by the framework to find who is implementing what. */
    .cra_name = "cbc(aes)Whosebug",

/* Driver name. Can be used to request a specific implementation of an algorithm. */
    .cra_driver_name = "Whosebug-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;

}

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

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

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

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

name         : cbc(aes)Whosebug
driver       : Whosebug-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, 0) 这是 API 中已经存在的名称之一时,一切正常.

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