// File: /workspace/github_c_makefile_star2_O0/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
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movq $0x0, -0x28(%rbp)
movw $0x0, -0x2a(%rbp)
movb $0x0, -0x2c(%rbp)
movzbl -0x2c(%rbp), %eax
movq -0x8(%rbp), %rcx
movzbl 0x28(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3258
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
subl $0x5, %eax
je 0x31c7
jmp 0x31ba
movl -0x30(%rbp), %eax
subl $0x6, %eax
je 0x31fb
jmp 0x3247
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movzwl 0x4(%rax), %eax
cmpl $0x1, %eax
jne 0x31f9
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rax
movb (%rax), %al
movb %al, -0x19(%rbp)
jmp 0x3249
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movw 0x4(%rax), %ax
movw %ax, -0x2a(%rbp)
movzwl -0x2a(%rbp), %eax
movl %eax, %edi
callq 0x23e0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rsi
movzwl -0x2a(%rbp), %eax
movl %eax, %edx
callq 0x2270
jmp 0x3249
jmp 0x3249
jmp 0x324b
movb -0x2c(%rbp), %al
addb $0x1, %al
movb %al, -0x2c(%rbp)
jmp 0x318a
movzbl -0x19(%rbp), %eax
cmpl $0x0, %eax
jne 0x3276
movl $0x4, %edi
leaq 0x43b8(%rip), %rsi      # 0x7625
movb $0x0, %al
callq 0x4d70
jmp 0x32df
cmpq $0x0, -0x28(%rbp)
je 0x3286
movzwl -0x2a(%rbp), %eax
cmpl $0x0, %eax
jne 0x329b
movl $0x4, %edi
leaq 0x43d8(%rip), %rsi      # 0x766a
movb $0x0, %al
callq 0x4d70
jmp 0x32df
movb -0x19(%rbp), %al
subb $0x1, %al
jne 0x32c4
jmp 0x32a4
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movw -0x2a(%rbp), %ax
movq -0x18(%rbp), %rcx
movzwl %ax, %edx
callq 0x3390
testb $0x1, %al
jne 0x32c2
jmp 0x32df
jmp 0x32dd
movzbl -0x19(%rbp), %edx
movl $0x3, %edi
leaq 0x43db(%rip), %rsi      # 0x76af
movb $0x0, %al
callq 0x4d70
jmp 0x32df
jmp 0x32e5
movb $0x0, -0x2b(%rbp)
jmp 0x32fc
movb $0x1, -0x2b(%rbp)
movl $0x1, %edi
leaq 0x43f3(%rip), %rsi      # 0x76e8
movb $0x0, %al
callq 0x4d70
cmpq $0x0, -0x28(%rbp)
je 0x3318
jmp 0x3305
movq -0x28(%rbp), %rdi
callq 0x2350
movq $0x0, -0x28(%rbp)
jmp 0x3318
movb -0x2b(%rbp), %al
andb $0x1, %al
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)