LXR | KVM | PM | Time | Interrupt | Systems Performance | Bootup Optimization

Linux Crypto(4):框架核心层

Linux Crypto 核心框架层统一管理加密算法实现(软件/硬件),为内核和用户空间提供异步/同步加密接口,通过算法注册、会话管理(tfm)和请求调度(async_request)实现安全高效的数据加解密服务。

1 核心作用

1. 算法管理:统一注册/注销加密算法(如 AES、SHA256)。
2. 接口抽象:为上层(用户空间/内核模块)提供统一 API。
3. 异步处理:管理异步加密请求,提升并发性能。
4. 算法查找:按名称/类型动态查找算法实现。
5. 模板机制:支持组合算法(如 cbc(aes))。

2 核心数据结构

struct crypto_alg是加密算法的"能力描述符",描述算法的静态属性和操作集(所有实例共享)。
struct crypto_alg {
        struct list_head cra_list;       // 全局算法链表节点
        struct list_head cra_users;      // 使用此算法的tfm链表

        u32 cra_flags;                   // 算法标志(同步/异步等)
        unsigned int cra_blocksize;      // 块大小(如AES=16字节)
        unsigned int cra_ctxsize;        // 算法上下文所需内存大小
        unsigned int cra_alignmask;      // 内存对齐掩码要求

        int cra_priority;                // 算法优先级(值越大优先级越高)
        refcount_t cra_refcnt;           // 算法引用计数

        char cra_name[CRYPTO_MAX_ALG_NAME];       // 算法名称(如"aes")
        char cra_driver_name[CRYPTO_MAX_ALG_NAME];// 驱动实现名称

        const struct crypto_type *cra_type;       // 算法类型描述符

        union {                         // 算法操作函数集
                struct cipher_alg cipher;          // 对称加密操作集
                struct compress_alg compress;      // 压缩操作集
        } cra_u;

        int (*cra_init)(struct crypto_tfm *tfm);  // tfm初始化回调
        void (*cra_exit)(struct crypto_tfm *tfm); // tfm销毁回调
        void (*cra_destroy)(struct crypto_alg *alg); // 算法销毁回调

        struct module *cra_module;       // 所属内核模块
} CRYPTO_MINALIGN_ATTR; 

 struct crypto_tfm是算法实例的"运行时工作台",包含会话特定数据(每个用户独立)。

struct crypto_tfm {
        refcount_t refcnt;               // 引用计数器

        u32 crt_flags;                  // 运行时标志(如CRYPTO_TFM_REQ_MAY_SLEEP)

        int node;                        // NUMA节点ID(用于内存分配)

        void (*exit)(struct crypto_tfm *tfm); // 自定义销毁函数

        struct crypto_alg *__crt_alg;    // 指向关联的crypto_alg

        void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; // 算法上下文(存储密钥/状态等)
}; 

 struct crypto_async_request是异步加密请求的"任务单",描述单次加密操作及其上下文

struct crypto_async_request {
        struct list_head list;           // 请求队列链表节点
        crypto_completion_t complete;    // 异步操作完成回调函数
        void *data;                      // 请求数据(如scatterlist)
        struct crypto_tfm *tfm;          // 关联的tfm实例

        u32 flags;                       // 请求标志(如CRYPTO_TFM_REQ_MAY_BACKLOG)
}; 

 struct crypto_template是安全算法模板,用于动态生成组合算法(如 cbc(aes))。

struct crypto_template {  
        struct list_head list;           // 全局模板链表节点  
        char name[CRYPTO_MAX_ALG_NAME];  // 模板名称(如 "cbc")  
        struct module *module;           // 所属内核模块  
        int (*create)(struct crypto_template *tmpl,  
                      struct rtattr **tb); // 模板实例创建函数  
        struct list_head instances;     // 已创建的模板实例链表  
}

struct crypto_template是模板动态生成的算法实例(如 cbc(aes) 的具体实现)。

struct crypto_instance {  
        struct crypto_alg alg;          // 嵌入的算法描述符  
        struct crypto_template *tmpl;   // 所属模板  
        void *__ctx[] CRYPTO_MINALIGN_ATTR; // 私有上下文  
}

3 核心API

1. 算法注册/注销

int crypto_register_alg(struct crypto_alg *alg); -- 注册单个算法到全局链表。  
void crypto_unregister_alg(struct crypto_alg *alg); -- 注销算法。  

2. TFM 管理

struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 type, u32 mask); -- 创建 tfm 实例(已废弃,推荐类型专用 API)。  
void crypto_free_tfm(struct crypto_tfm *tfm); -- 销毁 tfm 并释放资源。  

3. 异步请求处理

void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen); -- 初始化异步请求队列。  
int crypto_enqueue_request(struct crypto_queue *queue, struct crypto_async_request *request); -- 提交异步请求。  
struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue); -- 取出待处理请求。  
void crypto_request_complete(struct crypto_async_request *req, int err); -- 触发完成回调(由驱动调用)。  

4. 算法查找

struct crypto_alg *crypto_find_alg(const char *alg_name, ...); -- 按名称查找算法实现。  

5. 同步封装

int crypto_skcipher_encrypt(struct skcipher_request *req); -- 同步加密(阻塞直到完成)。  
int crypto_skcipher_decrypt(struct skcipher_request *req); -- 同步解密。  

6. 模板注册/注销

int crypto_register_template(struct crypto_template *tmpl);-- 注册模板到全局系统(如注册 CBC 模式模板)  

void crypto_unregister_template(struct crypto_template *tmpl);-- 注销模板及其生成的所有算法实例  
7. 模板实例创建 (在模板的 create() 回调中实现)
int crypto_register_instance(struct crypto_template *tmpl,  
                             struct crypto_instance *inst);  -- 将模板创建的算法实例(struct crypto_instance)注册为核心算法  
 

posted on 2025-07-23 23:59  ArnoldLu  阅读(137)  评论(0)    收藏  举报

导航