tpm2-tools源码分析之tpm2_rsaencrypt.c(1)

news/2024/10/24 4:48:27/

TPM 2.0中的tpm2_rsaencrypt命令对应的源文件就是tpm2_rsaencrypt.c,该文件位于tpm2-tools/tools/下,一共有169行(版本5.5)。

tpm2_rsaencrypt的功能是使用TPM执行RSA加密操作。根据IETF RFC 3447(PKCS#1),使用指定的填充方案对文件数据的内容执行RSA加密

下边用几篇文章的篇幅对tpm2_rsaencrypt.c文件结合tpm2_rsaencrypt命令进行深入的、完全的解析。

先来看第一段代码:

// Register this tool with tpm2_tool.c
TPM2_TOOL_REGISTER("rsaencrypt", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)

TPM2_TOOL_REGISTER是一个宏定义,在tpm2-tools/tools/tpm2_tool.h中,代码如下:

#define TPM2_TOOL_REGISTER(tool_name,tool_onstart,tool_onrun,tool_onstop,tool_onexit) \static const tpm2_tool tool = { \.name		= tool_name, \.onstart	= tool_onstart, \.onrun		= tool_onrun, \.onstop		= tool_onstop, \.onexit		= tool_onexit, \}; \static void \__attribute__((__constructor__)) \__attribute__((__used__)) \_tpm2_tool_init(void) \{ \tpm2_tool_register(&tool); \}

TPM2_TOOLS_REGISTER宏定义是整个tpm2-tools中的命令所共用的,是一个框架性质的代码。

在本文件tpm2_rsaencrypt.c也可以说tpm2_rsaencrypt命令中,宏展开后为:

static const tpm2_tool tool = {.name		= "rsaencrypt",.onstart	= tpm2_tool_onstart,.onrun		= tpm2_tool_onrun,.onstop		= NULL,.onexit		= NULL,
};
static void__attribute__((__constructor__))__attribute__((__used__))_tpm2_tool_init(void)
{tpm2_tool_register(&tool);
}

tpm2_tool结构的定义也在tpm2-tools/tools/tpm2_tool.h中,代码如下:

typedef struct {const char * name;tpm2_tool_onstart_t onstart;tpm2_tool_onrun_t onrun;tpm2_tool_onstop_t onstop;tpm2_tool_onexit_t onexit;
} tpm2_tool;

其中包含的相关函数指针如下(同文件中,就在上边):

/*** An optional interface for tools to specify what options they support.* They are concatenated with main's options and passed to getopt_long.* @param opts*  The callee can choose to set *opts to a tpm_options pointer allocated*  via tpm2_options_new(). Setting *opts to NULL is not an error, and*  Indicates that no options are specified by the tool.** @return*  True on success, false on error.*/
typedef bool (*tpm2_tool_onstart_t)(tpm2_options **opts);/*** This is the main interface for tools, after tcti and sapi/esapi initialization* are performed.* @param ectx*  The system/esapi api context.* @param flags*  Flags that tools may wish to respect.* @return*  A tool_rc indicating status.*/
typedef tool_rc (*tpm2_tool_onrun_t)(ESYS_CONTEXT *ectx, tpm2_option_flags flags);/*** Called after tpm2_tool_onrun() is invoked. ESAPI context is still valid during this call.* @param ectx*  The system/esapi api context.* @return*  A tool_rc indicating status.*/
typedef tool_rc (*tpm2_tool_onstop_t)(ESYS_CONTEXT *ectx);/*** Called when the tool is exiting, useful for cleanup.*/
typedef void (*tpm2_tool_onexit_t)(void);

tpm2_tool_register函数在tpm2-tools/tools/tpm2_tools.c中实现,代码如下:

/** Build a list of the TPM2 tools linked into this executable*/
#ifndef TPM2_TOOLS_MAX
#define TPM2_TOOLS_MAX 1024
#endif
static const tpm2_tool *tools[TPM2_TOOLS_MAX];
static unsigned tool_count;void tpm2_tool_register(const tpm2_tool *tool) {if (tool_count < TPM2_TOOLS_MAX) {tools[tool_count++] = tool;} else {LOG_ERR("Over tool count");abort();}
}

回到tpm2_rsaencrypt.c中,来看具体的几个函数。

(1)tpm2_tool_onstart

tpm2_tool_onstart函数代码如下:

static bool tpm2_tool_onstart(tpm2_options **opts) {static const struct option topts[] = {{"output",      required_argument, NULL, 'o'},{"key-context", required_argument, NULL, 'c'},{"scheme",      required_argument, NULL, 's'},{"label",       required_argument, NULL, 'l'},};*opts = tpm2_options_new("o:c:s:l:", ARRAY_LEN(topts), topts, on_option,on_args, 0);return *opts != NULL;
}

(2)tpm2_tool_onrun

tpm2_tool_onrun函数代码如下:

static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *context, tpm2_option_flags flags) {UNUSED(flags);tool_rc rc = init(context);if (rc != tool_rc_success) {return rc;}return rsa_encrypt_and_save(context);
}

后续文章对这几个函数进行深入解析。


http://www.ppmy.cn/news/63756.html

相关文章

《花雕学AI》ChatGPT 的 Prompt 用法,不是随便写就行的,这 13 种才是最有效的

ChatGPT 是一款基于 GPT-3 模型的人工智能写作工具&#xff0c;它可以根据用户的输入和要求&#xff0c;生成各种类型和风格的文本内容&#xff0c;比如文章、故事、诗歌、对话、摘要等。ChatGPT 的强大之处在于它可以灵活地适应不同的写作场景和目的&#xff0c;只要用户给出合…

LinuX系统

主要是练&#xff1a; Linux是&#xff1a;一套免费使用和自由传播的操作系统&#xff0c;免费、开源的操作系统&#xff0c;稳定、安全、高效 使用场景&#xff1a;生产环境的服务器&#xff0c;通常使用Linux&#xff0c;运行我们编写的代码 分类&#xff1a;市场需求&#x…

《花雕学AI》Poe 一站式 AI 工具箱:ChatGPT4 体验邀请,亲,不要错过哦!

你有没有想过&#xff0c;如果你能在同一个平台上体验多种不同的 AI 模型&#xff0c;和他们进行有趣、有用、有深度的对话&#xff0c;甚至还能轻松地分享你的对话给其他人&#xff0c;那该有多好&#xff1f;如果你有这样的想法&#xff0c;那么你一定不能错过 Poe 一站式 AI…

QJsonObject 类

1.QJsonObject简介 QJsonObject类封装了一个JSON对象。   JSON对象是键值对的列表&#xff0c;其中键是唯一的字符串&#xff0c;值由QJsonValue表示。   QJsonObject可以转换为QVariantMap&#xff0c;也可以从QVariantMap转换。您可以从中查询具有size()、insert()和remo…

Java设计模式(二十三)模版方法模式

一、概述 模板方法模式是一种行为型设计模式&#xff0c;它定义了一个算法的骨架&#xff0c;将一些步骤的实现延迟到子类中。模板方法模式通过在抽象类中定义模板方法&#xff0c;该方法包含了算法的主要逻辑和调用顺序&#xff0c;而具体的实现细节则由子类来完成。 二、代…

基于Spring Boot的在线考试系统

系统分析 可行性分析 一个完整的系统&#xff0c;可行性分析是必须要有的&#xff0c;因为关系到系统生存问题&#xff0c;对开发的意义进行分析&#xff0c;能否通过本系统来补充线下在线考试管理模式中的缺限&#xff0c;去解决其中的不足等&#xff0c;通过对本系统&#…

PMP证书“扫盲”时间2023年考证人快看过来

二&#xff0c;PMP报考指南 学历与工作经验要求&#xff1a; 本科及以上学历&#xff0c;三年或以上的项目管理工作经验&#xff1b; 专科及以上学历&#xff0c;五年或以上的项目管理工作经验。 项目管理培训&#xff1a;35小时以上的项目管理教育/培训。 备注&#xff1…

javaScript---toString()与Object.prototype.toString()

1、为什么对象obj.toString()返回为”[object Object]”,而其他类型.toString()返回是相应的字符串? var arr= [1,2,3] arr.toString() //"1,2,3" var arr1=[] arr.toString() //"" var num= 12; num.toString() //"12" var str = "maidu…