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.h File Reference

Go to the source code of this file.

Data Structures

struct  crypto_rekey_session_t
 crypto rekey session is intentionally a small, concrete record. Fields such as security, send, context, activated_ns give callers the identity and current value they need. It is normally borrowed from, or owned by, the surrounding subsystem—not independently managed. More...

Typedefs

typedef asciichat_error_t(* crypto_rekey_emit_fn) (void *, unsigned, crypto_rekey_message_t, const void *, size_t)

Enumerations

enum  crypto_rekey_message_t {
  CRYPTO_REKEY_NONE , CRYPTO_REKEY_REQUEST , CRYPTO_REKEY_RESPONSE , CRYPTO_REKEY_COMPLETE ,
  CRYPTO_REKEY_MARKER , CRYPTO_REKEY_ACK , CRYPTO_REKEY_NEEDED
}

Functions

asciichat_error_t crypto_rekey_session_init (crypto_rekey_session_t *, crypto_record_t *, crypto_rekey_emit_fn, void *, uint64_t now_ns)
asciichat_error_t crypto_rekey_session_tick (crypto_rekey_session_t *, uint64_t now_ns)
asciichat_error_t crypto_rekey_session_request (crypto_rekey_session_t *, uint64_t now_ns)
asciichat_error_t crypto_rekey_session_receive (crypto_rekey_session_t *, unsigned, unsigned, crypto_rekey_message_t, const void *, size_t, uint64_t now_ns, bool *consumed)
void crypto_rekey_session_cleanup (crypto_rekey_session_t *)

Typedef Documentation

◆ crypto_rekey_emit_fn

typedef asciichat_error_t(* crypto_rekey_emit_fn) (void *, unsigned, crypto_rekey_message_t, const void *, size_t)

Definition at line 19 of file rekey.h.

Enumeration Type Documentation

◆ crypto_rekey_message_t

Logical rotation messages; the protocol adapter owns their wire encoding.

Enumerator
CRYPTO_REKEY_NONE 
CRYPTO_REKEY_REQUEST 
CRYPTO_REKEY_RESPONSE 
CRYPTO_REKEY_COMPLETE 
CRYPTO_REKEY_MARKER 
CRYPTO_REKEY_ACK 
CRYPTO_REKEY_NEEDED 

Definition at line 10 of file rekey.h.

10 {
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

Function Documentation

◆ crypto_rekey_session_cleanup()

void crypto_rekey_session_cleanup ( crypto_rekey_session_t * s)

Definition at line 244 of file rekey.c.

244 {
245 if (!s)
246 return;
247 if (s->exchange_ns && s->security && s->security->crypto &&
250 sodium_memzero(s, sizeof(*s));
251}
void crypto_rekey_abort(crypto_context_t *ctx)
Abort rekeying and fallback to old keys.
crypto_context_t * crypto
Definition record.h:27
crypto_record_t * security
Definition rekey.h:30
uint64_t exchange_ns
Definition rekey.h:33

References crypto_record::crypto, crypto_rekey_abort(), crypto_rekey_session_t::exchange_ns, crypto_context_t::rekey_in_progress, and crypto_rekey_session_t::security.

Referenced by crypto_session_destroy().

◆ crypto_rekey_session_init()

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_ns )

The caller serializes access with the associated record context.

Definition at line 63 of file rekey.c.

66 {
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}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
uint64_t key_epoch
Definition record.h:28
crypto rekey session is intentionally a small, concrete record. Fields such as security,...
Definition rekey.h:29

References ASCIICHAT_OK, crypto_record::crypto, ERROR_INVALID_PARAM, crypto_record::key_epoch, crypto_rekey_session_t::security, and SET_ERRNO.

Referenced by crypto_session_lock().

◆ crypto_rekey_session_receive()

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_ns,
bool * consumed )

Definition at line 150 of file rekey.c.

155 {
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}
#define NET_TO_HOST_U64(val)
Definition endian.h:224
unsigned long long uint64_t
unsigned char uint8_t
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_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ERROR_CRYPTO
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
#define CRYPTO_RECORD_RELIABLE
Definition record.h:13
#define CRYPTO_RECORD_CHANNELS
Definition record.h:11
asciichat_error_t crypto_rekey_session_tick(crypto_rekey_session_t *s, uint64_t now)
Definition rekey.c:87
#define CRYPTO_PUBLIC_KEY_SIZE
Public key size (X25519).
Definition sizes.h:72
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
bool server
Definition record.h:37
bool previous_allowed[3]
Definition record.h:32
uint8_t deferred_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition rekey.h:35
uint64_t activated_ns
Definition rekey.h:33
uint64_t peer_ack
Definition rekey.h:34

References crypto_rekey_session_t::activated_ns, ASCIICHAT_OK, crypto_record::crypto, CRYPTO_OK, CRYPTO_PUBLIC_KEY_SIZE, CRYPTO_RECORD_CHANNELS, crypto_record_commit_epoch(), crypto_record_prepare_epoch(), CRYPTO_RECORD_RELIABLE, crypto_record_retire_epoch_flow(), CRYPTO_REKEY_ACK, CRYPTO_REKEY_COMPLETE, CRYPTO_REKEY_MARKER, CRYPTO_REKEY_NEEDED, CRYPTO_REKEY_NONE, crypto_rekey_process_response(), CRYPTO_REKEY_REQUEST, CRYPTO_REKEY_RESPONSE, crypto_rekey_session_tick(), crypto_rekey_session_t::deferred_key, crypto_rekey_session_t::deferred_request, ERROR_CRYPTO, ERROR_INVALID_PARAM, ERROR_NETWORK_PROTOCOL, crypto_rekey_session_t::exchange_ns, crypto_rekey_session_t::expecting_complete, crypto_record::key_epoch, crypto_record::last_receive_epoch, crypto_rekey_session_t::markers_sent, NET_TO_HOST_U64, crypto_rekey_session_t::peer_ack, crypto_record::pending_prepared, crypto_record::previous_allowed, crypto_record::previous_valid, crypto_rekey_session_t::remote_requested, crypto_rekey_session_t::requested, crypto_rekey_session_t::security, crypto_record::server, SET_ERRNO, and crypto_rekey_session_t::waiting_response.

Referenced by acip_security_rekey_receive(), and crypto_session_receive().

◆ crypto_rekey_session_request()

asciichat_error_t crypto_rekey_session_request ( crypto_rekey_session_t * s,
uint64_t now_ns )

Definition at line 140 of file rekey.c.

141 {
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}

References ASCIICHAT_OK, crypto_rekey_session_tick(), crypto_rekey_session_t::exchange_ns, and crypto_rekey_session_t::requested.

Referenced by acip_security_request_rekey().

◆ crypto_rekey_session_tick()

asciichat_error_t crypto_rekey_session_tick ( crypto_rekey_session_t * s,
uint64_t now_ns )

Definition at line 87 of file rekey.c.

88 {
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}
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).
crypto_result_t crypto_rekey_init(crypto_context_t *ctx)
Initiate rekeying by generating new ephemeral keys.
@ ERROR_NETWORK_TIMEOUT
Definition error_codes.h:80
#define CRYPTO_REKEY_TIMEOUT_NS
Definition rekey.c:6
uint64_t last_request_ns
Definition rekey.h:33

References crypto_rekey_session_t::activated_ns, ASCIICHAT_OK, crypto_record::crypto, CRYPTO_OK, CRYPTO_PUBLIC_KEY_SIZE, crypto_record_prepare_epoch(), crypto_rekey_init(), CRYPTO_REKEY_NEEDED, crypto_rekey_process_request(), CRYPTO_REKEY_REQUEST, CRYPTO_REKEY_RESPONSE, CRYPTO_REKEY_TIMEOUT_NS, crypto_rekey_session_t::deferred_key, crypto_rekey_session_t::deferred_request, ERROR_CRYPTO, ERROR_NETWORK_TIMEOUT, crypto_rekey_session_t::exchange_ns, crypto_rekey_session_t::expecting_complete, crypto_rekey_session_t::last_request_ns, crypto_context_t::rekey_packet_count, crypto_context_t::rekey_packet_threshold, crypto_context_t::rekey_time_threshold, crypto_rekey_session_t::remote_requested, crypto_rekey_session_t::requested, crypto_rekey_session_t::security, crypto_record::server, SET_ERRNO, crypto_context_t::temp_public_key, and crypto_rekey_session_t::waiting_response.

Referenced by crypto_rekey_session_receive(), crypto_rekey_session_request(), and crypto_session_tick().