| 1 | //----------------------------------------------------------------------------- |
| 2 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, |
| 3 | // at your option, any later version. See the LICENSE.txt file for the text of |
| 4 | // the license. |
| 5 | //----------------------------------------------------------------------------- |
| 6 | // Hitag2 emulation (preliminary test version) |
| 7 | // |
| 8 | // (c) 2009 Henryk Plötz <henryk@ploetzli.ch> |
| 9 | //----------------------------------------------------------------------------- |
| 10 | // Hitag2 complete rewrite of the code |
| 11 | // - Fixed modulation/encoding issues |
| 12 | // - Rewrote code for transponder emulation |
| 13 | // - Added snooping of transponder communication |
| 14 | // - Added reader functionality |
| 15 | // |
| 16 | // (c) 2012 Roel Verdult |
| 17 | //----------------------------------------------------------------------------- |
| 18 | |
| 19 | #include "proxmark3.h" |
| 20 | #include "apps.h" |
| 21 | #include "util.h" |
| 22 | #include "hitag2.h" |
| 23 | #include "string.h" |
| 24 | #include "BigBuf.h" |
| 25 | |
| 26 | static bool bQuiet; |
| 27 | static bool bCrypto; |
| 28 | static bool bAuthenticating; |
| 29 | static bool bPwd; |
| 30 | static bool bSuccessful; |
| 31 | |
| 32 | struct hitag2_tag { |
| 33 | uint32_t uid; |
| 34 | enum { |
| 35 | TAG_STATE_RESET = 0x01, // Just powered up, awaiting GetSnr |
| 36 | TAG_STATE_ACTIVATING = 0x02 , // In activation phase (password mode), sent UID, awaiting reader password |
| 37 | TAG_STATE_ACTIVATED = 0x03, // Activation complete, awaiting read/write commands |
| 38 | TAG_STATE_WRITING = 0x04, // In write command, awaiting sector contents to be written |
| 39 | } state; |
| 40 | unsigned int active_sector; |
| 41 | byte_t crypto_active; |
| 42 | uint64_t cs; |
| 43 | byte_t sectors[12][4]; |
| 44 | }; |
| 45 | |
| 46 | static struct hitag2_tag tag = { |
| 47 | .state = TAG_STATE_RESET, |
| 48 | .sectors = { // Password mode: | Crypto mode: |
| 49 | [0] = { 0x02, 0x4e, 0x02, 0x20}, // UID | UID |
| 50 | [1] = { 0x4d, 0x49, 0x4b, 0x52}, // Password RWD | 32 bit LSB key |
| 51 | [2] = { 0x20, 0xf0, 0x4f, 0x4e}, // Reserved | 16 bit MSB key, 16 bit reserved |
| 52 | [3] = { 0x0e, 0xaa, 0x48, 0x54}, // Configuration, password TAG | Configuration, password TAG |
| 53 | [4] = { 0x46, 0x5f, 0x4f, 0x4b}, // Data: F_OK |
| 54 | [5] = { 0x55, 0x55, 0x55, 0x55}, // Data: UUUU |
| 55 | [6] = { 0xaa, 0xaa, 0xaa, 0xaa}, // Data: .... |
| 56 | [7] = { 0x55, 0x55, 0x55, 0x55}, // Data: UUUU |
| 57 | [8] = { 0x00, 0x00, 0x00, 0x00}, // RSK Low |
| 58 | [9] = { 0x00, 0x00, 0x00, 0x00}, // RSK High |
| 59 | [10] = { 0x00, 0x00, 0x00, 0x00}, // RCF |
| 60 | [11] = { 0x00, 0x00, 0x00, 0x00}, // SYNC |
| 61 | }, |
| 62 | }; |
| 63 | |
| 64 | // ToDo: define a meaningful maximum size for auth_table. The bigger this is, the lower will be the available memory for traces. |
| 65 | // Historically it used to be FREE_BUFFER_SIZE, which was 2744. |
| 66 | #define AUTH_TABLE_LENGTH 2744 |
| 67 | static byte_t* auth_table; |
| 68 | static size_t auth_table_pos = 0; |
| 69 | static size_t auth_table_len = AUTH_TABLE_LENGTH; |
| 70 | |
| 71 | static byte_t password[4]; |
| 72 | static byte_t NrAr[8]; |
| 73 | static byte_t key[8]; |
| 74 | static uint64_t cipher_state; |
| 75 | |
| 76 | /* Following is a modified version of cryptolib.com/ciphers/hitag2/ */ |
| 77 | // Software optimized 48-bit Philips/NXP Mifare Hitag2 PCF7936/46/47/52 stream cipher algorithm by I.C. Wiener 2006-2007. |
| 78 | // For educational purposes only. |
| 79 | // No warranties or guarantees of any kind. |
| 80 | // This code is released into the public domain by its author. |
| 81 | |
| 82 | // Basic macros: |
| 83 | |
| 84 | #define u8 uint8_t |
| 85 | #define u32 uint32_t |
| 86 | #define u64 uint64_t |
| 87 | #define rev8(x) ((((x)>>7)&1)+((((x)>>6)&1)<<1)+((((x)>>5)&1)<<2)+((((x)>>4)&1)<<3)+((((x)>>3)&1)<<4)+((((x)>>2)&1)<<5)+((((x)>>1)&1)<<6)+(((x)&1)<<7)) |
| 88 | #define rev16(x) (rev8 (x)+(rev8 (x>> 8)<< 8)) |
| 89 | #define rev32(x) (rev16(x)+(rev16(x>>16)<<16)) |
| 90 | #define rev64(x) (rev32(x)+(rev32(x>>32)<<32)) |
| 91 | #define bit(x,n) (((x)>>(n))&1) |
| 92 | #define bit32(x,n) ((((x)[(n)>>5])>>((n)))&1) |
| 93 | #define inv32(x,i,n) ((x)[(i)>>5]^=((u32)(n))<<((i)&31)) |
| 94 | #define rotl64(x, n) ((((u64)(x))<<((n)&63))+(((u64)(x))>>((0-(n))&63))) |
| 95 | |
| 96 | // Single bit Hitag2 functions: |
| 97 | #define i4(x,a,b,c,d) ((u32)((((x)>>(a))&1)+(((x)>>(b))&1)*2+(((x)>>(c))&1)*4+(((x)>>(d))&1)*8)) |
| 98 | |
| 99 | static const u32 ht2_f4a = 0x2C79; // 0010 1100 0111 1001 |
| 100 | static const u32 ht2_f4b = 0x6671; // 0110 0110 0111 0001 |
| 101 | static const u32 ht2_f5c = 0x7907287B; // 0111 1001 0000 0111 0010 1000 0111 1011 |
| 102 | |
| 103 | static u32 _f20 (const u64 x) |
| 104 | { |
| 105 | u32 i5; |
| 106 | |
| 107 | i5 = ((ht2_f4a >> i4 (x, 1, 2, 4, 5)) & 1)* 1 |
| 108 | + ((ht2_f4b >> i4 (x, 7,11,13,14)) & 1)* 2 |
| 109 | + ((ht2_f4b >> i4 (x,16,20,22,25)) & 1)* 4 |
| 110 | + ((ht2_f4b >> i4 (x,27,28,30,32)) & 1)* 8 |
| 111 | + ((ht2_f4a >> i4 (x,33,42,43,45)) & 1)*16; |
| 112 | |
| 113 | return (ht2_f5c >> i5) & 1; |
| 114 | } |
| 115 | |
| 116 | static u64 _hitag2_init (const u64 key, const u32 serial, const u32 IV) |
| 117 | { |
| 118 | u32 i; |
| 119 | u64 x = ((key & 0xFFFF) << 32) + serial; |
| 120 | |
| 121 | for (i = 0; i < 32; i++) |
| 122 | { |
| 123 | x >>= 1; |
| 124 | x += (u64) (_f20 (x) ^ (((IV >> i) ^ (key >> (i+16))) & 1)) << 47; |
| 125 | } |
| 126 | return x; |
| 127 | } |
| 128 | |
| 129 | static u64 _hitag2_round (u64 *state) |
| 130 | { |
| 131 | u64 x = *state; |
| 132 | |
| 133 | x = (x >> 1) + |
| 134 | ((((x >> 0) ^ (x >> 2) ^ (x >> 3) ^ (x >> 6) |
| 135 | ^ (x >> 7) ^ (x >> 8) ^ (x >> 16) ^ (x >> 22) |
| 136 | ^ (x >> 23) ^ (x >> 26) ^ (x >> 30) ^ (x >> 41) |
| 137 | ^ (x >> 42) ^ (x >> 43) ^ (x >> 46) ^ (x >> 47)) & 1) << 47); |
| 138 | |
| 139 | *state = x; |
| 140 | return _f20 (x); |
| 141 | } |
| 142 | |
| 143 | // "MIKRON" = O N M I K R |
| 144 | // Key = 4F 4E 4D 49 4B 52 - Secret 48-bit key |
| 145 | // Serial = 49 43 57 69 - Serial number of the tag, transmitted in clear |
| 146 | // Random = 65 6E 45 72 - Random IV, transmitted in clear |
| 147 | //~28~DC~80~31 = D7 23 7F CE - Authenticator value = inverted first 4 bytes of the keystream |
| 148 | |
| 149 | // The code below must print out "D7 23 7F CE 8C D0 37 A9 57 49 C1 E6 48 00 8A B6". |
| 150 | // The inverse of the first 4 bytes is sent to the tag to authenticate. |
| 151 | // The rest is encrypted by XORing it with the subsequent keystream. |
| 152 | |
| 153 | static u32 _hitag2_byte (u64 * x) |
| 154 | { |
| 155 | u32 i, c; |
| 156 | |
| 157 | for (i = 0, c = 0; i < 8; i++) c += (u32) _hitag2_round (x) << (i^7); |
| 158 | return c; |
| 159 | } |
| 160 | |
| 161 | static int hitag2_reset(void) { |
| 162 | tag.state = TAG_STATE_RESET; |
| 163 | tag.crypto_active = 0; |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int hitag2_init(void) { |
| 168 | hitag2_reset(); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void hitag2_cipher_reset(struct hitag2_tag *tag, const byte_t *iv) |
| 173 | { |
| 174 | uint64_t key = ((uint64_t)tag->sectors[2][2]) | |
| 175 | ((uint64_t)tag->sectors[2][3] << 8) | |
| 176 | ((uint64_t)tag->sectors[1][0] << 16) | |
| 177 | ((uint64_t)tag->sectors[1][1] << 24) | |
| 178 | ((uint64_t)tag->sectors[1][2] << 32) | |
| 179 | ((uint64_t)tag->sectors[1][3] << 40); |
| 180 | uint32_t uid = ((uint32_t)tag->sectors[0][0]) | |
| 181 | ((uint32_t)tag->sectors[0][1] << 8) | |
| 182 | ((uint32_t)tag->sectors[0][2] << 16) | |
| 183 | ((uint32_t)tag->sectors[0][3] << 24); |
| 184 | uint32_t iv_ = (((uint32_t)(iv[0]))) | |
| 185 | (((uint32_t)(iv[1])) << 8) | |
| 186 | (((uint32_t)(iv[2])) << 16) | |
| 187 | (((uint32_t)(iv[3])) << 24); |
| 188 | tag->cs = _hitag2_init(rev64(key), rev32(uid), rev32(iv_)); |
| 189 | } |
| 190 | |
| 191 | static int hitag2_cipher_authenticate(uint64_t* cs, const byte_t *authenticator_is) |
| 192 | { |
| 193 | byte_t authenticator_should[4]; |
| 194 | authenticator_should[0] = ~_hitag2_byte(cs); |
| 195 | authenticator_should[1] = ~_hitag2_byte(cs); |
| 196 | authenticator_should[2] = ~_hitag2_byte(cs); |
| 197 | authenticator_should[3] = ~_hitag2_byte(cs); |
| 198 | return (memcmp(authenticator_should, authenticator_is, 4) == 0); |
| 199 | } |
| 200 | |
| 201 | static int hitag2_cipher_transcrypt(uint64_t* cs, byte_t *data, unsigned int bytes, unsigned int bits) |
| 202 | { |
| 203 | int i; |
| 204 | for(i=0; i<bytes; i++) data[i] ^= _hitag2_byte(cs); |
| 205 | for(i=0; i<bits; i++) data[bytes] ^= _hitag2_round(cs) << (7-i); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | // Sam7s has several timers, we will use the source TIMER_CLOCK1 (aka AT91C_TC_CLKS_TIMER_DIV1_CLOCK) |
| 210 | // TIMER_CLOCK1 = MCK/2, MCK is running at 48 MHz, Timer is running at 48/2 = 24 MHz |
| 211 | // Hitag units (T0) have duration of 8 microseconds (us), which is 1/125000 per second (carrier) |
| 212 | // T0 = TIMER_CLOCK1 / 125000 = 192 |
| 213 | #define T0 192 |
| 214 | |
| 215 | #define SHORT_COIL() LOW(GPIO_SSC_DOUT) |
| 216 | #define OPEN_COIL() HIGH(GPIO_SSC_DOUT) |
| 217 | |
| 218 | #define HITAG_FRAME_LEN 20 |
| 219 | #define HITAG_T_STOP 36 /* T_EOF should be > 36 */ |
| 220 | #define HITAG_T_LOW 8 /* T_LOW should be 4..10 */ |
| 221 | #define HITAG_T_0_MIN 15 /* T[0] should be 18..22 */ |
| 222 | #define HITAG_T_1_MIN 25 /* T[1] should be 26..30 */ |
| 223 | //#define HITAG_T_EOF 40 /* T_EOF should be > 36 */ |
| 224 | #define HITAG_T_EOF 80 /* T_EOF should be > 36 */ |
| 225 | #define HITAG_T_WAIT_1 200 /* T_wresp should be 199..206 */ |
| 226 | #define HITAG_T_WAIT_2 90 /* T_wresp should be 199..206 */ |
| 227 | #define HITAG_T_WAIT_MAX 300 /* bit more than HITAG_T_WAIT_1 + HITAG_T_WAIT_2 */ |
| 228 | |
| 229 | #define HITAG_T_TAG_ONE_HALF_PERIOD 10 |
| 230 | #define HITAG_T_TAG_TWO_HALF_PERIOD 25 |
| 231 | #define HITAG_T_TAG_THREE_HALF_PERIOD 41 |
| 232 | #define HITAG_T_TAG_FOUR_HALF_PERIOD 57 |
| 233 | |
| 234 | #define HITAG_T_TAG_HALF_PERIOD 16 |
| 235 | #define HITAG_T_TAG_FULL_PERIOD 32 |
| 236 | |
| 237 | #define HITAG_T_TAG_CAPTURE_ONE_HALF 13 |
| 238 | #define HITAG_T_TAG_CAPTURE_TWO_HALF 25 |
| 239 | #define HITAG_T_TAG_CAPTURE_THREE_HALF 41 |
| 240 | #define HITAG_T_TAG_CAPTURE_FOUR_HALF 57 |
| 241 | |
| 242 | |
| 243 | static void hitag_send_bit(int bit) { |
| 244 | LED_A_ON(); |
| 245 | // Reset clock for the next bit |
| 246 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG; |
| 247 | |
| 248 | // Fixed modulation, earlier proxmark version used inverted signal |
| 249 | if(bit == 0) { |
| 250 | // Manchester: Unloaded, then loaded |__--| |
| 251 | LOW(GPIO_SSC_DOUT); |
| 252 | while(AT91C_BASE_TC0->TC_CV < T0*HITAG_T_TAG_HALF_PERIOD); |
| 253 | HIGH(GPIO_SSC_DOUT); |
| 254 | while(AT91C_BASE_TC0->TC_CV < T0*HITAG_T_TAG_FULL_PERIOD); |
| 255 | } else { |
| 256 | // Manchester: Loaded, then unloaded |--__| |
| 257 | HIGH(GPIO_SSC_DOUT); |
| 258 | while(AT91C_BASE_TC0->TC_CV < T0*HITAG_T_TAG_HALF_PERIOD); |
| 259 | LOW(GPIO_SSC_DOUT); |
| 260 | while(AT91C_BASE_TC0->TC_CV < T0*HITAG_T_TAG_FULL_PERIOD); |
| 261 | } |
| 262 | LED_A_OFF(); |
| 263 | } |
| 264 | |
| 265 | static void hitag_send_frame(const byte_t* frame, size_t frame_len) |
| 266 | { |
| 267 | // Send start of frame |
| 268 | for(size_t i=0; i<5; i++) { |
| 269 | hitag_send_bit(1); |
| 270 | } |
| 271 | |
| 272 | // Send the content of the frame |
| 273 | for(size_t i=0; i<frame_len; i++) { |
| 274 | hitag_send_bit((frame[i/8] >> (7-(i%8)))&1); |
| 275 | } |
| 276 | |
| 277 | // Drop the modulation |
| 278 | LOW(GPIO_SSC_DOUT); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | static void hitag2_handle_reader_command(byte_t* rx, const size_t rxlen, byte_t* tx, size_t* txlen) |
| 283 | { |
| 284 | byte_t rx_air[HITAG_FRAME_LEN]; |
| 285 | |
| 286 | // Copy the (original) received frame how it is send over the air |
| 287 | memcpy(rx_air,rx,nbytes(rxlen)); |
| 288 | |
| 289 | if(tag.crypto_active) { |
| 290 | hitag2_cipher_transcrypt(&(tag.cs),rx,rxlen/8,rxlen%8); |
| 291 | } |
| 292 | |
| 293 | // Reset the transmission frame length |
| 294 | *txlen = 0; |
| 295 | |
| 296 | // Try to find out which command was send by selecting on length (in bits) |
| 297 | switch (rxlen) { |
| 298 | // Received 11000 from the reader, request for UID, send UID |
| 299 | case 05: { |
| 300 | // Always send over the air in the clear plaintext mode |
| 301 | if(rx_air[0] != 0xC0) { |
| 302 | // Unknown frame ? |
| 303 | return; |
| 304 | } |
| 305 | *txlen = 32; |
| 306 | memcpy(tx,tag.sectors[0],4); |
| 307 | tag.crypto_active = 0; |
| 308 | } |
| 309 | break; |
| 310 | |
| 311 | // Read/Write command: ..xx x..y yy with yyy == ~xxx, xxx is sector number |
| 312 | case 10: { |
| 313 | unsigned int sector = (~( ((rx[0]<<2)&0x04) | ((rx[1]>>6)&0x03) ) & 0x07); |
| 314 | // Verify complement of sector index |
| 315 | if(sector != ((rx[0]>>3)&0x07)) { |
| 316 | //DbpString("Transmission error (read/write)"); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | switch (rx[0] & 0xC6) { |
| 321 | // Read command: 11xx x00y |
| 322 | case 0xC0: |
| 323 | memcpy(tx,tag.sectors[sector],4); |
| 324 | *txlen = 32; |
| 325 | break; |
| 326 | |
| 327 | // Inverted Read command: 01xx x10y |
| 328 | case 0x44: |
| 329 | for (size_t i=0; i<4; i++) { |
| 330 | tx[i] = tag.sectors[sector][i] ^ 0xff; |
| 331 | } |
| 332 | *txlen = 32; |
| 333 | break; |
| 334 | |
| 335 | // Write command: 10xx x01y |
| 336 | case 0x82: |
| 337 | // Prepare write, acknowledge by repeating command |
| 338 | memcpy(tx,rx,nbytes(rxlen)); |
| 339 | *txlen = rxlen; |
| 340 | tag.active_sector = sector; |
| 341 | tag.state=TAG_STATE_WRITING; |
| 342 | break; |
| 343 | |
| 344 | // Unknown command |
| 345 | default: |
| 346 | Dbprintf("Uknown command: %02x %02x",rx[0],rx[1]); |
| 347 | return; |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | break; |
| 352 | |
| 353 | // Writing data or Reader password |
| 354 | case 32: { |
| 355 | if(tag.state == TAG_STATE_WRITING) { |
| 356 | // These are the sector contents to be written. We don't have to do anything else. |
| 357 | memcpy(tag.sectors[tag.active_sector],rx,nbytes(rxlen)); |
| 358 | tag.state=TAG_STATE_RESET; |
| 359 | return; |
| 360 | } else { |
| 361 | // Received RWD password, respond with configuration and our password |
| 362 | if(memcmp(rx,tag.sectors[1],4) != 0) { |
| 363 | DbpString("Reader password is wrong"); |
| 364 | return; |
| 365 | } |
| 366 | *txlen = 32; |
| 367 | memcpy(tx,tag.sectors[3],4); |
| 368 | } |
| 369 | } |
| 370 | break; |
| 371 | |
| 372 | // Received RWD authentication challenge and respnse |
| 373 | case 64: { |
| 374 | // Store the authentication attempt |
| 375 | if (auth_table_len < (AUTH_TABLE_LENGTH-8)) { |
| 376 | memcpy(auth_table+auth_table_len,rx,8); |
| 377 | auth_table_len += 8; |
| 378 | } |
| 379 | |
| 380 | // Reset the cipher state |
| 381 | hitag2_cipher_reset(&tag,rx); |
| 382 | // Check if the authentication was correct |
| 383 | if(!hitag2_cipher_authenticate(&(tag.cs),rx+4)) { |
| 384 | // The reader failed to authenticate, do nothing |
| 385 | Dbprintf("auth: %02x%02x%02x%02x%02x%02x%02x%02x Failed!",rx[0],rx[1],rx[2],rx[3],rx[4],rx[5],rx[6],rx[7]); |
| 386 | return; |
| 387 | } |
| 388 | // Succesful, but commented out reporting back to the Host, this may delay to much. |
| 389 | // Dbprintf("auth: %02x%02x%02x%02x%02x%02x%02x%02x OK!",rx[0],rx[1],rx[2],rx[3],rx[4],rx[5],rx[6],rx[7]); |
| 390 | |
| 391 | // Activate encryption algorithm for all further communication |
| 392 | tag.crypto_active = 1; |
| 393 | |
| 394 | // Use the tag password as response |
| 395 | memcpy(tx,tag.sectors[3],4); |
| 396 | *txlen = 32; |
| 397 | } |
| 398 | break; |
| 399 | } |
| 400 | |
| 401 | // LogTraceHitag(rx,rxlen,0,0,false); |
| 402 | // LogTraceHitag(tx,*txlen,0,0,true); |
| 403 | |
| 404 | if(tag.crypto_active) { |
| 405 | hitag2_cipher_transcrypt(&(tag.cs), tx, *txlen/8, *txlen%8); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | static void hitag_reader_send_bit(int bit) { |
| 410 | LED_A_ON(); |
| 411 | // Reset clock for the next bit |
| 412 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG; |
| 413 | |
| 414 | // Binary puls length modulation (BPLM) is used to encode the data stream |
| 415 | // This means that a transmission of a one takes longer than that of a zero |
| 416 | |
| 417 | // Enable modulation, which means, drop the the field |
| 418 | HIGH(GPIO_SSC_DOUT); |
| 419 | |
| 420 | // Wait for 4-10 times the carrier period |
| 421 | while(AT91C_BASE_TC0->TC_CV < T0*6); |
| 422 | // SpinDelayUs(8*8); |
| 423 | |
| 424 | // Disable modulation, just activates the field again |
| 425 | LOW(GPIO_SSC_DOUT); |
| 426 | |
| 427 | if(bit == 0) { |
| 428 | // Zero bit: |_-| |
| 429 | while(AT91C_BASE_TC0->TC_CV < T0*22); |
| 430 | |
| 431 | } else { |
| 432 | // One bit: |_--| |
| 433 | while(AT91C_BASE_TC0->TC_CV < T0*28); |
| 434 | } |
| 435 | LED_A_OFF(); |
| 436 | } |
| 437 | |
| 438 | |
| 439 | static void hitag_reader_send_frame(const byte_t* frame, size_t frame_len) |
| 440 | { |
| 441 | // Send the content of the frame |
| 442 | for(size_t i=0; i<frame_len; i++) { |
| 443 | hitag_reader_send_bit((frame[i/8] >> (7-(i%8)))&1); |
| 444 | } |
| 445 | // Send EOF |
| 446 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG; |
| 447 | // Enable modulation, which means, drop the the field |
| 448 | HIGH(GPIO_SSC_DOUT); |
| 449 | // Wait for 4-10 times the carrier period |
| 450 | while(AT91C_BASE_TC0->TC_CV < T0*6); |
| 451 | // Disable modulation, just activates the field again |
| 452 | LOW(GPIO_SSC_DOUT); |
| 453 | } |
| 454 | |
| 455 | size_t blocknr; |
| 456 | |
| 457 | static bool hitag2_password(byte_t* rx, const size_t rxlen, byte_t* tx, size_t* txlen) { |
| 458 | // Reset the transmission frame length |
| 459 | *txlen = 0; |
| 460 | |
| 461 | // Try to find out which command was send by selecting on length (in bits) |
| 462 | switch (rxlen) { |
| 463 | // No answer, try to resurrect |
| 464 | case 0: { |
| 465 | // Stop if there is no answer (after sending password) |
| 466 | if (bPwd) { |
| 467 | DbpString("Password failed!"); |
| 468 | return false; |
| 469 | } |
| 470 | *txlen = 5; |
| 471 | memcpy(tx,"\xc0",nbytes(*txlen)); |
| 472 | } break; |
| 473 | |
| 474 | // Received UID, tag password |
| 475 | case 32: { |
| 476 | if (!bPwd) { |
| 477 | *txlen = 32; |
| 478 | memcpy(tx,password,4); |
| 479 | bPwd = true; |
| 480 | memcpy(tag.sectors[blocknr],rx,4); |
| 481 | blocknr++; |
| 482 | } else { |
| 483 | |
| 484 | if(blocknr == 1){ |
| 485 | //store password in block1, the TAG answers with Block3, but we need the password in memory |
| 486 | memcpy(tag.sectors[blocknr],tx,4); |
| 487 | } else { |
| 488 | memcpy(tag.sectors[blocknr],rx,4); |
| 489 | } |
| 490 | |
| 491 | blocknr++; |
| 492 | if (blocknr > 7) { |
| 493 | DbpString("Read succesful!"); |
| 494 | bSuccessful = true; |
| 495 | return false; |
| 496 | } |
| 497 | *txlen = 10; |
| 498 | tx[0] = 0xc0 | (blocknr << 3) | ((blocknr^7) >> 2); |
| 499 | tx[1] = ((blocknr^7) << 6); |
| 500 | } |
| 501 | } break; |
| 502 | |
| 503 | // Unexpected response |
| 504 | default: { |
| 505 | Dbprintf("Uknown frame length: %d",rxlen); |
| 506 | return false; |
| 507 | } break; |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | static bool hitag2_crypto(byte_t* rx, const size_t rxlen, byte_t* tx, size_t* txlen) { |
| 513 | // Reset the transmission frame length |
| 514 | *txlen = 0; |
| 515 | |
| 516 | if(bCrypto) { |
| 517 | hitag2_cipher_transcrypt(&cipher_state,rx,rxlen/8,rxlen%8); |
| 518 | } |
| 519 | |
| 520 | // Try to find out which command was send by selecting on length (in bits) |
| 521 | switch (rxlen) { |
| 522 | // No answer, try to resurrect |
| 523 | case 0: { |
| 524 | // Stop if there is no answer while we are in crypto mode (after sending NrAr) |
| 525 | if (bCrypto) { |
| 526 | // Failed during authentication |
| 527 | if (bAuthenticating) { |
| 528 | DbpString("Authentication failed!"); |
| 529 | return false; |
| 530 | } else { |
| 531 | // Failed reading a block, could be (read/write) locked, skip block and re-authenticate |
| 532 | if (blocknr == 1) { |
| 533 | // Write the low part of the key in memory |
| 534 | memcpy(tag.sectors[1],key+2,4); |
| 535 | } else if (blocknr == 2) { |
| 536 | // Write the high part of the key in memory |
| 537 | tag.sectors[2][0] = 0x00; |
| 538 | tag.sectors[2][1] = 0x00; |
| 539 | tag.sectors[2][2] = key[0]; |
| 540 | tag.sectors[2][3] = key[1]; |
| 541 | } else { |
| 542 | // Just put zero's in the memory (of the unreadable block) |
| 543 | memset(tag.sectors[blocknr],0x00,4); |
| 544 | } |
| 545 | blocknr++; |
| 546 | bCrypto = false; |
| 547 | } |
| 548 | } else { |
| 549 | *txlen = 5; |
| 550 | memcpy(tx,"\xc0",nbytes(*txlen)); |
| 551 | } |
| 552 | } break; |
| 553 | |
| 554 | // Received UID, crypto tag answer |
| 555 | case 32: { |
| 556 | if (!bCrypto) { |
| 557 | uint64_t ui64key = key[0] | ((uint64_t)key[1]) << 8 | ((uint64_t)key[2]) << 16 | ((uint64_t)key[3]) << 24 | ((uint64_t)key[4]) << 32 | ((uint64_t)key[5]) << 40; |
| 558 | uint32_t ui32uid = rx[0] | ((uint32_t)rx[1]) << 8 | ((uint32_t)rx[2]) << 16 | ((uint32_t)rx[3]) << 24; |
| 559 | cipher_state = _hitag2_init(rev64(ui64key), rev32(ui32uid), 0); |
| 560 | memset(tx,0x00,4); |
| 561 | memset(tx+4,0xff,4); |
| 562 | hitag2_cipher_transcrypt(&cipher_state,tx+4,4,0); |
| 563 | *txlen = 64; |
| 564 | bCrypto = true; |
| 565 | bAuthenticating = true; |
| 566 | } else { |
| 567 | // Check if we received answer tag (at) |
| 568 | if (bAuthenticating) { |
| 569 | bAuthenticating = false; |
| 570 | } else { |
| 571 | // Store the received block |
| 572 | memcpy(tag.sectors[blocknr],rx,4); |
| 573 | blocknr++; |
| 574 | } |
| 575 | if (blocknr > 7) { |
| 576 | DbpString("Read succesful!"); |
| 577 | bSuccessful = true; |
| 578 | return false; |
| 579 | } |
| 580 | *txlen = 10; |
| 581 | tx[0] = 0xc0 | (blocknr << 3) | ((blocknr^7) >> 2); |
| 582 | tx[1] = ((blocknr^7) << 6); |
| 583 | } |
| 584 | } break; |
| 585 | |
| 586 | // Unexpected response |
| 587 | default: { |
| 588 | Dbprintf("Uknown frame length: %d",rxlen); |
| 589 | return false; |
| 590 | } break; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | if(bCrypto) { |
| 595 | // We have to return now to avoid double encryption |
| 596 | if (!bAuthenticating) { |
| 597 | hitag2_cipher_transcrypt(&cipher_state, tx, *txlen/8, *txlen%8); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | return true; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | static bool hitag2_authenticate(byte_t* rx, const size_t rxlen, byte_t* tx, size_t* txlen) { |
| 606 | // Reset the transmission frame length |
| 607 | *txlen = 0; |
| 608 | |
| 609 | // Try to find out which command was send by selecting on length (in bits) |
| 610 | switch (rxlen) { |
| 611 | // No answer, try to resurrect |
| 612 | case 0: { |
| 613 | // Stop if there is no answer while we are in crypto mode (after sending NrAr) |
| 614 | if (bCrypto) { |
| 615 | DbpString("Authentication failed!"); |
| 616 | return false; |
| 617 | } |
| 618 | *txlen = 5; |
| 619 | memcpy(tx,"\xc0",nbytes(*txlen)); |
| 620 | } break; |
| 621 | |
| 622 | // Received UID, crypto tag answer |
| 623 | case 32: { |
| 624 | if (!bCrypto) { |
| 625 | *txlen = 64; |
| 626 | memcpy(tx,NrAr,8); |
| 627 | bCrypto = true; |
| 628 | } else { |
| 629 | DbpString("Authentication succesful!"); |
| 630 | return true; |
| 631 | } |
| 632 | } break; |
| 633 | |
| 634 | // Unexpected response |
| 635 | default: { |
| 636 | Dbprintf("Uknown frame length: %d",rxlen); |
| 637 | return false; |
| 638 | } break; |
| 639 | } |
| 640 | |
| 641 | return true; |
| 642 | } |
| 643 | |
| 644 | |
| 645 | static bool hitag2_test_auth_attempts(byte_t* rx, const size_t rxlen, byte_t* tx, size_t* txlen) { |
| 646 | |
| 647 | // Reset the transmission frame length |
| 648 | *txlen = 0; |
| 649 | |
| 650 | // Try to find out which command was send by selecting on length (in bits) |
| 651 | switch (rxlen) { |
| 652 | // No answer, try to resurrect |
| 653 | case 0: { |
| 654 | // Stop if there is no answer while we are in crypto mode (after sending NrAr) |
| 655 | if (bCrypto) { |
| 656 | Dbprintf("auth: %02x%02x%02x%02x%02x%02x%02x%02x Failed, removed entry!",NrAr[0],NrAr[1],NrAr[2],NrAr[3],NrAr[4],NrAr[5],NrAr[6],NrAr[7]); |
| 657 | |
| 658 | // Removing failed entry from authentiations table |
| 659 | memcpy(auth_table+auth_table_pos,auth_table+auth_table_pos+8,8); |
| 660 | auth_table_len -= 8; |
| 661 | |
| 662 | // Return if we reached the end of the authentications table |
| 663 | bCrypto = false; |
| 664 | if (auth_table_pos == auth_table_len) { |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | // Copy the next authentication attempt in row (at the same position, b/c we removed last failed entry) |
| 669 | memcpy(NrAr,auth_table+auth_table_pos,8); |
| 670 | } |
| 671 | *txlen = 5; |
| 672 | memcpy(tx,"\xc0",nbytes(*txlen)); |
| 673 | } break; |
| 674 | |
| 675 | // Received UID, crypto tag answer, or read block response |
| 676 | case 32: { |
| 677 | if (!bCrypto) { |
| 678 | *txlen = 64; |
| 679 | memcpy(tx,NrAr,8); |
| 680 | bCrypto = true; |
| 681 | } else { |
| 682 | Dbprintf("auth: %02x%02x%02x%02x%02x%02x%02x%02x OK",NrAr[0],NrAr[1],NrAr[2],NrAr[3],NrAr[4],NrAr[5],NrAr[6],NrAr[7]); |
| 683 | bCrypto = false; |
| 684 | if ((auth_table_pos+8) == auth_table_len) { |
| 685 | return false; |
| 686 | } |
| 687 | auth_table_pos += 8; |
| 688 | memcpy(NrAr,auth_table+auth_table_pos,8); |
| 689 | } |
| 690 | } break; |
| 691 | |
| 692 | default: { |
| 693 | Dbprintf("Uknown frame length: %d",rxlen); |
| 694 | return false; |
| 695 | } break; |
| 696 | } |
| 697 | |
| 698 | return true; |
| 699 | } |
| 700 | |
| 701 | |
| 702 | void SnoopHitag(uint32_t type) { |
| 703 | int frame_count; |
| 704 | int response; |
| 705 | int overflow; |
| 706 | bool rising_edge; |
| 707 | bool reader_frame; |
| 708 | int lastbit; |
| 709 | bool bSkip; |
| 710 | int tag_sof; |
| 711 | byte_t rx[HITAG_FRAME_LEN]; |
| 712 | size_t rxlen=0; |
| 713 | |
| 714 | FpgaDownloadAndGo(FPGA_BITSTREAM_LF); |
| 715 | |
| 716 | // Clean up trace and prepare it for storing frames |
| 717 | set_tracing(TRUE); |
| 718 | clear_trace(); |
| 719 | |
| 720 | auth_table_len = 0; |
| 721 | auth_table_pos = 0; |
| 722 | |
| 723 | BigBuf_free(); |
| 724 | auth_table = (byte_t *)BigBuf_malloc(AUTH_TABLE_LENGTH); |
| 725 | memset(auth_table, 0x00, AUTH_TABLE_LENGTH); |
| 726 | |
| 727 | DbpString("Starting Hitag2 snoop"); |
| 728 | LED_D_ON(); |
| 729 | |
| 730 | // Set up eavesdropping mode, frequency divisor which will drive the FPGA |
| 731 | // and analog mux selection. |
| 732 | FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_TOGGLE_MODE); |
| 733 | FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz |
| 734 | SetAdcMuxFor(GPIO_MUXSEL_LOPKD); |
| 735 | RELAY_OFF(); |
| 736 | |
| 737 | // Configure output pin that is connected to the FPGA (for modulating) |
| 738 | AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT; |
| 739 | AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT; |
| 740 | |
| 741 | // Disable modulation, we are going to eavesdrop, not modulate ;) |
| 742 | LOW(GPIO_SSC_DOUT); |
| 743 | |
| 744 | // Enable Peripheral Clock for TIMER_CLOCK1, used to capture edges of the reader frames |
| 745 | AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1); |
| 746 | AT91C_BASE_PIOA->PIO_BSR = GPIO_SSC_FRAME; |
| 747 | |
| 748 | // Disable timer during configuration |
| 749 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; |
| 750 | |
| 751 | // Capture mode, defaul timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger, |
| 752 | // external trigger rising edge, load RA on rising edge of TIOA. |
| 753 | uint32_t t1_channel_mode = AT91C_TC_CLKS_TIMER_DIV1_CLOCK | AT91C_TC_ETRGEDG_BOTH | AT91C_TC_ABETRG | AT91C_TC_LDRA_BOTH; |
| 754 | AT91C_BASE_TC1->TC_CMR = t1_channel_mode; |
| 755 | |
| 756 | // Enable and reset counter |
| 757 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; |
| 758 | |
| 759 | // Reset the received frame, frame count and timing info |
| 760 | memset(rx,0x00,sizeof(rx)); |
| 761 | frame_count = 0; |
| 762 | response = 0; |
| 763 | overflow = 0; |
| 764 | reader_frame = false; |
| 765 | lastbit = 1; |
| 766 | bSkip = true; |
| 767 | tag_sof = 4; |
| 768 | |
| 769 | while(!BUTTON_PRESS() && !usb_poll_validate_length()) { |
| 770 | // Watchdog hit |
| 771 | WDT_HIT(); |
| 772 | |
| 773 | // Receive frame, watch for at most T0*EOF periods |
| 774 | while (AT91C_BASE_TC1->TC_CV < T0*HITAG_T_EOF) { |
| 775 | // Check if rising edge in modulation is detected |
| 776 | if(AT91C_BASE_TC1->TC_SR & AT91C_TC_LDRAS) { |
| 777 | // Retrieve the new timing values |
| 778 | int ra = (AT91C_BASE_TC1->TC_RA/T0); |
| 779 | |
| 780 | // Find out if we are dealing with a rising or falling edge |
| 781 | rising_edge = (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_FRAME) > 0; |
| 782 | |
| 783 | // Shorter periods will only happen with reader frames |
| 784 | if (!reader_frame && rising_edge && ra < HITAG_T_TAG_CAPTURE_ONE_HALF) { |
| 785 | // Switch from tag to reader capture |
| 786 | LED_C_OFF(); |
| 787 | reader_frame = true; |
| 788 | memset(rx,0x00,sizeof(rx)); |
| 789 | rxlen = 0; |
| 790 | } |
| 791 | |
| 792 | // Only handle if reader frame and rising edge, or tag frame and falling edge |
| 793 | if (reader_frame != rising_edge) { |
| 794 | overflow += ra; |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | // Add the buffered timing values of earlier captured edges which were skipped |
| 799 | ra += overflow; |
| 800 | overflow = 0; |
| 801 | |
| 802 | if (reader_frame) { |
| 803 | LED_B_ON(); |
| 804 | // Capture reader frame |
| 805 | if(ra >= HITAG_T_STOP) { |
| 806 | if (rxlen != 0) { |
| 807 | //DbpString("wierd0?"); |
| 808 | } |
| 809 | // Capture the T0 periods that have passed since last communication or field drop (reset) |
| 810 | response = (ra - HITAG_T_LOW); |
| 811 | } else if(ra >= HITAG_T_1_MIN ) { |
| 812 | // '1' bit |
| 813 | rx[rxlen / 8] |= 1 << (7-(rxlen%8)); |
| 814 | rxlen++; |
| 815 | } else if(ra >= HITAG_T_0_MIN) { |
| 816 | // '0' bit |
| 817 | rx[rxlen / 8] |= 0 << (7-(rxlen%8)); |
| 818 | rxlen++; |
| 819 | } else { |
| 820 | // Ignore wierd value, is to small to mean anything |
| 821 | } |
| 822 | } else { |
| 823 | LED_C_ON(); |
| 824 | // Capture tag frame (manchester decoding using only falling edges) |
| 825 | if(ra >= HITAG_T_EOF) { |
| 826 | if (rxlen != 0) { |
| 827 | //DbpString("wierd1?"); |
| 828 | } |
| 829 | // Capture the T0 periods that have passed since last communication or field drop (reset) |
| 830 | // We always recieve a 'one' first, which has the falling edge after a half period |-_| |
| 831 | response = ra-HITAG_T_TAG_HALF_PERIOD; |
| 832 | } else if(ra >= HITAG_T_TAG_CAPTURE_FOUR_HALF) { |
| 833 | // Manchester coding example |-_|_-|-_| (101) |
| 834 | rx[rxlen / 8] |= 0 << (7-(rxlen%8)); |
| 835 | rxlen++; |
| 836 | rx[rxlen / 8] |= 1 << (7-(rxlen%8)); |
| 837 | rxlen++; |
| 838 | } else if(ra >= HITAG_T_TAG_CAPTURE_THREE_HALF) { |
| 839 | // Manchester coding example |_-|...|_-|-_| (0...01) |
| 840 | rx[rxlen / 8] |= 0 << (7-(rxlen%8)); |
| 841 | rxlen++; |
| 842 | // We have to skip this half period at start and add the 'one' the second time |
| 843 | if (!bSkip) { |
| 844 | rx[rxlen / 8] |= 1 << (7-(rxlen%8)); |
| 845 | rxlen++; |
| 846 | } |
| 847 | lastbit = !lastbit; |
| 848 | bSkip = !bSkip; |
| 849 | } else if(ra >= HITAG_T_TAG_CAPTURE_TWO_HALF) { |
| 850 | // Manchester coding example |_-|_-| (00) or |-_|-_| (11) |
| 851 | if (tag_sof) { |
| 852 | // Ignore bits that are transmitted during SOF |
| 853 | tag_sof--; |
| 854 | } else { |
| 855 | // bit is same as last bit |
| 856 | rx[rxlen / 8] |= lastbit << (7-(rxlen%8)); |
| 857 | rxlen++; |
| 858 | } |
| 859 | } else { |
| 860 | // Ignore wierd value, is to small to mean anything |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | // Check if frame was captured |
| 867 | if(rxlen > 0) { |
| 868 | frame_count++; |
| 869 | if (!LogTraceHitag(rx,rxlen,response,0,reader_frame)) { |
| 870 | DbpString("Trace full"); |
| 871 | break; |
| 872 | } |
| 873 | |
| 874 | // Check if we recognize a valid authentication attempt |
| 875 | if (nbytes(rxlen) == 8) { |
| 876 | // Store the authentication attempt |
| 877 | if (auth_table_len < (AUTH_TABLE_LENGTH-8)) { |
| 878 | memcpy(auth_table+auth_table_len,rx,8); |
| 879 | auth_table_len += 8; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | // Reset the received frame and response timing info |
| 884 | memset(rx,0x00,sizeof(rx)); |
| 885 | response = 0; |
| 886 | reader_frame = false; |
| 887 | lastbit = 1; |
| 888 | bSkip = true; |
| 889 | tag_sof = 4; |
| 890 | overflow = 0; |
| 891 | |
| 892 | LED_B_OFF(); |
| 893 | LED_C_OFF(); |
| 894 | } else { |
| 895 | // Save the timer overflow, will be 0 when frame was received |
| 896 | overflow += (AT91C_BASE_TC1->TC_CV/T0); |
| 897 | } |
| 898 | // Reset the frame length |
| 899 | rxlen = 0; |
| 900 | // Reset the timer to restart while-loop that receives frames |
| 901 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG; |
| 902 | } |
| 903 | LED_A_ON(); |
| 904 | LED_B_OFF(); |
| 905 | LED_C_OFF(); |
| 906 | LED_D_OFF(); |
| 907 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; |
| 908 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; |
| 909 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
| 910 | LED_A_OFF(); |
| 911 | set_tracing(TRUE); |
| 912 | // Dbprintf("frame received: %d",frame_count); |
| 913 | // Dbprintf("Authentication Attempts: %d",(auth_table_len/8)); |
| 914 | // DbpString("All done"); |
| 915 | } |
| 916 | |
| 917 | void SimulateHitagTag(bool tag_mem_supplied, byte_t* data) { |
| 918 | int frame_count; |
| 919 | int response; |
| 920 | int overflow; |
| 921 | byte_t rx[HITAG_FRAME_LEN]; |
| 922 | size_t rxlen=0; |
| 923 | byte_t tx[HITAG_FRAME_LEN]; |
| 924 | size_t txlen=0; |
| 925 | bool bQuitTraceFull = false; |
| 926 | bQuiet = false; |
| 927 | |
| 928 | FpgaDownloadAndGo(FPGA_BITSTREAM_LF); |
| 929 | |
| 930 | // Clean up trace and prepare it for storing frames |
| 931 | set_tracing(TRUE); |
| 932 | clear_trace(); |
| 933 | |
| 934 | auth_table_len = 0; |
| 935 | auth_table_pos = 0; |
| 936 | byte_t* auth_table; |
| 937 | BigBuf_free(); |
| 938 | auth_table = (byte_t *)BigBuf_malloc(AUTH_TABLE_LENGTH); |
| 939 | memset(auth_table, 0x00, AUTH_TABLE_LENGTH); |
| 940 | |
| 941 | DbpString("Starting Hitag2 simulation"); |
| 942 | LED_D_ON(); |
| 943 | hitag2_init(); |
| 944 | |
| 945 | if (tag_mem_supplied) { |
| 946 | DbpString("Loading hitag2 memory..."); |
| 947 | memcpy((byte_t*)tag.sectors,data,48); |
| 948 | } |
| 949 | |
| 950 | uint32_t block = 0; |
| 951 | for (size_t i=0; i<12; i++) { |
| 952 | for (size_t j=0; j<4; j++) { |
| 953 | block <<= 8; |
| 954 | block |= tag.sectors[i][j]; |
| 955 | } |
| 956 | Dbprintf("| %d | %08x |",i,block); |
| 957 | } |
| 958 | |
| 959 | // Set up simulator mode, frequency divisor which will drive the FPGA |
| 960 | // and analog mux selection. |
| 961 | FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_READER_FIELD); |
| 962 | FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz |
| 963 | SetAdcMuxFor(GPIO_MUXSEL_LOPKD); |
| 964 | RELAY_OFF(); |
| 965 | |
| 966 | // Configure output pin that is connected to the FPGA (for modulating) |
| 967 | AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT; |
| 968 | AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT; |
| 969 | |
| 970 | // Disable modulation at default, which means release resistance |
| 971 | LOW(GPIO_SSC_DOUT); |
| 972 | |
| 973 | // Enable Peripheral Clock for TIMER_CLOCK0, used to measure exact timing before answering |
| 974 | AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC0); |
| 975 | |
| 976 | // Enable Peripheral Clock for TIMER_CLOCK1, used to capture edges of the reader frames |
| 977 | AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1); |
| 978 | AT91C_BASE_PIOA->PIO_BSR = GPIO_SSC_FRAME; |
| 979 | |
| 980 | // Disable timer during configuration |
| 981 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; |
| 982 | |
| 983 | // Capture mode, default timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger, |
| 984 | // external trigger rising edge, load RA on rising edge of TIOA. |
| 985 | AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK | AT91C_TC_ETRGEDG_RISING | AT91C_TC_ABETRG | AT91C_TC_LDRA_RISING; |
| 986 | |
| 987 | // Reset the received frame, frame count and timing info |
| 988 | memset(rx,0x00,sizeof(rx)); |
| 989 | frame_count = 0; |
| 990 | response = 0; |
| 991 | overflow = 0; |
| 992 | |
| 993 | // Enable and reset counter |
| 994 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; |
| 995 | |
| 996 | while(!BUTTON_PRESS() && !usb_poll_validate_length()) { |
| 997 | // Watchdog hit |
| 998 | WDT_HIT(); |
| 999 | |
| 1000 | // Receive frame, watch for at most T0*EOF periods |
| 1001 | while (AT91C_BASE_TC1->TC_CV < T0*HITAG_T_EOF) { |
| 1002 | // Check if rising edge in modulation is detected |
| 1003 | if(AT91C_BASE_TC1->TC_SR & AT91C_TC_LDRAS) { |
| 1004 | // Retrieve the new timing values |
| 1005 | int ra = (AT91C_BASE_TC1->TC_RA/T0) + overflow; |
| 1006 | overflow = 0; |
| 1007 | |
| 1008 | // Reset timer every frame, we have to capture the last edge for timing |
| 1009 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; |
| 1010 | |
| 1011 | LED_B_ON(); |
| 1012 | |
| 1013 | // Capture reader frame |
| 1014 | if(ra >= HITAG_T_STOP) { |
| 1015 | if (rxlen != 0) { |
| 1016 | //DbpString("wierd0?"); |
| 1017 | } |
| 1018 | // Capture the T0 periods that have passed since last communication or field drop (reset) |
| 1019 | response = (ra - HITAG_T_LOW); |
| 1020 | } else if(ra >= HITAG_T_1_MIN ) { |
| 1021 | // '1' bit |
| 1022 | rx[rxlen / 8] |= 1 << (7-(rxlen%8)); |
| 1023 | rxlen++; |
| 1024 | } else if(ra >= HITAG_T_0_MIN) { |
| 1025 | // '0' bit |
| 1026 | rx[rxlen / 8] |= 0 << (7-(rxlen%8)); |
| 1027 | rxlen++; |
| 1028 | } else { |
| 1029 | // Ignore wierd value, is to small to mean anything |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | // Check if frame was captured |
| 1035 | if(rxlen > 4) { |
| 1036 | frame_count++; |
| 1037 | if (!bQuiet) { |
| 1038 | if (!LogTraceHitag(rx,rxlen,response,0,true)) { |
| 1039 | DbpString("Trace full"); |
| 1040 | if (bQuitTraceFull) { |
| 1041 | break; |
| 1042 | } else { |
| 1043 | bQuiet = true; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | // Disable timer 1 with external trigger to avoid triggers during our own modulation |
| 1049 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; |
| 1050 | |
| 1051 | // Process the incoming frame (rx) and prepare the outgoing frame (tx) |
| 1052 | hitag2_handle_reader_command(rx,rxlen,tx,&txlen); |
| 1053 | |
| 1054 | // Wait for HITAG_T_WAIT_1 carrier periods after the last reader bit, |
| 1055 | // not that since the clock counts since the rising edge, but T_Wait1 is |
| 1056 | // with respect to the falling edge, we need to wait actually (T_Wait1 - T_Low) |
| 1057 | // periods. The gap time T_Low varies (4..10). All timer values are in |
| 1058 | // terms of T0 units |
| 1059 | while(AT91C_BASE_TC0->TC_CV < T0*(HITAG_T_WAIT_1-HITAG_T_LOW)); |
| 1060 | |
| 1061 | // Send and store the tag answer (if there is any) |
| 1062 | if (txlen) { |
| 1063 | // Transmit the tag frame |
| 1064 | hitag_send_frame(tx,txlen); |
| 1065 | // Store the frame in the trace |
| 1066 | if (!bQuiet) { |
| 1067 | if (!LogTraceHitag(tx,txlen,0,0,false)) { |
| 1068 | DbpString("Trace full"); |
| 1069 | if (bQuitTraceFull) { |
| 1070 | break; |
| 1071 | } else { |
| 1072 | bQuiet = true; |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | // Reset the received frame and response timing info |
| 1079 | memset(rx,0x00,sizeof(rx)); |
| 1080 | response = 0; |
| 1081 | |
| 1082 | // Enable and reset external trigger in timer for capturing future frames |
| 1083 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; |
| 1084 | LED_B_OFF(); |
| 1085 | } |
| 1086 | // Reset the frame length |
| 1087 | rxlen = 0; |
| 1088 | // Save the timer overflow, will be 0 when frame was received |
| 1089 | overflow += (AT91C_BASE_TC1->TC_CV/T0); |
| 1090 | // Reset the timer to restart while-loop that receives frames |
| 1091 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG; |
| 1092 | } |
| 1093 | LED_B_OFF(); |
| 1094 | LED_D_OFF(); |
| 1095 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; |
| 1096 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; |
| 1097 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
| 1098 | |
| 1099 | DbpString("Sim Stopped"); |
| 1100 | set_tracing(TRUE); |
| 1101 | } |
| 1102 | |
| 1103 | void ReaderHitag(hitag_function htf, hitag_data* htd) { |
| 1104 | int frame_count; |
| 1105 | int response; |
| 1106 | byte_t rx[HITAG_FRAME_LEN]; |
| 1107 | size_t rxlen=0; |
| 1108 | byte_t txbuf[HITAG_FRAME_LEN]; |
| 1109 | byte_t* tx = txbuf; |
| 1110 | size_t txlen=0; |
| 1111 | int lastbit; |
| 1112 | bool bSkip; |
| 1113 | int reset_sof; |
| 1114 | int tag_sof; |
| 1115 | int t_wait = HITAG_T_WAIT_MAX; |
| 1116 | bool bStop; |
| 1117 | bool bQuitTraceFull = false; |
| 1118 | |
| 1119 | FpgaDownloadAndGo(FPGA_BITSTREAM_LF); |
| 1120 | // Reset the return status |
| 1121 | bSuccessful = false; |
| 1122 | |
| 1123 | // Clean up trace and prepare it for storing frames |
| 1124 | set_tracing(TRUE); |
| 1125 | clear_trace(); |
| 1126 | |
| 1127 | DbpString("Starting Hitag reader family"); |
| 1128 | |
| 1129 | // Check configuration |
| 1130 | switch(htf) { |
| 1131 | case RHT2F_PASSWORD: { |
| 1132 | Dbprintf("List identifier in password mode"); |
| 1133 | memcpy(password,htd->pwd.password,4); |
| 1134 | blocknr = 0; |
| 1135 | bQuitTraceFull = false; |
| 1136 | bQuiet = false; |
| 1137 | bPwd = false; |
| 1138 | } break; |
| 1139 | |
| 1140 | case RHT2F_AUTHENTICATE: { |
| 1141 | DbpString("Authenticating using nr,ar pair:"); |
| 1142 | memcpy(NrAr,htd->auth.NrAr,8); |
| 1143 | Dbhexdump(8,NrAr,false); |
| 1144 | bQuiet = false; |
| 1145 | bCrypto = false; |
| 1146 | bAuthenticating = false; |
| 1147 | bQuitTraceFull = true; |
| 1148 | } break; |
| 1149 | |
| 1150 | case RHT2F_CRYPTO: { |
| 1151 | DbpString("Authenticating using key:"); |
| 1152 | memcpy(key,htd->crypto.key,4); //HACK; 4 or 6?? I read both in the code. |
| 1153 | Dbhexdump(6,key,false); |
| 1154 | blocknr = 0; |
| 1155 | bQuiet = false; |
| 1156 | bCrypto = false; |
| 1157 | bAuthenticating = false; |
| 1158 | bQuitTraceFull = true; |
| 1159 | } break; |
| 1160 | |
| 1161 | case RHT2F_TEST_AUTH_ATTEMPTS: { |
| 1162 | Dbprintf("Testing %d authentication attempts",(auth_table_len/8)); |
| 1163 | auth_table_pos = 0; |
| 1164 | memcpy(NrAr, auth_table, 8); |
| 1165 | bQuitTraceFull = false; |
| 1166 | bQuiet = false; |
| 1167 | bCrypto = false; |
| 1168 | } break; |
| 1169 | |
| 1170 | default: { |
| 1171 | Dbprintf("Error, unknown function: %d",htf); |
| 1172 | set_tracing(FALSE); |
| 1173 | return; |
| 1174 | } break; |
| 1175 | } |
| 1176 | |
| 1177 | LED_D_ON(); |
| 1178 | hitag2_init(); |
| 1179 | |
| 1180 | // Configure output and enable pin that is connected to the FPGA (for modulating) |
| 1181 | AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT; |
| 1182 | AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT; |
| 1183 | |
| 1184 | // Set fpga in edge detect with reader field, we can modulate as reader now |
| 1185 | FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_READER_FIELD); |
| 1186 | |
| 1187 | // Set Frequency divisor which will drive the FPGA and analog mux selection |
| 1188 | FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz |
| 1189 | SetAdcMuxFor(GPIO_MUXSEL_LOPKD); |
| 1190 | RELAY_OFF(); |
| 1191 | |
| 1192 | // Disable modulation at default, which means enable the field |
| 1193 | LOW(GPIO_SSC_DOUT); |
| 1194 | |
| 1195 | // Give it a bit of time for the resonant antenna to settle. |
| 1196 | SpinDelay(30); |
| 1197 | |
| 1198 | // Enable Peripheral Clock for TIMER_CLOCK0, used to measure exact timing before answering |
| 1199 | AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC0); |
| 1200 | |
| 1201 | // Enable Peripheral Clock for TIMER_CLOCK1, used to capture edges of the tag frames |
| 1202 | AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1); |
| 1203 | AT91C_BASE_PIOA->PIO_BSR = GPIO_SSC_FRAME; |
| 1204 | |
| 1205 | // Disable timer during configuration |
| 1206 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; |
| 1207 | |
| 1208 | // Capture mode, defaul timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger, |
| 1209 | // external trigger rising edge, load RA on falling edge of TIOA. |
| 1210 | AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK | AT91C_TC_ETRGEDG_FALLING | AT91C_TC_ABETRG | AT91C_TC_LDRA_FALLING; |
| 1211 | |
| 1212 | // Enable and reset counters |
| 1213 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; |
| 1214 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; |
| 1215 | |
| 1216 | // Reset the received frame, frame count and timing info |
| 1217 | frame_count = 0; |
| 1218 | response = 0; |
| 1219 | lastbit = 1; |
| 1220 | bStop = false; |
| 1221 | |
| 1222 | // Tag specific configuration settings (sof, timings, etc.) |
| 1223 | if (htf < 10){ |
| 1224 | // hitagS settings |
| 1225 | reset_sof = 1; |
| 1226 | t_wait = 200; |
| 1227 | DbpString("Configured for hitagS reader"); |
| 1228 | } else if (htf < 20) { |
| 1229 | // hitag1 settings |
| 1230 | reset_sof = 1; |
| 1231 | t_wait = 200; |
| 1232 | DbpString("Configured for hitag1 reader"); |
| 1233 | } else if (htf < 30) { |
| 1234 | // hitag2 settings |
| 1235 | reset_sof = 4; |
| 1236 | t_wait = HITAG_T_WAIT_2; |
| 1237 | DbpString("Configured for hitag2 reader"); |
| 1238 | } else { |
| 1239 | Dbprintf("Error, unknown hitag reader type: %d",htf); |
| 1240 | set_tracing(FALSE); |
| 1241 | return; |
| 1242 | } |
| 1243 | |
| 1244 | while(!bStop && !BUTTON_PRESS()) { |
| 1245 | // Watchdog hit |
| 1246 | WDT_HIT(); |
| 1247 | |
| 1248 | // Check if frame was captured and store it |
| 1249 | if(rxlen > 0) { |
| 1250 | frame_count++; |
| 1251 | if (!bQuiet) { |
| 1252 | if (!LogTraceHitag(rx,rxlen,response,0,false)) { |
| 1253 | DbpString("Trace full"); |
| 1254 | if (bQuitTraceFull) { |
| 1255 | break; |
| 1256 | } else { |
| 1257 | bQuiet = true; |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | // By default reset the transmission buffer |
| 1264 | tx = txbuf; |
| 1265 | switch(htf) { |
| 1266 | case RHT2F_PASSWORD: { |
| 1267 | bStop = !hitag2_password(rx,rxlen,tx,&txlen); |
| 1268 | } break; |
| 1269 | case RHT2F_AUTHENTICATE: { |
| 1270 | bStop = !hitag2_authenticate(rx,rxlen,tx,&txlen); |
| 1271 | } break; |
| 1272 | case RHT2F_CRYPTO: { |
| 1273 | bStop = !hitag2_crypto(rx,rxlen,tx,&txlen); |
| 1274 | } break; |
| 1275 | case RHT2F_TEST_AUTH_ATTEMPTS: { |
| 1276 | bStop = !hitag2_test_auth_attempts(rx,rxlen,tx,&txlen); |
| 1277 | } break; |
| 1278 | default: { |
| 1279 | Dbprintf("Error, unknown function: %d",htf); |
| 1280 | set_tracing(FALSE); |
| 1281 | return; |
| 1282 | } break; |
| 1283 | } |
| 1284 | |
| 1285 | // Send and store the reader command |
| 1286 | // Disable timer 1 with external trigger to avoid triggers during our own modulation |
| 1287 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; |
| 1288 | |
| 1289 | // Wait for HITAG_T_WAIT_2 carrier periods after the last tag bit before transmitting, |
| 1290 | // Since the clock counts since the last falling edge, a 'one' means that the |
| 1291 | // falling edge occured halfway the period. with respect to this falling edge, |
| 1292 | // we need to wait (T_Wait2 + half_tag_period) when the last was a 'one'. |
| 1293 | // All timer values are in terms of T0 units |
| 1294 | while(AT91C_BASE_TC0->TC_CV < T0*(t_wait+(HITAG_T_TAG_HALF_PERIOD*lastbit))); |
| 1295 | |
| 1296 | // Transmit the reader frame |
| 1297 | hitag_reader_send_frame(tx,txlen); |
| 1298 | |
| 1299 | // Enable and reset external trigger in timer for capturing future frames |
| 1300 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; |
| 1301 | |
| 1302 | // Add transmitted frame to total count |
| 1303 | if(txlen > 0) { |
| 1304 | frame_count++; |
| 1305 | if (!bQuiet) { |
| 1306 | // Store the frame in the trace |
| 1307 | if (!LogTraceHitag(tx,txlen,HITAG_T_WAIT_2,0,true)) { |
| 1308 | if (bQuitTraceFull) { |
| 1309 | break; |
| 1310 | } else { |
| 1311 | bQuiet = true; |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | // Reset values for receiving frames |
| 1318 | memset(rx,0x00,sizeof(rx)); |
| 1319 | rxlen = 0; |
| 1320 | lastbit = 1; |
| 1321 | bSkip = true; |
| 1322 | tag_sof = reset_sof; |
| 1323 | response = 0; |
| 1324 | |
| 1325 | // Receive frame, watch for at most T0*EOF periods |
| 1326 | while (AT91C_BASE_TC1->TC_CV < T0*HITAG_T_WAIT_MAX) { |
| 1327 | // Check if falling edge in tag modulation is detected |
| 1328 | if(AT91C_BASE_TC1->TC_SR & AT91C_TC_LDRAS) { |
| 1329 | // Retrieve the new timing values |
| 1330 | int ra = (AT91C_BASE_TC1->TC_RA/T0); |
| 1331 | |
| 1332 | // Reset timer every frame, we have to capture the last edge for timing |
| 1333 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG; |
| 1334 | |
| 1335 | LED_B_ON(); |
| 1336 | |
| 1337 | // Capture tag frame (manchester decoding using only falling edges) |
| 1338 | if(ra >= HITAG_T_EOF) { |
| 1339 | if (rxlen != 0) { |
| 1340 | //DbpString("wierd1?"); |
| 1341 | } |
| 1342 | // Capture the T0 periods that have passed since last communication or field drop (reset) |
| 1343 | // We always recieve a 'one' first, which has the falling edge after a half period |-_| |
| 1344 | response = ra-HITAG_T_TAG_HALF_PERIOD; |
| 1345 | } else if(ra >= HITAG_T_TAG_CAPTURE_FOUR_HALF) { |
| 1346 | // Manchester coding example |-_|_-|-_| (101) |
| 1347 | rx[rxlen / 8] |= 0 << (7-(rxlen%8)); |
| 1348 | rxlen++; |
| 1349 | rx[rxlen / 8] |= 1 << (7-(rxlen%8)); |
| 1350 | rxlen++; |
| 1351 | } else if(ra >= HITAG_T_TAG_CAPTURE_THREE_HALF) { |
| 1352 | // Manchester coding example |_-|...|_-|-_| (0...01) |
| 1353 | rx[rxlen / 8] |= 0 << (7-(rxlen%8)); |
| 1354 | rxlen++; |
| 1355 | // We have to skip this half period at start and add the 'one' the second time |
| 1356 | if (!bSkip) { |
| 1357 | rx[rxlen / 8] |= 1 << (7-(rxlen%8)); |
| 1358 | rxlen++; |
| 1359 | } |
| 1360 | lastbit = !lastbit; |
| 1361 | bSkip = !bSkip; |
| 1362 | } else if(ra >= HITAG_T_TAG_CAPTURE_TWO_HALF) { |
| 1363 | // Manchester coding example |_-|_-| (00) or |-_|-_| (11) |
| 1364 | if (tag_sof) { |
| 1365 | // Ignore bits that are transmitted during SOF |
| 1366 | tag_sof--; |
| 1367 | } else { |
| 1368 | // bit is same as last bit |
| 1369 | rx[rxlen / 8] |= lastbit << (7-(rxlen%8)); |
| 1370 | rxlen++; |
| 1371 | } |
| 1372 | } else { |
| 1373 | // Ignore wierd value, is to small to mean anything |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | // We can break this loop if we received the last bit from a frame |
| 1378 | if (AT91C_BASE_TC1->TC_CV > T0*HITAG_T_EOF) { |
| 1379 | if (rxlen>0) break; |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | LED_B_OFF(); |
| 1384 | LED_D_OFF(); |
| 1385 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; |
| 1386 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; |
| 1387 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
| 1388 | Dbprintf("DONE: frame received: %d",frame_count); |
| 1389 | cmd_send(CMD_ACK,bSuccessful,0,0,(byte_t*)tag.sectors,48); |
| 1390 | set_tracing(FALSE); |
| 1391 | } |