加解密(C/C++)
==========

以AES256、RSA1024、SM2和DES64为例，完成加解密。具体的场景介绍及支持的算法规格，请参考[加解密支持的算法](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-encryption-decryption-overview#支持的算法)。

#### 在CMake脚本中链接相关动态库

target\_link\_libraries(entry PUBLIC libhuks\_ndk.z.so)

#### 开发步骤

**生成密钥**

1.  指定密钥别名，密钥别名命名规范参考[密钥生成介绍及算法规格](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-key-generation-overview)。
    
2.  初始化密钥属性集。
    
3.  调用[OH\_Huks\_GenerateKeyItem](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-api-h#oh_huks_generatekeyitem)生成密钥，具体请参考[密钥生成](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-key-generation-overview)。
    

除此之外，开发者也可以参考[密钥导入](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-key-import-overview)，导入已有的密钥。

**加密**

1.  获取密钥别名。
    
2.  获取待加密的数据。
    
3.  调用[OH\_Huks\_InitParamSet](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-param-h#oh_huks_initparamset)指定算法参数配置。
    
    文档中提供多个示例，当使用不同算法时，请注意配置对应参数。
    
    *   使用AES算法加密，选取的分组模式为CBC、填充模式为PKCS7时，参数IV必选，请见开发案例：[AES/CBC/PKCS7](#aescbcpkcs7)。
    *   使用AES算法加密，选取的分组模式为GCM时，参数NONCE可选，AAD可选，请见开发案例：[AES/GCM/NoPadding](#aesgcmnopadding)。
    *   使用AES算法加密，选取的分组模式为CCM时，参数NONCE可选，AAD可选，请见开发案例：[AES/CCM/NoPadding](#aesccmnopadding)。
    *   使用RSA算法加密，需要选择相对应的分组模式、填充模式以及摘要算法DIGEST，请见开发案例：[RSA/ECB/PKCS1\_V1\_5](#rsaecbpkcs1_v1_5)和[RSA/ECB/OAEP/SHA256](#rsaecboaepsha256)。
    *   使用SM2算法加密，摘要算法DIGEST需要指定为SM3，请见开发案例：[SM2](#sm2)。
    
    详细规格请参考[加密/解密介绍及算法规格](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-encryption-decryption-overview)。
    
4.  调用[OH\_Huks\_InitSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-api-h#oh_huks_initsession)初始化密钥会话，并获取会话的句柄handle。
    
5.  调用[OH\_Huks\_FinishSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-api-h#oh_huks_finishsession)结束密钥会话，获取加密后的密文。
    

**解密**

1.  获取密钥别名。
    
2.  获取待解密的密文。
    
3.  调用[OH\_Huks\_InitParamSet](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-param-h#oh_huks_initparamset)指定算法参数配置。
    
    文档中提供多个示例，当使用不同算法时，请注意配置对应参数。
    
    *   使用AES算法解密，用例中选取的分组模式为GCM时，必须要填参数NONCE和参数AEAD，AAD可选，请见开发案例：[AES/GCM/NoPadding](#aesgcmnopadding)。
    *   其余示例参数与加密要求一致。
    
    详细规格请参考[加密/解密介绍及算法规格](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-encryption-decryption-overview)。
    
4.  调用[OH\_Huks\_InitSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-api-h#oh_huks_initsession)初始化密钥会话，并获取会话的句柄handle。
    
5.  调用[OH\_Huks\_FinishSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-api-h#oh_huks_finishsession)结束密钥会话，获取解密后的数据。
    

**删除密钥**

当密钥废弃不用时，需要调用OH\_Huks\_DeleteKeyItem删除密钥，具体请参考[密钥删除](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-delete-key-ndk)。

#### 开发案例

#### \[h2\]AES/CBC/PKCS7

#include "huks/native\_huks\_api.h"
#include "huks/native\_huks\_param.h"
#include "napi/native\_api.h"
#include <cstring>
#include "CryptoArchitectureKit/crypto\_architecture\_kit.h"

static OH\_Huks\_Result InitParamSet(struct OH\_Huks\_ParamSet \*\*paramSet, const struct OH\_Huks\_Param \*params,
    uint32\_t paramCount)
{
    OH\_Huks\_Result ret = OH\_Huks\_InitParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_AddParams(\*paramSet, params, paramCount);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    ret = OH\_Huks\_BuildParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    return ret;
}

static OH\_Crypto\_ErrCode genRandomNumber(uint32\_t randomLength, uint8\_t \*out)
{
    // 创建随机数生成器。
    OH\_CryptoRand \*rand = nullptr;
    OH\_Crypto\_ErrCode ret = OH\_CryptoRand\_Create(&rand);
    if (ret != CRYPTO\_SUCCESS) {
        return ret;
    }
    Crypto\_DataBlob blob = {out, randomLength};
    // 生成指定长度的随机数。
    ret = OH\_CryptoRand\_GenerateRandom(rand, randomLength, &blob);
    if (ret != CRYPTO\_SUCCESS) {
        OH\_CryptoRand\_Destroy(rand);
        return ret;
    }
    OH\_CryptoRand\_Destroy(rand);

    return CRYPTO\_SUCCESS;
}

static const uint32\_t IV\_SIZE = 16;
static uint8\_t IV\[IV\_SIZE\] = {0};
static OH\_Crypto\_ErrCode ret = genRandomNumber(IV\_SIZE, IV);
static struct OH\_Huks\_Param g\_genEncDecParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_AES},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT | OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_AES\_KEY\_SIZE\_256},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_NONE},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_CBC}};

static struct OH\_Huks\_Param g\_encryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_AES},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_AES\_KEY\_SIZE\_256},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_NONE},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_CBC},
    {.tag = OH\_HUKS\_TAG\_IV,
     .blob = {
         .size = IV\_SIZE,
         .data = (uint8\_t \*)IV
     }}};

static struct OH\_Huks\_Param g\_decryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_AES},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_AES\_KEY\_SIZE\_256},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_NONE},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_CBC},
    {.tag = OH\_HUKS\_TAG\_IV,
     .blob = {
         .size = IV\_SIZE,
         .data = (uint8\_t \*)IV
     }}};

static const uint32\_t AES\_COMMON\_SIZE = 1024;
OH\_Huks\_Result HksAesCipherTestEncrypt(const struct OH\_Huks\_Blob \*keyAlias,
                                       const struct OH\_Huks\_ParamSet \*encryptParamSet,
                                       const struct OH\_Huks\_Blob \*inData, struct OH\_Huks\_Blob \*cipherText)
{
    uint8\_t handleE\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleEncrypt = {sizeof(uint64\_t), handleE};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleEncrypt, encryptParamSet, inData, cipherText);
    return ret;
}

OH\_Huks\_Result HksAesCipherTestDecrypt(const struct OH\_Huks\_Blob \*keyAlias,
                                       const struct OH\_Huks\_ParamSet \*decryptParamSet,
                                       const struct OH\_Huks\_Blob \*cipherText, struct OH\_Huks\_Blob \*plainText,
                                       const struct OH\_Huks\_Blob \*inData)
{
    uint8\_t handleD\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleDecrypt = {sizeof(uint64\_t), handleD};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleDecrypt, decryptParamSet, cipherText, plainText);
    return ret;
}

napi\_value TestAesCbc(napi\_env env, napi\_callback\_info info)
{
    char tmpKeyAlias\[\] = "test\_enc\_dec";
    struct OH\_Huks\_Blob keyAlias = {(uint32\_t)strlen(tmpKeyAlias), (uint8\_t \*)tmpKeyAlias};
    struct OH\_Huks\_ParamSet \*genParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*encryptParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*decryptParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        /\* 1. 模拟生成密钥场景 \*/
        /\*
         \* 1.1. 获取生成密钥算法参数配置
         \*/
        ohResult = InitParamSet(&genParamSet, g\_genEncDecParams, sizeof(g\_genEncDecParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\*
         \* 1.2. 调用generateKeyItem
         \*/
        ohResult = OH\_Huks\_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 2. 模拟加密场景 \*/
        /\*
         \* 2.1. 获取加密算法参数配置
         \*/
        ohResult = InitParamSet(&encryptParamSet, g\_encryptParams, sizeof(g\_encryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        char tmpInData\[\] = "AES\_ECB\_INDATA\_1";
        struct OH\_Huks\_Blob inData = {(uint32\_t)strlen(tmpInData), (uint8\_t \*)tmpInData};
        uint8\_t cipher\[AES\_COMMON\_SIZE\] = {0};
        struct OH\_Huks\_Blob cipherText = {AES\_COMMON\_SIZE, cipher};
        /\*
         \* 2.2. 调用HksAesCipherTestEncrypt获取加密后的密文
         \*/
        ohResult = HksAesCipherTestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 3. 模拟解密场景 \*/
        /\*
         \* 3.1. 获取解密算法参数配置
         \*/
        ohResult = InitParamSet(&decryptParamSet, g\_decryptParams, sizeof(g\_decryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        uint8\_t plain\[AES\_COMMON\_SIZE\] = {0};
        struct OH\_Huks\_Blob plainText = {AES\_COMMON\_SIZE, plain};
        /\*
         \* 3.2. 调用HksAesCipherTestDecrypt获取解密后的数据
         \*/
        ohResult = HksAesCipherTestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
    } while (0);
    /\* 4. 模拟删除密钥场景 \*/
    /\*
     \* 4.1. 调用deleteKeyItem删除密钥
     \*/
    (void)OH\_Huks\_DeleteKeyItem(&keyAlias, genParamSet);

    OH\_Huks\_FreeParamSet(&genParamSet);
    OH\_Huks\_FreeParamSet(&encryptParamSet);
    OH\_Huks\_FreeParamSet(&decryptParamSet);
    
    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}

#### \[h2\]AES/GCM/NoPadding

准备加解密密钥材料：

#include "huks/native\_huks\_api.h"
#include "huks/native\_huks\_param.h"
#include "napi/native\_api.h"
#include <cstring>
#include "CryptoArchitectureKit/crypto\_architecture\_kit.h"

static OH\_Huks\_Result InitParamSet(struct OH\_Huks\_ParamSet \*\*paramSet, const struct OH\_Huks\_Param \*params,
    uint32\_t paramCount)
{
    OH\_Huks\_Result ret = OH\_Huks\_InitParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_AddParams(\*paramSet, params, paramCount);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    ret = OH\_Huks\_BuildParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    return ret;
}

static OH\_Crypto\_ErrCode genRandomNumber(uint32\_t randomLength, uint8\_t \*out)
{
    // 创建随机数生成器。
    OH\_CryptoRand \*rand = nullptr;
    OH\_Crypto\_ErrCode ret = OH\_CryptoRand\_Create(&rand);
    if (ret != CRYPTO\_SUCCESS) {
        return ret;
    }
    Crypto\_DataBlob blob = {out, randomLength};
    // 生成指定长度的随机数。
    ret = OH\_CryptoRand\_GenerateRandom(rand, randomLength, &blob);
    if (ret != CRYPTO\_SUCCESS) {
        OH\_CryptoRand\_Destroy(rand);
        return ret;
    }
    OH\_CryptoRand\_Destroy(rand);
 
    return CRYPTO\_SUCCESS;
}

static const uint32\_t NONCE\_SIZE = 12;
static const uint32\_t AAD\_SIZE = 16;
static const uint32\_t AE\_TAG\_SIZE = 16;
static char AEAD\[AE\_TAG\_SIZE\] = {0};
static char AAD\[AAD\_SIZE\] = "cdcdcdcdcdcdcdc"; // this is a test value, for real use it should be different every time
static uint8\_t NONCE\[NONCE\_SIZE\] = {0};
static OH\_Crypto\_ErrCode ret = genRandomNumber(NONCE\_SIZE, NONCE);
static struct OH\_Huks\_Param g\_genEncDecParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_AES},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT | OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_AES\_KEY\_SIZE\_256},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_NONE},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_GCM}};

static struct OH\_Huks\_Param g\_encryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_AES},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_AES\_KEY\_SIZE\_256},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_NONE},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_GCM},
    {.tag = OH\_HUKS\_TAG\_NONCE,
     .blob = {
         .size = NONCE\_SIZE,
         .data = (uint8\_t \*)NONCE // this is a test value, for real use the iv should be different every time
     }},
    {.tag = OH\_HUKS\_TAG\_ASSOCIATED\_DATA,
     .blob = {
         .size = AAD\_SIZE,
         .data = (uint8\_t \*)AAD // this is a test value, for real use the iv should be different every time
     }}};

static struct OH\_Huks\_Param g\_decryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_AES},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_AES\_KEY\_SIZE\_256},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_NONE},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_GCM},
    {.tag = OH\_HUKS\_TAG\_NONCE,
     .blob = {
         .size = NONCE\_SIZE,
         .data = (uint8\_t \*)NONCE // this is a test value, for real use the iv should be different every time
     }},
    {.tag = OH\_HUKS\_TAG\_ASSOCIATED\_DATA,
     .blob = {
         .size = AAD\_SIZE,
         .data = (uint8\_t \*)AAD // this is a test value, for real use the iv should be different every time
     }},
    {.tag = OH\_HUKS\_TAG\_AE\_TAG,
     .blob = {
         .size = AE\_TAG\_SIZE,
         .data = (uint8\_t \*)AEAD // this is a test value, for real use the iv should be different every time
     }}};

static const uint32\_t AES\_GCM\_SIZE = 1024;
OH\_Huks\_Result HksAesGcmTestEncrypt(const struct OH\_Huks\_Blob \*keyAlias,
    const struct OH\_Huks\_ParamSet \*encryptParamSet,
    const struct OH\_Huks\_Blob \*inData, struct OH\_Huks\_Blob \*cipherText)
{
    uint8\_t handleE\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleEncrypt = {sizeof(uint64\_t), handleE};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleEncrypt, encryptParamSet, inData, cipherText);
    return ret;
}

OH\_Huks\_Result HksAesGcmTestDecrypt(const struct OH\_Huks\_Blob \*keyAlias,
    const struct OH\_Huks\_ParamSet \*decryptParamSet,
    const struct OH\_Huks\_Blob \*cipherText, struct OH\_Huks\_Blob \*plainText,
    const struct OH\_Huks\_Blob \*inData)
{
    uint8\_t handleD\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleDecrypt = {sizeof(uint64\_t), handleD};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleDecrypt, decryptParamSet, cipherText, plainText);
    return ret;
}

执行加解密流程：

napi\_value TestAesGcm(napi\_env env, napi\_callback\_info info)
{
    char tmpKeyAlias\[\] = "test\_enc\_dec";
    struct OH\_Huks\_Blob keyAlias = {(uint32\_t)strlen(tmpKeyAlias), (uint8\_t \*)tmpKeyAlias};
    struct OH\_Huks\_ParamSet \*genParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*encryptParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*decryptParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        /\* 1. 模拟生成密钥场景 \*/
        /\*
         \* 1.1. 获取生成密钥算法参数配置
         \*/
        ohResult = InitParamSet(&genParamSet, g\_genEncDecParams, sizeof(g\_genEncDecParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\*
         \* 1.2. 调用generateKeyItem
         \*/
        ohResult = OH\_Huks\_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 2. 模拟加密场景 \*/
        /\*
         \* 2.1. 获取加密算法参数配置
         \*/
        ohResult = InitParamSet(&encryptParamSet, g\_encryptParams, sizeof(g\_encryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        char tmpInData\[\] = "AES\_GCM\_INDATA\_1";
        struct OH\_Huks\_Blob inData = {(uint32\_t)strlen(tmpInData), (uint8\_t \*)tmpInData};
        uint8\_t cipher\[AES\_GCM\_SIZE\] = {0};
        struct OH\_Huks\_Blob cipherText = {AES\_GCM\_SIZE, cipher};
        /\*
         \* 2.2. 调用HksAesGcmTestEncrypt获取加密后的密文
         \*/
        ohResult = HksAesGcmTestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 3. 模拟解密场景 \*/
        /\*
         \* 3.1. 获取解密算法参数配置
         \*/
        strncpy(AEAD, (char \*)cipherText.data + strlen(tmpInData), AE\_TAG\_SIZE);
        cipherText.data\[strlen(tmpInData)\] = '\\0';
        cipherText.size -= AE\_TAG\_SIZE;
        ohResult = InitParamSet(&decryptParamSet, g\_decryptParams, sizeof(g\_decryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\*
         \* 3.2. 调用HksAesGcmTestDecrypt获取解密后的数据
         \*/
        uint8\_t plainBuffer\[AES\_GCM\_SIZE\] = {0};
        struct OH\_Huks\_Blob plainText = {AES\_GCM\_SIZE, plainBuffer};
        ohResult = HksAesGcmTestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
    } while (0);
    /\* 4. 模拟删除密钥场景 \*/
    /\*
     \* 4.1. 调用deleteKeyItem删除密钥
     \*/
    (void)OH\_Huks\_DeleteKeyItem(&keyAlias, genParamSet);

    OH\_Huks\_FreeParamSet(&genParamSet);
    OH\_Huks\_FreeParamSet(&encryptParamSet);
    OH\_Huks\_FreeParamSet(&decryptParamSet);
    
    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}

#### \[h2\]AES/CCM/NoPadding

#include "huks/native\_huks\_api.h"
#include "huks/native\_huks\_param.h"
#include "napi/native\_api.h"
#include <string.h>

static const uint32\_t IV\_SIZE = 16;
static const uint32\_t AEAD\_TAG\_LEN = 14;
static char IV\[IV\_SIZE\] = { 0 }; // this is a test value, for real use the iv should be different every time.
static char AEAD\[AEAD\_TAG\_LEN\] = { 0 };
static char NONCE\[OH\_HUKS\_AE\_NONCE\_LEN\] = { 0 };
static struct OH\_Huks\_Param g\_genEncDecParams\[\] = {
    {
        .tag = OH\_HUKS\_TAG\_ALGORITHM,
        .uint32Param = OH\_HUKS\_ALG\_AES
    }, {
        .tag = OH\_HUKS\_TAG\_PURPOSE,
        .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT | OH\_HUKS\_KEY\_PURPOSE\_DECRYPT
    }, {
        .tag = OH\_HUKS\_TAG\_KEY\_SIZE,
        .uint32Param = OH\_HUKS\_AES\_KEY\_SIZE\_256
    }, {
        .tag = OH\_HUKS\_TAG\_PADDING,
        .uint32Param = OH\_HUKS\_PADDING\_NONE
    }, {
        .tag = OH\_HUKS\_TAG\_BLOCK\_MODE,
        .uint32Param = OH\_HUKS\_MODE\_CCM
    }
};
static struct OH\_Huks\_Param g\_encryptParams\[\] = {
    {
        .tag = OH\_HUKS\_TAG\_ALGORITHM,
        .uint32Param = OH\_HUKS\_ALG\_AES
    }, {
        .tag = OH\_HUKS\_TAG\_PURPOSE,
        .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT
    }, {
        .tag = OH\_HUKS\_TAG\_KEY\_SIZE,
        .uint32Param = OH\_HUKS\_AES\_KEY\_SIZE\_256
    }, {
        .tag = OH\_HUKS\_TAG\_PADDING,
        .uint32Param = OH\_HUKS\_PADDING\_NONE
    }, {
        .tag = OH\_HUKS\_TAG\_BLOCK\_MODE,
        .uint32Param = OH\_HUKS\_MODE\_CCM
    }, {
        .tag = OH\_HUKS\_TAG\_IV,
        .blob = {
            .size = IV\_SIZE,
            .data = (uint8\_t \*)IV // this is a test value, for real use the iv should be different every time.
        }
    }, {
        .tag = OH\_HUKS\_TAG\_NONCE,
        .blob = {
            .size = OH\_HUKS\_AE\_NONCE\_LEN,
            .data = (uint8\_t \*)NONCE
        }
    }, {
        .tag = OH\_HUKS\_TAG\_AE\_TAG\_LEN,
        .uint32Param = AEAD\_TAG\_LEN
    }
};
static struct OH\_Huks\_Param g\_decryptParams\[\] = {
    {
        .tag = OH\_HUKS\_TAG\_ALGORITHM,
        .uint32Param = OH\_HUKS\_ALG\_AES
    }, {
        .tag = OH\_HUKS\_TAG\_PURPOSE,
        .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_DECRYPT
    }, {
        .tag = OH\_HUKS\_TAG\_KEY\_SIZE,
        .uint32Param = OH\_HUKS\_AES\_KEY\_SIZE\_256
    }, {
        .tag = OH\_HUKS\_TAG\_PADDING,
        .uint32Param = OH\_HUKS\_PADDING\_NONE
    }, {
        .tag = OH\_HUKS\_TAG\_BLOCK\_MODE,
        .uint32Param = OH\_HUKS\_MODE\_CCM
    }, {
        .tag = OH\_HUKS\_TAG\_IV,
        .blob = {
            .size = IV\_SIZE,
            .data = (uint8\_t \*)IV // this is a test value, for real use the iv should be different every time.
        }
    }, {
        .tag = OH\_HUKS\_TAG\_NONCE,
        .blob = {
            .size = OH\_HUKS\_AE\_NONCE\_LEN,
            .data = (uint8\_t \*)NONCE
        }
    }, {
        .tag = OH\_HUKS\_TAG\_AE\_TAG,
        .blob = {
            .size = AEAD\_TAG\_LEN,
            .data = (uint8\_t \*)AEAD
        }
    }, {
        .tag = OH\_HUKS\_TAG\_AE\_TAG\_LEN,
        .uint32Param = AEAD\_TAG\_LEN
    }
};
static const uint32\_t AES\_COMMON\_SIZE = 1024;

OH\_Huks\_Result InitParamSet(
    struct OH\_Huks\_ParamSet \*\*paramSet,
    const struct OH\_Huks\_Param \*params,
    uint32\_t paramCount)
{
    OH\_Huks\_Result ret = OH\_Huks\_InitParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_AddParams(\*paramSet, params, paramCount);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    ret = OH\_Huks\_BuildParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    return ret;
}

OH\_Huks\_Result HksAesCipherTestEncrypt(
        const struct OH\_Huks\_Blob \*keyAlias, const struct OH\_Huks\_ParamSet \*encryptParamSet,
        const struct OH\_Huks\_Blob \*inData, struct OH\_Huks\_Blob \*cipherText)
{
    uint8\_t handleE\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleEncrypt = {sizeof(uint64\_t), handleE};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleEncrypt, encryptParamSet, inData, cipherText);
    return ret;
}

OH\_Huks\_Result HksAesCipherTestDecrypt(const struct OH\_Huks\_Blob \*keyAlias,
    const struct OH\_Huks\_ParamSet \*decryptParamSet, const struct OH\_Huks\_Blob \*cipherText,
    struct OH\_Huks\_Blob \*plainText)
{
    uint8\_t handleD\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleDecrypt = {sizeof(uint64\_t), handleD};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleDecrypt, decryptParamSet, cipherText, plainText);
    return ret;
}

static napi\_value EncDecKey(napi\_env env, napi\_callback\_info info)
{
    char tmpKeyAlias\[\] = "test\_aes\_ccm\_enc\_dec";
    struct OH\_Huks\_Blob keyAlias = { (uint32\_t)strlen(tmpKeyAlias), (uint8\_t \*)tmpKeyAlias };
    struct OH\_Huks\_ParamSet \*genParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*encryptParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*decryptParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        /\* 1. Generate Key \*/
        /\*
        \* 模拟生成密钥场景
        \* 1.1. 确定密钥别名
        \*/
        /\*
        \* 1.2. 获取生成密钥算法参数配置
        \*/
        ohResult = InitParamSet(&genParamSet, g\_genEncDecParams, sizeof(g\_genEncDecParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\*
        \* 1.3. 调用generateKeyItem
        \*/
        ohResult = OH\_Huks\_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 2. Encrypt \*/
        /\*
        \* 模拟加密场景
        \* 2.1. 获取密钥别名
        \*/
        /\*
        \* 2.2. 获取待加密的数据
        \*/
        /\*
        \* 2.3. 获取加密算法参数配置
        \*/
        ohResult = InitParamSet(&encryptParamSet, g\_encryptParams, sizeof(g\_encryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        char tmpInData\[\] = "AES\_CCM\_INDATA\_1";
        uint32\_t dataLen = (uint32\_t)strlen(tmpInData);
        struct OH\_Huks\_Blob inData = { dataLen, (uint8\_t \*)tmpInData };
        uint8\_t cipher\[AES\_COMMON\_SIZE\] = {0};
        struct OH\_Huks\_Blob cipherText = {AES\_COMMON\_SIZE, cipher};
        /\*
        \* 2.4. 调用initSession获取handle
        \*/
        /\*
        \* 2.5. 调用finishSession获取加密后的密文
        \*/
        ohResult = HksAesCipherTestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        strncpy(AEAD, (char \*)cipherText.data + dataLen, AEAD\_TAG\_LEN);
        cipherText.data\[dataLen\] = '\\0';
        cipherText.size -= AEAD\_TAG\_LEN;
        /\* 3. Decrypt \*/
        /\*
        \* 模拟解密场景
        \* 3.1. 获取密钥别名
        \*/
        /\*
        \* 3.2. 获取待解密的密文
        \*/
        /\*
        \* 3.3. 获取解密算法参数配置
        \*/
        ohResult = InitParamSet(&decryptParamSet, g\_decryptParams, sizeof(g\_decryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        uint8\_t plain\[AES\_COMMON\_SIZE\] = {0};
        struct OH\_Huks\_Blob plainText = {AES\_COMMON\_SIZE, plain};
        /\*
        \* 3.4. 调用initSession获取handle
        \*/
        /\*
        \* 3.5. 调用finishSession获取解密后的数据
        \*/
        ohResult = HksAesCipherTestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText);
    } while (0);
    /\* 4. Delete Key \*/
    /\*
    \* 模拟删除密钥场景
    \* 4.1. 获取密钥别名
    \*/
    /\*
    \* 4.2. 调用deleteKeyItem删除密钥
    \*/
    (void)OH\_Huks\_DeleteKeyItem(&keyAlias, genParamSet);
        
    OH\_Huks\_FreeParamSet(&genParamSet);
    OH\_Huks\_FreeParamSet(&encryptParamSet);
    OH\_Huks\_FreeParamSet(&decryptParamSet);
    
    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}

#### \[h2\]RSA/ECB/PKCS1\_V1\_5

#include "huks/native\_huks\_api.h"
#include "huks/native\_huks\_param.h"
#include "napi/native\_api.h"
#include <cstring>

static OH\_Huks\_Result InitParamSet(struct OH\_Huks\_ParamSet \*\*paramSet, const struct OH\_Huks\_Param \*params,
                                   uint32\_t paramCount)
{
    OH\_Huks\_Result ret = OH\_Huks\_InitParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_AddParams(\*paramSet, params, paramCount);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    ret = OH\_Huks\_BuildParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    return ret;
}

static struct OH\_Huks\_Param g\_genEncDecParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_RSA},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT | OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_RSA\_KEY\_SIZE\_1024},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_ECB},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_PKCS1\_V1\_5},
    {.tag = OH\_HUKS\_TAG\_DIGEST, .uint32Param = OH\_HUKS\_DIGEST\_NONE}};

static struct OH\_Huks\_Param g\_encryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_RSA},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_RSA\_KEY\_SIZE\_1024},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_PKCS1\_V1\_5},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_ECB},
    {.tag = OH\_HUKS\_TAG\_DIGEST, .uint32Param = OH\_HUKS\_DIGEST\_NONE}};

static struct OH\_Huks\_Param g\_decryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_RSA},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_RSA\_KEY\_SIZE\_1024},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_PKCS1\_V1\_5},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_ECB},
    {.tag = OH\_HUKS\_TAG\_DIGEST, .uint32Param = OH\_HUKS\_DIGEST\_NONE}};

static const uint32\_t RSA\_COMMON\_SIZE = 1024;
OH\_Huks\_Result HksRsaPkcsTestEncrypt(const struct OH\_Huks\_Blob \*keyAlias,
    const struct OH\_Huks\_ParamSet \*encryptParamSet,
    const struct OH\_Huks\_Blob \*inData, struct OH\_Huks\_Blob \*cipherText)
{
    uint8\_t handleE\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleEncrypt = {sizeof(uint64\_t), handleE};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleEncrypt, encryptParamSet, inData, cipherText);
    return ret;
}

OH\_Huks\_Result HksRsaPkcsTestDecrypt(const struct OH\_Huks\_Blob \*keyAlias,
    const struct OH\_Huks\_ParamSet \*decryptParamSet,
    const struct OH\_Huks\_Blob \*cipherText, struct OH\_Huks\_Blob \*plainText,
    const struct OH\_Huks\_Blob \*inData)
{
    uint8\_t handleD\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleDecrypt = {sizeof(uint64\_t), handleD};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleDecrypt, decryptParamSet, cipherText, plainText);
    return ret;
}

napi\_value TestRsaEcbPkcs(napi\_env env, napi\_callback\_info info)
{
    char tmpKeyAlias\[\] = "test\_enc\_dec";
    struct OH\_Huks\_Blob keyAlias = {(uint32\_t)strlen(tmpKeyAlias), (uint8\_t \*)tmpKeyAlias};
    struct OH\_Huks\_ParamSet \*genParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*encryptParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*decryptParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        /\* 1. 模拟生成密钥场景 \*/
        /\*
         \* 1.1. 获取生成密钥算法参数配置
         \*/
        ohResult = InitParamSet(&genParamSet, g\_genEncDecParams, sizeof(g\_genEncDecParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\*
         \* 1.2. 调用generateKeyItem
         \*/
        ohResult = OH\_Huks\_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 2. 模拟加密场景 \*/
        /\*
         \* 2.1. 获取加密算法参数配置
         \*/
        ohResult = InitParamSet(&encryptParamSet, g\_encryptParams, sizeof(g\_encryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        char tmpInData\[\] = "RSA\_ECB\_OAEP\_IN";
        struct OH\_Huks\_Blob inData = {(uint32\_t)strlen(tmpInData), (uint8\_t \*)tmpInData};
        uint8\_t cipher\[RSA\_COMMON\_SIZE\] = {0};
        struct OH\_Huks\_Blob cipherText = {RSA\_COMMON\_SIZE, cipher};
        /\*
         \* 2.2. 调用HksRsaPkcsTestEncrypt获取加密后的密文
         \*/
        ohResult = HksRsaPkcsTestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 3. 模拟解密场景 \*/
        /\*
         \* 3.1. 获取解密算法参数配置
         \*/
        ohResult = InitParamSet(&decryptParamSet, g\_decryptParams, sizeof(g\_decryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        uint8\_t plain\[RSA\_COMMON\_SIZE\] = {0};
        struct OH\_Huks\_Blob plainText = {RSA\_COMMON\_SIZE, plain};
        /\*
         \* 3.2. 调用HksRsaPkcsTestDecrypt获取解密后的数据
         \*/
        ohResult = HksRsaPkcsTestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
    } while (0);
    /\* 4. 模拟删除密钥场景 \*/
    /\*
     \* 4.1. 调用deleteKeyItem删除密钥
     \*/
    (void)OH\_Huks\_DeleteKeyItem(&keyAlias, genParamSet);

    OH\_Huks\_FreeParamSet(&genParamSet);
    OH\_Huks\_FreeParamSet(&encryptParamSet);
    OH\_Huks\_FreeParamSet(&decryptParamSet);
    
    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}

#### \[h2\]RSA/ECB/OAEP/SHA256

#include "huks/native\_huks\_api.h"
#include "huks/native\_huks\_param.h"
#include "napi/native\_api.h"
#include <cstring>

static OH\_Huks\_Result InitParamSet(struct OH\_Huks\_ParamSet \*\*paramSet, const struct OH\_Huks\_Param \*params,
    uint32\_t paramCount)
{
    OH\_Huks\_Result ret = OH\_Huks\_InitParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_AddParams(\*paramSet, params, paramCount);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    ret = OH\_Huks\_BuildParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    return ret;
}

static struct OH\_Huks\_Param g\_genEncDecParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_RSA},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT | OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_RSA\_KEY\_SIZE\_1024},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_OAEP},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_ECB},
    {.tag = OH\_HUKS\_TAG\_DIGEST, .uint32Param = OH\_HUKS\_DIGEST\_SHA256}};

static struct OH\_Huks\_Param g\_encryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_RSA},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_RSA\_KEY\_SIZE\_1024},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_OAEP},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_ECB},
    {.tag = OH\_HUKS\_TAG\_DIGEST, .uint32Param = OH\_HUKS\_DIGEST\_SHA256}};

static struct OH\_Huks\_Param g\_decryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_RSA},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_RSA\_KEY\_SIZE\_1024},
    {.tag = OH\_HUKS\_TAG\_PADDING, .uint32Param = OH\_HUKS\_PADDING\_OAEP},
    {.tag = OH\_HUKS\_TAG\_BLOCK\_MODE, .uint32Param = OH\_HUKS\_MODE\_ECB},
    {.tag = OH\_HUKS\_TAG\_DIGEST, .uint32Param = OH\_HUKS\_DIGEST\_SHA256}};

static const uint32\_t RSA\_COMMON\_SIZE = 1024;
OH\_Huks\_Result HksRsaOaepTestEncrypt(const struct OH\_Huks\_Blob \*keyAlias,
    const struct OH\_Huks\_ParamSet \*encryptParamSet,
    const struct OH\_Huks\_Blob \*inData, struct OH\_Huks\_Blob \*cipherText)
{
    uint8\_t handleE\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleEncrypt = {sizeof(uint64\_t), handleE};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleEncrypt, encryptParamSet, inData, cipherText);
    return ret;
}

OH\_Huks\_Result HksRsaOaepTestDecrypt(const struct OH\_Huks\_Blob \*keyAlias,
    const struct OH\_Huks\_ParamSet \*decryptParamSet,
    const struct OH\_Huks\_Blob \*cipherText, struct OH\_Huks\_Blob \*plainText,
    const struct OH\_Huks\_Blob \*inData)
{
    uint8\_t handleD\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleDecrypt = {sizeof(uint64\_t), handleD};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleDecrypt, decryptParamSet, cipherText, plainText);
    return ret;
}

napi\_value TestRsaEcbOaep(napi\_env env, napi\_callback\_info info)
{
    char tmpKeyAlias\[\] = "test\_enc\_dec";
    struct OH\_Huks\_Blob keyAlias = {(uint32\_t)strlen(tmpKeyAlias), (uint8\_t \*)tmpKeyAlias};
    struct OH\_Huks\_ParamSet \*genParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*encryptParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*decryptParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        /\* 1. 模拟生成密钥场景 \*/
        /\*
         \* 1.1. 获取生成密钥算法参数配置
         \*/
        ohResult = InitParamSet(&genParamSet, g\_genEncDecParams, sizeof(g\_genEncDecParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\*
         \* 1.2. 调用generateKeyItem
         \*/
        ohResult = OH\_Huks\_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 2. 模拟加密场景 \*/
        /\*
         \* 2.1. 获取加密算法参数配置
         \*/
        ohResult = InitParamSet(&encryptParamSet, g\_encryptParams, sizeof(g\_encryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        char tmpInData\[\] = "RSA\_ECB\_OAEP\_IN";
        struct OH\_Huks\_Blob inData = {(uint32\_t)strlen(tmpInData), (uint8\_t \*)tmpInData};
        uint8\_t cipher\[RSA\_COMMON\_SIZE\] = {0};
        struct OH\_Huks\_Blob cipherText = {RSA\_COMMON\_SIZE, cipher};
        /\*
         \* 2.2. 调用HksRsaOaepTestEncrypt获取加密后的密文
         \*/
        ohResult = HksRsaOaepTestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 3. 模拟解密场景 \*/
        /\*
         \* 3.1. 获取解密算法参数配置
         \*/
        ohResult = InitParamSet(&decryptParamSet, g\_decryptParams, sizeof(g\_decryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        uint8\_t plain\[RSA\_COMMON\_SIZE\] = {0};
        struct OH\_Huks\_Blob plainText = {RSA\_COMMON\_SIZE, plain};
        /\*
         \* 3.2. 调用HksRsaOaepTestDecrypt获取解密后的数据
         \*/
        ohResult = HksRsaOaepTestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
    } while (0);
    /\* 4. 模拟删除密钥场景 \*/
    /\*
     \* 4.1. 调用deleteKeyItem删除密钥
     \*/
    (void)OH\_Huks\_DeleteKeyItem(&keyAlias, genParamSet);

    OH\_Huks\_FreeParamSet(&genParamSet);
    OH\_Huks\_FreeParamSet(&encryptParamSet);
    OH\_Huks\_FreeParamSet(&decryptParamSet);
    
    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}

#### \[h2\]SM2

#include "huks/native\_huks\_api.h"
#include "huks/native\_huks\_param.h"
#include "napi/native\_api.h"
#include <cstring>

static OH\_Huks\_Result InitParamSet(struct OH\_Huks\_ParamSet \*\*paramSet, const struct OH\_Huks\_Param \*params,
    uint32\_t paramCount)
{
    OH\_Huks\_Result ret = OH\_Huks\_InitParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_AddParams(\*paramSet, params, paramCount);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    ret = OH\_Huks\_BuildParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeParamSet(paramSet);
        return ret;
    }
    return ret;
}

static struct OH\_Huks\_Param g\_genEncDecParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_SM2},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT | OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_SM2\_KEY\_SIZE\_256},
    {.tag = OH\_HUKS\_TAG\_DIGEST, .uint32Param = OH\_HUKS\_DIGEST\_SM3}};

static struct OH\_Huks\_Param g\_encryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_SM2},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_ENCRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_SM2\_KEY\_SIZE\_256},
    {.tag = OH\_HUKS\_TAG\_DIGEST, .uint32Param = OH\_HUKS\_DIGEST\_SM3}};

static struct OH\_Huks\_Param g\_decryptParams\[\] = {
    {.tag = OH\_HUKS\_TAG\_ALGORITHM, .uint32Param = OH\_HUKS\_ALG\_SM2},
    {.tag = OH\_HUKS\_TAG\_PURPOSE, .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_DECRYPT},
    {.tag = OH\_HUKS\_TAG\_KEY\_SIZE, .uint32Param = OH\_HUKS\_SM2\_KEY\_SIZE\_256},
    {.tag = OH\_HUKS\_TAG\_DIGEST, .uint32Param = OH\_HUKS\_DIGEST\_SM3}};

static const uint32\_t SM2\_SIZE = 1024;
OH\_Huks\_Result HksSm2TestEncrypt(const struct OH\_Huks\_Blob \*keyAlias,
    const struct OH\_Huks\_ParamSet \*encryptParamSet,
    const struct OH\_Huks\_Blob \*inData, struct OH\_Huks\_Blob \*cipherText)
{
    uint8\_t handleE\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleEncrypt = {sizeof(uint64\_t), handleE};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleEncrypt, encryptParamSet, inData, cipherText);
    return ret;
}

OH\_Huks\_Result HksSm2TestDecrypt(const struct OH\_Huks\_Blob \*keyAlias,
    const struct OH\_Huks\_ParamSet \*decryptParamSet,
    const struct OH\_Huks\_Blob \*cipherText, struct OH\_Huks\_Blob \*plainText,
    const struct OH\_Huks\_Blob \*inData)
{
    uint8\_t handleD\[sizeof(uint64\_t)\] = {0};
    struct OH\_Huks\_Blob handleDecrypt = {sizeof(uint64\_t), handleD};
    OH\_Huks\_Result ret = OH\_Huks\_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_FinishSession(&handleDecrypt, decryptParamSet, cipherText, plainText);
    return ret;
}

napi\_value TestSm2(napi\_env env, napi\_callback\_info info)
{
    char tmpKeyAlias\[\] = "test\_sm2key";
    struct OH\_Huks\_Blob keyAlias = {(uint32\_t)strlen(tmpKeyAlias), (uint8\_t \*)tmpKeyAlias};
    struct OH\_Huks\_ParamSet \*genParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*encryptParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*decryptParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        /\* 1. 模拟生成密钥场景 \*/
        /\*
         \* 1.1. 获取生成密钥算法参数配置
         \*/
        ohResult = InitParamSet(&genParamSet, g\_genEncDecParams, sizeof(g\_genEncDecParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\*
         \* 1.2. 调用generateKeyItem
         \*/
        ohResult = OH\_Huks\_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 2. 模拟加密场景 \*/
        /\*
         \* 2.1. 获取加密算法参数配置
         \*/
        ohResult = InitParamSet(&encryptParamSet, g\_encryptParams, sizeof(g\_encryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        char tmpInData\[\] = "AES\_ECB\_INDATA\_1";
        struct OH\_Huks\_Blob inData = {(uint32\_t)strlen(tmpInData), (uint8\_t \*)tmpInData};
        uint8\_t cipher\[SM2\_SIZE\] = {0};
        struct OH\_Huks\_Blob cipherText = {SM2\_SIZE, cipher};
        /\*
         \* 2.2. 调用HksSm2TestEncrypt获取加密后的密文
         \*/
        ohResult = HksSm2TestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 3. 模拟解密场景 \*/
        /\*
         \* 3.1. 获取解密算法参数配置
         \*/
        ohResult = InitParamSet(&decryptParamSet, g\_decryptParams, sizeof(g\_decryptParams) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        uint8\_t plain\[SM2\_SIZE\] = {0};
        struct OH\_Huks\_Blob plainText = {SM2\_SIZE, plain};
        /\*
         \* 3.2. 调用HksSm2TestDecrypt获取解密后的数据
         \*/
        ohResult = HksSm2TestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
    } while (0);
    /\* 4. 模拟删除密钥场景 \*/
    /\*
     \* 4.1. 调用deleteKeyItem删除密钥
     \*/
    (void)OH\_Huks\_DeleteKeyItem(&keyAlias, genParamSet);

    OH\_Huks\_FreeParamSet(&genParamSet);
    OH\_Huks\_FreeParamSet(&encryptParamSet);
    OH\_Huks\_FreeParamSet(&decryptParamSet);
    
    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}