对称加密是 Linux 内核 Crypto 框架中另一类核心算法,广泛应用于数据加密、安全传输和存储等场景。与哈希算法类似,对称加密驱动的适配过程也遵循 Crypto 引擎的统一架构,但在具体实现上存在一些关键差异。
对称加密算法(如 AES、DES、SM4 等)通常以分组密码方式工作,支持多种操作模式,如 ECB、CBC、CTR 等。在驱动开发中,我们不仅需要处理算法本身的加解密逻辑,还需要考虑数据块对齐、初始向量(IV)处理、密钥管理等额外因素。
本文将以 AES-ECB 算法为例,详细介绍对称加密算法的驱动适配流程。与哈希算法开发指南一致,本文仍采用内核中的软件实现接口模拟硬件处理过程,重点说明驱动适配的通用架构、关键数据结构和核心实现方法。实际开发时,可将示例中的软件计算部分替换为具体的硬件操作。
对称加密算法同样需要定义专用的上下文结构和请求上下文结构,以管理算法状态和处理过程中的临时数据:
#define CIPHER_DECRYPT BIT(0)/* 对称加密算法上下文 */structdemo_cipher_ctx {structcrypto_engine_ctxenginectx;structdemo_crypto_dev *crypto_dev;unsignedint keylen; u8 key[AES_MAX_KEY_SIZE]; u8 iv[AES_BLOCK_SIZE];/* for fallback */structcrypto_skcipher *fallback_tfm;};/* 对称加密算法请求上下文 */structdemo_cipher_rctx { u32 flags;unsignedint total_len; /* 请求总长度 */structscatterlist *sgs;/* 源散列表 */structscatterlist *sgd;/* 目的散列表 */int nsgs; /* 已映射的散列表个数 */int nsgd; /* 已映射的散列表个数 */unsignedint sg_off; /* 当前散列表偏移 */unsignedint block_size; u8 buf[HASH_BLOCK_SIZE_MAX] __aligned(sizeof(u32)); /* 缓存数据 */size_t buf_len; /* 缓存数据长度 *//* for fallback request */structskcipher_requestfallback_req;/* 对称加密算法回退请求 */};结构体struct demo_cipher_ctx定义转换操作上下文,用于保存密钥、IV 等持久化数据。结构体struct demo_cipher_rctx定义加密请求上下文,用于管理单个请求的处理状态和数据缓存。
同样也需要定义一个宏,方便后续定义加密算法实例。
/* 对称加密算法 */enum demo_cipher_algo { CIPHER_ALGO_AES_ECB, CIPHER_ALGO_AES_CBC, CIPHER_ALGO_AES_CTR,};/* 对称加密算法实例定义 */#define DEMO_CIPHER_ALGO_INIT(cipher_algo, mode, algo_name) {\ .name = #algo_name,\ .type = ALG_TYPE_CIPHER,\ .algo = CIPHER_ALGO_##cipher_algo##_##mode,\ .alg.cipher = {\ .setkey = demo_cipher_setkey,\ .encrypt = demo_cipher_encrypt,\ .decrypt = demo_cipher_decrypt,\ .init = demo_cipher_init,\ .exit = demo_cipher_exit,\ .min_keysize = cipher_algo##_MIN_KEY_SIZE,\ .max_keysize = cipher_algo##_MAX_KEY_SIZE,\ .ivsize = cipher_algo##_BLOCK_SIZE,\ .chunksize = cipher_algo##_BLOCK_SIZE,\ .walksize = cipher_algo##_BLOCK_SIZE,\ .base = {\ .cra_name = #algo_name,\ .cra_driver_name = #algo_name"-demo",\ .cra_priority = DEMO_CRYPTO_PRIORITY,\ .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |\ CRYPTO_ALG_ASYNC |\ CRYPTO_ALG_NEED_FALLBACK,\ .cra_blocksize = cipher_algo##_BLOCK_SIZE,\ .cra_ctxsize = sizeof(struct demo_cipher_ctx),\ .cra_alignmask = 3,\ .cra_module = THIS_MODULE,\ } \ } \}chunksize。这里我们以 AES-ECB 算法为例,详细介绍对称加密驱动的实现。加密驱动需要实现算法实例定义中声明的三个核心接口:
由于硬件加速引擎通常是安装块进行进行计算的,因此对于长度非块对齐或者缓存地址非word对齐的请求等交给crypto框架进行fallback处理。值得注意的是算法实例定义时需要声明回退标志,即.cra_flags添加CRYPTO_ALG_NEED_FALLBACK。
首先需要初始化算法实例,主要设置软件回退方案和引擎请求回调处理接口。
staticintdemo_cipher_init(struct crypto_skcipher *tfm){/* 获取算法上下文 */structdemo_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);/* 获取算法定义 */structskcipher_alg *alg = crypto_skcipher_alg(tfm);structdemo_crypto_alg *algt;/* 获取算法实例,并关联到crypto设备 */ algt = container_of(alg, struct demo_crypto_alg, alg.cipher); ctx->crypto_dev = algt->crypto_dev; dev_dbg(ctx->crypto_dev->dev, "%s\n", __func__);/* 分配回退TFM */ ctx->fallback_tfm = crypto_alloc_skcipher(crypto_tfm_alg_name(&tfm->base), 0, CRYPTO_ALG_NEED_FALLBACK);if (IS_ERR(ctx->fallback_tfm)) { dev_err(ctx->crypto_dev->dev, "Could not load fallback driver.\n");return PTR_ERR(ctx->fallback_tfm); } /* 设置请求上下文大小 */ crypto_skcipher_set_reqsize(tfm, sizeof(struct demo_cipher_rctx) + crypto_skcipher_reqsize(ctx->fallback_tfm));/* 设置引擎请求回调接口 */ ctx->enginectx.op.do_one_request = demo_cipher_do_one_request; ctx->enginectx.op.prepare_request = demo_cipher_prepare_request; ctx->enginectx.op.unprepare_request = demo_cipher_unprepare_request;return0;}staticvoiddemo_cipher_exit(struct crypto_skcipher *tfm){structdemo_cipher_ctx *ctx = crypto_skcipher_ctx(tfm); memzero_explicit(ctx->key, ctx->keylen); crypto_free_skcipher(ctx->fallback_tfm);}首先是配置密钥接口,这里需要暂存密钥,另外可能进行回退处理,同样需要设置到回退tfm中。
staticintdemo_cipher_setkey(struct crypto_skcipher *tfm, const u8 *key, unsignedint keylen){structdemo_cipher_ctx *ctx = crypto_skcipher_ctx(tfm); dev_dbg(ctx->crypto_dev->dev, "\n\n**********************A new cipher request**********************\n"); dev_dbg(ctx->crypto_dev->dev, "%s\n", __func__); dev_dbg(ctx->crypto_dev->dev, "%s: key[%d] = %*ph\n", __func__, keylen, keylen, key);if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256) {return -EINVAL; } ctx->keylen = keylen;memcpy(ctx->key, key, keylen);return crypto_skcipher_setkey(ctx->fallback_tfm, key, keylen);}接着我们需要编写回退处理函数,检查非块对齐长度的请求和非word对齐的buffer,如果是这种情形,需要进行回退:
staticintdemo_cipher_need_fallback(struct skcipher_request *req){structcrypto_skcipher *tfm = crypto_skcipher_reqtfm(req);structdemo_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);// struct demo_cipher_rctx *rctx = skcipher_request_ctx(req);unsignedint bs = crypto_skcipher_blocksize(tfm);structscatterlist *sgs, *sgd;unsignedint stodo, dtodo, len; dev_dbg(ctx->crypto_dev->dev, "%s\n", __func__);/* 不支持0字节加解密 */if (!req->cryptlen) {return1; }/* 不支持非块对齐的sg */ len = req->cryptlen; sgs = req->src; sgd = req->dst;while (sgs && sgd) {if (!IS_ALIGNED(sgs->offset, sizeof(u32))) {returntrue; }if (!IS_ALIGNED(sgd->offset, sizeof(u32))) {returntrue; } stodo = min(len, sgs->length); dtodo = min(len, sgd->length);if (stodo % bs || dtodo % bs) {return1; }if (stodo != dtodo) {return1; } len -= stodo; sgs = sg_next(sgs); sgd = sg_next(sgd); }return0;}staticintdemo_cipher_do_fallback(struct skcipher_request *req){structcrypto_skcipher *tfm = crypto_skcipher_reqtfm(req);structdemo_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);structdemo_cipher_rctx *rctx = skcipher_request_ctx(req);int err; dev_dbg(ctx->crypto_dev->dev, "%s\n", __func__); skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm); skcipher_request_set_callback(&rctx->fallback_req, req->base.flags, req->base.complete, req->base.data); skcipher_request_set_crypt(&rctx->fallback_req, req->src, req->dst, req->cryptlen, req->iv);if (rctx->flags & CIPHER_DECRYPT) { err = crypto_skcipher_decrypt(&rctx->fallback_req); } else { err = crypto_skcipher_encrypt(&rctx->fallback_req); }return err;}最后适配加解密驱动接口,检查是否需要回退,如果不需要就提交加密请求给引擎队列:
staticintdemo_cipher_encrypt(struct skcipher_request *req){structcrypto_skcipher *tfm = crypto_skcipher_reqtfm(req);structdemo_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);structdemo_cipher_rctx *rctx = skcipher_request_ctx(req); dev_dbg(ctx->crypto_dev->dev, "%s\n", __func__); rctx->flags = 0;/* 检查是否需要fallback */if (demo_cipher_need_fallback(req)) {return demo_cipher_do_fallback(req); }/* 提交加密请求给引擎队列 */return crypto_transfer_skcipher_request_to_engine(ctx->crypto_dev->engine, req);}staticintdemo_cipher_decrypt(struct skcipher_request *req){structcrypto_skcipher *tfm = crypto_skcipher_reqtfm(req);structdemo_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);structdemo_cipher_rctx *rctx = skcipher_request_ctx(req); dev_dbg(ctx->crypto_dev->dev, "%s\n", __func__); rctx->flags = CIPHER_DECRYPT;if (demo_cipher_need_fallback(req)) {return demo_cipher_do_fallback(req); }return crypto_transfer_skcipher_request_to_engine(ctx->crypto_dev->engine, req);}加密引擎框架收到请求后,会排队进行处理,调用回调接口,这里核心接口就是do_one_request:
staticintdemo_cipher_do_one_request(struct crypto_engine *engine, void *areq){int err, decrypt;/* 获取原始加密请求 */structskcipher_request *req = skcipher_request_cast(areq);/* 获取加密转换 */structcrypto_skcipher *tfm = crypto_skcipher_reqtfm(req);/* 获取加密上下文 */structdemo_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);/* 获取加密请求上下文 */structdemo_cipher_rctx *rctx = skcipher_request_ctx(req);/* 获取加密算法定义 */structskcipher_alg *alg = crypto_skcipher_alg(tfm);structdemo_crypto_alg *algt;unsignedint ivsize;unsignedint bs = crypto_skcipher_blocksize(tfm);/* 获取加密算法实例 */ algt = container_of(alg, struct demo_crypto_alg, alg.cipher); dev_dbg(ctx->crypto_dev->dev, "%s\n", __func__); dev_dbg(ctx->crypto_dev->dev, "%s: algo: %s, req->cryptlen = %d\n", __func__, algt->name, req->cryptlen); rctx->total_len = ALIGN(req->cryptlen, bs); rctx->sgs = req->src; rctx->sgd = req->dst; rctx->sg_off = 0; rctx->buf_len = 0; rctx->block_size = bs; decrypt = rctx->flags & CIPHER_DECRYPT; ivsize = crypto_skcipher_ivsize(tfm);/* 加密计算 */ err = demo_cipher_compute(ctx->crypto_dev, rctx, ctx->key, ctx->keylen);if (err) { dev_err(ctx->crypto_dev->dev, "cipher compute error\n");gotoexit; }/* 获取输出IV */if (req->iv && ivsize) {// 对于CBC/CTR等模式,需要输出IV给req->ivif (err) { dev_err(ctx->crypto_dev->dev, "cipher get out iv error\n");gotoexit; } }exit:/* 完成请求 */ crypto_finalize_skcipher_request(engine, req, err);return0;}每个请求的长度为req->cryptlen,源数据放到req->src散列表中,目标数据放到req->dst中。通常如果有硬件加速引擎,就可以启动硬件,触发DMA传输,进行加解密计算。计算的结果需要通过req->dst返回给调用者,最后需要调用crypto_finalize_skcipher_request函数完成加密请求,否则会导致请求阻塞。
这里我们使用软实现进行加解密,调用内核的软实现接口:
staticintdemo_cipher_compute(struct demo_crypto_dev *crypto_dev, struct demo_cipher_rctx *rctx, u8 *key, size_t keylen){int err = 0;structscatterlist *sgs, *sgd;unsignedint total_len = rctx->total_len;unsignedint bs, processed = 0;structcrypto_aes_ctxaes_ctx; dev_dbg(crypto_dev->dev, "%s\n", __func__); sgs = rctx->sgs; sgd = rctx->sgd; bs = rctx->block_size; err = aes_expandkey(&aes_ctx, key, keylen);if (err) { dev_err(crypto_dev->dev, "aes expand key error\n");return err; }/* 遍历sg列表 */while (processed < total_len) {/* 非对齐应当已经fallback处理 */if (sgs->length % bs) { dev_err(crypto_dev->dev, "sg data length must be multiple of %d\n", bs);return -EFAULT; }for (int i = 0; i < sgs->length; i += bs) {if (rctx->flags & CIPHER_DECRYPT) { aes_decrypt(&aes_ctx, sg_virt(sgd) + i, sg_virt(sgs) + i); } else { aes_encrypt(&aes_ctx, sg_virt(sgd) + i, sg_virt(sgs) + i); } } processed += sgs->length; sgs = sg_next(sgs); sgd = sg_next(sgd); }return0;}最后需要进行加密算法实例的定义,并放到密码算法数组中crypto_algs。当我们进行驱动注册时,就可以将这个ecb(aes)算法注册到系统中。
structdemo_crypto_algdemo_cipher_aes_ecb = DEMO_CIPHER_ALGO_INIT(AES, ECB, ecb(aes));执行模块编译,生成文件demo_crypto.ko和crypto_engine.ko。然后拷贝到目标系统上,注意需要先安装引擎KO,再安装驱动KO:
$ insmod crypto_engine.ko$ insmod demo_crypto.ko[75304.994703] demo-crypto 53050000.crypto: will run requests pump with realtime priority[75305.009286] demo-crypto 53050000.crypto: Demo Crypto(V0.1) platform driver probed如果哈希算法驱动没有问题,功能测试通过,会打印如上日志,否则会打印具体测试失败在哪一项。加密驱动加载成功后,我们可以通过命令查看是否已经注册成功:
name : ecb(aes)driver : ecb(aes)-demomodule : demo_cryptopriority : 300refcnt : 1selftest : passedinternal : notype : skcipherasync : yesblocksize : 16min keysize : 16max keysize : 32ivsize : 16chunksize : 16walksize : 16如果想查看更多测试日志,可以调整打印等级。可以看出系统测试框架会测试所有驱动接口实现,不同的消息长度,非对齐的buffer等情形:
$ echo 8 > /proc/sys/kernel/printk$ insmod demo_crypto.ko[ 2378.819875] demo-crypto 53050000.crypto: will run requests pump with realtime priority[ 2378.828018] demo-crypto 53050000.crypto: demo_crypto_register[ 2378.871256] demo-crypto 53050000.crypto: demo_cipher_init[ 2378.876741] demo-crypto 53050000.crypto: [ 2378.876741] [ 2378.876741] **********************A new cipher request**********************[ 2378.889372] demo-crypto 53050000.crypto: demo_cipher_setkey[ 2378.895025] demo-crypto 53050000.crypto: demo_cipher_setkey: key[16] = 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f[ 2378.905824] demo-crypto 53050000.crypto: demo_cipher_encrypt[ 2378.911570] demo-crypto 53050000.crypto: demo_cipher_need_fallback[ 2378.917782] demo-crypto 53050000.crypto: demo_cipher_do_one_request[ 2378.924111] demo-crypto 53050000.crypto: demo_cipher_do_one_request: algo: ecb(aes), req->cryptlen = 16[ 2378.933554] demo-crypto 53050000.crypto: demo_cipher_compute[ 2378.939274] demo-crypto 53050000.crypto: [ 2378.939274] [ 2378.939274] **********************A new cipher request**********************[ 2378.951849] demo-crypto 53050000.crypto: demo_cipher_setkey[ 2378.957428] demo-crypto 53050000.crypto: demo_cipher_setkey: key[16] = 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f[ 2378.968178] demo-crypto 53050000.crypto: demo_cipher_encrypt[ 2378.973879] demo-crypto 53050000.crypto: demo_cipher_need_fallback[ 2378.980079] demo-crypto 53050000.crypto: demo_cipher_do_one_request[ 2378.986392] demo-crypto 53050000.crypto: demo_cipher_do_one_request: algo: ecb(aes), req->cryptlen = 16[ 2378.995835] demo-crypto 53050000.crypto: demo_cipher_compute[ 2379.001572] demo-crypto 53050000.crypto: [ 2379.001572] [ 2379.001572] **********************A new cipher request**********************[ 2379.014128] demo-crypto 53050000.crypto: demo_cipher_setkey[ 2379.019705] demo-crypto 53050000.crypto: demo_cipher_setkey: key[16] = 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f[ 2379.030447] demo-crypto 53050000.crypto: demo_cipher_encrypt[ 2379.036146] demo-crypto 53050000.crypto: demo_cipher_need_fallback[ 2379.042376] demo-crypto 53050000.crypto: demo_cipher_do_one_request[ 2379.048649] demo-crypto 53050000.crypto: demo_cipher_do_one_request: algo: ecb(aes), req->cryptlen = 16[ 2379.058082] demo-crypto 53050000.crypto: demo_cipher_compute[ 2379.063813] demo-crypto 53050000.crypto: [ 2379.063813] [ 2379.063813] **********************A new cipher request**********************[ 2379.076368] demo-crypto 53050000.crypto: demo_cipher_setkey[ 2379.081978] demo-crypto 53050000.crypto: demo_cipher_setkey: key[16] = 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f[ 2379.092727] demo-crypto 53050000.crypto: demo_cipher_encrypt[ 2379.098393] demo-crypto 53050000.crypto: demo_cipher_need_fallback[ 2379.104622] demo-crypto 53050000.crypto: demo_cipher_do_fallback[ 2379.110678] demo-crypto 53050000.crypto: [ 2379.110678] [ 2379.110678] **********************A new cipher request**********************[ 2379.123236] demo-crypto 53050000.crypto: demo_cipher_setkey[ 2379.128812] demo-crypto 53050000.crypto: demo_cipher_setkey: key[16] = 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f[ 2379.139557] demo-crypto 53050000.crypto: demo_cipher_encrypt[ 2379.145256] demo-crypto 53050000.crypto: demo_cipher_need_fallback[ 2379.151492] demo-crypto 53050000.crypto: demo_cipher_do_one_request[ 2379.157765] demo-crypto 53050000.crypto: demo_cipher_do_one_request: algo: ecb(aes), req->cryptlen = 16[ 2379.167193] demo-crypto 53050000.crypto: demo_cipher_compute[ 2379.172927] demo-crypto 53050000.crypto: [ 2379.172927] [ 2379.172927] **********************A new cipher request**********************[ 2379.185484] demo-crypto 53050000.crypto: demo_cipher_setkey[ 2379.191092] demo-crypto 53050000.crypto: demo_cipher_setkey: key[16] = 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f[ 2379.201837] demo-crypto 53050000.crypto: demo_cipher_encrypt[ 2379.207505] demo-crypto 53050000.crypto: demo_cipher_need_fallback[ 2379.213732] demo-crypto 53050000.crypto: demo_cipher_do_fallback[ 2379.219781] demo-crypto 53050000.crypto: [ 2379.219781] [ 2379.219781] **********************A new cipher request**********************[ 2379.232335] demo-crypto 53050000.crypto: demo_cipher_setkey[ 2379.237913] demo-crypto 53050000.crypto: demo_cipher_setkey: key[16] = 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f[ 2379.248663] demo-crypto 53050000.crypto: demo_cipher_encrypt[ 2379.254363] demo-crypto 53050000.crypto: demo_cipher_need_fallback[ 2379.260548] demo-crypto 53050000.crypto: demo_cipher_do_fallback[ 2379.266617] demo-crypto 53050000.crypto: [ 2379.266617] [ 2379.266617] **********************A new cipher request**********************[ 2379.279174] demo-crypto 53050000.crypto: demo_cipher_setkey[ 2379.284781] demo-crypto 53050000.crypto: demo_cipher_setkey: key[16] = 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f[ 2379.295525] demo-crypto 53050000.crypto: demo_cipher_encrypt[ 2379.301225] demo-crypto 53050000.crypto: demo_cipher_need_fallback[ 2379.307410] demo-crypto 53050000.crypto: demo_cipher_do_fallback[ 2379.313475] demo-crypto 53050000.crypto: [ 2379.313475] [ 2379.313475] **********************A new cipher request**********************[ 2379.326038] demo-crypto 53050000.crypto: demo_cipher_setkey[ 2379.331649] demo-crypto 53050000.crypto: demo_cipher_setkey: key[24] = 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17[ 2379.344473] demo-crypto 53050000.crypto: demo_cipher_encrypt[ 2379.350139] demo-crypto 53050000.crypto: demo_cipher_need_fallback[ 2379.356373] demo-crypto 53050000.crypto: demo_cipher_do_one_request[ 2379.362682] demo-crypto 53050000.crypto: demo_cipher_do_one_request: algo: ecb(aes), req->cryptlen = 16[ 2379.372109] demo-crypto 53050000.crypto: demo_cipher_compute另外我们也可以通过tcryp测试程序检查对称加密算法驱动实现:
$ insmod tcrypt.ko mode=10