作者簡(jiǎn)介:
baron (csdn:代碼改變世界ctw),九年手機(jī)安全/SOC底層安全開發(fā)經(jīng)驗(yàn)。擅長(zhǎng)trustzone/tee安全產(chǎn)品的設(shè)計(jì)和開發(fā)。
在默認(rèn)情況下,本文講述的都是ARMV8-aarch64架構(gòu),linux kernel 5.14
目錄
1、RSA 開關(guān)
2、RSA 實(shí)現(xiàn)
3、其它非對(duì)稱密碼
4、非對(duì)稱密碼算法的使用示例
5、總結(jié)
1、RSA 開關(guān)
RSA的實(shí)現(xiàn)由 CONFIG_CRYPTO_RSA 宏控制,該宏依賴于:
CONFIG_CRYPTO_AKCIPHER
CONFIG_CRYPTO_MANAGER
CONFIG_MPILIB
CONFIG_ASN1
(linux/crypto/Kconfig) config CRYPTO_RSA tristate "RSA algorithm" select CRYPTO_AKCIPHER select CRYPTO_MANAGER select MPILIB select ASN1 helpGenericimplementationoftheRSApublickeyalgorithm.
2、RSA 實(shí)現(xiàn)
(linux/crypto/rsa.c) static struct akcipher_alg rsa = { .encrypt = rsa_enc, .decrypt = rsa_dec, .set_priv_key = rsa_set_priv_key, .set_pub_key = rsa_set_pub_key, .max_size = rsa_max_size, .exit = rsa_exit_tfm, .base = { .cra_name = "rsa", .cra_driver_name = "rsa-generic", .cra_priority = 100, .cra_module = THIS_MODULE, .cra_ctxsize = sizeof(struct rsa_mpi_key), },};
主要實(shí)現(xiàn)了4個(gè)功能:
rsasetpriv_key
rsasetpub_key
rsa_enc
rsa_dec
其中 rsa_set_priv_key和 rsa_set_pub_key的實(shí)現(xiàn),主要就是接受raw格式的密鑰(DER格密鑰),將其轉(zhuǎn)換成nddpq等因子填充到密碼學(xué)結(jié)構(gòu)體中。rsa_enc和 rsa_dec ,主要就是 公鑰加密、私鑰解密的這種用法。
此類密碼學(xué)具體算法的實(shí)現(xiàn),都是由 linux/lib/mpi 第三方libary實(shí)現(xiàn)的,是一種C語言的實(shí)現(xiàn)方式。
3、其它非對(duì)稱密碼
(1)、實(shí)現(xiàn)了3個(gè)ecdsa的密碼算法
ecdsanistp192
ecdsanistp256
ecdsanistp384
以為ecdsanistp192 為例:
(linux/crypto/ecdsa.c) static struct akcipher_alg ecdsa_nist_p192 = { .verify = ecdsa_verify, .set_pub_key = ecdsa_set_pub_key, .max_size = ecdsa_max_size, .init = ecdsa_nist_p192_init_tfm, .exit = ecdsa_exit_tfm, .base = { .cra_name = "ecdsa-nist-p192", .cra_driver_name = "ecdsa-nist-p192-generic", .cra_priority = 100, .cra_module = THIS_MODULE, .cra_ctxsize = sizeof(struct ecc_ctx), },};
僅僅實(shí)現(xiàn)了兩個(gè)接口函數(shù):
ecdsa_verify : 公鑰驗(yàn)簽
ecdsasetpub_key :導(dǎo)入公鑰
(2)、實(shí)現(xiàn)了1個(gè)sm2的密碼算法
(linux/crypto/sm2.c) static struct akcipher_alg sm2 = { .verify = sm2_verify, .set_pub_key = sm2_set_pub_key, .max_size = sm2_max_size, .init = sm2_init_tfm, .exit = sm2_exit_tfm, .base = { .cra_name = "sm2", .cra_driver_name = "sm2-generic", .cra_priority = 100, .cra_module = THIS_MODULE, .cra_ctxsize = sizeof(struct mpi_ec_ctx), },};
僅僅實(shí)現(xiàn)了兩個(gè)接口函數(shù):
sm2_verify : 公鑰驗(yàn)簽
sm2setpub_key :導(dǎo)入公鑰
(3)、實(shí)現(xiàn)了1個(gè)ecr的密碼算法
(linux/crypto/ecrdsa.c) static struct akcipher_alg ecrdsa_alg = { .verify = ecrdsa_verify, .set_pub_key = ecrdsa_set_pub_key, .max_size = ecrdsa_max_size, .exit = ecrdsa_exit_tfm, .base = { .cra_name = "ecrdsa", .cra_driver_name = "ecrdsa-generic", .cra_priority = 100, .cra_module = THIS_MODULE, .cra_ctxsize = sizeof(struct ecrdsa_ctx), },};
僅僅實(shí)現(xiàn)了兩個(gè)接口函數(shù):
ecrdsa_verify : 公鑰驗(yàn)簽
ecrdsasetpub_key :導(dǎo)入公鑰
4、非對(duì)稱密碼算法的使用示例
如下所示,實(shí)現(xiàn)了 public_key_verify_signature(key,signature), 這個(gè)函數(shù)的實(shí)現(xiàn),也被export出來,相當(dāng)于又封裝了一層。另外其它模塊如果有對(duì)非對(duì)稱密碼學(xué)算法的需求,也可以直接調(diào)用非對(duì)稱密碼學(xué)算法的API,例如直接調(diào)用如下這樣的函數(shù):
crypto_akcipher_verify()
crypto_akcipher_set_pub_key()
如下是 public_key_verify_signature(key,signature)的實(shí)現(xiàn),也可以當(dāng)作非對(duì)稱密碼學(xué)算法的使用示例:
(linux/crypto/asymmetric_keys/public_key.c) /* * Verify a signature using a public key. */int public_key_verify_signature(const struct public_key *pkey, const struct public_key_signature *sig){ struct crypto_wait cwait; struct crypto_akcipher *tfm; struct akcipher_request *req; struct scatterlist src_sg[2]; char alg_name[CRYPTO_MAX_ALG_NAME]; char *key, *ptr; int ret; pr_devel("==>%s() ", __func__); BUG_ON(!pkey); BUG_ON(!sig); BUG_ON(!sig->s); ret = software_key_determine_akcipher(sig->encoding, sig->hash_algo, pkey, alg_name); if (ret < 0) return ret; tfm = crypto_alloc_akcipher(alg_name, 0, 0); if (IS_ERR(tfm)) return PTR_ERR(tfm); ret = -ENOMEM; req = akcipher_request_alloc(tfm, GFP_KERNEL); if (!req) goto error_free_tfm; key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen, GFP_KERNEL); if (!key) goto error_free_req; memcpy(key, pkey->key, pkey->keylen); ptr = key + pkey->keylen; ptr = pkey_pack_u32(ptr, pkey->algo); ptr = pkey_pack_u32(ptr, pkey->paramlen); memcpy(ptr, pkey->params, pkey->paramlen); if (pkey->key_is_private) ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen); else ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen); if (ret) goto error_free_key; if (sig->pkey_algo && strcmp(sig->pkey_algo, "sm2") == 0 && sig->data_size) { ret = cert_sig_digest_update(sig, tfm); if (ret) goto error_free_key; } sg_init_table(src_sg, 2); sg_set_buf(&src_sg[0], sig->s, sig->s_size); sg_set_buf(&src_sg[1], sig->digest, sig->digest_size); akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size, sig->digest_size); crypto_init_wait(&cwait); akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, crypto_req_done, &cwait); ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait); error_free_key: kfree(key);error_free_req: akcipher_request_free(req);error_free_tfm: crypto_free_akcipher(tfm); pr_devel("<==%s() = %d ", __func__, ret); if (WARN_ON_ONCE(ret > 0)) ret = -EINVAL; return ret;}EXPORT_SYMBOL_GPL(public_key_verify_signature);
5、總結(jié)
Linux Kernel非對(duì)稱密碼算法的實(shí)現(xiàn)總結(jié)如下:
實(shí)現(xiàn)了RSA的:“導(dǎo)入公鑰、導(dǎo)入私鑰、公鑰加密私鑰解密” 功能
實(shí)現(xiàn)了ecdsa的:”導(dǎo)入公鑰、公鑰驗(yàn)簽” 功能
實(shí)現(xiàn)了sm2的:”導(dǎo)入公鑰、公鑰驗(yàn)簽” 功能
實(shí)現(xiàn)了ecr的:”導(dǎo)入公鑰、公鑰驗(yàn)簽” 功能
原文標(biāo)題:Linux Kernel中非對(duì)稱密碼算法的實(shí)現(xiàn)
文章出處:【微信公眾號(hào):Linux閱碼場(chǎng)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
-
Linux
+關(guān)注
關(guān)注
87文章
11319瀏覽量
209829 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4338瀏覽量
62738 -
RSA
+關(guān)注
關(guān)注
0文章
59瀏覽量
18908 -
密碼算法
+關(guān)注
關(guān)注
0文章
19瀏覽量
7399
原文標(biāo)題:Linux Kernel中非對(duì)稱密碼算法的實(shí)現(xiàn)
文章出處:【微信號(hào):LinuxDev,微信公眾號(hào):Linux閱碼場(chǎng)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論