fixed connection bugs

This commit is contained in:
2025-11-02 15:26:30 +05:00
parent 8179609d47
commit 94fcbe5daf
9 changed files with 61 additions and 29 deletions

View File

@@ -176,7 +176,6 @@ void AESStreamDecryptor_construct(AESStreamDecryptor* ptr, Array(u8) key, const
Result(u32) AESStreamDecryptor_decrypt(AESStreamDecryptor* ptr, Array(u8) src, Array(u8) dst){
Deferral(4);
try_assert(src.size >= AESStreamEncryptor_calcDstSize(0));
u32 decrypted_size = __AESStreamDecryptor_calcDstSize(src.size);
try_assert(dst.size >= decrypted_size);

View File

@@ -88,6 +88,7 @@ typedef struct AESStreamEncryptor {
/// @param dec_class &br_aes_XXX_ctr_vtable
void AESStreamEncryptor_construct(AESStreamEncryptor* ptr, Array(u8) key, const br_block_ctr_class* ctr_class);
/// use this only at the beginning of the stream
#define AESStreamEncryptor_calcDstSize(src_size) (src_size + __AES_BLOCK_IV_SIZE)
/// @brief If ptr->block_counter == 0, writes random IV to `dst`. After that writes encrypted data to dst.
@@ -114,7 +115,7 @@ typedef struct AESStreamDecryptor {
void AESStreamDecryptor_construct(AESStreamDecryptor* ptr, Array(u8) key, const br_block_ctr_class* ctr_class);
/// @brief Reads IV from `src`, then decrypts data and writes it to dst
/// @param src array of size at least AESStreamEncryptor_calcDstSize(0).
/// @param src array of any size
/// @param dst array of size >= src.size
/// @return size of decrypted data
Result(u32) AESStreamDecryptor_decrypt(AESStreamDecryptor* ptr, Array(u8) src, Array(u8) dst);

View File

@@ -180,14 +180,15 @@ void RSAEncryptor_construct(RSAEncryptor* ptr, const br_rsa_public_key* pk){
}
Result(u32) RSAEncryptor_encrypt(RSAEncryptor* ptr, Array(u8) src, Array(u8) dst){
const u32 max_src_size = RSAEncryptor_calcMaxSrcSize(ptr->pk->nlen * 8, 256);
u32 key_size_bytes = ptr->pk->nlen;
const u32 max_src_size = RSAEncryptor_calcMaxSrcSize(key_size_bytes * 8, 256);
if(src.size > max_src_size){
return RESULT_ERROR_FMT("src.size (%u) must be <= %u (use RSAEncryptor_calcMaxSrcSize)",
src.size, max_src_size);
}
if(dst.size < ptr->pk->nlen){
if(dst.size < key_size_bytes){
return RESULT_ERROR_FMT("dst.size (%u) must be >= %u (key length in bytes)",
dst.size, (u32)ptr->pk->nlen);
dst.size, key_size_bytes);
}
size_t sz = br_rsa_i31_oaep_encrypt(
&ptr->rng.vtable, &br_sha256_vtable,
@@ -207,18 +208,26 @@ void RSADecryptor_construct(RSADecryptor* ptr, const br_rsa_private_key* sk){
ptr->sk = sk;
}
Result(u32) RSADecryptor_decrypt(RSADecryptor* ptr, Array(u8) buf){
if(buf.size != ptr->sk->n_bitlen/8){
return RESULT_ERROR_FMT("buf.size (%u) must be == %u (key length in bytes)",
buf.size, ptr->sk->n_bitlen/8);
Result(u32) RSADecryptor_decrypt(RSADecryptor* ptr, Array(u8) src, Array(u8) dst){
u32 key_size_bits = ptr->sk->n_bitlen;
if(src.size != key_size_bits/8){
return RESULT_ERROR_FMT("src.size (%u) must be == %u (key length in bytes)",
src.size, key_size_bits/8);
}
size_t sz = buf.size;
const u32 max_src_size = RSAEncryptor_calcMaxSrcSize(key_size_bits, 256);
if(dst.size < max_src_size){
return RESULT_ERROR_FMT("dst.size (%u) must be >= %u (use RSAEncryptor_calcMaxSrcSize)",
dst.size, max_src_size);
}
memcpy(dst.data, src.data, src.size);
size_t sz = src.size;
size_t r = br_rsa_i31_oaep_decrypt(
&br_sha256_vtable,
NULL, 0,
ptr->sk,
buf.data, &sz);
dst.data, &sz);
if(r == 0){
return RESULT_ERROR("RSA encryption failed", false);

View File

@@ -101,6 +101,7 @@ typedef struct RSADecryptor {
/// RSA OAEP encryption with SHA256 hashing algorithm
void RSADecryptor_construct(RSADecryptor* ptr, const br_rsa_private_key* sk);
/// @param buf buffer with size == key size in bytes
/// @param src buffer with size == key size in bytes
/// @param dst buffer with size >= `RSAEncryptor_calcMaxSrcSize(key_size_bits, 256)`
/// @return size of decrypted data
Result(u32) RSADecryptor_decrypt(RSADecryptor* ptr, Array(u8) buf);
Result(u32) RSADecryptor_decrypt(RSADecryptor* ptr, Array(u8) src, Array(u8) dst);