You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.3 KiB
156 lines
4.3 KiB
/*
|
|
* Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2021. All rights reserved.
|
|
* Description: Test the file of the asymmetric key hash interface.
|
|
* Author: Hisilicon
|
|
* Create: 2019-09-20
|
|
*/
|
|
|
|
#include "sample_otp_base.h"
|
|
|
|
static td_s32 get_asymmetric_key_hash_lock_stat(td_s32 argc, td_char *argv[]);
|
|
static td_s32 set_asymmetric_key_hash(td_s32 argc, td_char *argv[]);
|
|
|
|
static otp_sample g_otp_sample_data[] = {
|
|
{ 0, "help", NULL, { "Display this help and exit.", "example: ./sample_otp_asymmetrickeyhash help" } },
|
|
{
|
|
1, "set", set_asymmetric_key_hash,
|
|
{
|
|
"Set asymmetric key hash ==> UAPI_OTP_ASYMMETRIC_KEY_RSA.",
|
|
"example: ./sample_otp_asymmetrickeyhash set rsa"
|
|
}
|
|
},
|
|
{
|
|
2, "set", set_asymmetric_key_hash,
|
|
{
|
|
"Set asymmetric key hash ==> UAPI_OTP_ASYMMETRIC_KEY_SM2.",
|
|
"example: ./sample_otp_asymmetrickeyhash set sm2"
|
|
}
|
|
},
|
|
{
|
|
3, "get", get_asymmetric_key_hash_lock_stat,
|
|
{
|
|
"Get asymmetric key hash lock stat ==> UAPI_OTP_ASYMMETRIC_KEY_RSA.",
|
|
"example: ./sample_otp_asymmetrickeyhash get rsa"
|
|
}
|
|
},
|
|
{
|
|
4, "get", get_asymmetric_key_hash_lock_stat,
|
|
{
|
|
"Get asymmetric key hash lock stat ==> UAPI_OTP_ASYMMETRIC_KEY_SM2.",
|
|
"example: ./sample_otp_asymmetrickeyhash get sm2"
|
|
}
|
|
},
|
|
};
|
|
|
|
static td_void get_type(td_s32 argc, td_char *argv[], uapi_otp_asymmetric_key_type *key_type)
|
|
{
|
|
if (argv[0x2] == TD_NULL) {
|
|
sample_printf("argv[2] is NULL\n");
|
|
goto out;
|
|
}
|
|
|
|
if (key_type == TD_NULL) {
|
|
sample_printf("key_type is NULL\n");
|
|
goto out;
|
|
}
|
|
|
|
if (case_strcmp("rsa", argv[0x2])) {
|
|
*key_type = UAPI_OTP_ASYMMETRIC_KEY_RSA;
|
|
} else if (case_strcmp("sm2", argv[0x2])) {
|
|
*key_type = UAPI_OTP_ASYMMETRIC_KEY_SM2;
|
|
} else {
|
|
*key_type = UAPI_OTP_ASYMMETRIC_KEY_MAX;
|
|
}
|
|
|
|
unused(argc);
|
|
out:
|
|
return;
|
|
}
|
|
|
|
static td_s32 get_asymmetric_key_hash_lock_stat(td_s32 argc, td_char *argv[])
|
|
{
|
|
td_s32 ret;
|
|
td_bool status = TD_FALSE;
|
|
uapi_otp_asymmetric_key_type key_type = UAPI_OTP_ASYMMETRIC_KEY_MAX;
|
|
|
|
get_type(argc, argv, &key_type);
|
|
if (key_type == UAPI_OTP_ASYMMETRIC_KEY_MAX) {
|
|
sample_printf("Don't have key type\n");
|
|
ret = TD_FAILURE;
|
|
goto out;
|
|
}
|
|
|
|
ret = uapi_otp_get_asymmetric_key_hash_lock_stat(key_type, &status);
|
|
if (ret != TD_SUCCESS) {
|
|
sample_printf("Failed to get asymmetric key hash lock stat, ret = 0x%x \n", ret);
|
|
goto out;
|
|
}
|
|
|
|
sample_printf("Get asymmetric key hash lock stat: %d\n", status);
|
|
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
static td_s32 set_asymmetric_key_hash(td_s32 argc, td_char *argv[])
|
|
{
|
|
td_s32 ret;
|
|
td_u8 hash[0x20] = {
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07,
|
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x16, 0x17,
|
|
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x26, 0x27,
|
|
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x36, 0x37,
|
|
};
|
|
uapi_otp_asymmetric_key_type key_type = UAPI_OTP_ASYMMETRIC_KEY_MAX;
|
|
|
|
get_type(argc, argv, &key_type);
|
|
if (key_type == UAPI_OTP_ASYMMETRIC_KEY_MAX) {
|
|
sample_printf("NO chip id sel\n");
|
|
ret = TD_FAILURE;
|
|
goto out;
|
|
}
|
|
|
|
ret = uapi_otp_set_asymmetric_key_hash(key_type, hash, sizeof(hash));
|
|
if (ret != TD_SUCCESS) {
|
|
sample_printf("Failed to set asymmetric key hash, ret = 0x%x \n", ret);
|
|
goto out;
|
|
}
|
|
|
|
print_buffer("Set asymmetric key hash", hash, sizeof(hash));
|
|
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
td_s32 main(int argc, char *argv[])
|
|
{
|
|
td_s32 ret;
|
|
|
|
if (argc < 0x3 || argv == TD_NULL) {
|
|
sample_printf("sample parameter error.\n");
|
|
ret = SOC_ERR_OTP_INVALID_PARA;
|
|
goto out1;
|
|
}
|
|
|
|
if (case_strcmp("help", argv[1])) {
|
|
show_usage(g_otp_sample_data, sizeof(g_otp_sample_data) / sizeof(g_otp_sample_data[0]));
|
|
ret = TD_SUCCESS;
|
|
goto out0;
|
|
}
|
|
|
|
ret = uapi_otp_init();
|
|
if (ret != TD_SUCCESS) {
|
|
sample_printf("OTP init failed, ret = 0x%x \n", ret);
|
|
goto out1;
|
|
}
|
|
|
|
ret = run_cmdline(argc, argv, g_otp_sample_data, sizeof(g_otp_sample_data) / sizeof(g_otp_sample_data[0]));
|
|
|
|
(td_void)uapi_otp_deinit();
|
|
|
|
out1:
|
|
show_returne_msg(g_otp_sample_data, sizeof(g_otp_sample_data) / sizeof(g_otp_sample_data[0]), ret);
|
|
out0:
|
|
return ret;
|
|
}
|