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

📄 desmain.c

📁 DES( Data Encryption Standard)算法
💻 C
📖 第 1 页 / 共 3 页
字号:

	FP(r2,l2);
	data2[0]=l2;
	data2[1]=r2;

	FP(r3,l3);
	data3[0]=l3;
	data3[1]=r3;	
	}	
	
/******************************************************************************/
/*     des_encrypt_core_3ch                                                   */
/*          This is the core Data Encryption Standard (DES) routine which     */
/*          encrypts 64 bits of data.  It does not perform the initial        */
/*          permutations or rotations of this data which are characteristic.  */
/*			Of the DES algorithm.  These rotations and permutations are done  */
/*          before this algorithm is called for every example included in     */
/*			this package, although these rotations and permutations do not    */
/*			increase the security of the algorithm and have predominantly     */ 
/*          historical value.  They are included because they are part of the */
/*          definition of DES but may be removed to increase performance.     */
/*                                                                            */
/*          This algorithm is exactly equivalent to des_encrypt_core above,   */
/*          but with three of everything.  It has been modified to handle     */
/*          three channels in parallel as an optimization.                    */
/*                                                                            */
/*          This code also includes a compiler flag option "DUAL_BANK." If    */
/*          -dDUAL_BANK is specified as a compiler option, the code makes a   */
/*          second copy of the des_SPtrans lookup table (the S- and P-Box     */
/*          transformations.)  It places on copy in the memory block from     */
/*          0x8000 0000 - 0x8000 7FFF and the second copy in the second       */
/*          memory block from 0x8000 8000 - 0x8000 FFFF.  This greatly        */
/*          the number of bank hits incurred by this random lookup process.   */
/*          The -dDUAL_BANK option should not be used with the 'C6211 as the  */
/*          second copy of the des_SPtrans lookup table will only decrease    */
/*          efficiency of the cache.                                          */
/*                                                                            */
/*          des_encrypt_core_3ch is called by des_encrypt_block_3ch and       */
/*          des3_encrypt_block_3ch.  See also the description of these        */
/*          functions below in this file.                                     */
/******************************************************************************/

void des_encrypt_core_3ch(data1, ks1, data2, ks2, data3, ks3)
DES_LONG *data1;
DES_LONG *data2;
DES_LONG *data3;

des_key_schedule ks1;
des_key_schedule ks2;
des_key_schedule ks3;
	{
	DES_LONG l1,r1,t1,u1, l2,r2,t2,u2, l3,r3,t3,u3, temp;   
	DES_LONG *s1, *s2, *s3;	
	DES_LONG lor11, lor12, lor13, lor14, lor21, lor22, lor23, lor24;
	DES_LONG lor31, lor32, lor33, lor34;
	unsigned int u11, u12, u13, u14, u21, u22, u23, u24, u31, u32, u33, u34;
	unsigned int t11, t12, t13, t14, t21, t22, t23, t24, t31, t32, t33, t34;
    int i;

	/* Things have been modified so that the initial rotate is
	 * done outside the loop.  This required the
	 * des_SPtrans values in sp.h to be rotated 1 bit to the right.
	 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
	 * for pointing this out. */

	s1=(DES_LONG *)ks1;
	s2=(DES_LONG *)ks2;
	s3=(DES_LONG *)ks3;

    r1= data1[0];
    l1= data1[1];
    r2= data2[0];
    l2= data2[1];
    r3= data3[0];
    l3= data3[1];

	for (i=0; i<32; i+=2) 
		{       
		u1=r1^s1[i  ]; 
		t1=r1^s1[i+1];
		t1=ROTATE(t1,4); 
		u11 = EXTRACT(u1, 24, 26); 
		u12 = EXTRACT(u1, 16, 26); 
		u13 = EXTRACT(u1, 8, 26); 
		u14 = EXTRACT(u1, 0, 26); 
		lor11 = des_SPtrans[0][u11];
#ifdef DUAL_BANK
		lor12 = des_SPtrans2[4][u13]; 
#else
		lor12 = des_SPtrans[4][u13]; 
#endif
		lor11 ^= des_SPtrans[2][u12];
#ifdef DUAL_BANK
		lor12 ^= des_SPtrans2[6][u14]; 
#else
		lor12 ^= des_SPtrans[6][u14]; 
#endif		
		t11 = EXTRACT(t1, 24, 26); 
		t12 = EXTRACT(t1, 16, 26); 
		t13 = EXTRACT(t1, 8, 26); 
		t14 = EXTRACT(t1, 0, 26); 
		lor13 = des_SPtrans[1][t11]; 
#ifdef DUAL_BANK
		lor14 = des_SPtrans2[5][t13];  
#else
		lor14 = des_SPtrans[5][t13];
#endif
		lor13 ^= des_SPtrans[3][t12];  
#ifdef DUAL_BANK
		lor14 ^= des_SPtrans2[7][t14]; 
#else
		lor14 ^= des_SPtrans[7][t14];
#endif				
		lor11 ^= lor12;
		lor13 ^= lor14;
		lor11 ^= lor13;
		 
		temp = l1 ^ lor11;
		l1 = r1;
		r1 = temp;       
		
		u2=r2^s2[i  ]; 
		t2=r2^s2[i+1];
		t2=ROTATE(t2,4); 
		u21 = EXTRACT(u2, 24, 26); 
		u22 = EXTRACT(u2, 16, 26); 
		u23 = EXTRACT(u2, 8, 26); 
		u24 = EXTRACT(u2, 0, 26); 

		lor21 = des_SPtrans[0][u21]; 
#ifdef DUAL_BANK
		lor22 = des_SPtrans2[4][u23]; 	  
#else
		lor22 = des_SPtrans[4][u23]; 	  
#endif
		lor21 ^= des_SPtrans[2][u22];
#ifdef DUAL_BANK
		lor22 ^= des_SPtrans2[6][u24]; 
#else
		lor22 ^= des_SPtrans[6][u24]; 
#endif
		t21 = EXTRACT(t2, 24, 26); 
		t22 = EXTRACT(t2, 16, 26); 
		t23 = EXTRACT(t2, 8, 26); 
		t24 = EXTRACT(t2, 0, 26); 
		lor23 = des_SPtrans[1][t21]; 
#ifdef DUAL_BANK
		lor24 = des_SPtrans2[5][t23]; 
#else
		lor24 = des_SPtrans[5][t23]; 
#endif
		lor23 ^= des_SPtrans[3][t22]; 
#ifdef DUAL_BANK
		lor24 ^= des_SPtrans2[7][t24]; 
#else
		lor24 ^= des_SPtrans[7][t24]; 
#endif

		lor21 ^= lor22;
		lor23 ^= lor24;
		lor21 ^= lor23;
		 
		temp = l2 ^ lor21;
		l2 = r2;
		r2 = temp;  
		
		u3=r3^s3[i  ]; 
		t3=r3^s3[i+1];
		t3=ROTATE(t3,4); 
		u31 = EXTRACT(u3, 24, 26); 
		u32 = EXTRACT(u3, 16, 26); 
		u33 = EXTRACT(u3, 8, 26); 
		u34 = EXTRACT(u3, 0, 26); 
		lor31 = des_SPtrans[0][u31];   
#ifdef DUAL_BANK
		lor32 = des_SPtrans2[4][u33]; 
#else
		lor32 = des_SPtrans[4][u33]; 
#endif
		lor31 ^= des_SPtrans[2][u32];
#ifdef DUAL_BANK
		lor32 ^= des_SPtrans2[6][u34]; 
#else
		lor32 ^= des_SPtrans[6][u34]; 
#endif
		t31 = EXTRACT(t3, 24, 26); 
		t32 = EXTRACT(t3, 16, 26); 
		t33 = EXTRACT(t3, 8, 26); 
		t34 = EXTRACT(t3, 0, 26); 
		lor33 = des_SPtrans[1][t31]; 
#ifdef DUAL_BANK
		lor34 = des_SPtrans2[5][t33]; 
#else
		lor34 = des_SPtrans[5][t33]; 
#endif
		lor33 ^= des_SPtrans[3][t32]; 
#ifdef DUAL_BANK
		lor34 ^= des_SPtrans2[7][t34];
#else
		lor34 ^= des_SPtrans[7][t34];
#endif		        

		lor31 ^= lor32;
		lor33 ^= lor34;
		lor31 ^= lor33;
		 
		temp = l3 ^ lor31;
		l3 = r3;
		r3 = temp;
		}

	data1[0] = l1;
	data1[1] = r1;      
	data2[0] = l2;
	data2[1] = r2;      
	data3[0] = l3;
	data3[1] = r3;   
	
	} 
    

/******************************************************************************/
/*     des_ecb_encrypt                                                        */
/*          This is the Data Encryption Standard (DES) routine which encrypts */
/*          a data array of arbitrary length in the Electronic Code Book mode */
/*          (ECB).  It assumes that the input array has a length divisible by */
/*          24 bytes.  If this data array does not have a length divisible by */
/*          24 bytes, it should be zero padded to prevent overflow.  The      */
/*          output array should be similarly padded.  The variable "length"   */
/*          does not need to be divisible by 24 as long as the input and      */
/*          output arrays are properly padded.                                */
/******************************************************************************/
void des_ecb_encrypt(input1, ks, output1, length)
des_cblock (*input1); 
des_key_schedule ks;
des_cblock (*output1);
int length;
{
	register DES_LONG *inint1, *outint1;
	register int l=length; 

	inint1 = (DES_LONG *)input1;  
	outint1 = (DES_LONG *)output1; 
	
	for (; l>0; l-=24)
		{

		*outint1++ = (*inint1++);
		*outint1++ = (*inint1++);
		*outint1++ = (*inint1++);
		*outint1++ = (*inint1++);
		*outint1++ = (*inint1++);
		*outint1++ = (*inint1++);
		
		des_encrypt_block_3ch((DES_LONG *)(outint1 - 6),ks, (DES_LONG *)(outint1 - 4),ks, (DES_LONG *)(outint1 - 2),ks);
	
		}
}

/******************************************************************************/
/*     des_ecb_decrypt                                                        */
/*          This is the Data Encryption Standard (DES) routine which decrypts */
/*          a data array of arbitrary length in the Electronic Code Book mode */
/*          (ECB).  It assumes that the input array has a length divisible by */
/*          24 bytes.  If this data array does not have a length divisible by */
/*          24 bytes, it should be zero padded to prevent overflow.  The      */
/*          output array should be similarly padded.  The variable "length"   */
/*          does not need to be divisible by 24 as long as the input and      */
/*          output arrays are properly padded.                                */
/*																			  */
/*          The key schedule provided to this function should be the same key */
/*          schedule that is provided to des_ecb_encrypt.  The function       */
/*          reorganizes the key schedule to decrypt using the block           */
/*          encryption function.  (It is a property of the DES algorithm that */
/*          the core encryption routine is also used to decrypt, but with a   */
/*          modification to the key schedule.)                                */
/******************************************************************************/
void des_ecb_decrypt(input, ks, output, length)
des_cblock (*input); 
des_key_schedule ks;
des_cblock (*output);
int length;    
{
	register DES_LONG *inint, *outint;
	register int l=length; 
	des_key_schedule ks_decrypt;
    register int i;
    
	inint=(DES_LONG *)input;
	outint=(DES_LONG *)output;   
	
	/*  Generate key schedule to decrypt using DES_encrypt                */	
	for(i=0;i<32;i+=2){ 
		*(((DES_LONG *) ks_decrypt)+i  ) = *(((DES_LONG *) ks)+30-i);
		*(((DES_LONG *) ks_decrypt)+i+1) = *(((DES_LONG *) ks)+31-i);
	}	

	for (; l>0; l-=24)
		{
		*outint++ = (*inint++);
		*outint++ = (*inint++);
		*outint++ = (*inint++);
		*outint++ = (*inint++);
		*outint++ = (*inint++);
		*outint++ = (*inint++);

		des_encrypt_block_3ch((DES_LONG *)(outint-6),ks_decrypt, (DES_LONG *)(outint - 4),ks_decrypt, (DES_LONG *)(outint - 2), ks_decrypt);
 		} 
}         


/******************************************************************************/
/*     des_cbc_encrypt                                                        */
/*          This is the Data Encryption Standard (DES) routine which encrypts */
/*          a data array of arbitrary  length in the Cipher Block Chaining    */
/*          (CBC) mode.  It assumes that the input array has a length         */
/*          divisible by 8 bytes.  If this data array does not have a length  */
/*          divisible by 8 bytes, it should be zero padded to prevent         */
/*          overflow.  The output array should be similarly padded.           */
/*                                                                            */
/*          The initialization vector, "ivec" is overwritten with the last    */
/*          8 bytes of encrypted output.  This new "ivec" may be used as the  */
/*          initialization vector for the next block of data.                 */
/******************************************************************************/
void des_cbc_encrypt(input, ivec, ks, output, length)
des_cblock (*input);
des_cblock (*ivec);
des_key_schedule ks;
des_cblock (*output);
int length;
{
	register DES_LONG tout0,tout1;
	register DES_LONG *inint, *outint;
	DES_LONG *iv;

	register int l=length;   

	inint = (DES_LONG *)input;  
	outint = (DES_LONG *)output;
	iv=(DES_LONG *)ivec;

	tout0 = *iv++;
	tout1 = *iv++;

	for (; l>=0; l-=8)
		{

		*(outint   ) = (*inint++) ^ tout0;
		*(outint +1) = (*inint++) ^ tout1;
		
		des_encrypt_block((DES_LONG *)outint,ks);

		tout0=*outint++;
		tout1=*outint++;
		}
	iv=(DES_LONG *)ivec;

	*iv++ = tout0;
	*iv++ = tout1;
}


/******************************************************************************/
/*     des_cbc_decrypt                                                        */
/*          This is the Data Encryption Standard (DES) routine which decrypts */
/*          a data array of arbitrary length in the Cipher Block Chaining mode*/
/*          (CBC).  It assumes that the input array has a length divisible by */
/*          8 bytes.  If this data array does not have a length divisible by  */
/*          8 bytes, it should be zero padded to prevent overflow.  The       */
/*          output array should be similarly padded.  The variable "length"   */
/*          does not need to be divisible by 8 as long as the input and       */
/*          output arrays are properly padded.                                */
/*                                                                            */
/*          The initialization vector, "ivec" is overwritten with the last    */
/*          8 bytes of de-encrypted output.  This new "ivec" may be used as   */
/*          the initialization vector for the next block of data.             */
/*																			  */
/*          The key schedule provided to this function should be the same key */
/*          schedule that is provided to des_cbc_encrypt.  The function       */
/*          reorganizes the key schedule to decrypt using the block           */
/*          encryption function.  (It is a property of the DES algorithm that */
/*          the core encryption routine is also used to decrypt, but with a   */
/*          modification to the key schedule.)                                */
/******************************************************************************/			
void des_cbc_decrypt(input, ivec, ks, output, length)
des_cblock (*input);
des_cblock (*ivec);
des_key_schedule ks;
des_cblock (*output);
int length;
{
	register DES_LONG xor0,xor1;
	register DES_LONG t0,t1;
	register DES_LONG *inint, *outint;
	DES_LONG *iv;

	register int l=length,i;  
	
	des_key_schedule ks_decrypt;
	
	/*  Generate key schedule to decrypt using DES_encrypt               */	
	for(i=0;i<32;i+=2){ 
		*(((DES_LONG *) ks_decrypt)+i  ) = *(((DES_LONG *) ks)+30-i);
		*(((DES_LONG *) ks_decrypt)+i+1) = *(((DES_LONG *) ks)+31-i);
	}	

	inint=(DES_LONG *)input;
	outint=(DES_LONG *)output;
	iv=(DES_LONG *)ivec;  

	xor0  = *iv++;
	xor1  = *iv++;

	for (; l>0; l-=8)
		{
		*(outint   ) = t0  = (*inint++);
		*(outint +1) = t1  = (*inint++);

		des_encrypt_block((DES_LONG *)outint,ks_decrypt);
		
		*outint++ ^= xor0;
		*outint++ ^= xor1;
		
		xor0  = t0;
		xor1  = t1;
		} 
	iv=(DES_LONG *)ivec;
	*iv++ = t0;
	*iv++ = t1;	
}         



/*void des_encrypt_core_3ch(data1, ks1, data2, ks2, data3, ks3)
DES_LONG *data1;
DES_LONG *data2;
DES_LONG *data3;

des_key_schedule ks1;
des_key_schedule ks2;
des_key_schedule ks3;
	{
	DES_LONG l1,r1,t1,u1, l2,r2,t2,u2, l3,r3,t3,u3, temp;   
	DES_LONG *s1, *s2, *s3;	
	DES_LONG lor11, lor12, lor13, lor14, lor21, lor22, lor23, lor24;
	DES_LONG lor31, lor32, lor33, lor34;
	unsigned int u11, u12, u13, u14, u21, u22, u23, u24, u31, u32, u33, u34;
	unsigned int t11, t12, t13, t14, t21, t22, t23, t24, t31, t32, t33, t34;
    int i;

	/* Things have been modified so that the initial rotate is
	 * done outside the loop.  This required the
	 * des_SPtrans values in sp.h to be rotated 1 bit to the right.
	 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
	 * for pointing this out. */

/*	s1=(DES_LONG *)ks1;
	s2=(DES_LONG *)ks2;
	s3=(DES_LONG *)ks3;

    r1= data1[0];
    l1= data1[1];
    r2= data2[0];
    l2= data2[1];
    r3= data3[0];
    l3= data3[1];

	for (i=0; i<32; i+=2) 
		{       
		u1=r1^s1[i  ]; 
		t1=r1^s1[i+1];
		t1=ROTATE(t1,4); 
		u11 = EXTRACT(u1, 24, 26); 
		u12 = EXTRACT(u1, 16, 26); 
		u13 = EXTRACT(u1, 8, 26); 
		u14 = EXTRACT(u1, 0, 26); 
		lor11 = des_SPtrans[0][u11];
#ifdef DUAL_BANK
		lor12 = des_SPtrans2[4][u13]; 
#else
		lor12 = des_SPtrans[4][u13]; 
#endif
		lor11 ^= des_SPtrans[2][u12];
#ifdef DUAL_BANK
		lor12 ^= des_SPtrans2[6][u14]; 
#else
		lor12 ^= des_SPtrans[6][u14]; 
#endif		
		t11 = EXTRACT(t1, 24, 26); 
		t12 = EXTRACT(t1, 16, 26); 
		t13 = EXTRACT(t1, 8, 26); 
		t14 = EXTRACT(t1, 0, 26); 
		lor13 = des_SPtrans[1][t11]; 
#ifdef DUAL_BANK

⌨️ 快捷键说明

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