签名/验签(C/C++)
============

以密钥算法为RSA、摘要算法为SHA384、填充模式为PSS的密钥为例，完成签名、验签：

*   [密钥算法为RSA、摘要算法为SHA384、填充模式为PSS](#rsasha384pss)

具体的场景介绍及支持的算法规格，请参考[签名/验签介绍及算法规格](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-ukey-signing-signature-verification-overview)。

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

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

#### 开发步骤

**签名**

1.  通过证书管理系统能力提供的[证书选择接口](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certificatemanagerdialogopenauthorizedialog22)获取[keyUri](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certreference22)作为resourceId，并作为密钥别名，[打开资源](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-open-close-resource-ndk#打开资源)后完成PIN码认证。
    
2.  指定待签名的明文数据。
    
3.  调用[OH\_Huks\_InitParamSet](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-param-h#oh_huks_initparamset)指定算法参数配置，并指定KeyClass参数，tag为[OH\_HUKS\_TAG\_KEY\_CLASS](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-type-h#oh_huks_tag)，值为[OH\_HUKS\_KEY\_CLASS\_EXTENSION](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-type-h#oh_huks_keyclasstype)。
    
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)结束密钥会话，获取签名signature。
    

**验签**

1.  通过证书管理系统能力提供的[证书选择接口](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certificatemanagerdialogopenauthorizedialog22)获取[keyUri](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certreference22)作为resourceId，并作为密钥别名，然后[打开资源](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-open-close-resource-ndk#打开资源)。
    
2.  获取待验证的签名。
    
3.  调用[OH\_Huks\_InitParamSet](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-param-h#oh_huks_initparamset)指定算法参数配置，并指定KeyClass参数，tag为[OH\_HUKS\_TAG\_KEY\_CLASS](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-type-h#oh_huks_tag)，值为[OH\_HUKS\_KEY\_CLASS\_EXTENSION](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-type-h#oh_huks_keyclasstype)。
    
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\_UpdateSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-api-h#oh_huks_updatesession)更新密钥会话。
    
6.  调用[OH\_Huks\_FinishSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-api-h#oh_huks_finishsession)结束密钥会话，验证签名。
    

#### 开发案例

#### \[h2\]RSA/SHA384/PSS

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

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\_signParamsTest\[\] = {
    {
        .tag = OH\_HUKS\_TAG\_ALGORITHM,
        .uint32Param = OH\_HUKS\_ALG\_RSA
    }, {
        .tag = OH\_HUKS\_TAG\_PURPOSE,
        .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_SIGN
    }, {
        .tag = OH\_HUKS\_TAG\_KEY\_SIZE,
        .uint32Param = OH\_HUKS\_RSA\_KEY\_SIZE\_2048
    }, {
        .tag = OH\_HUKS\_TAG\_PADDING,
        .uint32Param = OH\_HUKS\_PADDING\_PSS
    }, {
        .tag = OH\_HUKS\_TAG\_DIGEST,
        .uint32Param = OH\_HUKS\_DIGEST\_SHA384
    }, {
        .tag = OH\_HUKS\_TAG\_KEY\_CLASS,
        .uint32Param = OH\_HUKS\_KEY\_CLASS\_EXTENSION
    }
};

static struct OH\_Huks\_Param g\_verifyParamsTest\[\] = {
    {
        .tag = OH\_HUKS\_TAG\_ALGORITHM,
        .uint32Param = OH\_HUKS\_ALG\_RSA
    }, {
        .tag = OH\_HUKS\_TAG\_PURPOSE,
        .uint32Param = OH\_HUKS\_KEY\_PURPOSE\_VERIFY
    }, {
        .tag = OH\_HUKS\_TAG\_KEY\_SIZE,
        .uint32Param = OH\_HUKS\_RSA\_KEY\_SIZE\_2048
    }, {
        .tag = OH\_HUKS\_TAG\_PADDING,
        .uint32Param = OH\_HUKS\_PADDING\_PSS
    }, {
        .tag = OH\_HUKS\_TAG\_DIGEST,
        .uint32Param = OH\_HUKS\_DIGEST\_SHA384
    }, {
        .tag = OH\_HUKS\_TAG\_KEY\_CLASS,
        .uint32Param = OH\_HUKS\_KEY\_CLASS\_EXTENSION
    }
};

static const uint32\_t RSA\_COMMON\_SIZE = 1024;
static const char \*DATA\_TO\_SIGN = "Hks\_RSA\_Sign\_Verify\_Test\_0000000000000000000000000000000000000000000000000000000"
                                  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
                                  "0000000000000000000000000000000000000000000000000000000000000000000000000\_string";
static const char \*KEY\_ALIAS = "{\\"providerName\\":\\"testProviderName\\",\\"abilityName\\":\\"CryptoExtension\\","
                              "\\"bundleName\\":\\"com.example.cryptoapplication\\",\\"index\\":{\\"key\\":\\"testKey\\"}}";

static napi\_value SignVerifyKey(napi\_env env, napi\_callback\_info info)
{
    // 假设keyAlias是获取的resourceId
    struct OH\_Huks\_Blob keyAlias = {
        (uint32\_t)strlen(KEY\_ALIAS),
        (uint8\_t \*)KEY\_ALIAS
    };
    struct OH\_Huks\_Blob inData = {
        (uint32\_t)strlen(DATA\_TO\_SIGN),
        (uint8\_t \*)DATA\_TO\_SIGN
    };
    struct OH\_Huks\_ParamSet \*signParamSet = nullptr;
    struct OH\_Huks\_ParamSet \*verifyParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        ohResult = InitParamSet(&signParamSet, g\_signParamsTest, sizeof(g\_signParamsTest) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        ohResult = InitParamSet(&verifyParamSet, g\_verifyParamsTest,
            sizeof(g\_verifyParamsTest) / sizeof(OH\_Huks\_Param));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        /\* 1. Sign \*/
        // Init
        uint8\_t handleS\[sizeof(uint64\_t)\] = {0};
        struct OH\_Huks\_Blob handleSign = { (uint32\_t)sizeof(uint64\_t), handleS };
        ohResult = OH\_Huks\_InitSession(&keyAlias, signParamSet, &handleSign, nullptr);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        // Finish
        uint8\_t outDataS\[RSA\_COMMON\_SIZE\] = {0};
        struct OH\_Huks\_Blob outDataSign = { RSA\_COMMON\_SIZE, outDataS };
        ohResult = OH\_Huks\_FinishSession(&handleSign, signParamSet,  &inData, &outDataSign);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        
        /\* 2. Verify \*/
        // Init
        uint8\_t handleV\[sizeof(uint64\_t)\] = {0};
        struct OH\_Huks\_Blob handleVerify = { (uint32\_t)sizeof(uint64\_t), handleV };
        ohResult = OH\_Huks\_InitSession(&keyAlias, verifyParamSet, &handleVerify, nullptr);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        // Update loop
        uint8\_t temp\[\] = "out";
        struct OH\_Huks\_Blob verifyOut = { (uint32\_t)sizeof(temp), temp };
        ohResult = OH\_Huks\_UpdateSession(&handleVerify, verifyParamSet, &inData, &verifyOut);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        // Finish
        ohResult = OH\_Huks\_FinishSession(&handleVerify, verifyParamSet, &outDataSign, &verifyOut);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
    } while (0);
    OH\_Huks\_FreeParamSet(&signParamSet);
    OH\_Huks\_FreeParamSet(&verifyParamSet);
    
    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}