// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x408, %rsp            # imm = 0x408
movl 0x418(%rsp), %eax
movq 0x410(%rsp), %rax
movq %rdi, 0x3f8(%rsp)
movq %rsi, 0x3f0(%rsp)
movq %rdx, 0x3e8(%rsp)
movq %rcx, 0x3e0(%rsp)
movq %r8, 0x3d8(%rsp)
movq %r9, 0x3d0(%rsp)
movq $0x0, 0x38(%rsp)
cmpl $0x1, 0x418(%rsp)
jne 0x1cf4
cmpq $0x10, 0x3e0(%rsp)
jae 0x1cf4
movl $0xffffffff, 0x404(%rsp) # imm = 0xFFFFFFFF
jmp 0x2310
leaq 0x390(%rsp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x1040
movq 0x3f0(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x390(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x398(%rsp)
cmpl $0x0, 0x418(%rsp)
jne 0x1d48
movq 0x3e0(%rsp), %rax
addq $0x3f, %rax
shrq $0x6, %rax
movq %rax, 0x38(%rsp)
jmp 0x1d81
cmpl $0x1, 0x418(%rsp)
jne 0x1d7f
movq 0x3e0(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x3e0(%rsp)
movq 0x3e0(%rsp), %rax
addq $0x40, %rax
subq $0x1, %rax
shrq $0x6, %rax
movq %rax, 0x38(%rsp)
jmp 0x1d81
leaq 0x150(%rsp), %rdi
leaq 0x390(%rsp), %rsi
movq 0x3d8(%rsp), %rdx
movq 0x3d0(%rsp), %rcx
callq 0x1100
leaq 0x190(%rsp), %rdi
movl $0x10, %esi
callq 0x1140
leaq 0x350(%rsp), %rdi
leaq 0x390(%rsp), %rdx
leaq 0x190(%rsp), %rcx
leaq 0x1229(%rip), %rsi      # 0x3000
callq 0x10b0
leaq 0x310(%rsp), %rdi
leaq 0x350(%rsp), %rsi
callq 0x1070
leaq 0x310(%rsp), %rdi
leaq 0x310(%rsp), %rsi
callq 0x1070
leaq 0x2d0(%rsp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x1040
movq 0x410(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x2d0(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x2d8(%rsp)
movb $-0x80, 0x2e0(%rsp)
leaq 0x290(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
leaq 0x390(%rsp), %rdx
leaq 0x11a0(%rip), %rcx      # 0x3000
callq 0x10b0
leaq 0x250(%rsp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x1040
leaq 0x290(%rsp), %rdi
leaq 0x290(%rsp), %rsi
leaq 0x310(%rsp), %rdx
movl $0x1, %ecx
callq 0x1110
leaq 0x250(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x290(%rsp), %rdx
callq 0x1130
leaq 0x250(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x390(%rsp), %rdx
leaq 0x190(%rsp), %rcx
callq 0x10b0
movq $0x1, 0x40(%rsp)
movq 0x40(%rsp), %rax
cmpq 0x38(%rsp), %rax
jge 0x201a
leaq 0x210(%rsp), %rdi
movq 0x3e8(%rsp), %rsi
movq 0x40(%rsp), %rax
subq $0x1, %rax
shlq $0x6, %rax
addq %rax, %rsi
movl $0x40, %edx
callq 0x1150
leaq 0x1d0(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x210(%rsp), %rdx
callq 0x1130
movq 0x3f8(%rsp), %rdi
movq 0x40(%rsp), %rax
subq $0x1, %rax
shlq $0x6, %rax
addq %rax, %rdi
leaq 0x1d0(%rsp), %rsi
movl $0x40, %edx
callq 0x1150
leaq 0x290(%rsp), %rdi
leaq 0x290(%rsp), %rsi
leaq 0x310(%rsp), %rdx
movq 0x40(%rsp), %rax
addq $0x1, %rax
movl %eax, %ecx
callq 0x1110
leaq 0x50(%rsp), %rdi
leaq 0x290(%rsp), %rsi
leaq 0x250(%rsp), %rdx
callq 0x1130
cmpl $0x0, 0x418(%rsp)
jne 0x1fd7
leaq 0x250(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x390(%rsp), %rdx
leaq 0x210(%rsp), %rcx
callq 0x10b0
jmp 0x2005
cmpl $0x1, 0x418(%rsp)
jne 0x2003
leaq 0x250(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x390(%rsp), %rdx
leaq 0x1d0(%rsp), %rcx
callq 0x10b0
jmp 0x2005
jmp 0x2007
movq 0x40(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x1ee6
cmpq $0x0, 0x3e0(%rsp)
je 0x222c
movq 0x3e0(%rsp), %rax
andq $0x3f, %rax
movl %eax, 0x48(%rsp)
leaq 0x210(%rsp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x1040
leaq 0x210(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x3e8(%rsp), %rax
movq 0x38(%rsp), %rcx
subq $0x1, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
cmpl $0x0, 0x48(%rsp)
jne 0x2089
movl $0x40, %eax
movl %eax, 0x24(%rsp)
jmp 0x2091
movl 0x48(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdi
movl 0x24(%rsp), %eax
movl %eax, %eax
movl %eax, %edx
callq 0x1150
leaq 0x1d0(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x210(%rsp), %rdx
callq 0x1130
movq 0x3f8(%rsp), %rax
movq 0x38(%rsp), %rcx
subq $0x1, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
leaq 0x1d0(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x48(%rsp)
jne 0x2101
movl $0x40, %eax
movl %eax, 0xc(%rsp)
jmp 0x2109
movl 0x48(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %eax
movl %eax, %eax
movl %eax, %edx
callq 0x1150
cmpl $0x0, 0x48(%rsp)
jne 0x2131
movl $0x1, 0x4c(%rsp)
jmp 0x2139
movl $0x2, 0x4c(%rsp)
leaq 0x290(%rsp), %rdi
leaq 0x290(%rsp), %rsi
leaq 0x350(%rsp), %rdx
movl 0x4c(%rsp), %ecx
callq 0x1030
leaq 0x250(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x290(%rsp), %rdx
callq 0x1130
cmpl $0x0, 0x418(%rsp)
jne 0x21bb
cmpl $0x2, 0x4c(%rsp)
jne 0x2194
movl 0x48(%rsp), %eax
movb $-0x80, 0x210(%rsp,%rax)
leaq 0x110(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x390(%rsp), %rdx
leaq 0x210(%rsp), %rcx
callq 0x10b0
jmp 0x222a
cmpl $0x1, 0x418(%rsp)
jne 0x2228
cmpl $0x2, 0x4c(%rsp)
jne 0x2203
movl 0x48(%rsp), %eax
movb $-0x80, 0x1d0(%rsp,%rax)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
leaq 0x1d0(%rsp), %rdi
addq %rax, %rdi
movl $0x40, %eax
subl 0x48(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x1040
leaq 0x110(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x390(%rsp), %rdx
leaq 0x1d0(%rsp), %rcx
callq 0x10b0
jmp 0x222a
jmp 0x2246
leaq 0x110(%rsp), %rdi
leaq 0x250(%rsp), %rsi
movl $0x40, %edx
callq 0x1150
leaq 0xd0(%rsp), %rdi
leaq 0x150(%rsp), %rsi
leaq 0x110(%rsp), %rdx
callq 0x1130
cmpl $0x0, 0x418(%rsp)
jne 0x22a3
movq 0x3f8(%rsp), %rax
movq 0x3e0(%rsp), %rcx
movq 0xd0(%rsp), %rdx
movq %rdx, (%rax,%rcx)
movq 0xd8(%rsp), %rdx
movq %rdx, 0x8(%rax,%rcx)
movl $0x0, 0x404(%rsp)
jmp 0x2310
movq 0x3e8(%rsp), %rax
movq 0x3e0(%rsp), %rcx
movq (%rax,%rcx), %rdx
movq %rdx, 0x90(%rsp)
movq 0x8(%rax,%rcx), %rax
movq %rax, 0x98(%rsp)
leaq 0xd0(%rsp), %rax
leaq 0x90(%rsp), %rcx
vmovdqu (%rcx), %xmm1
vmovdqu (%rax), %xmm0
vpxor %xmm1, %xmm0, %xmm0
vptest %xmm0, %xmm0
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x2305
movl $0x0, 0x404(%rsp)
jmp 0x2310
movl $0xffffffff, 0x404(%rsp) # imm = 0xFFFFFFFF
movl 0x404(%rsp), %eax
addq $0x408, %rsp            # imm = 0x408
retq
nop