ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
record.c
Go to the documentation of this file.
2#include <recast/defer.h>
3
4#include <ascii-chat/buffer.h>
6#include <sodium.h>
7#include <string.h>
8
9#define CRYPTO_RECORD_PREFIX_SIZE 24
10#define CRYPTO_RECORD_EPOCH_SIZE 8
11
12static bool valid_flow(unsigned traffic_class, unsigned delivery) {
13 return (unsigned)traffic_class <= (CRYPTO_RECORD_CHANNELS - 1) &&
14 (unsigned)delivery <= CRYPTO_RECORD_DATAGRAM;
15}
16
17static bool ready(const crypto_record_t *state) {
18 return state && state->crypto && state->crypto->initialized &&
20 state->crypto->handshake_complete &&
22 state->crypto->mac_size == CRYPTO_MAC_SIZE &&
23 state->crypto->rekey_count == state->key_epoch;
24}
25
27 crypto_context_t *crypto, bool server) {
28 if (!state || state->crypto || !crypto || !crypto->initialized ||
29 !crypto->encrypt_data || !crypto->key_exchange_complete ||
30 !crypto->handshake_complete || crypto->nonce_size != CRYPTO_NONCE_SIZE ||
31 crypto->mac_size != CRYPTO_MAC_SIZE || crypto->rekey_in_progress)
33 "Session handshake and encryption must be ready");
34 *state = (crypto_record_t){
35 .crypto = crypto, .key_epoch = crypto->rekey_count, .server = server};
36 return ASCIICHAT_OK;
37}
38
40 unsigned traffic_class,
41 unsigned delivery, const void *packet,
42 size_t length, uint8_t *wire,
43 size_t capacity, size_t *out_length) {
44 return crypto_record_seal_into_domain(state, traffic_class, delivery, packet,
45 length, wire, capacity, out_length,
46 NULL);
47}
48
50 crypto_record_t *state, unsigned traffic_class, unsigned delivery,
51 const void *packet, size_t length, uint8_t *wire, size_t capacity,
52 size_t *out_length, buffer_domain_t *requested_domain) {
53 if (!wire || !out_length)
55 "Missing Record encrypted packet output");
56 *out_length = 0;
57 if (!ready(state) || !valid_flow(traffic_class, delivery))
59 "Record encryption is not ready for this flow");
60 if (!packet || !length)
61 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing record plaintext");
63 state->send_sequence[traffic_class][delivery] == UINT64_MAX)
65 "Record packet or sequence limit exceeded");
66 size_t plain_length = CRYPTO_RECORD_PREFIX_SIZE + length;
67 size_t cipher_capacity =
68 plain_length + state->crypto->nonce_size + state->crypto->mac_size;
69 if (capacity < CRYPTO_RECORD_EPOCH_SIZE + cipher_capacity)
71 "Record ciphertext output is too small");
72 buffer_domain_t *record_buffer_domain =
73 requested_domain ? requested_domain
74 : (traffic_class == CRYPTO_RECORD_CONTROL_CHANNEL
76 : NULL);
77 uint8_t *plain = buffer_alloc(record_buffer_domain, plain_length);
78 if (!plain) {
79 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate Record packet");
80 }
81 memcpy(plain, "ACR1", 4);
82 plain[4] = state->server ? 1 : 0;
83 plain[5] = (uint8_t)traffic_class;
84 plain[6] = (uint8_t)delivery;
85 plain[7] = 0;
86 uint64_t sequence = state->send_sequence[traffic_class][delivery] + 1;
87 uint64_t network_sequence = HOST_TO_NET_U64(sequence);
88 memcpy(plain + 8, &network_sequence, sizeof(network_sequence));
89 uint64_t network_epoch = HOST_TO_NET_U64(state->key_epoch);
90 memcpy(plain + 16, &network_epoch, sizeof(network_epoch));
91 memcpy(plain + CRYPTO_RECORD_PREFIX_SIZE, packet, length);
92 size_t cipher_length = 0;
93 crypto_result_t crypto_result = crypto_encrypt(
94 state->crypto, plain, plain_length, wire + CRYPTO_RECORD_EPOCH_SIZE,
95 cipher_capacity, &cipher_length);
96 buffer_release(plain);
97 if (crypto_result != CRYPTO_OK) {
98 return SET_ERRNO(ERROR_CRYPTO, "Cannot encrypt Record packet");
99 }
100 memcpy(wire, &network_epoch, sizeof(network_epoch));
101 cipher_length += CRYPTO_RECORD_EPOCH_SIZE;
102 state->send_sequence[traffic_class][delivery] = sequence;
103 *out_length = cipher_length;
104 return ASCIICHAT_OK;
105}
106
108 crypto_record_t *state, unsigned traffic_class, unsigned delivery,
109 const void *packet, size_t length, uint8_t **out, size_t *out_length,
110 uint64_t *sequence_out, crypto_record_validate_fn validate, void *context) {
111 if (!out || !out_length)
113 "Missing record decrypted packet output");
114 *out = NULL;
115 *out_length = 0;
116 if (sequence_out)
117 *sequence_out = 0;
118 if (!ready(state) || delivery > CRYPTO_RECORD_DATAGRAM ||
119 (traffic_class != CRYPTO_RECORD_ANY_CHANNEL &&
120 !valid_flow(traffic_class, delivery)))
122 "Record decryption is not ready for this flow");
123 const size_t record_overhead = CRYPTO_RECORD_EPOCH_SIZE +
124 (size_t)state->crypto->nonce_size +
125 (size_t)state->crypto->mac_size;
126 if (!packet || length < record_overhead ||
128 (size_t)state->crypto->nonce_size +
129 (size_t)state->crypto->mac_size)
131 "Invalid Record encrypted packet size");
132 const uint8_t *cipher = packet;
133 size_t cipher_length = length;
134 uint64_t network_epoch;
135 memcpy(&network_epoch, cipher, sizeof(network_epoch));
136 uint64_t epoch = NET_TO_HOST_U64(network_epoch);
137 crypto_context_t pending_crypto;
138 crypto_context_t *decrypt_crypto = state->crypto;
139 bool pending = false;
140 if (epoch != state->key_epoch) {
141 if (state->previous_valid && epoch == state->previous_epoch) {
142 decrypt_crypto = &state->previous_crypto;
143 } else if (state->key_epoch != UINT64_MAX &&
144 epoch == state->key_epoch + 1 && state->pending_prepared &&
145 state->crypto->rekey_in_progress &&
146 state->crypto->has_temp_key) {
147 pending_crypto = *state->crypto;
148 pending_crypto.session = NULL;
149 memcpy(pending_crypto.shared_key, state->crypto->temp_shared_key,
150 sizeof(pending_crypto.shared_key));
151 pending_crypto.rekey_in_progress = false;
152 pending_crypto.has_temp_key = false;
153 decrypt_crypto = &pending_crypto;
154 pending = true;
155 } else {
156 return SET_ERRNO(ERROR_CRYPTO, "Unknown or retired record key epoch");
157 }
158 }
159 cipher += CRYPTO_RECORD_EPOCH_SIZE;
160 cipher_length -= CRYPTO_RECORD_EPOCH_SIZE;
161 uint8_t *plain = buffer_alloc(NULL, cipher_length);
162 if (!plain) {
163 if (pending)
164 sodium_memzero(&pending_crypto, sizeof(pending_crypto));
165 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate record decrypted packet");
166 }
167 defer(if (plain) buffer_release(plain));
168 size_t plain_length = 0;
169 crypto_result_t crypto_result =
170 crypto_decrypt(decrypt_crypto, cipher, cipher_length, plain,
171 cipher_length, &plain_length);
172 if (pending)
173 sodium_memzero(&pending_crypto, sizeof(pending_crypto));
174 if (crypto_result != CRYPTO_OK) {
175 return SET_ERRNO(ERROR_CRYPTO, "Cannot authenticate Record packet");
176 }
177 if (plain_length < CRYPTO_RECORD_PREFIX_SIZE || memcmp(plain, "ACR1", 4) ||
178 plain[4] != (state->server ? 0 : 1) ||
179 (traffic_class != CRYPTO_RECORD_ANY_CHANNEL &&
180 plain[5] != traffic_class) ||
181 plain[5] >= CRYPTO_RECORD_CHANNELS || plain[6] != (uint8_t)delivery ||
182 plain[7] || memcmp(plain + 16, &network_epoch, sizeof(network_epoch))) {
184 "Invalid authenticated record flow metadata");
185 }
186 traffic_class = plain[5];
187 if (epoch == state->previous_epoch && state->previous_valid &&
188 !state->previous_allowed[traffic_class]) {
189 return SET_ERRNO(ERROR_CRYPTO, "Retired record channel epoch");
190 }
191 uint64_t network_sequence;
192 memcpy(&network_sequence, plain + 8, sizeof(network_sequence));
193 uint64_t sequence = NET_TO_HOST_U64(network_sequence);
194 uint64_t high = state->receive_high[traffic_class][delivery];
195 uint64_t seen = state->datagram_seen[traffic_class];
196 if (!sequence || (delivery == CRYPTO_RECORD_RELIABLE && sequence <= high) ||
197 (delivery == CRYPTO_RECORD_DATAGRAM && sequence <= high &&
198 (high - sequence >= 64 ||
199 (seen & (UINT64_C(1) << (high - sequence)))))) {
200 return SET_ERRNO(ERROR_CRYPTO, "Replayed or stale Record packet");
201 }
202 size_t inner_length = plain_length - CRYPTO_RECORD_PREFIX_SIZE;
203 asciichat_error_t result =
204 validate ? validate(context, traffic_class, delivery,
205 plain + CRYPTO_RECORD_PREFIX_SIZE, inner_length)
206 : ASCIICHAT_OK;
207 if (result != ASCIICHAT_OK) {
208 return result;
209 }
210 if (pending) {
211 result = crypto_record_commit_epoch(state);
212 if (result != ASCIICHAT_OK) {
213 return result;
214 }
215 }
216 if (delivery == CRYPTO_RECORD_DATAGRAM) {
217 if (sequence > high) {
218 uint64_t advance = sequence - high;
219 seen = advance >= 64 ? UINT64_C(1) : (seen << advance) | UINT64_C(1);
220 high = sequence;
221 } else {
222 seen |= UINT64_C(1) << (high - sequence);
223 }
224 state->datagram_seen[traffic_class] = seen;
225 } else {
226 high = sequence;
227 }
228 state->receive_high[traffic_class][delivery] = high;
229 state->last_receive_epoch = epoch;
230 state->last_receive_channel = traffic_class;
231 state->last_receive_delivery = delivery;
232 memmove(plain, plain + CRYPTO_RECORD_PREFIX_SIZE, inner_length);
233 *out = plain;
234 plain = NULL;
235 *out_length = inner_length;
236 if (sequence_out)
237 *sequence_out = sequence;
238 return ASCIICHAT_OK;
239}
240
242 if (!ready(state) || state->previous_valid || state->pending_prepared ||
243 state->key_epoch == UINT64_MAX || !state->crypto->rekey_in_progress ||
244 !state->crypto->has_temp_key)
246 "record key epoch is not ready to prepare");
247 state->pending_prepared = true;
248 return ASCIICHAT_OK;
249}
250
252 if (!ready(state) || !state->pending_prepared || state->previous_valid ||
253 !state->crypto->rekey_in_progress || !state->crypto->has_temp_key)
255 "record key epoch is not ready to commit");
256 crypto_context_t previous = *state->crypto;
257 previous.session = NULL;
258 previous.rekey_in_progress = previous.has_temp_key = false;
259 sodium_memzero(previous.temp_private_key, sizeof(previous.temp_private_key));
260 sodium_memzero(previous.temp_public_key, sizeof(previous.temp_public_key));
261 sodium_memzero(previous.temp_shared_key, sizeof(previous.temp_shared_key));
262 if (crypto_rekey_commit(state->crypto) != CRYPTO_OK) {
263 sodium_memzero(&previous, sizeof(previous));
264 return SET_ERRNO(ERROR_CRYPTO, "Cannot commit record key epoch");
265 }
266 state->previous_crypto = previous;
267 sodium_memzero(&previous, sizeof(previous));
268 state->previous_epoch = state->key_epoch;
269 state->key_epoch = state->crypto->rekey_count;
270 state->previous_valid = true;
271 state->pending_prepared = false;
272 for (unsigned i = 0; i < CRYPTO_RECORD_CHANNELS; ++i)
273 state->previous_allowed[i] = true;
274 return ASCIICHAT_OK;
275}
276
278 unsigned traffic_class,
279 uint64_t epoch) {
280 if (!ready(state) || (unsigned)traffic_class > (CRYPTO_RECORD_CHANNELS - 1) ||
281 epoch != state->key_epoch || !state->previous_valid)
283 "Invalid record epoch retirement marker");
284 state->previous_allowed[traffic_class] = false;
285 if (!state->previous_allowed[0] && !state->previous_allowed[1] &&
286 !state->previous_allowed[2]) {
287 sodium_memzero(&state->previous_crypto, sizeof(state->previous_crypto));
288 state->previous_valid = false;
289 }
290 return ASCIICHAT_OK;
291}
292
294 if (state)
295 sodium_memzero(state, sizeof(*state));
296}
#define SET_ERRNO(code,...)
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:294
buffer_domain_t * buffer_control_domain(void)
Definition buffer.c:465
struct buffer_domain buffer_domain_t
Definition buffer.h:28
#define buffer_release(data)
Definition buffer.h:108
🔄 Network byte order conversion helpers
#define NET_TO_HOST_U64(val)
Definition endian.h:224
#define HOST_TO_NET_U64(val)
Definition endian.h:209
unsigned long long uint64_t
unsigned char uint8_t
crypto_result_t crypto_rekey_commit(crypto_context_t *ctx)
Commit to new keys after successful REKEY_COMPLETE.
crypto_result_t crypto_encrypt(crypto_context_t *ctx, const uint8_t *plaintext, size_t plaintext_len, uint8_t *ciphertext_out, size_t ciphertext_out_size, size_t *ciphertext_len_out)
Encrypt data using XSalsa20-Poly1305.
crypto_result_t
Cryptographic operation result codes.
crypto_result_t crypto_decrypt(crypto_context_t *ctx, const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext_out, size_t plaintext_out_size, size_t *plaintext_len_out)
Decrypt data using XSalsa20-Poly1305.
#define CRYPTO_MAX_PLAINTEXT_SIZE
Maximum plaintext size (1MB).
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ ERROR_CRYPTO
@ ERROR_INVALID_PARAM
static bool size_t gotel_query_t * out
Definition queries.h:10
asciichat_error_t crypto_record_commit_epoch(crypto_record_t *state)
Definition record.c:251
#define CRYPTO_RECORD_PREFIX_SIZE
Definition record.c:9
asciichat_error_t crypto_record_init(crypto_record_t *state, crypto_context_t *crypto, bool server)
Definition record.c:26
asciichat_error_t crypto_record_seal_into_domain(crypto_record_t *state, unsigned traffic_class, unsigned delivery, const void *packet, size_t length, uint8_t *wire, size_t capacity, size_t *out_length, buffer_domain_t *requested_domain)
Definition record.c:49
#define CRYPTO_RECORD_EPOCH_SIZE
Definition record.c:10
void crypto_record_cleanup(crypto_record_t *state)
Definition record.c:293
asciichat_error_t crypto_record_prepare_epoch(crypto_record_t *state)
Definition record.c:241
asciichat_error_t crypto_record_seal_into(crypto_record_t *state, unsigned traffic_class, unsigned delivery, const void *packet, size_t length, uint8_t *wire, size_t capacity, size_t *out_length)
Definition record.c:39
asciichat_error_t crypto_record_open_sequence(crypto_record_t *state, unsigned traffic_class, unsigned delivery, const void *packet, size_t length, uint8_t **out, size_t *out_length, uint64_t *sequence_out, crypto_record_validate_fn validate, void *context)
Definition record.c:107
asciichat_error_t crypto_record_retire_epoch_flow(crypto_record_t *state, unsigned traffic_class, uint64_t epoch)
Definition record.c:277
asciichat_error_t(* crypto_record_validate_fn)(void *, unsigned, unsigned, const void *, size_t)
Definition record.h:40
struct crypto_record crypto_record_t
When code needs to work with crypto record, it keeps crypto, key_epoch, last_receive_epoch,...
#define CRYPTO_RECORD_CONTROL_CHANNEL
Definition record.h:12
#define CRYPTO_RECORD_RELIABLE
Definition record.h:13
#define CRYPTO_RECORD_DATAGRAM
Definition record.h:14
#define CRYPTO_RECORD_CHANNELS
Definition record.h:11
#define CRYPTO_RECORD_ANY_CHANNEL
Definition record.h:15
#define CRYPTO_NONCE_SIZE
Nonce size (XSalsa20).
Definition sizes.h:84
#define CRYPTO_MAC_SIZE
MAC size (Poly1305).
Definition sizes.h:90
🔤 String Manipulation and Shell Escaping Utilities
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
struct crypto_session * session
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
uint64_t last_receive_epoch
Definition record.h:28
unsigned last_receive_delivery
Definition record.h:36
bool previous_valid
Definition record.h:31
crypto_context_t previous_crypto
Definition record.h:29
uint64_t datagram_seen[3]
Definition record.h:35
bool pending_prepared
Definition record.h:31
uint64_t receive_high[3][2]
Definition record.h:34
crypto_context_t * crypto
Definition record.h:27
uint64_t key_epoch
Definition record.h:28
uint64_t send_sequence[3][2]
Definition record.h:33
bool server
Definition record.h:37
bool previous_allowed[3]
Definition record.h:32
unsigned last_receive_channel
Definition record.h:36
uint64_t previous_epoch
Definition record.h:30