//--------------------------------------------------------------------------- #include #include #include #include #include #include #pragma hdrstop #include "ProtectCard.h" #include "Define.h" #include "Des.h" #include "Pcilib.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "W:\\TpwTest\\hasp32b.obj" //--------------------------------------------------------------------------- #define CPORT 0xE0 // Æ÷Æ® ½ÃÀÛ ÁÖ¼Ò #define MaxIFCVersion 10 //--------------------------------------------------------------------------- TProtectCard *ProtectCard; //--------------------------------------------------------------------------- int __fastcall toJulian() { SYSTEMTIME st; int jyear, jmonth, jul, greg, curDay, ja; GetLocalTime(&st); jyear = st.wYear; jmonth = st.wMonth; if (st.wYear < 0) jyear++; if (st.wMonth > 2) jmonth++; else { jyear--; jmonth += 13; } jul = 365.25*jyear + 30.6001*jmonth + st.wDay + 1720995.0; greg = 15 + 31*(10+12*1582); curDay = st.wDay + 31*(st.wMonth +12*st.wYear); if (curDay >= greg) { ja = 0.01*jyear; jul += 2-ja+(0.25*ja); } return jul; } //--------------------------------------------------------------------------- static bool __fastcall checkTime() { unsigned char blk[8], k[8] = {'^', '*', 'F', 'S', '0', '>', '?', '['}; int nowJul, pastJul, due, sumDay; TRegIniFile *RegIniFile; AnsiString pw; int timePass[4]; RegIniFile = new TRegIniFile(RegFilename); if (RegIniFile) { pw = RegIniFile->ReadString("Common", "Product No.", ""); if (pw == "") goto fail; sscanf(pw.c_str(), "%d-%d-%d-%d", &timePass[0], &timePass[1], &timePass[2], &timePass[3]); delete RegIniFile; } else return false; blk[0] = timePass[0] & 0xFF; blk[1] = (timePass[0] >> 8) & 0xFF; blk[2] = timePass[1] & 0xFF; blk[3] = (timePass[1] >> 8) & 0xFF; blk[4] = timePass[2] & 0xFF; blk[5] = (timePass[2] >> 8) & 0xFF; blk[6] = timePass[3] & 0xFF; blk[7] = (timePass[3] >> 8) & 0xFF; decrypt(k, blk); sumDay = blk[0]; sumDay |= blk[1] << 8; sumDay |= blk[2] << 16; sumDay |= blk[3] << 24; pastJul = sumDay; sumDay = blk[4]; sumDay |= blk[5] << 8; sumDay |= blk[6] << 16; sumDay |= blk[7] << 24; due = sumDay; nowJul = toJulian(); if (nowJul >= pastJul) { if (nowJul < (pastJul + due)) return true; else return false; } else return false; fail : delete RegIniFile; return true; } //--------------------------------------------------------------------------- #ifdef LOCK_IFC static Byte __stdcall InportByte(Word port) { if(GetDeviceOK()){ Byte cc; AtWriteB(iatpci.cs[ATPCI_CS3]+0,port); // port´Â CPORTºÎÅÍ ½ÃÀÛÇÑ´Ù //(Byte)portÇØµµ µÈ´Ù AtWriteB(iatpci.cs[ATPCI_CS3]+4,0x10); AtWriteB(iatpci.cs[ATPCI_CS3]+4,0x14); AtWriteB(iatpci.cs[ATPCI_CS3]+4,0x1D); cc = AtReadB(iatpci.cs[ATPCI_CS3]+8); AtWriteB(iatpci.cs[ATPCI_CS3]+4,0x00); return cc; } else { asm { mov dx, port xor ax, ax in al, dx } } } //--------------------------------------------------------------------------- static Word __stdcall InportWord(Word port) { asm { mov dx, port in ax, dx } } //--------------------------------------------------------------------------- static void __stdcall OutportByte(Word port, Byte data) { if(GetDeviceOK()){ AtWriteB(iatpci.cs[ATPCI_CS3]+0,port); // port´Â CPORTºÎÅÍ ½ÃÀÛÇÑ´Ù //(Byte)portÇØµµ µÈ´Ù AtWriteB(iatpci.cs[ATPCI_CS3]+4,0x10); AtWriteB(iatpci.cs[ATPCI_CS3]+4,0x14); AtWriteB(iatpci.cs[ATPCI_CS3]+4,0x16); AtWriteB(iatpci.cs[ATPCI_CS3]+0,data); AtWriteB(iatpci.cs[ATPCI_CS3]+4,0x10); AtWriteB(iatpci.cs[ATPCI_CS3]+4,0x00); } else { asm { mov dx, port mov al, data out dx, al } } } //--------------------------------------------------------------------------- static void __stdcall OutportWord(Word port, Word data) { asm { mov dx, port mov ax, data out dx, ax } } //--------------------------------------------------------------------------- static Word CPort; static Word CPassL, CPassH; static Byte vBoard; static Byte buf; //--------------------------------------------------------------------------- __fastcall TProtectCard::TProtectCard() { AppStart(); } //--------------------------------------------------------------------------- __fastcall TProtectCard::~TProtectCard() { AppEnd(); } //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- bool __fastcall TProtectCard::Check(int id) { int t; clock_t c; OutportByte(CPort, id); c = clock(); t = 25; SetTimer(t); while (1) { if (GetTimer()==t) t = t-1; if (t==0) break; if (clock()-c>=5) break; } return t<24; } //--------------------------------------------------------------------------- // Public Function //--------------------------------------------------------------------------- int __fastcall TProtectCard::Init(AnsiString rfn) { if(GetDeviceOK()){ if (!checkTime()) return 0; CPort = CPORT; vBoard = InportByte(CPort+0x0F); if (vBoard<10 || vBoard>MaxIFCVersion) { // 10Àº PCI ¹öÀüÀÇ ÃÖ¼Ò°ª return 0; } else { OutportByte(CPort, 0);// 0 == id // CPassL = CPort+0x110; // CPassH = CPort+0x111; CPassL = CPort+0x10; CPassH = CPort+0x11; WritePassword(); return InportByte(CPort+0x0A)&0xFF; //0x7F => 0xFF //vBoard 10¿¡¼­ Ãß°¡µÈ TexTricot´Â 0x80 } } else { try { TRegIniFile *RegIniFile; int id; if (!checkTime()) return 0; CPort = 0xFDE0; RegIniFile = new TRegIniFile(rfn); if (RegIniFile) { id = RegIniFile->ReadInteger("Device", "IFCid", 0); delete RegIniFile; } else id = 0; if (id>1) { switch (id) { case 1: CPort = 0xFEE0; break; case 2: CPort = 0xFE00; break; } if (Check(id)==false) { CPort = 0xFDE0; id = 0; if (Check(id)==false) { return 0; } } } vBoard = InportByte(CPort+0x0F); if (vBoard<8 || vBoard>MaxIFCVersion) return 0; else { RegIniFile = new TRegIniFile(rfn); if (RegIniFile) { id = RegIniFile->ReadInteger("Device", "IFCid", 0); delete RegIniFile; } else id = 0; if (id>2) id = 0; switch (id) { case 1: CPort = 0xFEE0; break; case 2: CPort = 0xFE00; break; } OutportByte(CPort, id); if (CPort==0xFDE0) { CPassL = CPort+0x110; CPassH = CPort+0x111; } else { CPassL = CPort+0x10; CPassH = CPort+0x11; } WritePassword(); return InportByte(CPort+0xA)&0x7F; } } catch(...) { return 0; } } } //--------------------------------------------------------------------------- int __fastcall TProtectCard::Version() { return vBoard; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::ReadPassword() { Word Rand; int i, c1, c2; buf = InportByte(CPassL); Rand = 0x4500+buf; if (vBoard<4) { OutportByte(CPassL, 0); buf = InportByte(CPassH); return buf==0x55; } else { while (1) { buf = InportByte(CPassH); if ((buf & 0x80)==0) break; } for (i=0; i<=15; i++) { c1 = ((Rand >> 3) & 1) ^ ((Rand >> 12) & 1); c2 = ((Rand >> 14) & 1) ^ ((Rand >> 15) & 1); Rand = Rand << 1; Rand = Rand+(c1 ^ c2); } buf = InportByte(CPassH); return buf==(Rand & 0x7F); } } //--------------------------------------------------------------------------- void __fastcall TProtectCard::WritePassword() { Byte pass; if (vBoard<4) { OutportByte(CPassL, 0x55); OutportByte(CPassH, 0x55); } else { pass = 0; while (pass==0) { pass = rand()&0xFF; } OutportByte(CPassL, pass); OutportByte(CPassH, 0x80); } } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::CheckPassword() { if (ReadPassword()) { WritePassword(); return true; } return false; } //--------------------------------------------------------------------------- void __fastcall TProtectCard::SetTimer(Byte msec) { OutportByte(CPort+1, msec); } //--------------------------------------------------------------------------- Byte __fastcall TProtectCard::GetTimer() { return InportByte(CPort+1); } //--------------------------------------------------------------------------- #else // LOCK_IFC //LOCK_HASP #define IS_HASP 1 #define GET_HASP_CODE 2 #define READ_MEMO 3 #define WRITE_MEMO 4 #define GET_HASP_STATUS 5 #define GET_ID_NUM 6 #define READ_MEMO_BLOCK 50 #define WRITE_MEMO_BLOCK 51 //--------------------------------------------------------------------------- static Byte DesKey[14][13] = { " ", "eUvJ-nvh?aq9", "EmTxOX'60af#", "i'/[E15)'FU9", "5sv1Bt<_M,c!", "KF/ZPw7> 8) & 0xFF; blk[2] = pw[1] & 0xFF; blk[3] = (pw[1] >> 8) & 0xFF; blk[4] = pw[2] & 0xFF; blk[5] = (pw[2] >> 8) & 0xFF; blk[6] = pw[3] & 0xFF; blk[7] = (pw[3] >> 8)&0xFF; decrypt(k, blk); SeedCode = (blk[1] << 8) | blk[0]; Pass[0] = (blk[3] << 8) | blk[2]; Pass[1] = (blk[5] << 8) | blk[4]; Pass[2] = (blk[7] << 8) | blk[6]; hasp(IS_HASP, 0, 0, Pass1, Pass2, &p1, &p2, &p3, &p4); if (p1==0) return -2; hasp(GET_HASP_CODE, SeedCode, 0, Pass1, Pass2, &p1, &p2, &p3, &p4); if (Pass[0]!=p1 || Pass[1]!=p2 || Pass[2]!=p3) return -3; p1 = 0; /* Address */ p2 = 0; hasp(READ_MEMO, 0, 0, Pass1, Pass2, &p1, &p2, &p3, &p4); if (p3!=0) return -4; item = p2; p1 = 1; /* Address */ p2 = 0; hasp(READ_MEMO, 0, 0, Pass1, Pass2, &p1, &p2, &p3, &p4); if (p3!=0) return -5; if (p2 == 0xFFFF) p2 = 0; return (p2<<14) | item; } //--------------------------------------------------------------------------- void __fastcall TProtectCard::Read() { int p1, p2, p3, p4; if (hasp && pwIndex==0) { pwIndex = 1+rand()%7; p1 = pwIndex*4; p2 = 4; p4 = int(pwData+pwIndex*8); hasp(READ_MEMO_BLOCK, SeedCode, 0, Pass1, Pass2, &p1, &p2, &p3, &p4); if(p3!=0) isHacking = true; } } //--------------------------------------------------------------------------- void __fastcall TProtectCard::Check() { if(isHacking) return; Byte *pw, blk[8]={0}; if (hasp && pwIndex) { pw = (Byte *)malloc(112); if (pw) { memcpy(pw, pwData, 112); memcpy(blk, pw+pwIndex*8, 8); strcpy(DesKey[0], DesKey[pwIndex]); decrypt(DesKey[0], blk); isHacking = (SeedCode==((blk[1]<<8)|blk[0])) && (Pass[0]==((blk[3]<<8)|blk[2])) && (Pass[1]==((blk[5]<<8)|blk[4])) && (Pass[2]==((blk[7]<<8)|blk[6])) ? false : true; pwIndex = 0; free(pw); } } } //--------------------------------------------------------------------------- #endif