数据结构之通用算法说明
- 定义
加密框架将算法的属性抽象为算法说明数据结构struct crypto_alg,加密框架中的每一个算法(基础算法和衍生算法)都表示为一个算法说明数据结构的实例,因此将struct crypto_alg称为通用算法说明数据结构。后续章节中如无特殊说明,算法说明数据结构和通用算法数据结构均指的是struct crypto_alg。
在root/include/linux/crypto.h中,算法说明数据结构struct crypto_alg定义如下:
struct crypto_alg {
struct list_head cra_list;
struct list_head cra_users;
u32 cra_flags;
unsigned int cra_blocksize;
unsigned int cra_ctxsize;
unsigned int cra_alignmask;
int cra_priority;
atomic_t cra_refcnt;
char cra_name[CRYPTO_MAX_ALG_NAME];
char cra_driver_name[CRYPTO_MAX_ALG_NAME];
const struct crypto_type *cra_type;
union {
struct ablkcipher_alg ablkcipher;
struct aead_alg aead;
struct blkcipher_alg blkcipher;
struct cipher_alg cipher;
struct compress_alg compress;
struct rng_alg rng;
} cra_u;
int (*cra_init)(struct crypto_tfm *tfm);
void (*cra_exit)(struct crypto_tfm *tfm);
void (*cra_destroy)(struct crypto_alg *alg);
struct module *cra_module;
}
数据结构struct crypto_alg中各成员变量含义如下所示,其中前缀cra为crypto_alg的缩写:
1)cra_list:算法管理链表节点,向加密框架注册算法实际上就是将cra_list添加到全局的算法管理链表的过程,管理算法链表的表头为crypto_alg_list;
2)cra_users:算法用户链表表头,将由算法根据算法模板创建的新算法视为本算法的一个用户;
3)cra_flag:算法标志,包括算法状态和算法类型等标志位,其中低4比特表示算法类型;
4)cra_blocksize:算法分组长度,单位:字节;
5)cra_ctxsize:算法上下文空间大小,单位:字节;
6)cra_alignmask:算法输入输出数据地址对齐要求屏蔽位,alignmask+1表示地址对齐要求,如算法输入输出数据地址要求4字节对齐,则alignmask=3;
7)cra_priority:算法优先级;
8)cra_refcnt:算法引用计数;
9)cra_name[CRYPTO_MAX_ALG_NAME]:算法名,最多为64个字符;
10)cra_driver_name[CRYPTO_MAX_ALG_NAME]:算法驱动名,最多为64个字符。注册时,如果未指定算法驱动名,则按“算法名-generic”规则定义算法驱动名;
11)cra_type:算法类型,其数据类型为const,因此称之为算法类型常量。如果算法能够提供某种密码服务,必须设置cra_type,并且与cra_flag中的算法类型保持一致;
12)cra_u:算法个性化属性,联合体变量,其各成员变量含义如下:
a)ablkcipher:异步块加密算法个性化属性;
b)aead:认证加密算法个性化属性;
c)blkcipher:块加密算法个性化属性;
d)cipher