/* * Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2021. All rights reserved. * Description: clear keyladder test. * Author: Hisilicon * Create: 2019-07-25 */ #include "securec.h" #include "uapi_system.h" #include "uapi_keyslot.h" #include "uapi_klad.h" #include "parse_config_file.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 klad_mark() printf("[%-32s][line:%04d]mark\n", __FUNCTION__, __LINE__) #define klad_err_print_hex(val) printf("[%-32s][line:%04d]%s = 0x%08x\n", __FUNCTION__, __LINE__, #val, val) #define klad_err_print_info(val) printf("[%-32s][line:%04d]%s\n", __FUNCTION__, __LINE__, val) #define klad_err_print_val(val) printf("[%-32s][line:%04d]%s = %d\n", __FUNCTION__, __LINE__, #val, val) #define klad_err_print_point(val) printf("[%-32s][line:%04d]%s = %p\n", __FUNCTION__, __LINE__, #val, val) #define klad_print_error_code(err_code) \ printf("[%-32s][line:%04d]return [0x%08x]\n", __FUNCTION__, __LINE__, err_code) #define klad_print_error_func(func, err_code) \ printf("[%-32s][line:%04d]call [%s] return [0x%08x]\n", __FUNCTION__, __LINE__, #func, err_code) #define KEY_LEN 16 static td_u8 g_test_key[KEY_LEN] = {0}; static td_s32 priv_klad_init(td_handle *handle_ks, td_handle *handle_klad) { td_s32 ret; uapi_keyslot_attr ks_attr = {0}; ret = uapi_sys_init(); if (ret != TD_SUCCESS) { klad_print_error_func(uapi_sys_init, ret); return ret; } ret = uapi_keyslot_init(); if (ret != TD_SUCCESS) { klad_print_error_func(uapi_keyslot_init, ret); goto sys_deinit; } ret = uapi_klad_init(); if (ret != TD_SUCCESS) { klad_print_error_func(uapi_klad_init, ret); goto ks_deinit; } ks_attr.type = UAPI_KEYSLOT_TYPE_MCIPHER; ks_attr.secure_mode = UAPI_KEYSLOT_SECURE_MODE_NONE; ret = uapi_keyslot_create(&ks_attr, handle_ks); if (ret != TD_SUCCESS) { klad_print_error_func(uapi_keyslot_create, ret); goto klad_deinit; } ret = uapi_klad_create(handle_klad); if (ret != TD_SUCCESS) { klad_print_error_func(uapi_klad_create, ret); goto ks_destroy; } return ret; ks_destroy: uapi_keyslot_destroy(*handle_ks); klad_deinit: uapi_klad_deinit(); ks_deinit: uapi_keyslot_deinit(); sys_deinit: uapi_sys_deinit(); return ret; } static td_s32 priv_klad_attach(td_handle handle_ks, td_handle handle_klad) { td_s32 ret; errno_t errno; uapi_klad_attr attr_klad = {0}; uapi_klad_clear_key key_clear = {0}; td_char input_cmd[0x20] = {0}; attr_klad.klad_cfg.owner_id = 0; attr_klad.klad_cfg.klad_type = UAPI_KLAD_TYPE_CLEARCW; attr_klad.key_cfg.decrypt_support = 1; attr_klad.key_cfg.encrypt_support = 1; attr_klad.key_cfg.engine = UAPI_CRYPTO_ALG_RAW_AES; ret = uapi_klad_set_attr(handle_klad, &attr_klad); if (ret != TD_SUCCESS) { klad_print_error_func(uapi_klad_set_attr, ret); goto out; } ret = uapi_klad_attach(handle_klad, handle_ks); if (ret != TD_SUCCESS) { klad_print_error_func(uapi_klad_attach, ret); goto out; } key_clear.odd = 0; key_clear.key_size = KEY_LEN; errno = memcpy_s(key_clear.key, UAPI_KLAD_MAX_KEY_LEN, g_test_key, KEY_LEN); if (errno != EOK) { klad_print_error_func(memcpy_s, errno); goto klad_detach; } ret = uapi_klad_set_clear_key(handle_klad, &key_clear); if (ret != TD_SUCCESS) { klad_print_error_func(uapi_klad_set_clear_key, ret); goto klad_detach; } printf("Sample executed successfully. \n"); 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; } } goto out; klad_detach: uapi_klad_detach(handle_klad, handle_ks); out: return ret; } static td_void priv_klad_detach(td_handle handle_klad, td_handle handle_ks) { td_s32 ret; ret = uapi_klad_detach(handle_klad, handle_ks); if (ret != TD_SUCCESS) { klad_print_error_func(uapi_klad_detach, ret); } return; } static td_void priv_klad_deinit(td_handle handle_klad, td_handle handle_ks) { uapi_klad_destroy(handle_klad); uapi_keyslot_destroy(handle_ks); uapi_klad_deinit(); uapi_keyslot_deinit(); uapi_sys_deinit(); return; } static td_s32 priv_data_init(const td_char *cfg_path) { td_s32 ret; td_s32 value_num = 0; ret = parse_config_file(cfg_path, &value_num); if (ret != TD_SUCCESS || value_num == 0 || value_num > MAX_VAR_NUM) { klad_print_error_func(parse_config_file, ret); return ret; } ret = get_key_value(SESSIONKEY_TAG, g_test_key, KEY_LEN); if (ret != TD_SUCCESS) { klad_print_error_func(get_key_value, ret); return ret; } return TD_SUCCESS; } td_s32 main(td_s32 argc, td_char *argv[]) { td_s32 ret; td_handle handle_ks = 0; td_handle handle_klad = 0; (void)argc; if (argv == TD_NULL || argv[0x1] == TD_NULL) { printf("Please enter the path of the config file.!!!\n"); return TD_FAILURE; } ret = priv_data_init(argv[0x1]); if (ret != TD_SUCCESS) { return ret; } ret = priv_klad_init(&handle_ks, &handle_klad); if (ret != TD_SUCCESS) { return ret; } ret = priv_klad_attach(handle_ks, handle_klad); if (ret != TD_SUCCESS) { klad_print_error_func(priv_klad_attach, ret); goto ks_destroy; } priv_klad_detach(handle_klad, handle_ks); ks_destroy: priv_klad_deinit(handle_klad, handle_ks); return ret; }