1# Key Agreement Using ECDH (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 [ECDH](crypto-key-agreement-overview.md#ecdh). 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 a 256-bit asymmetric key (**keyPair**) of the ECC type. 15 16 For details about how to generate an ECC asymmetric key pair, see the following example. To learn more, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc) 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 **ECC256** to create a key protocol generator of the ECC type with the key length of 256 bits. 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 doTestEcdhKeyAgreement() 29{ 30 // The public and private key pair data is transferred from an external system. 31 uint8_t pubKeyArray[] = {48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 32 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 33 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 34 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 35 171, 34, 145, 124, 174, 57, 92}; 36 uint8_t priKeyArray[] = {48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 37 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 38 8, 42, 134, 72, 206, 61, 3, 1, 7}; 39 40 // Create an ECC key generator. 41 OH_CryptoAsymKeyGenerator *eccGen = nullptr; 42 OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("ECC256", &eccGen); 43 if (ret != CRYPTO_SUCCESS) { 44 return ret; 45 } 46 47 // Key pair A passed from an external system. 48 Crypto_DataBlob pubKeyBlob = {pubKeyArray, sizeof(pubKeyArray)}; 49 Crypto_DataBlob priKeyBlob = {priKeyArray, sizeof(priKeyArray)}; 50 OH_CryptoKeyPair *keyPairA = nullptr; 51 ret = OH_CryptoAsymKeyGenerator_Convert(eccGen, CRYPTO_DER, &pubKeyBlob, &priKeyBlob, &keyPairA); 52 if (ret != CRYPTO_SUCCESS) { 53 OH_CryptoAsymKeyGenerator_Destroy(eccGen); 54 return ret; 55 } 56 57 // Key pair B generated internally. 58 OH_CryptoKeyPair *keyPairB = nullptr; 59 ret = OH_CryptoAsymKeyGenerator_Generate(eccGen, &keyPairB); 60 if (ret != CRYPTO_SUCCESS) { 61 OH_CryptoKeyPair_Destroy(keyPairA); 62 OH_CryptoAsymKeyGenerator_Destroy(eccGen); 63 return ret; 64 } 65 66 // Create a key agreement generator. 67 OH_CryptoKeyAgreement *eccKeyAgreement = nullptr; 68 ret = OH_CryptoKeyAgreement_Create("ECC256", &eccKeyAgreement); 69 if (ret != CRYPTO_SUCCESS) { 70 OH_CryptoKeyPair_Destroy(keyPairA); 71 OH_CryptoKeyPair_Destroy(keyPairB); 72 OH_CryptoAsymKeyGenerator_Destroy(eccGen); 73 return ret; 74 } 75 76 // Use the public key of A and the private key of B to perform key agreement. 77 OH_CryptoPrivKey *privKeyB = OH_CryptoKeyPair_GetPrivKey(keyPairB); 78 OH_CryptoPubKey *pubKeyA = OH_CryptoKeyPair_GetPubKey(keyPairA); 79 Crypto_DataBlob secret1 = { 0 }; 80 ret = OH_CryptoKeyAgreement_GenerateSecret(eccKeyAgreement, privKeyB, pubKeyA, &secret1); 81 if (ret != CRYPTO_SUCCESS) { 82 OH_CryptoKeyAgreement_Destroy(eccKeyAgreement); 83 OH_CryptoKeyPair_Destroy(keyPairA); 84 OH_CryptoKeyPair_Destroy(keyPairB); 85 OH_CryptoAsymKeyGenerator_Destroy(eccGen); 86 return ret; 87 } 88 89 // Use the private key of A and the public key of B to perform key agreement. 90 OH_CryptoPrivKey *privKeyA = OH_CryptoKeyPair_GetPrivKey(keyPairA); 91 OH_CryptoPubKey *pubKeyB = OH_CryptoKeyPair_GetPubKey(keyPairB); 92 Crypto_DataBlob secret2 = { 0 }; 93 ret = OH_CryptoKeyAgreement_GenerateSecret(eccKeyAgreement, privKeyA, pubKeyB, &secret2); 94 if (ret != CRYPTO_SUCCESS) { 95 OH_Crypto_FreeDataBlob(&secret1); 96 OH_CryptoKeyAgreement_Destroy(eccKeyAgreement); 97 OH_CryptoKeyPair_Destroy(keyPairA); 98 OH_CryptoKeyPair_Destroy(keyPairB); 99 OH_CryptoAsymKeyGenerator_Destroy(eccGen); 100 return ret; 101 } 102 103 // Compare the secrets. 104 if ((secret1.len == secret2.len) && 105 (memcmp(secret1.data, secret2.data, secret1.len) == 0)) { 106 printf("ecdh success\n"); 107 } else { 108 printf("ecdh result is not equal\n"); 109 ret = CRYPTO_OPERTION_ERROR; 110 } 111 112 // Free resources. 113 OH_Crypto_FreeDataBlob(&secret1); 114 OH_Crypto_FreeDataBlob(&secret2); 115 OH_CryptoKeyAgreement_Destroy(eccKeyAgreement); 116 OH_CryptoKeyPair_Destroy(keyPairA); 117 OH_CryptoKeyPair_Destroy(keyPairB); 118 OH_CryptoAsymKeyGenerator_Destroy(eccGen); 119 return ret; 120} 121``` 122