// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %rcx, -0x30(%rbp)
movq %rdx, -0x60(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq 0x18(%rbp), %r12
movq 0x10(%rbp), %r14
movq 0x40(%rdi), %rdi
callq 0x2220
movq %rax, -0x40(%rbp)
movq %rbx, -0x48(%rbp)
movq 0x50(%rbx), %rbx
movl $0x20, %edi
callq 0x41cc
movq %rax, %r13
movw $0x10, (%r12)
movl $0x1, %edi
movl $0x10, %esi
callq 0x2120
movq %rax, (%r14)
movq %rax, %rdi
movl $0x10, %esi
callq 0x2240
testl %eax, %eax
je 0x3501
movq (%r14), %rsi
movzwl (%r12), %edx
movq %r15, %rdi
movq %r13, %rcx
callq 0x4389
testb %al, %al
je 0x350a
movq -0x30(%rbp), %rax
movw $0xc, (%rax)
movl $0x1, %edi
movl $0xc, %esi
callq 0x2120
movq -0x60(%rbp), %r14
movq %rax, (%r14)
movq %rax, %rdi
movl $0xc, %esi
callq 0x2240
testl %eax, %eax
movq -0x40(%rbp), %r12
je 0x3526
callq 0x2110
testq %rax, %rax
je 0x352f
movq %rax, %r15
callq 0x2280
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20f0
testl %eax, %eax
je 0x3572
movq -0x30(%rbp), %rax
movzwl (%rax), %edx
movq %r15, %rdi
movl $0x9, %esi
xorl %ecx, %ecx
callq 0x2250
testl %eax, %eax
je 0x357b
movq %r13, -0x30(%rbp)
movq (%r13), %rcx
movq (%r14), %r8
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x20f0
testl %eax, %eax
je 0x3584
movslq %r13d, %r14
leaq (%r12,%r14), %rsi
movq 0x8(%rbx), %rcx
movzwl 0x2(%rbx), %r8d
movq %r15, %rdi
leaq -0x34(%rbp), %rdx
callq 0x2060
testl %eax, %eax
je 0x358d
movslq -0x34(%rbp), %r13
addq %r14, %r13
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x3424
movq %r12, %rsi
addq %r13, %rsi
leaq -0x34(%rbp), %rdx
movq %r15, %rdi
callq 0x2290
testl %eax, %eax
je 0x3596
movslq -0x34(%rbp), %rbx
movq -0x50(%rbp), %rax
movw $0x10, (%rax)
movl $0x1, %edi
movl $0x10, %esi
callq 0x2120
movq -0x58(%rbp), %rcx
movq %rax, (%rcx)
movq %r15, %rdi
movl $0x10, %esi
movl $0x10, %edx
movq %rax, %rcx
callq 0x2250
testl %eax, %eax
je 0x359f
movslq %r13d, %rax
addq %rax, %rbx
movq -0x48(%rbp), %r13
movq 0x50(%r13), %rdi
testq %rdi, %rdi
je 0x34d6
movq 0x10(%rdi), %r14
callq 0x3c25
movq %r14, %rdi
testq %r14, %r14
jne 0x34c5
callq 0x3bc9
movq %rax, 0x50(%r13)
movw %bx, 0x2(%rax)
movw $0x0, (%rax)
movq $0x0, 0x10(%rax)
movq %r12, 0x8(%rax)
movq %rbx, 0x40(%r13)
movb $0x1, %r14b
movq -0x30(%rbp), %r13
jmp 0x3550
leaq 0x1fc4(%rip), %rsi      # 0x54cc
jmp 0x3511
leaq 0x1fec(%rip), %rsi      # 0x54fd
xorl %r15d, %r15d
movl $0x4, %edi
xorl %eax, %eax
callq 0x3a20
movq -0x40(%rbp), %r12
jmp 0x3545
leaq 0x2009(%rip), %rsi      # 0x5536
jmp 0x3536
leaq 0x202f(%rip), %rsi      # 0x5565
xorl %r15d, %r15d
movl $0x4, %edi
xorl %eax, %eax
callq 0x3a20
movq %r12, %rdi
callq 0x21d0
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x21b0
movq %r13, %rdi
callq 0x435f
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2010(%rip), %rsi      # 0x5589
jmp 0x3539
leaq 0x2025(%rip), %rsi      # 0x55a7
jmp 0x3539
leaq 0x2048(%rip), %rsi      # 0x55d3
jmp 0x35a6
leaq 0x206d(%rip), %rsi      # 0x5601
jmp 0x35a6
leaq 0x2083(%rip), %rsi      # 0x5620
jmp 0x35a6
leaq 0x20a6(%rip), %rsi      # 0x564c
movl $0x4, %edi
xorl %eax, %eax
callq 0x3a20
movq -0x30(%rbp), %r13
jmp 0x3545