added rsa-gen mode

This commit is contained in:
2025-09-29 12:22:29 +05:00
parent f933d30863
commit b1ca05759e
6 changed files with 90 additions and 49 deletions

View File

@@ -11,20 +11,16 @@ Result(void) RSA_generateKeyPair(u32 key_size,
br_rsa_private_key* sk, br_rsa_public_key* pk,
const br_prng_class** rng_vtable_ptr)
{
Deferral(16);
Deferral(8);
bool success = false;
rng_init_sha256_seedFromTime(rng_vtable_ptr);
void* sk_buf = malloc(BR_RSA_KBUF_PRIV_SIZE(key_size));
Defer(
if(!success)
free(sk_buf)
);
void* pk_buf = malloc(BR_RSA_KBUF_PUB_SIZE(key_size));
Defer(
if(!success)
free(pk_buf)
if(!success){
free(sk_buf);
free(pk_buf);
}
);
success = br_rsa_i31_keygen(rng_vtable_ptr, sk, sk_buf, pk, pk_buf, key_size, DEFAULT_PUBLIC_EXPONENT);
@@ -35,8 +31,28 @@ Result(void) RSA_generateKeyPair(u32 key_size,
Return RESULT_VOID;
}
Result(void) RSA_generateKeyPairFromTime(u32 key_size,
br_rsa_private_key* sk, br_rsa_public_key* pk)
{
Deferral(8);
br_hmac_drbg_context time_based_rng = { .vtable = &br_hmac_drbg_vtable };
rng_init_sha256_seedFromTime(&time_based_rng.vtable);
try_void(RSA_generateKeyPair(key_size, sk, pk, &time_based_rng.vtable));
Return RESULT_VOID;
}
Result(void) RSA_generateKeyPairFromPassword(u32 key_size,
br_rsa_private_key* sk, br_rsa_public_key* pk, str password)
{
Deferral(8);
br_hmac_drbg_context password_based_rng = { .vtable = &br_hmac_drbg_vtable };
br_hmac_drbg_init(&password_based_rng, &br_sha256_vtable, password.data, password.size);
try_void(RSA_generateKeyPair(key_size, sk, pk, &password_based_rng.vtable));
Return RESULT_VOID;
}
Result(void) RSA_computePublicKey(const br_rsa_private_key* sk, br_rsa_public_key* pk){
Deferral(16);
Deferral(8);
br_rsa_compute_modulus compute_modulus = br_rsa_i31_compute_modulus;
br_rsa_compute_pubexp compute_pubexp = br_rsa_i31_compute_pubexp;

View File

@@ -18,6 +18,7 @@
void hash_password(Array(u8) password, u8* out_buffer, i32 iterations);
#define password_hash_size 32
#define __passhash_lvl_iter 1e5
//////////////////////////////////////////////////////////////////////////////
// rng.c //
@@ -86,6 +87,8 @@ void DecryptorAES_decrypt(DecryptorAES* ptr, Array(u8) src, Array(u8) dst, u32*
// RSA.c //
//////////////////////////////////////////////////////////////////////////////
#define __rsa_key_size_default 3072
/// @brief generate random key pair based on system time
/// @param key_size size of public key in bits (2048/3072/4096)
/// @param sk key for decryption
@@ -95,6 +98,12 @@ Result(void) RSA_generateKeyPair(u32 key_size,
br_rsa_private_key* sk, br_rsa_public_key* pk,
const br_prng_class** rng_vtable_ptr);
Result(void) RSA_generateKeyPairFromTime(u32 key_size,
br_rsa_private_key* sk, br_rsa_public_key* pk);
Result(void) RSA_generateKeyPairFromPassword(u32 key_size,
br_rsa_private_key* sk, br_rsa_public_key* pk, str password);
static inline void RSA_destroyPrivateKey(br_rsa_private_key* sk){
free(sk->p);
}