⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 square.c

📁 加密算法实现 Pegwit is a program for performing public key file encryption and authentication. Encr
💻 C
📖 第 1 页 / 共 2 页
字号:
   Calculate 4 outputs of each round in parallel using esi, edx, ecx, edi
   eax is used to hold the sub-round input
   ebx is used as a byte index register (and also to address text)
   ebp is used to address roundKeys and tables
   5 words of stack are used 
     1 - saved value of ebp
     2 - saved value of roundKeys address
     3,4,5 - round inputs (eax has first input)
   These are destroyed if DESTROY_TEMPORARIES is set
*/

#define INITIAL __asm      \
{                          \
  __asm push ebp  /*1*/    \
  __asm mov ebx, text      \
  __asm mov ebp, roundKeys \
  __asm push ebp  /*2*/    \
  __asm mov eax, [ebp][12] \
  __asm xor eax, [ebx][12] \
  __asm push eax  /*3*/    \
  __asm mov eax, [ebp][8]  \
  __asm xor eax, [ebx][8]  \
  __asm push eax  /*4*/    \
  __asm mov eax, [ebp][4]  \
  __asm xor eax, [ebx][4]  \
  __asm push eax  /*5*/    \
  __asm mov eax, [ebp][0]  \
  __asm xor eax, [ebx][0]  \
  __asm sub ebx, ebx       \
}

#define INIT_ROUND(RN)  __asm      \
{ /* load roundKeys */             \
  __asm mov edi, [ebp][RN*16+0*4]  \
  __asm mov ecx, [ebp][RN*16+1*4]  \
  __asm mov edx, [ebp][RN*16+2*4]  \
  __asm mov esi, [ebp][RN*16+3*4]  \
}

#define SUB_ROUND(TABLE) __asm \
{ /* the real work */          \
  __asm mov ebp,offset TABLE   \
  __asm mov bl,al              \
  __asm xor edi, [ebp][4*ebx]  \
  __asm mov bl,ah              \
  __asm shr eax,16             \
  __asm xor ecx, [ebp][4*ebx]  \
  __asm mov bl,al              \
  __asm shr eax,8              \
  __asm xor edx, [ebp][4*ebx]  \
  __asm xor esi, [ebp][4*eax]  \
}

#define END_ROUND __asm     \
{ /* save round output*/    \
  __asm mov ebp,[esp] /*2*/ \
  __asm push esi      /*3*/ \
  __asm push edx      /*4*/ \
  __asm push ecx      /*5*/ \
  __asm mov eax,edi         \
}

#define ENCRYPT_ROUND(RN) _asm  \
{ /* encryption round */ \
  INIT_ROUND(RN)         \
  SUB_ROUND(Te0)         \
  __asm pop eax  /*5*/   \
  SUB_ROUND(Te1)         \
  __asm pop eax  /*4*/   \
  SUB_ROUND(Te2)         \
  __asm pop eax  /*3*/   \
  SUB_ROUND(Te3)         \
  END_ROUND              \
}

#define DECRYPT_ROUND(RN) _asm  \
{ /* decryption round */ \
  INIT_ROUND(RN)         \
  SUB_ROUND(Td0)         \
  __asm pop eax  /*5*/   \
  SUB_ROUND(Td1)         \
  __asm pop eax  /*4*/   \
  SUB_ROUND(Td2)         \
  __asm pop eax  /*3*/   \
  SUB_ROUND(Td3)         \
  END_ROUND              \
}

#define HALF_FINAL __asm     \
{                            \
  __asm mov bl,al            \
  __asm mov bl, [ebp][ebx]   \
  __asm xor edi, ebx         \
  __asm mov bl,ah            \
  __asm shr eax,16           \
  __asm xor cl, [ebp][ebx]   \
  __asm mov bl,al            \
  __asm xor dl, [ebp][ebx]   \
  __asm mov bl, ah           \
  __asm mov bl, [ebp][ebx]   \
  __asm xor esi, ebx         \
  __asm pop eax              \
  __asm ror edi,8            \
  __asm mov bl,al            \
  __asm mov bl, [ebp][ebx]   \
  __asm xor edi,ebx          \
  __asm ror edi,8            \
  __asm sub bh,bh            \
  __asm mov bl,ah            \
  __asm shr eax,16           \
  __asm xor ch, [ebp][ebx]   \
  __asm ror ecx,16           \
  __asm mov bl,al            \
  __asm xor dh, [ebp][ebx]   \
  __asm ror edx,16           \
  __asm mov bl,ah            \
  __asm ror esi,8            \
  __asm mov bl, [ebp][ebx]   \
  __asm xor esi,ebx          \
  __asm ror esi,8            \
}

#define FINAL(TABLE) _asm  \
{                          \
  INIT_ROUND(8)            \
  mov ebp,offset TABLE     \
  HALF_FINAL      /*5*/    \
  __asm pop eax   /*4*/    \
  HALF_FINAL      /*3*/    \
  __asm pop ebp   /*2*/    \
  __asm pop ebp   /*1*/    \
  __asm mov ebx, text      \
  __asm mov [ebx][4*3],esi \
  __asm mov [ebx][4*2],edx \
  __asm mov [ebx][4*1],ecx \
  __asm mov [ebx][4*0],edi \
}

#define BURN_STACK _asm  \
{                        \
  __asm mov eax, esp     \
  __asm push 0L          \
  __asm push 0L          \
  __asm push 0L          \
  __asm push 0L          \
  __asm push 0L          \
  __asm mov esp, eax     \
}

void squareEncrypt (word32 text[4], squareKeySchedule roundKeys)
{ 
  INITIAL
  ENCRYPT_ROUND(1)
  ENCRYPT_ROUND(2)
  ENCRYPT_ROUND(3)
  ENCRYPT_ROUND(4)
  ENCRYPT_ROUND(5)
  ENCRYPT_ROUND(6)
  ENCRYPT_ROUND(7)
  FINAL(Se)
#ifdef DESTROY_TEMPORARIES
  BURN_STACK
#endif /* ?DESTROY_TEMPORARIES */
}

void squareDecrypt (word32 text[4], squareKeySchedule roundKeys)
{ 
  INITIAL
  DECRYPT_ROUND(1)
  DECRYPT_ROUND(2)
  DECRYPT_ROUND(3)
  DECRYPT_ROUND(4)
  DECRYPT_ROUND(5)
  DECRYPT_ROUND(6)
  DECRYPT_ROUND(7)
  FINAL(Sd)
#ifdef DESTROY_TEMPORARIES
  BURN_STACK
#endif /* ?DESTROY_TEMPORARIES */
}

#else /* !ASSEMBLER_CORE */

#define squareRound(text, temp, T0, T1, T2, T3, roundKey) \
{ \
	temp[0] = T0[GETB0 (text[0])] \
			^ T1[GETB0 (text[1])] \
			^ T2[GETB0 (text[2])] \
			^ T3[GETB0 (text[3])] \
			^ roundKey[0]; \
	temp[1] = T0[GETB1 (text[0])] \
			^ T1[GETB1 (text[1])] \
			^ T2[GETB1 (text[2])] \
			^ T3[GETB1 (text[3])] \
			^ roundKey[1]; \
	temp[2] = T0[GETB2 (text[0])] \
			^ T1[GETB2 (text[1])] \
			^ T2[GETB2 (text[2])] \
			^ T3[GETB2 (text[3])] \
			^ roundKey[2]; \
	temp[3] = T0[GETB3 (text[0])] \
			^ T1[GETB3 (text[1])] \
			^ T2[GETB3 (text[2])] \
			^ T3[GETB3 (text[3])] \
			^ roundKey[3]; \
} /* squareRound */


#define squareFinal(text, temp, S, roundKey) \
{ \
	text[0] = PUTB0 (S[GETB0 (temp[0])]) \
			^ PUTB1 (S[GETB0 (temp[1])]) \
			^ PUTB2 (S[GETB0 (temp[2])]) \
			^ PUTB3 (S[GETB0 (temp[3])]) \
			^ roundKey[0]; \
	text[1] = PUTB0 (S[GETB1 (temp[0])]) \
			^ PUTB1 (S[GETB1 (temp[1])]) \
			^ PUTB2 (S[GETB1 (temp[2])]) \
			^ PUTB3 (S[GETB1 (temp[3])]) \
			^ roundKey[1]; \
	text[2] = PUTB0 (S[GETB2 (temp[0])]) \
			^ PUTB1 (S[GETB2 (temp[1])]) \
			^ PUTB2 (S[GETB2 (temp[2])]) \
			^ PUTB3 (S[GETB2 (temp[3])]) \
			^ roundKey[2]; \
	text[3] = PUTB0 (S[GETB3 (temp[0])]) \
			^ PUTB1 (S[GETB3 (temp[1])]) \
			^ PUTB2 (S[GETB3 (temp[2])]) \
			^ PUTB3 (S[GETB3 (temp[3])]) \
			^ roundKey[3]; \
} /* squareFinal */


void squareEncrypt (word32 text[4], squareKeySchedule roundKeys)
{
	word32 temp[4];

	/* initial key addition */
	text[0] ^= roundKeys[0][0];
	text[1] ^= roundKeys[0][1];
	text[2] ^= roundKeys[0][2];
	text[3] ^= roundKeys[0][3];

	/* R - 1 full rounds */
	squareRound (text, temp, Te0, Te1, Te2, Te3, roundKeys[1]);
	squareRound (temp, text, Te0, Te1, Te2, Te3, roundKeys[2]);
	squareRound (text, temp, Te0, Te1, Te2, Te3, roundKeys[3]);
	squareRound (temp, text, Te0, Te1, Te2, Te3, roundKeys[4]);
	squareRound (text, temp, Te0, Te1, Te2, Te3, roundKeys[5]);
	squareRound (temp, text, Te0, Te1, Te2, Te3, roundKeys[6]);
	squareRound (text, temp, Te0, Te1, Te2, Te3, roundKeys[7]);

	/* last round (diffusion becomes only transposition) */
	squareFinal (text, temp, Se, roundKeys[R]);

#ifdef DESTROY_TEMPORARIES
	/* destroy sensitive data: */
	temp[0] = temp[1] = temp[2] = temp[3] = 0L;
#endif /* ?DESTROY_TEMPORARIES */
} /* squareEncrypt */


void squareDecrypt (word32 text[4], squareKeySchedule roundKeys)
{
	word32 temp[4];

	/* initial key addition */
	text[0] ^= roundKeys[0][0];
	text[1] ^= roundKeys[0][1];
	text[2] ^= roundKeys[0][2];
	text[3] ^= roundKeys[0][3];

	/* R - 1 full rounds */
	squareRound (text, temp, Td0, Td1, Td2, Td3, roundKeys[1]);
	squareRound (temp, text, Td0, Td1, Td2, Td3, roundKeys[2]);
	squareRound (text, temp, Td0, Td1, Td2, Td3, roundKeys[3]);
	squareRound (temp, text, Td0, Td1, Td2, Td3, roundKeys[4]);
	squareRound (text, temp, Td0, Td1, Td2, Td3, roundKeys[5]);
	squareRound (temp, text, Td0, Td1, Td2, Td3, roundKeys[6]);
	squareRound (text, temp, Td0, Td1, Td2, Td3, roundKeys[7]);

	/* last round (diffusion becomes only transposition) */
	squareFinal (text, temp, Sd, roundKeys[R]);

#ifdef DESTROY_TEMPORARIES
	/* destroy sensitive data: */
	temp[0] = temp[1] = temp[2] = temp[3] = 0L;
#endif /* ?DESTROY_TEMPORARIES */
} /* squareDecrypt */

#endif /* ?ASSEMBLER_CORE */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -