// File: /workspace/github_c_makefile_star2_O2/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 $0x3a8, %rsp            # imm = 0x3A8
movq %rdx, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movl 0x3e8(%rsp), %r13d
cmpl $0x1, %r13d
sete %al
cmpq $0x10, %rcx
setb %dl
testb %al, %dl
je 0x17ca
pushq $-0x1
popq %rax
jmp 0x1bfb
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0xc0(%rsp)
vmovups %ymm0, 0xb0(%rsp)
movq 0x3e0(%rsp), %rbp
vmovups (%rsi), %xmm0
vmovaps %xmm0, 0xa0(%rsp)
testl %r13d, %r13d
je 0x1817
cmpl $0x1, %r13d
jne 0x182a
leaq -0x10(%rcx), %rax
addq $0x2f, %rcx
shrq $0x6, %rcx
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
jmp 0x1837
movq %rcx, (%rsp)
leaq 0x3f(%rcx), %rax
shrq $0x6, %rax
movq %rax, 0x8(%rsp)
jmp 0x1837
movq %rcx, (%rsp)
movq $0x0, 0x8(%rsp)
leaq 0x1a0(%rsp), %rdi
leaq 0xa0(%rsp), %r12
movq %r12, %rsi
movq %r8, %rdx
movq %r9, %rcx
vzeroupper
callq 0x10f0
leaq 0x1e0(%rsp), %r15
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x1130
leaq 0x178e(%rip), %rbx      # 0x3000
leaq 0x2a0(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x10a0
leaq 0x260(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x1070
movq %r15, %rdi
movq %r15, %rsi
callq 0x1070
vxorps %xmm0, %xmm0, %xmm0
leaq 0x360(%rsp), %rsi
vmovups %ymm0, 0x20(%rsi)
vmovups %ymm0, 0x10(%rsi)
vmovups (%rbp), %xmm0
vmovaps %xmm0, (%rsi)
movb $-0x80, 0x10(%rsi)
leaq 0x220(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rdx
movq %rbx, %rcx
vzeroupper
callq 0x10a0
leaq 0xe0(%rsp), %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x20(%rbx)
vmovups %ymm0, (%rbx)
pushq $0x1
popq %rcx
movq %rbp, %rdi
movq %rbp, %rsi
movq %r15, %rdx
vzeroupper
callq 0x1100
movq %rbx, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1120
movq %rbx, %rdi
movq %rbx, %rsi
movq %r12, %rdx
leaq 0x1e0(%rsp), %rcx
callq 0x10a0
testl %r13d, %r13d
leaq 0x20(%rsp), %rax
leaq 0x60(%rsp), %r13
cmoveq %rax, %r13
pushq $0x1
popq %r12
xorl %r15d, %r15d
leaq 0x2e0(%rsp), %r14
cmpq 0x8(%rsp), %r12
jae 0x1a06
movq 0x18(%rsp), %rax
vmovups (%rax,%r15), %ymm0
vmovups 0x20(%rax,%r15), %ymm1
vmovups %ymm1, 0x40(%rsp)
vmovups %ymm0, 0x20(%rsp)
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
leaq 0x20(%rsp), %rdx
vzeroupper
callq 0x1120
vmovups 0x60(%rsp), %ymm0
vmovups 0x80(%rsp), %ymm1
movq 0x10(%rsp), %rax
vmovups %ymm1, 0x20(%rax,%r15)
vmovups %ymm0, (%rax,%r15)
leal 0x1(%r12), %ecx
movq %rbp, %rdi
movq %rbp, %rsi
leaq 0x260(%rsp), %rdx
vzeroupper
callq 0x1100
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x1120
cmpl $0x2, 0x3e8(%rsp)
jae 0x19fa
movq %rbx, %rdi
movq %r14, %rsi
leaq 0xa0(%rsp), %rdx
movq %r13, %rcx
callq 0x10a0
addq $0x40, %r15
incq %r12
jmp 0x1952
movq (%rsp), %r14
testq %r14, %r14
je 0x1b33
xorl %ebx, %ebx
movl %r14d, %r12d
andl $0x3f, %r12d
pushq $0x40
popq %r15
cmovnel %r12d, %r15d
setne %bl
pushq $0x40
popq %rax
subq %r15, %rax
xorl %edx, %edx
testl %r12d, %r12d
cmovneq %rax, %rdx
movq 0x8(%rsp), %rcx
shlq $0x6, %rcx
movq 0x18(%rsp), %rax
leaq (%rax,%rcx), %r14
addq $-0x40, %r14
leaq (%rsp,%r15), %rdi
addq $0x20, %rdi
movq 0x10(%rsp), %rax
leaq (%rax,%rcx), %rbp
addq $-0x40, %rbp
xorl %esi, %esi
callq 0x1040
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1140
leaq 0x60(%rsp), %r13
leaq 0xe0(%rsp), %r14
movq %r13, %rdi
movq %r14, %rsi
leaq 0x20(%rsp), %rdx
callq 0x1120
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1140
incl %ebx
leaq 0x220(%rsp), %r15
leaq 0x2a0(%rsp), %rdx
movq %r15, %rdi
movq %r15, %rsi
movl %ebx, %ecx
callq 0x1030
movq %r14, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1120
movl 0x3e8(%rsp), %ebx
testl %ebx, %ebx
je 0x1b60
cmpl $0x1, %ebx
movq (%rsp), %r14
jne 0x1b96
testl %r12d, %r12d
je 0x1b14
movl %r14d, %eax
andl $0x3f, %eax
movb $-0x80, 0x60(%rsp,%rax)
movl %r12d, %eax
leaq (%rax,%r13), %rdi
incq %rdi
xorl $0x3f, %r12d
xorl %esi, %esi
movq %r12, %rdx
callq 0x1040
leaq 0x120(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
leaq 0x60(%rsp), %rcx
jmp 0x1b91
vmovups 0xe0(%rsp), %ymm0
vmovups 0x100(%rsp), %ymm1
vmovups %ymm1, 0x140(%rsp)
vmovups %ymm0, 0x120(%rsp)
movl 0x3e8(%rsp), %ebx
jmp 0x1b96
testl %r12d, %r12d
movq (%rsp), %r14
je 0x1b74
movl %r14d, %eax
andl $0x3f, %eax
movb $-0x80, 0x20(%rsp,%rax)
leaq 0x120(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0x10a0
leaq 0x160(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
leaq 0x120(%rsp), %rdx
vzeroupper
callq 0x1120
testl %ebx, %ebx
je 0x1be5
movq 0x18(%rsp), %rax
vmovdqu (%rax,%r14), %xmm0
vmovdqa %xmm0, 0x320(%rsp)
vpxor 0x160(%rsp), %xmm0, %xmm0
xorl %eax, %eax
vptest %xmm0, %xmm0
setne %al
negl %eax
jmp 0x1bfb
vmovaps 0x160(%rsp), %xmm0
movq 0x10(%rsp), %rax
vmovups %xmm0, (%rax,%r14)
xorl %eax, %eax
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)