// File: /workspace/github_c_makefile_star2_O1/TimeToogo[P]ff-proxy/src/crypto.c
// Function: ff_derive_key
bool ff_derive_key(
    struct ff_request *request,
    struct ff_encryption_config *config,
    struct ff_derived_key *out_key)
{
    uint8_t key_derivation_mode = 0;
    uint8_t *salt = NULL;
    uint16_t salt_length = 0;
    bool ret_val;

    for (uint8_t i = 0; i < request->options_length; i++)
    {
        switch (request->options[i]->type)
        {
        case FF_REQUEST_OPTION_TYPE_KEY_DERIVE_MODE:
            if (request->options[i]->length == 1)
            {
                key_derivation_mode = (uint8_t)*request->options[i]->value;
            };
            break;

        case FF_REQUEST_OPTION_TYPE_KEY_DERIVE_SALT:
            salt_length = request->options[i]->length;
            salt = malloc(salt_length);
            memcpy(salt, request->options[i]->value, salt_length);
            break;

        default:
            break;
        }
    }

    if (key_derivation_mode == 0)
    {
        ff_log(FF_ERROR, "Failed derive encryption key, incoming request does not specify mode");
        goto error;
    }

    if (salt == NULL || salt_length == 0)
    {
        ff_log(FF_ERROR, "Failed derive encryption key, incoming request does not specify salt");
        goto error;
    }

    switch (key_derivation_mode)
    {
    case FF_KEY_DERIVE_MODE_PBKDF2:
        if (!ff_derive_key_pbkdf2(config, salt, salt_length, out_key))
        {
            goto error;
        }
        break;

    default:
        ff_log(FF_WARNING, "Encountered request with unknown key derivation mode: %u", key_derivation_mode);
        goto error;
    }

    goto done;

error:
    ret_val = false;
    goto cleanup;

done:
    ret_val = true;
    ff_log(FF_DEBUG, "Successfully derived encryption key");
    goto cleanup;

cleanup:
    if (salt != NULL)
    {
        FREE(salt);
    }

    return ret_val;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, -0x50(%rbp)
movq %rsi, -0x48(%rbp)
movzbl 0x28(%rdi), %r15d
testq %r15, %r15
je 0x3052
movq 0x30(%rdi), %r14
xorl %ebx, %ebx
movq $0x0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq (%r14,%rbx,8), %r13
movl (%r13), %eax
cmpl $0x6, %eax
je 0x3020
cmpl $0x5, %eax
jne 0x3048
cmpw $0x1, 0x4(%r13)
jne 0x3048
movq 0x8(%r13), %rax
movb (%rax), %al
movq %rax, -0x38(%rbp)
jmp 0x3048
movzwl 0x4(%r13), %eax
movq %rax, -0x40(%rbp)
movzwl %ax, %r12d
movq %r12, %rdi
callq 0x23c0
movq 0x8(%r13), %rsi
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %r12, %rdx
callq 0x2260
incq %rbx
cmpq %rbx, %r15
jne 0x2ffa
jmp 0x306a
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rdx
testb %dl, %dl
je 0x30a5
movq -0x30(%rbp), %rbx
testq %rbx, %rbx
sete %al
movq -0x40(%rbp), %rsi
testw %si, %si
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x30c1
leaq 0x35a6(%rip), %rsi      # 0x663a
xorl %r14d, %r14d
movl $0x4, %edi
xorl %eax, %eax
callq 0x4100
jmp 0x3113
leaq 0x3549(%rip), %rsi      # 0x65f5
xorl %r14d, %r14d
movl $0x4, %edi
xorl %eax, %eax
callq 0x4100
movq -0x30(%rbp), %rbx
jmp 0x3113
movzbl %dl, %edx
cmpl $0x1, %edx
jne 0x30f8
movzwl %si, %edx
movq -0x48(%rbp), %rdi
movq %rbx, %rsi
movq -0x50(%rbp), %rcx
callq 0x3163
testb %al, %al
je 0x3110
leaq 0x35d1(%rip), %rsi      # 0x66b8
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
movb $0x1, %r14b
jmp 0x3113
leaq 0x3580(%rip), %rsi      # 0x667f
xorl %r14d, %r14d
movl $0x3, %edi
xorl %eax, %eax
callq 0x4100
jmp 0x3113
xorl %r14d, %r14d
testq %rbx, %rbx
je 0x3120
movq %rbx, %rdi
callq 0x2340
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq