Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
cb385b33
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
cb385b33
编写于
8月 17, 2022
作者:
scrawman
提交者:
Gitee
8月 17, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update zh-cn/device-dev/subsystems/subsys-security-huks-guide.md.
Signed-off-by:
zqr2001
<
1805768383@qq.com
>
上级
9da3e42f
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
205 addition
and
163 deletion
+205
-163
zh-cn/device-dev/subsystems/subsys-security-huks-guide.md
zh-cn/device-dev/subsystems/subsys-security-huks-guide.md
+205
-163
未找到文件。
zh-cn/device-dev/subsystems/subsys-security-huks-guide.md
浏览文件 @
cb385b33
...
...
@@ -585,201 +585,243 @@ Hdi接口到HUKS Core的适配在以下目录中:
详细代码可以参考
[
hks_core_service.c
](
https://gitee.com/openharmony/security_huks/blob/master/services/huks_standard/huks_engine/main/core/src/hks_core_service.c
)
文件。
三段式Init接口
```
c
// 三段式Init接口
int32_t
HksCoreInit
(
const
struct
HksBlob
*
key
,
const
struct
HksParamSet
*
paramSet
,
struct
HksBlob
*
handle
,
int32_t
HksCoreInit
(
const
struct
HksBlob
*
key
,
const
struct
HksParamSet
*
paramSet
,
struct
HksBlob
*
handle
,
struct
HksBlob
*
token
)
{
HKS_LOG_D
(
"HksCoreInit in Core start"
);
uint32_t
pur
=
0
;
uint32_t
alg
=
0
;
if
(
key
==
NULL
||
paramSet
==
NULL
||
handle
==
NULL
||
token
==
NULL
)
{
HKS_LOG_E
(
"the pointer param entered is invalid"
);
return
HKS_FAILURE
;
}
```
if
(
handle
->
size
<
sizeof
(
uint64_t
))
{
HKS_LOG_E
(
"handle size is too small, size : %u"
,
handle
->
size
);
return
HKS_ERROR_INSUFFICIENT_MEMORY
;
}
1.
检查参数是否合规,初始化handle。
struct
HuksKeyNode
*
keyNode
=
HksCreateKeyNode
(
key
,
paramSet
);
if
(
keyNode
==
NULL
||
handle
==
NULL
)
{
HKS_LOG_E
(
"the pointer param entered is invalid"
);
return
HKS_ERROR_BAD_STATE
;
}
//初始化handle。通过handle向session中存储信息,供Update/Finish使用。使得外部可以通过同个handle分多次进行同一密钥操作。
handle
->
size
=
sizeof
(
uint64_t
);
(
void
)
memcpy_s
(
handle
->
data
,
handle
->
size
,
&
(
keyNode
->
handle
),
handle
->
size
);
//从参数中提取出算法
int32_t
ret
=
GetPurposeAndAlgorithm
(
paramSet
,
&
pur
,
&
alg
);
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
keyNode
->
handle
);
return
ret
;
}
```
c
HKS_LOG_D
(
"HksCoreInit in Core start"
);
uint32_t
pur
=
0
;
uint32_t
alg
=
0
;
//检查参数
if
(
key
==
NULL
||
paramSet
==
NULL
||
handle
==
NULL
||
token
==
NULL
)
{
HKS_LOG_E
(
"the pointer param entered is invalid"
);
return
HKS_FAILURE
;
}
ret
=
HksCoreSecureAccessInitParams
(
keyNode
,
paramSet
,
token
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"init secure access params failed"
);
HksDeleteKeyNode
(
keyNode
->
handle
);
return
ret
;
}
uint32_t
i
;
uint32_t
size
=
HKS_ARRAY_SIZE
(
g_hksCoreInitHandler
);
for
(
i
=
0
;
i
<
size
;
i
++
)
{
if
(
g_hksCoreInitHandler
[
i
].
pur
==
pur
)
{
HKS_LOG_E
(
"Core HksCoreInit [pur] = %d, pur = %d"
,
g_hksCoreInitHandler
[
i
].
pur
,
pur
);
ret
=
g_hksCoreInitHandler
[
i
].
handler
(
keyNode
,
paramSet
,
alg
);
break
;
}
}
if
(
handle
->
size
<
sizeof
(
uint64_t
))
{
HKS_LOG_E
(
"handle size is too small, size : %u"
,
handle
->
size
);
return
HKS_ERROR_INSUFFICIENT_MEMORY
;
}
//解密密钥文件
struct
HuksKeyNode
*
keyNode
=
HksCreateKeyNode
(
key
,
paramSet
);
if
(
keyNode
==
NULL
||
handle
==
NULL
)
{
HKS_LOG_E
(
"the pointer param entered is invalid"
);
return
HKS_ERROR_BAD_STATE
;
}
//通过handle向session中存储信息,供Update/Finish使用。使得外部可以通过同个handle分多次进行同一密钥操作。
handle
->
size
=
sizeof
(
uint64_t
);
(
void
)
memcpy_s
(
handle
->
data
,
handle
->
size
,
&
(
keyNode
->
handle
),
handle
->
size
);
```
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
keyNode
->
handle
);
HKS_LOG_E
(
"CoreInit failed, ret : %d"
,
ret
);
return
ret
;
}
2.
从参数中提取出算法和目的,获取对应的算法库处理函数。
if
(
i
==
size
)
{
HksDeleteKeyNode
(
keyNode
->
handle
);
HKS_LOG_E
(
"don't found purpose, pur : %u"
,
pur
);
return
HKS_FAILURE
;
```
c
//从参数中提取出算法
int32_t
ret
=
GetPurposeAndAlgorithm
(
paramSet
,
&
pur
,
&
alg
);
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
keyNode
->
handle
);
return
ret
;
}
//检查密钥参数
ret
=
HksCoreSecureAccessInitParams
(
keyNode
,
paramSet
,
token
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"init secure access params failed"
);
HksDeleteKeyNode
(
keyNode
->
handle
);
return
ret
;
}
//通过密钥使用目的获取对应的算法库处理函数
uint32_t
i
;
uint32_t
size
=
HKS_ARRAY_SIZE
(
g_hksCoreInitHandler
);
for
(
i
=
0
;
i
<
size
;
i
++
)
{
if
(
g_hksCoreInitHandler
[
i
].
pur
==
pur
)
{
HKS_LOG_E
(
"Core HksCoreInit [pur] = %d, pur = %d"
,
g_hksCoreInitHandler
[
i
].
pur
,
pur
);
ret
=
g_hksCoreInitHandler
[
i
].
handler
(
keyNode
,
paramSet
,
alg
);
break
;
}
}
```
3.
异常结果检查
HKS_LOG_D
(
"HksCoreInit in Core end"
);
```
c
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
keyNode
->
handle
);
HKS_LOG_E
(
"CoreInit failed, ret : %d"
,
ret
);
return
ret
;
}
if
(
i
==
size
)
{
HksDeleteKeyNode
(
keyNode
->
handle
);
HKS_LOG_E
(
"don't found purpose, pur : %u"
,
pur
);
return
HKS_FAILURE
;
}
HKS_LOG_D
(
"HksCoreInit in Core end"
);
return
ret
;
```
// 三段式Update接口
int32_t
HksCoreUpdate
(
const
struct
HksBlob
*
handle
,
const
struct
HksParamSet
*
paramSet
,
const
struct
HksBlob
*
inData
,
三段式Update接口
```
c
int32_t
HksCoreUpdate
(
const
struct
HksBlob
*
handle
,
const
struct
HksParamSet
*
paramSet
,
const
struct
HksBlob
*
inData
,
struct
HksBlob
*
outData
)
{
HKS_LOG_D
(
"HksCoreUpdate in Core start"
);
uint32_t
pur
=
0
;
uint32_t
alg
=
0
;
if
(
handle
==
NULL
||
paramSet
==
NULL
||
inData
==
NULL
)
{
HKS_LOG_E
(
"the pointer param entered is invalid"
);
return
HKS_FAILURE
;
}
```
uint64_t
sessionId
;
struct
HuksKeyNode
*
keyNode
=
NULL
;
//根据handle获取本次三段式操作需要的上下文
int32_t
ret
=
GetParamsForUpdateAndFinish
(
handle
,
&
sessionId
,
&
keyNode
,
&
pur
,
&
alg
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"GetParamsForCoreUpdate failed"
);
return
ret
;
}
//校验参数
ret
=
HksCoreSecureAccessVerifyParams
(
keyNode
,
paramSet
);
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
sessionId
);
HKS_LOG_E
(
"HksCoreUpdate secure access verify failed"
);
return
ret
;
}
1.
检查参数是否合规,根据handle获取本次三段式操作需要的上下文。
```
c
HKS_LOG_D
(
"HksCoreUpdate in Core start"
);
uint32_t
pur
=
0
;
uint32_t
alg
=
0
;
//检查参数
if
(
handle
==
NULL
||
paramSet
==
NULL
||
inData
==
NULL
)
{
HKS_LOG_E
(
"the pointer param entered is invalid"
);
return
HKS_FAILURE
;
}
uint64_t
sessionId
;
struct
HuksKeyNode
*
keyNode
=
NULL
;
//根据handle获取本次三段式操作需要的上下文
int32_t
ret
=
GetParamsForUpdateAndFinish
(
handle
,
&
sessionId
,
&
keyNode
,
&
pur
,
&
alg
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"GetParamsForCoreUpdate failed"
);
return
ret
;
}
```
2.
校验密钥参数,调用对应的算法库函数
uint32_t
i
;
uint32_t
size
=
HKS_ARRAY_SIZE
(
g_hksCoreUpdateHandler
);
for
(
i
=
0
;
i
<
size
;
i
++
)
{
//调用对应的密码学处理函数
if
(
g_hksCoreUpdateHandler
[
i
].
pur
==
pur
)
{
struct
HksBlob
appendInData
=
{
0
,
NULL
};
ret
=
HksCoreAppendAuthInfoBeforeUpdate
(
keyNode
,
pur
,
paramSet
,
inData
,
&
appendInData
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"before update: append auth info failed"
);
break
;
}
ret
=
g_hksCoreUpdateHandler
[
i
].
handler
(
keyNode
,
paramSet
,
appendInData
.
data
==
NULL
?
inData
:
&
appendInData
,
outData
,
alg
);
if
(
appendInData
.
data
!=
NULL
)
{
HKS_FREE_BLOB
(
appendInData
);
}
```
c
//校验密钥参数
ret
=
HksCoreSecureAccessVerifyParams
(
keyNode
,
paramSet
);
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
sessionId
);
HKS_LOG_E
(
"HksCoreUpdate secure access verify failed"
);
return
ret
;
}
//调用对应的算法库密钥处理函数
uint32_t
i
;
uint32_t
size
=
HKS_ARRAY_SIZE
(
g_hksCoreUpdateHandler
);
for
(
i
=
0
;
i
<
size
;
i
++
)
{
if
(
g_hksCoreUpdateHandler
[
i
].
pur
==
pur
)
{
struct
HksBlob
appendInData
=
{
0
,
NULL
};
ret
=
HksCoreAppendAuthInfoBeforeUpdate
(
keyNode
,
pur
,
paramSet
,
inData
,
&
appendInData
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"before update: append auth info failed"
);
break
;
}
ret
=
g_hksCoreUpdateHandler
[
i
].
handler
(
keyNode
,
paramSet
,
appendInData
.
data
==
NULL
?
inData
:
&
appendInData
,
outData
,
alg
);
if
(
appendInData
.
data
!=
NULL
)
{
HKS_FREE_BLOB
(
appendInData
);
}
break
;
}
}
```
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
keyNode
->
handle
);
HKS_LOG_E
(
"CoreUpdate failed, ret : %d"
,
ret
);
return
ret
;
}
3.
异常结果检查
if
(
i
==
size
)
{
HksDeleteKeyNode
(
sessionId
);
HKS_LOG_E
(
"don't found purpose, pur : %u"
,
pur
);
return
HKS_FAILURE
;
}
```
c
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
keyNode
->
handle
)
;
HKS_LOG_E
(
"CoreUpdate failed, ret : %d"
,
ret
);
return
ret
;
}
// 三段式Finish接口
if
(
i
==
size
)
{
HksDeleteKeyNode
(
sessionId
);
HKS_LOG_E
(
"don't found purpose, pur : %u"
,
pur
);
return
HKS_FAILURE
;
}
return
ret
;
```
三段式Finish接口
```
c
int32_t
HksCoreFinish
(
const
struct
HksBlob
*
handle
,
const
struct
HksParamSet
*
paramSet
,
const
struct
HksBlob
*
inData
,
struct
HksBlob
*
outData
)
{
HKS_LOG_D
(
"HksCoreFinish in Core start"
);
uint32_t
pur
=
0
;
uint32_t
alg
=
0
;
if
(
handle
==
NULL
||
paramSet
==
NULL
||
inData
==
NULL
)
{
HKS_LOG_E
(
"the pointer param entered is invalid"
);
return
HKS_FAILURE
;
}
```
uint64_t
sessionId
;
struct
HuksKeyNode
*
keyNode
=
NULL
;
//根据handle获取本次三段式操作需要的上下文
int32_t
ret
=
GetParamsForUpdateAndFinish
(
handle
,
&
sessionId
,
&
keyNode
,
&
pur
,
&
alg
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"GetParamsForCoreUpdate failed"
);
return
ret
;
}
//校验参数
ret
=
HksCoreSecureAccessVerifyParams
(
keyNode
,
paramSet
);
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
sessionId
);
HKS_LOG_E
(
"HksCoreFinish secure access verify failed"
);
return
ret
;
}
1.
检查参数是否合规,根据handle获取本次三段式操作需要的上下文。
```
c
HKS_LOG_D
(
"HksCoreFinish in Core start"
);
uint32_t
pur
=
0
;
uint32_t
alg
=
0
;
//检查参数
if
(
handle
==
NULL
||
paramSet
==
NULL
||
inData
==
NULL
)
{
HKS_LOG_E
(
"the pointer param entered is invalid"
);
return
HKS_FAILURE
;
}
uint64_t
sessionId
;
struct
HuksKeyNode
*
keyNode
=
NULL
;
//根据handle获取本次三段式操作需要的上下文
int32_t
ret
=
GetParamsForUpdateAndFinish
(
handle
,
&
sessionId
,
&
keyNode
,
&
pur
,
&
alg
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"GetParamsForCoreUpdate failed"
);
return
ret
;
}
```
c
uint32_t
i
;
uint32_t
size
=
HKS_ARRAY_SIZE
(
g_hksCoreFinishHandler
);
for
(
i
=
0
;
i
<
size
;
i
++
)
{
//调用对应的密码学处理函数
if
(
g_hksCoreFinishHandler
[
i
].
pur
==
pur
)
{
uint32_t
outDataBufferSize
=
(
outData
==
NULL
)
?
0
:
outData
->
size
;
struct
HksBlob
appendInData
=
{
0
,
NULL
};
ret
=
HksCoreAppendAuthInfoBeforeFinish
(
keyNode
,
pur
,
paramSet
,
inData
,
&
appendInData
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"before finish: append auth info failed"
);
break
;
}
ret
=
g_hksCoreFinishHandler
[
i
].
handler
(
keyNode
,
paramSet
,
appendInData
.
data
==
NULL
?
inData
:
&
appendInData
,
outData
,
alg
);
if
(
appendInData
.
data
!=
NULL
)
{
HKS_FREE_BLOB
(
appendInData
);
}
if
(
ret
!=
HKS_SUCCESS
)
{
break
;
}
//添加密钥操作结束标签
ret
=
HksCoreAppendAuthInfoAfterFinish
(
keyNode
,
pur
,
paramSet
,
outDataBufferSize
,
outData
);
2.
校验密钥参数,调用对应的算法库函数,添加密钥操作结束标签
```
c
//校验密钥参数
ret
=
HksCoreSecureAccessVerifyParams
(
keyNode
,
paramSet
);
if
(
ret
!=
HKS_SUCCESS
)
{
HksDeleteKeyNode
(
sessionId
);
HKS_LOG_E
(
"HksCoreFinish secure access verify failed"
);
return
ret
;
}
//调用对应的算法库密钥处理函数
uint32_t
i
;
uint32_t
size
=
HKS_ARRAY_SIZE
(
g_hksCoreFinishHandler
);
for
(
i
=
0
;
i
<
size
;
i
++
)
{
if
(
g_hksCoreFinishHandler
[
i
].
pur
==
pur
)
{
uint32_t
outDataBufferSize
=
(
outData
==
NULL
)
?
0
:
outData
->
size
;
struct
HksBlob
appendInData
=
{
0
,
NULL
};
ret
=
HksCoreAppendAuthInfoBeforeFinish
(
keyNode
,
pur
,
paramSet
,
inData
,
&
appendInData
);
if
(
ret
!=
HKS_SUCCESS
)
{
HKS_LOG_E
(
"before finish: append auth info failed"
);
break
;
}
ret
=
g_hksCoreFinishHandler
[
i
].
handler
(
keyNode
,
paramSet
,
appendInData
.
data
==
NULL
?
inData
:
&
appendInData
,
outData
,
alg
);
if
(
appendInData
.
data
!=
NULL
)
{
HKS_FREE_BLOB
(
appendInData
);
}
if
(
ret
!=
HKS_SUCCESS
)
{
break
;
}
//添加密钥操作结束标签
ret
=
HksCoreAppendAuthInfoAfterFinish
(
keyNode
,
pur
,
paramSet
,
outDataBufferSize
,
outData
);
break
;
}
}
```
if
(
i
==
size
)
{
HKS_LOG_E
(
"don't found purpose, pur : %d"
,
pur
);
ret
=
HKS_FAILURE
;
}
//删除对应的session
HksDeleteKeyNode
(
sessionId
);
HKS_LOG_D
(
"HksCoreFinish in Core end"
);
return
ret
;
3.
异常结果检查,删除session
```
c
if
(
i
==
size
)
{
HKS_LOG_E
(
"don't found purpose, pur : %d"
,
pur
);
ret
=
HKS_FAILURE
;
}
```
//删除对应的session
HksDeleteKeyNode
(
sessionId
);
HKS_LOG_D
(
"HksCoreFinish in Core end"
);
return
ret
;
```
### 调测验证
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录