implemented EncryptedSocket
This commit is contained in:
49
src/network/EncryptedSocket.c
Normal file
49
src/network/EncryptedSocket.c
Normal 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);
|
||||
}
|
||||
25
src/network/EncryptedSocket.h
Normal file
25
src/network/EncryptedSocket.h
Normal 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);
|
||||
Reference in New Issue
Block a user