1# 使用ECDSA密钥对验签(C/C++) 2 3对应的算法规格请查看[签名验签算法规格:ECDSA](crypto-sign-sig-verify-overview.md#ecdsa)。 4 5## 在CMake脚本中链接相关动态库 6```txt 7target_link_libraries(entry PUBLIC libohcrypto.so) 8``` 9 10## 开发步骤 11 121. 调用[OH_CryptoVerify_Create](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_create),指定字符串参数'ECC256|SHA256',创建非对称密钥类型为ECC256、摘要算法为SHA256的Verify实例,用于完成验签操作。 13 142. 调用[OH_CryptoVerify_Init](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_init),使用公钥(OH_CryptoPubKey)初始化Verify实例。 15 163. 调用[OH_CryptoVerify_Update](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_update),传入待验证的数据。当前单次update长度没有限制,开发者可以根据数据量判断如何调用update,如果数据量较小,可以直接调用OH_CryptoVerify_Final接口一次性传入。 17 184. 调用[OH_CryptoVerify_Final](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_final),对数据进行验签。 19 20```c++ 21#include "CryptoArchitectureKit/crypto_common.h" 22#include "CryptoArchitectureKit/crypto_signature.h" 23#include "CryptoArchitectureKit/crypto_asym_key.h" 24 25static bool doTestEcdsaSignature() 26{ 27 OH_CryptoAsymKeyGenerator *keyCtx = nullptr; 28 OH_CryptoKeyPair *keyPair = nullptr; 29 OH_CryptoVerify *verify = nullptr; 30 31 uint8_t plainText[] = { 32 0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b, 0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c 33 }; // 待验证数据,仅供参考。 34 Crypto_DataBlob msgBlob = { 35 .data = reinterpret_cast<uint8_t *>(plainText), 36 .len = sizeof(plainText) 37 }; 38 39 uint8_t pubKeyText[] = { 40 0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 41 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00, 0x03, 0x4d, 0xe4, 0xbb, 0x11, 0x10, 42 0x1a, 0xd2, 0x05, 0x74, 0xf1, 0x0b, 0xb4, 0x75, 0x57, 0xf4, 0x3e, 0x55, 0x14, 0x17, 0x05, 0x4a, 43 0xb2, 0xfb, 0x8c, 0x84, 0x64, 0x38, 0x02, 0xa0, 0x2a, 0xa6, 0xf0 44 }; // DER格式的公钥编码数据,仅供参考。 45 46 Crypto_DataBlob keyBlob = { 47 .data = reinterpret_cast<uint8_t *>(pubKeyText), 48 .len = sizeof(pubKeyText) 49 }; 50 51 uint8_t signText[] = { 52 0x30, 0x44, 0x02, 0x20, 0x21, 0x89, 0x99, 0xb1, 0x56, 0x4e, 0x3a, 0x2c, 0x16, 0x08, 0xb5, 0x8a, 53 0x06, 0x6f, 0x67, 0x47, 0x1b, 0x04, 0x18, 0x7d, 0x53, 0x2d, 0xba, 0x00, 0x38, 0xd9, 0xe3, 0xe7, 54 0x8c, 0xcf, 0x76, 0x83, 0x02, 0x20, 0x13, 0x54, 0x84, 0x9d, 0x73, 0x40, 0xc3, 0x92, 0x66, 0xdc, 55 0x3e, 0xc9, 0xf1, 0x4c, 0x33, 0x84, 0x2a, 0x76, 0xaf, 0xc6, 0x61, 0x84, 0x5c, 0xae, 0x4b, 0x0d, 56 0x3c, 0xb0, 0xc8, 0x04, 0x89, 0x71 57 }; // 签名数据,仅供参考。 58 59 Crypto_DataBlob signBlob = { 60 .data = reinterpret_cast<uint8_t *>(signText), 61 .len = sizeof(signText) 62 }; 63 64 OH_Crypto_ErrCode ret = CRYPTO_SUCCESS; 65 66 ret = OH_CryptoAsymKeyGenerator_Create((const char *)"ECC256", &keyCtx); 67 if (ret != CRYPTO_SUCCESS) { 68 return false; 69 } 70 ret = OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_DER, &keyBlob, nullptr, &keyPair); // 将DER格式的公钥编码数据转换为OH_CryptoKeyPair。 71 if (ret != CRYPTO_SUCCESS) { 72 OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 73 return false; 74 } 75 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); // 获取公钥对象。 76 // verify 77 ret = OH_CryptoVerify_Create((const char *)"ECC256|SHA256", &verify); // 创建Verify实例。 78 if (ret != CRYPTO_SUCCESS) { 79 OH_CryptoVerify_Destroy(verify); 80 OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 81 return false; 82 } 83 ret = OH_CryptoVerify_Init(verify, pubKey); 84 if (ret != CRYPTO_SUCCESS) { 85 OH_CryptoVerify_Destroy(verify); 86 OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 87 return false; 88 } 89 bool res = OH_CryptoVerify_Final(verify, &msgBlob, &signBlob); 90 if (res != true) { 91 OH_CryptoVerify_Destroy(verify); 92 OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 93 return false; 94 } 95 96 OH_CryptoVerify_Destroy(verify); 97 OH_CryptoAsymKeyGenerator_Destroy(keyCtx); 98 OH_CryptoKeyPair_Destroy(keyPair); 99 return res; 100} 101``` 102