1 /*
2  * Copyright (C) 2022 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 "account_version_util.h"
17 #include "common_defs.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "iso_auth_task_common.h"
21 #include "pake_defs.h"
22 #include "pake_protocol_dl_common.h"
23 #include "pake_protocol_ec_common.h"
24 #include "pake_v2_auth_task_common.h"
25 
26 #define IS_SUPPORT_CURVE_256 true
27 #define IS_SUPPORT_CURVE_25519 false
28 
29 DECLARE_HC_VECTOR(AccountVersionInfoVec, void *)
30 IMPLEMENT_HC_VECTOR(AccountVersionInfoVec, void *, 1)
31 
32 static AccountVersionInfoVec g_authVersionInfoVec;
33 
34 static uint64_t g_authVersionNo = 0;
35 
IsAuthPakeV2EcP256Supported(void)36 static bool IsAuthPakeV2EcP256Supported(void)
37 {
38     return IsPakeV2AuthTaskSupported() && (GetPakeEcAlg() == PAKE_ALG_EC) && IS_SUPPORT_CURVE_256;
39 }
40 
IsAuthIsoSupported(void)41 static bool IsAuthIsoSupported(void)
42 {
43     return IsIsoAuthTaskSupported();
44 }
45 
46 static AccountVersionInfo g_authVersionInfoAll[] = {
47     { AUTH_PAKE_V2_EC_P256, PAKE_V2, PAKE_ALG_EC, CURVE_256, false, IsAuthPakeV2EcP256Supported, CreatePakeV2AuthTask},
48     { AUTH_ISO, ISO, PAKE_ALG_NONE, CURVE_NONE, false, IsAuthIsoSupported, CreateIsoAuthTask }
49 };
50 
InitVersionInfos(void)51 void InitVersionInfos(void)
52 {
53     g_authVersionInfoVec = CREATE_HC_VECTOR(AccountVersionInfoVec);
54     uint32_t size = sizeof(g_authVersionInfoAll) / sizeof(AccountVersionInfo);
55     for (uint32_t i = 0; i < size; i++) {
56         if (!g_authVersionInfoAll[i].isTaskSupported()) {
57             continue;
58         }
59         (void)g_authVersionInfoVec.pushBackT(&g_authVersionInfoVec, (void *)(&g_authVersionInfoAll[i]));
60         g_authVersionNo |= g_authVersionInfoAll[i].versionNo;
61     }
62 }
63 
DestroyVersionInfos(void)64 void DestroyVersionInfos(void)
65 {
66     DESTROY_HC_VECTOR(AccountVersionInfoVec, &g_authVersionInfoVec);
67 }
68 
NegotiateForAuth(int32_t credentialType)69 static const AccountVersionInfo *NegotiateForAuth(int32_t credentialType)
70 {
71     uint64_t versionNo;
72     if (credentialType == SYMMETRIC_CRED) {
73         versionNo = AUTH_ISO;
74     } else if (credentialType == ASYMMETRIC_CRED) {
75         versionNo = AUTH_PAKE_V2_EC_P256;
76     } else {
77         LOGE("Invalid credential type for auth: %d.", credentialType);
78         return NULL;
79     }
80     uint32_t index;
81     void **ptr = NULL;
82     FOR_EACH_HC_VECTOR(g_authVersionInfoVec, index, ptr) {
83         AccountVersionInfo *temp = (AccountVersionInfo *)(*ptr);
84         if ((temp->versionNo & versionNo) == versionNo) {
85             return temp;
86         }
87     }
88     LOGE("Version is not matched, failed to negotiate for account auth.");
89     return NULL;
90 }
91 
GetNegotiatedVersionInfo(int32_t operationCode,int32_t credentialType)92 const AccountVersionInfo *GetNegotiatedVersionInfo(int32_t operationCode, int32_t credentialType)
93 {
94     // Now, only support auth negotiate.
95     if (operationCode != AUTHENTICATE) {
96         LOGE("operationCode is not auth, not supported.");
97         return NULL;
98     }
99     return NegotiateForAuth(credentialType);
100 }
101 
GetSupportedVersionNo(int32_t operationCode)102 uint64_t GetSupportedVersionNo(int32_t operationCode)
103 {
104     (void)operationCode; // Now, only support auth negotiate.
105     return g_authVersionNo;
106 }
107