int singleNumber2(int A[], int n) { if (n == 1) return A[0]; int ones = 0 ,twos = 0; for (int i =0 ; i < n ; i ++) { ones = (A[i] ^ ones) & ~twos ; twos = (A[i] ^ twos ) & ~ones; printf(" A: "); printHexString( A[i]); printf("ones: "); printHexString( ones); printf("twos: "); printHexString( twos); //printHexString( ones | twos); printf("next\n"); } return ones | twos ; }
byte PN532::InListPassiveTarget(const byte maxtg, const byte brty, byte * data, const byte length) { // byte inidatalen = 0; packet[0] = COMMAND_InListPassiveTarget; packet[1] = maxtg; // max 1 cards at once (we can set this to 2 later) packet[2] = brty; if (length > 0) { memcpy(packet + 3, data, length); } #ifdef PN532DEBUG Serial.print("InListPassiveTarget << "); printHexString(packet, length + 3); Serial.println(); #endif sendpacket(3 + length); last_command = COMMAND_InListPassiveTarget; comm_status = COMMAND_ISSUED; if (!checkACKframe()) { comm_status = ACK_NOT_RECEIVED; return 0; } #ifdef PN532DEBUG Serial.println("ACKed."); #endif comm_status = ACK_FRAME_RECEIVED; return 1; }
boolean PN532::InCommunicateThru(const byte * data, const byte len) { packet[0] = COMMAND_InCommunicateThru; packet[1] = len + 1; memcpy(packet + 2, data, len); #ifdef FELICADEBUG Serial.print("Thru: "); Serial.print("count = "); Serial.print(len + 1, DEC); Serial.print(" "); printHexString(packet, len + 2); Serial.println(); #endif /* Send the command */ sendpacket(len + 2); comm_status = COMMAND_ISSUED; last_command = COMMAND_InCommunicateThru; if (!checkACKframe()) { comm_status = ACK_NOT_RECEIVED; return 0; } comm_status = ACK_FRAME_RECEIVED; return 1; }
byte PN532::mifare_ReadDataBlock(uint8_t blockNumber, uint8_t * data) { #ifdef MIFAREDEBUG Serial.print("Try to read 16 bytes from block "); Serial.println(blockNumber); #endif // Access the Target 1 after polling by InList or AutoPoll // Authentication must be succeeded so the uid (and its length) is // stored in target.UID /* Send the command */ if (!InDataExchange(1, MIFARE_CMD_READ, blockNumber, packet, 0)) { #ifdef MIFAREDEBUG Serial.println("Failed to receive ACK for read command"); #endif } byte c = getCommandResponse(packet); if (!c ) { #ifdef MIFAREDEBUG Serial.println("Unexpected response"); printHexString(packet, 26); Serial.println(); #endif return 0; } //#define MIFAREDEBUG #ifdef MIFAREDEBUG Serial.print("Packet "); printHexString(packet, c); Serial.println(); #endif if (packet[0] != 0) { // error. return 0; } memcpy(data, packet + 1, 16); #ifdef MIFAREDEBUG Serial.print("data "); Serial.println(blockNumber); printHexString(data, 16); Serial.println(); #endif //#undef MIFAREDEBUG return 16; }
byte PN532::receivepacket() { chksum = 0; byte i; byte n = 0; wire.requestFrom((int) i2c_addr, BUFFER_LENGTH); receive(); for (i = 0; i < 6; i++) { packet[i] = receive(); } // preamble if (memcmp(packet, "\x00\x00\xff", 3) != 0 || ((packet[3] + packet[4]) & 0xff != 0)) { #ifdef PN532DEBUG Serial.println("received illigale preamble."); #endif comm_status = WRONG_PREAMBLE; return 0; } n = packet[3]; if (((packet[3] + packet[4]) & 0xff) != 0x00) { comm_status = WRONG_PREAMBLE; return 0; } // wire.requestFrom((int) i2c_addr, (int) n); for (; i < n + 5 + 2; i++) { packet[i] = receive(); } #ifdef PN532DEBUG Serial.print("recieve packet Len = "); Serial.print(n, DEC); Serial.print(", "); printHexString(packet, n + 7); Serial.println(); Serial.print("xsum: "); Serial.print(chksum, HEX); Serial.println(); #endif if (chksum != 0) { comm_status = CHECKSUMERROR; #ifdef PN532DEBUG Serial.println("!!! Checksum error !!!"); return 0; #endif } if (packet[6 + n] != 0) { #ifdef PN532DEBUG Serial.println("termination 0x00 error"); #endif comm_status = WRONG_POSTAMBLE; return 0; } memmove(packet, packet + 5, n); return n; }
int main(int argc, const char * argv[]) { printHexString( 8 ); printHexString( 1 ); int test[] = {1,2,3,3,2,1,4,4,4,4,6,5,6,5}; int r = singleNumber( test , (int) (sizeof(test)/sizeof(test[0]) ) ); cout<<"result: "<<r<<endl; // int test2[] = {3,12,12,12,3,4,4,4,8,8,8}; // int test2[] = { -2, -2, -4, -2 }; int test2[] = { 2, 2, 4, 2 }; int r2 = singleNumber2( test2 , (int) (sizeof(test2)/sizeof(test2[0]) ) ); cout<<"result: "<<r2<<endl; return 0; }
void keyConvert(char key[], int roundNum) { char left[28]; char right[28]; char compressed[48]; int i = 0; //left if (roundNum == 1 || roundNum == 2 || roundNum == 9 || roundNum == 16) { //shift left for (i = 0; i < 27; i++) { left[i] = key[i + 1]; right[i] = key[i + 29]; } left[27] = key[0]; right[27] = key[28]; } else { //shift left for (i = 0; i < 26; i++) { left[i] = key[i + 2]; right[i] = key[i + 30]; } left[26] = key[0]; left[27] = key[1]; right[26] = key[28]; right[27] = key[29]; //key change } //key change for (i = 0; i < 27; i++) { key[i] = left[i]; key[i + 28] = right[i]; } //key compression for (i = 0; i < 48; i++) compressed[i] = key[compression_table[i] - 1]; printf("Round%d Key:", roundNum); printHexString(compressed, 12); }
BOOL TEST_DMA_READ_QUERY_MEMORY(PCIE_HANDLE hPCIe){ BOOL bPass=TRUE; //const int nTestSize = MEM_SIZE; const PCIE_LOCAL_ADDRESS LocalAddr = DEMO_PCIE_MEM_QUERY_ADDR; // read back test pattern and verify if (bPass){ bPass = PCIE_DmaRead(hPCIe, LocalAddr, outQueryByte, MAX_QUERY_SIZE_BYTE); if (!bPass){ printf("06:DMA Memory:PCIE_DmaRead failed\r\n"); }else{ printHexString(outQueryByte); } } return bPass; }
void finalPer(char text[]) { int i; char temp[64]; char num; //initial Permutation on temperary array for (i = 0; i < 64; i++) { temp[i] = text[final_permutation[i] - 1]; } //copy data for (i = 0; i < 64; i++) { text[i] = temp[i]; } //print check printf("Final Permutation:"); printHexString(text, 16); }
byte PN532::felica_Polling(byte * resp, const word syscode) { // Polling command, with system code request. resp[0] = FELICA_CMD_POLLING; resp[1] = syscode & 0xff; resp[2] = syscode >> 8 & 0xff; resp[3] = 0x01; // request code: request sys code resp[4] = 0; // time slot # byte result = InCommunicateThru(resp, 5); result = getCommunicateThruResponse(resp); printHexString(resp, result); if (resp[0] == FELICA_CMD_POLLING + 1) { target.IDLength = 8; memcpy(target.IDm, resp + 1, target.IDLength); target.NFCType = Type_FeliCa212kb; return result; } target.NFCType = Type_Empty; target.IDLength = 0; return 0; }
byte PN532::InDataExchange(const byte Tg, const byte * data, const byte length) { // Prepare the authentication command // packet[0] = COMMAND_InDataExchange; /* Data Exchange Header */ packet[1] = Tg; /* target number */ memcpy(packet + 2, data, length); #ifdef MIFAREDEBUG printHexString(packet, length + 2); Serial.println(); #endif sendpacket(length + 2); comm_status = COMMAND_ISSUED; last_command = COMMAND_InDataExchange; if (!(checkACKframe())) { comm_status = ACK_NOT_RECEIVED; return 0; } comm_status = ACK_FRAME_RECEIVED; return 1; }
byte PN532::getCommunicateThruResponse(byte * data) { // InCommunicateThru(data, len); /* Read the response packet */ int count = getCommandResponse(packet); if (!count ) { return 0; } #ifdef FELICADEBUG Serial.print("Response: "); Serial.print("count = "); Serial.print(count, DEC); Serial.print(" "); printHexString(packet, count); Serial.println(); #endif if (packet[0] != 0) { return 0; } count = packet[1] - 1; memcpy(data, packet + 2, count); return count; }
BOOL TEST_DMA_READ_DATABASE_MEMORY(PCIE_HANDLE hPCIe){ BOOL bPass=TRUE; strcpy(databaseblock,""); const PCIE_LOCAL_ADDRESS LocalAddr = DEMO_PCIE_MEM_SUBJECT_ADDR; // read back test pattern and verify if (bPass){ bPass = PCIE_DmaRead(hPCIe, LocalAddr, databaseblock, DATABASE_BLOCK_SIZE); if (!bPass){ printf("06:DMA Memory:PCIE_DmaRead failed\r\n"); }else{ printHexString(outQueryByte); } } return bPass; }
BOOL TEST_DMA_WRITE_QUERY_MEMORY(PCIE_HANDLE hPCIe){ BOOL bPass=TRUE; const PCIE_LOCAL_ADDRESS LocalAddr = DEMO_PCIE_MEM_QUERY_ADDR; printf("Input your query (maximum %d):", MAX_QUERY_SIZE_CHAR); scanf("%s",inQueryADN); CovertQuery2Bit(inQueryADN, MAX_QUERY_SIZE_CHAR, inQueryByte, MAX_QUERY_SIZE_BYTE); printHexString(inQueryByte); // write test pattern if (bPass){ bPass = PCIE_DmaWrite(hPCIe, LocalAddr, inQueryByte, MAX_QUERY_SIZE_BYTE); if (!bPass) printf("05:DMA Memory:PCIE_DmaWrite failed\r\n"); } // read back test pattern and verify /* if (bPass){ bPass = PCIE_DmaRead(hPCIe, LocalAddr, outQueryByte, MAX_QUERY_SIZE_BYTE); if (!bPass){ printf("06:DMA Memory:PCIE_DmaRead failed\r\n"); }else{ for(i=0; i<MAX_QUERY_SIZE_BYTE && bPass; i++){ if (outQueryByte[i] != inQueryByte[i]){ bPass = FALSE; printf("07:DMA Memory:Read-back verify unmatch, index = %d, read=%xh, expected=%xh\r\n", i,outQueryByte[i], inQueryByte[i]); } } } } */ return bPass; }
// InDataExchange for Mifare byte PN532::InDataExchange(const byte Tg, const byte micmd, const byte blkaddr, const byte * data, const byte datalen) { // Prepare a Mifare command // packet[0] = COMMAND_InDataExchange; /* Data Exchange Header */ packet[1] = Tg; /* target number */ packet[2] = micmd; packet[3] = blkaddr; memcpy(packet + 4, data, datalen); #ifdef MIFAREDEBUG Serial.print("Sending in InDataExchange: "); printHexString(packet, datalen + 4); Serial.println(); #endif sendpacket(datalen + 4); comm_status = COMMAND_ISSUED; last_command = COMMAND_InDataExchange; if (!(checkACKframe())) { comm_status = ACK_NOT_RECEIVED; return 0; } comm_status = ACK_FRAME_RECEIVED; return 1; }
byte PN532::getCommandResponse(byte * resp, const long & wmillis) { if (!IRQ_wait(wmillis)) return 0; comm_status = REQUEST_RECEIVE; byte count = receivepacket(); #ifdef PN532COMM Serial.print("com resp. >> "); printHexString(packet, count); Serial.println(); #endif //#undef PN532DEBUG if (!(packet[0] == 0xd5 && packet[1] == (last_command + 1))) { #ifdef PN532DEBUG Serial.println("Missmatch with the last command."); #endif comm_status = RESP_COMMAND_MISSMATCH; return 0; } comm_status = RESP_RECEIVED; count -= 2; memcpy(resp, packet + 2, count); return count; }
byte PN532::InAutoPoll(const byte pollnr, const byte period, const byte * types, const byte typeslen) { byte N = min(max(1, typeslen), 15); packet[0] = COMMAND_InAutoPoll; packet[1] = pollnr; // no. of polls packet[2] = period; // no. of 150 ms period // the first type N = 1 mandatory memcpy(packet + 3, types, N); #ifdef PN532DEBUG Serial.println("InAutoPoll sending"); printHexString(packet, typeslen + 3); Serial.println(); #endif last_command = COMMAND_InAutoPoll; sendpacket(3 + N); comm_status = COMMAND_ISSUED; if (!checkACKframe()) return 0; comm_status = ACK_FRAME_RECEIVED; #ifdef PN532DEBUG Serial.println("InAutoPoll ACKed."); #endif return 1; }
BOOL TEST_LOAD_DATABASE(FILE *ptr_file) { BOOL number_flag = TRUE; char buf[DATABASE_LINE_SIZE]; char *database_temp; int bytecounter = 0; BOOL found = FALSE; strcpy(databaseblock, ""); while (!full_buffer) //while (fgets(buf, 1000, ptr_file) != NULL) { if(bytecounter + DATABASE_LINE_SIZE > DATABASE_BLOCK_SIZE) { full_buffer = TRUE; bytecounter = 0; } else { //printf("%s", buf); if(fgets(buf, DATABASE_LINE_SIZE, ptr_file) == NULL){ printf("07: Cant read file\n"); return FALSE; } linecounter++; if (found) { if (strncmp(buf, endMakerdata, EndMarkersize) == 0) { //printf("[Binh]%s", buf); found = FALSE; } else { /* get the first token */ database_temp = strtok(buf, delimiter); number_flag = TRUE; /* walk through other tokens */ strcpy(database_line, ""); while (database_temp != NULL){ if (number_flag == FALSE){ //printf("%s", database_temp); strcat(database_line, database_temp); } else { number_flag = FALSE; } database_temp = strtok(NULL, delimiter); } //printf(database_line); CovertQuery2Bit(database_line, strlen(database_line), database_convert_byte, strlen(database_line) / 3 + 1); //printHexString(database_convert_byte); strncat(databaseblock,database_convert_byte,strlen(database_line)); bytecounter += strlen(database_line); } } else if (strncmp(buf, startMakerdata, StartMarkersize) == 0){ //printf("[Binh] %s", buf); found = TRUE; } } } printHexString(databaseblock); full_buffer = FALSE; //reset for next reading printf("#line: %d", linecounter); return TRUE; }
void SessionParser::parse(const boost::uint8_t* p_data, boost::uint16_t p_length, boost::uint32_t p_srcAddr) { switch(m_state) { case k_none: if (p_length > 13 && memcmp(p_data, "POST /jsproxy", 13) == 0) { std::cout << "[+] Initial request found." << std::endl; m_state = k_request; } break; case k_request: if (p_length > 17 && memcmp(p_data, "HTTP/1.1 200 OK\r\n", 17) == 0) { std::cout << "[+] Server challenge received." << std::endl; m_serverAddress = p_srcAddr; std::string packet; packet.assign(reinterpret_cast<const char*>(p_data), p_length); if (moveToPayload(packet) && packet.length() > 32) { std::size_t index = 0; for (std::size_t i = 0; i < 24 && index < packet.length(); i++) { if (i < 8) { codePointAt(packet, index); } else { m_rchallenge.push_back(codePointAt(packet, index)); } } } m_state = k_challenge; } break; case k_challenge: { if (p_length < 13 || memcmp(p_data, "POST /jsproxy", 13) != 0) { break; } std::cout << "[+] Challenge response found." << std::endl; m_state = k_done; std::string packet; packet.assign(reinterpret_cast<const char*>(p_data), p_length); if (!moveToPayload(packet)) { std::cerr << "Failed to find the payload." << std::endl; break; } std::size_t index = 0; for (std::size_t i = 0; i < 26; i++) { codePointAt(packet, index); } m_lchallenge.assign(packet.data() + index, 16); index += 16; for (std::size_t i = 0; i < 8; i++) { codePointAt(packet, index); } for (std::size_t i = 0; i < 8 && index < packet.length(); i++) { m_response1.push_back(codePointAt(packet, index)); } for (std::size_t i = 0; i < 8 && index < packet.length(); i++) { m_response2.push_back(codePointAt(packet, index)); } for (std::size_t i = 0; i < 8 && index < packet.length(); i++) { m_response3.push_back(codePointAt(packet, index)); } m_response.assign(m_response1); m_response.append(m_response2); m_response.append(m_response3); m_username.assign(packet.data() + index, packet.length() - index); std::cout << "Username: "******"Failed to recover key3!" << std::endl; return; } std::cout << "DES Key 3: "; printHexString(key3, true); // guess des key 2 std::string key2; if (!getKey2(key2, m_response2, challengeHash)) { std::cerr << "Failed to recover key2!" << std::endl; return; } std::cout << "DES Key 2: "; printHexString(key2, true); // guess des key 1 std::string key1; if (!getKey1(key1, m_response1, challengeHash)) { std::cerr << "Failed to recover key1!" << std::endl; return; } std::cout << "DES Key 1: "; printHexString(key1, true); // retrieve the pwdhash from our 3 keys std::string pwdHash(makePwdHash(key1)); pwdHash.append(makePwdHash(key2)); pwdHash.append(makePwdHash(key3)); pwdHash.resize(16); std::cout << "Password SHA-1: "; printHexString(pwdHash); // create the pwd hash hash for master key creation std::string pwdHashHash(MD4::md4(pwdHash)); std::cout << "SHA-1(Password SHA-1): "; printHexString(pwdHashHash); // create the master key std::string masterKey(pwdHashHash); masterKey.append(m_response); masterKey.append("This is the MPPE Master Key"); unsigned char sharesult[20] = { 0 }; sha1::calc(masterKey.data(), masterKey.size(), sharesult); masterKey.assign((char*)sharesult, 16); std::cout<< "Master Key: "; printHexString(masterKey); } break; case k_challenge_response: case k_decrypt: case k_done: default: break; } }