Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Xts Acts
提交
fd279506
X
Xts Acts
项目概览
OpenHarmony
/
Xts Acts
大约 1 年 前同步成功
通知
9
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
X
Xts Acts
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fd279506
编写于
12月 15, 2021
作者:
C
code4lala
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add deviceauth algorithm test case
Signed-off-by:
N
code4lala
<
fenglala@outlook.com
>
上级
dfdd0ed8
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
1763 addition
and
1 deletion
+1763
-1
security/deviceauth/deviceauth_basic_deps/BUILD.gn
security/deviceauth/deviceauth_basic_deps/BUILD.gn
+10
-0
security/deviceauth/deviceauth_basic_deps/device_auth_test.cpp
...ity/deviceauth/deviceauth_basic_deps/device_auth_test.cpp
+13
-0
security/deviceauth/deviceauth_basic_deps/hc_alg_test.cpp
security/deviceauth/deviceauth_basic_deps/hc_alg_test.cpp
+1
-0
security/deviceauth/deviceauth_basic_deps/hctest.h
security/deviceauth/deviceauth_basic_deps/hctest.h
+4
-0
security_lite/deviceauth_basic_deps/BUILD.gn
security_lite/deviceauth_basic_deps/BUILD.gn
+28
-0
security_lite/deviceauth_basic_deps/deviceauth.gni
security_lite/deviceauth_basic_deps/deviceauth.gni
+8
-1
security_lite/deviceauth_basic_deps/hc_alg_test.c
security_lite/deviceauth_basic_deps/hc_alg_test.c
+1019
-0
security_lite/deviceauth_basic_deps/hc_alg_test.h
security_lite/deviceauth_basic_deps/hc_alg_test.h
+29
-0
security_lite/deviceauth_basic_deps/hc_alg_test_case.h
security_lite/deviceauth_basic_deps/hc_alg_test_case.h
+638
-0
security_lite/deviceauth_basic_deps/test.c
security_lite/deviceauth_basic_deps/test.c
+13
-0
未找到文件。
security/deviceauth/deviceauth_basic_deps/BUILD.gn
浏览文件 @
fd279506
...
...
@@ -38,6 +38,7 @@ DEVICEAUTH_BASIC_DEPS_CPP_SOURCE = [
"${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_file_iot_flash_test.cpp",
"${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_time_test.cpp",
"${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_file_common.cpp",
"${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_alg_test.cpp",
]
ohos_moduletest_suite("DeviceAuthBasicDepsTest") {
...
...
@@ -84,5 +85,14 @@ ohos_moduletest_suite("DeviceAuthBasicDepsTest") {
"UTILS_FILE_IMPLEMENTED=0",
"TEST_PTHREAD_CREATE_DETACHED=1",
"TEST_HC_FILE_OPEN_SERIES=1",
"TEST_HKDF_WITH_KEY_ALIAS_TRUE=1",
"TEST_IMPORT_ASYMMETRIC_KEY=0",
"TEST_COMPUTEHMAC_WITH_ISALIAS_TRUE=0",
"TEST_HASH_TO_POINT=1",
"TEST_GENERATE_KEY_PAIR_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET=1",
"TEST_BIG_NUM_EXP_MOD=1",
]
}
security/deviceauth/deviceauth_basic_deps/device_auth_test.cpp
浏览文件 @
fd279506
...
...
@@ -21,6 +21,7 @@
#include <cstring>
#include <ctime>
#include "hc_alg_test.h"
#include "hc_condition_test.h"
#include "hc_dev_info_test.h"
#include "hc_file_f_test.h"
...
...
@@ -186,4 +187,16 @@ HWTEST_F(DeviceAuthTest, TestCaseHcTime, TestSize.Level1)
TestHcTime
();
LOGI
(
"++++++++++++++++++++++++++++++++++++++++
\n
"
);
}
/**
* @tc.name: DeviceAuthTest.TestCaseHcAlg
* @tc.desc:
* @tc.type: FUNC
*/
HWTEST_F
(
DeviceAuthTest
,
TestCaseHcAlg
,
TestSize
.
Level1
)
{
LOGI
(
"++++++++++++++++++++++++++++++++++++++++
\n
"
);
TestHcAlg
();
LOGI
(
"++++++++++++++++++++++++++++++++++++++++
\n
"
);
}
}
// namespace
security/deviceauth/deviceauth_basic_deps/hc_alg_test.cpp
0 → 120000
浏览文件 @
fd279506
..
/
..
/
..
/
security_lite
/
deviceauth_basic_deps
/
hc_alg_test
.
c
\ No newline at end of file
security/deviceauth/deviceauth_basic_deps/hctest.h
浏览文件 @
fd279506
...
...
@@ -24,6 +24,9 @@
#define TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements) \
EXPECT_THAT(std::vector<uint8_t>((actual), (actual) + (num_elements)), \
::testing::ElementsAreArray((expected), (num_elements)))
#define TEST_ASSERT_EQUAL_HEX8_ARRAY_MESSAGE(expected, actual, num_elements, message) \
EXPECT_THAT(std::vector<uint8_t>((actual), (actual) + (num_elements)), \
::testing::ElementsAreArray((expected), (num_elements))) << (message)
#define TEST_ASSERT_EQUAL(expected, actual) EXPECT_EQ((actual), (expected))
#define TEST_ASSERT_EQUAL_MESSAGE(expected, actual, message) EXPECT_EQ((actual), (expected)) << (message)
#define TEST_ASSERT_EQUAL_STRING(expected, actual) EXPECT_STREQ((actual), (expected))
...
...
@@ -35,6 +38,7 @@
#define TEST_ASSERT_NOT_EQUAL(expected, actual) EXPECT_NE((actual), (expected))
#define TEST_ASSERT_NOT_EQUAL_MESSAGE(expected, actual, message) EXPECT_NE((actual), (expected)) << (message)
#define TEST_ASSERT_NOT_NULL(pointer) EXPECT_NE((pointer), nullptr)
#define TEST_ASSERT_NULL(pointer) EXPECT_EQ((pointer), nullptr)
#define TEST_ASSERT_TRUE(condition) EXPECT_TRUE((condition))
#define TEST_ASSERT_TRUE_MESSAGE(condition, message) EXPECT_TRUE((condition)) << (message)
...
...
security_lite/deviceauth_basic_deps/BUILD.gn
浏览文件 @
fd279506
...
...
@@ -50,9 +50,21 @@ hctest_suite("ActsSecurityHichainBasicDeps") {
"UTILS_FILE_IMPLEMENTED=1",
"TEST_PTHREAD_CREATE_DETACHED=0",
"TEST_HC_FILE_OPEN_SERIES=1",
"TEST_HKDF_WITH_KEY_ALIAS_TRUE=1",
"TEST_IMPORT_ASYMMETRIC_KEY=1",
"TEST_COMPUTEHMAC_WITH_ISALIAS_TRUE=1",
"TEST_HASH_TO_POINT=1",
"TEST_GENERATE_KEY_PAIR_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET=1",
"TEST_BIG_NUM_EXP_MOD=1",
]
if (defined(ohos_lite)) {
include_dirs += [
"//base/security/deviceauth/deps_adapter/os_adapter/interfaces/liteos",
]
if (ohos_kernel_type == "liteos_m") {
# liteos m
include_dirs += [ "//base/startup/syspara_lite/interfaces/kits" ]
...
...
@@ -72,6 +84,22 @@ hctest_suite("ActsSecurityHichainBasicDeps") {
}
if (board_name == "hispark_pegasus") {
# 3861
defines -= [
"TEST_HKDF_WITH_KEY_ALIAS_TRUE=1",
"TEST_HASH_TO_POINT=1",
"TEST_GENERATE_KEY_PAIR_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET=1",
"TEST_BIG_NUM_EXP_MOD=1",
]
defines += [
"TEST_HKDF_WITH_KEY_ALIAS_TRUE=0",
"TEST_HASH_TO_POINT=0",
"TEST_GENERATE_KEY_PAIR_WITH_STORAGE=0",
"TEST_AGREE_SHARED_SECRET_WITH_STORAGE=0",
"TEST_AGREE_SHARED_SECRET=0",
"TEST_BIG_NUM_EXP_MOD=0",
]
}
} else {
#liteos a or L1 linux
...
...
security_lite/deviceauth_basic_deps/deviceauth.gni
浏览文件 @
fd279506
...
...
@@ -27,9 +27,16 @@ DEVICEAUTH_BASIC_DEPS_SOURCE = [
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_file_iot_flash_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_time_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_file_common.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_alg_test.c",
]
DEVICEAUTH_BASIC_DEPS_INC = [
"//base/iot_hardware/peripheral/interfaces/kits",
"//utils/native/lite/include", # utils_file.h
"//utils/native/lite/include", # utils_file.h ohos_types.h
# alg test
"//base/security/deviceauth/deps_adapter/key_management_adapter/interfaces",
"//base/security/deviceauth/deps_adapter/os_adapter/interfaces",
"//base/security/deviceauth/common_lib/interfaces",
"//base/security/deviceauth/interfaces/innerkits",
]
security_lite/deviceauth_basic_deps/hc_alg_test.c
0 → 100644
浏览文件 @
fd279506
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hc_alg_test.h"
#include <alg_loader.h>
#include <device_auth.h>
#include <hal_error.h>
#include <hctest.h>
#include <stdlib.h>
#include <unistd.h>
#include "hc_alg_test_case.h"
#include "print_log.h"
#include "test_timer.h"
#define LOG_ERROR_IF_POINTER_NULL(pointer) LOGE("Pointer %s is %s", #pointer, ((pointer) == NULL ? "NULL" : "not NULL"))
#ifdef __cplusplus
extern
"C"
{
#endif
static
uint32_t
CountZero
(
const
uint8_t
*
buffer
,
uint32_t
length
)
{
uint32_t
zeroCount
=
0
;
for
(
uint32_t
i
=
0
;
i
<
length
;
++
i
)
{
if
(
buffer
[
i
]
==
0
)
{
++
zeroCount
;
}
}
return
zeroCount
;
}
static
void
FillRandom
(
uint8_t
*
msg
,
uint32_t
setZeroLength
,
uint32_t
randomLength
)
{
int
res
;
res
=
memset_s
(
msg
,
setZeroLength
,
0
,
setZeroLength
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
// generate random message
if
(
randomLength
==
1
)
{
msg
[
0
]
=
rand
()
%
(
UINT8_MAX
-
1
)
+
1
;
}
else
{
for
(
uint32_t
i
=
0
;
i
<
randomLength
;
++
i
)
{
msg
[
i
]
=
rand
()
%
UINT8_MAX
;
}
}
TEST_ASSERT_NOT_EQUAL
(
randomLength
,
CountZero
(
msg
,
randomLength
));
}
// Note: sha256 and sha256Compare will be set to all zero
static
void
TestSha256Inner
(
const
AlgLoader
*
loader
,
Uint8Buff
*
msg
,
Uint8Buff
*
sha256
,
Uint8Buff
*
sha256Compare
)
{
int
res
;
res
=
memset_s
(
sha256
->
val
,
SHA256_LEN
,
0
,
SHA256_LEN
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
res
=
memset_s
(
sha256Compare
->
val
,
SHA256_LEN
,
0
,
SHA256_LEN
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
sha256
(
msg
,
sha256
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
TEST_ASSERT_EQUAL
((
uint32_t
)
SHA256_LEN
,
sha256
->
length
);
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
sha256
(
msg
,
sha256Compare
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
TEST_ASSERT_EQUAL
((
uint32_t
)
SHA256_LEN
,
sha256Compare
->
length
);
uint32_t
zeroCount
=
CountZero
(
sha256
->
val
,
sha256
->
length
);
TEST_ASSERT_NOT_EQUAL_MESSAGE
((
uint32_t
)
SHA256_LEN
,
zeroCount
,
"invalid all zero sha256"
);
TEST_ASSERT_EQUAL_HEX8_ARRAY_MESSAGE
(
sha256
->
val
,
sha256Compare
->
val
,
SHA256_LEN
,
"different sha256 against the same message"
);
}
static
void
TestSha256
(
const
AlgLoader
*
loader
)
{
TEST_ASSERT_NOT_NULL
(
loader
->
sha256
);
if
(
loader
->
sha256
==
NULL
)
{
LOGE
(
"sha256 pointer is NULL! will not test!"
);
return
;
}
Uint8Buff
msg
=
{
(
uint8_t
*
)
malloc
(
SHA256_MSG_MAX_SIZE
),
SHA256_MSG_MAX_SIZE
};
TEST_ASSERT_NOT_NULL
(
msg
.
val
);
Uint8Buff
sha256
=
{
(
uint8_t
*
)
malloc
(
SHA256_LEN
),
SHA256_LEN
};
TEST_ASSERT_NOT_NULL
(
sha256
.
val
);
Uint8Buff
sha256Compare
=
{
(
uint8_t
*
)
malloc
(
SHA256_LEN
),
SHA256_LEN
};
TEST_ASSERT_NOT_NULL
(
sha256Compare
.
val
);
for
(
int
i
=
1
;
i
<=
SHA256_TEST_MIN_LIMIT
;
++
i
)
{
msg
.
length
=
i
;
LOGI
(
"test sha256 with msg length %d"
,
msg
.
length
);
FillRandom
(
msg
.
val
,
SHA256_MSG_MAX_SIZE
,
msg
.
length
);
TestSha256Inner
(
loader
,
&
msg
,
&
sha256
,
&
sha256Compare
);
}
msg
.
length
=
SHA256_MSG_MAX_SIZE
;
LOGI
(
"test sha256 with max msg length %d"
,
msg
.
length
);
FillRandom
(
msg
.
val
,
SHA256_MSG_MAX_SIZE
,
msg
.
length
);
TestSha256Inner
(
loader
,
&
msg
,
&
sha256
,
&
sha256Compare
);
for
(
int
i
=
0
;
i
<
SHA256_TEST_TIMES
;
++
i
)
{
msg
.
length
=
rand
()
%
SHA256_MSG_MAX_SIZE
+
1
;
LOGI
(
"test sha256 with random msg length %d"
,
msg
.
length
);
FillRandom
(
msg
.
val
,
SHA256_MSG_MAX_SIZE
,
msg
.
length
);
TestSha256Inner
(
loader
,
&
msg
,
&
sha256
,
&
sha256Compare
);
}
msg
.
val
=
(
uint8_t
*
)(
SHA256_EXAMPLE_MESSAGE
);
msg
.
length
=
strlen
(
SHA256_EXAMPLE_MESSAGE
);
TestSha256Inner
(
loader
,
&
msg
,
&
sha256
,
&
sha256Compare
);
TEST_ASSERT_EQUAL_HEX8_ARRAY_MESSAGE
(
sha256
.
val
,
SHA256_EXAMPLE_RESULT
,
SHA256_LEN
,
"incorrect sha256 result"
);
PrintBuffer
(
SHA256_EXAMPLE_MESSAGE
,
sha256
.
val
,
sha256
.
length
);
free
(
msg
.
val
);
free
(
sha256
.
val
);
free
(
sha256Compare
.
val
);
}
static
void
TestGenerateRandomInner
(
const
AlgLoader
*
loader
,
Uint8Buff
*
random
)
{
uint32_t
randomLength
=
random
->
length
;
int
res
;
res
=
memset_s
(
random
->
val
,
GEN_RANDOM_MAX_SIZE
,
0
,
GEN_RANDOM_MAX_SIZE
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
generateRandom
(
random
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
TEST_ASSERT_EQUAL
(
randomLength
,
random
->
length
);
uint32_t
zeroCount
=
CountZero
(
random
->
val
,
random
->
length
);
TEST_ASSERT_NOT_EQUAL_MESSAGE
(
random
->
length
,
zeroCount
,
"random all zero"
);
}
static
void
TestGenerateRandomNoPeriodicRepetition
(
const
AlgLoader
*
loader
,
Uint8Buff
*
previous
,
Uint8Buff
*
current
)
{
int
res
;
res
=
memset_s
(
previous
->
val
,
GEN_RANDOM_TEST_REPEAT_SIZE
,
0
,
GEN_RANDOM_TEST_REPEAT_SIZE
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
for
(
int
i
=
0
;
i
<
GEN_RANDOM_TEST_REPEAT_TIMES
;
++
i
)
{
res
=
memset_s
(
current
->
val
,
GEN_RANDOM_TEST_REPEAT_SIZE
,
0
,
GEN_RANDOM_TEST_REPEAT_SIZE
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
generateRandom
(
current
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
TEST_ASSERT_EQUAL
(
GEN_RANDOM_TEST_REPEAT_SIZE
,
current
->
length
);
int
equalCount
=
0
;
for
(
int
j
=
0
;
j
<
GEN_RANDOM_TEST_REPEAT_SIZE
;
++
j
)
{
if
(
previous
->
val
[
j
]
==
current
->
val
[
j
])
{
++
equalCount
;
}
}
TEST_ASSERT_NOT_EQUAL_MESSAGE
(
GEN_RANDOM_TEST_REPEAT_SIZE
,
equalCount
,
"random is the same as previous generated"
);
res
=
memcpy_s
(
previous
->
val
,
GEN_RANDOM_TEST_REPEAT_SIZE
,
current
->
val
,
GEN_RANDOM_TEST_REPEAT_SIZE
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
}
}
static
void
TestGenerateRandom
(
const
AlgLoader
*
loader
)
{
TEST_ASSERT_NOT_NULL
(
loader
->
generateRandom
);
if
(
loader
->
generateRandom
==
NULL
)
{
LOGE
(
"generateRandom pointer is NULL! will not test!"
);
return
;
}
Uint8Buff
random
=
{
(
uint8_t
*
)
malloc
(
GEN_RANDOM_MAX_SIZE
),
GEN_RANDOM_MAX_SIZE
};
TEST_ASSERT_NOT_NULL
(
random
.
val
);
for
(
int
i
=
1
;
i
<=
GEN_RANDOM_MIN_LIMIT
;
++
i
)
{
random
.
length
=
i
;
LOGI
(
"test generate random with length %d"
,
random
.
length
);
TestGenerateRandomInner
(
loader
,
&
random
);
}
random
.
length
=
GEN_RANDOM_MAX_SIZE
;
LOGI
(
"test generate random with max length %d"
,
random
.
length
);
TestGenerateRandomInner
(
loader
,
&
random
);
for
(
int
i
=
0
;
i
<
GEN_RANDOM_TEST_TIMES
;
++
i
)
{
random
.
length
=
rand
()
%
GEN_RANDOM_MAX_SIZE
+
1
;
LOGI
(
"test generate random with random length %d"
,
random
.
length
);
TestGenerateRandomInner
(
loader
,
&
random
);
}
// test whether the random number is repeated
Uint8Buff
previousUint8Buff
=
{
(
uint8_t
*
)
malloc
(
GEN_RANDOM_TEST_REPEAT_SIZE
),
GEN_RANDOM_TEST_REPEAT_SIZE
};
TEST_ASSERT_NOT_NULL
(
previousUint8Buff
.
val
);
Uint8Buff
currentUint8Buff
=
{
(
uint8_t
*
)
malloc
(
GEN_RANDOM_TEST_REPEAT_SIZE
),
GEN_RANDOM_TEST_REPEAT_SIZE
};
TEST_ASSERT_NOT_NULL
(
currentUint8Buff
.
val
);
TestGenerateRandomNoPeriodicRepetition
(
loader
,
&
previousUint8Buff
,
&
currentUint8Buff
);
free
(
currentUint8Buff
.
val
);
free
(
previousUint8Buff
.
val
);
free
(
random
.
val
);
}
// key will not be modified
// message will be malloced if message->val is NULL
// hmac will be clear with zero
static
void
TestComputeHmacInner
(
const
AlgLoader
*
loader
,
const
Uint8Buff
*
key
,
Uint8Buff
*
msg
,
Uint8Buff
*
hmac
,
bool
isAlias
)
{
int32_t
res
;
bool
allocMemory
=
false
;
if
(
msg
->
val
==
NULL
)
{
msg
->
val
=
(
uint8_t
*
)
malloc
(
msg
->
length
);
TEST_ASSERT_NOT_NULL
(
msg
->
val
);
allocMemory
=
true
;
res
=
memset_s
(
msg
->
val
,
msg
->
length
,
0
,
msg
->
length
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
FillRandom
(
msg
->
val
,
msg
->
length
,
msg
->
length
);
}
res
=
memset_s
(
hmac
->
val
,
hmac
->
length
,
0
,
hmac
->
length
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
computeHmac
(
key
,
msg
,
hmac
,
isAlias
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
uint32_t
zeroCount
=
CountZero
(
hmac
->
val
,
hmac
->
length
);
TEST_ASSERT_NOT_EQUAL_MESSAGE
(
hmac
->
length
,
zeroCount
,
"invalid all zero hmac"
);
if
(
allocMemory
&&
msg
->
val
!=
NULL
)
{
free
(
msg
->
val
);
msg
->
val
=
NULL
;
}
}
static
void
TestComputeHmacWithIsAliasTrue
(
const
AlgLoader
*
loader
,
Uint8Buff
*
msg
,
Uint8Buff
*
hmacKeyBuff
,
Uint8Buff
*
outHmacBuff
)
#if TEST_COMPUTEHMAC_WITH_ISALIAS_TRUE
{
TEST_ASSERT_NOT_NULL
(
loader
->
importAsymmetricKey
);
TEST_ASSERT_NOT_NULL
(
loader
->
deleteKey
);
if
(
loader
->
importAsymmetricKey
==
NULL
||
loader
->
deleteKey
==
NULL
)
{
LOGE
(
"importAsymmetricKey or deleteKey is NULL, can not test computeHmac with isAlias true"
);
return
;
}
LOGI
(
"test hmac for isAlias true"
);
msg
->
val
=
(
uint8_t
*
)
NULL
;
msg
->
length
=
TEST_HMAC_ISALIAS_TRUE_MSG_LENGTH
;
FillRandom
(
hmacKeyBuff
->
val
,
hmacKeyBuff
->
length
,
hmacKeyBuff
->
length
);
// write key with specified alias first, then do the test
const
ExtraInfo
info
=
{
.
authId
=
*
hmacKeyBuff
,
.
userType
=
DEVICE_TYPE_ACCESSORY
,
.
pairType
=
PAIR_TYPE_BIND
,
};
int
res
=
loader
->
importAsymmetricKey
(
hmacKeyBuff
,
hmacKeyBuff
,
&
info
);
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
TestComputeHmacInner
(
loader
,
hmacKeyBuff
,
msg
,
outHmacBuff
,
true
);
// delete key after the test
res
=
loader
->
deleteKey
(
hmacKeyBuff
);
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
}
#else // TEST_COMPUTEHMAC_WITH_ISALIAS_TRUE
{
LOGE
(
"no TEST_COMPUTEHMAC_WITH_ISALIAS_TRUE, do not test computeHmac with isAlias true"
);
TEST_ASSERT_TRUE
(
loader
->
importAsymmetricKey
==
NULL
||
loader
->
deleteKey
==
NULL
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
importAsymmetricKey
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
deleteKey
);
(
void
)(
msg
);
(
void
)(
hmacKeyBuff
);
(
void
)(
outHmacBuff
);
}
#endif // TEST_COMPUTEHMAC_WITH_ISALIAS_TRUE
static
void
TestComputeHmac
(
const
AlgLoader
*
loader
)
{
TEST_ASSERT_NOT_NULL
(
loader
->
computeHmac
);
if
(
loader
->
computeHmac
==
NULL
)
{
LOGE
(
"computeHmac pointer is NULL! will not test!"
);
return
;
}
Uint8Buff
hmacKeyBuff
=
{
(
uint8_t
*
)
malloc
(
TEST_HMAC_KEY_LEN
),
TEST_HMAC_KEY_LEN
};
TEST_ASSERT_NOT_NULL
(
hmacKeyBuff
.
val
);
Uint8Buff
msg
=
{
(
uint8_t
*
)
NULL
,
0
};
uint8_t
*
outHmac
=
(
uint8_t
*
)
malloc
(
HMAC_LEN
);
TEST_ASSERT_NOT_NULL
(
outHmac
);
Uint8Buff
outHmacBuff
=
{
(
uint8_t
*
)
outHmac
,
HMAC_LEN
};
TestComputeHmacWithIsAliasTrue
(
loader
,
&
msg
,
&
hmacKeyBuff
,
&
outHmacBuff
);
LOGI
(
"test hmac for isAlias false"
);
for
(
uint32_t
i
=
0
;
i
<
ARRAY_SIZE
(
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_ARRAY
);
++
i
)
{
msg
.
val
=
(
uint8_t
*
)
NULL
;
msg
.
length
=
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_ARRAY
[
i
];
LOGI
(
"test hmac for msg length %d"
,
msg
.
length
);
FillRandom
(
hmacKeyBuff
.
val
,
hmacKeyBuff
.
length
,
hmacKeyBuff
.
length
);
TestComputeHmacInner
(
loader
,
&
hmacKeyBuff
,
&
msg
,
&
outHmacBuff
,
false
);
}
free
(
hmacKeyBuff
.
val
);
hmacKeyBuff
.
val
=
(
uint8_t
*
)
NULL
;
LOGI
(
"test the correctness of hmac"
);
hmacKeyBuff
.
val
=
(
uint8_t
*
)(
HMAC_EXAMPLE_KEY
),
hmacKeyBuff
.
length
=
sizeof
(
HMAC_EXAMPLE_KEY
);
msg
.
val
=
(
uint8_t
*
)(
HMAC_EXAMPLE_MESSAGE
),
msg
.
length
=
strlen
(
HMAC_EXAMPLE_MESSAGE
);
TestComputeHmacInner
(
loader
,
&
hmacKeyBuff
,
&
msg
,
&
outHmacBuff
,
false
);
TEST_ASSERT_EQUAL
(
sizeof
(
HMAC_EXAMPLE_RESULT
),
outHmacBuff
.
length
);
TEST_ASSERT_EQUAL_HEX8_ARRAY_MESSAGE
(
HMAC_EXAMPLE_RESULT
,
outHmacBuff
.
val
,
sizeof
(
HMAC_EXAMPLE_RESULT
),
"incorrect hmac result"
);
PrintBuffer
(
"hmac"
,
outHmacBuff
.
val
,
outHmacBuff
.
length
);
free
(
outHmac
);
outHmac
=
NULL
;
outHmacBuff
.
val
=
(
uint8_t
*
)
NULL
;
}
struct
HkdfArgument
{
Uint8Buff
baseKey
;
Uint8Buff
salt
;
Uint8Buff
keyInfo
;
Uint8Buff
outHkdf
;
bool
isAlias
;
};
// baseKey, salt will be malloced and fill random if NULL
// keyInfo, isAlias will not be modified
// outHkdf will be clear with zero
static
void
TestComputeHkdfInner
(
const
AlgLoader
*
loader
,
struct
HkdfArgument
*
arg
)
{
bool
baseAlloced
=
false
,
saltAlloced
=
false
;
if
(
arg
->
baseKey
.
val
==
NULL
)
{
arg
->
baseKey
.
val
=
(
uint8_t
*
)
malloc
(
arg
->
baseKey
.
length
);
TEST_ASSERT_NOT_NULL
(
arg
->
baseKey
.
val
);
baseAlloced
=
true
;
FillRandom
(
arg
->
baseKey
.
val
,
arg
->
baseKey
.
length
,
arg
->
baseKey
.
length
);
}
if
(
arg
->
salt
.
val
==
NULL
)
{
arg
->
salt
.
val
=
(
uint8_t
*
)
malloc
(
arg
->
salt
.
length
);
TEST_ASSERT_NOT_NULL
(
arg
->
salt
.
val
);
saltAlloced
=
true
;
FillRandom
(
arg
->
salt
.
val
,
arg
->
salt
.
length
,
arg
->
salt
.
length
);
}
int32_t
res
;
res
=
memset_s
(
arg
->
outHkdf
.
val
,
arg
->
outHkdf
.
length
,
0
,
arg
->
outHkdf
.
length
);
TEST_ASSERT_EQUAL
(
EOK
,
res
);
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
computeHkdf
(
&
arg
->
baseKey
,
&
arg
->
salt
,
&
arg
->
keyInfo
,
&
arg
->
outHkdf
,
arg
->
isAlias
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
if
(
saltAlloced
&&
arg
->
salt
.
val
!=
NULL
)
{
free
(
arg
->
salt
.
val
);
arg
->
salt
.
val
=
(
uint8_t
*
)
NULL
;
}
if
(
baseAlloced
&&
arg
->
baseKey
.
val
!=
NULL
)
{
free
(
arg
->
baseKey
.
val
);
arg
->
baseKey
.
val
=
(
uint8_t
*
)
NULL
;
}
uint32_t
zeroCount
=
CountZero
(
arg
->
outHkdf
.
val
,
arg
->
outHkdf
.
length
);
TEST_ASSERT_NOT_EQUAL_MESSAGE
(
arg
->
outHkdf
.
length
,
zeroCount
,
"invalid all zero hkdf result"
);
}
#if TEST_HKDF_WITH_KEY_ALIAS_TRUE // {
static
void
TestComputeHkdfWithKeyAliasTrueFirstAndSecondStep
(
const
AlgLoader
*
loader
,
Uint8Buff
*
privKeyPairAlias
,
Uint8Buff
*
pubKeyPairAlias
,
ExtraInfo
*
extraInfo
,
Uint8Buff
*
sharedKeyAlias
)
{
LOGI
(
"First, generate two pairs of public-private key pairs"
);
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
generateKeyPairWithStorage
(
privKeyPairAlias
,
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_KEY_PAIR_KEY_ALIAS_LENGTH
,
ED25519
,
extraInfo
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
generateKeyPairWithStorage
(
pubKeyPairAlias
,
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_KEY_PAIR_KEY_ALIAS_LENGTH
,
ED25519
,
extraInfo
));
LOGI
(
"Second, agree shared key with privateKey and publicKey"
);
KeyBuff
privKeyBuff
=
{
privKeyPairAlias
->
val
,
privKeyPairAlias
->
length
,
true
};
KeyBuff
pubKeyBuff
=
{
pubKeyPairAlias
->
val
,
pubKeyPairAlias
->
length
,
true
};
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
agreeSharedSecretWithStorage
(
&
privKeyBuff
,
&
pubKeyBuff
,
ED25519
,
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_SHARED_KEY_LENGTH
,
sharedKeyAlias
));
}
static
void
TestComputeHkdfWithKeyAliasTrueThirdStep
(
const
AlgLoader
*
loader
,
struct
HkdfArgument
*
argument
,
Uint8Buff
*
sharedKeyAlias
)
{
LOGI
(
"Third, compute hkdf with shared key"
);
argument
->
baseKey
=
*
sharedKeyAlias
;
argument
->
salt
.
val
=
(
uint8_t
*
)
NULL
;
argument
->
salt
.
length
=
HKDF_PAKE_PSK_DERIVE_SECRET_ALIAS_SALT_LEN
;
argument
->
keyInfo
.
val
=
(
uint8_t
*
)(
TMP_AUTH_KEY_FACTOR
);
argument
->
keyInfo
.
length
=
strlen
(
TMP_AUTH_KEY_FACTOR
);
argument
->
outHkdf
.
val
=
(
uint8_t
*
)
malloc
(
HKDF_PAKE_PSK_DERIVE_SECRET_ALIAS_OUTKEY_LEN
);
TEST_ASSERT_NOT_NULL
(
argument
->
outHkdf
.
val
);
argument
->
outHkdf
.
length
=
HKDF_PAKE_PSK_DERIVE_SECRET_ALIAS_OUTKEY_LEN
;
argument
->
isAlias
=
true
;
TestComputeHkdfInner
(
loader
,
argument
);
free
(
argument
->
outHkdf
.
val
);
argument
->
outHkdf
.
val
=
(
uint8_t
*
)
NULL
;
}
static
void
TestComputeHkdfWithKeyAliasTrue
(
const
AlgLoader
*
loader
,
struct
HkdfArgument
*
argument
)
{
LOGI
(
"pake psk derive secret keyAlias true"
);
TEST_ASSERT_NOT_NULL
(
loader
->
generateKeyPairWithStorage
);
TEST_ASSERT_NOT_NULL
(
loader
->
agreeSharedSecretWithStorage
);
TEST_ASSERT_NOT_NULL
(
loader
->
deleteKey
);
if
(
!
loader
->
generateKeyPairWithStorage
||
!
loader
->
agreeSharedSecretWithStorage
||
!
loader
->
deleteKey
)
{
LOGE
(
"generateKeyPairWithStorage or agreeSharedSecretWithStorage or deleteKey pointer is NULL! "
"can not test hkdf with alias true!"
);
return
;
}
Uint8Buff
privKeyPairAlias
=
{
(
uint8_t
*
)
malloc
(
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_KEY_PAIR_KEY_ALIAS_LENGTH
),
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_KEY_PAIR_KEY_ALIAS_LENGTH
};
Uint8Buff
pubKeyPairAlias
=
{
(
uint8_t
*
)
malloc
(
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_KEY_PAIR_KEY_ALIAS_LENGTH
),
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_KEY_PAIR_KEY_ALIAS_LENGTH
};
ExtraInfo
extraInfo
=
{
{
(
uint8_t
*
)
malloc
(
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_AUTH_ID_LENGTH
),
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_AUTH_ID_LENGTH
},
DEVICE_TYPE_ACCESSORY
,
PAIR_TYPE_BIND
};
TEST_ASSERT_NOT_NULL
(
privKeyPairAlias
.
val
);
TEST_ASSERT_NOT_NULL
(
pubKeyPairAlias
.
val
);
TEST_ASSERT_NOT_NULL
(
extraInfo
.
authId
.
val
);
FillRandom
(
privKeyPairAlias
.
val
,
privKeyPairAlias
.
length
,
privKeyPairAlias
.
length
);
FillRandom
(
pubKeyPairAlias
.
val
,
pubKeyPairAlias
.
length
,
pubKeyPairAlias
.
length
);
FillRandom
(
extraInfo
.
authId
.
val
,
extraInfo
.
authId
.
length
,
extraInfo
.
authId
.
length
);
Uint8Buff
sharedKeyAlias
=
{
(
uint8_t
*
)
malloc
(
HKDF_PAKE_PSK_DERIVE_SECRET_ALIAS_BASEKEY_LEN
),
HKDF_PAKE_PSK_DERIVE_SECRET_ALIAS_BASEKEY_LEN
};
TEST_ASSERT_NOT_NULL
(
sharedKeyAlias
.
val
);
FillRandom
(
sharedKeyAlias
.
val
,
sharedKeyAlias
.
length
,
sharedKeyAlias
.
length
);
TestComputeHkdfWithKeyAliasTrueFirstAndSecondStep
(
loader
,
&
privKeyPairAlias
,
&
pubKeyPairAlias
,
&
extraInfo
,
&
sharedKeyAlias
);
TestComputeHkdfWithKeyAliasTrueThirdStep
(
loader
,
argument
,
&
sharedKeyAlias
);
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
deleteKey
(
&
privKeyPairAlias
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
deleteKey
(
&
pubKeyPairAlias
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
deleteKey
(
&
sharedKeyAlias
));
LOGD
(
"At last, free all memories allocated"
);
free
(
privKeyPairAlias
.
val
);
privKeyPairAlias
.
val
=
(
uint8_t
*
)
NULL
;
free
(
pubKeyPairAlias
.
val
);
pubKeyPairAlias
.
val
=
(
uint8_t
*
)
NULL
;
free
(
extraInfo
.
authId
.
val
);
extraInfo
.
authId
.
val
=
(
uint8_t
*
)
NULL
;
free
(
sharedKeyAlias
.
val
);
sharedKeyAlias
.
val
=
(
uint8_t
*
)
NULL
;
}
#else // TEST_HKDF_WITH_KEY_ALIAS_TRUE // } {
static
void
TestComputeHkdfWithKeyAliasTrue
(
const
AlgLoader
*
loader
,
struct
HkdfArgument
*
argument
)
{
LOGE
(
"no TEST_HKDF_WITH_KEY_ALIAS_TRUE, do not test hkdf with key alias true"
);
TEST_ASSERT_TRUE
(
loader
->
generateKeyPairWithStorage
==
NULL
||
loader
->
agreeSharedSecretWithStorage
==
NULL
||
loader
->
deleteKey
==
NULL
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
generateKeyPairWithStorage
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
agreeSharedSecretWithStorage
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
deleteKey
);
(
void
)(
argument
);
}
#endif // TEST_HKDF_WITH_KEY_ALIAS_TRUE // }
static
void
TestComputeHkdfCorrectness
(
const
AlgLoader
*
loader
,
struct
HkdfArgument
*
argument
)
{
LOGI
(
"test the correctness of hkdf"
);
argument
->
baseKey
.
val
=
(
uint8_t
*
)(
HKDF_EXAMPLE_MESSAGE
);
argument
->
baseKey
.
length
=
strlen
(
HKDF_EXAMPLE_MESSAGE
);
argument
->
salt
.
val
=
(
uint8_t
*
)(
HKDF_EXAMPLE_SALT
);
argument
->
salt
.
length
=
sizeof
(
HKDF_EXAMPLE_SALT
);
argument
->
keyInfo
.
val
=
(
uint8_t
*
)(
HKDF_EXAMPLE_INFO
);
argument
->
keyInfo
.
length
=
strlen
(
HKDF_EXAMPLE_INFO
);
argument
->
outHkdf
.
val
=
(
uint8_t
*
)
malloc
(
sizeof
(
HKDF_EXAMPLE_RESULT_KEY
));
TEST_ASSERT_NOT_NULL
(
argument
->
outHkdf
.
val
);
argument
->
outHkdf
.
length
=
sizeof
(
HKDF_EXAMPLE_RESULT_KEY
);
argument
->
isAlias
=
false
;
TestComputeHkdfInner
(
loader
,
argument
);
TEST_ASSERT_EQUAL
(
sizeof
(
HKDF_EXAMPLE_RESULT_KEY
),
argument
->
outHkdf
.
length
);
TEST_ASSERT_EQUAL_HEX8_ARRAY_MESSAGE
(
HKDF_EXAMPLE_RESULT_KEY
,
argument
->
outHkdf
.
val
,
sizeof
(
HKDF_EXAMPLE_RESULT_KEY
),
"incorrect hkdf result"
);
PrintBuffer
(
"hkdf result"
,
argument
->
outHkdf
.
val
,
argument
->
outHkdf
.
length
);
free
(
argument
->
outHkdf
.
val
);
argument
->
outHkdf
.
val
=
(
uint8_t
*
)
NULL
;
}
static
void
TestComputeHkdf
(
const
AlgLoader
*
loader
)
{
TEST_ASSERT_NOT_NULL
(
loader
->
computeHkdf
);
if
(
loader
->
computeHkdf
==
NULL
)
{
LOGE
(
"computeHkdf pointer is NULL! will not test!"
);
return
;
}
struct
HkdfArgument
argument
;
int
res
;
res
=
memset_s
(
&
argument
,
sizeof
(
argument
),
0
,
sizeof
(
argument
));
TEST_ASSERT_EQUAL
(
EOK
,
res
);
for
(
uint32_t
i
=
0
;
i
<
ARRAY_SIZE
(
g_hkdfTestCase
);
++
i
)
{
LOGI
(
"test case for:
\"
%s
\"
"
,
g_hkdfTestCase
[
i
].
caseName
);
LOGI
(
"test case for: baseKeyLength = %d, saltLength = %d, keyInfo =
\"
%s
\"
, outKeyLength = %d"
,
g_hkdfTestCase
[
i
].
baseKeyLength
,
g_hkdfTestCase
[
i
].
saltLength
,
g_hkdfTestCase
[
i
].
keyInfo
,
g_hkdfTestCase
[
i
].
outKeyLength
);
argument
.
baseKey
.
val
=
(
uint8_t
*
)
NULL
;
argument
.
baseKey
.
length
=
g_hkdfTestCase
[
i
].
baseKeyLength
;
argument
.
salt
.
val
=
(
uint8_t
*
)
NULL
;
argument
.
salt
.
length
=
g_hkdfTestCase
[
i
].
saltLength
;
argument
.
keyInfo
.
val
=
(
uint8_t
*
)(
g_hkdfTestCase
[
i
].
keyInfo
);
argument
.
keyInfo
.
length
=
strlen
(
g_hkdfTestCase
[
i
].
keyInfo
);
argument
.
outHkdf
.
val
=
(
uint8_t
*
)
malloc
(
g_hkdfTestCase
[
i
].
outKeyLength
);
TEST_ASSERT_NOT_NULL
(
argument
.
outHkdf
.
val
);
argument
.
outHkdf
.
length
=
g_hkdfTestCase
[
i
].
outKeyLength
;
argument
.
isAlias
=
false
;
TestComputeHkdfInner
(
loader
,
&
argument
);
free
(
argument
.
outHkdf
.
val
);
argument
.
outHkdf
.
val
=
(
uint8_t
*
)
NULL
;
}
TestComputeHkdfWithKeyAliasTrue
(
loader
,
&
argument
);
TestComputeHkdfCorrectness
(
loader
,
&
argument
);
}
#if TEST_IMPORT_ASYMMETRIC_KEY // {
enum
{
WAIT_FOR_WATCH_DOG
=
1
,
};
static
void
TestImportAsymmetricKey
(
const
AlgLoader
*
loader
)
{
TEST_ASSERT_NOT_NULL
(
loader
->
importAsymmetricKey
);
if
(
loader
->
importAsymmetricKey
==
NULL
)
{
LOGE
(
"importAsymmetricKey pointer is NULL! will not test!"
);
return
;
}
int
res
;
const
uint32_t
authIdLenArray
[]
=
{
TEST_IMPORT_ASYMMETRIC_KEY_AUTH_ID_LENGTH_32
,
TEST_IMPORT_ASYMMETRIC_KEY_AUTH_ID_LENGTH_64
};
for
(
uint32_t
i
=
0
;
i
<
ARRAY_SIZE
(
authIdLenArray
);
++
i
)
{
for
(
uint32_t
j
=
IMPORT_ASYMMETRIC_KEY_KEYALIAS_LEN_MIN
;
j
<=
IMPORT_ASYMMETRIC_KEY_KEYALIAS_LEN_MAX
;
++
j
)
{
LOGD
(
"auth id length = %d, msg length = %d"
,
authIdLenArray
[
i
],
j
);
Uint8Buff
keyAlias
=
{
(
uint8_t
*
)
malloc
(
j
),
j
};
Uint8Buff
authToken
=
{
(
uint8_t
*
)
malloc
(
IMPORT_ASYMMETRIC_KEY_AUTHTOKEN_LEN
),
IMPORT_ASYMMETRIC_KEY_AUTHTOKEN_LEN
};
ExtraInfo
extraInfo
=
{
{
(
uint8_t
*
)
malloc
(
authIdLenArray
[
i
]),
authIdLenArray
[
i
]
},
DEVICE_TYPE_ACCESSORY
,
PAIR_TYPE_BIND
};
TEST_ASSERT_NOT_NULL
(
keyAlias
.
val
);
TEST_ASSERT_NOT_NULL
(
authToken
.
val
);
TEST_ASSERT_NOT_NULL
(
extraInfo
.
authId
.
val
);
FillRandom
(
keyAlias
.
val
,
keyAlias
.
length
,
keyAlias
.
length
);
FillRandom
(
authToken
.
val
,
authToken
.
length
,
authToken
.
length
);
FillRandom
(
extraInfo
.
authId
.
val
,
extraInfo
.
authId
.
length
,
extraInfo
.
authId
.
length
);
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
importAsymmetricKey
(
&
keyAlias
,
&
authToken
,
&
extraInfo
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
LOGD
(
"test exist"
);
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
checkKeyExist
(
&
keyAlias
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
LOGD
(
"test delete"
);
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
deleteKey
(
&
keyAlias
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
free
(
keyAlias
.
val
);
keyAlias
.
val
=
(
uint8_t
*
)
NULL
;
free
(
authToken
.
val
);
authToken
.
val
=
(
uint8_t
*
)
NULL
;
free
(
extraInfo
.
authId
.
val
);
extraInfo
.
authId
.
val
=
(
uint8_t
*
)
NULL
;
}
LOGI
(
"sleep for %d seconds, waiting for watch dog"
,
WAIT_FOR_WATCH_DOG
);
sleep
(
WAIT_FOR_WATCH_DOG
);
}
}
#else // TEST_IMPORT_ASYMMETRIC_KEY // } {
static
void
TestImportAsymmetricKey
(
const
AlgLoader
*
loader
)
{
LOGE
(
"no TEST_IMPORT_ASYMMETRIC_KEY, do not test importAsymmetricKey"
);
TEST_ASSERT_NULL
(
loader
->
importAsymmetricKey
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
importAsymmetricKey
);
}
#endif // TEST_IMPORT_ASYMMETRIC_KEY // }
static
void
TestCheckKeyExist
(
const
AlgLoader
*
loader
)
{
TEST_ASSERT_NOT_NULL
(
loader
->
checkKeyExist
);
if
(
loader
->
checkKeyExist
==
NULL
)
{
LOGE
(
"checkKeyExist pointer is NULL! will not test!"
);
return
;
}
const
char
bufferKeyAlias
[]
=
"A non-existent key"
;
Uint8Buff
keyAlias
=
{
.
val
=
(
uint8_t
*
)
bufferKeyAlias
,
.
length
=
strlen
(
bufferKeyAlias
),
};
int32_t
res
;
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
checkKeyExist
(
&
keyAlias
));
TEST_ASSERT_NOT_EQUAL
(
HAL_SUCCESS
,
res
);
}
static
void
TestDeleteKey
(
const
AlgLoader
*
loader
)
{
TEST_ASSERT_NOT_NULL
(
loader
->
deleteKey
);
if
(
loader
->
deleteKey
==
NULL
)
{
LOGE
(
"deleteKey pointer is NULL! will not test!"
);
return
;
}
const
char
bufferKeyAlias
[]
=
"A non-existent key"
;
Uint8Buff
keyAlias
=
{
.
val
=
(
uint8_t
*
)
bufferKeyAlias
,
.
length
=
strlen
(
bufferKeyAlias
),
};
int32_t
res
;
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
deleteKey
(
&
keyAlias
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
}
static
void
TestAesGcmEncrypt
(
const
AlgLoader
*
loader
)
{
LOGI
(
"begin to test the AES-GCM algorithm encryption"
);
TEST_ASSERT_NOT_NULL
(
loader
->
aesGcmEncrypt
);
if
(
loader
->
aesGcmEncrypt
==
NULL
)
{
LOGE
(
"aesGcmEncrypt pointer is NULL! will not test!"
);
return
;
}
const
Uint8Buff
key
=
{
(
uint8_t
*
)
AES_GCM_TEST_KEY
,
AES_GCM_KEY_LEN
};
const
Uint8Buff
plainBuffer
=
{
(
uint8_t
*
)
PLAIN_CASE
,
PLAIN_LEN
};
uint8_t
*
nonce
=
(
uint8_t
*
)
malloc
(
NONCE_SIZE
);
TEST_ASSERT_NOT_NULL
(
nonce
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
nonce
,
NONCE_SIZE
,
0
,
NONCE_SIZE
));
GcmParam
gcmParams
;
gcmParams
.
aad
=
(
uint8_t
*
)
EXCHANGE_AAD
;
gcmParams
.
aadLen
=
(
uint32_t
)
strlen
(
EXCHANGE_AAD
);
gcmParams
.
nonce
=
nonce
;
gcmParams
.
nonceLen
=
NONCE_SIZE
;
uint8_t
*
cipher
=
(
uint8_t
*
)
malloc
(
CIPHER_LEN
);
TEST_ASSERT_NOT_NULL
(
cipher
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
cipher
,
CIPHER_LEN
,
0
,
CIPHER_LEN
));
Uint8Buff
cipherBuffer
=
{
cipher
,
CIPHER_LEN
};
int
ret
;
RUN_AND_PRINT_ELAPSED_TIME
(
ret
,
loader
->
aesGcmEncrypt
(
&
key
,
&
plainBuffer
,
&
gcmParams
,
false
,
&
cipherBuffer
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
PrintBuffer
(
"AES-GCM cipher"
,
cipher
,
CIPHER_LEN
);
TEST_ASSERT_EQUAL_HEX8_ARRAY
(
AES_GCM_CIPHER_CASE
,
cipher
,
CIPHER_LEN
);
free
(
nonce
);
free
(
cipher
);
}
static
void
TestAesGcmDecrypt
(
const
AlgLoader
*
loader
)
{
LOGI
(
"begin to test the AES-GCM algorithm decryption"
);
TEST_ASSERT_NOT_NULL
(
loader
->
aesGcmDecrypt
);
if
(
loader
->
aesGcmDecrypt
==
NULL
)
{
LOGE
(
"aesGcmDecrypt pointer is NULL! will not test!"
);
return
;
}
const
Uint8Buff
key
=
{
(
uint8_t
*
)
AES_GCM_TEST_KEY
,
AES_GCM_KEY_LEN
};
const
Uint8Buff
cipherBuffer
=
{
(
uint8_t
*
)
AES_GCM_CIPHER_CASE
,
CIPHER_LEN
};
uint8_t
*
nonce
=
(
uint8_t
*
)
malloc
(
NONCE_SIZE
);
TEST_ASSERT_NOT_NULL
(
nonce
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
nonce
,
NONCE_SIZE
,
0
,
NONCE_SIZE
));
GcmParam
gcmParams
;
gcmParams
.
aad
=
(
uint8_t
*
)
EXCHANGE_AAD
;
gcmParams
.
aadLen
=
(
uint32_t
)
strlen
(
EXCHANGE_AAD
);
gcmParams
.
nonce
=
nonce
;
gcmParams
.
nonceLen
=
NONCE_SIZE
;
uint8_t
*
plain
=
(
uint8_t
*
)
malloc
(
PLAIN_LEN
);
TEST_ASSERT_NOT_NULL
(
plain
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
plain
,
PLAIN_LEN
,
0
,
PLAIN_LEN
));
Uint8Buff
plainBuffer
=
{
plain
,
PLAIN_LEN
};
int
ret
;
RUN_AND_PRINT_ELAPSED_TIME
(
ret
,
loader
->
aesGcmDecrypt
(
&
key
,
&
cipherBuffer
,
&
gcmParams
,
false
,
&
plainBuffer
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
PrintBuffer
(
"AES-GCM plain"
,
plain
,
PLAIN_LEN
);
TEST_ASSERT_EQUAL_HEX8_ARRAY
(
PLAIN_CASE
,
plain
,
PLAIN_LEN
);
free
(
nonce
);
free
(
plain
);
}
static
void
TestHashToPoint
(
const
AlgLoader
*
loader
)
#if TEST_HASH_TO_POINT
{
TEST_ASSERT_NOT_NULL
(
loader
->
hashToPoint
);
if
(
loader
->
hashToPoint
==
NULL
)
{
LOGE
(
"hashToPoint pointer is NULL! will not test!"
);
return
;
}
const
Uint8Buff
hash
=
{
(
uint8_t
*
)
HASH_TO_POINT_CASE
,
SHA256_LEN
};
uint8_t
*
point
=
(
uint8_t
*
)
malloc
(
SHA256_LEN
);
TEST_ASSERT_NOT_NULL
(
point
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
point
,
SHA256_LEN
,
0
,
SHA256_LEN
));
Uint8Buff
pointBuffer
=
{
point
,
SHA256_LEN
};
int
ret
;
RUN_AND_PRINT_ELAPSED_TIME
(
ret
,
loader
->
hashToPoint
(
&
hash
,
X25519
,
&
pointBuffer
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
PrintBuffer
(
"hashToPoint"
,
point
,
SHA256_LEN
);
TEST_ASSERT_EQUAL_HEX8_ARRAY
(
HASH_TO_POINT_RESULT
,
point
,
PLAIN_LEN
);
free
(
point
);
}
#else // TEST_HASH_TO_POINT
{
LOGE
(
"no TEST_HASH_TO_POINT, do not test loader->hashToPoint!"
);
TEST_ASSERT_NULL
(
loader
->
hashToPoint
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
hashToPoint
);
}
#endif // TEST_HASH_TO_POINT
#if TEST_AGREE_SHARED_SECRET_WITH_STORAGE
static
void
VerifyAgreeSharedSecretWithStorage
(
const
AlgLoader
*
loader
,
const
Uint8Buff
*
key1
,
const
Uint8Buff
*
key2
)
{
const
Uint8Buff
keyInfo
=
{
(
uint8_t
*
)
HKDF_EXAMPLE_INFO
,
strlen
(
HKDF_EXAMPLE_INFO
)
};
const
Uint8Buff
salt
=
{
(
uint8_t
*
)
HKDF_EXAMPLE_SALT
,
sizeof
(
HKDF_EXAMPLE_SALT
)
};
Uint8Buff
outHkdf1
=
{
(
uint8_t
*
)
malloc
(
ED25519_KEY_BYTE_LEN
),
ED25519_KEY_BYTE_LEN
};
Uint8Buff
outHkdf2
=
{
(
uint8_t
*
)
malloc
(
ED25519_KEY_BYTE_LEN
),
ED25519_KEY_BYTE_LEN
};
TEST_ASSERT_NOT_NULL
(
outHkdf1
.
val
);
TEST_ASSERT_NOT_NULL
(
outHkdf2
.
val
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
outHkdf1
.
val
,
ED25519_KEY_BYTE_LEN
,
0
,
ED25519_KEY_BYTE_LEN
));
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
outHkdf2
.
val
,
ED25519_KEY_BYTE_LEN
,
0
,
ED25519_KEY_BYTE_LEN
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
computeHkdf
(
key1
,
&
salt
,
&
keyInfo
,
&
outHkdf1
,
true
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
computeHkdf
(
key2
,
&
salt
,
&
keyInfo
,
&
outHkdf2
,
true
));
TEST_ASSERT_EQUAL_HEX8_ARRAY
(
outHkdf1
.
val
,
outHkdf2
.
val
,
ED25519_KEY_BYTE_LEN
);
free
(
outHkdf1
.
val
);
free
(
outHkdf2
.
val
);
}
static
int32_t
GenerateKeyPairWithStorage
(
const
AlgLoader
*
loader
,
const
Uint8Buff
*
keyAlias
)
{
const
ExtraInfo
extraInfo
=
{
.
authId
=
{
.
val
=
(
uint8_t
*
)
malloc
(
TEST_GENERATE_KEY_PAIR_WITH_STORAGE_AUTH_ID_LENGTH
),
.
length
=
TEST_GENERATE_KEY_PAIR_WITH_STORAGE_AUTH_ID_LENGTH
,
},
.
userType
=
DEVICE_TYPE_ACCESSORY
,
.
pairType
=
PAIR_TYPE_BIND
,
};
TEST_ASSERT_NOT_NULL
(
extraInfo
.
authId
.
val
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
extraInfo
.
authId
.
val
,
extraInfo
.
authId
.
length
,
0
,
extraInfo
.
authId
.
length
));
int32_t
ret
=
loader
->
generateKeyPairWithStorage
(
keyAlias
,
ED25519_KEY_BYTE_LEN
,
ED25519
,
&
extraInfo
);
free
(
extraInfo
.
authId
.
val
);
return
ret
;
}
static
void
TestAgreeSharedSecretWithStorage
(
const
AlgLoader
*
loader
)
{
TEST_ASSERT_NOT_NULL
(
loader
->
agreeSharedSecretWithStorage
);
TEST_ASSERT_NOT_NULL
(
loader
->
computeHkdf
);
TEST_ASSERT_NOT_NULL
(
loader
->
generateKeyPairWithStorage
);
TEST_ASSERT_NOT_NULL
(
loader
->
deleteKey
);
if
(
!
loader
->
agreeSharedSecretWithStorage
||
!
loader
->
computeHkdf
||
!
loader
->
generateKeyPairWithStorage
||
!
loader
->
deleteKey
)
{
LOGE
(
"one of required function pointer is NULL! will not test!"
);
return
;
}
Uint8Buff
keyAliasA
=
{
(
uint8_t
*
)
KEY_PAIR_ALIAS_1
,
strlen
(
KEY_PAIR_ALIAS_1
)
};
Uint8Buff
keyAliasB
=
{
(
uint8_t
*
)
KEY_PAIR_ALIAS_2
,
strlen
(
KEY_PAIR_ALIAS_2
)
};
int
ret
;
ret
=
GenerateKeyPairWithStorage
(
loader
,
&
keyAliasA
);
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
ret
=
GenerateKeyPairWithStorage
(
loader
,
&
keyAliasB
);
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
if
(
ret
!=
HAL_SUCCESS
)
{
LOGE
(
"GenerateKeyPair failed! will not test!"
);
return
;
}
KeyBuff
keyA
=
{
keyAliasA
.
val
,
keyAliasA
.
length
,
true
};
KeyBuff
keyB
=
{
keyAliasB
.
val
,
keyAliasB
.
length
,
true
};
Uint8Buff
sharedKeyAlias1
=
{
(
uint8_t
*
)
SHARED_KEY_ALIAS_1
,
strlen
(
SHARED_KEY_ALIAS_1
)
};
Uint8Buff
sharedKeyAlias2
=
{
(
uint8_t
*
)
SHARED_KEY_ALIAS_2
,
strlen
(
SHARED_KEY_ALIAS_1
)
};
Algorithm
alg
=
ED25519
;
const
uint32_t
keyLength
=
ED25519_KEY_BYTE_LEN
;
RUN_AND_PRINT_ELAPSED_TIME
(
ret
,
loader
->
agreeSharedSecretWithStorage
(
&
keyA
,
&
keyB
,
alg
,
keyLength
,
&
sharedKeyAlias1
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
RUN_AND_PRINT_ELAPSED_TIME
(
ret
,
loader
->
agreeSharedSecretWithStorage
(
&
keyB
,
&
keyA
,
alg
,
keyLength
,
&
sharedKeyAlias2
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
VerifyAgreeSharedSecretWithStorage
(
loader
,
&
sharedKeyAlias1
,
&
sharedKeyAlias2
);
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
deleteKey
(
&
sharedKeyAlias1
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
deleteKey
(
&
sharedKeyAlias2
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
deleteKey
(
&
keyAliasA
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
deleteKey
(
&
keyAliasB
));
}
#else // TEST_AGREE_SHARED_SECRET_WITH_STORAGE
static
void
TestAgreeSharedSecretWithStorage
(
const
AlgLoader
*
loader
)
{
LOGE
(
"no TEST_AGREE_SHARED_SECRET_WITH_STORAGE, do not test loader->agreeSharedSecretWithStorage!"
);
TEST_ASSERT_TRUE
(
loader
->
agreeSharedSecretWithStorage
==
NULL
||
loader
->
computeHkdf
==
NULL
||
loader
->
generateKeyPairWithStorage
==
NULL
||
loader
->
deleteKey
==
NULL
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
agreeSharedSecretWithStorage
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
computeHkdf
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
generateKeyPairWithStorage
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
deleteKey
);
}
#endif // TEST_AGREE_SHARED_SECRET_WITH_STORAGE
static
void
TestAgreeSharedSecret
(
const
AlgLoader
*
loader
)
#if TEST_AGREE_SHARED_SECRET
{
TEST_ASSERT_NOT_NULL
(
loader
->
agreeSharedSecret
);
if
(
!
loader
->
agreeSharedSecret
)
{
LOGE
(
"agreeSharedSecret pointer is NULL! will not test!"
);
return
;
}
Algorithm
alg
=
X25519
;
const
uint32_t
keyLength
=
X25519_KEY_BYTE_LEN
;
KeyBuff
privateKeyA
=
{
(
uint8_t
*
)
X25519_PRIVATE_KEY_A
,
keyLength
,
false
};
KeyBuff
publicKeyA
=
{
(
uint8_t
*
)
X25519_PUBLIC_KEY_A
,
keyLength
,
false
};
KeyBuff
privateKeyB
=
{
(
uint8_t
*
)
X25519_PRIVATE_KEY_B
,
keyLength
,
false
};
KeyBuff
publicKeyB
=
{
(
uint8_t
*
)
X25519_PUBLIC_KEY_B
,
keyLength
,
false
};
Uint8Buff
sharedKey1
=
{
(
uint8_t
*
)
malloc
(
keyLength
),
keyLength
};
Uint8Buff
sharedKey2
=
{
(
uint8_t
*
)
malloc
(
keyLength
),
keyLength
};
TEST_ASSERT_NOT_NULL
(
sharedKey1
.
val
);
TEST_ASSERT_NOT_NULL
(
sharedKey2
.
val
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
sharedKey1
.
val
,
keyLength
,
0
,
keyLength
));
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
sharedKey2
.
val
,
keyLength
,
0
,
keyLength
));
int
ret
;
RUN_AND_PRINT_ELAPSED_TIME
(
ret
,
loader
->
agreeSharedSecret
(
&
privateKeyA
,
&
publicKeyB
,
alg
,
&
sharedKey1
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
RUN_AND_PRINT_ELAPSED_TIME
(
ret
,
loader
->
agreeSharedSecret
(
&
privateKeyB
,
&
publicKeyA
,
alg
,
&
sharedKey2
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
PrintBuffer
(
"shared key 1"
,
sharedKey1
.
val
,
keyLength
);
PrintBuffer
(
"shared key 2"
,
sharedKey2
.
val
,
keyLength
);
TEST_ASSERT_EQUAL_HEX8_ARRAY
(
sharedKey1
.
val
,
sharedKey2
.
val
,
ED25519_KEY_BYTE_LEN
);
free
(
sharedKey1
.
val
);
free
(
sharedKey2
.
val
);
}
#else // TEST_AGREE_SHARED_SECRET
{
LOGE
(
"no TEST_AGREE_SHARED_SECRET, do not test loader->agreeSharedSecret"
);
TEST_ASSERT_NULL
(
loader
->
agreeSharedSecret
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
agreeSharedSecret
);
}
#endif // TEST_AGREE_SHARED_SECRET
static
void
TestGenerateKeyPairWithStorage
(
const
AlgLoader
*
loader
)
#if TEST_GENERATE_KEY_PAIR_WITH_STORAGE
{
TEST_ASSERT_NOT_NULL
(
loader
->
generateKeyPairWithStorage
);
TEST_ASSERT_NOT_NULL
(
loader
->
checkKeyExist
);
TEST_ASSERT_NOT_NULL
(
loader
->
deleteKey
);
if
(
!
loader
->
generateKeyPairWithStorage
||
!
loader
->
checkKeyExist
||
!
loader
->
deleteKey
)
{
LOGE
(
"one of required function pointer is NULL! will not test!"
);
return
;
}
Uint8Buff
keyAlias
=
{
(
uint8_t
*
)
KEY_PAIR_ALIAS_1
,
strlen
(
KEY_PAIR_ALIAS_1
)
};
const
ExtraInfo
extraInfo
=
{
.
authId
=
{
.
val
=
(
uint8_t
*
)
malloc
(
TEST_GENERATE_KEY_PAIR_WITH_STORAGE_AUTH_ID_LENGTH
),
.
length
=
TEST_GENERATE_KEY_PAIR_WITH_STORAGE_AUTH_ID_LENGTH
,
},
.
userType
=
DEVICE_TYPE_ACCESSORY
,
.
pairType
=
PAIR_TYPE_BIND
,
};
TEST_ASSERT_NOT_NULL
(
extraInfo
.
authId
.
val
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
extraInfo
.
authId
.
val
,
extraInfo
.
authId
.
length
,
0
,
extraInfo
.
authId
.
length
));
int
ret
;
RUN_AND_PRINT_ELAPSED_TIME
(
ret
,
loader
->
generateKeyPairWithStorage
(
&
keyAlias
,
ED25519_KEY_BYTE_LEN
,
ED25519
,
&
extraInfo
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
checkKeyExist
(
&
keyAlias
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
loader
->
deleteKey
(
&
keyAlias
));
free
(
extraInfo
.
authId
.
val
);
}
#else // TEST_GENERATE_KEY_PAIR_WITH_STORAGE
{
LOGE
(
"no TEST_GENERATE_KEY_PAIR_WITH_STORAGE, do not test loader->generateKeyPairWithStorage!"
);
TEST_ASSERT_TRUE
(
loader
->
generateKeyPairWithStorage
==
NULL
||
loader
->
checkKeyExist
==
NULL
||
loader
->
deleteKey
==
NULL
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
generateKeyPairWithStorage
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
checkKeyExist
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
deleteKey
);
}
#endif // TEST_GENERATE_KEY_PAIR_WITH_STORAGE
static
void
TestBigNumExpMod
(
const
AlgLoader
*
loader
)
#if TEST_BIG_NUM_EXP_MOD
{
TEST_ASSERT_NOT_NULL
(
loader
->
bigNumExpMod
);
if
(
loader
->
bigNumExpMod
==
NULL
)
{
LOGE
(
"bigNumExpMod pointer is NULL! will not test!"
);
return
;
}
for
(
uint32_t
i
=
0
;
i
<
ARRAY_SIZE
(
BIG_NUM_TEST_CASES
);
++
i
)
{
uint32_t
baseLength
=
BIG_NUM_TEST_CASES
[
i
].
baseLength
;
uint32_t
expLength
=
BIG_NUM_TEST_CASES
[
i
].
expLength
;
uint32_t
primeLength
=
strlen
(
BIG_NUM_TEST_CASES
[
i
].
prime
)
>>
1
;
const
Uint8Buff
base
=
{
(
uint8_t
*
)
BIG_NUM_TEST_CASES
[
i
].
base
,
baseLength
};
const
Uint8Buff
exp
=
{
(
uint8_t
*
)
BIG_NUM_TEST_CASES
[
i
].
exp
,
expLength
};
Uint8Buff
modResult
=
{
(
uint8_t
*
)
malloc
(
primeLength
),
primeLength
};
TEST_ASSERT_NOT_NULL
(
modResult
.
val
);
TEST_ASSERT_EQUAL
(
EOK
,
memset_s
(
modResult
.
val
,
primeLength
,
0
,
primeLength
));
int
ret
;
LOGI
(
"test the big num case: base = %d, exp = %d, prime = %d"
,
baseLength
,
expLength
,
primeLength
);
RUN_AND_PRINT_ELAPSED_TIME
(
ret
,
loader
->
bigNumExpMod
(
&
base
,
&
exp
,
BIG_NUM_TEST_CASES
[
i
].
prime
,
&
modResult
));
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
ret
);
TEST_ASSERT_EQUAL_HEX8_ARRAY
(
BIG_NUM_TEST_CASES
[
i
].
result
,
modResult
.
val
,
primeLength
);
PrintBuffer
(
"mod result"
,
modResult
.
val
,
modResult
.
length
);
free
(
modResult
.
val
);
}
}
#else // TEST_BIG_NUM_EXP_MOD
{
LOGE
(
"no TEST_BIG_NUM_EXP_MOD, do not test loader->bigNumExpMod!"
);
TEST_ASSERT_NULL
(
loader
->
bigNumExpMod
);
LOG_ERROR_IF_POINTER_NULL
(
loader
->
bigNumExpMod
);
}
#endif // TEST_BIG_NUM_EXP_MOD
void
TestHcAlg
(
void
)
{
const
AlgLoader
*
loader
=
GetLoaderInstance
();
TEST_ASSERT_NOT_NULL
(
loader
);
if
(
loader
==
NULL
)
{
LOGE
(
"alg loader instance is NULL"
);
return
;
}
int32_t
res
;
RUN_AND_PRINT_ELAPSED_TIME
(
res
,
loader
->
initAlg
());
TEST_ASSERT_EQUAL
(
HAL_SUCCESS
,
res
);
TestSha256
(
loader
);
TestGenerateRandom
(
loader
);
TestComputeHmac
(
loader
);
TestComputeHkdf
(
loader
);
TestImportAsymmetricKey
(
loader
);
TestCheckKeyExist
(
loader
);
TestDeleteKey
(
loader
);
TestAesGcmEncrypt
(
loader
);
TestAesGcmDecrypt
(
loader
);
TestHashToPoint
(
loader
);
TestGenerateKeyPairWithStorage
(
loader
);
TestAgreeSharedSecretWithStorage
(
loader
);
TestAgreeSharedSecret
(
loader
);
TestBigNumExpMod
(
loader
);
}
#ifdef __cplusplus
}
#endif
security_lite/deviceauth_basic_deps/hc_alg_test.h
0 → 100644
浏览文件 @
fd279506
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HC_ALG_TEST_H
#define HC_ALG_TEST_H
#ifdef __cplusplus
extern
"C"
{
#endif
void
TestHcAlg
(
void
);
#ifdef __cplusplus
}
#endif
#endif // HC_ALG_TEST_H
security_lite/deviceauth_basic_deps/hc_alg_test_case.h
0 → 100644
浏览文件 @
fd279506
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HC_ALG_TEST_CASE_H
#define HC_ALG_TEST_CASE_H
#include <ohos_types.h> // ARRAY_SIZE
#include <stdint.h>
#ifdef __cplusplus
extern
"C"
{
#endif
#define KEY_PAIR_ALIAS_1 "test: key pair 1"
#define KEY_PAIR_ALIAS_2 "test: key pair 2"
#define SHARED_KEY_ALIAS_1 "test: shared key 1"
#define SHARED_KEY_ALIAS_2 "test: shared key 2"
#define EXCHANGE_AAD "hichain_iso_exchange"
enum
{
NONCE_SIZE
=
12
,
AES_GCM_KEY_LEN
=
128
/
8
,
TAG_LEN
=
16
,
CIPHER_LEN
=
32
,
PLAIN_LEN
=
16
,
X25519_KEY_BYTE_LEN
=
32
,
ED25519_KEY_BYTE_LEN
=
32
,
};
typedef
struct
{
uint32_t
baseLength
;
uint32_t
expLength
;
const
char
*
prime
;
const
uint8_t
*
base
;
const
uint8_t
*
exp
;
const
uint8_t
*
result
;
}
BigNumTestCase
;
static
const
uint8_t
AES_GCM_TEST_KEY
[
AES_GCM_KEY_LEN
]
=
{
0xc7
,
0x4f
,
0x98
,
0x27
,
0x09
,
0xcf
,
0xbe
,
0x04
,
0x03
,
0x81
,
0xbc
,
0x4a
,
0x45
,
0xed
,
0xe9
,
0xcb
};
static
const
uint8_t
AES_GCM_CIPHER_CASE
[
CIPHER_LEN
]
=
{
0xf0
,
0x1f
,
0x08
,
0xb2
,
0xb1
,
0x79
,
0xc9
,
0x65
,
0xc6
,
0x45
,
0x0c
,
0xc9
,
0xb9
,
0xb6
,
0x07
,
0xc0
,
0x27
,
0x0c
,
0x8d
,
0xa0
,
0x33
,
0x6a
,
0x5b
,
0xa9
,
0x0f
,
0x1a
,
0x01
,
0xde
,
0x43
,
0xda
,
0x79
,
0x7f
};
static
const
uint8_t
PLAIN_CASE
[]
=
"This is the case"
;
static
const
uint8_t
HASH_TO_POINT_CASE
[]
=
{
0x75
,
0x6d
,
0xee
,
0x8b
,
0xa1
,
0xce
,
0xce
,
0xa3
,
0x1e
,
0x68
,
0x02
,
0xe8
,
0xe4
,
0xbe
,
0xe0
,
0x9e
,
0x3b
,
0x01
,
0xfa
,
0xe0
,
0x34
,
0x77
,
0x65
,
0x2c
,
0xf0
,
0x2F
,
0x33
,
0x7a
,
0x54
,
0x6b
,
0xd9
,
0xdd
};
static
const
uint8_t
HASH_TO_POINT_RESULT
[]
=
{
0xd4
,
0xa8
,
0x7e
,
0xa6
,
0xd7
,
0x07
,
0xb0
,
0xea
,
0xef
,
0x28
,
0xd8
,
0x98
,
0x89
,
0xcc
,
0xa0
,
0xd8
,
0x65
,
0x4d
,
0x8e
,
0xdd
,
0x4b
,
0x16
,
0x7a
,
0x0d
,
0xde
,
0x4f
,
0xf2
,
0x99
,
0xd3
,
0x4c
,
0xb4
,
0x45
};
static
const
uint8_t
X25519_PRIVATE_KEY_A
[
X25519_KEY_BYTE_LEN
]
=
{
0x6c
,
0xe7
,
0xf5
,
0x5c
,
0x2d
,
0xc8
,
0x89
,
0x6b
,
0xfc
,
0x64
,
0xe8
,
0xc4
,
0x58
,
0xfe
,
0xbe
,
0x13
,
0x45
,
0x01
,
0xa4
,
0x7a
,
0xa1
,
0x60
,
0xcd
,
0x4c
,
0xaa
,
0x7b
,
0x19
,
0x10
,
0x23
,
0x33
,
0xa1
,
0x32
};
static
const
uint8_t
X25519_PUBLIC_KEY_A
[
X25519_KEY_BYTE_LEN
]
=
{
0x10
,
0x65
,
0x4b
,
0x65
,
0xf9
,
0x40
,
0x10
,
0x20
,
0x4b
,
0x2f
,
0xff
,
0xc6
,
0xaf
,
0xb3
,
0xa8
,
0x86
,
0x69
,
0xda
,
0x32
,
0xf4
,
0xa7
,
0x00
,
0x37
,
0x82
,
0x7e
,
0xd3
,
0x39
,
0xd1
,
0x02
,
0x2b
,
0x23
,
0x5d
};
static
const
uint8_t
X25519_PRIVATE_KEY_B
[
X25519_KEY_BYTE_LEN
]
=
{
0xc3
,
0xb2
,
0xc7
,
0xc3
,
0x1c
,
0x7e
,
0xad
,
0x20
,
0x5f
,
0x00
,
0xdf
,
0x1a
,
0xe0
,
0xe6
,
0x9e
,
0x6c
,
0xf8
,
0xac
,
0x2f
,
0xd5
,
0xea
,
0xa5
,
0xb6
,
0xe7
,
0x75
,
0x46
,
0xcb
,
0x85
,
0xbc
,
0x0e
,
0x68
,
0xb6
};
static
const
uint8_t
X25519_PUBLIC_KEY_B
[
X25519_KEY_BYTE_LEN
]
=
{
0xd7
,
0x7b
,
0xa7
,
0x19
,
0x8b
,
0x3e
,
0x54
,
0x4c
,
0xc3
,
0x45
,
0xb9
,
0x61
,
0xa0
,
0x9c
,
0x23
,
0xfc
,
0xeb
,
0x1f
,
0x9c
,
0x30
,
0x0f
,
0x3c
,
0xcd
,
0x17
,
0x7d
,
0xdc
,
0x50
,
0x0f
,
0x40
,
0x03
,
0x90
,
0x2e
};
static
const
char
PRIME_HEX_384
[]
=
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74"
"020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437"
"4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05"
"98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB"
"9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
"3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33"
"A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
"ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864"
"D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E2"
"08E24FA074E5AB3143DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF"
;
static
const
char
PRIME_HEX_256
[]
=
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74"
"020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437"
"4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05"
"98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB"
"9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
"3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF"
;
static
const
uint8_t
BASE_HEX_32
[]
=
{
0x42
,
0x6B
,
0x93
,
0xAA
,
0x93
,
0x27
,
0xD7
,
0x43
,
0x8A
,
0x92
,
0x67
,
0x77
,
0x77
,
0x23
,
0x51
,
0xAA
,
0xD5
,
0xCF
,
0xA3
,
0xDB
,
0x53
,
0x0B
,
0x81
,
0x77
,
0x85
,
0x85
,
0xCC
,
0x83
,
0x50
,
0x41
,
0x38
,
0xE3
};
static
const
uint8_t
BASE_HEX_256
[]
=
{
0xDA
,
0xC2
,
0xE1
,
0x78
,
0x68
,
0xD7
,
0x3E
,
0x0E
,
0xB2
,
0x1A
,
0x33
,
0xA9
,
0x1F
,
0x0C
,
0xD2
,
0x44
,
0x75
,
0xEC
,
0xDC
,
0xF3
,
0x47
,
0x7B
,
0xEF
,
0x7D
,
0x22
,
0x01
,
0x94
,
0x34
,
0x01
,
0x8F
,
0x9A
,
0x57
,
0xC1
,
0x04
,
0xFE
,
0x09
,
0x9C
,
0xB1
,
0x50
,
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0x59
,
0x14
,
0xB7
,
0x94
,
0x46
,
0x57
,
0x44
,
0x10
,
0x4E
,
0x6E
,
0xC5
,
0x68
,
0xDA
,
0x82
,
0x92
,
0x5A
,
0xDB
,
0x89
,
0x5D
,
0x3B
,
0x5E
,
0xB3
,
0x24
,
0xB4
,
0x55
,
0x4A
,
0xE9
,
0x41
,
0xCB
,
0xC6
,
0xE3
,
0xFE
,
0xC3
,
0xAD
,
0x2F
,
0xB3
,
0xA6
,
0x0F
,
0x7D
,
0x4C
,
0xA5
,
0xED
,
0x95
,
0x70
,
0x97
,
0x12
,
0x54
,
0xAB
,
0x58
,
0x0E
,
0xE5
,
0xF6
,
0x8B
,
0x37
,
0x0B
,
0x7C
,
0x06
,
0x54
,
0x89
,
0xDD
,
0x8A
,
0xA9
};
static
const
uint8_t
EXP_HEX_1
[]
=
{
0xE9
};
static
const
uint8_t
EXP_HEX_28
[]
=
{
0x8F
,
0x6F
,
0xC2
,
0xC0
,
0x64
,
0x37
,
0x1E
,
0x98
,
0x71
,
0xC9
,
0xF5
,
0x5E
,
0x45
,
0x5F
,
0x46
,
0x61
,
0x1B
,
0x4D
,
0x7D
,
0x23
,
0x21
,
0x01
,
0x1C
,
0x56
,
0xE2
,
0x77
,
0x14
,
0x64
};
static
const
uint8_t
EXP_HEX_32
[]
=
{
0xA9
,
0xC9
,
0x9A
,
0x5B
,
0xB5
,
0x9F
,
0xC6
,
0x9E
,
0xE3
,
0xF7
,
0x4B
,
0xFC
,
0xDC
,
0xF4
,
0x26
,
0x20
,
0xBE
,
0xAF
,
0x03
,
0x17
,
0xEC
,
0x56
,
0x2C
,
0xB5
,
0x46
,
0x13
,
0x33
,
0xD6
,
0x97
,
0x6A
,
0x33
,
0x7C
};
static
const
uint8_t
BASE_HEX_384
[]
=
{
0x37
,
0xBE
,
0xDE
,
0x74
,
0x1B
,
0x5D
,
0x7D
,
0x58
,
0x86
,
0x55
,
0xD3
,
0xCB
,
0x52
,
0x2A
,
0x6C
,
0x11
,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
,
0x13
,
0x4A
,
0xC8
,
0x60
,
0xA1
,
0x61
,
0x13
,
0x30
,
0xA6
,
0x4B
,
0x4F
,
0x6A
,
0x7C
,
0x82
,
0xF3
,
0x78
,
0x20
,
0x20
,
0x52
,
0x5B
,
0xED
,
0xD5
,
0xF8
,
0xBF
,
0x03
,
0x15
,
0xD8
,
0xBB
,
0x71
,
0x62
,
0x88
,
0x73
,
0xEB
,
0xFC
,
0x32
,
0x4E
,
0x20
,
0x17
,
0xEA
,
0xD6
,
0xBA
,
0xDF
,
0x55
,
0x26
,
0x6C
,
0x19
,
0x7F
,
0x7F
,
0x62
,
0x83
,
0x43
,
0x6C
,
0xA9
,
0xBD
,
0x12
,
0x2F
,
0x6B
,
0x8E
,
0x55
,
0x18
,
0x84
,
0xD6
,
0x0E
,
0x11
,
0xA2
,
0x78
,
0x05
,
0xD2
,
0x4F
,
0xEA
,
0xB3
,
0x61
,
0x47
,
0x9C
,
0xB2
,
0xCF
,
0x5C
,
0xE1
,
0xFF
,
0xC8
,
0x54
,
0x56
,
0x93
,
0x2F
,
0x71
,
0xE0
,
0xF5
,
0x85
,
0xC8
,
0x98
,
0x0C
,
0x0E
,
0x02
,
0x4A
,
0x1C
,
0xDB
,
0xB6
,
0x3F
,
0x78
,
0x4C
,
0x4F
,
0x1E
,
0x54
,
0xF9
,
0x48
,
0x2C
,
0xB1
,
0x6E
,
0x4D
,
0xD6
,
0x25
,
0xB3
,
0x91
,
0x5C
,
0x2B
,
0x9F
,
0x3A
,
0xFD
,
0xEB
,
0x75
,
0x44
,
0x17
};
static
const
BigNumTestCase
BIG_NUM_TEST_CASES
[]
=
{
// case 1:
{
.
baseLength
=
ARRAY_SIZE
(
BASE_HEX_32
),
.
expLength
=
ARRAY_SIZE
(
EXP_HEX_1
),
.
prime
=
PRIME_HEX_256
,
.
base
=
BASE_HEX_32
,
.
exp
=
EXP_HEX_1
,
.
result
=
BIG_NUM_TEST_CASE_1_RESULT
},
// case 2:
{
.
baseLength
=
ARRAY_SIZE
(
BASE_HEX_256
),
.
expLength
=
ARRAY_SIZE
(
EXP_HEX_28
),
.
prime
=
PRIME_HEX_256
,
.
base
=
BASE_HEX_256
,
.
exp
=
EXP_HEX_28
,
.
result
=
BIG_NUM_TEST_CASE_2_RESULT
},
// case 3:
{
.
baseLength
=
ARRAY_SIZE
(
BASE_HEX_32
),
.
expLength
=
ARRAY_SIZE
(
EXP_HEX_1
),
.
prime
=
PRIME_HEX_384
,
.
base
=
BASE_HEX_32
,
.
exp
=
EXP_HEX_1
,
.
result
=
BIG_NUM_TEST_CASE_3_RESULT
},
// case 4:
{
.
baseLength
=
ARRAY_SIZE
(
BASE_HEX_384
),
.
expLength
=
ARRAY_SIZE
(
EXP_HEX_32
),
.
prime
=
PRIME_HEX_384
,
.
base
=
BASE_HEX_384
,
.
exp
=
EXP_HEX_32
,
.
result
=
BIG_NUM_TEST_CASE_4_RESULT
}
};
enum
{
SHA256_MSG_MAX_SIZE
=
1024
,
SHA256_TEST_TIMES
=
100
,
SHA256_TEST_MIN_LIMIT
=
64
,
// test msg length form 1 to 64
GEN_RANDOM_MAX_SIZE
=
1024
,
GEN_RANDOM_TEST_TIMES
=
100
,
GEN_RANDOM_MIN_LIMIT
=
64
,
// test generate random from 1 to 64
GEN_RANDOM_TEST_REPEAT_SIZE
=
32
,
GEN_RANDOM_TEST_REPEAT_TIMES
=
100
,
TEST_HMAC_KEY_LEN
=
32
,
// equal to PSK_LEN PAKE_HMAC_KEY_LEN SHA256_LEN HC_SHA256_LEN
TEST_HMAC_ISALIAS_TRUE_MSG_LENGTH
=
32
,
// msg length when isAlias is true ISO_KEY_ALIAS_LEN
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
=
32
,
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
=
32
,
#define GENERATE_RETURN_KEY_STR "hichain_return_key"
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
=
16
,
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
=
16
,
#define HICHAIN_RETURN_KEY "hichain_return_key"
HKDF_PAKE_PSK_DERIVE_SECRET_ALIAS_BASEKEY_LEN
=
64
,
HKDF_PAKE_PSK_DERIVE_SECRET_ALIAS_SALT_LEN
=
32
,
#define TMP_AUTH_KEY_FACTOR "hichain_tmp_auth_enc_key"
HKDF_PAKE_PSK_DERIVE_SECRET_ALIAS_OUTKEY_LEN
=
32
,
HKDF_ISO_PSK_DERIVE_SESSIONKEY_BASEKEY_LEN
=
32
,
HKDF_ISO_PSK_DERIVE_SESSIONKEY_SALT_LEN
=
32
,
#define GENERATE_SESSION_KEY_STR "hichain_iso_session_key"
HKDF_ISO_PSK_DERIVE_SESSIONKEY_OUTKEY_LEN
=
32
,
HKDF_NEW_PAKE_PSK_DERIVE_SECRET_BASEKEY_LEN
=
32
*
2
,
// PAKE_PSK_LEN * BYTE_TO_HEX_OPER_LENGTH
HKDF_NEW_PAKE_PSK_DERIVE_SECRET_SALT_LEN
=
16
,
#define HICHAIN_SPEKE_BASE_INFO "hichain_speke_base_info"
HKDF_NEW_PAKE_PSK_DERIVE_SECRET_OUTKEY_LEN
=
32
,
HKDF_NEW_PAKE_SHAREDSECRET_DERIVE_SESSIONKEY_BASEKEY_LEN
=
SHA256_LEN
,
HKDF_NEW_PAKE_SHAREDSECRET_DERIVE_SESSIONKEY_SALT_LEN
=
16
,
#define HICHAIN_SPEKE_SESSIONKEY_INFO "hichain_speke_sessionkey_info"
HKDF_NEW_PAKE_SHAREDSECRET_DERIVE_SESSIONKEY_OUTKEY_LEN
=
32
,
HKDF_PAKE_PSK_DERIVE_SECRET_BASEKEY_LEN
=
32
*
2
,
// PAKE_PSK_LEN * BYTE_TO_HEX_OPER_LENGTH
HKDF_PAKE_PSK_DERIVE_SECRET_SALT_LEN
=
16
,
HKDF_PAKE_PSK_DERIVE_SECRET_OUTKEY_LEN
=
32
,
HKDF_PAKE_SHAREDSECRET_DERIVE_UNIONKEY_SALT_LEN
=
16
,
HKDF_PAKE_SHAREDSECRET_DERIVE_UNIONKEY_OUTKEY_LEN
=
48
,
IMPORT_ASYMMETRIC_KEY_KEYALIAS_LEN_MAX
=
32
,
IMPORT_ASYMMETRIC_KEY_KEYALIAS_LEN_MIN
=
1
,
IMPORT_ASYMMETRIC_KEY_AUTHTOKEN_LEN
=
32
,
};
enum
{
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_4
=
4
,
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_32
=
32
,
// the meaning of 16 * 2 + 256 * 2 is
// length of randSelf + length of randPeer + length of authIdPeer + length of authIdSelf
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_RAND_AUTH_ID_SELF_PEER
=
16
*
2
+
256
*
2
,
// MAC FF:FF:FF:FF:FF:FF
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_MAC
=
16
*
2
+
6
*
2
,
// UDID FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_UDID
=
16
*
2
+
16
*
2
,
// IMEI 999999999999999
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_IMEI
=
16
*
2
+
15
*
2
,
};
static
const
uint32_t
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_ARRAY
[]
=
{
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_4
,
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_32
,
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_RAND_AUTH_ID_SELF_PEER
,
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_MAC
,
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_UDID
,
TEST_HMAC_ISALIAS_FALSE_MSG_LENGTH_IMEI
,
};
static
const
char
SHA256_EXAMPLE_MESSAGE
[]
=
"hichain-sha256-example"
;
static
const
uint8_t
SHA256_EXAMPLE_RESULT
[
SHA256_LEN
]
=
{
0x53
,
0x0a
,
0x91
,
0x67
,
0xab
,
0x1f
,
0xf0
,
0x18
,
0xfa
,
0x1b
,
0x8e
,
0xb1
,
0x03
,
0xcf
,
0x1c
,
0x8a
,
0x1b
,
0x7b
,
0x05
,
0x0d
,
0xfb
,
0x2d
,
0xe0
,
0x60
,
0x19
,
0x64
,
0x50
,
0x76
,
0x69
,
0xd6
,
0xc5
,
0x79
,
};
static
const
char
HMAC_EXAMPLE_MESSAGE
[]
=
"hichain hmac message example"
;
static
const
uint8_t
HMAC_EXAMPLE_KEY
[
TEST_HMAC_KEY_LEN
]
=
{
0x01
,
0x23
,
0x45
,
0x67
,
0x89
,
0xab
,
0xcd
,
0xef
,
0x01
,
0x23
,
0x45
,
0x67
,
0x89
,
0xab
,
0xcd
,
0xef
,
0x01
,
0x23
,
0x45
,
0x67
,
0x89
,
0xab
,
0xcd
,
0xef
,
0x01
,
0x23
,
0x45
,
0x67
,
0x89
,
0xab
,
0xcd
,
0xef
,
};
static
const
uint8_t
HMAC_EXAMPLE_RESULT
[
HMAC_LEN
]
=
{
0x99
,
0xbe
,
0xaf
,
0x20
,
0x84
,
0xeb
,
0xae
,
0x5d
,
0xc4
,
0x1d
,
0x62
,
0x19
,
0xcb
,
0x52
,
0xe1
,
0x43
,
0xa1
,
0xf3
,
0x74
,
0xf8
,
0x33
,
0x61
,
0xe0
,
0x82
,
0x61
,
0xfe
,
0x19
,
0x09
,
0x1f
,
0xac
,
0x9d
,
0xb9
,
};
// Hashing type: SHA-256
static
const
char
HKDF_EXAMPLE_MESSAGE
[]
=
"hello hichain"
;
static
const
uint8_t
HKDF_EXAMPLE_SALT
[]
=
{
0x01
,
0x23
,
0x45
,
0x67
,
0x89
,
};
static
const
char
HKDF_EXAMPLE_INFO
[]
=
"hichain hkdf info"
;
static
const
uint8_t
HKDF_EXAMPLE_RESULT_KEY
[]
=
{
0x4e
,
0xc8
,
0xf7
,
0x6a
,
0x03
,
0xe9
,
0x18
,
0xbf
,
0xaa
,
0xaa
,
0x22
,
0xc1
,
0xd7
,
0x7e
,
0x0a
,
0xa8
,
};
static
const
char
hkdfCaseNameIsoSessionKeyDeriveReturnKey
[]
=
"iso sessionkey derive returnkey"
;
static
const
char
hkdfCaseNamePakeSessionKeyDeriveReturnKey
[]
=
"pake sessionkey derive returnkey"
;
static
const
char
hkdfCaseNameIsoPskDeriveSessionKey
[]
=
"iso psk derive sessionkey"
;
static
const
char
hkdfCaseNameNewPakePskDeriveSecret
[]
=
"new pake psk derive secret"
;
static
const
char
hkdfCaseNameNewPakeSharedSecretDeriveSessionKey
[]
=
"new pake sharedSecret derive sessionkey"
;
static
const
char
hkdfCaseNamePakePskDeriveSecretIsAliasFalse
[]
=
"pake psk derive secret isAlias false"
;
static
const
char
hkdfCaseNamePakeSharedSecretDeriveUnionKey
[]
=
"pake sharedSecret derive unionkey"
;
enum
{
HKDF_OUT_KEY_LENGTH_RANGE_16
=
16
,
HKDF_OUT_KEY_LENGTH_RANGE_32
=
32
,
HKDF_OUT_KEY_LENGTH_RANGE_64
=
64
,
HKDF_OUT_KEY_LENGTH_RANGE_128
=
128
,
HKDF_OUT_KEY_LENGTH_RANGE_256
=
256
,
HKDF_OUT_KEY_LENGTH_RANGE_512
=
512
,
HKDF_OUT_KEY_LENGTH_RANGE_1024
=
1024
,
HKDF_CASE_PAKE_SHARED_SECRET_DERIVE_UNION_KEY_BASE_KEY_LENGTH_ED25519
=
32
,
HKDF_CASE_PAKE_SHARED_SECRET_DERIVE_UNION_KEY_BASE_KEY_LENGTH_P256
=
64
,
};
static
const
struct
HkdfTestCase
{
const
char
*
caseName
;
uint32_t
baseKeyLength
;
uint32_t
saltLength
;
const
char
*
keyInfo
;
uint32_t
outKeyLength
;
}
g_hkdfTestCase
[]
=
{
{
.
caseName
=
hkdfCaseNameIsoSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
GENERATE_RETURN_KEY_STR
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_16
,
},
{
.
caseName
=
hkdfCaseNameIsoSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
GENERATE_RETURN_KEY_STR
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_32
,
},
{
.
caseName
=
hkdfCaseNameIsoSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
GENERATE_RETURN_KEY_STR
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_64
,
},
{
.
caseName
=
hkdfCaseNameIsoSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
GENERATE_RETURN_KEY_STR
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_128
,
},
{
.
caseName
=
hkdfCaseNameIsoSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
GENERATE_RETURN_KEY_STR
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_256
,
},
{
.
caseName
=
hkdfCaseNameIsoSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
GENERATE_RETURN_KEY_STR
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_512
,
},
{
.
caseName
=
hkdfCaseNameIsoSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_ISO_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
GENERATE_RETURN_KEY_STR
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_1024
,
},
{
.
caseName
=
hkdfCaseNamePakeSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_RETURN_KEY
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_16
,
},
{
.
caseName
=
hkdfCaseNamePakeSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_RETURN_KEY
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_32
,
},
{
.
caseName
=
hkdfCaseNamePakeSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_RETURN_KEY
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_64
,
},
{
.
caseName
=
hkdfCaseNamePakeSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_RETURN_KEY
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_128
,
},
{
.
caseName
=
hkdfCaseNamePakeSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_RETURN_KEY
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_256
,
},
{
.
caseName
=
hkdfCaseNamePakeSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_RETURN_KEY
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_512
,
},
{
.
caseName
=
hkdfCaseNamePakeSessionKeyDeriveReturnKey
,
.
baseKeyLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_PAKE_SESSIONKEY_DERIVE_RETURNKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_RETURN_KEY
,
.
outKeyLength
=
HKDF_OUT_KEY_LENGTH_RANGE_1024
,
},
{
.
caseName
=
hkdfCaseNameIsoPskDeriveSessionKey
,
.
baseKeyLength
=
HKDF_ISO_PSK_DERIVE_SESSIONKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_ISO_PSK_DERIVE_SESSIONKEY_SALT_LEN
,
.
keyInfo
=
GENERATE_SESSION_KEY_STR
,
.
outKeyLength
=
HKDF_ISO_PSK_DERIVE_SESSIONKEY_OUTKEY_LEN
,
},
{
.
caseName
=
hkdfCaseNameNewPakePskDeriveSecret
,
.
baseKeyLength
=
HKDF_NEW_PAKE_PSK_DERIVE_SECRET_BASEKEY_LEN
,
.
saltLength
=
HKDF_NEW_PAKE_PSK_DERIVE_SECRET_SALT_LEN
,
.
keyInfo
=
HICHAIN_SPEKE_BASE_INFO
,
.
outKeyLength
=
HKDF_NEW_PAKE_PSK_DERIVE_SECRET_OUTKEY_LEN
,
},
{
.
caseName
=
hkdfCaseNameNewPakeSharedSecretDeriveSessionKey
,
.
baseKeyLength
=
HKDF_NEW_PAKE_SHAREDSECRET_DERIVE_SESSIONKEY_BASEKEY_LEN
,
.
saltLength
=
HKDF_NEW_PAKE_SHAREDSECRET_DERIVE_SESSIONKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_SPEKE_SESSIONKEY_INFO
,
.
outKeyLength
=
HKDF_NEW_PAKE_SHAREDSECRET_DERIVE_SESSIONKEY_OUTKEY_LEN
,
},
{
.
caseName
=
hkdfCaseNamePakePskDeriveSecretIsAliasFalse
,
.
baseKeyLength
=
HKDF_PAKE_PSK_DERIVE_SECRET_BASEKEY_LEN
,
.
saltLength
=
HKDF_PAKE_PSK_DERIVE_SECRET_SALT_LEN
,
.
keyInfo
=
HICHAIN_SPEKE_BASE_INFO
,
.
outKeyLength
=
HKDF_PAKE_PSK_DERIVE_SECRET_OUTKEY_LEN
,
},
{
.
caseName
=
hkdfCaseNamePakeSharedSecretDeriveUnionKey
,
.
baseKeyLength
=
HKDF_CASE_PAKE_SHARED_SECRET_DERIVE_UNION_KEY_BASE_KEY_LENGTH_ED25519
,
.
saltLength
=
HKDF_PAKE_SHAREDSECRET_DERIVE_UNIONKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_SPEKE_SESSIONKEY_INFO
,
.
outKeyLength
=
HKDF_PAKE_SHAREDSECRET_DERIVE_UNIONKEY_OUTKEY_LEN
,
},
{
.
caseName
=
hkdfCaseNamePakeSharedSecretDeriveUnionKey
,
.
baseKeyLength
=
HKDF_CASE_PAKE_SHARED_SECRET_DERIVE_UNION_KEY_BASE_KEY_LENGTH_P256
,
.
saltLength
=
HKDF_PAKE_SHAREDSECRET_DERIVE_UNIONKEY_SALT_LEN
,
.
keyInfo
=
HICHAIN_SPEKE_SESSIONKEY_INFO
,
.
outKeyLength
=
HKDF_PAKE_SHAREDSECRET_DERIVE_UNIONKEY_OUTKEY_LEN
,
},
};
enum
{
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_KEY_PAIR_KEY_ALIAS_LENGTH
=
32
,
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_SHARED_KEY_LENGTH
=
32
,
TEST_COMPUTE_HKDF_WITH_KEY_ALIAS_TRUE_AUTH_ID_LENGTH
=
32
,
};
enum
{
TEST_IMPORT_ASYMMETRIC_KEY_AUTH_ID_LENGTH_32
=
32
,
TEST_IMPORT_ASYMMETRIC_KEY_AUTH_ID_LENGTH_64
=
64
,
};
enum
{
TEST_GENERATE_KEY_PAIR_WITH_STORAGE_AUTH_ID_LENGTH
=
32
,
};
#ifdef __cplusplus
}
#endif
#endif // HC_ALG_TEST_CASE_H
\ No newline at end of file
security_lite/deviceauth_basic_deps/test.c
浏览文件 @
fd279506
...
...
@@ -22,6 +22,7 @@
#include <string.h>
#include <time.h>
#include "hc_alg_test.h"
#include "hc_condition_test.h"
#include "hc_dev_info_test.h"
#include "hc_file_f_test.h"
...
...
@@ -169,4 +170,16 @@ LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseHcTime, Function | SmallTes
LOGI
(
"++++++++++++++++++++++++++++++++++++++++
\n
"
);
}
/**
* @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseHcAlg
* @tc.desc:
* @tc.type: FUNC
*/
LITE_TEST_CASE
(
DeviceAuthBasicDepsTestSuite
,
TestCaseHcAlg
,
Function
|
SmallTest
|
Level1
)
{
LOGI
(
"++++++++++++++++++++++++++++++++++++++++
\n
"
);
TestHcAlg
();
LOGI
(
"++++++++++++++++++++++++++++++++++++++++
\n
"
);
}
RUN_TEST_SUITE
(
DeviceAuthBasicDepsTestSuite
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录