📄 cave_test_new.c
字号:
k = msg_buf[msg_index - 1] ^ msg_buf[msg_index];
k &= etbox((unsigned char)(k ^ offseta), key);
msg_buf[msg_index - 1] ^= k;
msg_buf[msg_index] ^= k;
}
/* involutary lookup of present octet and offset rotation */
msg_buf[msg_index] = offseta ^
etbox((unsigned char)(msg_buf[msg_index] ^ offsetb), key);
offseta = (offseta << 1) | (offseta >> 7);
}
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
#define ECMEA_SOURCE_FILE
//#include "cavei.h" /* see Exhibit 2-3 */
//#include "ecmea.h" /* see Exhibit 2-29 */
#define MOBILE 1 /* set to 0 for base station algorithm */
void ECMEA(unsigned char *msg_buf, const int octet_count,
const unsigned char sync[2],
const unsigned int decrypt,
const unsigned int data_type)
{
unsigned char k, z, offset1, offset2, offsetc;
unsigned long x1, x2, s;
int msg_index;
unsigned char *key, *offset;
/* select key and offset key */
if (data_type)
{
key = ecmea_nf_key;
offset = offset_nf_key;
}
else
{
key = ecmea_key;
offset = offset_key;
}
/* calculate offsets */
/* offset12 =
((offset[1,0]+1)*(CS+1) mod 65537)^offset[3,2] mod 65536 */
x1 = ((unsigned long)offset[1] << 8) + (unsigned long)offset[0];
x2 = ((unsigned long)offset[3] << 8) + (unsigned long)offset[2];
s = ((unsigned long)sync[1] << 8) + (unsigned long)sync[0];
/* x1 = (((x1 + 1) * (s + 1)) % 65537) ^ x2; in two steps to
prevent overflow */
x1 = (x1 * (s + 1)) % 65537;
x1 = ((x1 + s + 1) % 65537) ^ x2;
offset1 = (unsigned char) (x1 >> 8);
offset2 = (unsigned char) (offset1 ^ x1);
if (offset2 == offset1)
offset2 ^= 1;
#if MOBILE
if (decrypt)
offsetc = offset2;
else
offsetc = offset1;
#else
if (decrypt)
offsetc = offset1;
else
offsetc = offset2;
#endif
/* initial transformation */
#if MOBILE
transform(msg_buf, octet_count, offset1, offset2, key);
#else
transform(msg_buf, octet_count, offset2, offset1, key);
#endif
/* CMEA */
/* first manipulation (inverse of third) */
z = 0;
for (msg_index = 0; msg_index < octet_count; msg_index++)
{
k = etbox((unsigned char)(z ^ offsetc), key);
msg_buf[msg_index] += k;
z = msg_buf[msg_index];
}
/* second manipulation (self-inverse) */
for (msg_index = 0; msg_index < octet_count - 1; msg_index += 2)
msg_buf[msg_index] ^= msg_buf[msg_index + 1];
/* third manipulation (inverse of first) */
z = 0;
for (msg_index = 0; msg_index < octet_count; msg_index++)
{
k = etbox((unsigned char)(z ^ offsetc), key);
z = msg_buf[msg_index];
msg_buf[msg_index] -= k;
}
/* final inverse transformation */
#if MOBILE
inv_transform(msg_buf, octet_count, offset2, offset1, key);
#else
inv_transform(msg_buf, octet_count, offset1, offset2, key);
#endif
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------
产生CDMAPLCM(见017p1-物理层(上)-参考标准)
--------------------------------------------------*/
void plcm_Generation(void)
{
int i,j;
unsigned char CdmaPLCM[5];
unsigned char plcm[5];
int binary[5][8]; //void ConvertHextoBinary(void)
printf("\n");
printf("用来产生CDMAPLCM !\n");
//第42、41个bit置为01
CdmaPLCM[0]=0x01;
for (i=0;i<5;i++) //plcm 专用长码掩码的产生 取最不重要的40bits
CdmaPLCM[i+1]=VPM[60+i];
//#ifdef _AUTH_DEBUG
printf("产生的CDMAPLCM=:0X");
for(i=0;i<6;i++)
printf("%2x",CdmaPLCM[i]);
printf("\n");
// #endif
for(i=0;i<6;i++)
printf("%2d ",CdmaPLCM[i]);
for(i=0;i<5;i++)
plcm[i]=CdmaPLCM[i+1];
printf("\n");
for(i=0;i<5;i++)
printf("%2d ",plcm[i]);
printf("\n");
printf("转化为二进制:");
for(i=0;i<5;i++)
for(j=0;j<8;j++)
{
binary[i][j]=plcm[i]%2;
plcm[i]= plcm[i]/2;
}
for(i=0;i<5;i++)
{
for(j=7;j>=0;j--)
printf("%d",binary[i][j]);
printf(" ");
}
return;
}
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
unsigned char ESN[4] = { 0xd7, 0x5a, 0x96, 0xec };
unsigned char MIN1[3] = { 0x79, 0x29, 0x71 };
unsigned char A_key[8];
unsigned char SSD_A_NEW[8], SSD_A[8];
unsigned char SSD_B_NEW[8], SSD_B[8];
/* data received from the network */
unsigned char RANDSSD[7] = { 0x4d, 0x18, 0xee, 0xaa,
0x05, 0x89, 0x5c };
unsigned char RAND1[4] = { 0x34, 0xa2, 0xb0, 0x5f };
unsigned char RAND2[4] = { 0x53, 0x75, 0xdf, 0x99 };
unsigned char RAND3[4] = { 0x6c, 0x00, 0x02, 0x58 };
/* cryptosync (meaning is air interface specific) */
unsigned char sync1[2] = { 0x3d, 0xa2 };
unsigned char sync2[2] = { 0xff, 0xff };
/* test plaintext */
unsigned char buf[6] = { 0xb6, 0x2d, 0xa2, 0x44, 0xfe, 0x9b };
/* entered A_key and checksum */
char digits[26] =
{ '1', '4', '1', '4', '2', '1', '3', '5', '6', '2',
'3', '7', '3', '0', '9', '5', '0', '4', '8', '8',
'0', '8', '6', '5', '0', '0' };
void pause(void)
{
printf("Enter to continue\n");
getchar();
}
void main(void)
{
int i, j;
unsigned char auth_data[3], test_buf[6];
unsigned long AUTHR;
/* check A_key and SSD */
if(A_Key_Verify(digits))
{
printf("A key verified ok\n");
}
else
{
printf("A key verification failed\n");
return;
}
/* check SSD generation process */
SSD_Generation(RANDSSD);
SSD_Update();
printf("SSD_A =");
for (i = 0; i < 4; i++)
{
printf(" ");
for (j = 0; j < 2; j++)
printf("%02x",(unsigned int)SSD_A[2*i+j]);
}
printf("\n");
printf("SSD_B =");
for (i = 0; i < 4; i++)
{
printf(" ");
for (j = 0; j < 2; j++)
printf("%02x",(unsigned int)SSD_B[2*i+j]);
}
printf("\n");
/* Inputs for test vectors */
/* put MIN1 into auth_data (no dialed digits for this test) */
for (i = 0; i < 3; i++)
auth_data[i] = MIN1[i];
/* vector 1 */
printf("\nVector 1\n\n");
AUTHR = Auth_Signature(RAND1,auth_data,SSD_A,1);
printf("RAND_CHALLENGE =");
for (i = 0; i < 2; i++)
{
printf(" ");
for (j = 0; j < 2; j++)
printf("%02x",(unsigned int)RAND1[2*i+j]);
}
printf("\n");
printf("AUTH_SIGNATURE = %01lx %04lx\n", AUTHR >> 16,
AUTHR & 0x0000ffff);
for (i = 0; i < 6; i++)
test_buf[i] = buf[i];
Key_VPM_Generation();
ECMEA_Secret_Generation();
Non_Financial_Seed_Key_Generation();
Non_Financial_Secret_Generation();
printf(" CMEA key =");
for (i = 0; i < 8; i++)
printf(" %02x",(unsigned int)cmeakey[i]);
printf("\n");
printf(" ECMEA key =");
for (i = 0; i < 8; i++)
printf(" %02x",(unsigned int)ecmea_key[i]);
printf("\n");
printf(" offset_key =");
for (i = 0; i < 4; i++)
printf(" %02x",(unsigned int)offset_key[i]);
printf("\n");
printf(" SEED_NF key =");
for (i = 0; i < 5; i++)
printf(" %02x",(unsigned int)seed_nf_key[i]);
printf("\n");
printf(" ECMEA_NF key =");
for (i = 0; i < 8; i++)
printf(" %02x",(unsigned int)ecmea_nf_key[i]);
printf("\n");
printf(" offset_nf_key =");
for (i = 0; i < 4; i++)
printf(" %02x",(unsigned int)offset_nf_key[i]);
printf("\n");
printf(" sync =");
printf(" %02x %02x\n",(unsigned int)sync1[0],
(unsigned int)sync1[1]);
pause();
printf(" Input =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
CMEA(test_buf,6);
printf(" CMEA Output =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
for (i = 0; i < 6; i++)
test_buf[i] = buf[i];
ECMEA(test_buf,6,sync1,0,0);
printf(" ECMEA Output =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
for (i = 0; i < 6; i++)
test_buf[i] = buf[i];
ECMEA(test_buf,6,sync1,0,1);
printf("ECMEA_NF Output =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
printf("VPM =");
for (i = 0; i < 65; i++)
{
printf(" %02x",(unsigned int)VPM[i]);
if (((i+1)%8) == 0)
printf("\n ");
}
//---------------------------------------------------------------------------//
plcm_Generation();
//---------------------------------------------------------------------------//
printf("\n");
pause();
/* vector 2 */
printf("\nVector 2\n\n");
AUTHR = Auth_Signature(RAND2,auth_data,SSD_A,1);
printf("RAND_CHALLENGE =");
for (i = 0; i < 2; i++)
{
printf(" ");
for (j = 0; j < 2; j++)
printf("%02x",(unsigned int)RAND2[2*i+j]);
}
printf("\n");
printf("AUTH_SIGNATURE = %01lx %04lx\n", AUTHR >> 16,
AUTHR & 0x0000ffff);
for (i = 0; i < 6; i++)
test_buf[i] = buf[i];
Key_VPM_Generation();
ECMEA_Secret_Generation();
Non_Financial_Seed_Key_Generation();
Non_Financial_Secret_Generation();
printf(" CMEA key =");
for (i = 0; i < 8; i++)
printf(" %02x",(unsigned int)cmeakey[i]);
printf("\n");
printf(" ECMEA key =");
for (i = 0; i < 8; i++)
printf(" %02x",(unsigned int)ecmea_key[i]);
printf("\n");
printf(" offset_key =");
for (i = 0; i < 4; i++)
printf(" %02x",(unsigned int)offset_key[i]);
printf("\n");
printf(" SEED_NF key =");
for (i = 0; i < 5; i++)
printf(" %02x",(unsigned int)seed_nf_key[i]);
printf("\n");
printf(" ECMEA_NF key =");
for (i = 0; i < 8; i++)
printf(" %02x",(unsigned int)ecmea_nf_key[i]);
printf("\n");
printf(" offset_nf_key =");
for (i = 0; i < 4; i++)
printf(" %02x",(unsigned int)offset_nf_key[i]);
printf("\n");
printf(" sync =");
printf(" %02x %02x\n",(unsigned int)sync2[0],
(unsigned int)sync2[1]);
pause();
printf(" Input =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
CMEA(test_buf,6);
printf(" CMEA Output =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
for (i = 0; i < 6; i++)
test_buf[i] = buf[i];
ECMEA(test_buf,6,sync2,0,0);
printf(" ECMEA Output =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
for (i = 0; i < 6; i++)
test_buf[i] = buf[i];
ECMEA(test_buf,6,sync2,0,1);
printf("ECMEA_NF Output =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
printf("VPM =");
for (i = 0; i < 65; i++)
{
printf(" %02x",(unsigned int)VPM[i]);
if (((i+1)%8) == 0)
printf("\n ");
}
//---------------------------------------------------------------------------//
plcm_Generation();
//---------------------------------------------------------------------------//
printf("\n");
pause();
/* vector 3 */
printf("\nVector 3\n\n");
AUTHR = Auth_Signature(RAND3,auth_data,SSD_A,1);
printf("RAND_CHALLENGE =");
for (i = 0; i < 2; i++)
{
printf(" ");
for (j = 0; j < 2; j++)
printf("%02x",(unsigned int)RAND3[2*i+j]);
}
printf("\n");
printf("AUTH_SIGNATURE = %01lx %04lx\n", AUTHR >> 16,
AUTHR & 0x0000ffff);
for (i = 0; i < 6; i++)
test_buf[i] = buf[i];
Key_VPM_Generation();
ECMEA_Secret_Generation();
Non_Financial_Seed_Key_Generation();
Non_Financial_Secret_Generation();
printf(" CMEA key =");
for (i = 0; i < 8; i++)
printf(" %02x",(unsigned int)cmeakey[i]);
printf("\n");
printf(" ECMEA key =");
for (i = 0; i < 8; i++)
printf(" %02x",(unsigned int)ecmea_key[i]);
printf("\n");
printf(" offset_key =");
for (i = 0; i < 4; i++)
printf(" %02x",(unsigned int)offset_key[i]);
printf("\n");
printf(" SEED_NF key =");
for (i = 0; i < 5; i++)
printf(" %02x",(unsigned int)seed_nf_key[i]);
printf("\n");
printf(" ECMEA_NF key =");
for (i = 0; i < 8; i++)
printf(" %02x",(unsigned int)ecmea_nf_key[i]);
printf("\n");
printf(" offset_nf_key =");
for (i = 0; i < 4; i++)
printf(" %02x",(unsigned int)offset_nf_key[i]);
printf("\n");
printf(" sync =");
printf(" %02x %02x\n",(unsigned int)sync2[0],
(unsigned int)sync2[1]);
pause();
printf(" Input =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
CMEA(test_buf,6);
printf(" CMEA Output =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
for (i = 0; i < 6; i++)
test_buf[i] = buf[i];
ECMEA(test_buf,6,sync2,0,0);
printf(" ECMEA Output =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
for (i = 0; i < 6; i++)
test_buf[i] = buf[i];
ECMEA(test_buf,6,sync2,0,1);
printf("ECMEA_NF Output =");
for (i = 0; i < 6; i++)
printf(" %02x",(unsigned int)test_buf[i]);
printf("\n");
printf("VPM =");
for (i = 0; i < 65; i++)
{
printf(" %02x",(unsigned int)VPM[i]);
if (((i+1)%8) == 0)
printf("\n ");
}
//---------------------------------------------------------------------------//
plcm_Generation();
//---------------------------------------------------------------------------//
printf("\n");
pause();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -