• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "key_agreement.h"
17 #include "base.h"
18 #include "log.h"
19 
20 #ifdef DESC
21 #undef DESC
22 #endif
23 #define DESC(...) 1
24 
25 static uint32_t g_sn_generator = 1;
26 static struct key_agreement_version g_current_version = { 1, 0, 0 };
27 
28 #if DESC("interface")
init_protocol(struct key_agreement_protocol * handle)29 void init_protocol(struct key_agreement_protocol *handle)
30 {
31     check_ptr_return(handle);
32     handle->state = PROTOCOL_INIT;
33     handle->last_state = PROTOCOL_INIT;
34     handle->last_time_sec = 0;
35     handle->sn = g_sn_generator++;
36 }
37 
set_state(struct key_agreement_protocol * handle,enum protocol_state new_state)38 void set_state(struct key_agreement_protocol *handle, enum protocol_state new_state)
39 {
40     check_ptr_return(handle);
41     enum protocol_state ori_state = handle->state;
42 
43     DBG_OUT("Object %u state is %u, new state is %u", handle->sn, ori_state, new_state);
44     if ((ori_state == PROTOCOL_TIMEOUT) || (ori_state == PROTOCOL_ERROR) || (ori_state == PROTOCOL_FINISH)) {
45         LOGE("Object %u state cannot change", handle->sn);
46         return; /* not allowed to modify these end status */
47     }
48     if (new_state < ori_state) {
49         LOGE("Object %u state cannot rollback", handle->sn);
50         return;
51     }
52 
53     if (handle->state != new_state) {
54         handle->last_state = ori_state;
55         handle->state = new_state;
56     }
57 }
58 
set_last_time_sec(struct key_agreement_protocol * handle)59 void set_last_time_sec(struct key_agreement_protocol *handle)
60 {
61     check_ptr_return(handle);
62     handle->last_time_sec = time((time_t *)NULL);
63 }
64 
get_current_version(void)65 struct key_agreement_version get_current_version(void)
66 {
67     return g_current_version;
68 }
69 
70 static bool is_version_bigger(struct key_agreement_version *dst, struct key_agreement_version *src);
71 static bool is_version_lesser(struct key_agreement_version *dst, struct key_agreement_version *src);
is_peer_support_current_version(struct key_agreement_version * peer_version,struct key_agreement_version * peer_support_version)72 bool is_peer_support_current_version(struct key_agreement_version *peer_version,
73     struct key_agreement_version *peer_support_version)
74 {
75     DBG_OUT("Peer version: %u.%u.%u, support version: %u.%u.%u, current version: %u.%u.%u",
76             peer_version->first, peer_version->second, peer_version->third,
77             peer_support_version->first, peer_support_version->second, peer_support_version->third,
78             g_current_version.first, g_current_version.second, g_current_version.third);
79     if (is_version_bigger(peer_version, &g_current_version)) {
80         return false;
81     }
82     if (is_version_lesser(peer_support_version, &g_current_version)) {
83         return false;
84     }
85     return true;
86 }
87 
is_version_bigger(struct key_agreement_version * dst,struct key_agreement_version * src)88 static bool is_version_bigger(struct key_agreement_version *dst, struct key_agreement_version *src)
89 {
90     uint32_t *dst_ptr = (uint32_t *)dst;
91     uint32_t *src_ptr = (uint32_t *)src;
92 
93     for (uint32_t i = 0; i < sizeof(struct key_agreement_version) / sizeof(uint32_t); i++) {
94         if (src_ptr[i] > dst_ptr[i]) {
95             return true;
96         }
97         if (src_ptr[i] < dst_ptr[i]) {
98             return false;
99         }
100     }
101     return false;
102 }
103 
is_version_lesser(struct key_agreement_version * dst,struct key_agreement_version * src)104 static bool is_version_lesser(struct key_agreement_version *dst, struct key_agreement_version *src)
105 {
106     uint32_t *dst_ptr = (uint32_t *)dst;
107     uint32_t *src_ptr = (uint32_t *)src;
108 
109     for (uint32_t i = 0; i < sizeof(struct key_agreement_version) / sizeof(uint32_t); i++) {
110         if (src_ptr[i] < dst_ptr[i]) {
111             return true;
112         }
113         if (src_ptr[i] > dst_ptr[i]) {
114             return false;
115         }
116     }
117     return false;
118 }
119 #endif /* DESC */
120 
121