Linux Crypto(5):算法实现层
算法实现层向框架核心层注册 struct crypto_alg 实例,提供同步或异步的算法运算回调(encrypt/decrypt/digest/sign 等)。
1 算法分类
算法实现层对不同类型算法进行类分类,如下:
| 类别 | 内核目录示例 | 注册类型 | 关键回调 | 典型算法 |
|---|---|---|---|---|
| SKCIPHER | crypto/skcipher/ | skcipher_alg | encrypt / decrypt | AES-CBC, ChaCha20 |
| AEAD | crypto/aead/ | aead_alg | encrypt / decrypt | AES-GCM, ChaPoly |
| HASH | crypto/hash/ | shash_alg / ahash_alg | init / update / final | SHA-256, BLAKE2b |
| HMAC | crypto/hmac.c | 模板 → shash_alg | 同上 | HMAC-SHA256 |
| AKCIPHER | crypto/akcipher/ | akcipher_alg | encrypt / decrypt / sign | RSA, ECDSA, SM2 |
| KPP | crypto/kpp/ | kpp_alg | generate_public / shared_secret | ECDH, DH |
| COMPRESS | crypto/compress/ | compress_alg | compress / decompress | LZ4, DEFLATE |
| RNG / DRBG | crypto/rng/ | rng_alg | generate | CTR-DRBG, HMAC-DRBG |
| 硬件驱动 | drivers/crypto/XXX/ | 与上述任一类别一致 | 同上,内部调用硬件寄存器 | AES-NI, QCE, CAAM |
2 分类算法数据结构和API实现表格
| 类别 | 核心数据结构 | 注册 API | 注销 API | 使用者分配 API | 典型实现文件 | 完整常用运算 / 参数 API(非 exhaustive,按使用频率列举) |
|---|---|---|---|---|---|---|
| SKCIPHER | struct skcipher_alg |
crypto_register_skcipher() |
crypto_unregister_skcipher() |
crypto_alloc_skcipher() |
crypto/aes.c + crypto/cbc.c |
• • • • • • |
| AEAD | struct aead_alg |
crypto_register_aead() |
crypto_unregister_aead() |
crypto_alloc_aead() |
crypto/gcm.c |
• • • • • • • • |
| HASH(同步) | struct shash_alg |
crypto_register_shash() |
crypto_unregister_shash() |
crypto_alloc_shash() |
crypto/sha256_generic.c |
• • • • • • • • • |
| AHASH(异步) | struct ahash_alg |
crypto_register_ahash() |
crypto_unregister_ahash() |
crypto_alloc_ahash() |
crypto/sha256_generic.c |
• • • • • • • • • |
| HMAC(模板) | 模板生成 shash_alg |
crypto_register_template(&hmac_tmpl) |
crypto_unregister_template(&hmac_tmpl) |
crypto_alloc_shash("hmac(sha256)", ...) |
crypto/hmac.c |
复用 HASH 系列 API |
| AKCIPHER | struct akcipher_alg |
crypto_register_akcipher() |
crypto_unregister_akcipher() |
crypto_alloc_akcipher() |
crypto/rsa.c |
• • • • • • • • |
| KPP | struct kpp_alg |
crypto_register_kpp() |
crypto_unregister_kpp() |
crypto_alloc_kpp() |
crypto/ecc.c |
• • • • • • • |
| COMPRESS | struct compress_alg |
crypto_register_compress() |
crypto_unregister_compress() |
crypto_alloc_compress() |
crypto/lz4.c |
• • • |
| RNG / DRBG | struct rng_alg |
crypto_register_rng() |
crypto_unregister_rng() |
crypto_alloc_rng() |
crypto/drbg.c |
• • • |
3 算法类型细节
3.1 akcipher非对称加解密算法
参考《Linux Crypto:akcipher以及rsa、rsa,sha256算法注册 - ArnoldLu - 博客园》。
浙公网安备 33010602011771号