5.4.x内核模块密码算法调用逻辑分析

本文详细解释了Linux内核中的加密API(如crypto_wait_req和completion机制),以及如何配合硬件加速器(如ccp_crypto_enqueue_request)进行操作,涉及任务调度、中断处理和完成事件的使用。还介绍了ccp引擎的工作流程,包括算法调用和任务队列管理。

一、以下是请求过程:

static inline int crypto_wait_req(int err, struct crypto_wait *wait)
{
   
   
	switch (err) {
   
   
	case -EINPROGRESS:
	case -EBUSY:
		wait_for_completion(&wait->completion);
		reinit_completion(&wait->completion);
		err = wait->err;
		break;
	};

	return err;
}

static inline void crypto_init_wait(struct crypto_wait *wait)
{
   
   
	init_completion(&wait->completion);
}

二、 completion 用法

通常wait_for_completion需要completion唤醒配合,如下:

#include <linux/completion.h>

static struct completion my_completion;                                            // 1. 定义完成事件

static int my_thread_function(void *data) {
   
   
    pr_info("Thread started...\n");
    msleep(5000); // 模拟一个长时间的操作
    pr_info("Operation completed!\n");

    complete(&my_completion);                                                   // 3. 标记完成事件
    return 0;
}

static int __init my_init(void) {
   
   
    struct task_struct *thread;

    pr_info("Module init...\n");

    // 初始化完成事件
    init_completion(&my_completion);

    // 创建一个内核线程
    thread = kthread_run(my_thread_function, NULL, "my_thread");
    if (IS_ERR(thread)) {
   
   
        pr_err("Failed to create thread.\n");
        return PTR_ERR(thread);
    }

    // 等待完成事件
    wait_for_completion(&my_completion);                   //2. 阻塞当前线程,直到完成事件被标记为已完成。
    pr_info("Main thread: Operation completed!\n");

    return 0;
}

三、 set_callback

static inline void ahash_request_set_callback(struct ahash_request *req,
					      u32 flags,
					      crypto_completion_t compl,
					      void *data)
{
   
   
	req->base.complete = compl;
	req->base.data = data;
	req->base.flags = flags;
}

四、crypto_ahash_update

硬件加速请求。

+static int ccp_do_sm3_update(struct 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值