// File: lmclaramunt[P]AES-Crypto/Cipher.cpp
// Function: Cipher::OFB_decrypt()
void Cipher::OFB_decrypt(){     
    Sequence tag = CBC_MAC(*inputBlock, false, true);             //Generate MAC Tag from Ciphertext
    Sequence readTag = inputBlock->getSequenceVector().at(0);     //Get the MAC Tag in the Ciphertext
    if(authenticateSequences(&readTag, &tag)){          //Authenticate Ciphertext
        Sequence ivSq(16);
        int i = -1;

        //Decrypt Ciphertext
        for(Sequence sq: inputBlock->getSequenceVector()) {
            i++;
            if(i <= 1) {
                ivSq.updateSequence(sq);    //Ignore first 2 block when decrypting, since these
                continue;                   //Contain MAC Tag and IV respectively
            }
            encrypt(&ivSq, aesKeyExp);
            sq = sq ^ ivSq; 
        }

        //Ciphertext has been authenticated and decrypted. Ready to write plaintext
        writePlainText();
    } else
        throw "Ciphertext has been modified, it won't be decrypted!\n";     //Authenticate-then-decrypt
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq 0x10(%rax), %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0xb960
movq -0xd0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x9c90
movl %edx, -0xbc(%rbp)
movq %rax, -0xb8(%rbp)
jmp 0xad47
movl -0xbc(%rbp), %eax
movq -0xb8(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movl %eax, -0x48(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x18(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x10(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x5100
movq -0xd0(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x5190
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xb990
movq -0xd0(%rbp), %rdi
movq (%rax), %rcx
movq %rcx, -0x60(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
leaq -0x60(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x9fe0
testb $0x1, %al
jne 0xadba
jmp 0xaef2
leaq -0x70(%rbp), %rdi
movl $0x10, %esi
callq 0x4a10
movq -0xd0(%rbp), %rax
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movq 0x10(%rax), %rdi
callq 0x5190
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x5b00
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
callq 0x5980
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xb6a0
testb $0x1, %al
jne 0xae1f
jmp 0xaee4
leaq -0x88(%rbp), %rdi
callq 0x68c0
movq (%rax), %rcx
movq %rcx, -0xa0(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
cmpl $0x1, -0x74(%rbp)
jg 0xae9d
movq -0xa0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xb0(%rbp), %rsi
movl -0xa8(%rbp), %edx
leaq -0x70(%rbp), %rdi
callq 0x4b70
jmp 0xaed3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x5100
jmp 0xaf22
movq -0xd0(%rbp), %rdi
movq (%rdi), %rdx
leaq -0x70(%rbp), %rsi
callq 0x9570
leaq -0xa0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4cc0
movq (%rax), %rcx
movq %rcx, -0xa0(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x98(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x68e0
jmp 0xae03
movq -0xd0(%rbp), %rdi
callq 0xa110
jmp 0xaf19
movl $0x8, %edi
callq 0x3120
movq %rax, %rdi
leaq 0x48cb(%rip), %rax      # 0xf7d1
movq %rax, (%rdi)
movq 0xb0b0(%rip), %rsi      # 0x15fc0
xorl %eax, %eax
movl %eax, %edx
callq 0x3490
addq $0xd0, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x34d0
nopl (%rax,%rax)