• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (ptr == NULL || *ptr == NULL) {
84             continue;
85         }
86         AccountVersionInfo *temp = (AccountVersionInfo *)(*ptr);
87         if ((temp->versionNo & versionNo) == versionNo) {
88             return temp;
89         }
90     }
91     LOGE("Version is not matched, failed to negotiate for account auth.");
92     return NULL;
93 }
94 
GetNegotiatedVersionInfo(int32_t operationCode,int32_t credentialType)95 const AccountVersionInfo *GetNegotiatedVersionInfo(int32_t operationCode, int32_t credentialType)
96 {
97     // Now, only support auth negotiate.
98     if (operationCode != AUTHENTICATE) {
99         LOGE("operationCode is not auth, not supported.");
100         return NULL;
101     }
102     return NegotiateForAuth(credentialType);
103 }
104 
GetSupportedVersionNo(int32_t operationCode)105 uint64_t GetSupportedVersionNo(int32_t operationCode)
106 {
107     (void)operationCode; // Now, only support auth negotiate.
108     return g_authVersionNo;
109 }
110