ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
rekey.c
Go to the documentation of this file.
3#include <sodium.h>
4#include <string.h>
5
6#define CRYPTO_REKEY_TIMEOUT_NS (30ULL * NS_PER_SEC_INT)
7
8static asciichat_error_t clock_step(crypto_rekey_session_t *s, uint64_t now) {
9 if (!s || !s->security || !s->security->crypto || !s->send ||
10 now < s->last_ns || now == UINT64_MAX)
12 "Invalid session rekey state or monotonic clock");
13 s->last_ns = now;
14 return ASCIICHAT_OK;
15}
16
17static asciichat_error_t send_rekey_packet(crypto_rekey_session_t *s,
18 unsigned flow,
20 const void *payload, size_t length) {
21 if (length > CRYPTO_PUBLIC_KEY_SIZE)
22 return SET_ERRNO(ERROR_INVALID_PARAM, "Rekey message exceeds its bound");
23 return s->send(s->context, flow, type, payload, length);
24}
25
26static bool settled(crypto_rekey_session_t *s) {
27 return !s->deferred_request && !s->waiting_response &&
30 s->peer_ack == s->security->key_epoch;
31}
32
33static asciichat_error_t observe_epoch(crypto_rekey_session_t *s) {
34 if (s->seen_epoch != s->security->key_epoch) {
35 if (s->seen_epoch == UINT64_MAX ||
36 s->security->key_epoch != s->seen_epoch + 1 || !s->exchange_ns)
37 return SET_ERRNO(ERROR_CRYPTO, "Unexpected session key epoch transition");
39 s->markers_sent = true;
40 s->ack_sent = false;
42 for (unsigned flow = 0; flow < CRYPTO_RECORD_CHANNELS; ++flow) {
43 asciichat_error_t result = send_rekey_packet(
44 s, (unsigned)flow, CRYPTO_REKEY_MARKER, &epoch, sizeof(epoch));
45 if (result != ASCIICHAT_OK)
46 return result;
47 }
48 }
49 if (s->markers_sent && !s->ack_sent && !s->security->previous_valid &&
52 asciichat_error_t result =
53 send_rekey_packet(s, 0, CRYPTO_REKEY_ACK, &epoch, sizeof(epoch));
54 if (result != ASCIICHAT_OK)
55 return result;
56 s->ack_sent = true;
57 }
58 if (settled(s))
59 s->exchange_ns = 0;
60 return ASCIICHAT_OK;
61}
62
64 crypto_record_t *security,
66 void *context, uint64_t now) {
67 if (!s || s->security || !security || !security->crypto || !send ||
68 now == UINT64_MAX)
70 "Invalid session rekey initialization");
71 *s = (crypto_rekey_session_t){.security = security,
72 .send = send,
73 .context = context,
74 .activated_ns = now,
75 .last_ns = now,
76 .seen_epoch = security->key_epoch,
77 .peer_ack = security->key_epoch};
78 return ASCIICHAT_OK;
79}
80
81static bool request_allowed(crypto_rekey_session_t *s, uint64_t now) {
82 return now - s->activated_ns >= (uint64_t)REKEY_MIN_INTERVAL &&
83 (!s->last_request_ns ||
85}
86
88 uint64_t now) {
89 asciichat_error_t result = clock_step(s, now);
90 if (result != ASCIICHAT_OK)
91 return result;
92 result = observe_epoch(s);
93 if (result != ASCIICHAT_OK)
94 return result;
97 "session key exchange or flow retirement timed out");
98 // A peer measures its interval at send time. Keep one authenticated request
99 // until our receive-side interval elapses; network jitter must not turn a
100 // valid rotation into a protocol violation or bypass the rate limit.
101 if (s->deferred_request && request_allowed(s, now)) {
102 crypto_context_t *crypto = s->security->crypto;
103 s->deferred_request = false;
104 s->expecting_complete = true;
105 s->last_request_ns = now;
106 if (crypto_rekey_init(crypto) != CRYPTO_OK ||
108 return SET_ERRNO(ERROR_CRYPTO, "Cannot compute session rekey response");
109 sodium_memzero(s->deferred_key, sizeof(s->deferred_key));
111 if (result == ASCIICHAT_OK)
112 result =
113 send_rekey_packet(s, 0, CRYPTO_REKEY_RESPONSE,
115 return result;
116 }
117 if (!settled(s) || !request_allowed(s, now))
118 return ASCIICHAT_OK;
119 crypto_context_t *crypto = s->security->crypto;
120 bool threshold =
121 crypto->rekey_packet_count >= crypto->rekey_packet_threshold ||
122 (crypto->rekey_time_threshold > 0 &&
123 now - s->activated_ns >= (uint64_t)crypto->rekey_time_threshold);
124 if (!s->requested && !threshold)
125 return ASCIICHAT_OK;
126 s->requested = false;
127 s->exchange_ns = now;
128 if (!s->security->server) {
129 s->remote_requested = true;
130 return send_rekey_packet(s, 0, CRYPTO_REKEY_NEEDED, NULL, 0);
131 }
132 if (crypto_rekey_init(crypto) != CRYPTO_OK)
133 return SET_ERRNO(ERROR_CRYPTO, "Cannot initiate session ephemeral rekey");
134 s->waiting_response = true;
135 s->last_request_ns = now;
136 return send_rekey_packet(s, 0, CRYPTO_REKEY_REQUEST, crypto->temp_public_key,
138}
139
141 uint64_t now) {
142 asciichat_error_t result = clock_step(s, now);
143 if (result != ASCIICHAT_OK)
144 return result;
145 if (!s->exchange_ns)
146 s->requested = true;
147 return crypto_rekey_session_tick(s, now);
148}
149
151 unsigned flow, unsigned delivery,
153 const void *payload,
154 size_t bytes, uint64_t now,
155 bool *consumed) {
156 if (!consumed || (!payload && bytes) || flow >= CRYPTO_RECORD_CHANNELS ||
157 type > CRYPTO_REKEY_NEEDED)
158 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid rekey input");
159 *consumed = false;
160 asciichat_error_t result = clock_step(s, now);
161 if (result != ASCIICHAT_OK)
162 return result;
163 result = observe_epoch(s);
164 if (result != ASCIICHAT_OK)
165 return result;
166 if (type == CRYPTO_REKEY_NONE)
167 return crypto_rekey_session_tick(s, now);
168 *consumed = true;
169 uint64_t epoch = s->security->key_epoch;
170 if (delivery != CRYPTO_RECORD_RELIABLE ||
171 s->security->last_receive_epoch != epoch ||
172 (type != CRYPTO_REKEY_MARKER && flow != 0))
173 return SET_ERRNO(
175 "session rekey message has invalid flow, delivery, or epoch");
176 crypto_context_t *crypto = s->security->crypto;
177 if (type == CRYPTO_REKEY_MARKER || type == CRYPTO_REKEY_ACK) {
178 uint64_t value;
179 if (bytes != sizeof(value))
181 "Invalid session epoch marker length");
182 memcpy(&value, payload, sizeof(value));
183 if (!epoch || NET_TO_HOST_U64(value) != epoch || !s->markers_sent)
185 "Unexpected session epoch marker");
186 if (type == CRYPTO_REKEY_MARKER) {
187 if (!s->security->previous_valid || !s->security->previous_allowed[flow])
189 "Duplicate session flow retirement marker");
190 result = crypto_record_retire_epoch_flow(s->security, flow, epoch);
191 } else {
192 s->peer_ack = epoch;
193 }
194 } else if (type == CRYPTO_REKEY_NEEDED) {
195 if (!s->security->server || bytes)
197 "Invalid session rekey request signal");
198 if (!s->exchange_ns)
199 s->requested = true;
200 } else if (type == CRYPTO_REKEY_REQUEST) {
201 if (s->security->server || bytes != CRYPTO_PUBLIC_KEY_SIZE ||
204 s->peer_ack != epoch)
206 "Unexpected session ephemeral rekey request");
207 s->exchange_ns = now;
208 s->remote_requested = false;
209 s->deferred_request = true;
210 memcpy(s->deferred_key, payload, sizeof(s->deferred_key));
211 } else if (type == CRYPTO_REKEY_RESPONSE) {
212 if (!s->security->server || !s->waiting_response ||
213 bytes != CRYPTO_PUBLIC_KEY_SIZE)
215 "Unexpected session ephemeral rekey response");
216 if (crypto_rekey_process_response(crypto, payload) != CRYPTO_OK)
217 return SET_ERRNO(ERROR_CRYPTO, "Cannot compute session next epoch");
219 if (result == ASCIICHAT_OK)
221 if (result == ASCIICHAT_OK) {
222 s->waiting_response = false;
223 s->activated_ns = now;
224 uint8_t proof = 0;
225 result = send_rekey_packet(s, 0, CRYPTO_REKEY_COMPLETE, &proof, 1);
226 }
227 } else {
228 if (s->security->server || !s->expecting_complete || bytes != 1 ||
229 ((const uint8_t *)payload)[0] || !epoch ||
232 "Unexpected session rekey confirmation");
233 s->expecting_complete = false;
234 s->activated_ns = now;
235 }
236 if (result != ASCIICHAT_OK)
237 return result;
238 result = observe_epoch(s);
239 if (result != ASCIICHAT_OK)
240 return result;
241 return crypto_rekey_session_tick(s, now);
242}
243
245 if (!s)
246 return;
247 if (s->exchange_ns && s->security && s->security->crypto &&
250 sodium_memzero(s, sizeof(*s));
251}
#define SET_ERRNO(code,...)
🔄 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
#define REKEY_MIN_INTERVAL
Minimum time interval between rekey requests (3 seconds in nanoseconds for testing,...
void crypto_rekey_abort(crypto_context_t *ctx)
Abort rekeying and fallback to old keys.
crypto_result_t crypto_rekey_process_request(crypto_context_t *ctx, const uint8_t *peer_new_public_key)
Process REKEY_REQUEST from peer (responder side).
#define REKEY_MIN_REQUEST_INTERVAL
Minimum interval between rekey requests (60 seconds in nanoseconds, DDoS protection).
crypto_result_t crypto_rekey_init(crypto_context_t *ctx)
Initiate rekeying by generating new ephemeral keys.
crypto_result_t crypto_rekey_process_response(crypto_context_t *ctx, const uint8_t *peer_new_public_key)
Process REKEY_RESPONSE from peer (initiator side).
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
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CRYPTO
@ ERROR_INVALID_PARAM
@ ERROR_NETWORK_TIMEOUT
Definition error_codes.h:80
asciichat_error_t crypto_record_commit_epoch(crypto_record_t *state)
Definition record.c:251
asciichat_error_t crypto_record_prepare_epoch(crypto_record_t *state)
Definition record.c:241
asciichat_error_t crypto_record_retire_epoch_flow(crypto_record_t *state, unsigned traffic_class, uint64_t epoch)
Definition record.c:277
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_RELIABLE
Definition record.h:13
#define CRYPTO_RECORD_CHANNELS
Definition record.h:11
asciichat_error_t crypto_rekey_session_receive(crypto_rekey_session_t *s, unsigned flow, unsigned delivery, crypto_rekey_message_t type, const void *payload, size_t bytes, uint64_t now, bool *consumed)
Definition rekey.c:150
#define CRYPTO_REKEY_TIMEOUT_NS
Definition rekey.c:6
asciichat_error_t crypto_rekey_session_tick(crypto_rekey_session_t *s, uint64_t now)
Definition rekey.c:87
asciichat_error_t crypto_rekey_session_init(crypto_rekey_session_t *s, crypto_record_t *security, crypto_rekey_emit_fn send, void *context, uint64_t now)
Definition rekey.c:63
void crypto_rekey_session_cleanup(crypto_rekey_session_t *s)
Definition rekey.c:244
asciichat_error_t crypto_rekey_session_request(crypto_rekey_session_t *s, uint64_t now)
Definition rekey.c:140
asciichat_error_t(* crypto_rekey_emit_fn)(void *, unsigned, crypto_rekey_message_t, const void *, size_t)
Definition rekey.h:19
crypto_rekey_message_t
Definition rekey.h:10
@ CRYPTO_REKEY_REQUEST
Definition rekey.h:12
@ CRYPTO_REKEY_NONE
Definition rekey.h:11
@ CRYPTO_REKEY_RESPONSE
Definition rekey.h:13
@ CRYPTO_REKEY_MARKER
Definition rekey.h:15
@ CRYPTO_REKEY_NEEDED
Definition rekey.h:17
@ CRYPTO_REKEY_COMPLETE
Definition rekey.h:14
@ CRYPTO_REKEY_ACK
Definition rekey.h:16
#define CRYPTO_PUBLIC_KEY_SIZE
Public key size (X25519).
Definition sizes.h:72
🔤 String Manipulation and Shell Escaping Utilities
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
uint64_t last_receive_epoch
Definition record.h:28
bool previous_valid
Definition record.h:31
bool pending_prepared
Definition record.h:31
crypto_context_t * crypto
Definition record.h:27
uint64_t key_epoch
Definition record.h:28
bool server
Definition record.h:37
bool previous_allowed[3]
Definition record.h:32
crypto rekey session is intentionally a small, concrete record. Fields such as security,...
Definition rekey.h:29
uint8_t deferred_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition rekey.h:35
uint64_t activated_ns
Definition rekey.h:33
crypto_record_t * security
Definition rekey.h:30
crypto_rekey_emit_fn send
Definition rekey.h:31
uint64_t seen_epoch
Definition rekey.h:33
uint64_t last_request_ns
Definition rekey.h:33
uint64_t peer_ack
Definition rekey.h:34
uint64_t exchange_ns
Definition rekey.h:33