使用X25519进行密钥协商(C/C++)
=====================

对应的算法规格请查看[密钥协商算法规格：X25519](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-key-agreement-overview#x25519)。

#### 开发步骤

1.  调用[OH\_CryptoAsymKeyGenerator\_Create](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-crypto-asym-key-h#oh_cryptoasymkeygenerator_create)、[OH\_CryptoAsymKeyGenerator\_Generate](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-crypto-asym-key-h#oh_cryptoasymkeygenerator_generate)、[OH\_CryptoAsymKeyGenerator\_Convert](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-crypto-asym-key-h#oh_cryptoasymkeygenerator_convert)生成密钥算法为X25519的非对称密钥（keyPair）。
    
    如何生成X25519非对称密钥，开发者可参考下文示例，并结合[非对称密钥生成和转换规格：X25519](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-asym-key-generation-conversion-spec#x25519)和[随机生成非对称密钥对](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-generate-asym-key-pair-randomly-ndk)理解。参考文档与当前示例可能存在入参差异，请在阅读时注意区分。
    
2.  调用[OH\_CryptoKeyAgreement\_Create](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-crypto-key-agreement-h#oh_cryptokeyagreement_create)，指定字符串参数'X25519'，创建密钥算法为X25519的密钥协议生成器。
    
3.  调用[OH\_CryptoKeyAgreement\_GenerateSecret](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-crypto-key-agreement-h#oh_cryptokeyagreement_generatesecret)，基于传入的私钥（keyPair.priKey）与公钥（keyPair.pubKey）进行密钥协商，返回共享密钥。
    

#include "CryptoArchitectureKit/crypto\_architecture\_kit.h"
#include "CryptoArchitectureKit/crypto\_key\_agreement.h"
#include "file.h"
#include <cstdio>
#include <cstring>


static OH\_Crypto\_ErrCode GenerateSecret(OH\_CryptoKeyAgreement \*x25519KeyAgreement, OH\_CryptoKeyPair \*keyPairA,
    OH\_CryptoKeyPair \*keyPairB, Crypto\_DataBlob \*secret)
{
    OH\_CryptoPrivKey \*privKey = OH\_CryptoKeyPair\_GetPrivKey(keyPairA);
    OH\_CryptoPubKey \*pubKey = OH\_CryptoKeyPair\_GetPubKey(keyPairB);
    return OH\_CryptoKeyAgreement\_GenerateSecret(x25519KeyAgreement, privKey, pubKey, secret);
}

static OH\_Crypto\_ErrCode CompareSecrets(Crypto\_DataBlob secret1, Crypto\_DataBlob secret2)
{
    if ((secret1.len == secret2.len) &&
        (memcmp(secret1.data, secret2.data, secret1.len) == 0)) {
        return CRYPTO\_SUCCESS;
    }
    return CRYPTO\_OPERTION\_ERROR;
}

static OH\_Crypto\_ErrCode ConvertKeyPairByBlob(OH\_CryptoAsymKeyGenerator \*x25519Gen, OH\_CryptoKeyPair \*\*keyPair)
{
    uint8\_t pubKeyArray\[\] = {48, 42, 48, 5, 6, 3, 43, 101, 110, 3, 33, 0, 36, 98, 216, 106, 74, 99, 179, 203, 81, 145,
                             147, 101, 139, 57, 74, 225, 119, 196, 207, 0, 50, 232, 93, 147, 188, 21, 225, 228, 54, 251,
                             230, 52};
    uint8\_t priKeyArray\[\] = {48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 110, 4, 34, 4, 32, 112, 65, 156, 73, 65, 89, 183,
                             39, 119, 229, 110, 12, 192, 237, 186, 153, 21, 122, 28, 176, 248, 108, 22, 242, 239, 179,
                             106, 175, 85, 65, 214, 90};
    Crypto\_DataBlob pubKeyBlob = {pubKeyArray, sizeof(pubKeyArray)};
    Crypto\_DataBlob priKeyBlob = {priKeyArray, sizeof(priKeyArray)};
    return OH\_CryptoAsymKeyGenerator\_Convert(x25519Gen, CRYPTO\_DER, &pubKeyBlob, &priKeyBlob, keyPair);
}

OH\_Crypto\_ErrCode doTestX25519KeyAgreement()
{
    OH\_CryptoAsymKeyGenerator \*x25519Gen = nullptr;
    OH\_CryptoKeyPair \*keyPairA = nullptr;
    OH\_CryptoKeyPair \*keyPairB = nullptr;
    OH\_CryptoKeyAgreement \*x25519KeyAgreement = nullptr;
    Crypto\_DataBlob secret1 = {0};
    Crypto\_DataBlob secret2 = {0};
    OH\_Crypto\_ErrCode ret = OH\_CryptoAsymKeyGenerator\_Create("X25519", &x25519Gen);
    if (ret != CRYPTO\_SUCCESS) {
        return ret;
    }

    ret = ConvertKeyPairByBlob(x25519Gen, &keyPairA);
    if (ret != CRYPTO\_SUCCESS) {
        goto goto\_cleanup;
    }

    ret = OH\_CryptoAsymKeyGenerator\_Generate(x25519Gen, &keyPairB);
    if (ret != CRYPTO\_SUCCESS) {
        goto goto\_cleanup;
    }

    ret = OH\_CryptoKeyAgreement\_Create("X25519", &x25519KeyAgreement);
    if (ret != CRYPTO\_SUCCESS) {
        goto goto\_cleanup;
    }

    // 使用A的公钥和B的私钥进行密钥协商。
    ret = GenerateSecret(x25519KeyAgreement, keyPairB, keyPairA, &secret1);
    if (ret != CRYPTO\_SUCCESS) {
        goto goto\_cleanup;
    }

    // 使用A的私钥和B的公钥进行密钥协商。
    ret = GenerateSecret(x25519KeyAgreement, keyPairA, keyPairB, &secret2);
    if (ret != CRYPTO\_SUCCESS) {
        goto goto\_cleanup;
    }

    ret = CompareSecrets(secret1, secret2);
    if (ret != CRYPTO\_SUCCESS) {
        printf("x25519 result is not equal\\n");
        goto goto\_cleanup;
    }

goto\_cleanup:
    OH\_Crypto\_FreeDataBlob(&secret1);
    OH\_Crypto\_FreeDataBlob(&secret2);
    OH\_CryptoKeyAgreement\_Destroy(x25519KeyAgreement);
    OH\_CryptoKeyPair\_Destroy(keyPairA);
    OH\_CryptoKeyPair\_Destroy(keyPairB);
    OH\_CryptoAsymKeyGenerator\_Destroy(x25519Gen);
    return ret;
}