implemented EncryptedSocket

This commit is contained in:
2025-10-25 16:43:14 +05:00
parent e0d9bfdcb3
commit eea36ec2a3
8 changed files with 125 additions and 21 deletions

View File

@@ -0,0 +1,49 @@
#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);
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);
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)
{
Deferral(4);
try(i32 r, i, socket_recv(ptr->sock, encrypted_buf));
encrypted_buf.size = r;
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,
NULLABLE(EndpointIPv4*) remote_end)
{
Deferral(4);
try(i32 r, i, socket_recvfrom(ptr->sock, encrypted_buf, remote_end));
encrypted_buf.size = r;
DecryptorAES_decrypt(&ptr->dec, encrypted_buf, decrypted_buf, (u32*)&r);
Return RESULT_VALUE(i, r);
}

View File

@@ -0,0 +1,25 @@
#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);
Result(i32) EncryptedSocket_recvfrom(EncryptedSocket* ptr,
Array(u8) encrypted_buf, Array(u8) decrypted_buf,
NULLABLE(EndpointIPv4*) remote_end);