/* * Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2021. All rights reserved. * Description: tee keyslot test. * Author: Hisilicon * Create: 2019-07-25 */ #include #include #include "uapi_keyslot.h" #define sample_get_inputcmd(input_cmd) fgets((char *)(input_cmd), (sizeof(input_cmd) - 1), stdin) #define ext_get_inputcmd(input_cmd) fgets((char *)(input_cmd), (sizeof(input_cmd) - 1), stdin) #define ext_err_dft(format, arg...) printf("%s,%d: " format, __FUNCTION__, __LINE__, ## arg) #define ext_info_dft(format, arg...) printf("%s,%d: " format, __FUNCTION__, __LINE__, ## arg) #define KLAD_HANDLE_LEN 0x4 static int privkey_slot_create(unsigned int *handle, unsigned int len, uapi_keyslot_attr *attr) { int ret; unsigned int i, n; if (len > KLAD_HANDLE_LEN) { ext_err_dft("len > KLAD_HANDLE_LEN\n"); return -1; } ret = uapi_keyslot_init(); if (ret != TD_SUCCESS) { goto out; } sleep(1); for (i = 0; i < len; i++) { handle[i] = 0; attr->type = UAPI_KEYSLOT_TYPE_MCIPHER; attr->secure_mode = UAPI_KEYSLOT_SECURE_MODE_NONE; ret = uapi_keyslot_create(attr, &handle[i]); if (ret != TD_SUCCESS) { printf("Failed to uapi_keyslot_create func, ret = 0x%x\n", ret); goto keyslot_destroy; } ext_err_dft("REE============> 0x%08x= 0x%08x\n", ret, handle[i]); sleep(1); } printf("please input 'q' to quit!\n"); goto out; keyslot_destroy: for (n = 0; n < ((i > KLAD_HANDLE_LEN) ? KLAD_HANDLE_LEN : i); n++) { if (uapi_keyslot_destroy(handle[n]) != TD_SUCCESS) { printf("Failed to uapi_keyslot_destroy func, handle: 0x%08x , index : 0x%x\n", handle[n], n); } } uapi_keyslot_deinit(); out: return ret; } static td_void priv_key_slot_destroy(const unsigned int *handle, unsigned int len) { int ret; unsigned int i; if (len > KLAD_HANDLE_LEN) { len = KLAD_HANDLE_LEN; } for (i = 0; i < len; i++) { ret = uapi_keyslot_destroy(handle[i]); printf("************> 0x%08x= 0x%08x\n", ret, handle[i]); sleep(1); } uapi_keyslot_deinit(); return; } int main(int argc, char *argv[]) { unsigned int handle[KLAD_HANDLE_LEN] = {0}; uapi_keyslot_attr attr = {0}; int i; int ret; td_char input_cmd[0x20] = {0}; (void)argc; if (argv == TD_NULL) { printf("argv is NULL \n"); return 0; } ret = privkey_slot_create(handle, KLAD_HANDLE_LEN, &attr); if (ret != TD_SUCCESS) { return ret; } while (1) { sample_get_inputcmd(input_cmd); if (input_cmd[0] == 'q') { printf("prepare to quit!\n"); break; } } for (i = 0; i < KLAD_HANDLE_LEN; i++) { ret = uapi_keyslot_destroy(handle[i]); ext_err_dft("************> 0x%08x= 0x%08x\n", ret, handle[i]); sleep(1); } for (i = 0; i < KLAD_HANDLE_LEN; i++) { handle[i] = 0; attr.type = UAPI_KEYSLOT_TYPE_MCIPHER; attr.secure_mode = UAPI_KEYSLOT_SECURE_MODE_TEE; ret = uapi_keyslot_create(&attr, &handle[i]); ext_err_dft("TEE============> 0x%08x= 0x%08x\n", ret, handle[i]); sleep(1); } printf("please input 'q' to quit!\n"); while (1) { sample_get_inputcmd(input_cmd); if (input_cmd[0] == 'q') { printf("prepare to quit!\n"); break; } } priv_key_slot_destroy(handle, KLAD_HANDLE_LEN); return ret; }