//
// sub_82E968 from 0x82e968 to 0x82ee5c (1268 bytes)
//
// 1 xrefs:
// > ??> @ 0xac0e30
//
// 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 sub_82E968(size_t a1, void *a2, _BYTE *a3, int a4, int a5)
{
int v7; // r0
int v8; // r6
int *v9; // r7
int *v10; // r8
int v11; // r0
int v12; // r4
int v13; // r4
_BYTE *v14; // r0
bool v15; // zf
int v16; // r2
_BYTE *v17; // r9
int v19; // r0
int v20; // r3
int v21; // r10
int v22; // r11
int v23; // r0
int v24; // r1
signed int v25; // r2
_DWORD *v26; // r0
int v27; // r0
unsigned int v28; // [sp+Ch] [bp-10h]
size_t v30; // [sp+10h] [bp-Ch]
size_t v31; // [sp+10h] [bp-Ch]
v7 = BN_CTX_new(a1);
v8 = v7;
if ( !v7 )
{
v28 = 0;
v17 = nullptr;
v13 = -1;
goto LABEL_17;
}
BN_CTX_start(v7);
v9 = (int *)BN_CTX_get(v8);
v10 = (int *)BN_CTX_get(v8);
v11 = BN_num_bits(*(_DWORD **)(a4 + 16));
v12 = v11 + 7;
if ( v11 + 7 < 0 )
v12 = v11 + 14;
v13 = v12 >> 3;
v14 = CRYPTO_malloc((void *)v13, (int)"crypto/rsa/rsa_ossl.c", 244);
v15 = v9 == nullptr;
if ( v9 )
v15 = v10 == nullptr;
v16 = v15;
v28 = v13;
if ( !v14 )
v16 |= 1u;
v17 = v14;
if ( v16 )
{
ERR_put_error(4, 102, 65, (int)"crypto/rsa/rsa_ossl.c", 246);
v13 = -1;
goto LABEL_16;
}
switch ( a5 )
{
case 3:
v19 = RSA_padding_add_none((int)v14, v13, a2, a1);
goto LABEL_19;
case 5:
v19 = RSA_padding_add_X931((int)v14, v13, a2, a1);
LABEL_19:
if ( v19 <= 0 || !BN_bin2bn(v17, v13, v9) )
goto LABEL_56;
if ( BN_ucmp(v9, *(int **)(a4 + 16)) >= 0 )
{
ERR_put_error(4, 102, 132, (int)"crypto/rsa/rsa_ossl.c", 274);
v13 = -1;
goto LABEL_16;
}
v20 = *(_DWORD *)(a4 + 56);
if ( (v20 & 0x80) != 0 )
{
v21 = 0;
v22 = 0;
goto LABEL_25;
}
CRYPTO_THREAD_write_lock(*(_DWORD *)(a4 + 84));
v21 = *(_DWORD *)(a4 + 76);
if ( !v21 )
{
v21 = RSA_setup_blinding(a4, v8);
*(_DWORD *)(a4 + 76) = v21;
if ( !v21 )
{
CRYPTO_THREAD_unlock(*(_DWORD *)(a4 + 84));
LABEL_65:
ERR_put_error(4, 102, 68, (int)"crypto/rsa/rsa_ossl.c", 281);
v13 = -1;
goto LABEL_16;
}
}
if ( BN_BLINDING_is_current_thread(v21) )
{
CRYPTO_THREAD_unlock(*(_DWORD *)(a4 + 84));
v27 = BN_BLINDING_convert_ex(v9, 0, v21, v8);
v22 = 0;
}
else
{
v21 = *(_DWORD *)(a4 + 80);
if ( v21 )
{
CRYPTO_THREAD_unlock(*(_DWORD *)(a4 + 84));
}
else
{
v21 = RSA_setup_blinding(a4, v8);
*(_DWORD *)(a4 + 80) = v21;
CRYPTO_THREAD_unlock(*(_DWORD *)(a4 + 84));
if ( !v21 )
goto LABEL_65;
}
v22 = BN_CTX_get(v8);
if ( !v22 )
{
ERR_put_error(4, 102, 65, (int)"crypto/rsa/rsa_ossl.c", 288);
v13 = -1;
goto LABEL_16;
}
BN_BLINDING_lock(v21);
v31 = BN_BLINDING_convert_ex(v9, v22, v21, v8);
BN_BLINDING_unlock(v21);
v27 = v31;
}
if ( !v27 )
goto LABEL_56;
v20 = *(_DWORD *)(a4 + 56);
LABEL_25:
if ( (v20 & 0x20) == 0
&& (!*(_DWORD *)(a4 + 28)
|| !*(_DWORD *)(a4 + 32)
|| !*(_DWORD *)(a4 + 36)
|| !*(_DWORD *)(a4 + 40)
|| !*(_DWORD *)(a4 + 44)) )
{
v26 = (_DWORD *)BN_new();
v30 = (size_t)v26;
if ( !v26 )
{
ERR_put_error(4, 102, 65, (int)"crypto/rsa/rsa_ossl.c", 304);
v13 = -1;
goto LABEL_16;
}
BN_with_flags(v26, *(int **)(a4 + 24), 4);
if ( (*(_DWORD *)(a4 + 56) & 2) != 0
&& !BN_MONT_CTX_set_locked((_BYTE **)(a4 + 60), *(_DWORD *)(a4 + 84), *(int ***)(a4 + 16), v8)
|| !(*(int (__fastcall **)(int *, int *, size_t, _DWORD, int, _DWORD))(*(_DWORD *)(a4 + 8) + 24))(
v10,
v9,
v30,
*(_DWORD *)(a4 + 16),
v8,
*(_DWORD *)(a4 + 60)) )
{
BN_free(v30);
v13 = -1;
goto LABEL_16;
}
BN_free(v30);
goto LABEL_32;
}
if ( (*(int (__fastcall **)(int *, int *, int, int))(*(_DWORD *)(a4 + 8) + 20))(v10, v9, a4, v8) )
{
LABEL_32:
if ( !v21 || BN_BLINDING_invert_ex(v10, v22, v21, v8) )
{
if ( a5 == 5 )
{
BN_sub(v9, *(_DWORD *)(a4 + 16), v10);
if ( BN_cmp(v10, v9) > 0 )
v10 = v9;
}
v23 = BN_num_bits(v10);
v24 = v23 + 7;
if ( v23 + 7 < 0 )
v24 = v23 + 14;
v25 = v13 - BN_bn2bin(v10, &a3[v13 - (v24 >> 3)]);
if ( v25 > 0 )
memset(a3, 0, v25);
goto LABEL_16;
}
}
LABEL_56:
v13 = -1;
goto LABEL_16;
case 1:
v19 = RSA_padding_add_PKCS1_type_1();
goto LABEL_19;
}
ERR_put_error(4, 102, 118, (int)"crypto/rsa/rsa_ossl.c", 262);
v13 = -1;
LABEL_16:
BN_CTX_end(v8);
LABEL_17:
BN_CTX_free(v8);
CRYPTO_clear_free(v17, v28, (int)"crypto/rsa/rsa_ossl.c", 352);
return v13;
}