// File: /workspace/github_cpp_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movl 0x1b0(%rsp), %ebp
xorl %r14d, %r14d
cmpl $0x10, %ebp
ja 0x249a
movl $0x15550, %eax          # imm = 0x15550
btl %ebp, %eax
jae 0x249a
movl %r9d, %r15d
leal -0xe(%r15), %eax
cmpw $-0x7, %ax
jb 0x249a
cmpw $0x8000, %cx            # imm = 0x8000
ja 0x249a
movq %rdx, %r12
movq %rdi, %r13
movq %r8, 0x30(%rsp)
movzwl %cx, %ebx
leal (%rsi,%rbx), %edi
addl $0x30, %edi
movq %rsi, 0x10(%rsp)
callq 0x1150
testq %rax, %rax
je 0x249a
movl 0x1c0(%rsp), %edx
movq %rax, 0x20(%rsp)
movq 0x1b8(%rsp), %rdi
movzwl %r15w, %r15d
leaq 0x70(%rsp), %rsi
callq 0x24af
pushq $0xf
popq %r14
movq %r15, 0x38(%rsp)
subl %r15d, %r14d
movl %r15d, (%rsp)
movq %r13, 0x28(%rsp)
movq 0x20(%rsp), %r13
movq %r13, %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rdx
movl %r14d, %ecx
movl %ebp, %r8d
movq 0x30(%rsp), %r9
callq 0x257e
pushq $0x10
popq %r15
leaq 0x1c(%rsp), %rsi
movl %r15d, (%rsi)
movq %r13, %rdi
movq %r12, %rdx
movl %ebx, %ecx
callq 0x25e5
leaq 0x1c(%rsp), %rax
movslq (%rax), %r12
leaq (%r12,%r13), %rdi
movslq 0x10(%rsp), %rbx
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
callq 0x10e0
addq %rbx, %r12
movl %r12d, %eax
cltd
idivl %r15d
subl %edx, %r15d
testl %edx, %edx
cmovel %edx, %r15d
movq %r12, %rdi
addq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1090
leaq 0x51(%rsp), %rbx
andq $0x0, (%rbx)
andq $0x0, 0x7(%rbx)
addl %r12d, %r15d
decb %r14b
andb $0x7, %r14b
movb %r14b, -0x1(%rbx)
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x10e0
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %r14
movaps %xmm0, (%r14)
movslq %r15d, %rsi
leaq 0x40(%rsp), %rdx
movq %r13, %rdi
leaq 0x70(%rsp), %rcx
movl 0x1c0(%rsp), %r8d
movq %r14, %r9
callq 0x19f6
movl 0x10(%rsp), %r13d
movq 0x1a0(%rsp), %r15
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
callq 0x10e0
addq %r13, %r15
movl %ebp, %r12d
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
movq %r12, %rdx
callq 0x10e0
movaps -0x1(%rbx), %xmm0
movaps %xmm0, (%r14)
pushq $0xf
popq %rsi
subl %ebp, %esi
movq %r14, %rdi
callq 0x216d
movq 0x1a0(%rsp), %rdi
movq %r13, %rsi
movq %rdi, %rdx
leaq 0x70(%rsp), %r13
movq %r13, %rcx
movl 0x1c0(%rsp), %ebx
movl %ebx, %r8d
movq %r14, %r9
callq 0x219d
leaq 0x50(%rsp), %r9
movq %r15, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
movl %ebx, %r8d
callq 0x219d
movq 0x20(%rsp), %rdi
callq 0x1130
addl 0x10(%rsp), %ebp
movq 0x1a8(%rsp), %rax
movl %ebp, (%rax)
pushq $0x1
popq %r14
movl %r14d, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq