decompiled_code:
__int64 __fastcall pk_hashlen_helper(__int64 a1, _QWORD *a2)
{
__int64 result; // rax
unsigned __int8 v3; // al

result = 0LL;
if ( !*a2 )
{
v3 = mbedtls_hash_info_get_size();
*a2 = v3;
result = (unsigned int)-(v3 == 0LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2)
{
__int64 result; // rax
unsigned __int8 v3; // al

result = 0LL;
if ( !*a2 )
{
v3 = mbedtls_hash_info_get_size();
*a2 = v3;
result = (unsigned int)-(v3 == 0LL);
}
return result;
}

original_code:
static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
{
    if (*hash_len != 0) {
        return 0;
    }
    *hash_len = mbedtls_hash_info_get_size(md_alg);
    if (*hash_len == 0) {
        return -1;
    }
    return 0;
}

canonical_original_code:
static inline int pk_hashlen_helper(enum mbedtls_md_type_t md_alg, unsigned long *hash_len)
{
    if (*hash_len != 0) {
        return 0;
    }
    *hash_len = mbedtls_hash_info_get_size(md_alg);
    if (*hash_len == 0) {
        return -1;
    }
    return 0;
}


repo: rcr/rirc

decompiled_code:
void __fastcall mbedtls_pk_free(_QWORD *a1)
{
if ( a1 )
{
if ( *a1 )
(*(void (__fastcall **)(_QWORD))(*a1 + 80LL))(a1[1]);
mbedtls_platform_zeroize(a1, 16LL);
}
}

canonical_decompiled_code:
void __fastcall func2(_QWORD *a1)
{
if ( a1 )
{
if ( *a1 )
(*(void (__fastcall **)(_QWORD))(*a1 + 80LL))(a1[1]);
mbedtls_platform_zeroize(a1, 16LL);
}
}

original_code:
void mbedtls_pk_free(mbedtls_pk_context *ctx)
{
    if (ctx == ((void *)0)) {
        return;
    }
    if (ctx->pk_info != ((void *)0)) {
        ctx->pk_info->ctx_free_func(ctx->pk_ctx);
    }
    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
}

canonical_original_code:
void mbedtls_pk_free(struct mbedtls_pk_context *ctx)
{
    if (ctx == ((void *)0)) {
        return;
    }
    if (ctx->pk_info != ((void *)0)) {
        ctx->pk_info->ctx_free_func(ctx->pk_ctx);
    }
    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
}

variable_types: struct mbedtls_pk_context { struct mbedtls_pk_info_t { enum mbedtls_pk_type_t { MBEDTLS_PK_NONE=0, MBEDTLS_PK_RSA=1, MBEDTLS_PK_ECKEY=2, MBEDTLS_PK_ECKEY_DH=3, MBEDTLS_PK_ECDSA=4, MBEDTLS_PK_RSA_ALT=5, MBEDTLS_PK_RSASSA_PSS=6, MBEDTLS_PK_OPAQUE=7 } type; char *name; unsigned long (*get_bitlen)(void *); int (*can_do)(enum mbedtls_pk_type_t { MBEDTLS_PK_NONE=0, MBEDTLS_PK_RSA=1, MBEDTLS_PK_ECKEY=2, MBEDTLS_PK_ECKEY_DH=3, MBEDTLS_PK_ECDSA=4, MBEDTLS_PK_RSA_ALT=5, MBEDTLS_PK_RSASSA_PSS=6, MBEDTLS_PK_OPAQUE=7 } type); int (*verify_func)(void *ctx, enum mbedtls_md_type_t { MBEDTLS_MD_NONE=0, MBEDTLS_MD_MD5=1, MBEDTLS_MD_SHA1=2, MBEDTLS_MD_SHA224=3, MBEDTLS_MD_SHA256=4, MBEDTLS_MD_SHA384=5, MBEDTLS_MD_SHA512=6, MBEDTLS_MD_RIPEMD160=7 } md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_len); int (*sign_func)(void *ctx, enum mbedtls_md_type_t { MBEDTLS_MD_NONE=0, MBEDTLS_MD_MD5=1, MBEDTLS_MD_SHA1=2, MBEDTLS_MD_SHA224=3, MBEDTLS_MD_SHA256=4, MBEDTLS_MD_SHA384=5, MBEDTLS_MD_SHA512=6, MBEDTLS_MD_RIPEMD160=7 } md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_size, unsigned long *sig_len, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); int (*decrypt_func)(void *ctx, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); int (*encrypt_func)(void *ctx, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); int (*check_pair_func)(void *pub, void *prv, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); void *(*ctx_alloc_func)(); void (*ctx_free_func)(void *ctx); void (*debug_func)(void *ctx, struct mbedtls_pk_debug_item { enum mbedtls_pk_debug_type { MBEDTLS_PK_DEBUG_NONE=0, MBEDTLS_PK_DEBUG_MPI=1, MBEDTLS_PK_DEBUG_ECP=2 } type; char *name; void *value; } *items); } *pk_info; void *pk_ctx; } *

return_type: void

user_defined_types: struct mbedtls_pk_context { struct mbedtls_pk_info_t *pk_info; void *pk_ctx; }

user_defined_types: struct mbedtls_pk_info_t { enum mbedtls_pk_type_t type; char *name; unsigned long (*get_bitlen)(void *); int (*can_do)(enum mbedtls_pk_type_t type); int (*verify_func)(void *ctx, enum mbedtls_md_type_t md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_len); int (*sign_func)(void *ctx, enum mbedtls_md_type_t md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_size, unsigned long *sig_len, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); int (*decrypt_func)(void *ctx, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); int (*encrypt_func)(void *ctx, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); int (*check_pair_func)(void *pub, void *prv, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); void *(*ctx_alloc_func)(); void (*ctx_free_func)(void *ctx); void (*debug_func)(void *ctx, struct mbedtls_pk_debug_item *items); }

user_defined_types: struct mbedtls_pk_debug_item { enum mbedtls_pk_debug_type type; char *name; void *value; }


decompiled_code:
void *__fastcall mbedtls_pk_info_from_type(unsigned int a1)
{
void *result; // rax

if ( a1 == 3 )
return &mbedtls_eckeydh_info;
if ( a1 > 3 )
{
result = &mbedtls_ecdsa_info;
if ( a1 != 4 )
result = 0LL;
}
else
{
result = &mbedtls_rsa_info;
if ( a1 != 1 )
{
result = &mbedtls_eckey_info;
if ( a1 != 2 )
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(unsigned int a1)
{
void *result; // rax

if ( a1 == 3 )
return &mbedtls_eckeydh_info;
if ( a1 > 3 )
{
result = &mbedtls_ecdsa_info;
if ( a1 != 4 )
result = 0LL;
}
else
{
result = &mbedtls_rsa_info;
if ( a1 != 1 )
{
result = &mbedtls_eckey_info;
if ( a1 != 2 )
result = 0LL;
}
}
return result;
}

original_code:
const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
{
    switch (pk_type) {
        case MBEDTLS_PK_RSA:
            return &mbedtls_rsa_info;
        case MBEDTLS_PK_ECKEY:
            return &mbedtls_eckey_info;
        case MBEDTLS_PK_ECKEY_DH:
            return &mbedtls_eckeydh_info;
        case MBEDTLS_PK_ECDSA:
            return &mbedtls_ecdsa_info;
        default:
            return ((void *)0);
    }
}

canonical_original_code:
struct mbedtls_pk_info_t *mbedtls_pk_info_from_type(enum mbedtls_pk_type_t pk_type)
{
    switch (pk_type) {
        case MBEDTLS_PK_RSA:
            return &mbedtls_rsa_info;
        case MBEDTLS_PK_ECKEY:
            return &mbedtls_eckey_info;
        case MBEDTLS_PK_ECKEY_DH:
            return &mbedtls_eckeydh_info;
        case MBEDTLS_PK_ECDSA:
            return &mbedtls_ecdsa_info;
        default:
            return ((void *)0);
    }
}

return_type: struct mbedtls_pk_info_t { enum mbedtls_pk_type_t { MBEDTLS_PK_NONE=0, MBEDTLS_PK_RSA=1, MBEDTLS_PK_ECKEY=2, MBEDTLS_PK_ECKEY_DH=3, MBEDTLS_PK_ECDSA=4, MBEDTLS_PK_RSA_ALT=5, MBEDTLS_PK_RSASSA_PSS=6, MBEDTLS_PK_OPAQUE=7 } type; char *name; unsigned long (*get_bitlen)(void *); int (*can_do)(enum mbedtls_pk_type_t { MBEDTLS_PK_NONE=0, MBEDTLS_PK_RSA=1, MBEDTLS_PK_ECKEY=2, MBEDTLS_PK_ECKEY_DH=3, MBEDTLS_PK_ECDSA=4, MBEDTLS_PK_RSA_ALT=5, MBEDTLS_PK_RSASSA_PSS=6, MBEDTLS_PK_OPAQUE=7 } type); int (*verify_func)(void *ctx, enum mbedtls_md_type_t { MBEDTLS_MD_NONE=0, MBEDTLS_MD_MD5=1, MBEDTLS_MD_SHA1=2, MBEDTLS_MD_SHA224=3, MBEDTLS_MD_SHA256=4, MBEDTLS_MD_SHA384=5, MBEDTLS_MD_SHA512=6, MBEDTLS_MD_RIPEMD160=7 } md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_len); int (*sign_func)(void *ctx, enum mbedtls_md_type_t { MBEDTLS_MD_NONE=0, MBEDTLS_MD_MD5=1, MBEDTLS_MD_SHA1=2, MBEDTLS_MD_SHA224=3, MBEDTLS_MD_SHA256=4, MBEDTLS_MD_SHA384=5, MBEDTLS_MD_SHA512=6, MBEDTLS_MD_RIPEMD160=7 } md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_size, unsigned long *sig_len, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); int (*decrypt_func)(void *ctx, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); int (*encrypt_func)(void *ctx, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); int (*check_pair_func)(void *pub, void *prv, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng); void *(*ctx_alloc_func)(); void (*ctx_free_func)(void *ctx); void (*debug_func)(void *ctx, struct mbedtls_pk_debug_item { enum mbedtls_pk_debug_type { MBEDTLS_PK_DEBUG_NONE=0, MBEDTLS_PK_DEBUG_MPI=1, MBEDTLS_PK_DEBUG_ECP=2 } type; char *name; void *value; } *items); } *


original_code:
int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
{
    if (info == ((void *)0) || ctx->pk_info != ((void *)0)) {
        return -0x3E80;
    }
    if ((ctx->pk_ctx = info->ctx_alloc_func()) == ((void *)0)) {
        return -0x3F80;
    }
    ctx->pk_info = info;
    return 0;
}

canonical_original_code:
int mbedtls_pk_setup(struct mbedtls_pk_context *ctx, struct mbedtls_pk_info_t *info)
{
    if (info == ((void *)0) || ctx->pk_info != ((void *)0)) {
        return -0x3E80;
    }
    if ((ctx->pk_ctx = info->ctx_alloc_func()) == ((void *)0)) {
        return -0x3F80;
    }
    ctx->pk_info = info;
    return 0;
}

variable_types:
info
struct mbedtls_pk_info_t *

return_type: int

user_defined_types: enum mbedtls_pk_type_t { MBEDTLS_PK_NONE=0, MBEDTLS_PK_RSA=1, MBEDTLS_PK_ECKEY=2, MBEDTLS_PK_ECKEY_DH=3, MBEDTLS_PK_ECDSA=4, MBEDTLS_PK_RSA_ALT=5, MBEDTLS_PK_RSASSA_PSS=6, MBEDTLS_PK_OPAQUE=7 }

user_defined_types: enum mbedtls_md_type_t { MBEDTLS_MD_NONE=0, MBEDTLS_MD_MD5=1, MBEDTLS_MD_SHA1=2, MBEDTLS_MD_SHA224=3, MBEDTLS_MD_SHA256=4, MBEDTLS_MD_SHA384=5, MBEDTLS_MD_SHA512=6, MBEDTLS_MD_RIPEMD160=7 }

user_defined_types: enum mbedtls_pk_debug_type { MBEDTLS_PK_DEBUG_NONE=0, MBEDTLS_PK_DEBUG_MPI=1, MBEDTLS_PK_DEBUG_ECP=2 }


decompiled_code:
__int64 __fastcall mbedtls_pk_can_do(__int64 a1, unsigned int a2)
{
if ( !a1 )
return 0LL;
if ( *(_QWORD *)a1 )
return (*(__int64 (__fastcall **)(_QWORD))(*(_QWORD *)a1 + 24LL))(a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned int a2)
{
if ( !a1 )
return 0LL;
if ( *(_QWORD *)a1 )
return (*(__int64 (__fastcall **)(_QWORD))(*(_QWORD *)a1 + 24LL))(a2);
return 0LL;
}

original_code:
int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
{
    if (ctx == ((void *)0) || ctx->pk_info == ((void *)0)) {
        return 0;
    }
    return ctx->pk_info->can_do(type);
}

canonical_original_code:
int mbedtls_pk_can_do(struct mbedtls_pk_context *ctx, enum mbedtls_pk_type_t type)
{
    if (ctx == ((void *)0) || ctx->pk_info == ((void *)0)) {
        return 0;
    }
    return ctx->pk_info->can_do(type);
}

variable_types: enum mbedtls_pk_type_t { MBEDTLS_PK_NONE=0, MBEDTLS_PK_RSA=1, MBEDTLS_PK_ECKEY=2, MBEDTLS_PK_ECKEY_DH=3, MBEDTLS_PK_ECDSA=4, MBEDTLS_PK_RSA_ALT=5, MBEDTLS_PK_RSASSA_PSS=6, MBEDTLS_PK_OPAQUE=7 }


decompiled_code:
__int64 __fastcall mbedtls_pk_verify_restartable(_QWORD *a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 (__fastcall *v9)(_QWORD, _QWORD, __int64, __int64, __int64, __int64); // rax
__int64 result; // rax
__int64 v11[6]; // [rsp+8h] [rbp-30h] BYREF

v11[0] = a4;
if ( (a2 != 0 || a4 != 0) && !a3 )
return 4294951296LL;
if ( !*a1 )
return 4294951296LL;
if ( (unsigned int)pk_hashlen_helper(a2, v11) )
return 4294951296LL;
v9 = *(__int64 (__fastcall **)(_QWORD, _QWORD, __int64, __int64, __int64, __int64))(*a1 + 32LL);
if ( v9 )
result = v9(a1[1], a2, a3, v11[0], a5, a6);
else
result = 4294951168LL;
return result;
}

original_code:
int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
                                  mbedtls_md_type_t md_alg,
                                  const unsigned char *hash, size_t hash_len,
                                  const unsigned char *sig, size_t sig_len,
                                  mbedtls_pk_restart_ctx *rs_ctx)
{
    if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info == ((void *)0) ||
        pk_hashlen_helper(md_alg, &hash_len) != 0) {
        return -0x3E80;
    }
    (void) rs_ctx;
    if (ctx->pk_info->verify_func == ((void *)0)) {
        return -0x3F00;
    }
    return ctx->pk_info->verify_func(ctx->pk_ctx, md_alg, hash, hash_len,
                                     sig, sig_len);
}

canonical_original_code:
int mbedtls_pk_verify_restartable(struct mbedtls_pk_context *ctx, enum mbedtls_md_type_t md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_len, void *rs_ctx)
{
    if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info == ((void *)0) ||
        pk_hashlen_helper(md_alg, &hash_len) != 0) {
        return -0x3E80;
    }
    (void) rs_ctx;
    if (ctx->pk_info->verify_func == ((void *)0)) {
        return -0x3F00;
    }
    return ctx->pk_info->verify_func(ctx->pk_ctx, md_alg, hash, hash_len,
                                     sig, sig_len);
}

variable_types: enum mbedtls_md_type_t { MBEDTLS_MD_NONE=0, MBEDTLS_MD_MD5=1, MBEDTLS_MD_SHA1=2, MBEDTLS_MD_SHA224=3, MBEDTLS_MD_SHA256=4, MBEDTLS_MD_SHA384=5, MBEDTLS_MD_SHA512=6, MBEDTLS_MD_RIPEMD160=7 }

variable_types:
hash
unsigned char *

variable_types:
sig
unsigned char *


decompiled_code:
__int64 __fastcall mbedtls_pk_verify(_QWORD *a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
return mbedtls_pk_verify_restartable(a1, a2, a3, a4, a5, a6);
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
return func6(a1, a2, a3, a4, a5, a6);
}

original_code:
int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
                      const unsigned char *hash, size_t hash_len,
                      const unsigned char *sig, size_t sig_len)
{
    return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
                                         sig, sig_len, ((void *)0));
}

canonical_original_code:
int mbedtls_pk_verify(struct mbedtls_pk_context *ctx, enum mbedtls_md_type_t md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_len)
{
    return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
                                         sig, sig_len, ((void *)0));
}


decompiled_code:
__int64 __fastcall mbedtls_pk_verify_ext(unsigned int a1, __int64 a2, _QWORD *a3, unsigned int a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8)
{
if ( (a6 != 0 || a4 != 0) && !a5 )
return 4294951296LL;
if ( !*a3 )
return 4294951296LL;
if ( !(unsigned int)mbedtls_pk_can_do((__int64)a3, a1) )
return 4294951168LL;
if ( a1 == 6 )
return 4294952576LL;
if ( a2 )
return 4294951296LL;
return mbedtls_pk_verify(a3, a4, a5, a6, a7, a8);
}

original_code:
int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
                          mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
                          const unsigned char *hash, size_t hash_len,
                          const unsigned char *sig, size_t sig_len)
{
    if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (!mbedtls_pk_can_do(ctx, type)) {
        return -0x3F00;
    }
    if (type != MBEDTLS_PK_RSASSA_PSS) {
        if (options != ((void *)0)) {
            return -0x3E80;
        }
        return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
    }
    return -0x3980;
}

canonical_original_code:
int mbedtls_pk_verify_ext(enum mbedtls_pk_type_t type, void *options, struct mbedtls_pk_context *ctx, enum mbedtls_md_type_t md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_len)
{
    if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (!mbedtls_pk_can_do(ctx, type)) {
        return -0x3F00;
    }
    if (type != MBEDTLS_PK_RSASSA_PSS) {
        if (options != ((void *)0)) {
            return -0x3E80;
        }
        return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
    }
    return -0x3980;
}

variable_types:
options
void *


original_code:
int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
                                mbedtls_md_type_t md_alg,
                                const unsigned char *hash, size_t hash_len,
                                unsigned char *sig, size_t sig_size, size_t *sig_len,
                                int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
                                mbedtls_pk_restart_ctx *rs_ctx)
{
    if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info == ((void *)0) || pk_hashlen_helper(md_alg, &hash_len) != 0) {
        return -0x3E80;
    }
    (void) rs_ctx;
    if (ctx->pk_info->sign_func == ((void *)0)) {
        return -0x3F00;
    }
    return ctx->pk_info->sign_func(ctx->pk_ctx, md_alg,
                                   hash, hash_len,
                                   sig, sig_size, sig_len,
                                   f_rng, p_rng);
}

canonical_original_code:
int mbedtls_pk_sign_restartable(struct mbedtls_pk_context *ctx, enum mbedtls_md_type_t md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_size, unsigned long *sig_len, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng, void *rs_ctx)
{
    if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info == ((void *)0) || pk_hashlen_helper(md_alg, &hash_len) != 0) {
        return -0x3E80;
    }
    (void) rs_ctx;
    if (ctx->pk_info->sign_func == ((void *)0)) {
        return -0x3F00;
    }
    return ctx->pk_info->sign_func(ctx->pk_ctx, md_alg,
                                   hash, hash_len,
                                   sig, sig_size, sig_len,
                                   f_rng, p_rng);
}

variable_types: int (*)(void *, unsigned char *, unsigned long)


decompiled_code:
__int64 __fastcall mbedtls_pk_sign(_QWORD *a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9)
{
return mbedtls_pk_sign_restartable(a1, a2, a3, a4, a5, a6, a7, a8, a9);
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1, unsigned int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9)
{
return func9(a1, a2, a3, a4, a5, a6, a7, a8, a9);
}

original_code:
int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
                    const unsigned char *hash, size_t hash_len,
                    unsigned char *sig, size_t sig_size, size_t *sig_len,
                    int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
    return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
                                       sig, sig_size, sig_len,
                                       f_rng, p_rng, ((void *)0));
}

canonical_original_code:
int mbedtls_pk_sign(struct mbedtls_pk_context *ctx, enum mbedtls_md_type_t md_alg, unsigned char *hash, unsigned long hash_len, unsigned char *sig, unsigned long sig_size, unsigned long *sig_len, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
                                       sig, sig_size, sig_len,
                                       f_rng, p_rng, ((void *)0));
}


decompiled_code:
__int64 __fastcall mbedtls_pk_decrypt(_QWORD *a1)
{
__int64 (__fastcall *v1)(_QWORD); // rax
__int64 result; // rax

if ( !*a1 )
return 4294951296LL;
v1 = *(__int64 (__fastcall **)(_QWORD))(*a1 + 48LL);
if ( v1 )
result = v1(a1[1]);
else
result = 4294951168LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(_QWORD *a1)
{
__int64 (__fastcall *v1)(_QWORD); // rax
__int64 result; // rax

if ( !*a1 )
return 4294951296LL;
v1 = *(__int64 (__fastcall **)(_QWORD))(*a1 + 48LL);
if ( v1 )
result = v1(a1[1]);
else
result = 4294951168LL;
return result;
}

original_code:
int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
                       const unsigned char *input, size_t ilen,
                       unsigned char *output, size_t *olen, size_t osize,
                       int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
    if (ctx->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info->decrypt_func == ((void *)0)) {
        return -0x3F00;
    }
    return ctx->pk_info->decrypt_func(ctx->pk_ctx, input, ilen,
                                      output, olen, osize, f_rng, p_rng);
}

canonical_original_code:
int mbedtls_pk_decrypt(struct mbedtls_pk_context *ctx, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    if (ctx->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info->decrypt_func == ((void *)0)) {
        return -0x3F00;
    }
    return ctx->pk_info->decrypt_func(ctx->pk_ctx, input, ilen,
                                      output, olen, osize, f_rng, p_rng);
}

variable_types:
input
unsigned char *

variable_types:
ilen
unsigned long

variable_types:
output
unsigned char *

variable_types:
olen
unsigned long *

variable_types:
osize
unsigned long


decompiled_code:
__int64 __fastcall mbedtls_pk_encrypt(_QWORD *a1)
{
__int64 (__fastcall *v1)(_QWORD); // rax
__int64 result; // rax

if ( !*a1 )
return 4294951296LL;
v1 = *(__int64 (__fastcall **)(_QWORD))(*a1 + 56LL);
if ( v1 )
result = v1(a1[1]);
else
result = 4294951168LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1)
{
__int64 (__fastcall *v1)(_QWORD); // rax
__int64 result; // rax

if ( !*a1 )
return 4294951296LL;
v1 = *(__int64 (__fastcall **)(_QWORD))(*a1 + 56LL);
if ( v1 )
result = v1(a1[1]);
else
result = 4294951168LL;
return result;
}

original_code:
int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
                       const unsigned char *input, size_t ilen,
                       unsigned char *output, size_t *olen, size_t osize,
                       int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
    if (ctx->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info->encrypt_func == ((void *)0)) {
        return -0x3F00;
    }
    return ctx->pk_info->encrypt_func(ctx->pk_ctx, input, ilen,
                                      output, olen, osize, f_rng, p_rng);
}

canonical_original_code:
int mbedtls_pk_encrypt(struct mbedtls_pk_context *ctx, unsigned char *input, unsigned long ilen, unsigned char *output, unsigned long *olen, unsigned long osize, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    if (ctx->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info->encrypt_func == ((void *)0)) {
        return -0x3F00;
    }
    return ctx->pk_info->encrypt_func(ctx->pk_ctx, input, ilen,
                                      output, olen, osize, f_rng, p_rng);
}


original_code:
int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
                          const mbedtls_pk_context *prv,
                          int (*f_rng)(void *, unsigned char *, size_t),
                          void *p_rng)
{
    if (pub->pk_info == ((void *)0) ||
        prv->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (f_rng == ((void *)0)) {
        return -0x3E80;
    }
    if (prv->pk_info->check_pair_func == ((void *)0)) {
        return -0x3980;
    }
    if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
        if (pub->pk_info->type != MBEDTLS_PK_RSA) {
            return -0x3F00;
        }
    } else {
        if (pub->pk_info != prv->pk_info) {
            return -0x3F00;
        }
    }
    return prv->pk_info->check_pair_func(pub->pk_ctx, prv->pk_ctx, f_rng, p_rng);
}

canonical_original_code:
int mbedtls_pk_check_pair(struct mbedtls_pk_context *pub, struct mbedtls_pk_context *prv, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    if (pub->pk_info == ((void *)0) ||
        prv->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (f_rng == ((void *)0)) {
        return -0x3E80;
    }
    if (prv->pk_info->check_pair_func == ((void *)0)) {
        return -0x3980;
    }
    if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
        if (pub->pk_info->type != MBEDTLS_PK_RSA) {
            return -0x3F00;
        }
    } else {
        if (pub->pk_info != prv->pk_info) {
            return -0x3F00;
        }
    }
    return prv->pk_info->check_pair_func(pub->pk_ctx, prv->pk_ctx, f_rng, p_rng);
}

variable_types:
prv
struct mbedtls_pk_context *


decompiled_code:
__int64 __fastcall mbedtls_pk_get_bitlen(_QWORD *a1)
{
if ( !a1 )
return 0LL;
if ( *a1 )
return (*(__int64 (__fastcall **)(_QWORD))(*a1 + 16LL))(a1[1]);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(_QWORD *a1)
{
if ( !a1 )
return 0LL;
if ( *a1 )
return (*(__int64 (__fastcall **)(_QWORD))(*a1 + 16LL))(a1[1]);
return 0LL;
}

original_code:
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
{
    if (ctx == ((void *)0) || ctx->pk_info == ((void *)0)) {
        return 0;
    }
    return ctx->pk_info->get_bitlen(ctx->pk_ctx);
}

canonical_original_code:
unsigned long mbedtls_pk_get_bitlen(struct mbedtls_pk_context *ctx)
{
    if (ctx == ((void *)0) || ctx->pk_info == ((void *)0)) {
        return 0;
    }
    return ctx->pk_info->get_bitlen(ctx->pk_ctx);
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall mbedtls_pk_debug(_QWORD *a1)
{
void (__fastcall *v1)(_QWORD); // rax

if ( !*a1 )
return 4294951296LL;
v1 = *(void (__fastcall **)(_QWORD))(*a1 + 88LL);
if ( !v1 )
return 4294951168LL;
v1(a1[1]);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(_QWORD *a1)
{
void (__fastcall *v1)(_QWORD); // rax

if ( !*a1 )
return 4294951296LL;
v1 = *(void (__fastcall **)(_QWORD))(*a1 + 88LL);
if ( !v1 )
return 4294951168LL;
v1(a1[1]);
return 0LL;
}

original_code:
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
{
    if (ctx->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info->debug_func == ((void *)0)) {
        return -0x3F00;
    }
    ctx->pk_info->debug_func(ctx->pk_ctx, items);
    return 0;
}

canonical_original_code:
int mbedtls_pk_debug(struct mbedtls_pk_context *ctx, struct mbedtls_pk_debug_item *items)
{
    if (ctx->pk_info == ((void *)0)) {
        return -0x3E80;
    }
    if (ctx->pk_info->debug_func == ((void *)0)) {
        return -0x3F00;
    }
    ctx->pk_info->debug_func(ctx->pk_ctx, items);
    return 0;
}

variable_types:
items
struct mbedtls_pk_debug_item *


decompiled_code:
const char *__fastcall mbedtls_pk_get_name(__int64 a1)
{
if ( !a1 )
return "invalid PK";
if ( *(_QWORD *)a1 )
return *(const char **)(*(_QWORD *)a1 + 8LL);
return "invalid PK";
}

canonical_decompiled_code:
const char *__fastcall func16(__int64 a1)
{
if ( !a1 )
return "invalid PK";
if ( *(_QWORD *)a1 )
return *(const char **)(*(_QWORD *)a1 + 8LL);
return "invalid PK";
}

original_code:
const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
{
    if (ctx == ((void *)0) || ctx->pk_info == ((void *)0)) {
        return "invalid PK";
    }
    return ctx->pk_info->name;
}

canonical_original_code:
char *mbedtls_pk_get_name(struct mbedtls_pk_context *ctx)
{
    if (ctx == ((void *)0) || ctx->pk_info == ((void *)0)) {
        return "invalid PK";
    }
    return ctx->pk_info->name;
}

return_type: char *


decompiled_code:
__int64 __fastcall mbedtls_pk_get_type(unsigned int **a1)
{
if ( !a1 )
return 0LL;
if ( *a1 )
return **a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned int **a1)
{
if ( !a1 )
return 0LL;
if ( *a1 )
return **a1;
return 0LL;
}

original_code:
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
{
    if (ctx == ((void *)0) || ctx->pk_info == ((void *)0)) {
        return MBEDTLS_PK_NONE;
    }
    return ctx->pk_info->type;
}

canonical_original_code:
enum mbedtls_pk_type_t mbedtls_pk_get_type(struct mbedtls_pk_context *ctx)
{
    if (ctx == ((void *)0) || ctx->pk_info == ((void *)0)) {
        return MBEDTLS_PK_NONE;
    }
    return ctx->pk_info->type;
}

return_type: enum mbedtls_pk_type_t { MBEDTLS_PK_NONE=0, MBEDTLS_PK_RSA=1, MBEDTLS_PK_ECKEY=2, MBEDTLS_PK_ECKEY_DH=3, MBEDTLS_PK_ECDSA=4, MBEDTLS_PK_RSA_ALT=5, MBEDTLS_PK_RSASSA_PSS=6, MBEDTLS_PK_OPAQUE=7 }


original_code:
static void chacha20_block(const uint32_t initial_state[16],
                           unsigned char keystream[64])
{
    uint32_t working_state[16];
    size_t i;
    memcpy(working_state,
           initial_state,
           (4U * 16U));
    for (i = 0U; i < 10U; i++) {
        chacha20_inner_block(working_state);
    }
    working_state[0] += initial_state[0];
    working_state[1] += initial_state[1];
    working_state[2] += initial_state[2];
    working_state[3] += initial_state[3];
    working_state[4] += initial_state[4];
    working_state[5] += initial_state[5];
    working_state[6] += initial_state[6];
    working_state[7] += initial_state[7];
    working_state[8] += initial_state[8];
    working_state[9] += initial_state[9];
    working_state[10] += initial_state[10];
    working_state[11] += initial_state[11];
    working_state[12] += initial_state[12];
    working_state[13] += initial_state[13];
    working_state[14] += initial_state[14];
    working_state[15] += initial_state[15];
    for (i = 0U; i < 16; i++) {
        size_t offset = i * 4U;
        { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((keystream) + (offset), __builtin_bswap32((uint32_t) (working_state[i]))); } else { mbedtls_put_unaligned_uint32((keystream) + (offset), ((uint32_t) (working_state[i]))); } };
    }
    mbedtls_platform_zeroize(working_state, sizeof(working_state));
}

canonical_original_code:
static void chacha20_block(unsigned int initial_state[16], unsigned char keystream[64])
{
    unsigned int working_state[16];
    unsigned long i;
    memcpy(working_state,
           initial_state,
           (4U * 16U));
    for (i = 0U; i < 10U; i++) {
        chacha20_inner_block(working_state);
    }
    working_state[0] += initial_state[0];
    working_state[1] += initial_state[1];
    working_state[2] += initial_state[2];
    working_state[3] += initial_state[3];
    working_state[4] += initial_state[4];
    working_state[5] += initial_state[5];
    working_state[6] += initial_state[6];
    working_state[7] += initial_state[7];
    working_state[8] += initial_state[8];
    working_state[9] += initial_state[9];
    working_state[10] += initial_state[10];
    working_state[11] += initial_state[11];
    working_state[12] += initial_state[12];
    working_state[13] += initial_state[13];
    working_state[14] += initial_state[14];
    working_state[15] += initial_state[15];
    for (i = 0U; i < 16; i++) {
        unsigned long offset = i * 4U;
        { if (((1234) == (4321))) { mbedtls_put_unaligned_uint32((keystream) + (offset), __builtin_bswap32((unsigned int) (working_state[i]))); } else { mbedtls_put_unaligned_uint32((keystream) + (offset), ((unsigned int) (working_state[i]))); } };
    }
    mbedtls_platform_zeroize(working_state, sizeof(working_state));
}

variable_types:
keystream
unsigned char [64]

variable_types:
i
unsigned long

variable_types:
offset
unsigned long


decompiled_code:
__int64 __fastcall mbedtls_chacha20_init(__int64 a1)
{
__int64 result; // rax

mbedtls_platform_zeroize(a1, 64LL);
result = mbedtls_platform_zeroize(a1 + 64, 64LL);
*(_QWORD *)(a1 + 128) = 64LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 result; // rax

mbedtls_platform_zeroize(a1, 64LL);
result = mbedtls_platform_zeroize(a1 + 64, 64LL);
*(_QWORD *)(a1 + 128) = 64LL;
return result;
}

original_code:
void mbedtls_chacha20_init(mbedtls_chacha20_context *ctx)
{
    mbedtls_platform_zeroize(ctx->state, sizeof(ctx->state));
    mbedtls_platform_zeroize(ctx->keystream8, sizeof(ctx->keystream8));
    ctx->keystream_bytes_used = (4U * 16U);
}

canonical_original_code:
void mbedtls_chacha20_init(struct mbedtls_chacha20_context *ctx)
{
    mbedtls_platform_zeroize(ctx->state, sizeof(ctx->state));
    mbedtls_platform_zeroize(ctx->keystream8, sizeof(ctx->keystream8));
    ctx->keystream_bytes_used = (4U * 16U);
}

variable_types: struct mbedtls_chacha20_context { unsigned int state[16]; unsigned char keystream8[64]; unsigned long keystream_bytes_used; } *


decompiled_code:
__int64 __fastcall mbedtls_chacha20_free(__int64 a1)
{
__int64 result; // rax

if ( a1 )
result = mbedtls_platform_zeroize(a1, 136LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 result; // rax

if ( a1 )
result = mbedtls_platform_zeroize(a1, 136LL);
return result;
}

original_code:
void mbedtls_chacha20_free(mbedtls_chacha20_context *ctx)
{
    if (ctx != ((void *)0)) {
        mbedtls_platform_zeroize(ctx, sizeof(mbedtls_chacha20_context));
    }
}

canonical_original_code:
void mbedtls_chacha20_free(struct mbedtls_chacha20_context *ctx)
{
    if (ctx != ((void *)0)) {
        mbedtls_platform_zeroize(ctx, sizeof(mbedtls_chacha20_context));
    }
}

user_defined_types: struct mbedtls_chacha20_context { unsigned int state[16]; unsigned char keystream8[64]; unsigned long keystream_bytes_used; }


decompiled_code:
__int64 __fastcall mbedtls_chacha20_setkey(_DWORD *a1, __int64 a2)
{
qmemcpy(a1, "expand 32-byte k", 16);
a1[4] = mbedtls_get_unaligned_uint32(a2);
a1[5] = mbedtls_get_unaligned_uint32(a2 + 4);
a1[6] = mbedtls_get_unaligned_uint32(a2 + 8);
a1[7] = mbedtls_get_unaligned_uint32(a2 + 12);
a1[8] = mbedtls_get_unaligned_uint32(a2 + 16);
a1[9] = mbedtls_get_unaligned_uint32(a2 + 20);
a1[10] = mbedtls_get_unaligned_uint32(a2 + 24);
a1[11] = mbedtls_get_unaligned_uint32(a2 + 28);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(_DWORD *a1, __int64 a2)
{
qmemcpy(a1, "expand 32-byte k", 16);
a1[4] = mbedtls_get_unaligned_uint32(a2);
a1[5] = mbedtls_get_unaligned_uint32(a2 + 4);
a1[6] = mbedtls_get_unaligned_uint32(a2 + 8);
a1[7] = mbedtls_get_unaligned_uint32(a2 + 12);
a1[8] = mbedtls_get_unaligned_uint32(a2 + 16);
a1[9] = mbedtls_get_unaligned_uint32(a2 + 20);
a1[10] = mbedtls_get_unaligned_uint32(a2 + 24);
a1[11] = mbedtls_get_unaligned_uint32(a2 + 28);
return 0LL;
}

original_code:
int mbedtls_chacha20_setkey(mbedtls_chacha20_context *ctx,
                            const unsigned char key[32])
{
    ctx->state[0] = 0x61707865;
    ctx->state[1] = 0x3320646e;
    ctx->state[2] = 0x79622d32;
    ctx->state[3] = 0x6b206574;
    ctx->state[4] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (0))) : mbedtls_get_unaligned_uint32((key) + (0)) );
    ctx->state[5] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (4))) : mbedtls_get_unaligned_uint32((key) + (4)) );
    ctx->state[6] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (8))) : mbedtls_get_unaligned_uint32((key) + (8)) );
    ctx->state[7] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (12))) : mbedtls_get_unaligned_uint32((key) + (12)) );
    ctx->state[8] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (16))) : mbedtls_get_unaligned_uint32((key) + (16)) );
    ctx->state[9] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (20))) : mbedtls_get_unaligned_uint32((key) + (20)) );
    ctx->state[10] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (24))) : mbedtls_get_unaligned_uint32((key) + (24)) );
    ctx->state[11] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (28))) : mbedtls_get_unaligned_uint32((key) + (28)) );
    return 0;
}

canonical_original_code:
int mbedtls_chacha20_setkey(struct mbedtls_chacha20_context *ctx, unsigned char key[32])
{
    ctx->state[0] = 0x61707865;
    ctx->state[1] = 0x3320646e;
    ctx->state[2] = 0x79622d32;
    ctx->state[3] = 0x6b206574;
    ctx->state[4] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (0))) : mbedtls_get_unaligned_uint32((key) + (0)) );
    ctx->state[5] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (4))) : mbedtls_get_unaligned_uint32((key) + (4)) );
    ctx->state[6] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (8))) : mbedtls_get_unaligned_uint32((key) + (8)) );
    ctx->state[7] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (12))) : mbedtls_get_unaligned_uint32((key) + (12)) );
    ctx->state[8] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (16))) : mbedtls_get_unaligned_uint32((key) + (16)) );
    ctx->state[9] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (20))) : mbedtls_get_unaligned_uint32((key) + (20)) );
    ctx->state[10] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (24))) : mbedtls_get_unaligned_uint32((key) + (24)) );
    ctx->state[11] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((key) + (28))) : mbedtls_get_unaligned_uint32((key) + (28)) );
    return 0;
}

variable_types:
key
unsigned char [32]


decompiled_code:
__int64 __fastcall mbedtls_chacha20_starts(__int64 a1, __int64 a2, int a3)
{
*(_DWORD *)(a1 + 48) = a3;
*(_DWORD *)(a1 + 52) = mbedtls_get_unaligned_uint32(a2);
*(_DWORD *)(a1 + 56) = mbedtls_get_unaligned_uint32(a2 + 4);
*(_DWORD *)(a1 + 60) = mbedtls_get_unaligned_uint32(a2 + 8);
mbedtls_platform_zeroize(a1 + 64, 64LL);
*(_QWORD *)(a1 + 128) = 64LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, int a3)
{
*(_DWORD *)(a1 + 48) = a3;
*(_DWORD *)(a1 + 52) = mbedtls_get_unaligned_uint32(a2);
*(_DWORD *)(a1 + 56) = mbedtls_get_unaligned_uint32(a2 + 4);
*(_DWORD *)(a1 + 60) = mbedtls_get_unaligned_uint32(a2 + 8);
mbedtls_platform_zeroize(a1 + 64, 64LL);
*(_QWORD *)(a1 + 128) = 64LL;
return 0LL;
}

original_code:
int mbedtls_chacha20_starts(mbedtls_chacha20_context *ctx,
                            const unsigned char nonce[12],
                            uint32_t counter)
{
    ctx->state[12] = counter;
    ctx->state[13] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((nonce) + (0))) : mbedtls_get_unaligned_uint32((nonce) + (0)) );
    ctx->state[14] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((nonce) + (4))) : mbedtls_get_unaligned_uint32((nonce) + (4)) );
    ctx->state[15] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((nonce) + (8))) : mbedtls_get_unaligned_uint32((nonce) + (8)) );
    mbedtls_platform_zeroize(ctx->keystream8, sizeof(ctx->keystream8));
    ctx->keystream_bytes_used = (4U * 16U);
    return 0;
}

canonical_original_code:
int mbedtls_chacha20_starts(struct mbedtls_chacha20_context *ctx, unsigned char nonce[12], unsigned int counter)
{
    ctx->state[12] = counter;
    ctx->state[13] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((nonce) + (0))) : mbedtls_get_unaligned_uint32((nonce) + (0)) );
    ctx->state[14] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((nonce) + (4))) : mbedtls_get_unaligned_uint32((nonce) + (4)) );
    ctx->state[15] = ((((1234) == (4321))) ? __builtin_bswap32(mbedtls_get_unaligned_uint32((nonce) + (8))) : mbedtls_get_unaligned_uint32((nonce) + (8)) );
    mbedtls_platform_zeroize(ctx->keystream8, sizeof(ctx->keystream8));
    ctx->keystream_bytes_used = (4U * 16U);
    return 0;
}

variable_types:
nonce
unsigned char [12]

variable_types:
counter
unsigned int


original_code:
int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx,
                            size_t size,
                            const unsigned char *input,
                            unsigned char *output)
{
    size_t offset = 0U;
    while (size > 0U && ctx->keystream_bytes_used < (4U * 16U)) {
        output[offset] = input[offset]
                         ^ ctx->keystream8[ctx->keystream_bytes_used];
        ctx->keystream_bytes_used++;
        offset++;
        size--;
    }
    while (size >= (4U * 16U)) {
        chacha20_block(ctx->state, ctx->keystream8);
        ctx->state[(12U)]++;
        mbedtls_xor(output + offset, input + offset, ctx->keystream8, 64U);
        offset += (4U * 16U);
        size -= (4U * 16U);
    }
    if (size > 0U) {
        chacha20_block(ctx->state, ctx->keystream8);
        ctx->state[(12U)]++;
        mbedtls_xor(output + offset, input + offset, ctx->keystream8, size);
        ctx->keystream_bytes_used = size;
    }
    return 0;
}

canonical_original_code:
int mbedtls_chacha20_update(struct mbedtls_chacha20_context *ctx, unsigned long size, unsigned char *input, unsigned char *output)
{
    unsigned long offset = 0U;
    while (size > 0U && ctx->keystream_bytes_used < (4U * 16U)) {
        output[offset] = input[offset]
                         ^ ctx->keystream8[ctx->keystream_bytes_used];
        ctx->keystream_bytes_used++;
        offset++;
        size--;
    }
    while (size >= (4U * 16U)) {
        chacha20_block(ctx->state, ctx->keystream8);
        ctx->state[(12U)]++;
        mbedtls_xor(output + offset, input + offset, ctx->keystream8, 64U);
        offset += (4U * 16U);
        size -= (4U * 16U);
    }
    if (size > 0U) {
        chacha20_block(ctx->state, ctx->keystream8);
        ctx->state[(12U)]++;
        mbedtls_xor(output + offset, input + offset, ctx->keystream8, size);
        ctx->keystream_bytes_used = size;
    }
    return 0;
}

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall mbedtls_chacha20_crypt(__int64 a1, __int64 a2, int a3, __int64 a4, __int64 a5, __int64 a6)
{
unsigned int v10; // ebx
__m128i v12[12]; // [rsp+0h] [rbp-C8h] BYREF

mbedtls_chacha20_init((__int64)v12);
v10 = mbedtls_chacha20_setkey(v12, a1);
if ( !v10 )
{
v10 = mbedtls_chacha20_starts((__int64)v12, a2, a3);
if ( !v10 )
v10 = mbedtls_chacha20_update(v12, a4, a5, a6);
}
mbedtls_chacha20_free((__int64)v12);
return v10;
}

original_code:
int mbedtls_chacha20_crypt(const unsigned char key[32],
                           const unsigned char nonce[12],
                           uint32_t counter,
                           size_t data_len,
                           const unsigned char *input,
                           unsigned char *output)
{
    mbedtls_chacha20_context ctx;
    int ret = -0x006E;
    mbedtls_chacha20_init(&ctx);
    ret = mbedtls_chacha20_setkey(&ctx, key);
    if (ret != 0) {
        goto cleanup;
    }
    ret = mbedtls_chacha20_starts(&ctx, nonce, counter);
    if (ret != 0) {
        goto cleanup;
    }
    ret = mbedtls_chacha20_update(&ctx, data_len, input, output);
cleanup:
    mbedtls_chacha20_free(&ctx);
    return ret;
}

canonical_original_code:
int mbedtls_chacha20_crypt(unsigned char key[32], unsigned char nonce[12], unsigned int counter, unsigned long data_len, unsigned char *input, unsigned char *output)
{
    struct mbedtls_chacha20_context ctx;
    int ret = -0x006E;
    mbedtls_chacha20_init(&ctx);
    ret = mbedtls_chacha20_setkey(&ctx, key);
    if (ret != 0) {
        goto cleanup;
    }
    ret = mbedtls_chacha20_starts(&ctx, nonce, counter);
    if (ret != 0) {
        goto cleanup;
    }
    ret = mbedtls_chacha20_update(&ctx, data_len, input, output);
cleanup:
    mbedtls_chacha20_free(&ctx);
    return ret;
}

variable_types: struct mbedtls_chacha20_context { unsigned int state[16]; unsigned char keystream8[64]; unsigned long keystream_bytes_used; }

variable_types: ret int


decompiled_code:
__int64 __fastcall entropy_update(_DWORD *a1, char a2, _BYTE *a3, unsigned __int64 a4)
{
_BYTE *v4; // r13
__int64 v5; // r12
unsigned int v6; // ebx
_BYTE v8[78]; // [rsp+0h] [rbp-78h] BYREF
char v9[42]; // [rsp+4Eh] [rbp-2Ah] BYREF

v4 = a3;
v5 = a4;
if ( a4 > 0x40 )
{
v6 = mbedtls_sha512(a3, a4, v8, 0LL);
if ( v6 )
goto LABEL_6;
v4 = v8;
v5 = 64LL;
}
v9[0] = a2;
v9[1] = v5;
if ( *a1 || (v6 = mbedtls_sha512_starts(a1 + 2, 0LL)) == 0 )
{
*a1 = 1;
v6 = mbedtls_sha512_update(a1 + 2, v9, 2LL);
if ( !v6 )
v6 = mbedtls_sha512_update(a1 + 2, v4, v5);
}
LABEL_6:
mbedtls_platform_zeroize(v8, 64LL);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, char a2, _BYTE *a3, unsigned __int64 a4)
{
_BYTE *v4; // r13
__int64 v5; // r12
unsigned int v6; // ebx
_BYTE v8[78]; // [rsp+0h] [rbp-78h] BYREF
char v9[42]; // [rsp+4Eh] [rbp-2Ah] BYREF

v4 = a3;
v5 = a4;
if ( a4 > 0x40 )
{
v6 = mbedtls_sha512(a3, a4, v8, 0LL);
if ( v6 )
goto LABEL_6;
v4 = v8;
v5 = 64LL;
}
v9[0] = a2;
v9[1] = v5;
if ( *a1 || (v6 = mbedtls_sha512_starts(a1 + 2, 0LL)) == 0 )
{
*a1 = 1;
v6 = mbedtls_sha512_update(a1 + 2, v9, 2LL);
if ( !v6 )
v6 = mbedtls_sha512_update(a1 + 2, v4, v5);
}
LABEL_6:
mbedtls_platform_zeroize(v8, 64LL);
return v6;
}

original_code:
static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
                          const unsigned char *data, size_t len)
{
    unsigned char header[2];
    unsigned char tmp[64];
    size_t use_len = len;
    const unsigned char *p = data;
    int ret = 0;
    if (use_len > 64) {
        if ((ret = mbedtls_sha512(data, len, tmp, 0)) != 0) {
            goto cleanup;
        }
        p = tmp;
        use_len = 64;
    }
    header[0] = source_id;
    header[1] = use_len & 0xFF;
    if (ctx->accumulator_started == 0 &&
        (ret = mbedtls_sha512_starts(&ctx->accumulator, 0)) != 0) {
        goto cleanup;
    } else {
        ctx->accumulator_started = 1;
    }
    if ((ret = mbedtls_sha512_update(&ctx->accumulator, header, 2)) != 0) {
        goto cleanup;
    }
    ret = mbedtls_sha512_update(&ctx->accumulator, p, use_len);
cleanup:
    mbedtls_platform_zeroize(tmp, sizeof(tmp));
    return ret;
}

canonical_original_code:
static int entropy_update(struct mbedtls_entropy_context *ctx, unsigned char source_id, unsigned char *data, unsigned long len)
{
    unsigned char header[2];
    unsigned char tmp[64];
    unsigned long use_len = len;
    unsigned char *p = data;
    int ret = 0;
    if (use_len > 64) {
        if ((ret = mbedtls_sha512(data, len, tmp, 0)) != 0) {
            goto cleanup;
        }
        p = tmp;
        use_len = 64;
    }
    header[0] = source_id;
    header[1] = use_len & 0xFF;
    if (ctx->accumulator_started == 0 &&
        (ret = mbedtls_sha512_starts(&ctx->accumulator, 0)) != 0) {
        goto cleanup;
    } else {
        ctx->accumulator_started = 1;
    }
    if ((ret = mbedtls_sha512_update(&ctx->accumulator, header, 2)) != 0) {
        goto cleanup;
    }
    ret = mbedtls_sha512_update(&ctx->accumulator, p, use_len);
cleanup:
    mbedtls_platform_zeroize(tmp, sizeof(tmp));
    return ret;
}

variable_types: struct mbedtls_entropy_context { int accumulator_started; struct mbedtls_sha512_context { unsigned long total[2]; unsigned long state[8]; unsigned char buffer[128]; } accumulator; int source_count; struct mbedtls_entropy_source_state { int (*f_source)(void *data, unsigned char *output, unsigned long len, unsigned long *olen); void *p_source; unsigned long size; unsigned long threshold; int strong; } source[20]; struct mbedtls_threading_mutex_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; char is_valid; } mutex; } *

variable_types:
data
unsigned char *

variable_types:
len
unsigned long

variable_types:
header
unsigned char [2]

variable_types:
tmp
unsigned char [64]

variable_types:
p
unsigned char *

user_defined_types: struct mbedtls_entropy_context { int accumulator_started; struct mbedtls_sha512_context accumulator; int source_count; struct mbedtls_entropy_source_state source[20]; struct mbedtls_threading_mutex_t mutex; }

user_defined_types: struct mbedtls_threading_mutex_t { union pthread_mutex_t mutex; char is_valid; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall entropy_gather_internal(_DWORD *a1)
{
int v1; // eax
_DWORD *v2; // rbx
int v3; // er13
int v4; // er12
unsigned int v5; // ebp
unsigned int v6; // eax
unsigned __int64 v8; // [rsp+8h] [rbp-C0h] BYREF
char v9[184]; // [rsp+10h] [rbp-B8h] BYREF

v1 = a1[54];
if ( !v1 )
return (unsigned int)-64;
if ( v1 <= 0 )
{
v5 = -61;
}
else
{
v2 = a1 + 56;
v3 = 0;
v4 = 0;
while ( 1 )
{
if ( v2[8] == 1 )
v3 = 1;
v8 = 0LL;
v5 = (*(__int64 (__fastcall **)(_QWORD, char *, __int64, unsigned __int64 *))v2)(
*((_QWORD *)v2 + 1),
v9,
128LL,
&v8);
if ( v5 )
break;
if ( v8 )
{
v6 = entropy_update(a1, v4, v9, v8);
if ( v6 )
return v6;
*((_QWORD *)v2 + 2) += v8;
}
++v4;
v2 += 10;
if ( a1[54] <= v4 )
{
if ( !v3 )
v5 = -61;
break;
}
}
}
mbedtls_platform_zeroize(v9, 128LL);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1)
{
int v1; // eax
_DWORD *v2; // rbx
int v3; // er13
int v4; // er12
unsigned int v5; // ebp
unsigned int v6; // eax
unsigned __int64 v8; // [rsp+8h] [rbp-C0h] BYREF
char v9[184]; // [rsp+10h] [rbp-B8h] BYREF

v1 = a1[54];
if ( !v1 )
return (unsigned int)-64;
if ( v1 <= 0 )
{
v5 = -61;
}
else
{
v2 = a1 + 56;
v3 = 0;
v4 = 0;
while ( 1 )
{
if ( v2[8] == 1 )
v3 = 1;
v8 = 0LL;
v5 = (*(__int64 (__fastcall **)(_QWORD, char *, __int64, unsigned __int64 *))v2)(
*((_QWORD *)v2 + 1),
v9,
128LL,
&v8);
if ( v5 )
break;
if ( v8 )
{
v6 = func0(a1, v4, v9, v8);
if ( v6 )
return v6;
*((_QWORD *)v2 + 2) += v8;
}
++v4;
v2 += 10;
if ( a1[54] <= v4 )
{
if ( !v3 )
v5 = -61;
break;
}
}
}
mbedtls_platform_zeroize(v9, 128LL);
return v5;
}

original_code:
static int entropy_gather_internal(mbedtls_entropy_context *ctx)
{
    int ret = -0x003C;
    int i;
    int have_one_strong = 0;
    unsigned char buf[128];
    size_t olen;
    if (ctx->source_count == 0) {
        return -0x0040;
    }
    for (i = 0; i < ctx->source_count; i++) {
        if (ctx->source[i].strong == 1) {
            have_one_strong = 1;
        }
        olen = 0;
        if ((ret = ctx->source[i].f_source(ctx->source[i].p_source,
                                           buf, 128, &olen)) != 0) {
            goto cleanup;
        }
        if (olen > 0) {
            if ((ret = entropy_update(ctx, (unsigned char) i,
                                      buf, olen)) != 0) {
                return ret;
            }
            ctx->source[i].size += olen;
        }
    }
    if (have_one_strong == 0) {
        ret = -0x003D;
    }
cleanup:
    mbedtls_platform_zeroize(buf, sizeof(buf));
    return ret;
}

canonical_original_code:
static int entropy_gather_internal(struct mbedtls_entropy_context *ctx)
{
    int ret = -0x003C;
    int i;
    int have_one_strong = 0;
    unsigned char buf[128];
    unsigned long olen;
    if (ctx->source_count == 0) {
        return -0x0040;
    }
    for (i = 0; i < ctx->source_count; i++) {
        if (ctx->source[i].strong == 1) {
            have_one_strong = 1;
        }
        olen = 0;
        if ((ret = ctx->source[i].f_source(ctx->source[i].p_source,
                                           buf, 128, &olen)) != 0) {
            goto cleanup;
        }
        if (olen > 0) {
            if ((ret = entropy_update(ctx, (unsigned char) i,
                                      buf, olen)) != 0) {
                return ret;
            }
            ctx->source[i].size += olen;
        }
    }
    if (have_one_strong == 0) {
        ret = -0x003D;
    }
cleanup:
    mbedtls_platform_zeroize(buf, sizeof(buf));
    return ret;
}

variable_types: i int

variable_types:
buf
unsigned char [128]

variable_types:
olen
unsigned long

user_defined_types: struct mbedtls_sha512_context { unsigned long total[2]; unsigned long state[8]; unsigned char buffer[128]; }

user_defined_types: struct mbedtls_entropy_source_state { int (*f_source)(void *data, unsigned char *output, unsigned long len, unsigned long *olen); void *p_source; unsigned long size; unsigned long threshold; int strong; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 __fastcall mbedtls_entropy_free(_DWORD *a1)
{
__int64 result; // rax

if ( *a1 != -1 )
{
mbedtls_mutex_free(a1 + 256);
mbedtls_sha512_free(a1 + 2);
a1[54] = 0;
result = mbedtls_platform_zeroize(a1 + 56, 800LL);
*a1 = -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
__int64 result; // rax

if ( *a1 != -1 )
{
mbedtls_mutex_free(a1 + 256);
mbedtls_sha512_free(a1 + 2);
a1[54] = 0;
result = mbedtls_platform_zeroize(a1 + 56, 800LL);
*a1 = -1;
}
return result;
}

original_code:
void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
{
    if (ctx->accumulator_started == -1) {
        return;
    }
    mbedtls_mutex_free(&ctx->mutex);
    mbedtls_sha512_free(&ctx->accumulator);
    ctx->source_count = 0;
    mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source));
    ctx->accumulator_started = -1;
}

canonical_original_code:
void mbedtls_entropy_free(struct mbedtls_entropy_context *ctx)
{
    if (ctx->accumulator_started == -1) {
        return;
    }
    mbedtls_mutex_free(&ctx->mutex);
    mbedtls_sha512_free(&ctx->accumulator);
    ctx->source_count = 0;
    mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source));
    ctx->accumulator_started = -1;
}


original_code:
int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
                               mbedtls_entropy_f_source_ptr f_source, void *p_source,
                               size_t threshold, int strong)
{
    int idx, ret = 0;
    if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
        return ret;
    }
    idx = ctx->source_count;
    if (idx >= 20) {
        ret = -0x003E;
        goto exit;
    }
    ctx->source[idx].f_source = f_source;
    ctx->source[idx].p_source = p_source;
    ctx->source[idx].threshold = threshold;
    ctx->source[idx].strong = strong;
    ctx->source_count++;
exit:
    if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
        return -0x001E;
    }
    return ret;
}

canonical_original_code:
int mbedtls_entropy_add_source(struct mbedtls_entropy_context *ctx, int (*f_source)(void *data, unsigned char *output, unsigned long len, unsigned long *olen), void *p_source, unsigned long threshold, int strong)
{
    int idx; int ret = 0;
    if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
        return ret;
    }
    idx = ctx->source_count;
    if (idx >= 20) {
        ret = -0x003E;
        goto exit;
    }
    ctx->source[idx].f_source = f_source;
    ctx->source[idx].p_source = p_source;
    ctx->source[idx].threshold = threshold;
    ctx->source[idx].strong = strong;
    ctx->source_count++;
exit:
    if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
        return -0x001E;
    }
    return ret;
}

variable_types: int (*)(void *data, unsigned char *output, unsigned long len, unsigned long *olen)

variable_types:
threshold
unsigned long

variable_types: strong int

variable_types: idx int


decompiled_code:
__int64 __fastcall mbedtls_entropy_init(__int64 a1)
{
*(_DWORD *)(a1 + 216) = 0;
*(_QWORD *)(a1 + 224) = 0LL;
*(_QWORD *)(a1 + 1016) = 0LL;
memset(
(void *)((a1 + 232) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 232) & 0xFFFFFFF8) + 1024) >> 3));
mbedtls_mutex_init(a1 + 1024);
*(_DWORD *)a1 = 0;
mbedtls_sha512_init(a1 + 8);
return mbedtls_entropy_add_source(a1, (__int64)&mbedtls_platform_entropy_poll, 0LL, 32LL, 1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
*(_DWORD *)(a1 + 216) = 0;
*(_QWORD *)(a1 + 224) = 0LL;
*(_QWORD *)(a1 + 1016) = 0LL;
memset(
(void *)((a1 + 232) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 232) & 0xFFFFFFF8) + 1024) >> 3));
mbedtls_mutex_init(a1 + 1024);
*(_DWORD *)a1 = 0;
mbedtls_sha512_init(a1 + 8);
return func3(a1, (__int64)&mbedtls_platform_entropy_poll, 0LL, 32LL, 1);
}

original_code:
void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
{
    ctx->source_count = 0;
    memset(ctx->source, 0, sizeof(ctx->source));
    mbedtls_mutex_init(&ctx->mutex);
    ctx->accumulator_started = 0;
    mbedtls_sha512_init(&ctx->accumulator);
    mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, ((void *)0),
                               32,
                               1);
}

canonical_original_code:
void mbedtls_entropy_init(struct mbedtls_entropy_context *ctx)
{
    ctx->source_count = 0;
    memset(ctx->source, 0, sizeof(ctx->source));
    mbedtls_mutex_init(&ctx->mutex);
    ctx->accumulator_started = 0;
    mbedtls_sha512_init(&ctx->accumulator);
    mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, ((void *)0),
                               32,
                               1);
}


decompiled_code:
__int64 __fastcall mbedtls_entropy_update_manual(_DWORD *a1, _BYTE *a2, unsigned __int64 a3)
{
unsigned int v4; // ebx

v4 = mbedtls_mutex_lock(a1 + 256);
if ( !v4 )
{
v4 = entropy_update(a1, 20, a2, a3);
if ( (unsigned int)mbedtls_mutex_unlock(a1 + 256) )
v4 = -30;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(_DWORD *a1, _BYTE *a2, unsigned __int64 a3)
{
unsigned int v4; // ebx

v4 = mbedtls_mutex_lock(a1 + 256);
if ( !v4 )
{
v4 = func0(a1, 20, a2, a3);
if ( (unsigned int)mbedtls_mutex_unlock(a1 + 256) )
v4 = -30;
}
return v4;
}

original_code:
int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
                                  const unsigned char *data, size_t len)
{
    int ret = -0x006E;
    if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
        return ret;
    }
    ret = entropy_update(ctx, 20, data, len);
    if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
        return -0x001E;
    }
    return ret;
}

canonical_original_code:
int mbedtls_entropy_update_manual(struct mbedtls_entropy_context *ctx, unsigned char *data, unsigned long len)
{
    int ret = -0x006E;
    if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
        return ret;
    }
    ret = entropy_update(ctx, 20, data, len);
    if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
        return -0x001E;
    }
    return ret;
}


decompiled_code:
__int64 __fastcall mbedtls_entropy_gather(_DWORD *a1)
{
unsigned int v1; // ebx

v1 = mbedtls_mutex_lock(a1 + 256);
if ( !v1 )
{
v1 = entropy_gather_internal(a1);
if ( (unsigned int)mbedtls_mutex_unlock(a1 + 256) )
v1 = -30;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(_DWORD *a1)
{
unsigned int v1; // ebx

v1 = mbedtls_mutex_lock(a1 + 256);
if ( !v1 )
{
v1 = func1(a1);
if ( (unsigned int)mbedtls_mutex_unlock(a1 + 256) )
v1 = -30;
}
return v1;
}

original_code:
int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
{
    int ret = -0x006E;
    if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
        return ret;
    }
    ret = entropy_gather_internal(ctx);
    if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
        return -0x001E;
    }
    return ret;
}

canonical_original_code:
int mbedtls_entropy_gather(struct mbedtls_entropy_context *ctx)
{
    int ret = -0x006E;
    if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
        return ret;
    }
    ret = entropy_gather_internal(ctx);
    if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
        return -0x001E;
    }
    return ret;
}


decompiled_code:
__int64 __fastcall mbedtls_entropy_func(_DWORD *a1, void *a2, size_t a3)
{
_DWORD *v4; // r15
unsigned int v5; // ebx
int v6; // er12
_DWORD *v7; // r14
_DWORD *v8; // rax
int v9; // ecx
unsigned __int64 v10; // rdi
int v11; // eax
_QWORD *v12; // r13
_DWORD *v13; // rsi
int v15; // eax
_QWORD *v16; // rax
__int64 src[15]; // [rsp+10h] [rbp-78h] BYREF

if ( a3 > 0x40 )
return (unsigned int)-60;
v4 = a1 + 256;
v5 = mbedtls_mutex_lock(a1 + 256);
if ( !v5 )
{
v6 = 257;
v7 = a1 + 70;
while ( 1 )
{
v5 = entropy_gather_internal(a1);
if ( v5 )
break;
v11 = a1[54];
if ( v11 > 0 )
{
v12 = a1 + 60;
v13 = &v7[10 * (v11 - 1)];
v8 = a1 + 60;
v10 = 0LL;
v9 = 1;
do
{
if ( *(_QWORD *)v8 < *((_QWORD *)v8 + 1) )
v9 = v5;
if ( v8[4] == 1 )
v10 += *(_QWORD *)v8;
v8 += 10;
}
while ( v8 != v13 );
if ( v9 && v10 > 0x3F )
{
src[0] = 0LL;
src[1] = 0LL;
src[2] = 0LL;
src[3] = 0LL;
src[4] = 0LL;
src[5] = 0LL;
src[6] = 0LL;
src[7] = 0LL;
v5 = mbedtls_sha512_finish(a1 + 2, src);
if ( !v5 )
{
mbedtls_sha512_free(a1 + 2);
mbedtls_sha512_init(a1 + 2);
v5 = mbedtls_sha512_starts(a1 + 2, 0LL);
if ( !v5 )
{
v5 = mbedtls_sha512_update(a1 + 2, src, 64LL);
if ( !v5 )
{
v5 = mbedtls_sha512(src, 64LL, src, 0LL);
if ( !v5 )
{
v15 = a1[54];
if ( v15 > 0 )
{
v16 = &v7[10 * (v15 - 1)];
do
{
*v12 = 0LL;
v12 += 5;
}
while ( v12 != v16 );
}
memcpy(a2, src, a3);
}
}
}
}
break;
}
}
if ( !--v6 )
{
v5 = -60;
break;
}
}
mbedtls_platform_zeroize(src, 64LL);
if ( (unsigned int)mbedtls_mutex_unlock(v4) )
v5 = -30;
}
return v5;
}

original_code:
int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
{
    int ret, count = 0, i, thresholds_reached;
    size_t strong_size;
    mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
    unsigned char buf[64];
    if (len > 64) {
        return -0x003C;
    }
    if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
        return ret;
    }
    do {
        if (count++ > 256) {
            ret = -0x003C;
            goto exit;
        }
        if ((ret = entropy_gather_internal(ctx)) != 0) {
            goto exit;
        }
        thresholds_reached = 1;
        strong_size = 0;
        for (i = 0; i < ctx->source_count; i++) {
            if (ctx->source[i].size < ctx->source[i].threshold) {
                thresholds_reached = 0;
            }
            if (ctx->source[i].strong == 1) {
                strong_size += ctx->source[i].size;
            }
        }
    } while (!thresholds_reached || strong_size < 64);
    memset(buf, 0, 64);
    if ((ret = mbedtls_sha512_finish(&ctx->accumulator, buf)) != 0) {
        goto exit;
    }
    mbedtls_sha512_free(&ctx->accumulator);
    mbedtls_sha512_init(&ctx->accumulator);
    if ((ret = mbedtls_sha512_starts(&ctx->accumulator, 0)) != 0) {
        goto exit;
    }
    if ((ret = mbedtls_sha512_update(&ctx->accumulator, buf,
                                     64)) != 0) {
        goto exit;
    }
    if ((ret = mbedtls_sha512(buf, 64,
                              buf, 0)) != 0) {
        goto exit;
    }
    for (i = 0; i < ctx->source_count; i++) {
        ctx->source[i].size = 0;
    }
    memcpy(output, buf, len);
    ret = 0;
exit:
    mbedtls_platform_zeroize(buf, sizeof(buf));
    if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
        return -0x001E;
    }
    return ret;
}

canonical_original_code:
int mbedtls_entropy_func(void *data, unsigned char *output, unsigned long len)
{
    int ret; int count = 0; int i; int thresholds_reached;
    unsigned long strong_size;
    struct mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
    unsigned char buf[64];
    if (len > 64) {
        return -0x003C;
    }
    if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
        return ret;
    }
    do {
        if (count++ > 256) {
            ret = -0x003C;
            goto exit;
        }
        if ((ret = entropy_gather_internal(ctx)) != 0) {
            goto exit;
        }
        thresholds_reached = 1;
        strong_size = 0;
        for (i = 0; i < ctx->source_count; i++) {
            if (ctx->source[i].size < ctx->source[i].threshold) {
                thresholds_reached = 0;
            }
            if (ctx->source[i].strong == 1) {
                strong_size += ctx->source[i].size;
            }
        }
    } while (!thresholds_reached || strong_size < 64);
    memset(buf, 0, 64);
    if ((ret = mbedtls_sha512_finish(&ctx->accumulator, buf)) != 0) {
        goto exit;
    }
    mbedtls_sha512_free(&ctx->accumulator);
    mbedtls_sha512_init(&ctx->accumulator);
    if ((ret = mbedtls_sha512_starts(&ctx->accumulator, 0)) != 0) {
        goto exit;
    }
    if ((ret = mbedtls_sha512_update(&ctx->accumulator, buf,
                                     64)) != 0) {
        goto exit;
    }
    if ((ret = mbedtls_sha512(buf, 64,
                              buf, 0)) != 0) {
        goto exit;
    }
    for (i = 0; i < ctx->source_count; i++) {
        ctx->source[i].size = 0;
    }
    memcpy(output, buf, len);
    ret = 0;
exit:
    mbedtls_platform_zeroize(buf, sizeof(buf));
    if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
        return -0x001E;
    }
    return ret;
}

variable_types:
data
void *

variable_types:
output
unsigned char *

variable_types: count int

variable_types:
buf
unsigned char [64]


decompiled_code:
__int64 __fastcall ecdh_init_internal(__int64 a1)
{
mbedtls_ecp_group_init();
mbedtls_mpi_init(a1 + 248);
mbedtls_ecp_point_init(a1 + 272);
mbedtls_ecp_point_init(a1 + 344);
return mbedtls_mpi_init(a1 + 416);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
mbedtls_ecp_group_init();
mbedtls_mpi_init(a1 + 248);
mbedtls_ecp_point_init(a1 + 272);
mbedtls_ecp_point_init(a1 + 344);
return mbedtls_mpi_init(a1 + 416);
}

original_code:
static void ecdh_init_internal(mbedtls_ecdh_context_mbed *ctx)
{
    mbedtls_ecp_group_init(&ctx->grp);
    mbedtls_mpi_init(&ctx->d);
    mbedtls_ecp_point_init(&ctx->Q);
    mbedtls_ecp_point_init(&ctx->Qp);
    mbedtls_mpi_init(&ctx->z);
}

canonical_original_code:
static void ecdh_init_internal(struct mbedtls_ecdh_context_mbed *ctx)
{
    mbedtls_ecp_group_init(&ctx->grp);
    mbedtls_mpi_init(&ctx->d);
    mbedtls_ecp_point_init(&ctx->Q);
    mbedtls_ecp_point_init(&ctx->Qp);
    mbedtls_mpi_init(&ctx->z);
}

variable_types: struct mbedtls_ecdh_context_mbed { struct mbedtls_ecp_group { enum mbedtls_ecp_group_id { MBEDTLS_ECP_DP_NONE=0, MBEDTLS_ECP_DP_SECP192R1=1, MBEDTLS_ECP_DP_SECP224R1=2, MBEDTLS_ECP_DP_SECP256R1=3, MBEDTLS_ECP_DP_SECP384R1=4, MBEDTLS_ECP_DP_SECP521R1=5, MBEDTLS_ECP_DP_BP256R1=6, MBEDTLS_ECP_DP_BP384R1=7, MBEDTLS_ECP_DP_BP512R1=8, MBEDTLS_ECP_DP_CURVE25519=9, MBEDTLS_ECP_DP_SECP192K1=10, MBEDTLS_ECP_DP_SECP224K1=11, MBEDTLS_ECP_DP_SECP256K1=12, MBEDTLS_ECP_DP_CURVE448=13 } id; struct mbedtls_mpi { int s; unsigned long n; unsigned long *p; } P; struct mbedtls_mpi A; struct mbedtls_mpi B; struct mbedtls_ecp_point { struct mbedtls_mpi X; struct mbedtls_mpi Y; struct mbedtls_mpi Z; } G; struct mbedtls_mpi N; unsigned long pbits; unsigned long nbits; unsigned int h; int (*modp)(struct mbedtls_mpi *); int (*t_pre)(struct mbedtls_ecp_point *, void *); int (*t_post)(struct mbedtls_ecp_point *, void *); void *t_data; struct mbedtls_ecp_point *T; unsigned long T_size; } grp; struct mbedtls_mpi d; struct mbedtls_ecp_point Q; struct mbedtls_ecp_point Qp; struct mbedtls_mpi z; } *

user_defined_types: struct mbedtls_ecdh_context_mbed { struct mbedtls_ecp_group grp; struct mbedtls_mpi d; struct mbedtls_ecp_point Q; struct mbedtls_ecp_point Qp; struct mbedtls_mpi z; }

user_defined_types: struct mbedtls_ecp_group { enum mbedtls_ecp_group_id id; struct mbedtls_mpi P; struct mbedtls_mpi A; struct mbedtls_mpi B; struct mbedtls_ecp_point G; struct mbedtls_mpi N; unsigned long pbits; unsigned long nbits; unsigned int h; int (*modp)(struct mbedtls_mpi *); int (*t_pre)(struct mbedtls_ecp_point *, void *); int (*t_post)(struct mbedtls_ecp_point *, void *); void *t_data; struct mbedtls_ecp_point *T; unsigned long T_size; }


decompiled_code:
__int64 ecdh_setup_internal()
{
__int64 result; // rax

result = mbedtls_ecp_group_load();
if ( (_DWORD)result )
result = 4294947200LL;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax

result = mbedtls_ecp_group_load();
if ( (_DWORD)result )
result = 4294947200LL;
return result;
}

original_code:
static int ecdh_setup_internal(mbedtls_ecdh_context_mbed *ctx,
                               mbedtls_ecp_group_id grp_id)
{
    int ret = -0x006E;
    ret = mbedtls_ecp_group_load(&ctx->grp, grp_id);
    if (ret != 0) {
        return -0x4E80;
    }
    return 0;
}

canonical_original_code:
static int ecdh_setup_internal(struct mbedtls_ecdh_context_mbed *ctx, enum mbedtls_ecp_group_id grp_id)
{
    int ret = -0x006E;
    ret = mbedtls_ecp_group_load(&ctx->grp, grp_id);
    if (ret != 0) {
        return -0x4E80;
    }
    return 0;
}

variable_types: enum mbedtls_ecp_group_id { MBEDTLS_ECP_DP_NONE=0, MBEDTLS_ECP_DP_SECP192R1=1, MBEDTLS_ECP_DP_SECP224R1=2, MBEDTLS_ECP_DP_SECP256R1=3, MBEDTLS_ECP_DP_SECP384R1=4, MBEDTLS_ECP_DP_SECP521R1=5, MBEDTLS_ECP_DP_BP256R1=6, MBEDTLS_ECP_DP_BP384R1=7, MBEDTLS_ECP_DP_BP512R1=8, MBEDTLS_ECP_DP_CURVE25519=9, MBEDTLS_ECP_DP_SECP192K1=10, MBEDTLS_ECP_DP_SECP224K1=11, MBEDTLS_ECP_DP_SECP256K1=12, MBEDTLS_ECP_DP_CURVE448=13 }

variable_types: ret int

user_defined_types: struct mbedtls_mpi { int s; unsigned long n; unsigned long *p; }

user_defined_types: struct mbedtls_ecp_point { struct mbedtls_mpi X; struct mbedtls_mpi Y; struct mbedtls_mpi Z; }


decompiled_code:
__int64 __fastcall ecdh_free_internal(__int64 a1)
{
mbedtls_ecp_group_free();
mbedtls_mpi_free(a1 + 248);
mbedtls_ecp_point_free(a1 + 272);
mbedtls_ecp_point_free(a1 + 344);
return mbedtls_mpi_free(a1 + 416);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
mbedtls_ecp_group_free();
mbedtls_mpi_free(a1 + 248);
mbedtls_ecp_point_free(a1 + 272);
mbedtls_ecp_point_free(a1 + 344);
return mbedtls_mpi_free(a1 + 416);
}

original_code:
static void ecdh_free_internal(mbedtls_ecdh_context_mbed *ctx)
{
    mbedtls_ecp_group_free(&ctx->grp);
    mbedtls_mpi_free(&ctx->d);
    mbedtls_ecp_point_free(&ctx->Q);
    mbedtls_ecp_point_free(&ctx->Qp);
    mbedtls_mpi_free(&ctx->z);
}

canonical_original_code:
static void ecdh_free_internal(struct mbedtls_ecdh_context_mbed *ctx)
{
    mbedtls_ecp_group_free(&ctx->grp);
    mbedtls_mpi_free(&ctx->d);
    mbedtls_ecp_point_free(&ctx->Q);
    mbedtls_ecp_point_free(&ctx->Qp);
    mbedtls_mpi_free(&ctx->z);
}

user_defined_types: enum mbedtls_ecp_group_id { MBEDTLS_ECP_DP_NONE=0, MBEDTLS_ECP_DP_SECP192R1=1, MBEDTLS_ECP_DP_SECP224R1=2, MBEDTLS_ECP_DP_SECP256R1=3, MBEDTLS_ECP_DP_SECP384R1=4, MBEDTLS_ECP_DP_SECP521R1=5, MBEDTLS_ECP_DP_BP256R1=6, MBEDTLS_ECP_DP_BP384R1=7, MBEDTLS_ECP_DP_BP512R1=8, MBEDTLS_ECP_DP_CURVE25519=9, MBEDTLS_ECP_DP_SECP192K1=10, MBEDTLS_ECP_DP_SECP224K1=11, MBEDTLS_ECP_DP_SECP256K1=12, MBEDTLS_ECP_DP_CURVE448=13 }


decompiled_code:
__int64 __fastcall ecdh_read_params_internal(__int64 a1, _QWORD *a2, __int64 a3)
{
return mbedtls_ecp_tls_read_point(a1, a1 + 344, a2, a3 - *a2);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, _QWORD *a2, __int64 a3)
{
return mbedtls_ecp_tls_read_point(a1, a1 + 344, a2, a3 - *a2);
}

original_code:
static int ecdh_read_params_internal(mbedtls_ecdh_context_mbed *ctx,
                                     const unsigned char **buf,
                                     const unsigned char *end)
{
    return mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, buf,
                                      end - *buf);
}

canonical_original_code:
static int ecdh_read_params_internal(struct mbedtls_ecdh_context_mbed *ctx, unsigned char **buf, unsigned char *end)
{
    return mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, buf,
                                      end - *buf);
}

variable_types:
buf
unsigned char **

variable_types:
end
unsigned char *


decompiled_code:
__int64 __fastcall ecdh_read_public_internal(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v5[0] = a2;
result = mbedtls_ecp_tls_read_point(a1, a1 + 344, v5, a3);
if ( !(_DWORD)result && v5[0] - a2 != a3 )
result = 4294946944LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v5[0] = a2;
result = mbedtls_ecp_tls_read_point(a1, a1 + 344, v5, a3);
if ( !(_DWORD)result && v5[0] - a2 != a3 )
result = 4294946944LL;
return result;
}

original_code:
static int ecdh_read_public_internal(mbedtls_ecdh_context_mbed *ctx,
                                     const unsigned char *buf, size_t blen)
{
    int ret = -0x006E;
    const unsigned char *p = buf;
    if ((ret = mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, &p,
                                          blen)) != 0) {
        return ret;
    }
    if ((size_t) (p - buf) != blen) {
        return -0x4F80;
    }
    return 0;
}

canonical_original_code:
static int ecdh_read_public_internal(struct mbedtls_ecdh_context_mbed *ctx, unsigned char *buf, unsigned long blen)
{
    int ret = -0x006E;
    unsigned char *p = buf;
    if ((ret = mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, &p,
                                          blen)) != 0) {
        return ret;
    }
    if ((unsigned long) (p - buf) != blen) {
        return -0x4F80;
    }
    return 0;
}

variable_types:
buf
unsigned char *

variable_types:
blen
unsigned long

variable_types:
p
unsigned char *


decompiled_code:
__int64 __fastcall ecdh_get_params_internal(__int64 a1, __int64 a2, int a3)
{
__int64 result; // rax

if ( a3 == 1 )
return mbedtls_ecp_copy(a1 + 344, a2 + 272);
if ( a3 )
return 4294946944LL;
result = mbedtls_ecp_copy(a1 + 272, a2 + 272);
if ( !(_DWORD)result )
result = mbedtls_mpi_copy(a1 + 248, a2 + 248);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, int a3)
{
__int64 result; // rax

if ( a3 == 1 )
return mbedtls_ecp_copy(a1 + 344, a2 + 272);
if ( a3 )
return 4294946944LL;
result = mbedtls_ecp_copy(a1 + 272, a2 + 272);
if ( !(_DWORD)result )
result = mbedtls_mpi_copy(a1 + 248, a2 + 248);
return result;
}

original_code:
static int ecdh_get_params_internal(mbedtls_ecdh_context_mbed *ctx,
                                    const mbedtls_ecp_keypair *key,
                                    mbedtls_ecdh_side side)
{
    int ret = -0x006E;
    if (side == MBEDTLS_ECDH_THEIRS) {
        return mbedtls_ecp_copy(&ctx->Qp, &key->Q);
    }
    if (side != MBEDTLS_ECDH_OURS) {
        return -0x4F80;
    }
    if ((ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0 ||
        (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0) {
        return ret;
    }
    return 0;
}

canonical_original_code:
static int ecdh_get_params_internal(struct mbedtls_ecdh_context_mbed *ctx, struct mbedtls_ecp_keypair *key, enum mbedtls_ecdh_side side)
{
    int ret = -0x006E;
    if (side == MBEDTLS_ECDH_THEIRS) {
        return mbedtls_ecp_copy(&ctx->Qp, &key->Q);
    }
    if (side != MBEDTLS_ECDH_OURS) {
        return -0x4F80;
    }
    if ((ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0 ||
        (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0) {
        return ret;
    }
    return 0;
}

variable_types: struct mbedtls_ecp_keypair { struct mbedtls_ecp_group grp; struct mbedtls_mpi d; struct mbedtls_ecp_point Q; } *

variable_types: enum mbedtls_ecdh_side { MBEDTLS_ECDH_OURS=0, MBEDTLS_ECDH_THEIRS=1 }


decompiled_code:
__int64 mbedtls_ecdh_can_do()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func9()
{
return 1LL;
}

original_code:
int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid)
{
    (void) gid;
    return 1;
}

canonical_original_code:
int mbedtls_ecdh_can_do(enum mbedtls_ecp_group_id gid)
{
    (void) gid;
    return 1;
}


decompiled_code:
__int64 __fastcall mbedtls_ecdh_gen_public(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
return ecdh_gen_public_restartable(a1, a2, a3, a4, a5, 0LL);
}

original_code:
int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
                            int (*f_rng)(void *, unsigned char *, size_t),
                            void *p_rng)
{
    return ecdh_gen_public_restartable(grp, d, Q, f_rng, p_rng, ((void *)0));
}

canonical_original_code:
int mbedtls_ecdh_gen_public(struct mbedtls_ecp_group *grp, struct mbedtls_mpi *d, struct mbedtls_ecp_point *Q, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    return ecdh_gen_public_restartable(grp, d, Q, f_rng, p_rng, ((void *)0));
}

variable_types: struct mbedtls_ecp_group { enum mbedtls_ecp_group_id { MBEDTLS_ECP_DP_NONE=0, MBEDTLS_ECP_DP_SECP192R1=1, MBEDTLS_ECP_DP_SECP224R1=2, MBEDTLS_ECP_DP_SECP256R1=3, MBEDTLS_ECP_DP_SECP384R1=4, MBEDTLS_ECP_DP_SECP521R1=5, MBEDTLS_ECP_DP_BP256R1=6, MBEDTLS_ECP_DP_BP384R1=7, MBEDTLS_ECP_DP_BP512R1=8, MBEDTLS_ECP_DP_CURVE25519=9, MBEDTLS_ECP_DP_SECP192K1=10, MBEDTLS_ECP_DP_SECP224K1=11, MBEDTLS_ECP_DP_SECP256K1=12, MBEDTLS_ECP_DP_CURVE448=13 } id; struct mbedtls_mpi { int s; unsigned long n; unsigned long *p; } P; struct mbedtls_mpi A; struct mbedtls_mpi B; struct mbedtls_ecp_point { struct mbedtls_mpi X; struct mbedtls_mpi Y; struct mbedtls_mpi Z; } G; struct mbedtls_mpi N; unsigned long pbits; unsigned long nbits; unsigned int h; int (*modp)(struct mbedtls_mpi *); int (*t_pre)(struct mbedtls_ecp_point *, void *); int (*t_post)(struct mbedtls_ecp_point *, void *); void *t_data; struct mbedtls_ecp_point *T; unsigned long T_size; } *

variable_types:
d
struct mbedtls_mpi *

variable_types:
Q
struct mbedtls_ecp_point *

variable_types: int (*)(void *, unsigned char *, unsigned long)


decompiled_code:
__int64 __fastcall mbedtls_ecdh_compute_shared(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
return ecdh_compute_shared_restartable(a1, a2, a3, a4, a5, a6, 0LL);
}

original_code:
int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z,
                                const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
                                int (*f_rng)(void *, unsigned char *, size_t),
                                void *p_rng)
{
    return ecdh_compute_shared_restartable(grp, z, Q, d,
                                           f_rng, p_rng, ((void *)0));
}

canonical_original_code:
int mbedtls_ecdh_compute_shared(struct mbedtls_ecp_group *grp, struct mbedtls_mpi *z, struct mbedtls_ecp_point *Q, struct mbedtls_mpi *d, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    return ecdh_compute_shared_restartable(grp, z, Q, d,
                                           f_rng, p_rng, ((void *)0));
}

variable_types:
z
struct mbedtls_mpi *


decompiled_code:
__int64 __fastcall mbedtls_ecdh_setup(__int64 a1, unsigned int a2)
{
*(_BYTE *)a1 = 0;
*(_DWORD *)(a1 + 8) = 1;
*(_DWORD *)(a1 + 4) = a2;
ecdh_init_internal(a1 + 16);
return ecdh_setup_internal(a1 + 16, a2);
}

original_code:
int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id)
{
    switch (grp_id) {
        default:
            ctx->point_format = 0;
            ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
            ctx->grp_id = grp_id;
            ecdh_init_internal(&ctx->ctx.mbed_ecdh);
            return ecdh_setup_internal(&ctx->ctx.mbed_ecdh, grp_id);
    }
}

canonical_original_code:
int mbedtls_ecdh_setup(struct mbedtls_ecdh_context *ctx, enum mbedtls_ecp_group_id grp_id)
{
    switch (grp_id) {
        default:
            ctx->point_format = 0;
            ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
            ctx->grp_id = grp_id;
            ecdh_init_internal(&ctx->ctx.mbed_ecdh);
            return ecdh_setup_internal(&ctx->ctx.mbed_ecdh, grp_id);
    }
}

variable_types: struct mbedtls_ecdh_context { unsigned char point_format; enum mbedtls_ecp_group_id { MBEDTLS_ECP_DP_NONE=0, MBEDTLS_ECP_DP_SECP192R1=1, MBEDTLS_ECP_DP_SECP224R1=2, MBEDTLS_ECP_DP_SECP256R1=3, MBEDTLS_ECP_DP_SECP384R1=4, MBEDTLS_ECP_DP_SECP521R1=5, MBEDTLS_ECP_DP_BP256R1=6, MBEDTLS_ECP_DP_BP384R1=7, MBEDTLS_ECP_DP_BP512R1=8, MBEDTLS_ECP_DP_CURVE25519=9, MBEDTLS_ECP_DP_SECP192K1=10, MBEDTLS_ECP_DP_SECP224K1=11, MBEDTLS_ECP_DP_SECP256K1=12, MBEDTLS_ECP_DP_CURVE448=13 } grp_id; enum mbedtls_ecdh_variant { MBEDTLS_ECDH_VARIANT_NONE=0, MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0=1 } var; union { struct mbedtls_ecdh_context_mbed { struct mbedtls_ecp_group { enum mbedtls_ecp_group_id { MBEDTLS_ECP_DP_NONE=0, MBEDTLS_ECP_DP_SECP192R1=1, MBEDTLS_ECP_DP_SECP224R1=2, MBEDTLS_ECP_DP_SECP256R1=3, MBEDTLS_ECP_DP_SECP384R1=4, MBEDTLS_ECP_DP_SECP521R1=5, MBEDTLS_ECP_DP_BP256R1=6, MBEDTLS_ECP_DP_BP384R1=7, MBEDTLS_ECP_DP_BP512R1=8, MBEDTLS_ECP_DP_CURVE25519=9, MBEDTLS_ECP_DP_SECP192K1=10, MBEDTLS_ECP_DP_SECP224K1=11, MBEDTLS_ECP_DP_SECP256K1=12, MBEDTLS_ECP_DP_CURVE448=13 } id; struct mbedtls_mpi { int s; unsigned long n; unsigned long *p; } P; struct mbedtls_mpi A; struct mbedtls_mpi B; struct mbedtls_ecp_point { struct mbedtls_mpi X; struct mbedtls_mpi Y; struct mbedtls_mpi Z; } G; struct mbedtls_mpi N; unsigned long pbits; unsigned long nbits; unsigned int h; int (*modp)(struct mbedtls_mpi *); int (*t_pre)(struct mbedtls_ecp_point *, void *); int (*t_post)(struct mbedtls_ecp_point *, void *); void *t_data; struct mbedtls_ecp_point *T; unsigned long T_size; } grp; struct mbedtls_mpi d; struct mbedtls_ecp_point Q; struct mbedtls_ecp_point Qp; struct mbedtls_mpi z; } mbed_ecdh; } ctx; } *

user_defined_types: struct mbedtls_ecdh_context { unsigned char point_format; enum mbedtls_ecp_group_id grp_id; enum mbedtls_ecdh_variant var; union { struct mbedtls_ecdh_context_mbed mbed_ecdh; } ctx; }


decompiled_code:
__int64 __fastcall mbedtls_ecdh_free(__int64 a1)
{
__int64 result; // rax

if ( a1 )
{
if ( *(_DWORD *)(a1 + 8) == 1 )
result = ecdh_free_internal(a1 + 16);
*(_BYTE *)a1 = 0;
*(_DWORD *)(a1 + 8) = 0;
*(_DWORD *)(a1 + 4) = 0;
}
return result;
}

original_code:
void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
{
    if (ctx == ((void *)0)) {
        return;
    }
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            ecdh_free_internal(&ctx->ctx.mbed_ecdh);
            break;
        default:
            break;
    }
    ctx->point_format = 0;
    ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
    ctx->grp_id = MBEDTLS_ECP_DP_NONE;
}

canonical_original_code:
void mbedtls_ecdh_free(struct mbedtls_ecdh_context *ctx)
{
    if (ctx == ((void *)0)) {
        return;
    }
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            ecdh_free_internal(&ctx->ctx.mbed_ecdh);
            break;
        default:
            break;
    }
    ctx->point_format = 0;
    ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
    ctx->grp_id = MBEDTLS_ECP_DP_NONE;
}

user_defined_types: enum mbedtls_ecdh_variant { MBEDTLS_ECDH_VARIANT_NONE=0, MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0=1 }


decompiled_code:
__int64 __fastcall mbedtls_ecdh_make_params(unsigned __int8 *a1, _QWORD *a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 result; // rax

if ( *((_DWORD *)a1 + 2) == 1 )
result = ecdh_make_params_internal((__int64)(a1 + 16), a2, *a1, a3, a4, a5, a6);
else
result = 4294946944LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(unsigned __int8 *a1, _QWORD *a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 result; // rax

if ( *((_DWORD *)a1 + 2) == 1 )
result = func11((__int64)(a1 + 16), a2, *a1, a3, a4, a5, a6);
else
result = 4294946944LL;
return result;
}

original_code:
int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,
                             unsigned char *buf, size_t blen,
                             int (*f_rng)(void *, unsigned char *, size_t),
                             void *p_rng)
{
    int restart_enabled = 0;
    (void) restart_enabled;
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_make_params_internal(&ctx->ctx.mbed_ecdh, olen,
                                             ctx->point_format, buf, blen,
                                             f_rng, p_rng,
                                             restart_enabled);
        default:
            return -0x4F80;
    }
}

canonical_original_code:
int mbedtls_ecdh_make_params(struct mbedtls_ecdh_context *ctx, unsigned long *olen, unsigned char *buf, unsigned long blen, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    int restart_enabled = 0;
    (void) restart_enabled;
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_make_params_internal(&ctx->ctx.mbed_ecdh, olen,
                                             ctx->point_format, buf, blen,
                                             f_rng, p_rng,
                                             restart_enabled);
        default:
            return -0x4F80;
    }
}

variable_types:
olen
unsigned long *


decompiled_code:
__int64 __fastcall mbedtls_ecdh_read_params(__int64 a1, _QWORD *a2, __int64 a3)
{
__int64 result; // rax
unsigned int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

result = mbedtls_ecp_tls_read_group_id(v5, a2, a3 - *a2);
if ( !(_DWORD)result )
{
result = mbedtls_ecdh_setup(a1, v5[0]);
if ( !(_DWORD)result )
{
if ( *(_DWORD *)(a1 + 8) == 1 )
result = ecdh_read_params_internal(a1 + 16, a2, a3);
else
result = 4294946944LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, _QWORD *a2, __int64 a3)
{
__int64 result; // rax
unsigned int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

result = mbedtls_ecp_tls_read_group_id(v5, a2, a3 - *a2);
if ( !(_DWORD)result )
{
result = func16(a1, v5[0]);
if ( !(_DWORD)result )
{
if ( *(_DWORD *)(a1 + 8) == 1 )
result = func6(a1 + 16, a2, a3);
else
result = 4294946944LL;
}
}
return result;
}

original_code:
int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx,
                             const unsigned char **buf,
                             const unsigned char *end)
{
    int ret = -0x006E;
    mbedtls_ecp_group_id grp_id;
    if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, end - *buf))
        != 0) {
        return ret;
    }
    if ((ret = mbedtls_ecdh_setup(ctx, grp_id)) != 0) {
        return ret;
    }
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_read_params_internal(&ctx->ctx.mbed_ecdh,
                                             buf, end);
        default:
            return -0x4F80;
    }
}

canonical_original_code:
int mbedtls_ecdh_read_params(struct mbedtls_ecdh_context *ctx, unsigned char **buf, unsigned char *end)
{
    int ret = -0x006E;
    enum mbedtls_ecp_group_id grp_id;
    if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, end - *buf))
        != 0) {
        return ret;
    }
    if ((ret = mbedtls_ecdh_setup(ctx, grp_id)) != 0) {
        return ret;
    }
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_read_params_internal(&ctx->ctx.mbed_ecdh,
                                             buf, end);
        default:
            return -0x4F80;
    }
}


decompiled_code:
__int64 __fastcall mbedtls_ecdh_get_params(__int64 a1, unsigned int *a2, unsigned int a3)
{
int v4; // eax
__int64 result; // rax

if ( a3 > 1 )
return 4294946944LL;
v4 = mbedtls_ecdh_grp_id(a1);
if ( v4 )
{
if ( v4 != *a2 )
return 4294946944LL;
}
else
{
result = mbedtls_ecdh_setup(a1, *a2);
if ( (_DWORD)result )
return result;
}
if ( *(_DWORD *)(a1 + 8) == 1 )
result = ecdh_get_params_internal(a1 + 16, (__int64)a2, a3);
else
result = 4294946944LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1, unsigned int *a2, unsigned int a3)
{
int v4; // eax
__int64 result; // rax

if ( a3 > 1 )
return 4294946944LL;
v4 = func0(a1);
if ( v4 )
{
if ( v4 != *a2 )
return 4294946944LL;
}
else
{
result = func16(a1, *a2);
if ( (_DWORD)result )
return result;
}
if ( *(_DWORD *)(a1 + 8) == 1 )
result = func8(a1 + 16, (__int64)a2, a3);
else
result = 4294946944LL;
return result;
}

original_code:
int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
                            const mbedtls_ecp_keypair *key,
                            mbedtls_ecdh_side side)
{
    int ret = -0x006E;
    if (side != MBEDTLS_ECDH_OURS && side != MBEDTLS_ECDH_THEIRS) {
        return -0x4F80;
    }
    if (mbedtls_ecdh_grp_id(ctx) == MBEDTLS_ECP_DP_NONE) {
        if ((ret = mbedtls_ecdh_setup(ctx, key->grp.id)) != 0) {
            return ret;
        }
    } else {
        if (mbedtls_ecdh_grp_id(ctx) != key->grp.id) {
            return -0x4F80;
        }
    }
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_get_params_internal(&ctx->ctx.mbed_ecdh,
                                            key, side);
        default:
            return -0x4F80;
    }
}

canonical_original_code:
int mbedtls_ecdh_get_params(struct mbedtls_ecdh_context *ctx, struct mbedtls_ecp_keypair *key, enum mbedtls_ecdh_side side)
{
    int ret = -0x006E;
    if (side != MBEDTLS_ECDH_OURS && side != MBEDTLS_ECDH_THEIRS) {
        return -0x4F80;
    }
    if (mbedtls_ecdh_grp_id(ctx) == MBEDTLS_ECP_DP_NONE) {
        if ((ret = mbedtls_ecdh_setup(ctx, key->grp.id)) != 0) {
            return ret;
        }
    } else {
        if (mbedtls_ecdh_grp_id(ctx) != key->grp.id) {
            return -0x4F80;
        }
    }
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_get_params_internal(&ctx->ctx.mbed_ecdh,
                                            key, side);
        default:
            return -0x4F80;
    }
}

user_defined_types: enum mbedtls_ecdh_side { MBEDTLS_ECDH_OURS=0, MBEDTLS_ECDH_THEIRS=1 }

user_defined_types: struct mbedtls_ecp_keypair { struct mbedtls_ecp_group grp; struct mbedtls_mpi d; struct mbedtls_ecp_point Q; }


decompiled_code:
__int64 __fastcall mbedtls_ecdh_make_public(unsigned __int8 *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 result; // rax

if ( *((_DWORD *)a1 + 2) == 1 )
result = ecdh_make_public_internal((__int64)(a1 + 16), a2, *a1, a3, a4, a5, a6);
else
result = 4294946944LL;
return result;
}

original_code:
int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,
                             unsigned char *buf, size_t blen,
                             int (*f_rng)(void *, unsigned char *, size_t),
                             void *p_rng)
{
    int restart_enabled = 0;
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_make_public_internal(&ctx->ctx.mbed_ecdh, olen,
                                             ctx->point_format, buf, blen,
                                             f_rng, p_rng,
                                             restart_enabled);
        default:
            return -0x4F80;
    }
}

canonical_original_code:
int mbedtls_ecdh_make_public(struct mbedtls_ecdh_context *ctx, unsigned long *olen, unsigned char *buf, unsigned long blen, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    int restart_enabled = 0;
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_make_public_internal(&ctx->ctx.mbed_ecdh, olen,
                                             ctx->point_format, buf, blen,
                                             f_rng, p_rng,
                                             restart_enabled);
        default:
            return -0x4F80;
    }
}


decompiled_code:
__int64 __fastcall mbedtls_ecdh_read_public(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 8) == 1 )
result = ecdh_read_public_internal(a1 + 16, a2, a3);
else
result = 4294946944LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func22(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 8) == 1 )
result = func7(a1 + 16, a2, a3);
else
result = 4294946944LL;
return result;
}

original_code:
int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
                             const unsigned char *buf, size_t blen)
{
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_read_public_internal(&ctx->ctx.mbed_ecdh,
                                             buf, blen);
        default:
            return -0x4F80;
    }
}

canonical_original_code:
int mbedtls_ecdh_read_public(struct mbedtls_ecdh_context *ctx, unsigned char *buf, unsigned long blen)
{
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_read_public_internal(&ctx->ctx.mbed_ecdh,
                                             buf, blen);
        default:
            return -0x4F80;
    }
}


decompiled_code:
__int64 __fastcall mbedtls_ecdh_calc_secret(__int64 a1, _QWORD *a2, __int64 a3, unsigned __int64 a4, __int64 a5, __int64 a6)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 8) == 1 )
result = ecdh_calc_secret_internal(a1 + 16, a2, a3, a4, a5, a6);
else
result = 4294946944LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func23(__int64 a1, _QWORD *a2, __int64 a3, unsigned __int64 a4, __int64 a5, __int64 a6)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 8) == 1 )
result = func14(a1 + 16, a2, a3, a4, a5, a6);
else
result = 4294946944LL;
return result;
}

original_code:
int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
                             unsigned char *buf, size_t blen,
                             int (*f_rng)(void *, unsigned char *, size_t),
                             void *p_rng)
{
    int restart_enabled = 0;
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_calc_secret_internal(&ctx->ctx.mbed_ecdh, olen, buf,
                                             blen, f_rng, p_rng,
                                             restart_enabled);
        default:
            return -0x4F80;
    }
}

canonical_original_code:
int mbedtls_ecdh_calc_secret(struct mbedtls_ecdh_context *ctx, unsigned long *olen, unsigned char *buf, unsigned long blen, int (*f_rng)(void *, unsigned char *, unsigned long), void *p_rng)
{
    int restart_enabled = 0;
    switch (ctx->var) {
        case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
            return ecdh_calc_secret_internal(&ctx->ctx.mbed_ecdh, olen, buf,
                                             blen, f_rng, p_rng,
                                             restart_enabled);
        default:
            return -0x4F80;
    }
}