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_keypair]
17 #include "CryptoArchitectureKit/crypto_common.h"
18 #include "CryptoArchitectureKit/crypto_asym_key.h"
19 #include "file.h"
20
doAsymEccCovert()21 OH_Crypto_ErrCode doAsymEccCovert()
22 {
23 OH_CryptoAsymKeyGenerator *ctx = nullptr;
24 OH_Crypto_ErrCode ret;
25
26 ret = OH_CryptoAsymKeyGenerator_Create("ECC256", &ctx);
27 if (ret != CRYPTO_SUCCESS) {
28 return ret;
29 }
30
31 uint8_t ecc224PubKeyBlobData[] = {48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42,
32 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 157, 58, 248, 205, 95,
33 171, 229, 33, 116, 44, 192, 12, 115, 119, 84, 156, 128, 56, 180, 246, 84,
34 43, 33, 244, 224, 221, 181, 154, 155, 222, 157, 124, 131, 217, 214, 134, 199,
35 155, 61, 196, 203, 107, 13, 227, 121, 57, 199, 109, 220, 103, 55, 78, 148,
36 185, 226, 212, 162, 31, 66, 201, 50, 129, 1, 156};
37
38 uint8_t ecc224PriKeyBlobData[] = {48, 49, 2, 1, 1, 4, 32, 255, 121, 33, 196, 188, 159, 112, 149, 146, 107,
39 243, 78, 152, 214, 12, 119, 87, 199, 207, 57, 116, 64, 150, 240, 121, 22, 88,
40 138, 196, 71, 70, 222, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7};
41 Crypto_DataBlob pubBlob = {.data = ecc224PubKeyBlobData, .len = sizeof(ecc224PubKeyBlobData)};
42 Crypto_DataBlob priBlob = {.data = ecc224PriKeyBlobData, .len = sizeof(ecc224PriKeyBlobData)};
43
44 OH_CryptoKeyPair *dupKeyPair = nullptr;
45 ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &pubBlob, &priBlob, &dupKeyPair);
46 if (ret != CRYPTO_SUCCESS) {
47 OH_CryptoAsymKeyGenerator_Destroy(ctx);
48 return ret;
49 }
50
51 OH_CryptoAsymKeyGenerator_Destroy(ctx);
52 OH_CryptoKeyPair_Destroy(dupKeyPair);
53 return ret;
54 }
55 // [End convert_ecc_keypair]
56