// File: /workspace/github_cpp_makefile_star2_O1/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 0x1d5b(%rip), %xmm0     # 0x58f0
leaq -0xc0(%rbp), %rbx
movaps %xmm0, (%rbx)
leaq -0x350(%rbp), %rdi
movq %rbx, %rsi
callq 0x46bc
leaq -0x208(%rbp), %rdi
movq %rbx, %rsi
callq 0x46bc
leaq -0x350(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x474a
leaq -0x208(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x474a
leaq -0x330(%rbp), %rsi
leaq -0x320(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
movl $0x80, %ecx
callq 0x1b78
movb $0x1, -0x22f(%rbp)
leaq -0x1e8(%rbp), %rsi
leaq -0x1d8(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
movl $0x80, %ecx
callq 0x1b78
leaq -0x208(%rbp), %rax
movb $0x1, 0x121(%rax)
xorl %r13d, %r13d
leaq -0x88(%rbp), %r12
leaq 0x1eda(%rip), %r15      # 0x5b21
leaq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movq %r12, %rdi
leaq 0x1dda(%rip), %rsi      # 0x5a36
leaq 0x1df7(%rip), %rdx      # 0x5a5a
callq 0x4a6c
callq 0x1050
movl %eax, %ebx
andl $0xf, %ebx
je 0x3c8a
xorl %r14d, %r14d
movq %r12, %rdi
movq %r15, %rsi
callq 0x11c0
incb %r14b
cmpb %bl, %r14b
jb 0x3c77
movq -0x88(%rbp), %rsi
movl -0x80(%rbp), %edx
incl %edx
leaq -0x350(%rbp), %rdi
leaq -0x40(%rbp), %rcx
leaq -0x2c(%rbp), %r8
callq 0x4794
testb %al, %al
je 0x3e46
movq -0x40(%rbp), %rsi
movl -0x2c(%rbp), %edx
leaq -0x208(%rbp), %rdi
leaq -0x38(%rbp), %rcx
leaq -0x44(%rbp), %r8
callq 0x484a
testb %al, %al
je 0x3e27
movq -0x38(%rbp), %rsi
movq %r12, %rdi
callq 0x1060
testl %eax, %eax
jne 0x3e08
callq 0x1050
andl $0xf, %eax
cmpl $0x4, %eax
ja 0x3db2
leaq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %rdi
leaq 0x1ef4(%rip), %rsi      # 0x5c01
leaq 0x1f24(%rip), %rdx      # 0x5c38
callq 0x4a6c
callq 0x1050
movl %eax, %ebx
andl $0xf, %ebx
je 0x3d3c
xorl %r14d, %r14d
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
callq 0x11c0
incb %r14b
cmpb %bl, %r14b
jb 0x3d28
movq -0x68(%rbp), %rsi
movl -0x60(%rbp), %edx
incl %edx
leaq -0x208(%rbp), %rdi
leaq -0x40(%rbp), %rcx
leaq -0x2c(%rbp), %r8
callq 0x4794
testb %al, %al
je 0x3e84
movq -0x40(%rbp), %rsi
movl -0x2c(%rbp), %edx
leaq -0x350(%rbp), %rdi
leaq -0x38(%rbp), %rcx
leaq -0x44(%rbp), %r8
callq 0x484a
testb %al, %al
je 0x3ea3
movq -0x38(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x1060
testl %eax, %eax
jne 0x3e65
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x3db2
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x1110
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x3dce
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x1110
incl %r13d
cmpl $0x2710, %r13d          # imm = 0x2710
jne 0x3c47
leaq -0x208(%rbp), %rdi
callq 0x494e
leaq -0x350(%rbp), %rdi
callq 0x494e
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1dbf(%rip), %rdi      # 0x5bce
leaq 0x1b64(%rip), %rsi      # 0x597a
leaq 0x1d85(%rip), %rcx      # 0x5ba2
movl $0x7c, %edx
callq 0x1090
leaq 0x1ca4(%rip), %rdi      # 0x5ad2
leaq 0x1b45(%rip), %rsi      # 0x597a
leaq 0x1d66(%rip), %rcx      # 0x5ba2
movl $0x7a, %edx
callq 0x1090
leaq 0x1ce6(%rip), %rdi      # 0x5b33
leaq 0x1b26(%rip), %rsi      # 0x597a
leaq 0x1d47(%rip), %rcx      # 0x5ba2
movl $0x76, %edx
callq 0x1090
leaq 0x1e8d(%rip), %rdi      # 0x5cf9
leaq 0x1b07(%rip), %rsi      # 0x597a
leaq 0x1d28(%rip), %rcx      # 0x5ba2
movl $0x8f, %edx
callq 0x1090
leaq 0x1dae(%rip), %rdi      # 0x5c39
leaq 0x1ae8(%rip), %rsi      # 0x597a
leaq 0x1d09(%rip), %rcx      # 0x5ba2
movl $0x8b, %edx
callq 0x1090
leaq 0x1e00(%rip), %rdi      # 0x5caa
leaq 0x1ac9(%rip), %rsi      # 0x597a
leaq 0x1cea(%rip), %rcx      # 0x5ba2
movl $0x8d, %edx
callq 0x1090
jmp 0x3ed3
movq %rax, %rbx
jmp 0x3f21
jmp 0x3ed3
jmp 0x3ef6
jmp 0x3ef6
jmp 0x3ef6
jmp 0x3ed8
movq %rax, %rbx
jmp 0x3f15
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x3ef9
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x1110
jmp 0x3ef9
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x3f15
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x1110
leaq -0x208(%rbp), %rdi
callq 0x494e
leaq -0x350(%rbp), %rdi
callq 0x494e
movq %rbx, %rdi
callq 0x11a0