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.
408 lines
10 KiB
408 lines
10 KiB
/*
|
|
* Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2021. All rights reserved.
|
|
* Description: keyladder mpi driver.
|
|
* Author: Hisilicon
|
|
* Create: 2019-08-13
|
|
*/
|
|
|
|
#include "mpi_klad_ext.h"
|
|
|
|
#include "mpi_klad_func.h"
|
|
#include "mpi_klad_mgmt.h"
|
|
#include "mpi_klad_syscall.h"
|
|
#include "mpi_demux_ext.h"
|
|
#include "uapi_cipher.h"
|
|
|
|
td_s32 ext_mpi_klad_init(td_void)
|
|
{
|
|
return ctl_klad_init();
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_deinit(td_void)
|
|
{
|
|
return ctl_klad_deinit();
|
|
}
|
|
|
|
td_void ext_klad_dump_buffer(const td_char *buf, td_u32 len)
|
|
{
|
|
const td_u32 lwidth = 0x200;
|
|
td_char str_buf[lwidth];
|
|
td_char *p = str_buf;
|
|
td_char *q = str_buf + lwidth;
|
|
td_u32 i;
|
|
|
|
p = str_buf;
|
|
if (buf == NULL) {
|
|
if (snprintf_s(p, q - p, q - p - 1, "*NULL*\n") == -1) {
|
|
print_err_func(snprintf_s, -1);
|
|
return;
|
|
}
|
|
} else if (len < (lwidth / 0x2)) {
|
|
for (i = 0; i < len; i++, p += 0x2) {
|
|
if (snprintf_s(p, q - p, q - p - 1, "%02x", buf[i]) == -1) {
|
|
print_err_func(snprintf_s, -1);
|
|
return;
|
|
}
|
|
}
|
|
*p = '\n';
|
|
p++;
|
|
*p = 0; /* end string with null char */
|
|
} else {
|
|
for (i = 0; (i < (lwidth / 0x4) - 1) && ((p + 0x9) < q); i++, p += 0x2) {
|
|
if (snprintf_s(p, q - p, q - p - 1, "%02x", buf[i]) == -1) {
|
|
print_err_func(snprintf_s, -1);
|
|
return;
|
|
}
|
|
}
|
|
if (snprintf_s(p, q - p, q - p - 1, " ... ") == -1) {
|
|
print_err_func(snprintf_s, -1);
|
|
return;
|
|
}
|
|
p += 0x5;
|
|
for (i = len - (lwidth / 0x4) + 1; (i < len) && ((p + 0x4) < q); i++, p += 0x2) {
|
|
if (snprintf_s(p, q - p, q - p - 1, "%02x", buf[i]) == -1) {
|
|
print_err_func(snprintf_s, -1);
|
|
return;
|
|
}
|
|
}
|
|
*p = '\n';
|
|
p++;
|
|
*p = 0; /* end string with null char */
|
|
}
|
|
ext_dbg_klad("%s", str_buf);
|
|
return;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_create(td_handle *create_handle)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
ret = klad_slot_mgmt_create_slot(create_handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_mgmt_create_slot, ret);
|
|
goto out;
|
|
}
|
|
if (create_handle == NULL) {
|
|
print_err_code(SOC_ERR_KLAD_NULL_PTR);
|
|
ret = SOC_ERR_KLAD_NULL_PTR;
|
|
goto out;
|
|
}
|
|
|
|
ret = klad_slot_mgmt_create_instance(*create_handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_mgmt_create_instance, ret);
|
|
goto out1;
|
|
}
|
|
get_curr_cost("mpi_create", &time_b);
|
|
return TD_SUCCESS;
|
|
|
|
out1:
|
|
ret = klad_slot_mgmt_destroy_slot(*create_handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_mgmt_destroy_slot, ret);
|
|
}
|
|
out:
|
|
get_curr_cost("mpi_create", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_destroy(td_handle handle)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
|
|
if (klad_slot_instance_initialzed(handle) == TD_TRUE) {
|
|
ret = klad_slot_instance_stop(handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_stop, ret);
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
ret = klad_slot_mgmt_destroy_instance(handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_mgmt_destroy_instance, ret);
|
|
goto out;
|
|
}
|
|
ret = klad_slot_mgmt_destroy_slot(handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_mgmt_destroy_slot, ret);
|
|
}
|
|
out:
|
|
get_curr_cost("mpi_destroy", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
static td_s32 priv_sw_klad_set_attr(td_handle handle, const ext_klad_attr *attr)
|
|
{
|
|
td_s32 ret;
|
|
|
|
if (klad_slot_instance_initialzed(handle) == TD_FALSE) {
|
|
ret = klad_slot_instance_init(handle, get_klad_type(attr->klad_cfg.klad_type));
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_set_attr, ret);
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
ret = klad_slot_instance_set_attr(handle, attr);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_set_attr, ret);
|
|
goto out;
|
|
}
|
|
ret = TD_SUCCESS;
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
static td_s32 priv_sw_klad_get_attr(td_handle handle, ext_klad_attr *attr)
|
|
{
|
|
return klad_slot_instance_get_attr(handle, attr);
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_set_attr(td_handle handle, const ext_klad_attr *attr)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
ret = priv_sw_klad_set_attr(handle, attr);
|
|
|
|
get_curr_cost("mpi_setattr", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_get_attr(td_handle handle, ext_klad_attr *attr)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
ret = priv_sw_klad_get_attr(handle, attr);
|
|
|
|
get_curr_cost("mpi_getattr", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_set_rootkey_attr(td_handle handle, const ext_rootkey_attr *rootkey_attr)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
|
|
if (klad_slot_instance_initialzed(handle) == TD_FALSE) {
|
|
ret = klad_slot_instance_init(handle, EXT_KLAD_COM);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_set_attr, ret);
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
ret = klad_slot_instance_set_rootkey_attr(handle, rootkey_attr);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_set_rootkey_attr, ret);
|
|
goto out;
|
|
}
|
|
ret = TD_SUCCESS;
|
|
out:
|
|
get_curr_cost("mpi_setrk", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_get_rootkey_attr(td_handle handle, ext_rootkey_attr *rootkey_attr)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
if (klad_slot_instance_initialzed(handle) == TD_FALSE) {
|
|
return SOC_ERR_KLAD_NOT_FIND_SESSION;
|
|
}
|
|
|
|
ret = klad_slot_instance_get_rootkey_attr(handle, rootkey_attr);
|
|
|
|
get_curr_cost("mpi_getrk", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
static td_s32 mpi_get_ks_handle(td_handle target, td_handle *ks)
|
|
{
|
|
td_s32 mod;
|
|
td_s32 ret = TD_SUCCESS;
|
|
|
|
if (get_fmw_version() == EXT_KLAD_FMW_V2) {
|
|
*ks = target;
|
|
return ret;
|
|
}
|
|
|
|
mod = TD_HANDLE_GET_MODULE_ID(target);
|
|
if (mod == SOC_ID_CIPHER) {
|
|
ret = uapi_cipher_symc_get_keyslot_handle(target, ks);
|
|
} else if (mod == SOC_ID_DEMUX) {
|
|
ret = ext_mpi_dmx_dsc_get_keyslot_handle(target, ks);
|
|
} else if (mod == SOC_ID_KEYSLOT) {
|
|
*ks = target;
|
|
} else {
|
|
ret = SOC_ERR_KLAD_INVALID_TARGET;
|
|
}
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_hex2(target, mod);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_attach(td_handle handle, td_handle target)
|
|
{
|
|
td_s32 ret;
|
|
td_handle ks = 0;
|
|
struct time_ns time_b = {0};
|
|
|
|
get_time(&time_b);
|
|
ret = mpi_get_ks_handle(target, &ks);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_hex(ret);
|
|
return ret;
|
|
}
|
|
|
|
ret = klad_slot_instance_attach(handle, ks);
|
|
|
|
get_curr_cost("mpi_attach", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_detach(td_handle handle, td_handle target)
|
|
{
|
|
td_s32 ret;
|
|
td_handle ks = 0;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
ret = mpi_get_ks_handle(target, &ks);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_hex(ret);
|
|
return ret;
|
|
}
|
|
|
|
ret = klad_slot_instance_detach(handle, ks);
|
|
|
|
get_curr_cost("mpi_detach", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_set_session_key(td_handle handle, const ext_klad_session_key *session_key)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
ret = klad_slot_instance_set_session_key(handle, session_key);
|
|
|
|
get_curr_cost("mpi_sessionkey", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_set_content_key(td_handle handle, const ext_klad_content_key *content_key)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
ret = klad_slot_instance_set_content_key(handle, content_key);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_set_content_key, ret);
|
|
goto out;
|
|
}
|
|
ret = klad_slot_instance_start(handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_start, ret);
|
|
}
|
|
out:
|
|
get_curr_cost("mpi_contentkey", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_set_clear_key(td_handle handle, const ext_klad_clear_key *clear_key)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
ret = klad_slot_clr_set_key(handle, clear_key);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_set_content_key, ret);
|
|
goto out;
|
|
}
|
|
ret = klad_slot_clr_start(handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_clr_start, ret);
|
|
}
|
|
out:
|
|
get_curr_cost("mpi_clrkey", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_async_set_content_key(td_handle handle, const ext_klad_content_key *key,
|
|
const klad_callback *call_back)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
get_time(&time_b);
|
|
ret = klad_slot_instance_set_content_key(handle, key);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_set_content_key, ret);
|
|
goto out;
|
|
}
|
|
ret = klad_slot_instance_async_start(handle, call_back);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_instance_async_start, ret);
|
|
}
|
|
out:
|
|
get_curr_cost("mpi_async contentkey", &time_b);
|
|
return ret;
|
|
}
|
|
|
|
td_s32 ext_mpi_klad_set_fp_key(td_handle handle, ext_klad_fp_key *key)
|
|
{
|
|
td_s32 ret;
|
|
struct time_ns time_b;
|
|
|
|
if (key == TD_NULL) {
|
|
print_err_code(SOC_ERR_KLAD_NULL_PTR);
|
|
return SOC_ERR_KLAD_NULL_PTR;
|
|
}
|
|
|
|
get_time(&time_b);
|
|
ret = klad_slot_fp_set_fp_key(handle, key);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_fp_set_fp_key, ret);
|
|
goto out;
|
|
}
|
|
if (key->operation == EXT_KLAD_FP_OPT_ROUTE) {
|
|
ret = klad_slot_fp_route(handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_fp_route, ret);
|
|
}
|
|
} else if (key->operation == EXT_KLAD_FP_OPT_DECRYPT) {
|
|
ret = klad_slot_fp_start(handle);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_fp_start, ret);
|
|
}
|
|
} else {
|
|
key->enc_key_size = key->key_size;
|
|
ret = klad_slot_fp_enc(handle, key->enc_key, key->enc_key_size);
|
|
if (ret != TD_SUCCESS) {
|
|
print_err_func(klad_slot_fp_enc, ret);
|
|
}
|
|
}
|
|
out:
|
|
get_curr_cost("mpi_set_fp contentkey", &time_b);
|
|
return ret;
|
|
}
|