// File: /workspace/github_c_makefile_star2_O3/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 %rsi, %rax
movzbl 0x28(%rdi), %r12d
leaq 0x3767(%rip), %rsi      # 0x65f5
testq %r12, %r12
je 0x2f3c
movq %rax, -0x48(%rbp)
movq %rdx, -0x50(%rbp)
movq 0x30(%rdi), %r15
xorl %ebx, %ebx
movq $0x0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq (%r15,%rbx,8), %r14
movl (%r14), %eax
cmpl $0x6, %eax
je 0x2ee2
cmpl $0x5, %eax
jne 0x2f0a
cmpw $0x1, 0x4(%r14)
jne 0x2f0a
movq 0x8(%r14), %rax
movb (%rax), %al
movq %rax, -0x38(%rbp)
jmp 0x2f0a
movzwl 0x4(%r14), %eax
movq %rax, -0x40(%rbp)
movzwl %ax, %r13d
movq %r13, %rdi
callq 0x23c0
movq 0x8(%r14), %rsi
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %r13, %rdx
callq 0x2260
incq %rbx
cmpq %rbx, %r12
jne 0x2ebd
movq -0x38(%rbp), %rsi
testb %sil, %sil
je 0x2f40
movq -0x30(%rbp), %rbx
testq %rbx, %rbx
sete %al
movq -0x40(%rbp), %rdx
testw %dx, %dx
sete %cl
orb %al, %cl
je 0x2f79
leaq 0x3700(%rip), %rsi      # 0x663a
jmp 0x2f4b
xorl %ebx, %ebx
jmp 0x2f4b
movq -0x30(%rbp), %rbx
leaq 0x36aa(%rip), %rsi      # 0x65f5
xorl %r14d, %r14d
movl $0x4, %edi
xorl %eax, %eax
callq 0x3fa4
testq %rbx, %rbx
je 0x2f67
movq %rbx, %rdi
callq 0x2340
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1, %sil
jne 0x2fae
movzwl %dx, %edx
movq -0x48(%rbp), %rdi
movq %rbx, %rsi
movq -0x50(%rbp), %rcx
callq 0x2ff9
testb %al, %al
je 0x2fca
leaq 0x371b(%rip), %rsi      # 0x66b8
movl $0x1, %edi
xorl %eax, %eax
callq 0x3fa4
movb $0x1, %r14b
jmp 0x2f5f
movzbl %sil, %edx
leaq 0x36c6(%rip), %rsi      # 0x667f
xorl %r14d, %r14d
movl $0x3, %edi
xorl %eax, %eax
callq 0x3fa4
jmp 0x2f5f
xorl %r14d, %r14d
jmp 0x2f5f