1e41f4b71Sopenharmony_ci# Signature Verification with an SM2 Key Pair (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [SM2](crypto-sign-sig-verify-overview.md#sm2). 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_ci1. Use [OH_CryptoVerify_Create](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_create) with the string parameter **'SM2_256|SM3'** to create a **Verify** instance. The key type is **SM2_256**, and MD algorithm is **SM3**. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_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**). 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_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. 20e41f4b71Sopenharmony_ci 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. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci4. Use [OH_CryptoVerify_Final](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_final) to verify the signature. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**Example** 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci```c++ 27e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h" 28e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_signature.h" 29e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h" 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_cistatic bool doTestSm2Signature() 32e41f4b71Sopenharmony_ci{ 33e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator *keyCtx = nullptr; 34e41f4b71Sopenharmony_ci OH_CryptoKeyPair *keyPair = nullptr; 35e41f4b71Sopenharmony_ci OH_CryptoVerify *verify = nullptr; 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci uint8_t plainText[] = { 38e41f4b71Sopenharmony_ci 0x96, 0x46, 0x2e, 0xde, 0x3f, 0x47, 0xbf, 0xd6, 0x87, 0x48, 0x36, 0x1d, 0x75, 0x35, 0xbd, 0xbc, 39e41f4b71Sopenharmony_ci 0x6b, 0x06, 0xe8, 0xb3, 0x68, 0x91, 0x53, 0xce, 0x76, 0x5d, 0x24, 0xda, 0xdc, 0xc4, 0x9f, 0x94, 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, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 48e41f4b71Sopenharmony_ci 0x81, 0x1c, 0xcf, 0x55, 0x01, 0x82, 0x2d, 0x03, 0x42, 0x00, 0x04, 0x80, 0x5b, 0x78, 0x04, 0xd7, 49e41f4b71Sopenharmony_ci 0xcf, 0xc3, 0x99, 0x63, 0xae, 0x88, 0xcd, 0xfc, 0xd6, 0x18, 0xf4, 0x08, 0xe8, 0xe3, 0x68, 0x47, 50e41f4b71Sopenharmony_ci 0x4f, 0x44, 0x0e, 0xb2, 0xba, 0x3a, 0xb3, 0x10, 0xf1, 0xc9, 0xd0, 0x84, 0xe2, 0xa4, 0x47, 0xbe, 51e41f4b71Sopenharmony_ci 0x72, 0xae, 0xf8, 0x6a, 0xeb, 0x6e, 0x10, 0xab, 0x52, 0x6b, 0x6a, 0x58, 0xc6, 0xb5, 0x78, 0xaa, 52e41f4b71Sopenharmony_ci 0x70, 0xe5, 0x58, 0x20, 0x4e, 0x34, 0x42, 0x77, 0x08, 0x27, 0x11, 53e41f4b71Sopenharmony_ci }; 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci Crypto_DataBlob keyBlob = { 56e41f4b71Sopenharmony_ci .data = reinterpret_cast<uint8_t *>(pubKeyText), 57e41f4b71Sopenharmony_ci .len = sizeof(pubKeyText) 58e41f4b71Sopenharmony_ci }; 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci uint8_t signText[] = { 61e41f4b71Sopenharmony_ci 0x30, 0x45, 0x02, 0x21, 0x00, 0xf4, 0xe7, 0x9d, 0x35, 0x33, 0xa6, 0x86, 0x2e, 0x2a, 0x97, 0x72, 62e41f4b71Sopenharmony_ci 0xc9, 0x46, 0x79, 0x65, 0xca, 0x4a, 0x71, 0x34, 0xca, 0xf7, 0x58, 0xb3, 0x26, 0xa5, 0xdb, 0xfa, 63e41f4b71Sopenharmony_ci 0x8b, 0xbe, 0xbf, 0x5f, 0x90, 0x02, 0x20, 0x53, 0xb4, 0x23, 0xb1, 0xe2, 0x8f, 0x2f, 0xe9, 0xc8, 64e41f4b71Sopenharmony_ci 0x22, 0xef, 0xab, 0x9b, 0x13, 0x08, 0x75, 0x8e, 0xb1, 0x9c, 0x59, 0xe5, 0xd6, 0x64, 0x35, 0xf5, 65e41f4b71Sopenharmony_ci 0xd1, 0xde, 0xfa, 0xfe, 0x80, 0x37, 0x1a, 66e41f4b71Sopenharmony_ci }; 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci Crypto_DataBlob signBlob = { 69e41f4b71Sopenharmony_ci .data = reinterpret_cast<uint8_t *>(signText), 70e41f4b71Sopenharmony_ci .len = sizeof(signText) 71e41f4b71Sopenharmony_ci }; 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci // keypair 74e41f4b71Sopenharmony_ci OH_Crypto_ErrCode ret = CRYPTO_SUCCESS; 75e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Create((const char *)"SM2_256", &keyCtx); 76e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 77e41f4b71Sopenharmony_ci return false; 78e41f4b71Sopenharmony_ci } 79e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_DER, &keyBlob, nullptr, &keyPair); 80e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 81e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 82e41f4b71Sopenharmony_ci return false; 83e41f4b71Sopenharmony_ci } 84e41f4b71Sopenharmony_ci OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); 85e41f4b71Sopenharmony_ci // verify 86e41f4b71Sopenharmony_ci ret = OH_CryptoVerify_Create((const char *)"SM2_256|SM3", &verify); 87e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 88e41f4b71Sopenharmony_ci OH_CryptoVerify_Destroy(verify); 89e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 90e41f4b71Sopenharmony_ci return false; 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci ret = OH_CryptoVerify_Init(verify, pubKey); 93e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 94e41f4b71Sopenharmony_ci OH_CryptoVerify_Destroy(verify); 95e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 96e41f4b71Sopenharmony_ci return false; 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci bool res = OH_CryptoVerify_Final(verify, &msgBlob, &signBlob); 99e41f4b71Sopenharmony_ci if (ret != true) { 100e41f4b71Sopenharmony_ci OH_CryptoVerify_Destroy(verify); 101e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 102e41f4b71Sopenharmony_ci return false; 103e41f4b71Sopenharmony_ci } 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci OH_CryptoVerify_Destroy(verify); 106e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 107e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(keyPair); 108e41f4b71Sopenharmony_ci return res; 109e41f4b71Sopenharmony_ci} 110e41f4b71Sopenharmony_ci``` 111