参考链接
crypto_larval
struct crypto_larval {
struct crypto_alg alg;
struct crypto_alg *adult;
struct completion completion;
u32 mask;
};
- 结构体名叫 crypto_larval (算法幼虫)
- internal.h - crypto/internal.h - Linux source code (v5.15.12) - Bootlin
- 数据成员 细分
- alg:算法幼虫对应的通用算法说明;
- adult:算法幼虫对应的算法成虫,即待注册或待检验的算法;
- completion:算法幼虫对应的完成量,用于在注册或检验过程中进行线程同步;struct completion - 用于维护“完成”状态的结构:这是用于维护“完成”状态的不透明结构。完成当前使用 FIFO 来排队必须等待的线程“完成”事件。
- mask:算法类型屏蔽字。
- 在加密框架中每种算法都对应一个通用算法说明,需要由算法管理链表管理,算法幼虫是一种特殊的算法,不“成熟”,无法提供算法服务,因此其通用算法说明只是用来表示算法幼虫的属性(如算法名,算法类型等),并不提供算法接口。
- 在加密框架中,算法动态注册和算法正确性检验都是由专门的内核线程实现的,与执行算法查找或算法注册的内核线程之间通过完成量机制实现线程间同步。
completion
/*
* struct completion - structure used to maintain state for a "completion"
*
* This is the opaque structure used to maintain the state for a "completion".
* Completions currently use a FIFO to queue threads that have to wait for
* the "completion" event.
*
* See also: complete(), wait_for_completion() (and friends _timeout,
* _interruptible, _interruptible_timeout, and _killable), init_completion(),
* reinit_completion(), and macros DECLARE_COMPLETION(),
* DECLARE_COMPLETION_ONSTACK().
*/
struct completion {
unsigned int done;
struct swait_queue_head wait;
};
crypto_larval_alloc
- api.c - crypto/api.c - Linux source code (v5.15.12) - Bootlin
- crypto_larval_alloc函数输入参数包括算法名name、算法类型type和算法类型屏蔽字mask,实现根据输入参数创建同名的算法幼虫larval的功能。
struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask)
{
struct crypto_larval *larval;
larval = kzalloc(sizeof(*larval), GFP_KERNEL);
if (!larval)
return ERR_PTR(-ENOMEM);
larval->mask = mask;
larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
larval->alg.cra_priority = -1;
larval->alg.cra_destroy = crypto_larval_destroy;
strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
init_completion(&larval->completion);
return larval;
}
EXPORT_SYMBOL_GPL(crypto_larval_alloc);
- 在crypto_larval_alloc函数中对新创建的算法幼虫的初始化如下所示
在算法幼虫初始化时,需要注意以下几点:
- a)算法幼虫与算法同名;
- b)算法幼虫的算法类型设置CRYPTO_ALG_LARVAL;
- c)算法幼虫的算法优先级为-1;
- d)算法幼虫设置了实例销毁接口crypto_larval_destroy。
- 将crypto_larval_alloc函数创建的算法幼虫称为通用算法幼虫,实际上就是注册用算法幼虫。
- completion.h - include/linux/completion.h - Linux source code (v5.15.12) - Bootlin
- init_completion
/**
* init_completion - Initialize a dynamically allocated completion
* @x: pointer to completion structure that is to be initialized
*
* This inline function will initialize a dynamically created completion
* structure.
*/
static inline void init_completion(struct completion *x)
{
x->done = 0;
init_swait_queue_head(&x->wait);
}
- init_await_queue_head()
- swait.h - include/linux/swait.h - Linux source code (v5.15.12) - Bootlin
__crypto_register_alg
- 在__crypto_register_alg函数中,再调用crypto_larval_alloc函数创建通用算法幼虫基础上,再进行如下设置,设置之后的算法幼虫称为检测用算法幼虫。
与通用算法幼虫(即注册用算法幼虫)相比,检测用算法幼虫增加了以下属性:
- a)检验用算法幼虫设置了对应的算法成虫; larval->adult = crypto_mod_get(alg);
- b)检验用算法幼虫设置了对应的算法驱动名;memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,CRYPTO_MAX_ALG_NAME);
- c)检验用算法幼虫继承了算法成功的算法优先级。larval->alg.cra_priority = alg->cra_priority;
- 没有修改引用计数吗?refcount_set(&larval->alg.cra_refcnt, 1);
- 在加密框架中,用是否设置了算法驱动名来区分注册用算法幼虫和检测用算法幼虫,由内联函数crypto_is_test_larval实现,如下所示。
crypto_is_test_larval函数
static inline int crypto_is_test_larval(struct crypto_larval *larval)
{
return larval->alg.cra_driver_name[0];
}
crypto_larval_add函数
- api.c - crypto/api.c - Linux source code (v5.15.12) - Bootlin
- crypto_larval_add函数输入参数包括算法名name、算法类型type和算法类型屏蔽字mask
static struct crypto_alg *crypto_larval_add(const char *name, u32 type,
u32 mask)
{
struct crypto_alg *alg;
struct crypto_larval *larval;
larval = crypto_larval_alloc(name, type, mask);
if (IS_ERR(larval))
return ERR_CAST(larval);
refcount_set(&larval->alg.cra_refcnt, 2);
down_write(&crypto_alg_sem);
alg = __crypto_alg_lookup(name, type, mask);
if (!alg) {
alg = &larval->alg;
list_add(&alg->cra_list, &crypto_alg_list);
}
up_write(&crypto_alg_sem);
if (alg != &larval->alg) {
kfree(larval);
if (crypto_is_larval(alg))
alg = crypto_larval_wait(alg);
}
return alg;
}
-
处理流程如下所示
- 如上所示,crypto_larval_add函数并不是简单调用crypto_larval_alloc函数创建注册用算法幼虫larval,在返回前要调用__crypto_alg_lookup函数再次查找是否有符合条件的算法alg,根据查找结果确认返回值。
- 算法幼虫larval和算法alg在数据结构上的共同点为通用算法说明,因此crypto_larval_add函数的返回值是通用算法说明,而调用者通过算法类型判断某个通用算法说明是否对应一个算法幼虫,由内联函数crypto_is_larval函数实现
crypto_is_larval
- 判断某个通用算法说明是否对应一个算法幼虫,由内联函数crypto_is_larval函数实现
- internal.h - crypto/internal.h - Linux source code (v5.15.12) - Bootlin
static inline int crypto_is_larval(struct crypto_alg *alg)
{
return alg->cra_flags & CRYPTO_ALG_LARVAL;
}
3)crypto_larval_wait函数
- crypto_larval_wait函数用于等待算法检测(即算法注册)完成,通过完成量实现与算法检测线程之间的同步,其完成量是带超时(60s)可中断的
- api.c - crypto/api.c - Linux source code (v5.15.12) - Bootlin
static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
{
struct crypto_larval *larval = (void *)alg;
long timeout;
timeout = wait_for_completion_killable_timeout(
&larval->completion, 60 * HZ);
alg = larval->adult;
if (timeout < 0)
alg = ERR_PTR(-EINTR);
else if (!timeout)
alg = ERR_PTR(-ETIMEDOUT);
else if (!alg)
alg = ERR_PTR(-ENOENT);
else if (IS_ERR(alg))
;
else if (crypto_is_test_larval(larval) &&
!(alg->cra_flags & CRYPTO_ALG_TESTED))
alg = ERR_PTR(-EAGAIN);
else if (!crypto_mod_get(alg))
alg = ERR_PTR(-EAGAIN);
crypto_mod_put(&larval->alg);
return alg;
}
- 超时中断,如下所示
timeout = wait_for_completion_interruptible_timeout(
&larval->completion, 60 * HZ);
- crypto_larval_wait函数只是简单地等待算法检测完成,而算法检测结果还需要通过其他条件进行综合判断,包括完成量返回的剩余时间timeout、算法幼虫关联的算法成虫alg(=larval->adult)
- 检测条件为 : 完成量返回的剩余时间timeout 和 算法幼虫关联的算法成虫alg
具体规则如下:
- i.根据timeout值判断线程同步结果
- timeout<0说明等待被外部信号中断,返回EINTR错误
- timeout=0说明等待算法注册超时,返回ETIMEDOUT
-
if (timeout < 0) alg = ERR_PTR(-EINTR); else if (!timeout) alg = ERR_PTR(-ETIMEDOUT);
-
ii.timeout>0说明算法注册在超时时间内完成,但是注册完成并不代表注册成功,还需要进一步判断
- 例如注册用算法幼虫关联的算法成功alg无效说明算法注册失败,返回ENOENT(即没有这个算法)错误;
- 如果算法幼虫为检验用算法幼虫,但算法未经过检验,返回EAGAIN错误,允许重新尝试算法注册流程,如下所示。
-
if (!alg) alg = ERR_PTR(-ENOENT); else if (crypto_is_test_larval(larval) && !(alg->cra_flags & CRYPTO_ALG_TESTED)) alg = ERR_PTR(-EAGAIN);
- iii.以上条件均不满足时,说明算法已成功完成注册,返回注册好的算法alg。
4)crypto_larval_kill函数
- 算法注册成功,同时也意味着算法幼虫使用的终结,注册线程将调用crypto_larval_kill函数“杀死”算法幼虫。
- api.c - crypto/api.c - Linux source code (v5.15.12) - Bootlin
void crypto_larval_kill(struct crypto_alg *alg)
{
struct crypto_larval *larval = (void *)alg;
down_write(&crypto_alg_sem);
list_del(&alg->cra_list);
up_write(&crypto_alg_sem);
complete_all(&larval->completion);
crypto_alg_put(alg);
}
EXPORT_SYMBOL_GPL(crypto_larval_kill);
具体操作流程
- 首先从算法管理链表中删除算法幼虫,使之变为孤儿; list_del(&alg->cra_list);
- 然后唤醒在算法幼虫完成量上等待的所有线程,使之变得无用;complete_all(&larval->completion);
- 最后释放算法幼虫的占有权,彻底判处其死刑。crypto_alg_put(alg);
- 内联函数crypto_alg_put用于释放算法alg(包括算法幼虫)的占有权
crypto_alg_put
static inline void crypto_alg_put(struct crypto_alg *alg)
{
if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
alg->cra_destroy(alg);
}
- 释放算法占有权时,如果算法定义了cra_destroy接口,将调用其进行进一步处理,而在创建算法幼虫(crypto_larval_alloc函数)时将算法幼虫的cra_destroy接口设置为crypto_larval_destroy函数。
- crypto_larval_destroy函数将先释放对算法成虫adult(如果有)的占用权,最终释放算法幼虫占用的内存空间,这样算法幼虫形神俱灭,消失的无影无踪。