// File: /workspace/github_cpp_makefile_star2_O3/DiplIngFrankGerlach[P]MST/./aes/aes.c
// Function: aes_decrypt_ccm
int aes_decrypt_ccm(const BYTE ciphertext[], WORD ciphertext_len, const BYTE assoc[], unsigned short assoc_len,
                    const BYTE nonce[], unsigned short nonce_len, BYTE plaintext[], WORD *plaintext_len,
                    WORD mac_len, int *mac_auth, const BYTE key_str[], int keysize)
{
	BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], mac_buf[16], *buf;
	int end_of_buf, plaintext_len_store_size;
	WORD key[60];

	if (ciphertext_len <= mac_len)
		return(FALSE);

	buf = (BYTE*)malloc(assoc_len + ciphertext_len /*ciphertext_len = plaintext_len + mac_len*/ + 48);
	if (! buf)
		return(FALSE);

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

	// Copy the plaintext and MAC to the output buffers.
	*plaintext_len = ciphertext_len - mac_len;
	plaintext_len_store_size = AES_BLOCK_SIZE - 1 - nonce_len;
	memcpy(plaintext, ciphertext, *plaintext_len);
	memcpy(mac, &ciphertext[*plaintext_len], mac_len);

	// Prepare the first counter block for use in decryption.
	ccm_prepare_first_ctr_blk(counter, nonce, nonce_len, plaintext_len_store_size);

	// Decrypt 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);   // (AES_BLOCK_SIZE - 1 - mac_len) is the byte size of the counting portion of the counter block.
	aes_decrypt_ctr(plaintext, *plaintext_len, plaintext, key, keysize, temp_iv);

	// Setting mac_auth to NULL disables the authentication check.
	if (mac_auth != NULL) {
		// Decrypt the MAC with CTR mode with a counter starting at 0.
		aes_decrypt_ctr(mac, mac_len, mac, key, keysize, counter);

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

		// Format the Associated Data into the authentication buffer.
		ccm_format_assoc_data(buf, &end_of_buf, assoc, assoc_len);

		// Format the Payload into the authentication buffer.
		ccm_format_payload_data(buf, &end_of_buf, plaintext, *plaintext_len);

		// 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_buf, key, keysize, temp_iv);

		// Compare the calculated MAC against the MAC embedded in the ciphertext to see if they are the same.
		if (! memcmp(mac, mac_buf, mac_len)) {
			*mac_auth = TRUE;
		}
		else {
			*mac_auth = FALSE;
			memset(plaintext, 0, *plaintext_len);
		}
	}

	free(buf);

	return(TRUE);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movl 0x1c0(%rsp), %eax
xorl %r12d, %r12d
movl %esi, %r14d
subl %eax, %r14d
jbe 0x2a16
movl %r9d, %ebp
movq %rdx, %r13
movq %rdi, %r15
movq %r8, 0x10(%rsp)
movzwl %cx, %eax
movq %rax, 0x48(%rsp)
leal (%rsi,%rax), %edi
addl $0x30, %edi
callq 0x1160
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x2a16
movq %r13, 0x40(%rsp)
movl 0x1d8(%rsp), %edx
movq 0x1d0(%rsp), %rdi
movq 0x1b8(%rsp), %r13
movq 0x1b0(%rsp), %rbx
leaq 0x80(%rsp), %rsi
callq 0x24e1
movl %r14d, (%r13)
movzwl %bp, %ebp
movl $0xf, %r12d
subl %ebp, %r12d
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10e0
movl (%r13), %r14d
addq %r14, %r15
movl 0x1c0(%rsp), %ebx
movl %ebx, %edx
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
movq %rdx, 0x8(%rsp)
callq 0x10e0
leaq 0x61(%rsp), %r15
xorl %eax, %eax
movq %rax, (%r15)
movq %rax, 0x7(%r15)
movq %r12, 0x30(%rsp)
leal -0x1(%r12), %eax
andb $0x7, %al
movb %al, -0x1(%r15)
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq %rbp, 0x38(%rsp)
movq %rbp, %rdx
callq 0x10e0
movaps -0x1(%r15), %xmm0
movaps %xmm0, 0x20(%rsp)
leal -0x10(%rbx), %eax
cmpl $-0x2, %eax
jg 0x282f
leal 0x1(%rbx), %eax
cltq
movl $0xf, %ecx
incb 0x20(%rsp,%rcx)
sete %dl
cmpq %rax, %rcx
jle 0x282f
decq %rcx
testb %dl, %dl
jne 0x281c
leaq 0x80(%rsp), %rcx
leaq 0x20(%rsp), %r9
movq 0x1b0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl 0x1d8(%rsp), %r13d
movl %r13d, %r8d
callq 0x2163
movq 0x1c8(%rsp), %r12
testq %r12, %r12
je 0x2a06
leaq 0x50(%rsp), %rdi
leaq 0x80(%rsp), %rcx
leaq 0x60(%rsp), %r9
movq 0x8(%rsp), %rsi
movq %rdi, %rdx
movl %r13d, %r8d
callq 0x2163
movq 0x1b8(%rsp), %rax
movslq (%rax), %r13
movl %ebx, %eax
addl $-0x2, %eax
shrl $0x1f, %eax
addl %ebx, %eax
addl $-0x2, %eax
andl $0xe, %eax
movl $0x6, %ecx
movq 0x38(%rsp), %r14
subl %r14d, %ecx
andl $0x7, %ecx
leal (%rcx,%rax,4), %eax
leal 0x40(%rax), %ecx
movq 0x48(%rsp), %rbx
testw %bx, %bx
movzbl %cl, %ecx
cmovel %eax, %ecx
movq 0x18(%rsp), %rbp
movb %cl, (%rbp)
leaq 0x1(%rbp), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x10e0
leaq (%r14,%rbp), %rdi
incq %rdi
movslq 0x30(%rsp), %rdx
xorl %esi, %esi
callq 0x1080
movq %r13, %rax
movb %al, 0xf(%rbp)
movb %ah, 0xe(%rbp)
movb %bl, 0x11(%rbp)
movb %bh, 0x10(%rbp)
leaq 0x12(%rbp), %rdi
movq 0x40(%rsp), %rsi
movq %rbx, %rdx
callq 0x10e0
leal 0x12(%rbx), %eax
andl $0xf, %eax
movl $0x10, %r14d
movq %r15, %r12
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 %ebx, %r15d
addl $0x12, %r15d
leaq (%r15,%rbp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x10e0
movl %r13d, %eax
addl %r15d, %eax
leal 0xf(%r13,%r15), %ecx
cmovnsl %eax, %ecx
andl $-0x10, %ecx
movslq %eax, %r15
subl %ecx, %eax
subl %eax, %r14d
testl %eax, %eax
cmovel %eax, %r14d
movq %rbp, %rdi
addq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1080
addl %r14d, %r15d
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r9
movaps %xmm0, (%r9)
movslq %r15d, %rsi
leaq 0x70(%rsp), %r14
movq %rbp, %rdi
movq %r14, %rdx
leaq 0x80(%rsp), %rcx
movl 0x1d8(%rsp), %r8d
callq 0x19fa
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
callq 0x1120
testl %eax, %eax
je 0x29f8
movq 0x1c8(%rsp), %rax
movl $0x0, (%rax)
movq 0x1b8(%rsp), %rax
movl (%rax), %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1080
jmp 0x2a06
movq 0x1c8(%rsp), %rax
movl $0x1, (%rax)
movq 0x18(%rsp), %rdi
callq 0x1140
movl $0x1, %r12d
movl %r12d, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq