//--------------------------------------------------------------------------- #include #include #include #include #include #include #pragma hdrstop #include "ProtectCard.h" #include "Define.h" #include "Des.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "W:\\TpwTest\\hasp32b.obj" 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; } //--------------------------------------------------------------------------- //#ifndef LOCK_CRYPKEY #ifdef LOCK_IFC //--------------------------------------------------------------------------- static Byte __stdcall InportByte(Word port) { 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) { 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 } } //--------------------------------------------------------------------------- #ifdef ACADEMY //--------------------------------------------------------------------------- #define OK 0x4F4B static Word CPort = 0xFEE0; //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- bool __fastcall TProtectCard::status_chk(Byte d) { int i, lp = 10000; for (i=0; i>= 2; if (status_chk(1)==false) goto fail; for (j=0; j>= 2; if (status_chk(1)==false) goto fail; } OutportByte(CPort, ((dt&3)|0x4)<<4); OutportByte(CPort, (((dt&3)|9)&0xb)<<4); if (status_chk(2)==false) goto fail; OutportByte(CPort, 0xf0); return true; fail: OutportByte(CPort, 0xf0); return false; } //--------------------------------------------------------------------------- int __fastcall TProtectCard::receive() { int i, j, d, dt, lp = 10000; for (i=j=dt=0; i=0) { pw = d; return true; } } return false; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::GetData(Word &pw) { if (send(4, 0x64)) { int d = receive(); if (d>=0) { pw = d; return true; } } return false; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::PutData(Word d) { return send(12, 0x440000+d) && receive()==OK; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::rand16(Byte lp, Word &seed) { int i, j, k; if (password(0, seed)) { for (k=0; k>9)^seed)>>3; j = ((seed>>1)^seed)>>14; seed <<= 1; seed |= ((i^j)&1); } return true; } return false; } //--------------------------------------------------------------------------- // Public Function //--------------------------------------------------------------------------- bool __fastcall TProtectCard::Init() { if (!checkTime()) return false; int j = (CPort>>2)&0xfc; if (send(12, 0x410000+(j<<8)+j) && receive()==OK) { return WritePassword(); } return false; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::SerialNO(Cardinal &no) { if (send(4, 0x4E)) { int d = receive(); if (d>=0) { no = d; return true; } } return false; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::Information(Word &infor) { if (send(4, 0x4D)) { int d = receive(); if (d>=0) { infor = d; return true; } } return false; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::ReadPassword() { Word lp, pw1, pw2; if (GetData(lp)) { if (rand16(lp, pw2)) { if (password(lp, pw1)) { if (pw1==pw2) return true; } } } return false; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::WritePassword() { Word seed = rand(); if (PutData(seed&0x0F)) { return send(12, 0x530000+seed) && receive()==OK; } return false; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::CheckPassword() { if (ReadPassword()) { if (WritePassword()) return true; } return false; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::SetTimer(Byte msec) { return send(8, 0x5700+msec) && receive()==OK; } //--------------------------------------------------------------------------- bool __fastcall TProtectCard::GetTimer(Byte &msec) { if (send(4, 0x52)) { int d = receive(); if (d>=0) { msec = d; return true; } } return false; } //--------------------------------------------------------------------------- #else // ACADEMY //--------------------------------------------------------------------------- #define MaxIFCVersion 8 static Word CPort; static Word CPassL, CPassH; static Byte vBoard; static Byte buf; //--------------------------------------------------------------------------- // 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) { 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; } } //--------------------------------------------------------------------------- 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); } //--------------------------------------------------------------------------- #endif // ACADEMY #else // LOCK_IFC //--------------------------------------------------------------------------- #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; /////////////////////////////////////////////////////////////////////////////// // ¹öÀü üũ¸¦ ÇÑ´Ù. by playzzang p1 = 2; /* Address */ p2 = 0; hasp(READ_MEMO, 0, 0, Pass1, Pass2, &p1, &p2, &p3, &p4); if (p3!=0) return -4; if (p2!=HASPTexProVersion1) return -6; p1 = 3; /* Address */ p2 = 0; hasp(READ_MEMO, 0, 0, Pass1, Pass2, &p1, &p2, &p3, &p4); if (p3!=0) return -4; if (p2!=HASPTexProVersion2) return -6; //////////////////////////////////////////////////////////////////////////////// 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()%13; p1 = pwIndex*4; p2 = 4; p4 = int(pwData+pwIndex*8); hasp(READ_MEMO_BLOCK, SeedCode, 0, Pass1, Pass2, &p1, &p2, &p3, &p4); } } //--------------------------------------------------------------------------- void __fastcall TProtectCard::Check() { Byte *pw, blk[8]; 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 // LOCK_IFC //#endif // defined not crypkey.....