// File: /workspace/github_cpp_makefile_star2_O3/DiplIngFrankGerlach[P]MST/./aes/aes.c
// Function: aes_encrypt_ccm
int aes_encrypt_ccm(const BYTE payload[], WORD payload_len, const BYTE assoc[], unsigned short assoc_len,
                    const BYTE nonce[], unsigned short nonce_len, BYTE out[], WORD *out_len,
                    WORD mac_len, const BYTE key_str[], int keysize)
{
	BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], *buf;
	int end_of_buf, payload_len_store_size;
	WORD key[60];

	if (mac_len != 4 && mac_len != 6 && mac_len != 8 && mac_len != 10 &&
	   mac_len != 12 && mac_len != 14 && mac_len != 16)
		return(FALSE);

	if (nonce_len < 7 || nonce_len > 13)
		return(FALSE);

	if (assoc_len > 32768 /* = 2^15 */)
		return(FALSE);

	buf = (BYTE*)malloc(payload_len + assoc_len + 48 /*Round both payload and associated data up a block size and add an extra block.*/);
	if (! buf)
		return(FALSE);

	// Prepare the key for usage.
	aes_key_setup(key_str, key, keysize);

	// Format the first block of the formatted data.
	payload_len_store_size = AES_BLOCK_SIZE - 1 - nonce_len;
	ccm_prepare_first_format_blk(buf, assoc_len, payload_len, payload_len_store_size, mac_len, nonce, nonce_len);
	end_of_buf = AES_BLOCK_SIZE;

	// Format the Associated Data, aka, assoc[].
	ccm_format_assoc_data(buf, &end_of_buf, assoc, assoc_len);

	// Format the Payload, aka payload[].
	ccm_format_payload_data(buf, &end_of_buf, payload, payload_len);

	// Create the first counter block.
	ccm_prepare_first_ctr_blk(counter, nonce, nonce_len, payload_len_store_size);

	// Perform the CBC operation with an IV of zeros on the formatted buffer to calculate the MAC.
	memset(temp_iv, 0, AES_BLOCK_SIZE);
	aes_encrypt_cbc_mac(buf, end_of_buf, mac, key, keysize, temp_iv);

	// Copy the Payload and MAC to the output buffer.
	memcpy(out, payload, payload_len);
	memcpy(&out[payload_len], mac, mac_len);

	// Encrypt the Payload with CTR mode with a counter starting at 1.
	memcpy(temp_iv, counter, AES_BLOCK_SIZE);
	increment_iv(temp_iv, AES_BLOCK_SIZE - 1 - mac_len);   // Last argument is the byte size of the counting portion of the counter block. /*BUG?*/
	aes_encrypt_ctr(out, payload_len, out, key, keysize, temp_iv);

	// Encrypt the MAC with CTR mode with a counter starting at 0.
	aes_encrypt_ctr(&out[payload_len], mac_len, &out[payload_len], key, keysize, counter);

	free(buf);
	*out_len = payload_len + mac_len;

	return(TRUE);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movl 0x1a0(%rsp), %r12d
xorl %r14d, %r14d
cmpl $0x10, %r12d
ja 0x24cc
movl $0x15550, %eax          # imm = 0x15550
btl %r12d, %eax
jae 0x24cc
movl %r9d, %r15d
leal -0xe(%r15), %eax
cmpw $-0x7, %ax
jb 0x24cc
movzwl %cx, %ebx
cmpl $0x8000, %ebx           # imm = 0x8000
ja 0x24cc
movq %rdx, %r13
movq %r8, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
leal (%rsi,%rbx), %edi
addl $0x30, %edi
movq %rsi, 0x8(%rsp)
callq 0x1160
testq %rax, %rax
je 0x24cc
movq %rax, %rbp
movl 0x1b0(%rsp), %edx
movq 0x1a8(%rsp), %rdi
movzwl %r15w, %r15d
leaq 0x60(%rsp), %rsi
callq 0x24e1
movl $0xf, %r14d
subl %r15d, %r14d
leal 0x38(,%r12,4), %eax
andl $0x38, %eax
movl $0x6, %ecx
subl %r15d, %ecx
movq %r15, 0x38(%rsp)
andl $0x7, %ecx
orl %eax, %ecx
leal 0x40(%rcx), %eax
testw %bx, %bx
movzbl %al, %eax
cmovel %ecx, %eax
movb %al, (%rbp)
leaq 0x1(%rbp), %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x10e0
leaq (%r15,%rbp), %rdi
incq %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1080
movq 0x8(%rsp), %rax
movb %al, 0xf(%rbp)
movb %ah, 0xe(%rbp)
movb %bl, 0x11(%rbp)
movb %bh, 0x10(%rbp)
leaq 0x12(%rbp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x10e0
leal 0x12(%rbx), %eax
andl $0xf, %eax
movl $0x10, %r13d
movl $0x10, %r15d
subl %eax, %r15d
movl %ebx, %eax
leaq (%rax,%rbp), %rdi
addq $0x12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1080
addl %r15d, %ebx
addl $0x12, %ebx
leaq (%rbx,%rbp), %rdi
movq 0x8(%rsp), %r15
movslq %r15d, %rdx
movq 0x18(%rsp), %rsi
callq 0x10e0
movl %ebx, %eax
addl %r15d, %eax
leal 0xf(%r15,%rbx), %ecx
cmovnsl %eax, %ecx
andl $-0x10, %ecx
movslq %eax, %rbx
subl %ecx, %eax
subl %eax, %r13d
testl %eax, %eax
cmovel %eax, %r13d
movq %rbp, %rdi
addq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x1080
addl %r13d, %ebx
leaq 0x51(%rsp), %r13
xorl %eax, %eax
movq %rax, (%r13)
movq %rax, 0x7(%r13)
decb %r14b
andb $0x7, %r14b
movb %r14b, -0x1(%r13)
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x10e0
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r9
movaps %xmm0, (%r9)
movslq %ebx, %rsi
leaq 0x40(%rsp), %rdx
movq %rbp, %rdi
leaq 0x60(%rsp), %rcx
movl 0x1b0(%rsp), %r8d
callq 0x19fa
movl %r15d, %r15d
movq 0x190(%rsp), %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x10e0
addq %r15, %rbx
movl %r12d, %r14d
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x10e0
movaps -0x1(%r13), %xmm0
leaq 0x20(%rsp), %rax
movaps %xmm0, (%rax)
cmpl $0xe, %r12d
ja 0x246a
leal 0x1(%r12), %eax
movl $0xf, %ecx
incb 0x20(%rsp,%rcx)
sete %dl
cmpq %rax, %rcx
jbe 0x246a
decq %rcx
testb %dl, %dl
jne 0x2457
leaq 0x60(%rsp), %r13
leaq 0x20(%rsp), %r9
movq 0x190(%rsp), %rdi
movq %r15, %rsi
movq %rdi, %rdx
movq %r13, %rcx
movl 0x1b0(%rsp), %r15d
movl %r15d, %r8d
callq 0x2163
leaq 0x50(%rsp), %r9
movq %rbx, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
movl %r15d, %r8d
callq 0x2163
movq %rbp, %rdi
callq 0x1140
addl 0x8(%rsp), %r12d
movq 0x198(%rsp), %rax
movl %r12d, (%rax)
movl $0x1, %r14d
movl %r14d, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq