• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-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 #include "hks_report_three_stage.h"
17 
18 #include <cstdint>
19 #include <string>
20 #include <unordered_map>
21 
22 #include "hks_event_info.h"
23 #include "hks_report_common.h"
24 #include "hks_report_three_stage_build.h"
25 #include "hks_template.h"
26 #include "hks_type.h"
27 #include "hks_type_enum.h"
28 
NeedReportCommon(const HksEventInfo * eventInfo)29 static bool NeedReportCommon(const HksEventInfo *eventInfo)
30 {
31     HKS_IF_NULL_LOGI_RETURN(eventInfo, false, "paramset or eventInfo is null")
32     return eventInfo->common.result.code != HKS_SUCCESS;
33 }
34 
CryptoInfoIsEqual(const HksEventInfo * info1,const HksEventInfo * info2)35 static bool CryptoInfoIsEqual(const HksEventInfo *info1, const HksEventInfo *info2)
36 {
37     HKS_IF_NOT_TRUE_RETURN(CheckEventCommon(info1, info2), false);
38     return (info1->cryptoInfo.keyInfo.alg == info2->cryptoInfo.keyInfo.alg) &&
39         (info1->common.operation == info2->common.operation);
40 }
41 
42 // crypto
HksParamSetToEventInfoCrypto(const struct HksParamSet * paramSet,HksEventInfo * eventInfo)43 int32_t HksParamSetToEventInfoCrypto(const struct HksParamSet *paramSet, HksEventInfo *eventInfo)
44 {
45     return BuildCommonInfo(paramSet, eventInfo);
46 }
47 
HksEventInfoNeedReportCrypto(const HksEventInfo * eventInfo)48 bool HksEventInfoNeedReportCrypto(const HksEventInfo *eventInfo)
49 {
50     return NeedReportCommon(eventInfo);
51 }
52 
HksEventInfoIsEqualCrypto(const HksEventInfo * info1,const HksEventInfo * info2)53 bool HksEventInfoIsEqualCrypto(const HksEventInfo *info1, const HksEventInfo *info2)
54 {
55     return CryptoInfoIsEqual(info1, info2);
56 }
57 
HksEventInfoAddCrypto(HksEventInfo * info1,const HksEventInfo * info2)58 void HksEventInfoAddCrypto(HksEventInfo *info1, const HksEventInfo *info2)
59 {
60     AddEventInfoCommon(info1, info2);
61 }
62 
HksEventInfoToMapCrypto(const HksEventInfo * info,std::unordered_map<std::string,std::string> & map)63 int32_t HksEventInfoToMapCrypto(const HksEventInfo *info, std::unordered_map<std::string, std::string>& map)
64 {
65     HKS_IF_NULL_LOGI_RETURN(info, HKS_ERROR_NULL_POINTER, "eventinfo is null")
66     KeyInfoToMap(&(info->cryptoInfo.keyInfo), map);
67     KeyAccessInfoToMap(&(info->cryptoInfo.accessCtlInfo), map);
68     CryptoInfoToMap(&(info->cryptoInfo), map);
69     return HKS_SUCCESS;
70 }
71 
72 // agree derive
HksParamSetToEventInfoAgreeDerive(const struct HksParamSet * paramSet,HksEventInfo * eventInfo)73 int32_t HksParamSetToEventInfoAgreeDerive(const struct HksParamSet *paramSet, HksEventInfo *eventInfo)
74 {
75     return BuildCommonInfo(paramSet, eventInfo);
76 }
77 
HksEventInfoNeedReportAgreeDerive(const HksEventInfo * eventInfo)78 bool HksEventInfoNeedReportAgreeDerive(const HksEventInfo *eventInfo)
79 {
80     return NeedReportCommon(eventInfo);
81 }
82 
HksEventInfoIsEqualAgreeDerive(const HksEventInfo * info1,const HksEventInfo * info2)83 bool HksEventInfoIsEqualAgreeDerive(const HksEventInfo *info1, const HksEventInfo *info2)
84 {
85     return CheckEventCommonAndKey(info1, info2);
86 }
87 
HksEventInfoAddAgreeDerive(HksEventInfo * info1,const HksEventInfo * info2)88 void HksEventInfoAddAgreeDerive(HksEventInfo *info1, const HksEventInfo *info2)
89 {
90     AddEventInfoCommon(info1, info2);
91 }
92 
HksEventInfoToMapAgreeDerive(const HksEventInfo * info,std::unordered_map<std::string,std::string> & map)93 int32_t HksEventInfoToMapAgreeDerive(const HksEventInfo *info, std::unordered_map<std::string, std::string>& map)
94 {
95     HKS_IF_NULL_LOGI_RETURN(info, HKS_ERROR_NULL_POINTER, "eventinfo is null")
96     KeyInfoToMap(&(info->agreeDeriveInfo.keyInfo), map);
97     KeyAccessInfoToMap(&(info->agreeDeriveInfo.accessCtlInfo), map);
98     AgreeDeriveInfoToMap(&(info->agreeDeriveInfo), map);
99     return HKS_SUCCESS;
100 }
101 
102 // MAC
HksParamSetToEventInfoMac(const struct HksParamSet * paramSet,HksEventInfo * eventInfo)103 int32_t HksParamSetToEventInfoMac(const struct HksParamSet *paramSet, HksEventInfo *eventInfo)
104 {
105     return BuildCommonInfo(paramSet, eventInfo);
106 }
107 
HksEventInfoNeedReportMac(const HksEventInfo * eventInfo)108 bool HksEventInfoNeedReportMac(const HksEventInfo *eventInfo)
109 {
110     return NeedReportCommon(eventInfo);
111 }
112 
HksEventInfoIsEqualMac(const HksEventInfo * info1,const HksEventInfo * info2)113 bool HksEventInfoIsEqualMac(const HksEventInfo *info1, const HksEventInfo *info2)
114 {
115     return CryptoInfoIsEqual(info1, info2);
116 }
117 
HksEventInfoAddMac(HksEventInfo * info1,const HksEventInfo * info2)118 void HksEventInfoAddMac(HksEventInfo *info1, const HksEventInfo *info2)
119 {
120     AddEventInfoCommon(info1, info2);
121 }
122 
HksEventInfoToMapMac(const HksEventInfo * info,std::unordered_map<std::string,std::string> & map)123 int32_t HksEventInfoToMapMac(const HksEventInfo *info, std::unordered_map<std::string, std::string>& map)
124 {
125     HKS_IF_NULL_LOGI_RETURN(info, HKS_ERROR_NULL_POINTER, "eventinfo is null")
126     KeyInfoToMap(&(info->macInfo.keyInfo), map);
127     KeyAccessInfoToMap(&(info->macInfo.accessCtlInfo), map);
128     return HKS_SUCCESS;
129 }
130 
131 // attest
HksParamSetToEventInfoAttest(const struct HksParamSet * paramSet,HksEventInfo * eventInfo)132 int32_t HksParamSetToEventInfoAttest(const struct HksParamSet *paramSet, HksEventInfo *eventInfo)
133 {
134     return BuildCommonInfo(paramSet, eventInfo);
135 }
136 
HksEventInfoNeedReportAttest(const HksEventInfo * eventInfo)137 bool HksEventInfoNeedReportAttest(const HksEventInfo *eventInfo)
138 {
139     return NeedReportCommon(eventInfo);
140 }
141 
HksEventInfoIsEqualAttest(const HksEventInfo * info1,const HksEventInfo * info2)142 bool HksEventInfoIsEqualAttest(const HksEventInfo *info1, const HksEventInfo *info2)
143 {
144     HKS_IF_NOT_TRUE_RETURN(CheckEventCommon(info1, info2), false);
145     return (info1->attestInfo.keyInfo.alg == info2->attestInfo.keyInfo.alg) &&
146         (info1->attestInfo.isAnonymous == info2->attestInfo.isAnonymous);
147 }
148 
HksEventInfoAddAttest(HksEventInfo * info1,const HksEventInfo * info2)149 void HksEventInfoAddAttest(HksEventInfo *info1, const HksEventInfo *info2)
150 {
151     AddEventInfoCommon(info1, info2);
152 }
153 
HksEventInfoToMapAttest(const HksEventInfo * info,std::unordered_map<std::string,std::string> & map)154 int32_t HksEventInfoToMapAttest(const HksEventInfo *info, std::unordered_map<std::string, std::string>& map)
155 {
156     HKS_IF_NULL_LOGI_RETURN(info, HKS_ERROR_NULL_POINTER, "eventinfo is null")
157     KeyInfoToMap(&(info->attestInfo.keyInfo), map);
158     AttestInfoToMap(&(info->attestInfo), map);
159     return HKS_SUCCESS;
160 }
161