wickr-crypto-c

transport_packet.h
1 /*
2 * Copyright © 2012-2020 Wickr Inc. All rights reserved.
3 *
4 * This code is being released for EDUCATIONAL, ACADEMIC, AND CODE REVIEW PURPOSES
5 * ONLY. COMMERCIAL USE OF THE CODE IS EXPRESSLY PROHIBITED. For additional details,
6 * please see LICENSE
7 *
8 * THE CODE IS MADE AVAILABLE "AS-IS" AND WITHOUT ANY EXPRESS OR
9 * IMPLIED GUARANTEES AS TO FITNESS, MERCHANTABILITY, NON-
10 * INFRINGEMENT OR OTHERWISE. IT IS NOT BEING PROVIDED IN TRADE BUT ON
11 * A VOLUNTARY BASIS ON BEHALF OF THE AUTHOR’S PART FOR THE BENEFIT
12 * OF THE LICENSEE AND IS NOT MADE AVAILABLE FOR CONSUMER USE OR ANY
13 * OTHER USE OUTSIDE THE TERMS OF THIS LICENSE. ANYONE ACCESSING THE
14 * CODE SHOULD HAVE THE REQUISITE EXPERTISE TO SECURE THEIR SYSTEM
15 * AND DEVICES AND TO ACCESS AND USE THE CODE FOR REVIEW PURPOSES
16 * ONLY. LICENSEE BEARS THE RISK OF ACCESSING AND USING THE CODE. IN
17 * PARTICULAR, AUTHOR BEARS NO LIABILITY FOR ANY INTERFERENCE WITH OR
18 * ADVERSE EFFECT THAT MAY OCCUR AS A RESULT OF THE LICENSEE
19 * ACCESSING AND/OR USING THE CODE ON LICENSEE’S SYSTEM.
20 */
21
22 #ifndef transport_packet_h
23 #define transport_packet_h
24
25 #include "buffer.h"
26 #include "identity.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
36 typedef enum {
37 TRANSPORT_PAYLOAD_TYPE_HANDSHAKE, /* Payload is a handshake control packet */
38 TRANSPORT_PAYLOAD_TYPE_CIPHERTEXT /* Payload contains encrypted application data */
39 } wickr_transport_packet_payload_type;
40
41 typedef enum {
42 TRANSPORT_MAC_TYPE_NONE, /* Packet contains no authentication */
43 TRANSPORT_MAC_TYPE_AUTH_CIPHER, /* Packet body contains ciphertext generated by an authenticated cipher such as AES-GCM */
44 TRANSPORT_MAC_TYPE_EC_P521 /* Packet was signed by a key with the P521 curve */
45 } wickr_transport_packet_mac_type;
46
64 struct wickr_transport_handshake_meta {
65 uint8_t protocol_version;
66 uint64_t flags; /* Future use */
67 };
68
69 typedef struct wickr_transport_handshake_meta wickr_transport_handshake_meta_t;
70
82 struct wickr_transport_data_meta {
83 uint64_t sequence_number;
84 };
85
86 typedef struct wickr_transport_data_meta wickr_transport_data_meta_t;
87
103 struct wickr_transport_packet_meta {
104 union {
105 wickr_transport_handshake_meta_t handshake;
106 wickr_transport_data_meta_t data;
107 } body_meta; /* Set based on body_type */
108 wickr_transport_packet_payload_type body_type;
109 wickr_transport_packet_mac_type mac_type;
110 };
111
112 typedef struct wickr_transport_packet_meta wickr_transport_packet_meta_t;
113
123 void wickr_transport_packet_meta_initialize_handshake(wickr_transport_packet_meta_t *meta_out,
124 uint8_t protocol_version,
125 wickr_transport_packet_mac_type mac_type);
126
136 void wickr_transport_packet_meta_initialize_data(wickr_transport_packet_meta_t *meta_out,
137 uint64_t sequence_number,
138 wickr_transport_packet_mac_type mac_type);
139
149 int wickr_transport_packet_meta_initialize_buffer(wickr_transport_packet_meta_t *meta_out,
150 const wickr_buffer_t *buffer);
151
158 wickr_buffer_t *wickr_transport_packet_meta_serialize(const wickr_transport_packet_meta_t *meta);
159
175 struct wickr_transport_packet {
176 wickr_buffer_t *network_buffer;
177 wickr_buffer_t *body;
178 wickr_buffer_t *mac;
179 wickr_transport_packet_meta_t meta;
180 };
181
182 typedef struct wickr_transport_packet wickr_transport_packet_t;
183
192 wickr_transport_packet_t *wickr_transport_packet_create(wickr_transport_packet_meta_t meta, wickr_buffer_t *body);
193
202 wickr_transport_packet_t *wickr_transport_packet_copy(const wickr_transport_packet_t *pkt);
203
210 void wickr_transport_packet_destroy(wickr_transport_packet_t **pkt);
211
220 wickr_buffer_t *wickr_transport_packet_serialize(const wickr_transport_packet_t *pkt);
221
230 wickr_transport_packet_t *wickr_transport_packet_create_from_buffer(const wickr_buffer_t *buffer);
231
242 bool wickr_transport_packet_sign(wickr_transport_packet_t *pkt,
243 const wickr_crypto_engine_t *engine,
244 const wickr_identity_chain_t *identity_chain);
245
256 bool wickr_transport_packet_verify(const wickr_transport_packet_t *packet,
257 const wickr_crypto_engine_t *engine,
258 wickr_identity_chain_t *identity_chain);
259
260 #ifdef __cplusplus
261 }
262 #endif
263
264 #endif /* transport_packet_h */
wickr_transport_packet_meta_serialize
wickr_buffer_t * wickr_transport_packet_meta_serialize(const wickr_transport_packet_meta_t *meta)
wickr_transport_packet_meta_initialize_data
void wickr_transport_packet_meta_initialize_data(wickr_transport_packet_meta_t *meta_out, uint64_t sequence_number, wickr_transport_packet_mac_type mac_type)
wickr_transport_packet_meta_initialize_buffer
int wickr_transport_packet_meta_initialize_buffer(wickr_transport_packet_meta_t *meta_out, const wickr_buffer_t *buffer)
wickr_transport_packet_meta_initialize_handshake
void wickr_transport_packet_meta_initialize_handshake(wickr_transport_packet_meta_t *meta_out, uint8_t protocol_version, wickr_transport_packet_mac_type mac_type)
wickr_transport_packet_verify
bool wickr_transport_packet_verify(const wickr_transport_packet_t *packet, const wickr_crypto_engine_t *engine, wickr_identity_chain_t *identity_chain)
wickr_transport_packet_copy
wickr_transport_packet_t * wickr_transport_packet_copy(const wickr_transport_packet_t *pkt)
wickr_transport_packet_serialize
wickr_buffer_t * wickr_transport_packet_serialize(const wickr_transport_packet_t *pkt)
wickr_transport_packet_create
wickr_transport_packet_t * wickr_transport_packet_create(wickr_transport_packet_meta_t meta, wickr_buffer_t *body)
wickr_transport_packet_create_from_buffer
wickr_transport_packet_t * wickr_transport_packet_create_from_buffer(const wickr_buffer_t *buffer)
wickr_transport_packet_sign
bool wickr_transport_packet_sign(wickr_transport_packet_t *pkt, const wickr_crypto_engine_t *engine, const wickr_identity_chain_t *identity_chain)
wickr_transport_packet_destroy
void wickr_transport_packet_destroy(wickr_transport_packet_t **pkt)
wickr_buffer
Represents an array of bytes and the length of the allocation associated with those bytes.
Definition: buffer.h:51
wickr_crypto_engine
Interface to a set of cryptographic operations used throughout the library. Currently the default imp...
Definition: crypto_engine.h:53
wickr_identity_chain
Represents a root -> node relationship as well as it's signature status.
Definition: identity.h:116
wickr_transport_data_meta
Metadata specifically for user data packets within a transport.
Definition: transport_packet.h:82
wickr_transport_data_meta::sequence_number
uint64_t sequence_number
Definition: transport_packet.h:83
wickr_transport_handshake_meta
Metadata specifically for handshake packets within a transport.
Definition: transport_packet.h:64
wickr_transport_handshake_meta::protocol_version
uint8_t protocol_version
Definition: transport_packet.h:65
wickr_transport_handshake_meta::flags
uint64_t flags
Definition: transport_packet.h:66
wickr_transport_packet_meta
Packet metadata included with all transport packets.
Definition: transport_packet.h:103
wickr_transport_packet_meta::mac_type
wickr_transport_packet_mac_type mac_type
Definition: transport_packet.h:109
wickr_transport_packet_meta::body_meta
union wickr_transport_packet_meta::@0 body_meta
wickr_transport_packet_meta::body_type
wickr_transport_packet_payload_type body_type
Definition: transport_packet.h:108
wickr_transport_packet
A packet used within the wickr_transport_ctx to package up data and related metadata + authentication...
Definition: transport_packet.h:175
wickr_transport_packet::meta
wickr_transport_packet_meta_t meta
Definition: transport_packet.h:179
wickr_transport_packet::network_buffer
wickr_buffer_t * network_buffer
Definition: transport_packet.h:176
wickr_transport_packet::mac
wickr_buffer_t * mac
Definition: transport_packet.h:178
wickr_transport_packet::body
wickr_buffer_t * body
Definition: transport_packet.h:177