307 lines
5.9 KiB
C
307 lines
5.9 KiB
C
|
|
|
|||
|
|
#include "password.h"
|
|||
|
|
#include "crc8.h"
|
|||
|
|
|
|||
|
|
|
|||
|
|
//-------------------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>봮<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
#define CHAR_TAB_LEN 32
|
|||
|
|
#define CHAR_BITS 5
|
|||
|
|
|
|||
|
|
#define PSWD_BIT_LEN (PSWDSTR_LEN*CHAR_BITS)
|
|||
|
|
#define PSWD_BYTE_LEN ((PSWD_BIT_LEN+CHAR_BITS-1)/8)
|
|||
|
|
|
|||
|
|
#define CHAR32MASK 0x1F
|
|||
|
|
|
|||
|
|
#define XOR_VAL 0x59
|
|||
|
|
|
|||
|
|
|
|||
|
|
static const char g_charTable[CHAR_TAB_LEN] =
|
|||
|
|
{
|
|||
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|||
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
|
|||
|
|
'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W',
|
|||
|
|
'X', 'Y',
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
#define CLINET_NUM 3 // <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
// <20>洢<EFBFBD><E6B4A2><EFBFBD>б<EFBFBD>
|
|||
|
|
static const u8 g_saveSWTab[CLINET_NUM+1][PSWDSTR_LEN] =
|
|||
|
|
{
|
|||
|
|
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }, // Ĭ<><C4AC>
|
|||
|
|
{ 4, 9, 17, 5, 10, 14, 16, 6, 18, 3, 20, 11, 1, 13, 19, 15, 2, 7, 12, 8 }, // <20><>ӯ
|
|||
|
|
{ 13, 7, 12, 4, 6, 19, 3, 10, 17, 20, 11, 18, 15, 5, 14, 2, 8, 1, 9, 16 }, // <20>Ϲ<EFBFBD>
|
|||
|
|
{ 10, 4, 11, 8, 16, 12, 5, 9, 17, 19, 7, 14, 20, 3, 13, 1, 15, 2, 6, 18 }, // <20>ٵ<EFBFBD>
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
|
|||
|
|
//-------------------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
typedef union
|
|||
|
|
{
|
|||
|
|
u8 pswdbin[PSWD_BYTE_LEN];
|
|||
|
|
struct
|
|||
|
|
{
|
|||
|
|
u8 check; // У<><D0A3><EFBFBD><EFBFBD>
|
|||
|
|
s32 totalTime; // <20>ۼƸı<C6B8>ʱ<EFBFBD><CAB1>
|
|||
|
|
s16 thisTime; // <20><><EFBFBD>θı<CEB8>ʱ<EFBFBD><CAB1>
|
|||
|
|
u32 cpuId; // CPU ID
|
|||
|
|
u8 clientId; // <20>ͻ<EFBFBD>ID
|
|||
|
|
u8 moveBit; // <20><>λ
|
|||
|
|
} __attribute__ ((packed)) pswdstruct;
|
|||
|
|
|
|||
|
|
struct
|
|||
|
|
{
|
|||
|
|
u8 check; // У<><D0A3><EFBFBD><EFBFBD>
|
|||
|
|
u8 datbuff[12];
|
|||
|
|
} __attribute__ ((packed)) pswdckdat;
|
|||
|
|
|
|||
|
|
} Password;
|
|||
|
|
|
|||
|
|
|
|||
|
|
//-------------------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
|
|||
|
|
// <20>õ<EFBFBD>ij<EFBFBD><C4B3>λ<EFBFBD><CEBB>ֵ
|
|||
|
|
int GetBitVal(u8 * buff, int bitidx)
|
|||
|
|
{
|
|||
|
|
int i;
|
|||
|
|
u8 mod = 0x01;
|
|||
|
|
u8 num = bitidx % 8;
|
|||
|
|
buff += bitidx / 8;
|
|||
|
|
|
|||
|
|
for (i = 0; i < num; i++)
|
|||
|
|
{
|
|||
|
|
mod *= 2;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ((*buff & mod) != 0)
|
|||
|
|
{
|
|||
|
|
return 1;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>λ<EFBFBD><CEBB>ֵ
|
|||
|
|
void SetBitVal(u8 * buff, int bitidx, int bitval)
|
|||
|
|
{
|
|||
|
|
int i;
|
|||
|
|
u8 mod = 0x01;
|
|||
|
|
u8 num = bitidx % 8;
|
|||
|
|
buff += bitidx / 8;
|
|||
|
|
|
|||
|
|
for (i = 0; i < num; i++)
|
|||
|
|
{
|
|||
|
|
mod *= 2;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (bitval != 0)
|
|||
|
|
{
|
|||
|
|
*buff |= mod;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
*buff &= (~mod);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
// ѭ<><D1AD><EFBFBD><EFBFBD>λ
|
|||
|
|
// movenum > 0, <20><><EFBFBD><EFBFBD>
|
|||
|
|
// movenum < 0, <20><><EFBFBD><EFBFBD>
|
|||
|
|
void CyclicShift(u8 * buff, int bitlen, int movenum)
|
|||
|
|
{
|
|||
|
|
int mvdir;
|
|||
|
|
if (buff == 0 || bitlen <= 1 || movenum == 0)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (movenum < 0) // <20><><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
mvdir = -1;
|
|||
|
|
}
|
|||
|
|
else // <20><><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
mvdir = 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
movenum *= mvdir;
|
|||
|
|
movenum %= bitlen;
|
|||
|
|
|
|||
|
|
int bitidx = 0;
|
|||
|
|
int nbidx;
|
|||
|
|
int bitval, tmpval;
|
|||
|
|
// ȡ<><C8A1> bitidx == 0 <20><>λ
|
|||
|
|
bitval = GetBitVal(buff, bitidx);
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>λ<EFBFBD><CEBB>
|
|||
|
|
if (mvdir < 0) // <20><><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
nbidx = bitidx - movenum;
|
|||
|
|
if (nbidx < 0)
|
|||
|
|
{
|
|||
|
|
nbidx += bitlen;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else // <20><><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
nbidx = bitidx + movenum;
|
|||
|
|
if (nbidx >= bitlen)
|
|||
|
|
{
|
|||
|
|
nbidx -= bitlen;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1> nbidx ԭ<><D4AD><EFBFBD><EFBFBD>λ
|
|||
|
|
tmpval = GetBitVal(buff, nbidx);
|
|||
|
|
// <20><> bitidx <20><>λд<CEBB>뵽 nbidx <20><>λ<EFBFBD><CEBB>
|
|||
|
|
SetBitVal(buff, nbidx, bitval);
|
|||
|
|
bitidx = nbidx;
|
|||
|
|
bitval = tmpval;
|
|||
|
|
}while(bitidx != 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//-------------------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
|
int DecodePswdString(PswdCtrl * pCtrl, char * pswdStr, u8 clientId)
|
|||
|
|
{
|
|||
|
|
char pslist[PSWDSTR_LEN]; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 32 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>
|
|||
|
|
Password pswd;
|
|||
|
|
u8 moveBit;
|
|||
|
|
|
|||
|
|
if (pCtrl == 0 || pswdStr == 0)
|
|||
|
|
{
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
memset(pCtrl, 0, sizeof(PswdCtrl));
|
|||
|
|
|
|||
|
|
if (clientId <= 0 || clientId > CLINET_NUM)
|
|||
|
|
{
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 1. <20>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>봮<EFBFBD><EFBFBD><F3A3ACB0>տͻ<D5BF><CDBB><EFBFBD><EFBFBD>з<EFBFBD><D0B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>е<EFBFBD>32<33><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
|
|||
|
|
{
|
|||
|
|
int i, idx;
|
|||
|
|
for (i = 0; i < PSWDSTR_LEN; i++)
|
|||
|
|
{
|
|||
|
|
idx = g_saveSWTab[clientId][i] - 1;
|
|||
|
|
pslist[i] = pswdStr[idx];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 2. <20><><EFBFBD><EFBFBD>32<33><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD>õ<EFBFBD>100λ<30>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
int i, j;
|
|||
|
|
u8 buff32[PSWDSTR_LEN]; // 32<33><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĶ<DDB5><C4B6><EFBFBD><EFBFBD>Ʊ<EFBFBD>ʾ
|
|||
|
|
for (i = 0; i < PSWDSTR_LEN; i++)
|
|||
|
|
{
|
|||
|
|
char ch32 = pslist[i];
|
|||
|
|
for (j = 0; j < CHAR_TAB_LEN; j++)
|
|||
|
|
{
|
|||
|
|
if (ch32 == g_charTable[j])
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (j >= CHAR_TAB_LEN)
|
|||
|
|
{
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
buff32[i] = j;
|
|||
|
|
}
|
|||
|
|
memset(&pswd, 0, sizeof(Password));
|
|||
|
|
for (i = PSWDSTR_LEN-1; i >= 0; i--)
|
|||
|
|
{
|
|||
|
|
u8 bin = buff32[i];
|
|||
|
|
bin &= CHAR32MASK;
|
|||
|
|
|
|||
|
|
for (j = PSWD_BYTE_LEN-1; j > 0; j--) // <20><>λ 5 λ
|
|||
|
|
{
|
|||
|
|
pswd.pswdbin[j] <<= CHAR_BITS;
|
|||
|
|
pswd.pswdbin[j] |= (pswd.pswdbin[j-1] >> (8-CHAR_BITS));
|
|||
|
|
}
|
|||
|
|
pswd.pswdbin[0] <<= CHAR_BITS;
|
|||
|
|
pswd.pswdbin[0] |= bin;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 3.<2E>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
int i;
|
|||
|
|
for (i = 0; i < PSWD_BYTE_LEN; i++)
|
|||
|
|
{
|
|||
|
|
pswd.pswdbin[i] ^= XOR_VAL;
|
|||
|
|
}
|
|||
|
|
pswd.pswdbin[PSWD_BYTE_LEN-1] &= 0x0f;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><F2A3ACB7><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ݶκõ<F3A3ACB5><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ݶΡ<DDB6>
|
|||
|
|
{
|
|||
|
|
int movenum, idx;
|
|||
|
|
pswd.pswdstruct.moveBit &= 0x0f; // <20><>λ<EFBFBD><CEBB><EFBFBD>ݶ<EFBFBD>
|
|||
|
|
moveBit = pswd.pswdstruct.moveBit; // <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
movenum = g_saveSWTab[clientId][moveBit]; // <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
|
|||
|
|
idx = 0x10;
|
|||
|
|
|
|||
|
|
while (idx < PSWDSTR_LEN && (movenum == 5 || movenum == 10 || movenum == 15 || movenum == 20))
|
|||
|
|
{
|
|||
|
|
movenum = g_saveSWTab[clientId][idx];
|
|||
|
|
idx++;
|
|||
|
|
}
|
|||
|
|
if (idx == PSWDSTR_LEN || (movenum == 5 || movenum == 10 || movenum == 15 || movenum == 20) || movenum <= 0 || movenum > PSWDSTR_LEN)
|
|||
|
|
{
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ((moveBit & 0x01) != 0) // <20><><EFBFBD>ܣ<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD>ƶ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
movenum *= -1;
|
|||
|
|
}
|
|||
|
|
CyclicShift(pswd.pswdbin, PSWD_BIT_LEN-4, movenum);
|
|||
|
|
}
|
|||
|
|
// 5. <20><><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ݶε<DDB6><CEB5><EFBFBD>ȷ<EFBFBD>ԡ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
u8 crc = CalcCrc8(pswd.pswdckdat.datbuff, sizeof(Password)-1);
|
|||
|
|
if (crc != pswd.pswdstruct.check)
|
|||
|
|
{
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
pCtrl->clientId = pswd.pswdstruct.clientId;
|
|||
|
|
pCtrl->cpuId = pswd.pswdstruct.cpuId;
|
|||
|
|
pCtrl->thishours = pswd.pswdstruct.thisTime / TIME_MUTI;
|
|||
|
|
pCtrl->totalhours = pswd.pswdstruct.totalTime / TIME_MUTI;
|
|||
|
|
|
|||
|
|
pCtrl->nexttotaltime = (pCtrl->thishours+pCtrl->totalhours)*TIME_MUTI + (int)(pswd.pswdstruct.thisTime+pswd.pswdstruct.totalTime)%TIME_MUTI;
|
|||
|
|
pCtrl->totaltime = pswd.pswdstruct.totalTime;
|
|||
|
|
pCtrl->thistime = pswd.pswdstruct.thisTime;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/*
|
|||
|
|
6.<EFBFBD><EFBFBD>֤<EFBFBD>ͻ<EFBFBD>ʶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>ǰ<EFBFBD>豸<EFBFBD><EFBFBD>
|
|||
|
|
7.<EFBFBD><EFBFBD>֤<EFBFBD>ϴμ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۼ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>Ͱ忨<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>£<EFBFBD><EFBFBD><EFBFBD>Сʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
8.<EFBFBD><EFBFBD><EFBFBD>µĸı<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>䣬<EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
9.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>봮<EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
*/
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|