1 // Copyright 2023 Google LLC 2 // 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 // https://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 #ifndef UKEY2_H 16 #define UKEY2_H 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 #include <stddef.h> 23 #include <stdint.h> 24 25 typedef uint64_t Ukey2HandshakeContextHandle; 26 typedef uint64_t Ukey2ConnectionContextHandle; 27 typedef uint8_t Aes256Key[32]; 28 29 typedef struct { 30 uint8_t* handle; 31 size_t len; 32 size_t cap; 33 } RustFFIByteArray; 34 35 typedef struct { 36 uint8_t* handle; 37 size_t len; 38 } CFFIByteArray; 39 40 typedef struct { 41 bool success; 42 RustFFIByteArray alert_to_send; 43 } CMessageParseResult; 44 45 typedef enum { 46 STATUS_GOOD = 0, 47 STATUS_ERR = 1, 48 } CD2DRestoreConnectionContextV1Status; 49 50 typedef struct { 51 Ukey2ConnectionContextHandle handle; 52 CD2DRestoreConnectionContextV1Status status; 53 } CD2DRestoreConnectionContextV1Result; 54 55 // Create a new ResponderD2DHandshakeContext 56 Ukey2HandshakeContextHandle responder_new(); 57 58 // Create a new InitiatorD2DHandshakeContext 59 Ukey2HandshakeContextHandle initiator_new(); 60 61 // Utilities 62 void rust_dealloc_ffi_byte_array(RustFFIByteArray array); 63 64 // Common handshake methods 65 bool is_handshake_complete(Ukey2HandshakeContextHandle handle); 66 RustFFIByteArray get_next_handshake_message(Ukey2HandshakeContextHandle handle); 67 CMessageParseResult parse_handshake_message(Ukey2HandshakeContextHandle handle, 68 CFFIByteArray message); 69 Ukey2ConnectionContextHandle to_connection_context( 70 Ukey2HandshakeContextHandle handle); 71 RustFFIByteArray get_verification_string(Ukey2HandshakeContextHandle handle, 72 size_t output_length); 73 74 // D2DConnectionContextV1 methods 75 RustFFIByteArray encode_message_to_peer(Ukey2ConnectionContextHandle handle, 76 CFFIByteArray message, 77 CFFIByteArray associated_data); 78 RustFFIByteArray decode_message_from_peer(Ukey2ConnectionContextHandle handle, 79 CFFIByteArray message, 80 CFFIByteArray associated_data); 81 RustFFIByteArray get_session_unique(Ukey2ConnectionContextHandle handle); 82 int get_sequence_number_for_encoding(Ukey2ConnectionContextHandle handle); 83 int get_sequence_number_for_decoding(Ukey2ConnectionContextHandle handle); 84 RustFFIByteArray save_session(Ukey2ConnectionContextHandle handle); 85 CD2DRestoreConnectionContextV1Result from_saved_session(CFFIByteArray data); 86 87 #ifdef __cplusplus 88 } // extern "C" 89 #endif 90 91 #endif 92