implemented some socket functions

This commit is contained in:
2025-07-21 18:55:40 +03:00
parent bce385a4b5
commit 1e41ab49e5
11 changed files with 213 additions and 23 deletions

125
src/cryptography/AES.c Executable file
View File

@@ -0,0 +1,125 @@
#include "cryptography.h"
#include "BearSSL/inc/bearssl_block.h"
#include <assert.h>
// size must be multiple of 16
#define __AES_BUFFER_SIZE 256
typedef struct EncryptorAES {
br_aes_ct64_cbcenc_keys enc_ctx;
Array buf;
Array iv;
} EncryptorAES;
typedef struct DecryptorAES {
br_aes_ct64_cbcdec_keys dec_ctx;
Array buf;
Array iv;
} DecryptorAES;
EncryptorAES* EncryptorAES_create(Array key){
assert(key.size == 16 || key.size == 24 || key.size == 32);
EncryptorAES* ptr = (EncryptorAES*)malloc(sizeof(EncryptorAES));
br_aes_ct64_cbcenc_init(&ptr->enc_ctx, key.data, key.size);
// size must be multiple of 16
ptr->buf = Array_alloc(u8, __AES_BUFFER_SIZE);
Array_memset(&ptr->buf, 0);
// size of one SHA block (16 bytes)
ptr->iv = Array_alloc(u8, 16);
Array_memset(&ptr->iv, 0);
return ptr;
}
void EncryptorAES_destroy(EncryptorAES* ptr){
free(ptr->buf.data);
free(ptr->iv.data);
}
void EncryptorAES_encrypt(EncryptorAES* ptr, Array src, Array dst){
assert(dst.size >= EncryptorAES_calcDstSize(src.size));
const EncryptedBlockInfo block_info = { .padding_size = 16 - src.size % 16 };
memcpy(ptr->buf.data, &block_info, sizeof(EncryptedBlockInfo));
// encrypt this struct
br_aes_ct64_cbcenc_run(&ptr->enc_ctx, ptr->iv.data, ptr->buf.data, sizeof(EncryptedBlockInfo));
// emit EncryptedBlockInfo to beginning of result
memcpy(dst.data, ptr->buf.data, sizeof(EncryptedBlockInfo));
dst.data += sizeof(EncryptedBlockInfo);
dst.size -= sizeof(EncryptedBlockInfo);
// write full blocks
while(src.size > ptr->buf.size){
memcpy(ptr->buf.data, src.data, ptr->buf.size);
src.data += ptr->buf.size;
src.size -= ptr->buf.size;
br_aes_ct64_cbcenc_run(&ptr->enc_ctx, ptr->iv.data, ptr->buf.data, ptr->buf.size);
memcpy(dst.data, ptr->buf.data, ptr->buf.size);
dst.data += ptr->buf.size;
dst.size -= ptr->buf.size;
}
// write incomplete block
Array_memset(&ptr->buf, 0);
memcpy(ptr->buf.data, src.data, src.size);
u32 src_size_padded = src.size + block_info.padding_size;
br_aes_ct64_cbcenc_run(&ptr->enc_ctx, ptr->iv.data, ptr->buf.data, src_size_padded);
memcpy(dst.data, ptr->buf.data, src_size_padded);
}
DecryptorAES* DecryptorAES_create(Array key){
assert(key.size == 16 || key.size == 24 || key.size == 32);
DecryptorAES* ptr = (DecryptorAES*)malloc(sizeof(DecryptorAES));
br_aes_ct64_cbcdec_init(&ptr->dec_ctx, key.data, key.size);
// size must be multiple of 16
ptr->buf = Array_alloc(u8, __AES_BUFFER_SIZE);
Array_memset(&ptr->buf, 0);
// size of one SHA block (16 bytes)
ptr->iv = Array_alloc(u8, 16);
Array_memset(&ptr->iv, 0);
return ptr;
}
void DecryptorAES_destroy(DecryptorAES* ptr){
free(ptr->buf.data);
free(ptr->iv.data);
}
void DecryptorAES_decrypt(DecryptorAES* ptr, Array src, Array dst, u32* decrypted_size){
assert(dst.size >= src.size);
// read EncryptedBlockInfo from beginning of data
EncryptedBlockInfo block_info;
memcpy(&block_info, src.data, sizeof(EncryptedBlockInfo));
src.data += sizeof(EncryptedBlockInfo);
src.size -= sizeof(EncryptedBlockInfo);
// decrypt this struct
br_aes_ct64_cbcdec_run(&ptr->dec_ctx, ptr->iv.data, &block_info, sizeof(EncryptedBlockInfo));
*decrypted_size = src.size - block_info.padding_size;
// write full blocks
while(src.size > ptr->buf.size){
memcpy(ptr->buf.data, src.data, ptr->buf.size);
src.data += ptr->buf.size;
src.size -= ptr->buf.size;
br_aes_ct64_cbcdec_run(&ptr->dec_ctx, ptr->iv.data, ptr->buf.data, ptr->buf.size);
memcpy(dst.data, ptr->buf.data, ptr->buf.size);
dst.data += ptr->buf.size;
dst.size -= ptr->buf.size;
}
// write incomplete block
Array_memset(&ptr->buf, 0);
memcpy(ptr->buf.data, src.data, src.size);
u32 src_size_padded = src.size + block_info.padding_size;
br_aes_ct64_cbcdec_run(&ptr->dec_ctx, ptr->iv.data, ptr->buf.data, src_size_padded);
memcpy(dst.data, ptr->buf.data, src_size_padded);
}

51
src/cryptography/cryptography.h Executable file
View File

@@ -0,0 +1,51 @@
#pragma once
#include "tlibc/std.h"
#include "tlibc/collections/Array.h"
#include "tlibc/string/str.h"
/// @brief hashes password multiple times using its own hash as salt
/// @param password some byte array
/// @param iterations number of iterations
/// @return Array<u8, 32>
Array hash_password(str password, i32 iterations);
typedef struct EncryptedBlockInfo {
u32 padding_size;
u32 _reserved;
u64 __reserved;
} EncryptedBlockInfo;
typedef struct EncryptorAES EncryptorAES;
/// @param key Array<u8, 16 | 24 | 32>
EncryptorAES* EncryptorAES_create(Array key);
void EncryptorAES_destroy(EncryptorAES* ptr);
/// @brief Encrypts `src` and writes output to `dst`.
/// @param src array of any size
/// @param dst array of size >= EncryptorAES_calcDstSize(src.size)
void EncryptorAES_encrypt(EncryptorAES* ptr, Array src, Array dst);
#define EncryptorAES_calcDstSize(SRC_SIZE) (ALIGN_TO(SRC_SIZE, 16) + sizeof(EncryptedBlockInfo))
typedef struct DecryptorAES DecryptorAES;
/// @param key Array<u8, 16 | 24 | 32>
DecryptorAES* DecryptorAES_create(Array key);
void DecryptorAES_destroy(DecryptorAES* ptr);
/// @brief Decrypts `src` and writes output to `dst`.
/// @param src array of any size
/// @param dst array of size >= src.size
/// @param decrypted_size size of original data without padding added by EncryptorAES_encrypt
void DecryptorAES_decrypt(DecryptorAES* ptr, Array src, Array dst, u32* decrypted_size);
typedef struct EncryptorRSA EncryptorRSA;
typedef struct DecryptorRSA DecryptorRSA;

18
src/cryptography/hash.c Executable file
View File

@@ -0,0 +1,18 @@
#include "cryptography.h"
#include "BearSSL/inc/bearssl_hash.h"
Array hash_password(str password, i32 iterations){
Array hash_buffer = Array_alloc(u8, br_sha256_SIZE);
Array_memset(&hash_buffer, 0);
br_sha256_context sha256_ctx;
br_sha256_init(&sha256_ctx);
for(i32 i = 0; i < iterations; i++){
br_sha256_update(&sha256_ctx, password.data, password.size);
br_sha256_out(&sha256_ctx, hash_buffer.data);
br_sha256_update(&sha256_ctx, hash_buffer.data, hash_buffer.size);
}
br_sha256_out(&sha256_ctx, hash_buffer.data);
return hash_buffer;
}