rsa key allocation on heap

This commit is contained in:
Timerix 2025-09-19 17:49:17 +05:00
parent 98ae36ad2f
commit 7ef2eb31ee
3 changed files with 13 additions and 15 deletions

View File

@ -5,13 +5,12 @@
// https://crypto.stackexchange.com/questions/3110/impacts-of-not-using-rsa-exponent-of-65537 // https://crypto.stackexchange.com/questions/3110/impacts-of-not-using-rsa-exponent-of-65537
#define DEFAULT_PUBLIC_EXPONENT 65537 #define DEFAULT_PUBLIC_EXPONENT 65537
bool RSA_generateKeyPair(u32 key_size, bool RSA_generateKeyPair(u32 key_size, br_rsa_private_key* sk, br_rsa_public_key* pk){
br_rsa_private_key* sk, void* sk_buf,
NULLABLE(br_rsa_public_key*) pk, NULLABLE(void* pk_buf))
{
br_hmac_drbg_context rng_ctx; br_hmac_drbg_context rng_ctx;
const br_prng_class** rng_class_ptr = &rng_ctx.vtable; const br_prng_class** rng_class_ptr = &rng_ctx.vtable;
rng_init_sha256_seedFromTime(&br_hmac_drbg_vtable, rng_class_ptr); rng_init_sha256_seedFromTime(&br_hmac_drbg_vtable, rng_class_ptr);
void* sk_buf = malloc(BR_RSA_KBUF_PRIV_SIZE(key_size));
void* pk_buf = malloc(BR_RSA_KBUF_PUB_SIZE(key_size));
u32 r = br_rsa_i31_keygen(rng_class_ptr, sk, sk_buf, pk, pk_buf, key_size, DEFAULT_PUBLIC_EXPONENT); u32 r = br_rsa_i31_keygen(rng_class_ptr, sk, sk_buf, pk, pk_buf, key_size, DEFAULT_PUBLIC_EXPONENT);
return r; return r;
} }

View File

@ -88,9 +88,15 @@ void DecryptorAES_decrypt(DecryptorAES* ptr, Array(u8) src, Array(u8) dst, u32*
// RSA.c // // RSA.c //
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
bool RSA_generateKeyPair(u32 key_size, bool RSA_generateKeyPair(u32 key_size, br_rsa_private_key* sk, br_rsa_public_key* pk);
br_rsa_private_key* sk, void* sk_buf,
NULLABLE(br_rsa_public_key*) pk, NULLABLE(void* pk_buf)); static inline void RSA_freePrivateKey(br_rsa_private_key* sk){
free(sk->p);
}
static inline void RSA_freePublicKey(br_rsa_public_key* sk){
free(sk->n);
}
/// @param sk some private key /// @param sk some private key
/// @param pk out public key. WARNING: .n is allocated on heap /// @param pk out public key. WARNING: .n is allocated on heap

View File

@ -15,16 +15,9 @@ int main(const int argc, cstr const* argv){
const u32 key_size = 2048; const u32 key_size = 2048;
br_rsa_private_key sk; br_rsa_private_key sk;
const u32 sk_buf_size = BR_RSA_KBUF_PRIV_SIZE(key_size);
u8 sk_buf[sk_buf_size];
memset(sk_buf, 0, sk_buf_size);
br_rsa_public_key pk; br_rsa_public_key pk;
const u32 pk_buf_size = BR_RSA_KBUF_PUB_SIZE(key_size);
u8 pk_buf[pk_buf_size];
memset(pk_buf, 0, pk_buf_size);
if(!RSA_generateKeyPair(key_size, &sk, sk_buf, &pk, pk_buf)){ if(!RSA_generateKeyPair(key_size, &sk, &pk)){
printfe("ERROR: can't generate RSA key pair\n"); printfe("ERROR: can't generate RSA key pair\n");
Return 1; Return 1;
} }