//
// BN_rand from 0x1097f08 to 0x1098118 (528 bytes)
//
// 10 xrefs:
// > sub_1005CF0 @ 0x1005eb8
// > BN_rand_range @ 0x1098664
// > BN_rand_range @ 0x10986b4
// > BN_GF2m_mod_solve_quad_arr @ 0x10f3dc8
// > bn_probable_prime_dh_retry @ 0x10f526c
// > bn_probable_prime_dh_coprime @ 0x10f5374
// > bn_probable_prime_dh @ 0x10f5488
// > BN_generate_prime_ex @ 0x10f590c
// > BN_generate_prime_ex @ 0x10f5a24
// > ??> @ 0x7db48
//
// This function is hookable on this platform (idp: arm, 32 bit: False, 64 bit: True!
//
// Using BromaIDA 8.3.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit 1c6ec395 at Thu Nov 26 21:29:22 2026 from https://github.com/geode-sdk/bindings
// Generated at Fri, 27 Nov 2026 13:17:24 +0000
//
__int64 __fastcall BN_rand(__int64 a1, int a2, int a3, int a4)
{
char v4; // w19
int v8; // w23
char *v9; // x22
long double v10; // q0
int v11; // w19
char v12; // w0
_BOOL4 v13; // w19
time_t timer; // [xsp+68h] [xbp+68h] BYREF
v4 = a2;
if ( a2 )
{
if ( a2 < 0 || a2 == 1 && a3 > 0 )
goto LABEL_4;
v8 = (a2 + 7) >> 3;
v9 = (char *)CRYPTO_malloc((void *)v8, (__int64)"crypto/bn/bn_rand.c", 36);
if ( v9 )
{
time(&timer);
*(_QWORD *)&v10 = 0;
RAND_add((__int64)&timer, 8, v10);
if ( (int)RAND_bytes((__int64)v9, (unsigned int)v8) <= 0 )
{
v13 = 0;
}
else
{
v11 = (v4 - 1) & 7;
if ( a3 < 0 )
{
v12 = *v9;
}
else if ( a3 )
{
if ( v11 )
{
v12 = (3 << (v11 - 1)) | *v9;
}
else
{
v12 = 1;
v9[1] |= 0x80u;
}
}
else
{
v12 = (1 << v11) | *v9;
}
*v9 = v12 & ~(-1 << (v11 + 1));
if ( a4 )
v9[v8 - 1] |= 1u;
v13 = BN_bin2bn(v9, v8, a1) != nullptr;
}
}
else
{
v13 = 0;
ERR_put_error(3, 127, 65, (__int64)"crypto/bn/bn_rand.c", 38);
}
CRYPTO_clear_free(v9, v8, (__int64)"crypto/bn/bn_rand.c", 0x57u);
return v13;
}
else
{
if ( a3 != -1 || a4 )
{
LABEL_4:
ERR_put_error(3, 127, 118, (__int64)"crypto/bn/bn_rand.c", 92);
return 0;
}
BN_set_word(a1, 0);
return 1;
}
}