// File: /workspace/github_c_makefile_star2_O2/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)
movq %rdi, %r12
movzbl 0x28(%rdi), %r14d
xorl %ebx, %ebx
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
cmpq %rbx, %r14
je 0x2e42
movq 0x30(%r12), %rax
movq (%rax,%rbx,8), %r15
movl (%r15), %eax
cmpl $0x6, %eax
je 0x2e15
cmpl $0x5, %eax
jne 0x2e3d
cmpw $0x1, 0x4(%r15)
jne 0x2e3d
movq 0x8(%r15), %rax
movb (%rax), %al
movq %rax, -0x38(%rbp)
jmp 0x2e3d
movzwl 0x4(%r15), %eax
movq %rax, -0x40(%rbp)
movzwl %ax, %r13d
movq %r13, %rdi
callq 0x23c0
movq 0x8(%r15), %rsi
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %r13, %rdx
callq 0x2260
incq %rbx
jmp 0x2de6
movq -0x38(%rbp), %rdx
testb %dl, %dl
je 0x2e7b
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 0x2e95
leaq 0x27ce(%rip), %rsi      # 0x563a
xorl %r14d, %r14d
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x3db4
jmp 0x2ee3
leaq 0x2773(%rip), %rsi      # 0x55f5
xorl %r14d, %r14d
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x3db4
movq -0x30(%rbp), %rbx
jmp 0x2ee3
cmpb $0x1, %dl
jne 0x2ec7
movzwl %si, %edx
movq -0x48(%rbp), %rdi
movq %rbx, %rsi
movq -0x50(%rbp), %rcx
callq 0x2f22
testb %al, %al
je 0x2ee0
leaq 0x2800(%rip), %rsi      # 0x56b8
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x3db4
movb $0x1, %r14b
jmp 0x2ee3
movzbl %dl, %edx
leaq 0x27ae(%rip), %rsi      # 0x567f
xorl %r14d, %r14d
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x3db4
jmp 0x2ee3
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x2340
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq