1e41f4b71Sopenharmony_ci# Signing and Signature Verification with an ECDSA Key Pair (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [ECDSA](crypto-sign-sig-verify-overview.md#ecdsa). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Adding the Dynamic Library in the CMake Script 8e41f4b71Sopenharmony_ci```txt 9e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libohcrypto.so) 10e41f4b71Sopenharmony_ci``` 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## How to Develop 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci1. Use [OH_CryptoVerify_Create](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_create) with the string parameter **'ECC256|SHA256'** to create a **Verify** instance. The key type is **ECC256**, and MD algorithm is **SHA256**. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci2. Use [OH_CryptoVerify_Init](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_init) to initialize the **Verify** instance by using the public key (**OH_CryptoPubKey**). 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci3. Use [OH_CryptoVerify_Update](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_update) to pass in the data to be verified. 21e41f4b71Sopenharmony_ci <br>Currently, the amount of data to be passed in by a single **OH_CryptoVerify_Update()** is not limited. You can determine how to pass in data based on the data volume. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci4. Use [OH_CryptoVerify_Final](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_final) to verify the signature. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**Example** 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci```c++ 28e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h" 29e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_signature.h" 30e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h" 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_cistatic bool doTestEcdsaSignature() 33e41f4b71Sopenharmony_ci{ 34e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator *keyCtx = nullptr; 35e41f4b71Sopenharmony_ci OH_CryptoKeyPair *keyPair = nullptr; 36e41f4b71Sopenharmony_ci OH_CryptoVerify *verify = nullptr; 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci uint8_t plainText[] = { 39e41f4b71Sopenharmony_ci 0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b, 0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c 40e41f4b71Sopenharmony_ci }; 41e41f4b71Sopenharmony_ci Crypto_DataBlob msgBlob = { 42e41f4b71Sopenharmony_ci .data = reinterpret_cast<uint8_t *>(plainText), 43e41f4b71Sopenharmony_ci .len = sizeof(plainText) 44e41f4b71Sopenharmony_ci }; 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci uint8_t pubKeyText[] = { 47e41f4b71Sopenharmony_ci 0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 48e41f4b71Sopenharmony_ci 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00, 0x03, 0x4d, 0xe4, 0xbb, 0x11, 0x10, 49e41f4b71Sopenharmony_ci 0x1a, 0xd2, 0x05, 0x74, 0xf1, 0x0b, 0xb4, 0x75, 0x57, 0xf4, 0x3e, 0x55, 0x14, 0x17, 0x05, 0x4a, 50e41f4b71Sopenharmony_ci 0xb2, 0xfb, 0x8c, 0x84, 0x64, 0x38, 0x02, 0xa0, 0x2a, 0xa6, 0xf0 51e41f4b71Sopenharmony_ci }; 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci Crypto_DataBlob keyBlob = { 54e41f4b71Sopenharmony_ci .data = reinterpret_cast<uint8_t *>(pubKeyText), 55e41f4b71Sopenharmony_ci .len = sizeof(pubKeyText) 56e41f4b71Sopenharmony_ci }; 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci uint8_t signText[] = { 59e41f4b71Sopenharmony_ci 0x30, 0x44, 0x02, 0x20, 0x21, 0x89, 0x99, 0xb1, 0x56, 0x4e, 0x3a, 0x2c, 0x16, 0x08, 0xb5, 0x8a, 60e41f4b71Sopenharmony_ci 0x06, 0x6f, 0x67, 0x47, 0x1b, 0x04, 0x18, 0x7d, 0x53, 0x2d, 0xba, 0x00, 0x38, 0xd9, 0xe3, 0xe7, 61e41f4b71Sopenharmony_ci 0x8c, 0xcf, 0x76, 0x83, 0x02, 0x20, 0x13, 0x54, 0x84, 0x9d, 0x73, 0x40, 0xc3, 0x92, 0x66, 0xdc, 62e41f4b71Sopenharmony_ci 0x3e, 0xc9, 0xf1, 0x4c, 0x33, 0x84, 0x2a, 0x76, 0xaf, 0xc6, 0x61, 0x84, 0x5c, 0xae, 0x4b, 0x0d, 63e41f4b71Sopenharmony_ci 0x3c, 0xb0, 0xc8, 0x04, 0x89, 0x71 64e41f4b71Sopenharmony_ci }; 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci Crypto_DataBlob signBlob = { 67e41f4b71Sopenharmony_ci .data = reinterpret_cast<uint8_t *>(signText), 68e41f4b71Sopenharmony_ci .len = sizeof(signText) 69e41f4b71Sopenharmony_ci }; 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci OH_Crypto_ErrCode ret = CRYPTO_SUCCESS; 72e41f4b71Sopenharmony_ci // keypair 73e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Create((const char *)"ECC256", &keyCtx); 74e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 75e41f4b71Sopenharmony_ci return false; 76e41f4b71Sopenharmony_ci } 77e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_DER, &keyBlob, nullptr, &keyPair); 78e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 79e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 80e41f4b71Sopenharmony_ci return false; 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); 83e41f4b71Sopenharmony_ci // verify 84e41f4b71Sopenharmony_ci ret = OH_CryptoVerify_Create((const char *)"ECC|SHA256", &verify); 85e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 86e41f4b71Sopenharmony_ci OH_CryptoVerify_Destroy(verify); 87e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 88e41f4b71Sopenharmony_ci return false; 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci ret = OH_CryptoVerify_Init(verify, pubKey); 91e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 92e41f4b71Sopenharmony_ci OH_CryptoVerify_Destroy(verify); 93e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 94e41f4b71Sopenharmony_ci return false; 95e41f4b71Sopenharmony_ci } 96e41f4b71Sopenharmony_ci bool res = OH_CryptoVerify_Final(verify, &msgBlob, &signBlob); 97e41f4b71Sopenharmony_ci if (ret != true) { 98e41f4b71Sopenharmony_ci OH_CryptoVerify_Destroy(verify); 99e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 100e41f4b71Sopenharmony_ci return false; 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci OH_CryptoVerify_Destroy(verify); 104e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 105e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(keyPair); 106e41f4b71Sopenharmony_ci return res; 107e41f4b71Sopenharmony_ci} 108e41f4b71Sopenharmony_ci``` 109