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

📄 mm32st.c

📁 phs 源代码 小灵通协议源代码 phs source code
💻 C
字号:
/******************************************************************************
	Copyright (C) 2001 UTStarcom (Hangzhou) Telecom Co., Ltd.
	All rights reserved.
    Contains confidential proprietary information of UTStarcom, Inc.
    Reverse engineering is prohibited.
    The copyright notice does not imply publication.

	Project Name: Engine
	Project No.	:
	File Name	: stephi.c
	Module		:  MM
	Description	: Stephi algorithm file. This file is obtained from other project and its quality has
	             been confirmed. The only interface that this file provides is Mm_STEPHIenc( ) which
	             should be called in other MM layer source files.

    ************** REVISION HISTORY *******************************************
	Date     Author			Reference
	======== ==========		==========================
	2002.1.1 Qian Aidong
******************************************************************************/
#ifndef PM_ONPC

#ifdef __cplusplus
extern "C"{
#endif
/************** INCLUDES *****************************************************/
#include "phs_def.h"
/************** LOCAL CONSTANTS **********************************************/

/************** LOCAL STRUCTURES, ENUMS, AND TYPEDEFS ************************/

/************** LOCAL FUNCTION PROTOTYPES ************************************/

/************** LOCAL MACROS *************************************************/

/************** LOCAL VARIABLES **********************************************/

/************** FUNCTION DEFINITIONS *****************************************/

/************** Remark by Qian Aidong ****************************************/



/******************************************************************************/
/* 1. ste_ope             :   stephi calculation							*/
/* 2. ste_setkey          :   partial key generation						*/
/* 3. ste_stephi          :   calculation 1									*/
/* 4. ste_round           :   calculation 2									*/
/* 5. ste_parity          :   calculation 3					                */
/******************************************************************************/

#include "stephi.h"

void ste_ope( STE_TEXT *key, STE_TEXT *inRaNumOutResult )
{
	U2	IK_S0,IK_S1;	/* Input_Key: Lower 32bits */
	U1	IK_C;		/* Input_Key: Upper  8bits */
	U2	SUBK[2*Nround];	/* Subkey: 16bits*2*Nround */

	IK_C  =  (U1) key->text[1];
	IK_S0 =  (U2) key->text[2];
	IK_S1 =  (U2) key->text[3];

	ste_setkey( IK_C, IK_S0, IK_S1, SUBK );

	ste_stephi_B( SUBK, (U2 *) inRaNumOutResult, 0 );
}


/* === Key Generation Function === */
/* [Input]  U1  KC 				*/
/*	    U2 KS0, KS1 : Input_KEY(40bits) */
/* [Output] U2 SUBK[2*Nround] : SUBKEYS     */
void ste_setkey( U1 KC, U2 KS0, U2 KS1, U2 SUBK[2*Nround] ){

	static U2 SYSK[16] = {
	0x529a,0xbfde,0xb76c,0x28af,0xe801,0x3fa7,0xf6c4,0xf62b,
	0x0afb,0xf7ad,0xe69b,0x2cf0,0x18be,0x65e0,0x4821,0x553c	};

	U2		x[4];
	U2	i;

	/* Data Transfer 40bits-->64bits */
	x[0]=0x0000;	x[1]=(U2)(KC&0xff);
	x[2]=KS0;	x[3]=KS1;

	/* Key Generation Loop */
	for( i=0 ; i<Nround ; i++ ){
		ste_round( x, SYSK[(2*i)&0x7], SYSK[(2*i+1)&0x7] );
		SUBK[2*i]=x[2];	SUBK[2*i+1]=x[3];
	}
}
/* === End of Key Generation Function === */


/* === Encrypt Function === */
/* [Input]  		U2 SUBK[2*Nround] : SUBKEYS 	*/
/* [Input/Output]	U2 x[4] : Data(64bits)	  	*/
/* [Input]			short	encdec	: encryption mode
		  if encdec=0: encrypt, else: decrypt		*/
/* x[4] --(Encrypt/Decrypt)--> x[4] by SUBK[2*Nround]		*/
void ste_stephi_B( U2 SUBK[2*Nround], U2 x[4], short encdec ){
	U2        i;
	U2		y[2];

	/* Encryption Mode */
	if( !(encdec) )
	{
		for( i=0 ; i<Nround ; i++ )
		{
			ste_round( x, SUBK[2*i], SUBK[2*i+1] );

		}
	}
	else
	{					/* Decryption Mode */
		for( i=(Nround-1) ; i>=0 ; i-- )
		{
			ste_round( x, SUBK[2*i], SUBK[2*i+1] );
		}
	}

	/* Final Replace */
	y[0]=x[0];	y[1]=x[1];
	x[0]=x[2];	x[1]=x[3];
	x[2]=y[0];	x[3]=y[1];
}
/* === End of Encrypt Function === */


/* === Round Function ( including Swap Operation ) ===  	*/
/* [Input/Output] U2 x[4] : Data(64bits)		*/
/* [Input] U2 k0,k1 : Round Key (16bits+16bits)	*/
/* x[4] --(Encrypt)--> x[4] by k0, k1 				*/
void ste_round( U2 x[4], U2 k0, U2 k1 )
{

	U2	kx0,kx1,kx2,kx3,b2,b3,c2,c3,d2,d3;

	/* ### F-funcion Operation ### */
	/* Data Transfer */
	c2=d2=x[2];	c3=d3=x[3];

	/* Key Addition1 */
	c2+=k0; 	c3+=k1;

	/* Bit Reverse */
	c2=~c2;		c3=~c3;

	/* Matrix */
	kx0=(k0>>1)|(k1<<15);	kx1=(k1>>1)|(k0<<15);

	if( ste_parity( c2, c3, kx0, kx1 ) )
	{
		b2=(c2^(~(kx0)))&0x0040;	b3=(c3^(~(kx1)))&0x0040;
	}
	else
	{
		b2=c2&0x0040;			b3=c3&0x0040;
	}

	kx2=(kx0>>1)|(kx1<<15);		kx3=(kx1>>1)|(kx0<<15);

	if( ste_parity( c2, c3, kx2, kx3 ) )
	{
		b2|= (c2^(~(kx0)))&0xffbf;	b3|= (c3^(~(kx1)))&0xffbf;
	}
	else
	{
		b2|=c2&0xffbf;			b3|=c3&0xffbf;
	}

	/* Permutation1 */
	c2=(b2&0xaa00)    |((b2&0x5500)>>8)|((b2&0x0002)<<13)
         |((b2&0x0008)<<9)|((b2&0x0020)<<5)|((b2&0x0080)<<1)
         |((b2&0x0001)<<7)|((b2&0x0004)<<3)|((b2&0x0010)>>1)
         |((b2&0x0040)>>5);
	c3=(b3&0xaa00)    |((b3&0x5500)>>8)|((b3&0x0002)<<13)
         |((b3&0x0008)<<9)|((b3&0x0020)<<5)|((b3&0x0080)<<1)
         |((b3&0x0001)<<7)|((b3&0x0004)<<3)|((b3&0x0010)>>1)
         |((b3&0x0040)>>5);

	/* Block Change1 */
	b2=(c2<<8)|(c3>>8);
	b3=(c2&0xff00)|(c3&0x00ff);

	/* Rot7(+)Rot1(+)Id */
	c2=((b2<<7)|(b3>>9))^((b2<<1)|(b3>>15))^b2;
	c3=((b3<<7)|(b2>>9))^((b3<<1)|(b2>>15))^b3;

	/* Block Change2 */
	b2=c2;
	b3=(c3<<8)|(c3>>8);

	/* Permutation2 */
        c2=(b2&0xbf7c)     |((b2&0x4000)>>13)|((b2&0x0080)>>7)
	 |((b2&0x0002)<<13)|((b2&0x0001)<<7);
        c3=(b3&0xbf7c)     |((b3&0x4000)>>13)|((b3&0x0080)>>7)
	 |((b3&0x0002)<<13)|((b3&0x0001)<<7);

	/* Key Addition2 */
	c2+=(k0+0x1357);	c3+=(k1+0x1357);

	/* 1bit left cyclic shift */
	b2=(c2<<1)|(c3>>15);	b3=(c3<<1)|(c2>>15);

	/* ### Swap Operation ### */
	x[2]=b2^x[0];	x[3]=b3^x[1];
	x[0]=d2;	x[1]=d3;

}
/* === Round Function === */


/* === Parity === */
/* [Input]  U2 x0,x1 : Data(32bits) 	*/
/* [Input]  U2 k0,k1 : Keys(32bits) 	*/
/* [Return] 0/1 : Even Parity of ((x0,x1)&(k0,k1))	*/
U1 ste_parity( U2 x0, U2 x1, U2 k0, U2 k1 ){
	static U1	pr[16] = { 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0 };
	x0&=k0;		x1&=k1;
	x0^=(x0>>8);	x1^=(x1>>8);
	x0^=(x0>>4);	x1^=(x1>>4);
	x0^=x1;
	return	pr[x0&0xf];
}
/* === End of Parity === */


/**************You can add a call function in your main function**************/
void	Mm_STEPHIenc(U1 *AuthKey, U1 *AuthRnd, U1 *AuthResult)	//STEPHI
{
	U2	AuthResultBuf[4];					// result buffer
	U2	AuthKeyBuf[4];						// authkey buffer
	U1 i, j;
	U1 AuthKeyReal[8];

	for(i = 0; i<5; i++)
	{
		AuthKeyReal[3+i] = *(AuthKey+i);
	}

// from now on, please remember to set AuthKey and AuthRnd before exe the following sentence
	for( i = 0, j= 0; i < 4; i++,j+=2 )
	{
		AuthKeyBuf[i]  = (U2)*(AuthKeyReal+j) << 8;
		AuthKeyBuf[i] |= (U2)*(AuthKeyReal+j+1) ;
	}

	for( i = 0, j = 0; i < 4; i++,j+=2 )
	{
		AuthResultBuf[i]  = (U2)*(AuthRnd+j) << 8;
		AuthResultBuf[i] |= (U2)*(AuthRnd+j+1) ;
	}

//
//	funtion ste_ope is the entry of the Stephi, please note the two parameters
//	Stephi calculation issued, cipher is stored in the variable AuthResultBuf
//
	ste_ope( (STE_TEXT*)AuthKeyBuf, (STE_TEXT*)AuthResultBuf );

// copy value of AuthResultBuf to AuthKeyBuf, it's simply a type conversion
	for( i = 0, j = 0; i < 4; i++,j+=2 )
	{
		*(AuthResult+j)   = (U1)(AuthResultBuf[i] >> 8);
		*(AuthResult+j+1) = (U1)AuthResultBuf[i];
	}
}

#ifdef __cplusplus
}
#endif

#endif

⌨️ 快捷键说明

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