make style

This commit is contained in:
Philippe Teuwen 2020-01-15 19:26:12 +01:00
commit b57f40e3d7
2 changed files with 321 additions and 311 deletions

View file

@ -341,18 +341,18 @@ static void hitag_reader_send_frame(const uint8_t *frame, size_t frame_len) {
size_t blocknr;
uint8_t hitag_crc(uint8_t *data, size_t length){
uint8_t hitag_crc(uint8_t *data, size_t length) {
uint8_t crc = 0xff;
unsigned int byte, bit;
for(byte=0; byte<((length+7)/8); byte++){
for (byte = 0; byte < ((length + 7) / 8); byte++) {
crc ^= *(data + byte);
bit = length < (8*(byte+1)) ? (length % 8) : 8;
while(bit--){
if(crc & 0x80){
crc<<=1;
bit = length < (8 * (byte + 1)) ? (length % 8) : 8;
while (bit--) {
if (crc & 0x80) {
crc <<= 1;
crc ^= 0x1d;
} else {
crc<<=1;
crc <<= 1;
}
}
}
@ -363,21 +363,21 @@ uint8_t hitag_crc(uint8_t *data, size_t length){
#define set_bit(data, i) *(data+(i/8)) |= (1 << (7-(i%8)))
#define clear_bit(data, i) *(data+(i/8)) &= ~(1 << (7-(i%8)))
#define flip_bit(data, i) *(data+(i/8)) ^= (1 << (7-(i%8)))
void fix_ac_decoding(uint8_t *input, size_t len){
void fix_ac_decoding(uint8_t *input, size_t len) {
// Reader routine tries to decode AC data after Manchester decoding
// AC has double the bitrate, extract data from bit-pairs
uint8_t temp[len / 16];
memset(temp, 0, sizeof(temp));
for (size_t i = 1; i < len; i += 2) {
if (test_bit(input, i) && test_bit(input, (i + 1))){
if (test_bit(input, i) && test_bit(input, (i + 1))) {
set_bit(temp, (i / 2));
}
}
memcpy(input, temp, sizeof(temp));
}
bool hitag_plain(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* txlen, bool hitag_s) {
bool hitag_plain(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t *txlen, bool hitag_s) {
uint8_t crc;
*txlen = 0;
switch (rxlen) {
@ -386,27 +386,27 @@ bool hitag_plain(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* txlen, bo
/*tx[0] = 0xb0; // Rev 3.0*/
tx[0] = 0x30; // Rev 2.0
*txlen = 5;
if(!bCollision) blocknr--;
if(blocknr < 0) {
if (!bCollision) blocknr--;
if (blocknr < 0) {
blocknr = 0;
}
if(!hitag_s){
if (!hitag_s) {
if (blocknr > 1 && blocknr < 31) {
blocknr=31;
blocknr = 31;
}
}
bCollision = true;
return true;
}
case 32: {
if(bCollision){
if (bCollision) {
// Select card by serial from response
tx[0] = 0x00 | rx[0] >> 5;
tx[1] = rx[0] << 3 | rx[1] >> 5;
tx[2] = rx[1] << 3 | rx[2] >> 5;
tx[3] = rx[2] << 3 | rx[3] >> 5;
tx[4] = rx[3] << 3;
crc = hitag_crc(tx,37);
crc = hitag_crc(tx, 37);
tx[4] |= crc >> 5;
tx[5] = crc << 3;
*txlen = 45;
@ -414,9 +414,9 @@ bool hitag_plain(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* txlen, bo
} else {
memcpy(tag.sectors[blocknr], rx, 4);
blocknr++;
if(!hitag_s){
if (!hitag_s) {
if (blocknr > 1 && blocknr < 31) {
blocknr=31;
blocknr = 31;
}
}
if (blocknr > 63) {
@ -429,16 +429,18 @@ bool hitag_plain(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* txlen, bo
Dbprintf("Reading page %02u", blocknr);
tx[0] = 0xc0 | blocknr >> 4; // RDPPAGE
tx[1] = blocknr << 4;
crc = hitag_crc(tx,12);
crc = hitag_crc(tx, 12);
tx[1] |= crc >> 4;
tx[2] = crc << 4;
*txlen = 20;
}
} break;
}
break;
default: {
Dbprintf("Uknown frame length: %d",rxlen);
Dbprintf("Uknown frame length: %d", rxlen);
return false;
} break;
}
break;
}
return true;
}
@ -446,7 +448,7 @@ bool hitag_plain(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* txlen, bo
size_t flipped_bit = 0;
uint32_t byte_value = 0;
bool hitag1_authenticate(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* txlen) {
bool hitag1_authenticate(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t *txlen) {
uint8_t crc;
*txlen = 0;
switch (rxlen) {
@ -455,17 +457,18 @@ bool hitag1_authenticate(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* t
/*tx[0] = 0xb0; // Rev 3.0*/
tx[0] = 0x30; // Rev 2.0
*txlen = 5;
if (bCrypto && byte_value <= 0xff){
if (bCrypto && byte_value <= 0xff) {
// to retry
bCrypto = false;
}
if(!bCollision) blocknr--;
if(blocknr < 0) {
if (!bCollision) blocknr--;
if (blocknr < 0) {
blocknr = 0;
}
bCollision = true;
// will receive 32-bit UID
} break;
}
break;
case 2: {
if (bAuthenticating) {
// received Auth init ACK, send nonce
@ -474,43 +477,44 @@ bool hitag1_authenticate(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* t
/*nonce[1] = 0x74;*/
/*nonce[2] = 0x80;*/
/*nonce[3] = 0xa5;*/
nonce[0]=byte_value;
nonce[0] = byte_value;
byte_value++;
/*set_bit(nonce,flipped_bit);*/
memcpy(tx,nonce,4);
memcpy(tx, nonce, 4);
*txlen = 32;
// will receive 32 bit encrypted Logdata
} else if (bCrypto) {
// authed, start reading
tx[0] = 0xe0 | blocknr >> 4; // RDCPAGE
tx[1] = blocknr << 4;
crc = hitag_crc(tx,12);
crc = hitag_crc(tx, 12);
tx[1] |= crc >> 4;
tx[2] = crc << 4;
*txlen = 20;
// will receive 32-bit encrypted page
}
} break;
}
break;
case 32: {
if (bCollision){
if (bCollision) {
// Select card by serial from response
tx[0] = 0x00 | rx[0] >> 5;
tx[1] = rx[0] << 3 | rx[1] >> 5;
tx[2] = rx[1] << 3 | rx[2] >> 5;
tx[3] = rx[2] << 3 | rx[3] >> 5;
tx[4] = rx[3] << 3;
crc = hitag_crc(tx,37);
crc = hitag_crc(tx, 37);
tx[4] |= crc >> 5;
tx[5] = crc << 3;
*txlen = 45;
bCollision = false;
bSelecting = true;
// will receive 32-bit configuration page
} else if (bSelecting){
} else if (bSelecting) {
// Initiate auth
tx[0] = 0xa0 | key_no >> 4; // WRCPAGE
tx[1] = blocknr << 4;
crc = hitag_crc(tx,12);
crc = hitag_crc(tx, 12);
tx[1] |= crc >> 4;
tx[2] = crc << 4;
*txlen = 20;
@ -525,7 +529,7 @@ bool hitag1_authenticate(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* t
Dbprintf("%02x%02x%02x%02x %02x%02x%02x%02x", rx[0], rx[1], rx[2], rx[3], tx[0], tx[1], tx[2], tx[3]);
// TODO replace with secret data stream
// TODO encrypt logdata_1
memcpy(tx,logdata_1,4);
memcpy(tx, logdata_1, 4);
*txlen = 32;
bAuthenticating = false;
bCrypto = true;
@ -533,7 +537,7 @@ bool hitag1_authenticate(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* t
} else if (bCrypto) {
// received 32-bit encrypted page
// TODO decrypt rx
memcpy(tag.sectors[blocknr],rx,4);
memcpy(tag.sectors[blocknr], rx, 4);
blocknr++;
if (blocknr > 63) {
DbpString("Read succesful!");
@ -543,23 +547,25 @@ bool hitag1_authenticate(uint8_t* rx, const size_t rxlen, uint8_t* tx, size_t* t
// TEST
Dbprintf("Succesfully authenticated with logdata:");
Dbhexdump(4,logdata_1,false);
Dbhexdump(4, logdata_1, false);
bSuccessful = true;
return false;
// read next page of card until done
tx[0] = 0xe0 | blocknr >> 4; // RDCPAGE
tx[1] = blocknr << 4;
crc = hitag_crc(tx,12);
crc = hitag_crc(tx, 12);
tx[1] |= crc >> 4;
tx[2] = crc << 4;
*txlen = 20;
}
} break;
}
break;
default: {
Dbprintf("Uknown frame length: %d",rxlen);
Dbprintf("Uknown frame length: %d", rxlen);
return false;
} break;
}
break;
}
return true;
@ -988,13 +994,13 @@ void SniffHitag(void) {
}
/*lf_count_edge_periods(10000);*/
while ((periods = lf_detect_gap(64)) != 0){
while ((periods = lf_detect_gap(64)) != 0) {
num_to_bytes(periods, 4, periods_bytes);
LogTrace(periods_bytes, 4, 0, 0, NULL, true);
}
/*
/*
// Check if frame was captured
if (rxlen > 0) {
// frame_count++;
@ -1008,7 +1014,7 @@ void SniffHitag(void) {
auth_table_len += 8;
}
}
*/
*/
}
lf_finalize();
@ -1227,9 +1233,10 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
case RHT1F_PLAIN: {
Dbprintf("Read public blocks in plain mode");
// this part will be unreadable
memset(tag.sectors+2, 0x0, 30);
memset(tag.sectors + 2, 0x0, 30);
blocknr = 0;
} break;
}
break;
case RHT1F_AUTHENTICATE: {
Dbprintf("Read all blocks in authed mode");
@ -1251,7 +1258,8 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
DbpString("Logdata_1:");
Dbhexdump(4, logdata_1, false);
blocknr = 0;
} break;
}
break;
case RHT2F_PASSWORD: {
Dbprintf("List identifier in password mode");
memcpy(password, htd->pwd.password, 4);
@ -1273,8 +1281,8 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
memcpy(key, htd->crypto.key, 6); //HACK; 4 or 6?? I read both in the code.
Dbhexdump(6, key, false);
DbpString("Nonce:");
Dbhexdump(4,nonce,false);
memcpy(nonce,htd->crypto.data,4);
Dbhexdump(4, nonce, false);
memcpy(nonce, htd->crypto.data, 4);
blocknr = 0;
bCrypto = false;
bAuthenticating = false;
@ -1310,7 +1318,7 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
uint8_t attempt_count = 0;
// Tag specific configuration settings (sof, timings, etc.)
if (htf < 10){
if (htf < 10) {
// hitagS settings
t_wait_1 = 204;
t_wait_2 = 128;
@ -1358,11 +1366,13 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
switch (htf) {
case RHT1F_PLAIN: {
bStop = !hitag_plain(rx, rxlen, tx, &txlen, false);
} break;
}
break;
case RHT1F_AUTHENTICATE: {
bStop = !hitag1_authenticate(rx, rxlen, tx, &txlen);
} break;
}
break;
case RHT2F_PASSWORD: {
bStop = !hitag2_password(rx, rxlen, tx, &txlen, false);
@ -1427,7 +1437,7 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
// Just break out of loop after an initial time-out (tag is probably not available)
if (periods == 0) break;
// Register the number of periods that have passed
response = t_wait_1-64 + periods;
response = t_wait_1 - 64 + periods;
// Indicate that we have dealt with the first edge
waiting_for_first_edge = false;
// The first edge is always a single NRZ bit, force periods on 16
@ -1437,20 +1447,20 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
} else {
// The function lf_count_edge_periods() returns 0 when a time-out occurs
if (periods == 0) {
Dbprintf("Detected timeout after [%d] nrz samples",nrzs);
Dbprintf("Detected timeout after [%d] nrz samples", nrzs);
break;
}
}
// Evaluate the number of periods before the next edge
if (periods > 24 && periods <= 64){
if (periods > 24 && periods <= 64) {
// Detected two sequential equal bits and a modulation switch
// NRZ modulation: (11 => --|) or (11 __|)
nrz_samples[nrzs++] = tag_modulation;
nrz_samples[nrzs++] = tag_modulation;
// Invert tag modulation state
tag_modulation ^= 1;
} else if (periods > 0 && periods <= 24){
} else if (periods > 0 && periods <= 24) {
// Detected one bit and a modulation switch
// NRZ modulation: (1 => -|) or (0 _|)
nrz_samples[nrzs++] = tag_modulation;
@ -1492,24 +1502,24 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
LED_B_ON();
// decode bitstream
manrawdecode((uint8_t*)nrz_samples, &nrzs, true, 0);
manrawdecode((uint8_t *)nrz_samples, &nrzs, true, 0);
// decode frame
// Verify if the header consists of five consecutive ones
if (nrzs < 5) {
Dbprintf("Detected unexpected number of manchester decoded samples [%d]",nrzs);
Dbprintf("Detected unexpected number of manchester decoded samples [%d]", nrzs);
break;
} else {
for (size_t i = 0; i < 5; i++){
for (size_t i = 0; i < 5; i++) {
if (nrz_samples[i] != 1) {
Dbprintf("Detected incorrect header, the bit [%d] is zero instead of one",i);
Dbprintf("Detected incorrect header, the bit [%d] is zero instead of one", i);
}
}
}
// Pack the response into a byte array
for (size_t i = 5; i < nrzs; i++){
for (size_t i = 5; i < nrzs; i++) {
uint8_t bit = nrz_samples[i];
rx[rxlen / 8] |= bit << (7 - (rxlen % 8));
rxlen++;