- secure_channel: add sc_stream_state + sc_stream_init/sc_stream_xor/sc_stream_cleanup
AES-128-CTR streaming cipher for incremental encrypt/decrypt
- stcp.h/c: shared stcp_conn lifecycle, set_on_close, rx/tx queue setup
- stcp_server: listen/accept, handshake (obfuscated pubkey + ECDH + stream), DATA phase
- stcp_client: connect, handshake, DATA phase
- Protocol: key_block(72) + enc(data+crc) + padding for handshake
size(2 LE) + enc(data+crc) for data messages
- Tests: test_stream_cipher (AES-CTR correctness)
test_stcp (6 cases: sizes 0..65535, 200 msgs, wrong key,
close detection, multi-client, interleaved send/recv)
- Fix: 0-length message handling in tx_queue callbacks
81 lines
2.2 KiB
C
81 lines
2.2 KiB
C
// stcp.h — Streaming TCP: shared structures, constants, connection lifecycle
|
|
#ifndef STCP_H
|
|
#define STCP_H
|
|
|
|
#include "secure_channel.h"
|
|
#include "crc32.h"
|
|
#include "../lib/u_async.h"
|
|
#include "../lib/socket_compat.h"
|
|
#include "../lib/ll_queue.h"
|
|
#include "../lib/debug_config.h"
|
|
#include <stdint.h>
|
|
|
|
#define STCP_MAX_MSG_SIZE 65535
|
|
#define STCP_RECV_BUF_INIT 8192
|
|
#define STCP_RECV_BUF_MAX 131072
|
|
#define STCP_HS_TIMEOUT 5000
|
|
#define STCP_CONNECT_TIMEOUT 10000
|
|
|
|
#define STCP_HS_CLIENT_MIN (SC_PUBKEY_ENC_SIZE + 6) // 72 + 2(padding_size enc) + 4(crc) = 78
|
|
#define STCP_HS_SERVER_MIN (SC_PUBKEY_ENC_SIZE + 7) // 72 + 1(status) + 2(padding_size) + 4(crc) = 79
|
|
#define STCP_HS_ENC_CLIENT 6 // padding_size(2) + CRC32(4)
|
|
#define STCP_HS_ENC_SERVER 7 // status(1) + padding_size(2) + CRC32(4)
|
|
|
|
#define STCP_STREAM_CLIENT_SEND 0
|
|
#define STCP_STREAM_SERVER_SEND 1
|
|
|
|
enum stcp_state {
|
|
STCP_STATE_INIT,
|
|
STCP_STATE_HS_CLIENT_SENT,
|
|
STCP_STATE_HS_SERVER_WAIT,
|
|
STCP_STATE_DATA,
|
|
STCP_STATE_CLOSED,
|
|
STCP_STATE_ERROR
|
|
};
|
|
|
|
struct stcp_conn {
|
|
socket_t sock;
|
|
struct UASYNC *ua;
|
|
void *socket_id;
|
|
|
|
enum stcp_state state;
|
|
uint8_t is_server;
|
|
uint8_t allocated; // 1 = allocated by u_calloc, free in stcp_conn_free
|
|
|
|
uint8_t session_key[SC_SESSION_KEY_SIZE];
|
|
struct sc_stream_state stream_send;
|
|
struct sc_stream_state stream_recv;
|
|
|
|
struct SC_MYKEYS my_keys;
|
|
uint8_t peer_pubkey[SC_PUBKEY_SIZE];
|
|
uint8_t peer_pubkey_set;
|
|
|
|
struct ll_queue *rx_queue;
|
|
struct ll_queue *tx_queue;
|
|
void (*tx_cb)(struct ll_queue *q, void *arg);
|
|
|
|
uint8_t *recv_buf;
|
|
size_t recv_buf_len;
|
|
size_t recv_buf_cap;
|
|
|
|
uint8_t *send_buf;
|
|
size_t send_len;
|
|
size_t send_offset;
|
|
|
|
size_t hs_expected_len;
|
|
uint8_t hs_key_processed;
|
|
|
|
void (*on_ready)(struct stcp_conn *conn, void *arg);
|
|
void *ready_arg;
|
|
|
|
void (*on_close)(struct stcp_conn *conn, int err, void *arg);
|
|
void *close_arg;
|
|
};
|
|
|
|
void stcp_conn_free(struct stcp_conn *c);
|
|
void stcp_conn_set_tx_queue(struct stcp_conn *c, struct ll_queue *q);
|
|
void stcp_conn_set_rx_queue(struct stcp_conn *c, struct ll_queue *q);
|
|
void stcp_conn_set_on_close(struct stcp_conn *c, void (*cb)(struct stcp_conn *conn, int err, void *arg), void *arg);
|
|
|
|
#endif
|