【问题标题】:Linux Kernel Crypto API : skcipher algorithm name not found by "crypto_alloc_skcipher"Linux 内核加密 API:“crypto_alloc_skcipher”未找到 skcipher 算法名称
【发布时间】:2021-07-31 18:39:01
【问题描述】:

我正在尝试使用加密 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, 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 上注册了?

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

【问题讨论】:

    标签: c linux cryptography driver cryptoapi


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-01-22
      • 2023-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多