//
// BN_bntest_rand from 0x8709b8 to 0x870c1c (612 bytes)
//
// 1 xrefs:
// > ??> @ 0x69dfc
//
// This function is hookable on this platform!
//
// Using BromaIDA 8.0.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit ba9f177b at Thu Jul 16 22:52:06 2026 from https://github.com/geode-sdk/bindings
//
int __fastcall BN_bntest_rand(int *a1, int a2, int a3, int a4)
{
bool v7; // zf
_BOOL4 v9; // r9
int v10; // r6
int v12; // r1
int v13; // r10
unsigned __int8 *v14; // r11
int v15; // r5
int v16; // r3
int v17; // r2
int v18; // r5
unsigned __int8 *v19; // [sp+Ch] [bp-10h]
unsigned __int8 v20; // [sp+13h] [bp-9h] BYREF
time_t timer[2]; // [sp+14h] [bp-8h] BYREF
if ( a2 )
{
if ( a2 < 0 )
goto LABEL_5;
v9 = a2 == 1;
if ( a3 <= 0 )
v9 = 0;
if ( v9 )
goto LABEL_5;
v10 = (a2 + 7) >> 3;
v19 = (unsigned __int8 *)CRYPTO_malloc((void *)v10, (int)"crypto/bn/bn_rand.c", 36);
if ( v19 )
{
time(timer);
RAND_add((int)timer, 4, 0, 0);
if ( RAND_bytes((int)v19, (a2 + 7) >> 3) > 0 )
{
if ( v10 <= 0 )
{
LABEL_25:
v15 = ((_BYTE)a2 - 1) & 7;
if ( a3 < 0 )
{
v16 = *v19;
}
else if ( a3 )
{
if ( v15 )
{
v12 = 3;
v16 = (unsigned __int8)(*v19 | (3 << (v15 - 1)));
}
else
{
v12 = (int)v19;
v16 = 1;
v19[1] = ~((unsigned int)~(v19[1] << 25) >> 25);
}
}
else
{
v16 = (unsigned __int8)(*v19 | (1 << v15));
}
v17 = 255;
v18 = v16 & ~(255 << (v15 + 1));
if ( a4 )
{
v17 = v10 - 1;
v12 = (int)v19;
}
*v19 = v18;
if ( a4 )
*(_BYTE *)(v12 + v17) = v19[v17] | 1;
v9 = BN_bin2bn(v19, v10, a1) != nullptr;
}
else
{
v13 = 0;
v14 = v19 - 1;
while ( RAND_bytes((int)&v20, 1) > 0 )
{
v12 = v20;
if ( (v20 & 0x80) != 0 )
{
if ( v13 > 0 )
{
v12 = *v14;
v14[1] = v12;
}
}
else if ( v20 > 0x29u )
{
if ( v20 <= 0x53u )
v14[1] = -1;
}
else
{
v14[1] = 0;
}
++v13;
++v14;
if ( v13 == v10 )
goto LABEL_25;
}
v9 = 0;
}
}
}
else
{
ERR_put_error(3, 127, 65, (int)"crypto/bn/bn_rand.c", 38);
v9 = 0;
}
CRYPTO_clear_free(v19, v10, (int)"crypto/bn/bn_rand.c", 87);
return v9;
}
else
{
v7 = a3 == -1;
if ( a3 == -1 )
v7 = a4 == 0;
if ( !v7 )
{
LABEL_5:
ERR_put_error(3, 127, 118, (int)"crypto/bn/bn_rand.c", 92);
return 0;
}
BN_set_word((int **)a1, 0);
return 1;
}
}