// File: /workspace/github_c_makefile_star2_O0/TimeToogo[P]ff-proxy/client/c/crypto.c
// Function: ff_client_encrypt_request_aes_256_gcm_pbkdf2
bool ff_client_encrypt_request_aes_256_gcm_pbkdf2(
    struct ff_request *request,
    struct ff_encryption_config *config,
    uint8_t **iv,
    uint16_t *iv_len,
    uint8_t **tag,
    uint16_t *tag_len,
    uint8_t **salt,
    uint16_t *salt_len)
{
    EVP_CIPHER_CTX *ctx = NULL;
    int len;
    bool ret_val;

    uint8_t *ciphertext_buff = malloc(request->payload_length * sizeof(uint8_t));
    int ciphertext_len = 0;
    struct ff_request_payload_node *payload_chunk = request->payload;
    struct ff_derived_key *key = ff_derived_key_alloc(32);

    *salt_len = 16;
    *salt = (uint8_t *)calloc(1, *salt_len);

    if (!RAND_bytes(*salt, *salt_len))
    {
        ff_log(FF_ERROR, "Failed to generate salt while encrypting request");
        goto error;
    }

    if (!ff_derive_key_pbkdf2(config, *salt, *salt_len, key))
    {
        ff_log(FF_ERROR, "Failed to derive encryption key while encrypting request");
        goto error;
    }

    *iv_len = 12;
    *iv = calloc(1, *iv_len);

    if (!RAND_bytes(*iv, *iv_len))
    {
        ff_log(FF_ERROR, "Failed to generate IV while encrypting request");
        goto error;
    }

    if (!(ctx = EVP_CIPHER_CTX_new()))
    {
        ff_log(FF_ERROR, "Failed to create new OpenSSL cipher");
        goto error;
    }

    if (!EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
    {
        ff_log(FF_ERROR, "Failed to init OpenSSL cipher");
        goto error;
    }

    if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, *iv_len, NULL))
    {
        ff_log(FF_ERROR, "Failed to update OpenSSL cipher - IV length");
        goto error;
    }

    if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key->key, *iv))
    {
        ff_log(FF_ERROR, "Failed to init OpenSSL cipher with key and IV");
        goto error;
    }

    do
    {
        if (!EVP_EncryptUpdate(ctx, ciphertext_buff + ciphertext_len, &len, payload_chunk->value, payload_chunk->length))
        {
            ff_log(FF_ERROR, "Failed encrypt request payload");
            goto error;
        }

        ciphertext_len += len;

    } while ((payload_chunk = payload_chunk->next) != NULL);

    if (!EVP_EncryptFinal_ex(ctx, ciphertext_buff + ciphertext_len, &len))
    {
        ff_log(FF_ERROR, "Failed encrypt and finalize request payload");
        goto error;
    }

    ciphertext_len += len;
    *tag_len = 16;
    *tag = calloc(1, 16);

    if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, *tag_len, *tag))
    {
        ff_log(FF_ERROR, "Failed to get cipher tag");
        goto error;
    }

    payload_chunk = request->payload;
    struct ff_request_payload_node *tmp_payload;

    while (payload_chunk != NULL)
    {
        tmp_payload = payload_chunk;
        payload_chunk = payload_chunk->next;
        ff_request_payload_node_free(tmp_payload);
    }

    request->payload = ff_request_payload_node_alloc();
    request->payload->length = ciphertext_len;
    request->payload->offset = 0;
    request->payload->next = NULL;
    request->payload->value = ciphertext_buff;
    request->payload_length = ciphertext_len;
    goto done;

error:
    FREE(ciphertext_buff);
    ret_val = false;
    goto cleanup;

done:
    ret_val = true;
    goto cleanup;

cleanup:
    EVP_CIPHER_CTX_free(ctx);
    ff_derived_key_free(key);

    return ret_val;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdi
shlq $0x0, %rdi
callq 0x2250
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x58(%rbp)
movl $0x20, %edi
callq 0x4f90
movq %rax, -0x60(%rbp)
movq 0x18(%rbp), %rax
movw $0x10, (%rax)
movq 0x18(%rbp), %rax
movzwl (%rax), %eax
movl %eax, %esi
movl $0x1, %edi
callq 0x2140
movq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq 0x10(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rbp), %rax
movzwl (%rax), %esi
callq 0x2270
cmpl $0x0, %eax
jne 0x3b2c
movl $0x4, %edi
leaq 0x29cc(%rip), %rsi      # 0x64ec
movb $0x0, %al
callq 0x4480
jmp 0x3e20
movq -0x10(%rbp), %rdi
movq 0x10(%rbp), %rax
movq (%rax), %rsi
movq 0x18(%rbp), %rax
movw (%rax), %ax
movq -0x60(%rbp), %rcx
movzwl %ax, %edx
callq 0x5210
testb $0x1, %al
jne 0x3b66
movl $0x4, %edi
leaq 0x29c3(%rip), %rsi      # 0x651d
movb $0x0, %al
callq 0x4480
jmp 0x3e20
movq -0x20(%rbp), %rax
movw $0xc, (%rax)
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
movl %eax, %esi
movl $0x1, %edi
callq 0x2140
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movzwl (%rax), %esi
callq 0x2270
cmpl $0x0, %eax
jne 0x3bbc
movl $0x4, %edi
leaq 0x29a6(%rip), %rsi      # 0x6556
movb $0x0, %al
callq 0x4480
jmp 0x3e20
callq 0x2130
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x3be3
movl $0x4, %edi
leaq 0x29ae(%rip), %rsi      # 0x6585
movb $0x0, %al
callq 0x4480
jmp 0x3e20
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
callq 0x22b0
movq -0x70(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x2110
cmpl $0x0, %eax
jne 0x3c24
movl $0x4, %edi
leaq 0x2991(%rip), %rsi      # 0x65a9
movb $0x0, %al
callq 0x4480
jmp 0x3e20
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rax
movzwl (%rax), %edx
movl $0x9, %esi
xorl %eax, %eax
movl %eax, %ecx
callq 0x2280
cmpl $0x0, %eax
jne 0x3c5a
movl $0x4, %edi
leaq 0x2979(%rip), %rsi      # 0x65c7
movb $0x0, %al
callq 0x4480
jmp 0x3e20
movq -0x38(%rbp), %rdi
movq -0x60(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq (%rax), %r8
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x2110
cmpl $0x0, %eax
jne 0x3c95
movl $0x4, %edi
leaq 0x296a(%rip), %rsi      # 0x65f3
movb $0x0, %al
callq 0x4480
jmp 0x3e20
jmp 0x3c97
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movslq -0x4c(%rbp), %rax
addq %rax, %rsi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x58(%rbp), %rax
movzwl 0x2(%rax), %r8d
leaq -0x3c(%rbp), %rdx
callq 0x2070
cmpl $0x0, %eax
jne 0x3cdd
movl $0x4, %edi
leaq 0x2950(%rip), %rsi      # 0x6621
movb $0x0, %al
callq 0x4480
jmp 0x3e20
movl -0x3c(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x3c97
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movslq -0x4c(%rbp), %rax
addq %rax, %rsi
leaq -0x3c(%rbp), %rdx
callq 0x22c0
cmpl $0x0, %eax
jne 0x3d2d
movl $0x4, %edi
leaq 0x291f(%rip), %rsi      # 0x6640
movb $0x0, %al
callq 0x4480
jmp 0x3e20
movl -0x3c(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movw $0x10, (%rax)
movl $0x1, %edi
movl $0x10, %esi
callq 0x2140
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rax
movzwl (%rax), %edx
movq -0x28(%rbp), %rax
movq (%rax), %rcx
movl $0x10, %esi
callq 0x2280
cmpl $0x0, %eax
jne 0x3d91
movl $0x4, %edi
leaq 0x28e7(%rip), %rsi      # 0x666c
movb $0x0, %al
callq 0x4480
jmp 0x3e20
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x3dc3
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x68(%rbp), %rdi
callq 0x47a0
jmp 0x3d9d
callq 0x4700
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
movl -0x4c(%rbp), %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movw %cx, 0x2(%rax)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movw $0x0, (%rax)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq $0x0, 0x10(%rax)
movq -0x48(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x8(%rax)
movslq -0x4c(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x40(%rax)
jmp 0x3e39
jmp 0x3e22
movq -0x48(%rbp), %rdi
callq 0x2200
movq $0x0, -0x48(%rbp)
movb $0x0, -0x3d(%rbp)
jmp 0x3e3d
movb $0x1, -0x3d(%rbp)
movq -0x38(%rbp), %rdi
callq 0x21e0
movq -0x60(%rbp), %rdi
callq 0x51b0
movb -0x3d(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)