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.

446 lines
18 KiB

/*
* Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2019. All rights reserved.
* Description: uapi_cipher
* Author: Hisilicon
* Create: 2019-06-18
*/
#include "cipher_soft_symc.h"
#include "cipher_soft_hash.h"
#include "soft_pke.h"
#include "crypto_osal_lib.h"
#include "uapi_cipher.h"
td_s32 uapi_cipher_symc_init(td_void)
{
return unify_uapi_cipher_symc_init();
}
td_s32 uapi_cipher_symc_deinit(td_void)
{
return unify_uapi_cipher_symc_deinit();
}
td_s32 uapi_cipher_symc_create(td_handle *symc_handle, const uapi_cipher_symc_attr *symc_attr)
{
if (symc_handle == TD_NULL || symc_attr == TD_NULL) {
crypto_log_err("input parameter is null!\n");
return TD_FAILURE;
}
symc_param param = {(crypto_symc_alg)symc_attr->symc_alg, (td_u32)symc_attr->work_mode};
alg_item item = {SYMC, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_symc_create(symc_handle, (crypto_symc_attr *)symc_attr);
}
return cipher_soft_symc_create(symc_handle, (crypto_symc_attr *)symc_attr);
}
td_s32 uapi_cipher_symc_destroy(td_handle symc_handle)
{
if (crypto_check_soft_alg_flag(symc_handle) == TD_TRUE) {
return cipher_soft_symc_destroy(symc_handle);
}
return unify_uapi_cipher_symc_destroy(symc_handle);
}
td_s32 uapi_cipher_symc_set_config(td_handle symc_handle, const uapi_cipher_symc_ctrl_t *symc_ctrl)
{
if (crypto_check_soft_alg_flag(symc_handle) == TD_TRUE) {
return cipher_soft_symc_set_config(symc_handle, (crypto_symc_ctrl_t *)symc_ctrl);
}
return unify_uapi_cipher_symc_set_config(symc_handle, (crypto_symc_ctrl_t *)symc_ctrl);
}
td_s32 uapi_cipher_symc_attach(td_handle symc_handle, td_handle keyslot_handle)
{
if (crypto_check_soft_alg_flag(symc_handle) == TD_TRUE) {
return cipher_soft_symc_attach(symc_handle, keyslot_handle);
}
return unify_uapi_cipher_symc_attach(symc_handle, keyslot_handle);
}
td_s32 uapi_cipher_symc_get_keyslot_handle(td_handle symc_handle, td_handle *keyslot_handle)
{
return unify_uapi_cipher_symc_get_keyslot_handle(symc_handle, keyslot_handle);
}
td_s32 uapi_cipher_symc_set_key(td_handle symc_handle, td_u8 *key, td_u32 key_len)
{
if (crypto_check_soft_alg_flag(symc_handle) == TD_TRUE) {
return cipher_soft_symc_set_key(symc_handle, key, key_len);
}
return unify_uapi_cipher_symc_set_key(symc_handle, key, key_len);
}
td_s32 uapi_cipher_symc_encrypt(td_handle symc_handle, uapi_cipher_buf_attr *src_buf,
uapi_cipher_buf_attr *dst_buf, td_u32 length)
{
if (crypto_check_soft_alg_flag(symc_handle) == TD_TRUE) {
return cipher_soft_symc_crypto(symc_handle, SOFT_SYMC_OPERATION_ENCRYTP, (crypto_buf_attr *)src_buf,
(crypto_buf_attr *)dst_buf, length);
}
return unify_uapi_cipher_symc_encrypt(symc_handle, (crypto_buf_attr *)src_buf, (crypto_buf_attr *)dst_buf, length);
}
td_s32 uapi_cipher_symc_decrypt(td_handle symc_handle, uapi_cipher_buf_attr *src_buf,
uapi_cipher_buf_attr *dst_buf, td_u32 length)
{
if (crypto_check_soft_alg_flag(symc_handle) == TD_TRUE) {
return cipher_soft_symc_crypto(symc_handle, SOFT_SYMC_OPERATION_DECRYTP, (crypto_buf_attr *)src_buf,
(crypto_buf_attr *)dst_buf, length);
}
return unify_uapi_cipher_symc_decrypt(symc_handle, (crypto_buf_attr *)src_buf, (crypto_buf_attr *)dst_buf, length);
}
td_s32 uapi_cipher_symc_get_tag(td_handle symc_handle, td_u8 *tag, td_u32 tag_length)
{
if (crypto_check_soft_alg_flag(symc_handle) == TD_TRUE) {
return cipher_soft_symc_get_tag(symc_handle, tag, tag_length);
}
return unify_uapi_cipher_symc_get_tag(symc_handle, tag, tag_length);
}
td_s32 uapi_cipher_symc_encrypt_multi(td_handle symc_handle, const uapi_cipher_symc_ctrl_t *symc_ctrl,
const uapi_cipher_symc_pack *src_buf_pack, const uapi_cipher_symc_pack *dst_buf_pack, td_u32 pack_num)
{
return unify_uapi_cipher_symc_encrypt_multi(symc_handle, (crypto_symc_ctrl_t *)symc_ctrl,
(crypto_symc_pack *)src_buf_pack, (crypto_symc_pack *)dst_buf_pack, pack_num);
}
td_s32 uapi_cipher_symc_decrypt_multi(td_handle symc_handle, const uapi_cipher_symc_ctrl_t *symc_ctrl,
const uapi_cipher_symc_pack *src_buf_pack, const uapi_cipher_symc_pack *dst_buf_pack, td_u32 pack_num)
{
return unify_uapi_cipher_symc_decrypt_multi(symc_handle, (crypto_symc_ctrl_t *)symc_ctrl,
(crypto_symc_pack *)src_buf_pack, (crypto_symc_pack *)dst_buf_pack, pack_num);
}
td_s32 uapi_cipher_mac_start(td_handle *symc_handle, const uapi_cipher_symc_mac_attr *mac_attr)
{
return unify_uapi_cipher_mac_start(symc_handle, (crypto_symc_mac_attr *)mac_attr);
}
td_s32 uapi_cipher_mac_update(td_handle symc_handle, const uapi_cipher_buf_attr *src_buf, td_u32 length)
{
return unify_uapi_cipher_mac_update(symc_handle, (crypto_buf_attr *)src_buf, length);
}
td_s32 uapi_cipher_mac_finish(td_handle symc_handle, td_u8 *mac, td_u32 *mac_length)
{
return unify_uapi_cipher_mac_finish(symc_handle, mac, mac_length);
}
td_s32 uapi_cipher_pbkdf2(const uapi_cipher_kdf_pbkdf2_param *param, td_u8 *out, const td_u32 out_len)
{
return unify_uapi_cipher_pbkdf2((crypto_kdf_pbkdf2_param *)param, out, out_len);
}
td_s32 uapi_cipher_symc_cenc_decrypt(td_handle symc_handle, const uapi_cipher_symc_cenc_param *cenc_param,
const uapi_cipher_buf_attr *src_buf, const uapi_cipher_buf_attr *dst_buf, td_u32 length)
{
return unify_uapi_cipher_symc_cenc_decrypt(symc_handle, (crypto_symc_cenc_param *)cenc_param,
(crypto_buf_attr *)src_buf, (crypto_buf_attr *)dst_buf, length);
}
td_s32 uapi_cipher_hash_destroy(td_handle hash_handle)
{
return unify_uapi_cipher_hash_destroy(hash_handle);
}
td_s32 uapi_cipher_hash_get(td_handle hash_handle, uapi_cipher_hash_clone_ctx *hash_clone_ctx)
{
return unify_uapi_cipher_hash_get(hash_handle, (crypto_hash_clone_ctx *)hash_clone_ctx);
}
td_s32 uapi_cipher_hash_set(td_handle hash_handle, const uapi_cipher_hash_clone_ctx *hash_clone_ctx)
{
return unify_uapi_cipher_hash_set(hash_handle, (crypto_hash_clone_ctx *)hash_clone_ctx);
}
td_s32 uapi_cipher_hash_init(td_void)
{
return unify_uapi_cipher_hash_init();
}
td_s32 uapi_cipher_hash_deinit(td_void)
{
return unify_uapi_cipher_hash_deinit();
}
td_s32 uapi_cipher_hash_start(td_handle *hash_handle, const uapi_cipher_hash_attr *hash_attr)
{
if (hash_handle == TD_NULL || hash_attr == TD_NULL) {
crypto_log_err("input parameter is null!\n");
return TD_FAILURE;
}
hash_param param = {(td_u32)hash_attr->hash_type};
alg_item item = {HASH, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_hash_start(hash_handle, (crypto_hash_attr *)hash_attr);
}
return cipher_soft_hash_start(hash_handle, (crypto_hash_attr *)hash_attr);
}
td_s32 uapi_cipher_hash_update(td_handle hash_handle, const uapi_cipher_buf_attr *src_buf,
const td_u32 len)
{
if (crypto_check_soft_alg_flag(hash_handle) == TD_TRUE) {
return cipher_soft_hash_update(hash_handle, (crypto_buf_attr *)src_buf, len);
}
return unify_uapi_cipher_hash_update(hash_handle, (crypto_buf_attr *)src_buf, len);
}
td_s32 uapi_cipher_hash_finish(td_handle hash_handle, td_u8 *out, td_u32 *out_len)
{
if (crypto_check_soft_alg_flag(hash_handle) == TD_TRUE) {
return cipher_soft_hash_finish(hash_handle, out, out_len);
}
return unify_uapi_cipher_hash_finish(hash_handle, out, out_len);
}
td_s32 uapi_cipher_hash(uapi_cipher_hash_type hash_type, const td_u8 *input,
td_u32 input_len, td_u8 *hash, td_u32 hash_len)
{
td_s32 ret;
td_handle hash_handle = 0;
uapi_cipher_hash_attr hash_attr = {0};
uapi_cipher_buf_attr buf_attr = {
.virt_addr = (td_u8 *)input
};
ret = uapi_cipher_hash_init();
if (ret != TD_SUCCESS) {
return ret;
}
hash_attr.hash_type = hash_type;
hash_attr.is_long_term = TD_TRUE;
ret = uapi_cipher_hash_start(&hash_handle, &hash_attr);
if (ret != TD_SUCCESS) {
goto error_hash_deinit;
}
ret = uapi_cipher_hash_update(hash_handle, &buf_attr, input_len);
if (ret != TD_SUCCESS) {
goto error_hash_destroy;
}
ret = uapi_cipher_hash_finish(hash_handle, hash, &hash_len);
if (ret != TD_SUCCESS) {
goto error_hash_destroy;
}
(td_void)uapi_cipher_hash_deinit();
return ret;
error_hash_destroy:
(td_void)uapi_cipher_hash_destroy(hash_handle);
error_hash_deinit:
(td_void)uapi_cipher_hash_deinit();
return ret;
}
td_s32 uapi_cipher_pke_init(td_void)
{
return unify_uapi_cipher_pke_init();
}
td_s32 uapi_cipher_pke_deinit(td_void)
{
return unify_uapi_cipher_pke_deinit();
}
td_s32 uapi_cipher_pke_ecc_gen_key(uapi_cipher_pke_ecc_type curve_type,
const uapi_cipher_pke_data *input_priv_key, const uapi_cipher_pke_data *output_priv_key,
const uapi_cipher_ecc_point *output_pub_key)
{
pke_param param = {ECC, GENKEY};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_ecc_gen_key((td_u32)curve_type, (drv_pke_data *)input_priv_key,
(drv_pke_data *)output_priv_key, (drv_pke_ecc_point *)output_pub_key);
}
return cipher_soft_pke_ecc_gen_key((td_u32)curve_type, (drv_pke_data *)input_priv_key,
(drv_pke_data *)output_priv_key, (drv_pke_ecc_point *)output_pub_key);
}
td_s32 uapi_cipher_pke_ecc_gen_ecdh_key(uapi_cipher_pke_ecc_type curve_type,
const uapi_cipher_ecc_point *input_pub_key, const uapi_cipher_pke_data *input_priv_key,
const uapi_cipher_pke_data *output_shared_key)
{
pke_param param = {ECC, GENKEY};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_ecc_gen_ecdh_key((td_u32)curve_type, (drv_pke_ecc_point *)input_pub_key,
(drv_pke_data *)input_priv_key, (drv_pke_data *)output_shared_key);
}
return cipher_soft_pke_ecc_gen_ecdh_key((td_u32)curve_type, (drv_pke_ecc_point *)input_pub_key,
(drv_pke_data *)input_priv_key, (drv_pke_data *)output_shared_key);
}
td_s32 uapi_cipher_pke_ecdsa_sign(uapi_cipher_pke_ecc_type curve_type, const uapi_cipher_pke_data *priv_key,
const uapi_cipher_pke_data *hash, const uapi_cipher_ecc_sig *sig)
{
pke_param param = {ECC, SIGN};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_ecdsa_sign((td_u32)curve_type, (drv_pke_data *)priv_key, (drv_pke_data *)hash,
(drv_pke_ecc_sig *)sig);
}
return cipher_soft_pke_ecdsa_sign((td_u32)curve_type, (drv_pke_data *)priv_key, (drv_pke_data *)hash,
(drv_pke_ecc_sig *)sig);
}
td_s32 uapi_cipher_pke_ecdsa_verify(uapi_cipher_pke_ecc_type curve_type, const uapi_cipher_ecc_point *pub_key,
const uapi_cipher_pke_data *hash, const uapi_cipher_ecc_sig *sig)
{
pke_param param = {ECC, VERIFY};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_ecdsa_verify((td_u32)curve_type, (drv_pke_ecc_point *)pub_key,
(drv_pke_data *)hash, (drv_pke_ecc_sig *)sig);
}
return cipher_soft_pke_ecdsa_verify((td_u32)curve_type, (drv_pke_ecc_point *)pub_key, (drv_pke_data *)hash,
(drv_pke_ecc_sig *)sig);
}
td_s32 uapi_cipher_pke_check_dot_on_curve(uapi_cipher_pke_ecc_type curve_type,
const uapi_cipher_ecc_point *pub_key, td_bool *is_on_curve)
{
pke_param param = {ECC, VERIFY};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_check_dot_on_curve((td_u32)curve_type, (drv_pke_ecc_point *)pub_key, is_on_curve);
}
return cipher_soft_pke_check_dot_on_curve((td_u32)curve_type, (drv_pke_ecc_point *)pub_key, is_on_curve);
}
td_s32 uapi_cipher_pke_sm2_dsa_hash(const uapi_cipher_pke_data *sm2_id, const uapi_cipher_ecc_point *pub_key,
const uapi_cipher_pke_msg *msg, uapi_cipher_pke_data *hash)
{
hash_param param = {CRYPTO_HASH_TYPE_SM3};
alg_item item = {HASH, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_sm2_dsa_hash((drv_pke_data *)sm2_id, (drv_pke_ecc_point *)pub_key,
(drv_pke_msg *)msg, (drv_pke_data *)hash);
}
return cipher_soft_pke_sm2_dsa_hash((drv_pke_data *)sm2_id, (drv_pke_ecc_point *)pub_key, (drv_pke_msg *)msg,
(drv_pke_data *)hash);
}
td_s32 uapi_cipher_pke_sm2_public_encrypt(const uapi_cipher_ecc_point *pub_key,
const uapi_cipher_pke_data *plain_text, const uapi_cipher_pke_data *cipher_text)
{
pke_param param = {SM2, ENCRYPT};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_sm2_public_encrypt((drv_pke_ecc_point *)pub_key, (drv_pke_data *)plain_text,
(drv_pke_data *)cipher_text);
}
return cipher_soft_pke_sm2_public_encrypt((drv_pke_ecc_point *)pub_key, (drv_pke_data *)plain_text,
(drv_pke_data *)cipher_text);
}
td_s32 uapi_cipher_pke_sm2_private_decrypt(const uapi_cipher_pke_data *priv_key,
const uapi_cipher_pke_data *cipher_text, const uapi_cipher_pke_data *plain_text)
{
pke_param param = {SM2, DECRYPT};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_sm2_private_decrypt((drv_pke_data *)priv_key, (drv_pke_data *)cipher_text,
(drv_pke_data *)plain_text);
}
return cipher_soft_pke_sm2_private_decrypt((drv_pke_data *)priv_key, (drv_pke_data *)cipher_text,
(drv_pke_data *)plain_text);
}
td_s32 uapi_cipher_pke_rsa_gen_key(const uapi_cipher_pke_data *input_e, uapi_cipher_rsa_priv_key *priv_key)
{
pke_param param = {RSA, GENKEY};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_rsa_gen_key((drv_pke_data *)input_e, (drv_pke_rsa_priv_key *)priv_key);
}
return cipher_soft_pke_rsa_gen_key((drv_pke_data *)input_e, (drv_pke_rsa_priv_key *)priv_key);
}
td_s32 uapi_cipher_pke_dh_gen_key(uapi_cipher_pke_data *g_data, uapi_cipher_pke_data *p_data,
uapi_cipher_pke_data *input_priv_key, uapi_cipher_pke_data *output_priv_key, uapi_cipher_pke_data *pub_key)
{
return cipher_soft_pke_dh_gen_key((drv_pke_data *)g_data, (drv_pke_data *)p_data, (drv_pke_data *)input_priv_key,
(drv_pke_data *)output_priv_key, (drv_pke_data *)pub_key);
}
td_s32 uapi_cipher_pke_dh_compute_key(uapi_cipher_pke_data *p_data, uapi_cipher_pke_data *priv_key,
uapi_cipher_pke_data *other_pub_key, uapi_cipher_pke_data *shared_secret)
{
return cipher_soft_pke_dh_compute_key((drv_pke_data *)p_data, (drv_pke_data *)priv_key,
(drv_pke_data *)other_pub_key, (drv_pke_data *)shared_secret);
}
td_s32 uapi_cipher_pke_rsa_sign(const uapi_cipher_rsa_priv_key *priv_key, uapi_cipher_rsa_scheme scheme,
uapi_cipher_pke_hash_type hash_type, const uapi_cipher_pke_data *input_hash, uapi_cipher_pke_data *sign)
{
pke_param param = {RSA, SIGN};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_rsa_sign((drv_pke_rsa_priv_key *)priv_key, (td_u32)scheme, (td_u32)hash_type,
(drv_pke_data *)input_hash, (drv_pke_data *)sign);
}
return cipher_soft_pke_rsa_sign((drv_pke_rsa_priv_key *)priv_key, (td_u32)scheme, (td_u32)hash_type,
(drv_pke_data *)input_hash, (drv_pke_data *)sign);
}
td_s32 uapi_cipher_pke_rsa_verify(const uapi_cipher_rsa_pub_key *pub_key, uapi_cipher_rsa_scheme scheme,
uapi_cipher_pke_hash_type hash_type, uapi_cipher_pke_data *input_hash, const uapi_cipher_pke_data *sig)
{
pke_param param = {RSA, VERIFY};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_rsa_verify((drv_pke_rsa_pub_key *)pub_key, (td_u32)scheme, (td_u32)hash_type,
(drv_pke_data *)input_hash, (drv_pke_data *)sig);
}
return cipher_soft_pke_rsa_verify((drv_pke_rsa_pub_key *)pub_key, (td_u32)scheme, (td_u32)hash_type,
(drv_pke_data *)input_hash, (drv_pke_data *)sig);
}
td_s32 uapi_cipher_pke_rsa_public_encrypt(uapi_cipher_rsa_scheme scheme, uapi_cipher_pke_hash_type hash_type,
const uapi_cipher_rsa_pub_key *pub_key, const uapi_cipher_pke_data *input, const uapi_cipher_pke_data *label,
const uapi_cipher_pke_data *output)
{
pke_param param = {RSA, ENCRYPT};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_rsa_public_encrypt((td_u32)scheme, (td_u32)hash_type,
(drv_pke_rsa_pub_key *)pub_key, (drv_pke_data *)input, (drv_pke_data *)label, (drv_pke_data *)output);
}
return cipher_soft_pke_rsa_public_encrypt((td_u32)scheme, (td_u32)hash_type, (drv_pke_rsa_pub_key *)pub_key,
(drv_pke_data *)input, (drv_pke_data *)label, (drv_pke_data *)output);
}
td_s32 uapi_cipher_pke_rsa_private_decrypt(uapi_cipher_rsa_scheme scheme, uapi_cipher_pke_hash_type hash_type,
const uapi_cipher_rsa_priv_key *priv_key, const uapi_cipher_pke_data *input, const uapi_cipher_pke_data *label,
uapi_cipher_pke_data *output)
{
pke_param param = {RSA, DECRYPT};
alg_item item = {PKE, &param};
if (is_chip_hard_alg_support(&item)) {
return unify_uapi_cipher_pke_rsa_private_decrypt((td_u32)scheme, (td_u32)hash_type,
(drv_pke_rsa_priv_key *)priv_key, (drv_pke_data *)input, (drv_pke_data *)label, (drv_pke_data *)output);
}
return cipher_soft_pke_rsa_private_decrypt((td_u32)scheme, (td_u32)hash_type, (drv_pke_rsa_priv_key *)priv_key,
(drv_pke_data *)input, (drv_pke_data *)label, (drv_pke_data *)output);
}
td_s32 uapi_cipher_trng_get_random(td_u32 *randnum)
{
return unify_uapi_cipher_trng_get_random(randnum);
}
td_s32 uapi_cipher_trng_get_multi_random(td_u32 size, td_u8 *randnum)
{
return unify_uapi_cipher_trng_get_multi_random(size, randnum);
}