• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Key Agreement Using X25519 (C/C++)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10For details about the algorithm specifications, see [X25519](crypto-key-agreement-overview.md#x25519).
11
12## How to Develop
13
141. Call [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_create), [OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_generate), and [OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_convert) to generate an asymmetric key (**keyPair**) of the X25519 type.
15
16   For details about how to generate an X25519 asymmetric key pair, see the following example. To learn more, see [X25519](crypto-asym-key-generation-conversion-spec.md#x25519) and [Randomly Generating an Asymmetric Key Pair](crypto-generate-asym-key-pair-randomly-ndk.md). There may be differences between the input parameters in the reference documents and those in the following example.
17
182. Call [OH_CryptoKeyAgreement_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-key-agreement-h.md#oh_cryptokeyagreement_create) and specify the string parameter **X25519** to create a key protocol generator of the X25519 type.
19
203. Call [OH_CryptoKeyAgreement_GenerateSecret](../../reference/apis-crypto-architecture-kit/capi-crypto-key-agreement-h.md#oh_cryptokeyagreement_generatesecret) to perform key agreement based on the passed private key (**keyPair.priKey**) and public key (**keyPair.pubKey**) and return the shared key.
21
22```C++
23#include "CryptoArchitectureKit/crypto_architecture_kit.h"
24#include "CryptoArchitectureKit/crypto_key_agreement.h"
25#include <stdio.h>
26#include <cstring>
27
28static OH_Crypto_ErrCode doTestX25519KeyAgreement()
29{
30    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,
31                             147, 101, 139, 57, 74, 225, 119, 196, 207, 0, 50, 232, 93, 147, 188, 21, 225, 228, 54, 251,
32                             230, 52};
33    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,
34                             39, 119, 229, 110, 12, 192, 237, 186, 153, 21, 122, 28, 176, 248, 108, 22, 242, 239, 179,
35                             106, 175, 85, 65, 214, 90};
36    // Create an X25519 key generator.
37    OH_CryptoAsymKeyGenerator *x25519Gen = nullptr;
38    OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("X25519", &x25519Gen);
39    if (ret != CRYPTO_SUCCESS) {
40        return ret;
41    }
42
43    // Key pair A passed from an external system.
44    Crypto_DataBlob pubKeyBlob = {pubKeyArray, sizeof(pubKeyArray)};
45    Crypto_DataBlob priKeyBlob = {priKeyArray, sizeof(priKeyArray)};
46    OH_CryptoKeyPair *keyPairA = nullptr;
47    ret = OH_CryptoAsymKeyGenerator_Convert(x25519Gen, CRYPTO_DER, &pubKeyBlob, &priKeyBlob, &keyPairA);
48    if (ret != CRYPTO_SUCCESS) {
49        OH_CryptoAsymKeyGenerator_Destroy(x25519Gen);
50        return ret;
51    }
52
53    // Key pair B generated internally.
54    OH_CryptoKeyPair *keyPairB = nullptr;
55    ret = OH_CryptoAsymKeyGenerator_Generate(x25519Gen, &keyPairB);
56    if (ret != CRYPTO_SUCCESS) {
57        OH_CryptoKeyPair_Destroy(keyPairA);
58        OH_CryptoAsymKeyGenerator_Destroy(x25519Gen);
59        return ret;
60    }
61
62    // Create a key agreement generator.
63    OH_CryptoKeyAgreement *x25519KeyAgreement = nullptr;
64    ret = OH_CryptoKeyAgreement_Create("X25519", &x25519KeyAgreement);
65    if (ret != CRYPTO_SUCCESS) {
66        OH_CryptoKeyPair_Destroy(keyPairA);
67        OH_CryptoKeyPair_Destroy(keyPairB);
68        OH_CryptoAsymKeyGenerator_Destroy(x25519Gen);
69        return ret;
70    }
71
72    // Use the public key of A and the private key of B to perform key agreement.
73    OH_CryptoPrivKey *privKeyB = OH_CryptoKeyPair_GetPrivKey(keyPairB);
74    OH_CryptoPubKey *pubKeyA = OH_CryptoKeyPair_GetPubKey(keyPairA);
75    Crypto_DataBlob secret1 = {0};
76    ret = OH_CryptoKeyAgreement_GenerateSecret(x25519KeyAgreement, privKeyB, pubKeyA, &secret1);
77    if (ret != CRYPTO_SUCCESS) {
78        OH_CryptoKeyAgreement_Destroy(x25519KeyAgreement);
79        OH_CryptoKeyPair_Destroy(keyPairA);
80        OH_CryptoKeyPair_Destroy(keyPairB);
81        OH_CryptoAsymKeyGenerator_Destroy(x25519Gen);
82        return ret;
83    }
84
85    // Use the private key of A and the public key of B to perform key agreement.
86    OH_CryptoPrivKey *privKeyA = OH_CryptoKeyPair_GetPrivKey(keyPairA);
87    OH_CryptoPubKey *pubKeyB = OH_CryptoKeyPair_GetPubKey(keyPairB);
88    Crypto_DataBlob secret2 = {0};
89    ret = OH_CryptoKeyAgreement_GenerateSecret(x25519KeyAgreement, privKeyA, pubKeyB, &secret2);
90    if (ret != CRYPTO_SUCCESS) {
91        OH_Crypto_FreeDataBlob(&secret1);
92        OH_CryptoKeyAgreement_Destroy(x25519KeyAgreement);
93        OH_CryptoKeyPair_Destroy(keyPairA);
94        OH_CryptoKeyPair_Destroy(keyPairB);
95        OH_CryptoAsymKeyGenerator_Destroy(x25519Gen);
96        return ret;
97    }
98
99    // Compare the secrets.
100    if ((secret1.len == secret2.len) && (memcmp(secret1.data, secret2.data, secret1.len) == 0)) {
101        printf("x25519 success\n");
102    } else {
103        printf("x25519 result is not equal\n");
104        ret = CRYPTO_OPERTION_ERROR;
105    }
106
107    // Free resources.
108    OH_Crypto_FreeDataBlob(&secret1);
109    OH_Crypto_FreeDataBlob(&secret2);
110    OH_CryptoKeyAgreement_Destroy(x25519KeyAgreement);
111    OH_CryptoKeyPair_Destroy(keyPairA);
112    OH_CryptoKeyPair_Destroy(keyPairB);
113    OH_CryptoAsymKeyGenerator_Destroy(x25519Gen);
114    return ret;
115}
116```
117