+ // *lo |= !result;
+// }
+// static void getParity37H(uint32_t *hi, uint32_t *lo){
+ // uint32_t result = 0;
+ // int i;
+
+ // // even parity
+ // for (i = 4;i >= 0;i--)
+ // result ^= (*hi >> i) & 1;
+ // for (i = 31;i >= 20;i--)
+ // result ^= (*lo >> i) & 1;
+ // *hi |= result << 4;
+
+ // // odd parity
+ // result = 0;
+ // for (i = 19;i >= 1;i--)
+ // result ^= (*lo >> i) & 1;
+ // *lo |= result;
+// }
+
+//static void calc26(uint16_t fc, uint32_t cardno, uint8_t *out){
+static void calc26(uint16_t fc, uint32_t cardno, uint8_t *out){
+ uint8_t wiegand[24];
+ num_to_bytebits(fc, 8, wiegand);
+ num_to_bytebits(cardno, 16, wiegand+8);
+ wiegand_add_parity(out, wiegand, sizeof(wiegand) );
+}
+// static void calc33(uint16_t fc, uint32_t cardno, uint8_t *out){
+// }
+static void calc34(uint16_t fc, uint32_t cardno, uint8_t *out){
+ uint8_t wiegand[32];
+ num_to_bytebits(fc, 16, wiegand);
+ num_to_bytebits(cardno, 16, wiegand + 16);
+ wiegand_add_parity(out, wiegand, sizeof(wiegand) );
+}
+// static void calc35(uint16_t fc, uint32_t cardno, uint8_t *out){
+ // *lo = ((cardno & 0xFFFFF) << 1) | fc << 21;
+ // *hi = (1 << 5) | ((fc >> 11) & 1);
+// }
+static void calc37S(uint16_t fc, uint32_t cardno, uint8_t *out){
+ // FC 2 - 17 - 16 bit
+ // cardno 18 - 36 - 19 bit
+ // Even P1 1 - 19
+ // Odd P37 19 - 36
+ uint8_t wiegand[35];
+ num_to_bytebits(fc, 16, wiegand);
+ num_to_bytebits(cardno, 19, wiegand + 16);
+ wiegand_add_parity(out, wiegand, sizeof(wiegand) );
+}
+static void calc37H(uint64_t cardno, uint8_t *out){
+ // SC NONE
+ // cardno 1-35 34 bits
+ // Even Parity 0th bit 1-18
+ // Odd Parity 36th bit 19-35
+ uint8_t wiegand[37];
+ num_to_bytebits( (uint32_t)(cardno >> 32), 2, wiegand);
+ num_to_bytebits( (uint32_t)(cardno >> 0), 32, wiegand + 2);
+ wiegand_add_parity(out, wiegand, sizeof(wiegand) );
+
+ printf("%x %x\n", (uint32_t)(cardno >> 32), (uint32_t)cardno );
+}
+// static void calc40(uint64_t cardno, uint8_t *out){
+ // cardno = (cardno & 0xFFFFFFFFFF);
+ // *lo = ((cardno & 0xFFFFFFFF) << 1 );
+ // *hi = (cardno >> 31);
+// }
+
+void calcWiegand(uint8_t fmtlen, uint16_t fc, uint64_t cardno, uint8_t *bits){
+ uint32_t cn32 = (cardno & 0xFFFFFFFF);
+ switch ( fmtlen ) {
+ case 26: calc26(fc, cn32, bits); break;
+ // case 33 : calc33(fc, cn32, bits); break;
+ case 34: calc34(fc, cn32, bits); break;
+ // case 35 : calc35(fc, cn32, bits); break;
+ case 37: calc37S(fc, cn32, bits); break;
+ case 38: calc37H(cardno, bits); break;
+ // case 40 : calc40(cardno, bits); break;
+ // case 44 : { break; }
+ // case 84 : { break; }
+ default: break;
+ }
+}
+
+int CmdHIDWiegand(const char *Cmd) {
+ uint32_t oem = 0, fc = 0;
+ uint64_t cardnum = 0;
+ uint64_t blocks = 0, wiegand = 0;
+
+ uint8_t bits[BITS];
+ uint8_t *bs = bits;
+ memset(bs, 0, sizeof(bits));
+
+ uint8_t ctmp = param_getchar(Cmd, 0);
+ if ( strlen(Cmd) == 0 || strlen(Cmd) < 3 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_hid_wiegand();
+
+ oem = param_get8(Cmd, 0);
+ fc = param_get32ex(Cmd, 1, 0, 10);
+ cardnum = param_get64ex(Cmd, 2, 0, 10);
+
+ uint8_t fmtlen[] = {26,33,34,35,37,38,40};
+
+ PrintAndLog("HID | OEM | FC | CN | Wiegand | HID Formatted");
+ PrintAndLog("----+-----+------+---------+-----------+--------------------");
+ for (uint8_t i = 0; i < sizeof(fmtlen); i++){
+ memset(bits, 0x00, sizeof(bits));
+ calcWiegand( fmtlen[i], fc, cardnum, bs);
+ printf("ice:: %s \n", sprint_bin(bs, fmtlen[i]));
+ wiegand = (uint64_t)bytebits_to_byte(bs, 32) << 32 | bytebits_to_byte(bs+32, 32);
+
+ addHIDMarker(fmtlen[i], bs);
+ printf("ice:: %s\n", sprint_bin(bs, BITS));
+ blocks = (uint64_t)bytebits_to_byte(bs+32, 32) << 32 | bytebits_to_byte(bs+64, 32);
+ uint8_t shifts = 64-fmtlen[i];
+ wiegand >>= shifts;
+
+ PrintAndLog(" %u | %03u | %03u | %" PRIu64 " | %" PRIX64 " | %" PRIX64 ,
+ fmtlen[i],
+ oem,
+ fc,
+ cardnum,
+ wiegand,
+ blocks
+ );
+ }
+ PrintAndLog("----+-----+-----+-------+-----------+--------------------");
+ return 0;
+}
+
+int CmdHIDBrute(const char *Cmd){
+
+ bool errors = false;
+ uint32_t fc = 0, cn = 0, delay = 1000;
+ uint8_t fmtlen = 0;
+ uint8_t bits[96];
+ uint8_t *bs = bits;
+ memset(bs, 0, sizeof(bits));
+ uint8_t cmdp = 0;
+
+ while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
+ switch(param_getchar(Cmd, cmdp)) {
+ case 'h':
+ case 'H':
+ return usage_lf_hid_brute();
+ case 'f':
+ case 'F':
+ fc = param_get32ex(Cmd ,cmdp+1, 0, 10);
+ if ( !fc )
+ errors = true;
+ cmdp += 2;
+ break;
+ case 'd':
+ case 'D':
+ // delay between attemps, defaults to 1000ms.
+ delay = param_get32ex(Cmd, cmdp+1, 1000, 10);
+ cmdp += 2;
+ break;
+ case 'c':
+ case 'C':
+ cn = param_get32ex(Cmd, cmdp+1, 0, 10);
+ // truncate cardnumber.
+ cn &= 0xFFFF;
+ cmdp += 2;
+ break;
+ case 'a':
+ case 'A':
+ fmtlen = param_get8(Cmd, cmdp+1);
+ cmdp += 2;
+ bool is_ftm_ok = FALSE;
+ uint8_t ftms[] = {26,33,34,35,37};
+ for ( uint8_t i = 0; i < sizeof(ftms); i++){
+ if ( ftms[i] == fmtlen ) {
+ is_ftm_ok = TRUE;
+ }
+ }
+ // negated
+ errors = !is_ftm_ok;
+ break;
+ default:
+ PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
+ errors = true;
+ break;
+ }
+ }
+ if ( fc == 0 ) errors = true;
+ if ( errors ) return usage_lf_hid_brute();
+
+ PrintAndLog("Brute-forcing HID reader");
+ PrintAndLog("Press pm3-button to abort simulation or run another command");
+
+ uint16_t up = cn;
+ uint16_t down = cn;
+
+ for (;;){
+
+ if ( offline ) {
+ printf("Device offline\n");
+ return 2;
+ }
+
+ if (ukbhit()) {
+ PrintAndLog("aborted via keyboard!");
+ return sendPing();
+ }
+
+ // Do one up
+ if ( up < 0xFFFF )
+ if ( !sendTry(fmtlen, fc, up++, delay, bs)) return 1;
+
+ // Do one down (if cardnumber is given)
+ if ( cn > 1 )
+ if ( down > 1 )
+ if ( !sendTry(fmtlen, fc, --down, delay, bs)) return 1;
+ }
+ return 0;