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 #ifndef __KEY_AGREEMENT_H__ 17 #define __KEY_AGREEMENT_H__ 18 19 #include <stdint.h> 20 #include <stdbool.h> 21 #include <time.h> 22 23 enum protocol_state { 24 PROTOCOL_UNINIT = 0, 25 PROTOCOL_INIT, 26 START_REQUEST, /* C->S */ 27 START_RESPONSE, /* S->C */ 28 END_REQUEST, /* C->S */ 29 PROTOCOL_TIMEOUT, 30 PROTOCOL_ERROR, 31 PROTOCOL_FINISH 32 }; 33 34 enum protocol_action { 35 SEND_START_REQUEST = 1, 36 SEND_START_RESPONSE, 37 SEND_END_REQUEST, 38 SEND_END_RESPONSE, 39 RECEIVE_END_RESPONSE 40 }; 41 42 #define CHALLENGE_BUFF_LENGTH 16 43 44 struct key_agreement_protocol { 45 enum protocol_state state; 46 enum protocol_state last_state; 47 uint32_t sn; 48 time_t last_time_sec; 49 }; 50 51 /* first.second.third */ 52 struct key_agreement_version { 53 uint32_t first; 54 uint32_t second; 55 uint32_t third; 56 }; 57 58 struct challenge { 59 uint32_t length; 60 uint8_t challenge[CHALLENGE_BUFF_LENGTH]; 61 }; 62 63 void init_protocol(struct key_agreement_protocol *handle); 64 void set_state(struct key_agreement_protocol *handle, enum protocol_state new_state); 65 void set_last_time_sec(struct key_agreement_protocol *handle); 66 struct key_agreement_version get_current_version(void); 67 bool is_peer_support_current_version(struct key_agreement_version *peer_version, 68 struct key_agreement_version *peer_support_version); 69 70 #endif /* __KEY_AGREEMENT_H__ */ 71