// File: /workspace/github_cpp_makefile_star2_O3/DiplIngFrankGerlach[P]MST/UnitTest.cpp
// Function: encryptedCommunicationExampleComplex()
void encryptedCommunicationExampleComplex()
{
   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

   for(uint32_t i=0; i < 10000; i++)
   {
	   string plaintext = "Schiller, Goethe, Von der Vogelweide";

           uint8_t n= rand() & 0xF;
           for(uint8_t j=0; j < n; j++)
           {
              plaintext += " three more words";
           }
	   uint8_t* secureMessage;
	   uint32_t lengthSM;
	   assert( endpointA.encryptToSecureMessage((uint8_t*)plaintext.c_str(),
		                                    plaintext.length()+1,
		                                    &secureMessage,
		                                    &lengthSM) );
	   
	   uint8_t* plaintextDec;
	   uint32_t lengthPT;
	   assert( endpointB.decryptSecureMessage(secureMessage,lengthSM,&plaintextDec,&lengthPT) );

	   assert( plaintext.compare( (const char*)plaintextDec) == 0 );


           if( (rand() & 0xF) < 5 )
           {
		   string plaintext2 = "Buy 100 shares of Apple and 200 shares of Deutsche Bank";

		   n= rand() & 0xF;
		   for(uint8_t j=0; j < n; j++)
		   {
		      plaintext2 += " three more words";
		   }
		   assert( endpointB.encryptToSecureMessage((uint8_t*)plaintext2.c_str(),
				                            plaintext2.length()+1,
				                            &secureMessage,
				                            &lengthSM) );
	   
		   assert( endpointA.decryptSecureMessage(secureMessage,lengthSM,&plaintextDec,&lengthPT) );

		   assert( plaintext2.compare((const char*)plaintextDec) == 0 );
           }
   }
   
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movaps 0x1dc3(%rip), %xmm0     # 0x58f0
leaq -0xc0(%rbp), %rbx
movaps %xmm0, (%rbx)
leaq -0x350(%rbp), %rdi
movq %rbx, %rsi
callq 0x4654
leaq -0x208(%rbp), %rdi
movq %rbx, %rsi
callq 0x4654
leaq -0x350(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x46e2
leaq -0x208(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x46e2
leaq -0x330(%rbp), %rsi
leaq -0x320(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
movl $0x80, %ecx
callq 0x1b25
movb $0x1, -0x22f(%rbp)
leaq -0x1e8(%rbp), %rsi
leaq -0x1d8(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
movl $0x80, %ecx
callq 0x1b25
leaq -0x208(%rbp), %rax
movb $0x1, 0x121(%rax)
xorl %r13d, %r13d
leaq -0x88(%rbp), %r12
leaq 0x1f42(%rip), %r15      # 0x5b21
leaq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movq %r12, %rdi
leaq 0x1e42(%rip), %rsi      # 0x5a36
leaq 0x1e5f(%rip), %rdx      # 0x5a5a
callq 0x4a08
callq 0x1050
movl %eax, %ebx
andl $0xf, %ebx
je 0x3c22
xorl %r14d, %r14d
movq %r12, %rdi
movq %r15, %rsi
callq 0x11c0
incb %r14b
cmpb %bl, %r14b
jb 0x3c0f
movq -0x88(%rbp), %rsi
movl -0x80(%rbp), %edx
incl %edx
leaq -0x350(%rbp), %rdi
leaq -0x40(%rbp), %rcx
leaq -0x2c(%rbp), %r8
callq 0x472c
testb %al, %al
je 0x3dde
movq -0x40(%rbp), %rsi
movl -0x2c(%rbp), %edx
leaq -0x208(%rbp), %rdi
leaq -0x38(%rbp), %rcx
leaq -0x44(%rbp), %r8
callq 0x47e2
testb %al, %al
je 0x3dbf
movq -0x38(%rbp), %rsi
movq %r12, %rdi
callq 0x1060
testl %eax, %eax
jne 0x3da0
callq 0x1050
andl $0xf, %eax
cmpl $0x4, %eax
ja 0x3d4a
leaq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %rdi
leaq 0x1f5c(%rip), %rsi      # 0x5c01
leaq 0x1f8c(%rip), %rdx      # 0x5c38
callq 0x4a08
callq 0x1050
movl %eax, %ebx
andl $0xf, %ebx
je 0x3cd4
xorl %r14d, %r14d
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
callq 0x11c0
incb %r14b
cmpb %bl, %r14b
jb 0x3cc0
movq -0x68(%rbp), %rsi
movl -0x60(%rbp), %edx
incl %edx
leaq -0x208(%rbp), %rdi
leaq -0x40(%rbp), %rcx
leaq -0x2c(%rbp), %r8
callq 0x472c
testb %al, %al
je 0x3e1c
movq -0x40(%rbp), %rsi
movl -0x2c(%rbp), %edx
leaq -0x350(%rbp), %rdi
leaq -0x38(%rbp), %rcx
leaq -0x44(%rbp), %r8
callq 0x47e2
testb %al, %al
je 0x3e3b
movq -0x38(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x1060
testl %eax, %eax
jne 0x3dfd
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x3d4a
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x1110
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x3d66
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x1110
incl %r13d
cmpl $0x2710, %r13d          # imm = 0x2710
jne 0x3bdf
leaq -0x208(%rbp), %rdi
callq 0x48e6
leaq -0x350(%rbp), %rdi
callq 0x48e6
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e27(%rip), %rdi      # 0x5bce
leaq 0x1bcc(%rip), %rsi      # 0x597a
leaq 0x1ded(%rip), %rcx      # 0x5ba2
movl $0x7c, %edx
callq 0x1090
leaq 0x1d0c(%rip), %rdi      # 0x5ad2
leaq 0x1bad(%rip), %rsi      # 0x597a
leaq 0x1dce(%rip), %rcx      # 0x5ba2
movl $0x7a, %edx
callq 0x1090
leaq 0x1d4e(%rip), %rdi      # 0x5b33
leaq 0x1b8e(%rip), %rsi      # 0x597a
leaq 0x1daf(%rip), %rcx      # 0x5ba2
movl $0x76, %edx
callq 0x1090
leaq 0x1ef5(%rip), %rdi      # 0x5cf9
leaq 0x1b6f(%rip), %rsi      # 0x597a
leaq 0x1d90(%rip), %rcx      # 0x5ba2
movl $0x8f, %edx
callq 0x1090
leaq 0x1e16(%rip), %rdi      # 0x5c39
leaq 0x1b50(%rip), %rsi      # 0x597a
leaq 0x1d71(%rip), %rcx      # 0x5ba2
movl $0x8b, %edx
callq 0x1090
leaq 0x1e68(%rip), %rdi      # 0x5caa
leaq 0x1b31(%rip), %rsi      # 0x597a
leaq 0x1d52(%rip), %rcx      # 0x5ba2
movl $0x8d, %edx
callq 0x1090
jmp 0x3e6b
movq %rax, %rbx
jmp 0x3eb9
jmp 0x3e6b
jmp 0x3e8e
jmp 0x3e8e
jmp 0x3e8e
jmp 0x3e70
movq %rax, %rbx
jmp 0x3ead
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x3e91
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x1110
jmp 0x3e91
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x3ead
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x1110
leaq -0x208(%rbp), %rdi
callq 0x48e6
leaq -0x350(%rbp), %rdi
callq 0x48e6
movq %rbx, %rdi
callq 0x11a0