// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/omdsha512k128n128tau128v2/ref/omdsha512.c
// Function: omdsha512_process
int
omdsha512_process(unsigned char *data, const unsigned char* key,
                  const unsigned char *data_process,
                  unsigned long long int data_processlen,
                  const unsigned char *ad, unsigned long long int adlen,
                  const unsigned char *nonce,
                  const enum mode encrypting)
{
   hashblock key_block;
   hashblock lstar, lzero;
   hashblock nonce_block;
   hashblock delta;
   hashblock h;
   hashblock res, res2;
   hashblock taures;
   hashblock taga, tage, tag, tag_prime;
   hashblock xor_res;
   unsigned int statedelta, data_process_modlen;
   long long int i, l = 0;

   /* if |K| > n => return -1  */
   if (10 > OMD_k || OMD_k > OMD_n)
      return -1;

   /* if |N| > n-1 => return -1 */
   if ((12 > OMD_lnonce) || (OMD_lnonce > OMD_n-1))
      return -1;

   /* if |C| < tau => return -1 */
   if ((encrypting == OMD_DECRYPT) && (data_processlen < OMD_tau))
      return -1;

   memset (key_block, 0x00, OMD_n);
   memcpy (key_block, key, OMD_k);

   if (encrypting == OMD_ENCRYPT)
      {
    /* M_1 || M_2 ··· M_{l−1} || M_l = M, where |M_i| = m for 1 ≤ i ≤ l−1
	 and |M_l| ≤ m */
	 /* l = ceil(|M|/m) */
	 l = (data_processlen + (OMD_m-1))/OMD_m;
      }
   else if (encrypting == OMD_DECRYPT)
      {
      /* C_1 || C_2 ··· C_{l−1} || C_l || Tag = \mathbb{C},
	 where |C_i| = m for 1 ≤ i ≤ l−1 and |C_l| ≤ m and |Tag| = \tau*/
    data_processlen = data_processlen - OMD_tau;
    /* l = ceil(|C|/m) */
    l = (data_processlen +OMD_m-1)/OMD_m;
      }

   /* Tag_a computed here to handle a=c overlap */
   /* Tag_a = HASH_k(ad)  */
   hash(taga, key_block, ad, adlen);

   /* CHANGE FROM V1 TO V2 HERE */
   /* L* = F_K(0^n, <tau>_m) */
   l2b (taures, OMD_tau);
   key_func (lstar, block0s, key_block, taures);

   /* L[0] = 4.L* */
   double_block (lzero, lstar);   /* 2.L_* */
   double_block (lzero, lzero);   /* 2.(2.L_*) */

   /* pad nonce */
   memset (nonce_block, 0x00, OMD_n);
   memcpy (nonce_block, nonce, OMD_lnonce);
   nonce_block[OMD_lnonce] = 0x80;

   /* \delta{N,0,0} = F_K(N || 10^{n-1-|N|}, 0^m  */
   key_func (delta, nonce_block, key_block, block0s);

   /* H = 0^n */
   memset (h, 0x00, OMD_n);

   /* \delta{N,1,0} = \delta{N,0,0} ^ L[ntz(1)]  */
   increment_masking_message (delta, delta, lzero, 1);

   /* H = F_K(H ^ \delta_{N, 1, 0}, <tau>_m)  */
   xor_block (h, h, delta);
   key_func (h, h, key_block, taures);

   /* for i = 1 to l-1 do  */
   /************************/
   for (i = 1; i < l; i++)
      {
	 /* C_i = H ^ M_i */
	 /* M_i = H ^ C_i */
	 memcpy (res,&data_process[(i-1)*OMD_m],OMD_m);
	 xor_block (res2, h, res);
	 memcpy (&data[(i-1)*OMD_m],res2,OMD_m);

	 /* \delta_{N, i, 0} = \delta{N, i-1, 0} ^ L[ntz(i)]  */
	 increment_masking_message (delta, delta, lzero, i+1);

	 /* H = F_K(H ^ \delta{N, i, 0}, M_i) */
	 xor_block (xor_res, delta, h);

	 if (encrypting == OMD_ENCRYPT)
	    /* res  = M_i */
	    key_func(h, xor_res, key_block, res);
	 else if (encrypting == OMD_DECRYPT)
	    /* res2 = M_i */
	    key_func(h, xor_res, key_block, res2);
      }

   /************************/
   /******** endfor ********/


   /* |M|>0 */
   if (data_processlen !=0 )
      {
	 /* C_l = H ^ M_l */
	 /* M_l = H ^ C_l *//* |M_l| = m then */
	 data_process_modlen = data_processlen % OMD_m;
	 memset (res,0x00,OMD_m);
	 memcpy (res, &data_process[(l-1)*OMD_m], (data_process_modlen==0
						  		  ? OMD_m
						  		  : data_process_modlen));
	 xor_block (res2, h, res);
	 memcpy (&data[(l-1)*OMD_m], res2, (data_process_modlen==0
					   ? OMD_m : data_process_modlen));

      /* if |M_\ell|=m */
      if (data_process_modlen == 0)
         statedelta = 1;
      /* else if |M_\ell|<m*/
      else
         statedelta = 2;

      /* \delta{N, l, 1} = \delta{N, l, 0} ^ (2 or 3)*L_* */
      final_masking_message (delta, delta, lstar, statedelta);
      /* Tag_e = F_K(H ^ \delta{N, l, 1}, M_l)  */
      xor_block (h, h, delta);

      if (encrypting == OMD_ENCRYPT)
	 {
	    /* if |M_\ell|<m, we pad*/
	    if (statedelta==2)
	       res[data_process_modlen]=0x80;

		 /* res  = M_i */
	    key_func(tage, h, key_block, res);
	 }
      else if (encrypting == OMD_DECRYPT)
	 {
	    if(statedelta==2)
	       {
		  /* if |M_\ell|<m, we pad*/
		  res2[data_process_modlen] = 0x80;
		  memset (&res2[data_process_modlen+1], 0x00,
			   OMD_m - data_process_modlen - 1);
	       }

         /* res2 = M_i */
         key_func(tage, h, key_block, res2);
	 }

      }
   /* |M| = 0 => tage = H */
   else
      {
	 memcpy(tage,h,OMD_n);
      }

   /* Tag = (Tag_e ^ Tag_a)[n-1 ... n- tau]  */
   xor_block(tag, taga, tage);

   /*ENCRYPT => output tag and return 0*/
   if (encrypting == OMD_ENCRYPT)
      {
	 memcpy (&data[data_processlen], tag, OMD_tau);

	 return 0;
      }

   /* or check tag */
   memcpy(tag_prime, &data_process[data_processlen], OMD_tau);

   if (memcmp (tag,tag_prime,OMD_tau) == 0)
      {
	 return 0;
      }

   return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3d8, %rsp            # imm = 0x3D8
movq %rcx, %r12
movq %rdx, %r14
movq %rdi, 0x8(%rsp)
movl 0x418(%rsp), %edi
cmpl $0x1, %edi
sete %cl
cmpq $0x10, %r12
setb %dl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %cl, %dl
jne 0x1f0e
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x30(%rsp)
vmovups %ymm0, 0x20(%rsp)
vmovups (%rsi), %xmm0
vmovaps %xmm0, 0x10(%rsp)
testl %edi, %edi
je 0x18ff
cmpl $0x1, %edi
jne 0x190d
leaq -0x10(%r12), %rax
addq $0x2f, %r12
shrq $0x6, %r12
movq %rax, (%rsp)
jmp 0x1914
movq %r12, (%rsp)
addq $0x3f, %r12
shrq $0x6, %r12
jmp 0x1914
movq %r12, (%rsp)
xorl %r12d, %r12d
movq 0x410(%rsp), %r15
leaq 0x2d0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r8, %rdx
movq %r9, %rcx
vzeroupper
callq 0x10d0
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x1f0(%rsp)
vmovups %ymm0, 0x1d0(%rsp)
movb $-0x80, %cl
movl $0x40, %eax
movb %cl, 0x1cf(%rsp,%rax)
movl $0x0, %ecx
decq %rax
jne 0x1954
vmovups 0x10(%rsp), %ymm0
vmovups 0x30(%rsp), %ymm1
leaq 0x150(%rsp), %rbx
vmovups %ymm1, 0x20(%rbx)
vmovups %ymm0, (%rbx)
vmovups 0x1d0(%rsp), %ymm0
vmovups 0x1f0(%rsp), %ymm1
vmovups %ymm0, 0x40(%rbx)
vmovups %ymm1, 0x60(%rbx)
leaq 0x165b(%rip), %rsi      # 0x3000
leaq 0x290(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rdx
vzeroupper
callq 0x10a0
leaq 0x310(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1060
movq %r13, %rdi
movq %r13, %rsi
callq 0x1060
vxorps %xmm2, %xmm2, %xmm2
leaq 0x390(%rsp), %rsi
vmovups %ymm2, 0x20(%rsi)
vmovups %ymm2, 0x10(%rsi)
vmovups (%r15), %xmm0
vmovaps %xmm0, (%rsi)
movb $-0x80, 0x10(%rsi)
vmovups 0x10(%rsp), %ymm0
vmovups 0x30(%rsp), %ymm1
vmovups %ymm1, 0x20(%rbx)
vmovups %ymm0, (%rbx)
vmovups %ymm2, 0x40(%rbx)
vmovups %ymm2, 0x60(%rbx)
leaq 0xd0(%rsp), %rdi
leaq 0x150(%rsp), %rbx
movq %rbx, %rdx
vzeroupper
callq 0x10a0
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x50(%rsp)
vmovups %ymm0, 0x70(%rsp)
vmovups (%r13), %ymm0
vmovups 0x20(%r13), %ymm1
vmovups %ymm1, 0x20(%rbx)
vmovups %ymm0, (%rbx)
xorl %eax, %eax
vmovups 0x150(%rsp,%rax), %ymm0
vxorps 0xd0(%rsp,%rax), %ymm0, %ymm0
vmovups %ymm0, 0xd0(%rsp,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1a5d
xorl %eax, %eax
vmovups 0xd0(%rsp,%rax), %ymm0
vxorps 0x50(%rsp,%rax), %ymm0, %ymm0
vmovups %ymm0, 0x50(%rsp,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1a84
vmovups 0x10(%rsp), %ymm0
vmovups 0x30(%rsp), %ymm1
leaq 0x150(%rsp), %rdx
vmovups %ymm1, 0x20(%rdx)
vmovups %ymm0, (%rdx)
vmovups 0x1d0(%rsp), %ymm0
vmovups 0x1f0(%rsp), %ymm1
vmovups %ymm0, 0x40(%rdx)
vmovups %ymm1, 0x60(%rdx)
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
vzeroupper
callq 0x10a0
cmpq $0x2, %r12
jb 0x1c67
leaq 0x190(%rsp), %r15
movl $0x1, %r13d
leaq 0xd0(%rsp), %rbp
leaq 0x310(%rsp), %rbx
movq %r13, %rax
shlq $0x6, %rax
vmovups -0x40(%r14,%rax), %ymm0
vmovups -0x20(%r14,%rax), %ymm1
addq $-0x40, %rax
vmovups %ymm1, 0xb0(%rsp)
vmovups %ymm0, 0x90(%rsp)
xorl %ecx, %ecx
vmovups 0x90(%rsp,%rcx), %ymm0
vxorps 0x50(%rsp,%rcx), %ymm0, %ymm0
vmovups %ymm0, 0x110(%rsp,%rcx)
addq $0x20, %rcx
cmpq $0x40, %rcx
jne 0x1b41
vmovups 0x110(%rsp), %ymm0
vmovups 0x130(%rsp), %ymm1
movq 0x8(%rsp), %rcx
vmovups %ymm1, 0x20(%rcx,%rax)
vmovups %ymm0, (%rcx,%rax)
incq %r13
movq %rbp, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl %r13d, %ecx
vzeroupper
callq 0x10e0
xorl %eax, %eax
vmovups 0x50(%rsp,%rax), %ymm0
vxorps 0xd0(%rsp,%rax), %ymm0, %ymm0
vmovups %ymm0, 0x250(%rsp,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1b9e
movl 0x418(%rsp), %eax
testl %eax, %eax
je 0x1c06
cmpl $0x1, %eax
jne 0x1c5e
vmovups 0x10(%rsp), %ymm0
vmovups 0x30(%rsp), %ymm1
vmovups %ymm1, 0x170(%rsp)
vmovups %ymm0, 0x150(%rsp)
vmovups 0x110(%rsp), %ymm0
vmovups 0x130(%rsp), %ymm1
jmp 0x1c36
vmovups 0x10(%rsp), %ymm0
vmovups 0x30(%rsp), %ymm1
vmovups %ymm1, 0x170(%rsp)
vmovups %ymm0, 0x150(%rsp)
vmovups 0x90(%rsp), %ymm0
vmovups 0xb0(%rsp), %ymm1
vmovups %ymm1, 0x20(%r15)
vmovups %ymm0, (%r15)
leaq 0x50(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x150(%rsp), %rdx
vzeroupper
callq 0x10a0
cmpq %r12, %r13
jne 0x1b14
movq (%rsp), %r15
testq %r15, %r15
je 0x1d58
movl %r15d, %r13d
andl $0x3f, %r13d
movl $0x40, %ebp
cmovnel %r13d, %ebp
movl $0x40, %edx
subq %rbp, %rdx
xorl %r15d, %r15d
testl %r13d, %r13d
cmoveq %r15, %rdx
shlq $0x6, %r12
leaq (%r14,%r12), %rbx
addq $-0x40, %rbx
addq $-0x40, %r12
leaq (%rsp,%rbp), %rdi
addq $0x90, %rdi
xorl %esi, %esi
vzeroupper
callq 0x1030
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x10f0
vmovups 0x90(%rsp,%r15), %ymm0
vxorps 0x50(%rsp,%r15), %ymm0, %ymm0
vmovups %ymm0, 0x110(%rsp,%r15)
addq $0x20, %r15
cmpq $0x40, %r15
jne 0x1cce
addq 0x8(%rsp), %r12
leaq 0x110(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
vzeroupper
callq 0x10f0
leaq 0x150(%rsp), %rdi
leaq 0x290(%rsp), %rsi
testl %r13d, %r13d
je 0x1d7b
callq 0x10c0
xorl %eax, %eax
movq (%rsp), %r15
vmovups 0x150(%rsp,%rax), %ymm0
vxorps 0xd0(%rsp,%rax), %ymm0, %ymm0
vmovups %ymm0, 0xd0(%rsp,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1d31
jmp 0x1dab
vmovups 0x50(%rsp), %ymm0
vmovups 0x70(%rsp), %ymm1
vmovups %ymm1, 0x230(%rsp)
vmovups %ymm0, 0x210(%rsp)
jmp 0x1ea0
callq 0x1060
xorl %eax, %eax
movq (%rsp), %r15
vmovups 0x150(%rsp,%rax), %ymm0
vxorps 0xd0(%rsp,%rax), %ymm0, %ymm0
vmovups %ymm0, 0xd0(%rsp,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1d86
movl 0x418(%rsp), %ecx
xorl %eax, %eax
vmovups 0xd0(%rsp,%rax), %ymm0
vxorps 0x50(%rsp,%rax), %ymm0, %ymm0
vmovups %ymm0, 0x50(%rsp,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1db4
testl %ecx, %ecx
je 0x1e3f
cmpl $0x1, %ecx
jne 0x1ea7
testl %r13d, %r13d
je 0x1e0e
movl %r15d, %eax
andl $0x3f, %eax
movb $-0x80, 0x110(%rsp,%rax)
movl %r13d, %eax
leaq (%rax,%rbx), %rdi
incq %rdi
xorl $0x3f, %r13d
xorl %esi, %esi
movq %r13, %rdx
vzeroupper
callq 0x1030
vmovups 0x10(%rsp), %ymm0
vmovups 0x30(%rsp), %ymm1
leaq 0x150(%rsp), %rdx
vmovups %ymm1, 0x20(%rdx)
vmovups %ymm0, (%rdx)
vmovups 0x110(%rsp), %ymm0
vmovups 0x130(%rsp), %ymm1
jmp 0x1e81
testl %r13d, %r13d
je 0x1e52
movl %r15d, %eax
andl $0x3f, %eax
movb $-0x80, 0x90(%rsp,%rax)
vmovups 0x10(%rsp), %ymm0
vmovups 0x30(%rsp), %ymm1
leaq 0x150(%rsp), %rdx
vmovups %ymm1, 0x20(%rdx)
vmovups %ymm0, (%rdx)
vmovups 0x90(%rsp), %ymm0
vmovups 0xb0(%rsp), %ymm1
vmovups %ymm0, 0x40(%rdx)
vmovups %ymm1, 0x60(%rdx)
leaq 0x210(%rsp), %rdi
leaq 0x50(%rsp), %rsi
vzeroupper
callq 0x10a0
movl 0x418(%rsp), %ecx
xorl %eax, %eax
vmovups 0x210(%rsp,%rax), %ymm0
vxorps 0x2d0(%rsp,%rax), %ymm0, %ymm0
vmovups %ymm0, 0x150(%rsp,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1ea9
testl %ecx, %ecx
je 0x1ef8
vmovdqu (%r14,%r15), %xmm0
vmovdqa %xmm0, 0x350(%rsp)
vpxor 0x150(%rsp), %xmm0, %xmm0
xorl %eax, %eax
vptest %xmm0, %xmm0
setne %al
negl %eax
jmp 0x1f0e
vmovdqa 0x150(%rsp), %xmm0
movq 0x8(%rsp), %rax
vmovdqu %xmm0, (%rax,%r15)
xorl %eax, %eax
addq $0x3d8, %rsp            # imm = 0x3D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
nop