ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
prepare.h File Reference

Go to the source code of this file.

Data Structures

struct  acip_packet_plan_t

Functions

asciichat_error_t acip_packet_plan (crypto_context_t *crypto, const socket_buffer_t *buffers, size_t count, acip_packet_plan_t *plan)
asciichat_error_t acip_packet_encrypt (crypto_context_t *crypto, const acip_packet_plan_t *plan, buffer_domain_t *domain, void *ciphertext, size_t capacity, packet_header_t *header, size_t *length)
 The useful part of acip packet plan is in buffers, count, plaintext_size, ciphertext_capacity: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path. Encrypt a successful encrypted plan into caller-owned ciphertext storage and construct its wire header. The caller chooses contiguous or vectored delivery, and must release storage only after delivery has consumed/copied it. This helper owns and releases only its temporary gather buffer, allocated from domain. Output length is zero on failure; no output may be sent on failure.
asciichat_error_t acip_packet_seal (crypto_record_t *, acip_traffic_class_t, acip_delivery_t, const void *, size_t, uint8_t **, size_t *)
asciichat_error_t acip_packet_open_sequence (crypto_record_t *, acip_traffic_class_t, acip_delivery_t, const void *, size_t, uint8_t **, size_t *, uint64_t *)
asciichat_error_t acip_packet_open (crypto_record_t *, acip_traffic_class_t, acip_delivery_t, const void *, size_t, uint8_t **, size_t *)
asciichat_error_t acip_packet_decrypt_payload (crypto_record_t *, unsigned channel, unsigned delivery, const void *, size_t, uint8_t **, size_t *, uint64_t *)

Function Documentation

◆ acip_packet_decrypt_payload()

asciichat_error_t acip_packet_decrypt_payload ( crypto_record_t * records,
unsigned channel,
unsigned delivery,
const void * payload,
size_t length,
uint8_t ** output,
size_t * written,
uint64_t * sequence )

Definition at line 164 of file prepare.c.

164 {
165 return crypto_record_open_sequence(records, flow, delivery, payload, length, output, written, sequence, validate_record_packet, NULL);
166}
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

References crypto_record_open_sequence().

Referenced by acip_packet_open_sequence(), and acip_security_decrypt().

◆ acip_packet_encrypt()

asciichat_error_t acip_packet_encrypt ( crypto_context_t * crypto,
const acip_packet_plan_t * plan,
buffer_domain_t * domain,
void * ciphertext,
size_t capacity,
packet_header_t * header,
size_t * length )

The useful part of acip packet plan is in buffers, count, plaintext_size, ciphertext_capacity: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path. Encrypt a successful encrypted plan into caller-owned ciphertext storage and construct its wire header. The caller chooses contiguous or vectored delivery, and must release storage only after delivery has consumed/copied it. This helper owns and releases only its temporary gather buffer, allocated from domain. Output length is zero on failure; no output may be sent on failure.

Definition at line 53 of file prepare.c.

53 {
54 if (length)
55 *length = 0;
56 if (!crypto || !plan || !plan->encrypt || !plan->buffers || !plan->count || !ciphertext || !header || !length)
57 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid ACIP encryption arguments");
58 if (capacity < plan->ciphertext_capacity)
59 return SET_ERRNO(ERROR_NETWORK_SIZE, "ACIP ciphertext storage too small");
60 const uint8_t *plaintext = (const uint8_t *)plan->buffers[0].data;
61 uint8_t *gathered = NULL;
62 if (plan->count > 1) {
63 gathered = (uint8_t *)buffer_alloc(domain, plan->plaintext_size);
64 if (!gathered)
65 return SET_ERRNO(ERROR_MEMORY, "Cannot gather ACIP encryption input");
66 size_t offset = 0;
67 for (size_t i = 0; i < plan->count; ++i) {
68 if (plan->buffers[i].len)
69 memcpy(gathered + offset, plan->buffers[i].data, plan->buffers[i].len);
70 offset += plan->buffers[i].len;
71 }
72 plaintext = gathered;
73 }
74 size_t encrypted_length = 0;
75 crypto_session_t *session = NULL;
76 bool shared = !plan->records_locked && crypto && crypto->session && plan->records == &crypto->session->records;
77 asciichat_error_t encrypted = shared ? crypto_session_lock(crypto, &session) : ASCIICHAT_OK;
78 if (encrypted == ASCIICHAT_OK)
79 encrypted = plan->records ? crypto_record_seal_into_domain(plan->records, plan->options.traffic_class, plan->options.delivery, plaintext, plan->plaintext_size, (uint8_t *)ciphertext, capacity,
80 &encrypted_length, domain)
81 : SET_ERRNO(ERROR_INVALID_STATE, "Missing authenticated record context");
82 if (session)
83 crypto_session_unlock(session);
84 buffer_release(gathered);
85 if (encrypted != ASCIICHAT_OK)
86 return encrypted;
87 socket_buffer_t slice = {ciphertext, encrypted_length};
88 asciichat_error_t result = packet_header_build(header, PACKET_TYPE_ENCRYPTED, &slice, 1, 0);
89 if (result != ASCIICHAT_OK)
90 return result;
91 *length = encrypted_length;
92 return ASCIICHAT_OK;
93}
#define SET_ERRNO(code,...)
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:294
#define buffer_release(data)
Definition buffer.h:108
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ ERROR_INVALID_PARAM
asciichat_error_t packet_header_build(packet_header_t *header, packet_type_t type, const socket_buffer_t *payload, size_t count, uint32_t client_id)
Definition packet.c:47
@ PACKET_TYPE_ENCRYPTED
Encrypted packet (after handshake completion).
Definition packet.h:369
struct crypto_session crypto_session_t
For crypto session, this is the live record that ties owner, mutex, records, rotation together....
void crypto_session_unlock(crypto_session_t *session)
asciichat_error_t crypto_session_lock(crypto_context_t *crypto, crypto_session_t **output)
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
crypto_record_t * records
Definition prepare.h:26
acip_send_options_t options
Definition prepare.h:28
const socket_buffer_t * buffers
Definition prepare.h:21
size_t plaintext_size
Definition prepare.h:23
acip_delivery_t delivery
Definition traffic.h:22
acip_traffic_class_t traffic_class
Definition traffic.h:21
struct crypto_session * session
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.

References ASCIICHAT_OK, buffer_alloc(), buffer_release, acip_packet_plan_t::buffers, acip_packet_plan_t::count, crypto_record_seal_into_domain(), crypto_session_lock(), crypto_session_unlock(), acip_send_options_t::delivery, acip_packet_plan_t::encrypt, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_MEMORY, ERROR_NETWORK_SIZE, acip_packet_plan_t::options, packet_header_build(), PACKET_TYPE_ENCRYPTED, acip_packet_plan_t::plaintext_size, acip_packet_plan_t::records, crypto_session::records, acip_packet_plan_t::records_locked, crypto_context_t::session, SET_ERRNO, and acip_send_options_t::traffic_class.

Referenced by acip_packet_seal(), and send_packet_secure().

◆ acip_packet_open()

asciichat_error_t acip_packet_open ( crypto_record_t * records,
acip_traffic_class_t flow,
acip_delivery_t delivery,
const void * packet,
size_t length,
uint8_t ** output,
size_t * written )

Definition at line 160 of file prepare.c.

160 {
161 return acip_packet_open_sequence(records, flow, delivery, packet, length, output, written, NULL);
162}
asciichat_error_t acip_packet_open_sequence(crypto_record_t *records, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written, uint64_t *sequence)
Definition prepare.c:145

References acip_packet_open_sequence().

◆ acip_packet_open_sequence()

asciichat_error_t acip_packet_open_sequence ( crypto_record_t * records,
acip_traffic_class_t flow,
acip_delivery_t delivery,
const void * packet,
size_t length,
uint8_t ** output,
size_t * written,
uint64_t * sequence )

Definition at line 145 of file prepare.c.

146 {
147 if (!output || !written)
148 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing decrypted packet output");
149 *output = NULL;
150 *written = 0;
151 packet_header_t header;
152 asciichat_error_t result = packet_validate(packet, length, 0, &header);
153 if (result != ASCIICHAT_OK)
154 return result;
155 if (NET_TO_HOST_U16(header.type) != PACKET_TYPE_ENCRYPTED)
156 return SET_ERRNO(ERROR_CRYPTO, "Expected encrypted packet");
157 return acip_packet_decrypt_payload(records, flow, delivery, (const uint8_t *)packet + sizeof(header), length - sizeof(header), output, written, sequence);
158}
#define NET_TO_HOST_U16(val)
Definition endian.h:115
@ ERROR_CRYPTO
asciichat_error_t packet_validate(const void *packet, size_t length, size_t limit, packet_header_t *output)
Definition packet.c:81
asciichat_error_t acip_packet_decrypt_payload(crypto_record_t *records, unsigned flow, unsigned delivery, const void *payload, size_t length, uint8_t **output, size_t *written, uint64_t *sequence)
Definition prepare.c:164
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641

References acip_packet_decrypt_payload(), ASCIICHAT_OK, ERROR_CRYPTO, ERROR_INVALID_PARAM, NET_TO_HOST_U16, PACKET_TYPE_ENCRYPTED, packet_validate(), and SET_ERRNO.

Referenced by acip_packet_open().

◆ acip_packet_plan()

asciichat_error_t acip_packet_plan ( crypto_context_t * crypto,
const socket_buffer_t * buffers,
size_t count,
acip_packet_plan_t * plan )

Validate sizes and decide eligibility without allocating or advancing crypto state.

Definition at line 9 of file prepare.c.

9 {
10 if (!plan)
11 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing ACIP packet plan");
12 *plan = (acip_packet_plan_t){0};
13 if (!buffers || !count || count > SOCKET_IOV_MAX)
14 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid ACIP packet vectors");
15 size_t size = 0;
16 for (size_t i = 0; i < count; ++i) {
17 if ((buffers[i].len && !buffers[i].data) || buffers[i].len > SOCKET_SEND_BUFFER_MAX - size)
18 return SET_ERRNO(ERROR_NETWORK_SIZE, "Invalid ACIP packet vector size");
19 size += buffers[i].len;
20 }
21 bool encrypt = false;
22 acip_send_options_t options = {.delivery = ACIP_DELIVERY_RELIABLE};
23 buffer_domain_t *domain = NULL;
24 if (buffers[0].len >= sizeof(packet_header_t)) {
25 packet_header_t header;
26 memcpy(&header, buffers[0].data, sizeof(header));
27 packet_type_t type = (packet_type_t)NET_TO_HOST_U16(header.type);
28 if (packet_is_handshake_type(type) || type == PACKET_TYPE_ERROR_MESSAGE)
29 domain = buffer_control_domain();
31 encrypt = crypto && crypto->encrypt_data && crypto_is_ready(crypto) && type != PACKET_TYPE_ENCRYPTED && !packet_is_initial_handshake_type(type);
32 } else {
33 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing ACIP packet header");
34 }
35 size_t capacity = 0;
36 crypto_record_t *records = NULL;
37 if (encrypt) {
38 if (size > CRYPTO_MAX_PLAINTEXT_SIZE - 24 || size > SOCKET_SEND_BUFFER_MAX - sizeof(packet_header_t) - CRYPTO_NONCE_SIZE - CRYPTO_MAC_SIZE)
39 return SET_ERRNO(ERROR_NETWORK_SIZE, "Encrypted ACIP packet too large");
40 crypto_session_t *session = NULL;
41 asciichat_error_t result = crypto_session_lock(crypto, &session);
42 if (result != ASCIICHAT_OK)
43 return result;
44 records = &session->records;
45 crypto_session_unlock(session);
46 capacity = size + 32 + crypto->nonce_size + crypto->mac_size;
47 }
48 *plan =
49 (acip_packet_plan_t){.buffers = buffers, .count = count, .plaintext_size = size, .ciphertext_capacity = capacity, .encrypt = encrypt, .domain = domain, .records = records, .options = options};
50 return ASCIICHAT_OK;
51}
buffer_domain_t * buffer_control_domain(void)
Definition buffer.c:465
struct buffer_domain buffer_domain_t
Definition buffer.h:28
#define NET_TO_HOST_U32(val)
Definition endian.h:85
bool crypto_is_ready(const crypto_context_t *ctx)
Check if key exchange is complete and ready for encryption.
#define CRYPTO_MAX_PLAINTEXT_SIZE
Maximum plaintext size (1MB).
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
@ PACKET_TYPE_CRYPTO_EPOCH_MARKER
Definition packet.h:359
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:388
struct crypto_record crypto_record_t
When code needs to work with crypto record, it keeps crypto, key_epoch, last_receive_epoch,...
#define CRYPTO_NONCE_SIZE
Nonce size (XSalsa20).
Definition sizes.h:84
#define CRYPTO_MAC_SIZE
MAC size (Poly1305).
Definition sizes.h:90
acip_traffic_class_t acip_packet_traffic_class(uint16_t type)
Definition traffic.c:4
acip_traffic_class_t
Definition traffic.h:9
@ ACIP_DELIVERY_RELIABLE
Definition traffic.h:16

References ACIP_DELIVERY_RELIABLE, acip_packet_traffic_class(), ASCIICHAT_OK, buffer_control_domain(), crypto_is_ready(), CRYPTO_MAC_SIZE, CRYPTO_MAX_PLAINTEXT_SIZE, CRYPTO_NONCE_SIZE, crypto_session_lock(), crypto_session_unlock(), crypto_context_t::encrypt_data, ERROR_INVALID_PARAM, ERROR_NETWORK_SIZE, crypto_context_t::mac_size, NET_TO_HOST_U16, NET_TO_HOST_U32, crypto_context_t::nonce_size, PACKET_TYPE_CRYPTO_EPOCH_MARKER, PACKET_TYPE_ENCRYPTED, PACKET_TYPE_ERROR_MESSAGE, crypto_session::records, SET_ERRNO, and acip_send_options_t::traffic_class.

Referenced by send_packet_secure().

◆ acip_packet_seal()

asciichat_error_t acip_packet_seal ( crypto_record_t * records,
acip_traffic_class_t flow,
acip_delivery_t delivery,
const void * packet,
size_t length,
uint8_t ** output,
size_t * written )

Definition at line 108 of file prepare.c.

108 {
109 if (!output || !written || !records || !records->crypto)
110 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid packet encryption arguments");
111 *output = NULL;
112 *written = 0;
113 if ((unsigned)flow > ACIP_TRAFFIC_VISUAL || (unsigned)delivery > ACIP_DELIVERY_DATAGRAM || (flow == ACIP_TRAFFIC_CONTROL && delivery == ACIP_DELIVERY_DATAGRAM))
114 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid packet output channel");
115 asciichat_error_t result = validate_record_packet(NULL, flow, delivery, packet, length);
116 if (result != ASCIICHAT_OK)
117 return result;
118 socket_buffer_t slice = {packet, length};
119 if (length > CRYPTO_MAX_PLAINTEXT_SIZE - 24)
120 return SET_ERRNO(ERROR_NETWORK_SIZE, "Packet exceeds record plaintext capacity");
121 acip_packet_plan_t plan = {.buffers = &slice,
122 .count = 1,
123 .plaintext_size = length,
124 .encrypt = true,
125 .records = records,
126 .records_locked = true,
127 .options = {.traffic_class = flow, .delivery = delivery},
128 .ciphertext_capacity = length + 32 + records->crypto->nonce_size + records->crypto->mac_size};
129 uint8_t *wire = (uint8_t *)buffer_alloc(plan.domain, sizeof(packet_header_t) + plan.ciphertext_capacity);
130 if (!wire)
131 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate encrypted packet");
132 packet_header_t header;
133 size_t encrypted_length = 0;
134 result = acip_packet_encrypt(records->crypto, &plan, plan.domain, wire + sizeof(header), plan.ciphertext_capacity, &header, &encrypted_length);
135 if (result != ASCIICHAT_OK) {
136 buffer_release(wire);
137 return result;
138 }
139 memcpy(wire, &header, sizeof(header));
140 *output = wire;
141 *written = sizeof(header) + encrypted_length;
142 return ASCIICHAT_OK;
143}
asciichat_error_t acip_packet_encrypt(crypto_context_t *crypto, const acip_packet_plan_t *plan, buffer_domain_t *domain, void *ciphertext, size_t capacity, packet_header_t *header, size_t *length)
The useful part of acip packet plan is in buffers, count, plaintext_size, ciphertext_capacity: togeth...
Definition prepare.c:53
size_t ciphertext_capacity
Definition prepare.h:24
buffer_domain_t * domain
Borrowed control reserve for handshake/error traffic; NULL otherwise.
Definition prepare.h:29
crypto_context_t * crypto
Definition record.h:27
@ ACIP_TRAFFIC_CONTROL
Definition traffic.h:10
@ ACIP_TRAFFIC_VISUAL
Definition traffic.h:12
@ ACIP_DELIVERY_DATAGRAM
Definition traffic.h:17

References ACIP_DELIVERY_DATAGRAM, acip_packet_encrypt(), ACIP_TRAFFIC_CONTROL, ACIP_TRAFFIC_VISUAL, ASCIICHAT_OK, buffer_alloc(), buffer_release, acip_packet_plan_t::ciphertext_capacity, crypto_record::crypto, CRYPTO_MAX_PLAINTEXT_SIZE, acip_packet_plan_t::domain, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_MEMORY, ERROR_NETWORK_SIZE, crypto_context_t::mac_size, crypto_context_t::nonce_size, and SET_ERRNO.

Referenced by acip_security_rekey_seal(), and acip_security_seal().