cryptography rework and beginning of tcp-chat-protocol

This commit is contained in:
2025-10-31 23:19:08 +05:00
parent 42702ffbe7
commit a0bcd2560a
24 changed files with 682 additions and 283 deletions

View File

@@ -1,50 +0,0 @@
#include "EncryptedSocket.h"
void EncryptedSocket_construct(EncryptedSocket* ptr, Socket sock, Array(u8) aes_key)
{
ptr->sock = sock;
EncryptorAES_construct(&ptr->enc, aes_key);
DecryptorAES_construct(&ptr->dec, aes_key);
}
Result(void) EncryptedSocket_send(EncryptedSocket* ptr,
Array(u8) decrypted_buf, Array(u8) encrypted_buf)
{
Deferral(4);
try_void(EncryptorAES_encrypt(&ptr->enc, decrypted_buf, encrypted_buf));
try_void(socket_send(ptr->sock, encrypted_buf));
Return RESULT_VOID;
}
Result(void) EncryptedSocket_sendto(EncryptedSocket* ptr,
Array(u8) decrypted_buf, Array(u8) encrypted_buf,
EndpointIPv4 remote_end)
{
Deferral(4);
try_void(EncryptorAES_encrypt(&ptr->enc, decrypted_buf, encrypted_buf));
try_void(socket_sendto(ptr->sock, encrypted_buf, remote_end));
Return RESULT_VOID;
}
Result(i32) EncryptedSocket_recv(EncryptedSocket* ptr,
Array(u8) encrypted_buf, Array(u8) decrypted_buf,
SocketRecvFlag flags)
{
Deferral(4);
try(i32 r, i, socket_recv(ptr->sock, encrypted_buf, flags));
encrypted_buf.size = r;
try_void(DecryptorAES_decrypt(&ptr->dec, encrypted_buf, decrypted_buf, (u32*)&r));
Return RESULT_VALUE(i, r);
}
Result(i32) EncryptedSocket_recvfrom(EncryptedSocket* ptr,
Array(u8) encrypted_buf, Array(u8) decrypted_buf,
SocketRecvFlag flags, NULLABLE(EndpointIPv4*) remote_end)
{
Deferral(4);
try(i32 r, i, socket_recvfrom(ptr->sock, encrypted_buf, flags, remote_end));
encrypted_buf.size = r;
try_void(DecryptorAES_decrypt(&ptr->dec, encrypted_buf, decrypted_buf, (u32*)&r));
Return RESULT_VALUE(i, r);
}

View File

@@ -1,27 +0,0 @@
#pragma once
#include "network/socket.h"
#include "cryptography/cryptography.h"
typedef struct EncryptedSocket {
Socket sock;
EncryptorAES enc;
DecryptorAES dec;
} EncryptedSocket;
void EncryptedSocket_construct(EncryptedSocket* ptr, Socket sock, Array(u8) aes_key);
Result(void) EncryptedSocket_send(EncryptedSocket* ptr,
Array(u8) decrypted_buf, Array(u8) encrypted_buf);
Result(void) EncryptedSocket_sendto(EncryptedSocket* ptr,
Array(u8) decrypted_buf, Array(u8) encrypted_buf,
EndpointIPv4 remote_end);
Result(i32) EncryptedSocket_recv(EncryptedSocket* ptr,
Array(u8) encrypted_buf, Array(u8) decrypted_buf,
SocketRecvFlag flags);
Result(i32) EncryptedSocket_recvfrom(EncryptedSocket* ptr,
Array(u8) encrypted_buf, Array(u8) decrypted_buf,
SocketRecvFlag flags,
NULLABLE(EndpointIPv4*) remote_end);

View File

@@ -0,0 +1,61 @@
#include "encrypted_sockets.h"
void EncryptedSocketTCP_construct(EncryptedSocketTCP* ptr, Socket sock, Array(u8) aes_key)
{
ptr->sock = sock;
AESStreamEncryptor_construct(&ptr->enc, aes_key, AESStream_DEFAULT_CLASS);
AESStreamDecryptor_construct(&ptr->dec, aes_key, AESStream_DEFAULT_CLASS);
}
Result(void) EncryptedSocketTCP_send(EncryptedSocketTCP* ptr,
Array(u8) decrypted_buf, Array(u8) encrypted_buf)
{
Deferral(4);
try(u32 encrypted_size, u, AESStreamEncryptor_encrypt(&ptr->enc, decrypted_buf, encrypted_buf));
encrypted_buf.size = encrypted_size;
try_void(socket_send(ptr->sock, encrypted_buf));
Return RESULT_VOID;
}
Result(i32) EncryptedSocketTCP_recv(EncryptedSocketTCP* ptr,
Array(u8) encrypted_buf, Array(u8) decrypted_buf,
SocketRecvFlag flags)
{
Deferral(4);
try(i32 received_size, i, socket_recv(ptr->sock, encrypted_buf, flags));
encrypted_buf.size = received_size;
try(i32 decrypted_size, u, AESStreamDecryptor_decrypt(&ptr->dec, encrypted_buf, decrypted_buf));
Return RESULT_VALUE(i, decrypted_size);
}
void EncryptedSocketUDP_construct(EncryptedSocketUDP* ptr, Socket sock, Array(u8) aes_key)
{
ptr->sock = sock;
AESBlockEncryptor_construct(&ptr->enc, aes_key, AESBlockEncryptor_DEFAULT_CLASS);
AESBlockDecryptor_construct(&ptr->dec, aes_key, AESBlockDecryptor_DEFAULT_CLASS);
}
Result(void) EncryptedSocketUDP_sendto(EncryptedSocketUDP* ptr,
Array(u8) decrypted_buf, Array(u8) encrypted_buf,
EndpointIPv4 remote_end)
{
Deferral(4);
try(u32 encrypted_size, u, AESBlockEncryptor_encrypt(&ptr->enc, decrypted_buf, encrypted_buf));
encrypted_buf.size = encrypted_size;
try_void(socket_sendto(ptr->sock, encrypted_buf, remote_end));
Return RESULT_VOID;
}
Result(i32) EncryptedSocketUDP_recvfrom(EncryptedSocketUDP* ptr,
Array(u8) encrypted_buf, Array(u8) decrypted_buf,
SocketRecvFlag flags, NULLABLE(EndpointIPv4*) remote_end)
{
Deferral(4);
try(i32 received_size, i, socket_recvfrom(ptr->sock, encrypted_buf, flags, remote_end));
encrypted_buf.size = received_size;
try(i32 decrypted_size, u, AESBlockDecryptor_decrypt(&ptr->dec, encrypted_buf, decrypted_buf));
Return RESULT_VALUE(i, decrypted_size);
}

View File

@@ -0,0 +1,44 @@
#pragma once
#include "network/socket.h"
#include "cryptography/AES.h"
//////////////////////////////////////////////////////////////////////////////
// EncryptedSocketTCP //
//////////////////////////////////////////////////////////////////////////////
typedef struct EncryptedSocketTCP {
Socket sock;
AESStreamEncryptor enc;
AESStreamDecryptor dec;
} EncryptedSocketTCP;
void EncryptedSocketTCP_construct(EncryptedSocketTCP* ptr, Socket sock, Array(u8) aes_key);
Result(void) EncryptedSocketTCP_send(EncryptedSocketTCP* ptr,
Array(u8) decrypted_buf, Array(u8) encrypted_buf);
Result(void) EncryptedSocketTCP_recv(EncryptedSocketTCP* ptr,
Array(u8) encrypted_buf, Array(u8) decrypted_buf,
SocketRecvFlag flags);
//////////////////////////////////////////////////////////////////////////////
// EncryptedSocketUDP //
//////////////////////////////////////////////////////////////////////////////
typedef struct EncryptedSocketUDP {
Socket sock;
AESBlockEncryptor enc;
AESBlockDecryptor dec;
} EncryptedSocketUDP;
void EncryptedSocketUDP_construct(EncryptedSocketUDP* ptr, Socket sock, Array(u8) aes_key);
Result(void) EncryptedSocketUDP_sendto(EncryptedSocketUDP* ptr,
Array(u8) decrypted_buf, Array(u8) encrypted_buf,
EndpointIPv4 remote_end);
Result(i32) EncryptedSocketUDP_recvfrom(EncryptedSocketUDP* ptr,
Array(u8) encrypted_buf, Array(u8) decrypted_buf,
SocketRecvFlag flags,
NULLABLE(EndpointIPv4*) remote_end);

View File

@@ -0,0 +1,17 @@
#include "constant.h"
const Magic64 PacketHeader_MAGIC = { .bytes = { 't', 'c', 'p', '-', 'c', 'h', 'a', 't' } };
Result(void) PacketHeader_validateMagic(PacketHeader* ptr){
if (ptr->magic.n != PacketHeader_MAGIC.n){
return RESULT_ERROR("invalid packet magic", false);
}
return RESULT_VOID;
}
void PacketHeader_construct(PacketHeader* ptr, u8 protocol_version, u16 type, u64 content_size){
ptr->magic.n = PacketHeader_MAGIC.n;
ptr->protocol_version = protocol_version;
ptr->type = type;
ptr->content_size = content_size;
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include "tlibc/errors.h"
#include "magic.h"
#define AES_SESSION_KEY_SIZE 32
extern const Magic64 PacketHeader_MAGIC;
typedef struct PacketHeader {
Magic64 magic;
u8 protocol_version;
u8 _reserved;
u16 type;
u64 content_size;
} __attribute__((aligned(64))) PacketHeader;
void PacketHeader_construct(PacketHeader* ptr, u8 protocol_version, u16 type, u64 content_size);
Result(void) PacketHeader_validateMagic(PacketHeader* ptr);

View File

@@ -0,0 +1,10 @@
#include "v1.h"
Result(void) ClientHandshake_tryConstruct(ClientHandshake* ptr, Array(u8) session_key){
Deferral(1);
try_assert(session_key.size == AES_SESSION_KEY_SIZE);
PacketHeader_construct(&ptr->header, PROTOCOL_VERSION, PacketType_ClientHandshake, session_key.size);
memcpy(ptr->session_key, session_key.data, session_key.size);
Return RESULT_VOID;
}

View File

@@ -0,0 +1,30 @@
#pragma once
#include "tlibc/errors.h"
#include "network/tcp-chat-protocol/constant.h"
#define PROTOCOL_VERSION 1 /* 1.0.0 */
typedef enum PacketType {
PacketType_Invalid,
PacketType_ErrorMessage,
PacketType_ClientHandshake,
PacketType_ServerHandshake,
} __attribute__((__packed__)) PacketType;
typedef struct ErrorMessage {
PacketHeader header;
/* content stream of size `header.content_size` */
} ErrorMessage;
typedef struct ClientHandshake {
PacketHeader header;
u8 session_key[AES_SESSION_KEY_SIZE];
} ClientHandshake;
Result(void) ClientHandshake_tryConstruct(ClientHandshake* ptr, Array(u8) session_key);
typedef struct ServerHandshake {
PacketHeader header;
u64 session_id;
} ServerHandshake;