// File: lmclaramunt[P]AES-Crypto/Cipher.cpp
// Function: Cipher::OFB_encrypt()
void Cipher::OFB_encrypt(){   
    Sequence ivSq(16), ivOriginal(16);
    time_t rawtime;
    struct tm * timeinfo;
    unsigned char iv[16];
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    strftime ((char *)iv, 16, "%r", timeinfo);
    ivSq.setSequence(iv);
    ivOriginal.updateSequence(ivSq);        //Keep track of IV to write it in Ciphertext
    
    //Encrypt
    for(Sequence sq: inputBlock->getSequenceVector()){
        encrypt(&ivSq, aesKeyExp);
        sq = sq ^ ivSq;
    }

    //Authenticate
    inputBlock->getSequenceVector().
        insert(inputBlock->getSequenceVector().begin(), ivOriginal);    //Append IV to Ciphertext
    Sequence tag = CBC_MAC(*inputBlock, true, false);       //Get MAC Tag, we are encrypting without padding
    
    //Write ciphertext after encryption and authentication
    writeCipherText(&tag);
    /* Sequence cleanup as it can leave traces in memory */
    ivSq.~Sequence();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0x18(%rbp), %rdi
movl $0x10, %esi
callq 0x4a10
leaq -0x28(%rbp), %rdi
movl $0x10, %esi
callq 0x4a10
leaq -0x30(%rbp), %rdi
callq 0x3240
leaq -0x30(%rbp), %rdi
callq 0x31b0
movq %rax, -0x38(%rbp)
leaq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rcx
movl $0x10, %esi
leaq 0x4d27(%rip), %rdx      # 0xf807
callq 0x31f0
leaq -0x50(%rbp), %rsi
leaq -0x18(%rbp), %rdi
callq 0x4a80
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rsi
movl -0x58(%rbp), %edx
leaq -0x28(%rbp), %rdi
callq 0x4b70
movq -0xf0(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x5190
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x5b00
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0x5980
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xb6a0
testb $0x1, %al
jne 0xab53
jmp 0xabac
leaq -0x70(%rbp), %rdi
callq 0x68c0
movq -0xf0(%rbp), %rdi
movq (%rax), %rcx
movq %rcx, -0x88(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
movq (%rdi), %rdx
leaq -0x18(%rbp), %rsi
callq 0x9570
leaq -0x88(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x4cc0
movq (%rax), %rcx
movq %rcx, -0x88(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x80(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x68e0
jmp 0xab40
movq -0xf0(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x5190
movq %rax, %rcx
movq -0xf0(%rbp), %rax
movq %rcx, -0x110(%rbp)
movq 0x10(%rax), %rdi
callq 0x5190
movq %rax, %rdi
callq 0x5b00
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xb920
movq -0x110(%rbp), %rdi
movq -0x90(%rbp), %rsi
leaq -0x28(%rbp), %rdx
callq 0xb720
movq %rax, %rcx
movq -0xf0(%rbp), %rax
movq %rcx, -0xa0(%rbp)
movq 0x10(%rax), %rsi
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0xb960
movq -0xf0(%rbp), %rdi
movq -0x108(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x9c90
movl %edx, -0xfc(%rbp)
movq %rax, -0xf8(%rbp)
jmp 0xac60
movl -0xfc(%rbp), %eax
movq -0xf8(%rbp), %rcx
movq %rcx, -0xe8(%rbp)
movl %eax, -0xe0(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0xe0(%rbp), %eax
movl %eax, -0xa8(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x5100
movq -0xf0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0xa2d0
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x5100
movq -0xd0(%rbp), %rdi
callq 0x34d0
nopw %cs:(%rax,%rax)