• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 // [Start verify_signatures_with_ecdsa_keys_c]
16 #include "signing_signature_verification.h"
17 
DoTestEcdsaSignature()18 bool DoTestEcdsaSignature()
19 {
20     OH_CryptoAsymKeyGenerator *keyCtx = nullptr;
21     OH_CryptoKeyPair *keyPair = nullptr;
22     OH_CryptoVerify *verify = nullptr;
23 
24     uint8_t plainText[] = {0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b,
25                            0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c};
26     Crypto_DataBlob msgBlob = {.data = reinterpret_cast<uint8_t *>(plainText), .len = sizeof(plainText)};
27 
28     uint8_t pubKeyText[] = {0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08,
29                             0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00, 0x03, 0x4d, 0xe4, 0xbb,
30                             0x11, 0x10, 0x1a, 0xd2, 0x05, 0x74, 0xf1, 0x0b, 0xb4, 0x75, 0x57, 0xf4, 0x3e, 0x55, 0x14,
31                             0x17, 0x05, 0x4a, 0xb2, 0xfb, 0x8c, 0x84, 0x64, 0x38, 0x02, 0xa0, 0x2a, 0xa6, 0xf0};
32 
33     Crypto_DataBlob keyBlob = {.data = reinterpret_cast<uint8_t *>(pubKeyText), .len = sizeof(pubKeyText)};
34 
35     uint8_t signText[] = {0x30, 0x44, 0x02, 0x20, 0x21, 0x89, 0x99, 0xb1, 0x56, 0x4e, 0x3a, 0x2c, 0x16, 0x08,
36                           0xb5, 0x8a, 0x06, 0x6f, 0x67, 0x47, 0x1b, 0x04, 0x18, 0x7d, 0x53, 0x2d, 0xba, 0x00,
37                           0x38, 0xd9, 0xe3, 0xe7, 0x8c, 0xcf, 0x76, 0x83, 0x02, 0x20, 0x13, 0x54, 0x84, 0x9d,
38                           0x73, 0x40, 0xc3, 0x92, 0x66, 0xdc, 0x3e, 0xc9, 0xf1, 0x4c, 0x33, 0x84, 0x2a, 0x76,
39                           0xaf, 0xc6, 0x61, 0x84, 0x5c, 0xae, 0x4b, 0x0d, 0x3c, 0xb0, 0xc8, 0x04, 0x89, 0x71};
40 
41     Crypto_DataBlob signBlob = {.data = reinterpret_cast<uint8_t *>(signText), .len = sizeof(signText)};
42 
43     OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
44     // keypair
45     ret = OH_CryptoAsymKeyGenerator_Create((const char *)"ECC256", &keyCtx);
46     if (ret != CRYPTO_SUCCESS) {
47         return false;
48     }
49     ret = OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_DER, &keyBlob, nullptr, &keyPair);
50     if (ret != CRYPTO_SUCCESS) {
51         OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
52         return false;
53     }
54     OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
55     // verify
56     ret = OH_CryptoVerify_Create((const char *)"ECC|SHA256", &verify);
57     if (ret != CRYPTO_SUCCESS) {
58         OH_CryptoVerify_Destroy(verify);
59         OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
60         return false;
61     }
62     ret = OH_CryptoVerify_Init(verify, pubKey);
63     if (ret != CRYPTO_SUCCESS) {
64         OH_CryptoVerify_Destroy(verify);
65         OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
66         return false;
67     }
68     bool res = OH_CryptoVerify_Final(verify, &msgBlob, &signBlob);
69     if (ret != true) {
70         OH_CryptoVerify_Destroy(verify);
71         OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
72         return false;
73     }
74 
75     OH_CryptoVerify_Destroy(verify);
76     OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
77     OH_CryptoKeyPair_Destroy(keyPair);
78     return res;
79 }
80 // [End verify_signatures_with_ecdsa_keys_c]