1 /*
2 * Copyright (c) 2024 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
17 #ifdef HKS_CONFIG_FILE
18 #include HKS_CONFIG_FILE
19 #else
20 #include "hks_config.h"
21 #endif
22
23 #include "hks_core_get_main_key.h"
24 #include "hks_ability.h"
25 #include "securec.h"
26
27 #ifndef _HARDWARE_ROOT_KEY_
28 #include "hks_rkc.h"
29
30 #ifdef L2_STANDARD
L2CoreGetMainKey(struct HksBlob * mainKey)31 static int32_t L2CoreGetMainKey(struct HksBlob *mainKey)
32 {
33 #ifdef HKS_USE_RKC_IN_STANDARD
34 return HksRkcGetMainKey(mainKey);
35 #else
36 (void)mainKey;
37 return HKS_ERROR_NOT_SUPPORTED;
38 #endif
39 }
40 #endif
41 #endif
42
HksCoreGetMainKey(const struct HksBlob * message,struct HksBlob * mainKey)43 int32_t HksCoreGetMainKey(const struct HksBlob *message, struct HksBlob *mainKey)
44 {
45 (void)message;
46
47 #ifndef _HARDWARE_ROOT_KEY_
48
49 #ifdef L2_STANDARD
50 return L2CoreGetMainKey(mainKey);
51 #else
52 return HksRkcGetMainKey(mainKey);
53 #endif
54
55 #else
56 /*
57 * Currently, root key is implemented using stubs.
58 * Product adaptation needs to be performed based on hardware capabilities.
59 */
60 uint8_t stubBuf[] = {
61 0x0c, 0xb4, 0x29, 0x39, 0xb7, 0x46, 0xa6, 0x4b,
62 0xdd, 0xf3, 0x75, 0x4c, 0xe0, 0x73, 0x91, 0x51,
63 0xc4, 0x88, 0xbe, 0xa4, 0xe1, 0x87, 0xb5, 0x42,
64 0x06, 0x27, 0x08, 0x21, 0xe2, 0x8f, 0x9b, 0xc1,
65 };
66
67 if (memcpy_s(mainKey->data, mainKey->size, stubBuf, sizeof(stubBuf)) != EOK) {
68 return HKS_ERROR_INSUFFICIENT_MEMORY;
69 }
70 return HKS_SUCCESS;
71 #endif
72 }
73
RegisterAbilityGetMainKey(void)74 void RegisterAbilityGetMainKey(void)
75 {
76 (void)RegisterAbility(HKS_CRYPTO_ABILITY_GET_MAIN_KEY, (void *)HksCoreGetMainKey);
77 }