📄 cave_test_new.c
字号:
#include <stdio.h>
//#include "cave.h" /* see Exhibit 2-2 */
//#include "ecmea.h" /* see Exhibit 2-29 */
/* NAM stored data */
#ifndef CAVE_H
#define CAVE_H
/* external header for CAVE and related procedures */
/* function declarations */
void CAVE( int number_of_rounds,
int *offset_1,
int *offset_2);
void A_Key_Checksum( char A_KEY_DIGITS[20],
char A_KEY_CHECKSUM[6]);
int A_Key_Verify( char A_KEY_DIGITS[26]);
void SSD_Generation( unsigned char RANDSSD[7]);
void SSD_Update(void);
unsigned long Auth_Signature( unsigned char RAND_CHALLENGE[4],
//const
unsigned char AUTH_DATA[3],
//const
unsigned char *SSD_AUTH,
//const
int SAVE_REGISTERS);
void Key_VPM_Generation(void);
void CMEA(unsigned char *msg_buf, int octet_count);
/* global variable definitions */
#ifdef CAVE_SOURCE_FILE
#define CAVE_GLOBAL
#else
#define CAVE_GLOBAL extern
#endif
/* externally available results */
//CAVE_GLOBAL
unsigned char cmeakey[8];
//CAVE_GLOBAL
unsigned char VPM[65];
//CAVE_GLOBAL
unsigned char SAVED_LFSR[4];
//CAVE_GLOBAL
int SAVED_OFFSET_1;
//CAVE_GLOBAL
int SAVED_OFFSET_2;
//CAVE_GLOBAL
unsigned char SAVED_RAND[4];
//CAVE_GLOBAL
unsigned char SAVED_DATA[3];
/* global constant definitions */
#ifndef CAVE_SOURCE_FILE
//CAVE_GLOBAL
unsigned char CaveTable[256];
//CAVE_GLOBAL
unsigned char ibox[256];
#endif // ifndef CAVE_SOURCE_FILE
#endif // ifndef CAVE_H
/* end of CAVE external header */
/********************************************************************/
/* internal header for CAVE, used by all cryptographic source files */
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void ECMEA_Secret_Generation(void);
void Non_Financial_Seed_Key_Generation(void);
void Non_Financial_Secret_Generation(void);
void ECMEA(unsigned char *msg_buf,
const int octet_count,
const unsigned char sync[2],
const unsigned int decrypt,
const unsigned int data_type);
#ifndef ECMEA_SOURCE_FILE
//extern
unsigned char ecmea_key[8];
//extern
unsigned char ecmea_nf_key[8];
//extern
unsigned char offset_key[4];
//extern
unsigned char offset_nf_key[4];
//extern
unsigned char seed_nf_key[5];
#endif
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//#include "cave.h" /* see Exhibit 2-2 */
/* authentication algorithm version (fixed) */
#define AAV 0xC7
#define LOMASK 0x0F
#define HIMASK 0xF0
#define TRUE 1
#define FALSE 0
/* NAM stored data */
//extern
unsigned char ESN[4];
//extern
unsigned char A_key[8];
//extern
unsigned char SSD_A_NEW[8], SSD_A[8];
//extern
unsigned char SSD_B_NEW[8], SSD_B[8];
/* saved outputs */
//CAVE_GLOBAL
unsigned char LFSR[4];
#define LFSR_A LFSR[0]
#define LFSR_B LFSR[1]
#define LFSR_C LFSR[2]
#define LFSR_D LFSR[3]
//CAVE_GLOBAL
unsigned char Register[16];
#define CAVE_SOURCE_FILE
//#include "cavei.h" /* see Exhibit 2-3 */
/********************************************************************/
/* table0 is the 4 lsbs of the array,
table1 is the 4 msbs of the array */
unsigned char CaveTable[256] =
{ 0xd9, 0x23, 0x5f, 0xe6, 0xca, 0x68, 0x97, 0xb0,
0x7b, 0xf2, 0x0c, 0x34, 0x11, 0xa5, 0x8d, 0x4e,
0x0a, 0x46, 0x77, 0x8d, 0x10, 0x9f, 0x5e, 0x62,
0xf1, 0x34, 0xec, 0xa5, 0xc9, 0xb3, 0xd8, 0x2b,
0x59, 0x47, 0xe3, 0xd2, 0xff, 0xae, 0x64, 0xca,
0x15, 0x8b, 0x7d, 0x38, 0x21, 0xbc, 0x96, 0x00,
0x49, 0x56, 0x23, 0x15, 0x97, 0xe4, 0xcb, 0x6f,
0xf2, 0x70, 0x3c, 0x88, 0xba, 0xd1, 0x0d, 0xae,
0xe2, 0x38, 0xba, 0x44, 0x9f, 0x83, 0x5d, 0x1c,
0xde, 0xab, 0xc7, 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fe, 0x6f, 0xa6, 0x4b, 0xa9, 0x42,
0x6c, 0x9a, 0x18, 0x8a, 0xd2, 0x39, 0x8f, 0x58,
0x13, 0xad, 0x88, 0x28, 0xb0, 0x35, 0xd7, 0xe1,
0x5d, 0x93, 0xc4, 0xb9, 0x55, 0x2b, 0x7d, 0xce,
0xe0, 0x31, 0xfd, 0x9b, 0x3a, 0x00, 0x34, 0xe5,
0xd8, 0xcf, 0xa5, 0x9d, 0xac, 0xdf, 0x7b, 0xf9,
0x85, 0x67, 0x8b, 0xf6, 0xf8, 0x37, 0x2d, 0x7e,
0x1e, 0xb2, 0x66, 0x01, 0x64, 0x05, 0xeb, 0x02,
0xec, 0xe7, 0xb1, 0x7a, 0x32, 0xda, 0xba, 0x4e };
/* CAVE local functions */
static unsigned char bit_val(const unsigned char octet, const int bit)
{
return((octet << (7 - bit)) & 0x80);
}
static void LFSR_cycle(void)
{
unsigned char temp;
int i;
temp = bit_val(LFSR_B,6);
temp ^= bit_val(LFSR_D,2);
temp ^= bit_val(LFSR_D,1);
temp ^= bit_val(LFSR_D,0);
/* Shift right LFSR, Discard LFSR_D[0] bit */
for (i = 3; i > 0; i--)
{
LFSR[i] >>= 1;
if (LFSR[i-1] & 0x01)
LFSR[i] |= 0x80;
}
LFSR[0] >>= 1;
LFSR_A |= temp;
}
static void Rotate_right_registers(void)
{
unsigned int temp_reg;
int i;
temp_reg = Register[15]; /* save lsb */
for (i = 15; i > 0; i--)
{
Register[i] >>= 1;
if (Register[i-1] & 0x01)
Register[i] |= 0x80;
}
Register[0] >>= 1;
if (temp_reg & 0x01)
Register[0] |= 0x80;
}
void CAVE(const int number_of_rounds,
int *offset_1,
int *offset_2)
{
unsigned char temp_reg0;
unsigned char lowNibble;
unsigned char hiNibble;
unsigned char temp;
int round_index;
int R_index;
int fail_count;
unsigned char T[16];
for (round_index = number_of_rounds - 1;
round_index >= 0;
round_index--)
{
/* save R0 for reuse later */
temp_reg0 = Register[0];
for (R_index = 0; R_index < 16; R_index++)
{
fail_count = 0;
while(1)
{
*offset_1 += (LFSR_A ^ Register[R_index]);
/* will overflow; mask to prevent */
*offset_1 &= 0xff;
lowNibble = CaveTable[*offset_1] & LOMASK;
if (lowNibble == (Register[R_index] & LOMASK))
{
LFSR_cycle();
fail_count++;
if (fail_count == 32)
{
LFSR_D++; /* no carry to LFSR_C */
break;
}
}
else break;
}
fail_count = 0;
while(1)
{
*offset_2 += (LFSR_B ^ Register[R_index]);
/* will overflow; mask to prevent */
*offset_2 &= 0xff;
hiNibble = CaveTable[*offset_2] & HIMASK;
if (hiNibble == (Register[R_index] & HIMASK))
{
LFSR_cycle();
fail_count++;
if (fail_count == 32)
{
LFSR_D++; /* no carry to LFSR_C */
break;
}
}
else
break;
}
temp = lowNibble | hiNibble;
if (R_index == 15)
Register[R_index] = temp_reg0 ^ temp;
else
Register[R_index] = Register[R_index+1] ^ temp;
LFSR_cycle();
}
Rotate_right_registers();
/* shuffle the mixing registers */
for (R_index = 0; R_index < 16; R_index++)
{
temp = CaveTable[16*round_index + R_index] & LOMASK;
T[temp] = Register[R_index];
}
for (R_index = 0; R_index < 16; R_index++)
Register[R_index] = T[R_index];
}
}
/* A_Key_Checksum has the same header as CAVE (see Exhibit 2-4) */
static void mul10(unsigned char i64[8], unsigned int carry)
{
int i;
unsigned int temp;
for (i = 7; i >= 0; i--)
{
temp = ((unsigned int)(i64[i]) * 10) + carry;
i64[i] = temp & 0xFF;
carry = temp >> 8;
}
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
static unsigned long Calc_Checksum(const unsigned char A_key[8])
{
int i,offset_1,offset_2;
unsigned long A_key_checksum;
/* see if 32 MSB are zero */
if ((A_key[0] | A_key[1] | A_key[2] | A_key[3]) != 0)
{
/* put 32 MSB into LFSR */
for (i = 0; i < 4; i++)
LFSR[i] = A_key[i];
}
else
{
/* put ESN into LFSR */
for (i = 0; i < 4; i++)
LFSR[i] = ESN[i];
}
/* put A_key into r0-r7 */
for (i = 0; i < 8; i++)
Register[i] = A_key[i];
Register[8] = AAV;
/* put ls 24 bits of A_key into r9-r11 */
for (i = 9; i < 12; i++)
Register[i] = A_key[5+i-9];
/* put ESN into r12-r15 */
for (i = 12; i < 16; i++)
Register[i] = ESN[i-12];
offset_1 = offset_2 = 128;
CAVE(8, &offset_1, &offset_2);
A_key_checksum =
( ((unsigned long)(Register[0] ^ Register[13]) << 16) +
((unsigned long)(Register[1] ^ Register[14]) << 8) +
((unsigned long)(Register[2] ^ Register[15]) )) & 0x3ffff;
////////////////////////////////////////////////////////////////////////////
printf("A_key_checksum(Calc_Checksum(temp_A_key)的计算结果)=");//后加的
printf("%x\n",A_key_checksum);//后加的
//////////////////////////////////////////////////////
return (A_key_checksum);
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
/* A_KEY_DIGITS contains the ASCII digits in the order to be entered */
void A_Key_Checksum(const char A_KEY_DIGITS[20],char A_KEY_CHECKSUM[6])
{
int i;
unsigned char temp_A_key[8];
unsigned long A_key_checksum;
/* convert digits to 64-bit representation in temp_A_key */
for (i = 0; i < 8; i++)
temp_A_key[i] = 0;
for (i = 0; i < 20; i++)
{
mul10(temp_A_key,(unsigned int)(A_KEY_DIGITS[i] - '0'));
}
A_key_checksum = Calc_Checksum(temp_A_key);
/* convert checksum to decimal digits */
for (i = 0; i < 6; i++)
{
A_KEY_CHECKSUM[5-i] = '0' + (char)(A_key_checksum % 10);
A_key_checksum /= 10;
}
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
/* A_Key_Verify has the same header as CAVE (see Exhibit 2-4) */
/* A_KEY_DIGITS contains the ASCII digits in the order entered */
int A_Key_Verify(const char A_KEY_DIGITS[26])
{
int i;
unsigned char temp_A_key[8];
unsigned long entered_checksum;
/* convert first 20 digits to 64-bit representation in temp_A_key */
for (i = 0; i < 8; i++)
temp_A_key[i] = 0;
for (i = 0; i < 20; i++)
{
mul10(temp_A_key,(unsigned int)(A_KEY_DIGITS[i] - '0'));
}
/////以下为后补充的////////////////
////////////////////////////////
printf("A_key= ");
for(i=0;i<8;i++)
printf("%3x", temp_A_key[i]);
printf("\n");
//////////////////////////////////////
/* convert last 6 digits to entered checksum */
entered_checksum = 0;
for (i = 20; i < 26; i++)
{
entered_checksum = (entered_checksum * 10)
+ (A_KEY_DIGITS[i] - '0');
}
if(Calc_Checksum(temp_A_key) == entered_checksum)
{
for (i = 0; i < 8; i++)
{
A_key[i] = temp_A_key[i];
SSD_A[i] = SSD_B[i] = 0;
}
return TRUE;
}
else
{
return FALSE;
}
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
/* SSD_Generation has the same header as CAVE (see Exhibit 2-4) */
void SSD_Generation(const unsigned char RANDSSD[7])
{
int i,offset_1,offset_2;
for (i = 0; i < 4; i++)
{
LFSR[i] = RANDSSD[i+3] ^ A_key[i] ^ A_key[i+4];
}
if ((LFSR[0] | LFSR[1] | LFSR[2] | LFSR[3]) == 0)
{
for (i = 0; i < 4; i++)
LFSR[i] = RANDSSD[i+3];
}
for (i = 0; i < 8; i++)
Register[i] = A_key[i];
Register[8] = AAV;
for (i = 9; i < 12; i++)
Register[i] = RANDSSD[i-9];
for (i = 12; i < 16; i++)
Register[i] = ESN[i-12];
offset_1 = offset_2 = 128;
CAVE(8, &offset_1, &offset_2);
for (i = 0; i < 8; i++)
{
SSD_A_NEW[i] = Register[i];
SSD_B_NEW[i] = Register[i+8];
}
}
/* SSD_Update has the same header as CAVE (see Exhibit 2-4) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -