// File: DiplIngFrankGerlach[P]MST/UnitTest.cpp
// Function: replayAttackFailTest()
void replayAttackFailTest()
{
   uint8_t sharedKey[16] = {11,55,11,33,67,89,91,11,55,11,33,67,89,91,0,0};

   MST_Endpoint endpointA(sharedKey);
   MST_Endpoint endpointB(sharedKey);

   uint8_t mcA[16];
   endpointA.generateMaskCounterExchange(mcA);

   uint8_t mcB[16];
   endpointB.generateMaskCounterExchange(mcB);

   //exchange of masking counters
   endpointA.decryptMaskCounterExchange(mcB);
   endpointB.decryptMaskCounterExchange(mcA);
 
   
   //now we can securely encrypt with all assurances of the protocol
   const char* plaintext = "transfer $100 from Account No 19112119 to 1882122";
   uint8_t* secureMessage;
   uint32_t lengthSM;
   uint32_t lp = strlen(plaintext)+1;
   assert( endpointA.encryptToSecureMessage((uint8_t*)plaintext,lp,&secureMessage,&lengthSM) );

   uint8_t* replayMessage = new uint8_t[lengthSM];
   memcpy(replayMessage,secureMessage,lengthSM);
   
   uint8_t* plaintextDec;
   uint32_t lengthPT;
   assert( endpointB.decryptSecureMessage(secureMessage,lengthSM,&plaintextDec,&lengthPT));

   //now comes the replay attack
   assert( endpointB.decryptSecureMessage(replayMessage,lengthSM,&plaintextDec,&lengthPT) == false);

   delete[] replayMessage;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x330, %rsp            # imm = 0x330
movaps 0x24de(%rip), %xmm0     # 0x8f00
movaps %xmm0, -0x10(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x10(%rbp), %rsi
movq %rsi, -0x308(%rbp)
callq 0x7250
movq -0x308(%rbp), %rsi
leaq -0x2a0(%rbp), %rdi
callq 0x7250
jmp 0x6a52
leaq -0x158(%rbp), %rdi
leaq -0x2c0(%rbp), %rsi
callq 0x72f0
jmp 0x6a67
leaq -0x2a0(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x72f0
jmp 0x6a7c
leaq -0x158(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x7350
jmp 0x6a91
leaq -0x2a0(%rbp), %rdi
leaq -0x2c0(%rbp), %rsi
callq 0x7350
jmp 0x6aa6
leaq 0x226e(%rip), %rax      # 0x8d1b
movq %rax, -0x2d8(%rbp)
movq -0x2d8(%rbp), %rdi
callq 0x20a0
incl %eax
movl %eax, -0x2e8(%rbp)
movq -0x2d8(%rbp), %rsi
movl -0x2e8(%rbp), %edx
leaq -0x158(%rbp), %rdi
leaq -0x2e0(%rbp), %rcx
leaq -0x2e4(%rbp), %r8
callq 0x73a0
movb %al, -0x309(%rbp)
jmp 0x6af7
movb -0x309(%rbp), %al
testb $0x1, %al
jne 0x6b03
jmp 0x6b05
jmp 0x6b5e
leaq 0x1ec0(%rip), %rdi      # 0x89cc
leaq 0x1dd8(%rip), %rsi      # 0x88eb
movl $0xcd, %edx
leaq 0x222e(%rip), %rcx      # 0x8d4d
callq 0x20e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2a8(%rbp)
movl %eax, -0x2ac(%rbp)
jmp 0x6c9a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2a8(%rbp)
movl %eax, -0x2ac(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x7610
jmp 0x6c9a
movl -0x2e4(%rbp), %eax
movl %eax, %edi
callq 0x2030
movq %rax, -0x318(%rbp)
jmp 0x6b74
movq -0x318(%rbp), %rax
movq %rax, -0x2f0(%rbp)
movq -0x2f0(%rbp), %rdi
movq -0x2e0(%rbp), %rsi
movl -0x2e4(%rbp), %eax
movl %eax, %edx
callq 0x2130
movq -0x2e0(%rbp), %rsi
movl -0x2e4(%rbp), %edx
leaq -0x2a0(%rbp), %rdi
leaq -0x2f8(%rbp), %rcx
leaq -0x2fc(%rbp), %r8
callq 0x74c0
movb %al, -0x319(%rbp)
jmp 0x6bcc
movb -0x319(%rbp), %al
testb $0x1, %al
jne 0x6bd8
jmp 0x6bda
jmp 0x6bf9
leaq 0x1e62(%rip), %rdi      # 0x8a43
leaq 0x1d03(%rip), %rsi      # 0x88eb
movl $0xd4, %edx
leaq 0x2159(%rip), %rcx      # 0x8d4d
callq 0x20e0
movq -0x2f0(%rbp), %rsi
movl -0x2e4(%rbp), %edx
leaq -0x2a0(%rbp), %rdi
leaq -0x2f8(%rbp), %rcx
leaq -0x2fc(%rbp), %r8
callq 0x74c0
movb %al, -0x31a(%rbp)
jmp 0x6c28
movb -0x31a(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x6c3a
jmp 0x6c59
leaq 0x2128(%rip), %rdi      # 0x8d69
leaq 0x1ca3(%rip), %rsi      # 0x88eb
movl $0xd7, %edx
leaq 0x20f9(%rip), %rcx      # 0x8d4d
callq 0x20e0
movq -0x2f0(%rbp), %rax
movq %rax, -0x328(%rbp)
cmpq $0x0, %rax
je 0x6c79
movq -0x328(%rbp), %rdi
callq 0x2220
leaq -0x2a0(%rbp), %rdi
callq 0x7610
leaq -0x158(%rbp), %rdi
callq 0x7610
addq $0x330, %rsp            # imm = 0x330
popq %rbp
retq
leaq -0x158(%rbp), %rdi
callq 0x7610
movq -0x2a8(%rbp), %rdi
callq 0x22a0
nopw %cs:(%rax,%rax)