• 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 
16 // [Start convert_ecc_uncompressed_pub_keypair]
17 #include "CryptoArchitectureKit/crypto_common.h"
18 #include "CryptoArchitectureKit/crypto_asym_key.h"
19 
doTestEccDataCovert()20 OH_Crypto_ErrCode doTestEccDataCovert()
21 {
22     OH_CryptoAsymKeyGenerator *generator = nullptr;
23     OH_CryptoKeyPair *keyPair = nullptr;
24     Crypto_DataBlob returnBlob = { .data = nullptr, .len = 0 };
25     OH_Crypto_ErrCode ret = CRYPTO_INVALID_PARAMS;
26 
27     ret = OH_CryptoAsymKeyGenerator_Create("ECC_BrainPoolP256r1", &generator);
28     if (ret != CRYPTO_SUCCESS) {
29         return ret;
30     }
31     uint8_t pubKeyBlobData[] = {
32         48, 90, 48, 20, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 9, 43, 36, 3, 3, 2,
33         8, 1, 1, 7, 3, 66, 0, 4, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178,
34         121, 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 157, 111, 40, 217, 215,
35         148, 120, 224, 205, 82, 83, 92, 185, 21, 211, 184, 5, 19, 114, 33, 86, 85,
36         228, 123, 242, 206, 200, 98, 178, 184, 130, 35, 232, 45, 5, 202, 189, 11,
37         46, 163, 156, 152
38     };
39     Crypto_DataBlob pubKeyUncompressedBlob = {
40         .data = pubKeyBlobData,
41         .len = sizeof(pubKeyBlobData),
42     };
43     ret = OH_CryptoAsymKeyGenerator_Convert(generator, CRYPTO_DER, &pubKeyUncompressedBlob, nullptr, &keyPair);
44     if (ret != CRYPTO_SUCCESS) {
45         OH_CryptoAsymKeyGenerator_Destroy(generator);
46         return ret;
47     }
48 
49     OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
50     ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, "X509|COMPRESSED", &returnBlob);
51     if (ret != CRYPTO_SUCCESS) {
52         OH_CryptoAsymKeyGenerator_Destroy(generator);
53         OH_CryptoKeyPair_Destroy(keyPair);
54         return ret;
55     }
56     OH_CryptoAsymKeyGenerator_Destroy(generator);
57     OH_CryptoKeyPair_Destroy(keyPair);
58     return ret;
59 }
60 // [End convert_ecc_uncompressed_pub_keypair]
61