mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2026-05-12 11:18:11 -07:00
hitag2crack stuff from RFidlar repo
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
ht2crack2buildtable
|
||||
ht2crack2search
|
||||
ht2crack2gentest
|
||||
|
||||
ht2crack2buildtable.exe
|
||||
ht2crack2search.exe
|
||||
ht2crack2gentest.exe
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,26 @@
|
||||
WARN=-Wall
|
||||
INCLUDE=-I../include
|
||||
CFLAGS=-c $(WARN) $(INCLUDE)
|
||||
# Linux libs
|
||||
LIBS=-pthread -D_GNU_SOURCE
|
||||
# Mac libs
|
||||
# LIBS=
|
||||
|
||||
all: ht2crack2buildtable.c ht2crack2search.c ht2crack2gentest.c hitagcrypto.o utilpart.o ht2crack2utils.o
|
||||
cc $(WARN) -o ht2crack2buildtable ht2crack2buildtable.c hitagcrypto.o ht2crack2utils.o $(LIBS)
|
||||
cc $(WARN) -o ht2crack2search ht2crack2search.c hitagcrypto.o utilpart.o ht2crack2utils.o $(LIBS)
|
||||
cc $(WARN) -o ht2crack2gentest ht2crack2gentest.c hitagcrypto.o utilpart.o ht2crack2utils.o $(LIBS)
|
||||
|
||||
ht2crack2utils.o: ht2crack2utils.c ht2crack2utils.h
|
||||
cc $(CFLAGS) ht2crack2utils.c
|
||||
|
||||
hitagcrypto.o: hitagcrypto.c hitagcrypto.h
|
||||
cc $(CFLAGS) hitagcrypto.c
|
||||
|
||||
utilpart.o: utilpart.c util.h
|
||||
cc $(CFLAGS) utilpart.c
|
||||
|
||||
clean:
|
||||
rm -rf *.o ht2crack2buildtable ht2crack2search ht2crack2gentest
|
||||
|
||||
fresh: clean all
|
||||
@@ -0,0 +1,487 @@
|
||||
/***************************************************************************
|
||||
* A copy of the GNU GPL is appended to this file. *
|
||||
* *
|
||||
* This licence is based on the nmap licence, and we express our gratitude *
|
||||
* for the work that went into producing it. There is no other connection *
|
||||
* between RFIDler and nmap either expressed or implied. *
|
||||
* *
|
||||
********************** IMPORTANT RFIDler LICENSE TERMS ********************
|
||||
* *
|
||||
* *
|
||||
* All references to RFIDler herein imply all it's derivatives, namely: *
|
||||
* *
|
||||
* o RFIDler-LF Standard *
|
||||
* o RFIDler-LF Lite *
|
||||
* o RFIDler-LF Nekkid *
|
||||
* *
|
||||
* *
|
||||
* RFIDler is (C) 2013-2015 Aperture Labs Ltd. *
|
||||
* *
|
||||
* This program is free software; you may redistribute and/or modify it *
|
||||
* under the terms of the GNU General Public License as published by the *
|
||||
* Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE *
|
||||
* CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your *
|
||||
* right to use, modify, and redistribute this software under certain *
|
||||
* conditions. If you wish to embed RFIDler technology into proprietary *
|
||||
* software or hardware, we sell alternative licenses *
|
||||
* (contact sales@aperturelabs.com). *
|
||||
* *
|
||||
* Note that the GPL places important restrictions on "derivative works", *
|
||||
* yet it does not provide a detailed definition of that term. To avoid *
|
||||
* misunderstandings, we interpret that term as broadly as copyright law *
|
||||
* allows. For example, we consider an application to constitute a *
|
||||
* derivative work for the purpose of this license if it does any of the *
|
||||
* following with any software or content covered by this license *
|
||||
* ("Covered Software"): *
|
||||
* *
|
||||
* o Integrates source code from Covered Software. *
|
||||
* *
|
||||
* o Is designed specifically to execute Covered Software and parse the *
|
||||
* results (as opposed to typical shell or execution-menu apps, which will *
|
||||
* execute anything you tell them to). *
|
||||
* *
|
||||
* o Includes Covered Software in a proprietary executable installer. The *
|
||||
* installers produced by InstallShield are an example of this. Including *
|
||||
* RFIDler with other software in compressed or archival form does not *
|
||||
* trigger this provision, provided appropriate open source decompression *
|
||||
* or de-archiving software is widely available for no charge. For the *
|
||||
* purposes of this license, an installer is considered to include Covered *
|
||||
* Software even if it actually retrieves a copy of Covered Software from *
|
||||
* another source during runtime (such as by downloading it from the *
|
||||
* Internet). *
|
||||
* *
|
||||
* o Links (statically or dynamically) to a library which does any of the *
|
||||
* above. *
|
||||
* *
|
||||
* o Executes a helper program, module, or script to do any of the above. *
|
||||
* *
|
||||
* This list is not exclusive, but is meant to clarify our interpretation *
|
||||
* of derived works with some common examples. Other people may interpret *
|
||||
* the plain GPL differently, so we consider this a special exception to *
|
||||
* the GPL that we apply to Covered Software. Works which meet any of *
|
||||
* these conditions must conform to all of the terms of this license, *
|
||||
* particularly including the GPL Section 3 requirements of providing *
|
||||
* source code and allowing free redistribution of the work as a whole. *
|
||||
* *
|
||||
* As another special exception to the GPL terms, Aperture Labs Ltd. grants*
|
||||
* permission to link the code of this program with any version of the *
|
||||
* OpenSSL library which is distributed under a license identical to that *
|
||||
* listed in the included docs/licenses/OpenSSL.txt file, and distribute *
|
||||
* linked combinations including the two. *
|
||||
* *
|
||||
* Any redistribution of Covered Software, including any derived works, *
|
||||
* must obey and carry forward all of the terms of this license, including *
|
||||
* obeying all GPL rules and restrictions. For example, source code of *
|
||||
* the whole work must be provided and free redistribution must be *
|
||||
* allowed. All GPL references to "this License", are to be treated as *
|
||||
* including the terms and conditions of this license text as well. *
|
||||
* *
|
||||
* Because this license imposes special exceptions to the GPL, Covered *
|
||||
* Work may not be combined (even as part of a larger work) with plain GPL *
|
||||
* software. The terms, conditions, and exceptions of this license must *
|
||||
* be included as well. This license is incompatible with some other open *
|
||||
* source licenses as well. In some cases we can relicense portions of *
|
||||
* RFIDler or grant special permissions to use it in other open source *
|
||||
* software. Please contact sales@aperturelabs.com with any such requests.*
|
||||
* Similarly, we don't incorporate incompatible open source software into *
|
||||
* Covered Software without special permission from the copyright holders. *
|
||||
* *
|
||||
* If you have any questions about the licensing restrictions on using *
|
||||
* RFIDler in other works, are happy to help. As mentioned above, we also *
|
||||
* offer alternative license to integrate RFIDler into proprietary *
|
||||
* applications and appliances. These contracts have been sold to dozens *
|
||||
* of software vendors, and generally include a perpetual license as well *
|
||||
* as providing for priority support and updates. They also fund the *
|
||||
* continued development of RFIDler. Please email sales@aperturelabs.com *
|
||||
* for further information. *
|
||||
* If you have received a written license agreement or contract for *
|
||||
* Covered Software stating terms other than these, you may choose to use *
|
||||
* and redistribute Covered Software under those terms instead of these. *
|
||||
* *
|
||||
* Source is provided to this software because we believe users have a *
|
||||
* right to know exactly what a program is going to do before they run it. *
|
||||
* This also allows you to audit the software for security holes (none *
|
||||
* have been found so far). *
|
||||
* *
|
||||
* Source code also allows you to port RFIDler to new platforms, fix bugs, *
|
||||
* and add new features. You are highly encouraged to send your changes *
|
||||
* to the RFIDler mailing list for possible incorporation into the *
|
||||
* main distribution. By sending these changes to Aperture Labs Ltd. or *
|
||||
* one of the Aperture Labs Ltd. development mailing lists, or checking *
|
||||
* them into the RFIDler source code repository, it is understood (unless *
|
||||
* you specify otherwise) that you are offering the RFIDler Project *
|
||||
* (Aperture Labs Ltd.) the unlimited, non-exclusive right to reuse, *
|
||||
* modify, and relicense the code. RFIDler will always be available Open *
|
||||
* Source, but this is important because the inability to relicense code *
|
||||
* has caused devastating problems for other Free Software projects (such *
|
||||
* as KDE and NASM). We also occasionally relicense the code to third *
|
||||
* parties as discussed above. If you wish to specify special license *
|
||||
* conditions of your contributions, just say so when you send them. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but *
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the RFIDler *
|
||||
* license file for more details (it's in a COPYING file included with *
|
||||
* RFIDler, and also available from *
|
||||
* https://github.com/ApertureLabsLtd/RFIDler/COPYING *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
// Author: unknown.
|
||||
// Modifications for RFIDler: Tony Naggs <tony.naggs@gmail.com>, Adam Laurie <adam@aperturelabs.com>
|
||||
|
||||
// uncomment this to build file as a standalone crypto test program
|
||||
// #define UNIT_TEST
|
||||
// also uncomment to include verbose debug prints
|
||||
// #define TEST_DEBUG
|
||||
|
||||
//#include <GenericTypeDefs.h>
|
||||
#include "HardwareProfile.h"
|
||||
#include "rfidler.h"
|
||||
#include "hitagcrypto.h"
|
||||
#include "util.h"
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if defined(UNIT_TEST) && defined(TEST_DEBUG)
|
||||
// Note that printf format %I64x prints 64 bit ints in MS Visual C/C++.
|
||||
// This may need changing for other compilers/platforms.
|
||||
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define DEBUG_PRINTF(...)
|
||||
#endif
|
||||
|
||||
|
||||
/* Brief info about NXP Hitag 1, Hitag 2, Hitag S and Hitag u (mu)
|
||||
|
||||
Hitag 125kHz RFID was created by a company called Mikron (Mikron Gesellschaft
|
||||
fur Integrierte Mikroelektronik Mbh), of Austria, for micropayment applications.
|
||||
At about the same time, late 1980s to early 1990s, Mikron developed the
|
||||
similarly featured Mifare micropayment card for 13.56MHz RFID.
|
||||
(Mikron's European Patent EP 0473569 A2 was filed 23 August 1991, with a
|
||||
priority date of 23 Aug 1990.)
|
||||
Mikron was subsequently acquired by Philips Semiconductors in 1995.
|
||||
Philips Semiconductors divsion subsequently became NXP.
|
||||
|
||||
+ Modulation read/write device -> transponder: 100 % ASK and binary pulse
|
||||
length coding
|
||||
+ Modulation transponder -> read/write device: Strong ASK modulation,
|
||||
selectable Manchester or Biphase coding
|
||||
+ Hitag S, Hitag u; anti-collision procedure
|
||||
+ Fast anti-collision protocol
|
||||
+ Hitag u; optional Cyclic Redundancy Check (CRC)
|
||||
+ Reader Talks First mode
|
||||
+ Hitag 2 & later; Transponder Talks First (TTF) mode
|
||||
+ Temporary switch from Transponder Talks First into Reader Talks First
|
||||
(RTF) Mode
|
||||
+ Data rate read/write device to transponder: 5.2 kbit/s
|
||||
+ Data rates transponder to read/write device: 2 kbit/s, 4 kbit/s, 8 kbit/s
|
||||
+ 32-bit password feature
|
||||
+ Hitag 2, S = 32-bit Unique Identifier
|
||||
+ Hitag u = 48-bit Unique Identifier
|
||||
+ Selectable password modes for reader / tag mutual authentication
|
||||
(Hitag 1 has 2 pairs of keys, later versions have 1 pair)
|
||||
+ Hitag 2 & Hitag S; Selectable encrypted mode, 48 bit key
|
||||
|
||||
Known tag types:
|
||||
|
||||
HITAG 1 2048 bits total memory
|
||||
|
||||
HITAG 2 256 Bit total memory Read/Write
|
||||
8 pages of 32 bits, inc UID (32),
|
||||
secret key (64), password (24), config (8)
|
||||
|
||||
HITAG S 32 32 bits Unique Identifier Read Only
|
||||
HITAG S 256 256 bits total memory Read/Write
|
||||
HITAG S 2048 2048 bits total memory Read/Write
|
||||
|
||||
HITAG u RO64 64 bits total memory Read Only
|
||||
HITAG u 128 bits total memory Read/Write
|
||||
HITAG u Advanced 512 bits total memory Read/Write
|
||||
HITAG u Advanced+ 1760 bits total memory Read/Write
|
||||
|
||||
Default 48-bit key for Hitag 2, S encryption:
|
||||
"MIKRON" = O N M I K R
|
||||
Key = 4F 4E 4D 49 4B 52
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// We want the crypto functions to be as fast as possible, so optimize!
|
||||
// The best compiler optimization in Microchip's free XC32 edition is -O1
|
||||
//#pragma GCC optimize("O1")
|
||||
|
||||
// private, nonlinear function to generate 1 crypto bit
|
||||
static uint32_t hitag2_crypt(uint64_t x);
|
||||
|
||||
|
||||
// macros to pick out 4 bits in various patterns of 1s & 2s & make a new number
|
||||
#define pickbits2_2(S, A, B) ( ((S >> A) & 3) | ((S >> (B - 2)) & 0xC) )
|
||||
#define pickbits1x4(S, A, B, C, D) ( ((S >> A) & 1) | ((S >> (B - 1)) & 2) | \
|
||||
((S >> (C - 2)) & 4) | ((S >> (D - 3)) & 8) )
|
||||
#define pickbits1_1_2(S, A, B, C) ( ((S >> A) & 1) | ((S >> (B - 1)) & 2) | \
|
||||
((S >> (C - 2)) & 0xC) )
|
||||
#define pickbits2_1_1(S, A, B, C) ( ((S >> A) & 3) | ((S >> (B - 2)) & 4) | \
|
||||
((S >> (C - 3)) & 8) )
|
||||
#define pickbits1_2_1(S, A, B, C) ( ((S >> A) & 1) | ((S >> (B - 1)) & 6) | \
|
||||
((S >> (C - 3)) & 8) )
|
||||
|
||||
|
||||
static uint32_t hitag2_crypt(uint64_t s)
|
||||
{
|
||||
const uint32_t ht2_function4a = 0x2C79; // 0010 1100 0111 1001
|
||||
const uint32_t ht2_function4b = 0x6671; // 0110 0110 0111 0001
|
||||
const uint32_t ht2_function5c = 0x7907287B; // 0111 1001 0000 0111 0010 1000 0111 1011
|
||||
uint32_t bitindex;
|
||||
|
||||
bitindex = (ht2_function4a >> pickbits2_2 (s, 1, 4)) & 1;
|
||||
bitindex |= ((ht2_function4b << 1) >> pickbits1_1_2 (s, 7, 11, 13)) & 0x02;
|
||||
bitindex |= ((ht2_function4b << 2) >> pickbits1x4 (s, 16, 20, 22, 25)) & 0x04;
|
||||
bitindex |= ((ht2_function4b << 3) >> pickbits2_1_1 (s, 27, 30, 32)) & 0x08;
|
||||
bitindex |= ((ht2_function4a << 4) >> pickbits1_2_1(s, 33, 42, 45)) & 0x10;
|
||||
|
||||
DEBUG_PRINTF("hitag2_crypt bitindex = %02x\n", bitindex);
|
||||
return (ht2_function5c >> bitindex) & 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parameters:
|
||||
* Hitag_State* pstate - output, internal state after initialisation
|
||||
* uint64_t sharedkey - 48 bit key shared between reader & tag
|
||||
* uint32_t serialnum - 32 bit tag serial number
|
||||
* uint32_t initvector - 32 bit random IV from reader, part of tag authentication
|
||||
*/
|
||||
void hitag2_init(Hitag_State* pstate, uint64_t sharedkey, uint32_t serialnum, uint32_t initvector)
|
||||
{
|
||||
// init state, from serial number and lowest 16 bits of shared key
|
||||
uint64_t state = ((sharedkey & 0xFFFF) << 32) | serialnum;
|
||||
|
||||
// mix the initialisation vector and highest 32 bits of the shared key
|
||||
initvector ^= (uint32_t) (sharedkey >> 16);
|
||||
|
||||
// move 16 bits from (IV xor Shared Key) to top of uint64_t state
|
||||
// these will be XORed in turn with output of the crypto function
|
||||
state |= (uint64_t) initvector << 48;
|
||||
initvector >>= 16;
|
||||
|
||||
// unrolled loop is faster on PIC32 (MIPS), do 32 times
|
||||
// shift register, then calc new bit
|
||||
state >>= 1;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
|
||||
// highest 16 bits of IV XOR Shared Key
|
||||
state |= (uint64_t) initvector << 47;
|
||||
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state ^= (uint64_t) hitag2_crypt(state) << 47;
|
||||
|
||||
DEBUG_PRINTF("hitag2_init result = %012I64x\n", state);
|
||||
pstate->shiftreg = state;
|
||||
/* naive version for reference, LFSR has 16 taps
|
||||
pstate->lfsr = state ^ (state >> 2) ^ (state >> 3) ^ (state >> 6)
|
||||
^ (state >> 7) ^ (state >> 8) ^ (state >> 16) ^ (state >> 22)
|
||||
^ (state >> 23) ^ (state >> 26) ^ (state >> 30) ^ (state >> 41)
|
||||
^ (state >> 42) ^ (state >> 43) ^ (state >> 46) ^ (state >> 47);
|
||||
*/
|
||||
{
|
||||
// optimise with one 64-bit intermediate
|
||||
uint64_t temp = state ^ (state >> 1);
|
||||
pstate->lfsr = state ^ (state >> 6) ^ (state >> 16)
|
||||
^ (state >> 26) ^ (state >> 30) ^ (state >> 41)
|
||||
^ (temp >> 2) ^ (temp >> 7) ^ (temp >> 22)
|
||||
^ (temp >> 42) ^ (temp >> 46);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Return up to 32 crypto bits.
|
||||
* Last bit is in least significant bit, earlier bits are shifted left.
|
||||
* Note that the Hitag transmission protocol is least significant bit,
|
||||
* so we may want to change this, or add a function, that returns the
|
||||
* crypto output bits in the other order.
|
||||
*
|
||||
* Parameters:
|
||||
* Hitag_State* pstate - in/out, internal cipher state after initialisation
|
||||
* uint32_t steps - number of bits requested, (capped at 32)
|
||||
*/
|
||||
uint32_t hitag2_nstep(Hitag_State* pstate, uint32_t steps)
|
||||
{
|
||||
uint64_t state = pstate->shiftreg;
|
||||
uint32_t result = 0;
|
||||
uint64_t lfsr = pstate->lfsr;
|
||||
|
||||
if (steps == 0)
|
||||
return 0;
|
||||
|
||||
// commented out the restriction on number of steps so we can step further in one go.
|
||||
// this still only returns 32 bits obviously
|
||||
// if (steps > 32)
|
||||
// steps = 32;
|
||||
|
||||
do {
|
||||
// update shift registers
|
||||
if (lfsr & 1) {
|
||||
state = (state >> 1) | 0x800000000000;
|
||||
lfsr = (lfsr >> 1) ^ 0xB38083220073;
|
||||
|
||||
// accumulate next bit of crypto
|
||||
result = (result << 1) | hitag2_crypt(state);
|
||||
} else {
|
||||
state >>= 1;
|
||||
lfsr >>= 1;
|
||||
|
||||
result = (result << 1) | hitag2_crypt(state);
|
||||
}
|
||||
} while (--steps);
|
||||
|
||||
DEBUG_PRINTF("hitag2_nstep state = %012I64x, result %02x\n", state, result);
|
||||
pstate->shiftreg = state;
|
||||
pstate->lfsr = lfsr;
|
||||
return result;
|
||||
}
|
||||
|
||||
// end of crypto core, revert to default optimization level
|
||||
//#pragma GCC reset_options
|
||||
|
||||
|
||||
/* Test code
|
||||
|
||||
Test data and below information about it comes from
|
||||
http://www.mikrocontroller.net/attachment/102194/hitag2.c
|
||||
Written by "I.C. Wiener 2006-2007"
|
||||
|
||||
"MIKRON" = O N M I K R
|
||||
Key = 4F 4E 4D 49 4B 52 - Secret 48-bit key
|
||||
Serial = 49 43 57 69 - Serial number of the tag, transmitted in clear
|
||||
Random = 65 6E 45 72 - Random IV, transmitted in clear
|
||||
~28~DC~80~31 = D7 23 7F CE - Authenticator value = inverted first 4 bytes of the keystream
|
||||
|
||||
The code below must print out "D7 23 7F CE 8C D0 37 A9 57 49 C1 E6 48 00 8A B6".
|
||||
The inverse of the first 4 bytes is sent to the tag to authenticate.
|
||||
The rest is encrypted by XORing it with the subsequent keystream.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
unsigned int hitag2_benchtest_gen32()
|
||||
{
|
||||
const uint64_t key = 0x4ad292b272f2;
|
||||
const uint32_t serial = 0x96eac292;
|
||||
const uint32_t initvec = 0x4ea276a6;
|
||||
Hitag_State state;
|
||||
|
||||
// init crypto
|
||||
hitag2_init(&state, key, serial, initvec);
|
||||
|
||||
// benchmark: generation of 32 bit stream (excludes initialisation)
|
||||
GetTimer_us(RESET);
|
||||
|
||||
(void) hitag2_nstep(&state, 32);
|
||||
|
||||
return GetTimer_us(NO_RESET);
|
||||
}
|
||||
|
||||
|
||||
unsigned int hitag2_benchtest(uint32_t count)
|
||||
{
|
||||
const uint64_t key = 0x4ad292b272f2;
|
||||
const uint32_t serial = 0x96eac292;
|
||||
const uint32_t initvec = 0x4ea276a6;
|
||||
Hitag_State state;
|
||||
uint32_t i;
|
||||
|
||||
// start timer
|
||||
GetTimer_us(RESET);
|
||||
|
||||
// benchmark: initialise crypto & generate 32 bit authentication
|
||||
// adding i stops gcc optimizer moving init function call out of loop
|
||||
for (i = 0; i < count; i++) {
|
||||
hitag2_init(&state, key, serial, initvec + i);
|
||||
(void) hitag2_nstep(&state, 32);
|
||||
}
|
||||
|
||||
return GetTimer_us(NO_RESET);
|
||||
}
|
||||
|
||||
|
||||
unsigned hitag2_verifytest()
|
||||
{
|
||||
uint8_t expected[16] = { 0xD7, 0x23, 0x7F, 0xCE, 0x8C, 0xD0, 0x37, 0xA9, 0x57, 0x49, 0xC1, 0xE6, 0x48, 0x00, 0x8A, 0xB6 };
|
||||
// key = 0x4ad292b272f2 after each byte has its bit order reversed
|
||||
// serial = 0x96eac292 ditto
|
||||
// initvec = 0x4ea276a6 ditto
|
||||
const uint64_t key = rev64 (0x524B494D4E4FUL);
|
||||
const uint32_t serial = rev32 (0x69574349);
|
||||
const uint32_t initvec = rev32 (0x72456E65);
|
||||
|
||||
uint32_t i;
|
||||
Hitag_State state;
|
||||
|
||||
// initialise
|
||||
hitag2_init(&state, key, serial, initvec);
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
// get 8 bits of keystream
|
||||
uint8_t x = (uint8_t) hitag2_nstep(&state, 8);
|
||||
uint8_t y = expected[i];
|
||||
|
||||
DEBUG_PRINTF ("%02X (%02X) \n", x, y);
|
||||
if (x != y)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
unsigned pass = hitag2_verifytest();
|
||||
|
||||
printf ("Crypto Verify test = %s\n\n", pass ? "PASS" : "FAIL");
|
||||
|
||||
if (pass) {
|
||||
hitag2_benchtest(10000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // UNIT_TEST
|
||||
@@ -0,0 +1,171 @@
|
||||
/***************************************************************************
|
||||
* A copy of the GNU GPL is appended to this file. *
|
||||
* *
|
||||
* This licence is based on the nmap licence, and we express our gratitude *
|
||||
* for the work that went into producing it. There is no other connection *
|
||||
* between RFIDler and nmap either expressed or implied. *
|
||||
* *
|
||||
********************** IMPORTANT RFIDler LICENSE TERMS ********************
|
||||
* *
|
||||
* *
|
||||
* All references to RFIDler herein imply all it's derivatives, namely: *
|
||||
* *
|
||||
* o RFIDler-LF Standard *
|
||||
* o RFIDler-LF Lite *
|
||||
* o RFIDler-LF Nekkid *
|
||||
* *
|
||||
* *
|
||||
* RFIDler is (C) 2013-2014 Aperture Labs Ltd. *
|
||||
* *
|
||||
* This program is free software; you may redistribute and/or modify it *
|
||||
* under the terms of the GNU General Public License as published by the *
|
||||
* Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE *
|
||||
* CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your *
|
||||
* right to use, modify, and redistribute this software under certain *
|
||||
* conditions. If you wish to embed RFIDler technology into proprietary *
|
||||
* software or hardware, we sell alternative licenses *
|
||||
* (contact sales@aperturelabs.com). *
|
||||
* *
|
||||
* Note that the GPL places important restrictions on "derivative works", *
|
||||
* yet it does not provide a detailed definition of that term. To avoid *
|
||||
* misunderstandings, we interpret that term as broadly as copyright law *
|
||||
* allows. For example, we consider an application to constitute a *
|
||||
* derivative work for the purpose of this license if it does any of the *
|
||||
* following with any software or content covered by this license *
|
||||
* ("Covered Software"): *
|
||||
* *
|
||||
* o Integrates source code from Covered Software. *
|
||||
* *
|
||||
* o Is designed specifically to execute Covered Software and parse the *
|
||||
* results (as opposed to typical shell or execution-menu apps, which will *
|
||||
* execute anything you tell them to). *
|
||||
* *
|
||||
* o Includes Covered Software in a proprietary executable installer. The *
|
||||
* installers produced by InstallShield are an example of this. Including *
|
||||
* RFIDler with other software in compressed or archival form does not *
|
||||
* trigger this provision, provided appropriate open source decompression *
|
||||
* or de-archiving software is widely available for no charge. For the *
|
||||
* purposes of this license, an installer is considered to include Covered *
|
||||
* Software even if it actually retrieves a copy of Covered Software from *
|
||||
* another source during runtime (such as by downloading it from the *
|
||||
* Internet). *
|
||||
* *
|
||||
* o Links (statically or dynamically) to a library which does any of the *
|
||||
* above. *
|
||||
* *
|
||||
* o Executes a helper program, module, or script to do any of the above. *
|
||||
* *
|
||||
* This list is not exclusive, but is meant to clarify our interpretation *
|
||||
* of derived works with some common examples. Other people may interpret *
|
||||
* the plain GPL differently, so we consider this a special exception to *
|
||||
* the GPL that we apply to Covered Software. Works which meet any of *
|
||||
* these conditions must conform to all of the terms of this license, *
|
||||
* particularly including the GPL Section 3 requirements of providing *
|
||||
* source code and allowing free redistribution of the work as a whole. *
|
||||
* *
|
||||
* As another special exception to the GPL terms, Aperture Labs Ltd. grants*
|
||||
* permission to link the code of this program with any version of the *
|
||||
* OpenSSL library which is distributed under a license identical to that *
|
||||
* listed in the included docs/licenses/OpenSSL.txt file, and distribute *
|
||||
* linked combinations including the two. *
|
||||
* *
|
||||
* Any redistribution of Covered Software, including any derived works, *
|
||||
* must obey and carry forward all of the terms of this license, including *
|
||||
* obeying all GPL rules and restrictions. For example, source code of *
|
||||
* the whole work must be provided and free redistribution must be *
|
||||
* allowed. All GPL references to "this License", are to be treated as *
|
||||
* including the terms and conditions of this license text as well. *
|
||||
* *
|
||||
* Because this license imposes special exceptions to the GPL, Covered *
|
||||
* Work may not be combined (even as part of a larger work) with plain GPL *
|
||||
* software. The terms, conditions, and exceptions of this license must *
|
||||
* be included as well. This license is incompatible with some other open *
|
||||
* source licenses as well. In some cases we can relicense portions of *
|
||||
* RFIDler or grant special permissions to use it in other open source *
|
||||
* software. Please contact sales@aperturelabs.com with any such requests.*
|
||||
* Similarly, we don't incorporate incompatible open source software into *
|
||||
* Covered Software without special permission from the copyright holders. *
|
||||
* *
|
||||
* If you have any questions about the licensing restrictions on using *
|
||||
* RFIDler in other works, are happy to help. As mentioned above, we also *
|
||||
* offer alternative license to integrate RFIDler into proprietary *
|
||||
* applications and appliances. These contracts have been sold to dozens *
|
||||
* of software vendors, and generally include a perpetual license as well *
|
||||
* as providing for priority support and updates. They also fund the *
|
||||
* continued development of RFIDler. Please email sales@aperturelabs.com *
|
||||
* for further information. *
|
||||
* If you have received a written license agreement or contract for *
|
||||
* Covered Software stating terms other than these, you may choose to use *
|
||||
* and redistribute Covered Software under those terms instead of these. *
|
||||
* *
|
||||
* Source is provided to this software because we believe users have a *
|
||||
* right to know exactly what a program is going to do before they run it. *
|
||||
* This also allows you to audit the software for security holes (none *
|
||||
* have been found so far). *
|
||||
* *
|
||||
* Source code also allows you to port RFIDler to new platforms, fix bugs, *
|
||||
* and add new features. You are highly encouraged to send your changes *
|
||||
* to the RFIDler mailing list for possible incorporation into the *
|
||||
* main distribution. By sending these changes to Aperture Labs Ltd. or *
|
||||
* one of the Aperture Labs Ltd. development mailing lists, or checking *
|
||||
* them into the RFIDler source code repository, it is understood (unless *
|
||||
* you specify otherwise) that you are offering the RFIDler Project *
|
||||
* (Aperture Labs Ltd.) the unlimited, non-exclusive right to reuse, *
|
||||
* modify, and relicense the code. RFIDler will always be available Open *
|
||||
* Source, but this is important because the inability to relicense code *
|
||||
* has caused devastating problems for other Free Software projects (such *
|
||||
* as KDE and NASM). We also occasionally relicense the code to third *
|
||||
* parties as discussed above. If you wish to specify special license *
|
||||
* conditions of your contributions, just say so when you send them. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but *
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the RFIDler *
|
||||
* license file for more details (it's in a COPYING file included with *
|
||||
* RFIDler, and also available from *
|
||||
* https://github.com/ApertureLabsLtd/RFIDler/COPYING *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
// Author: unknown.
|
||||
// Modifications for RFIDler: Tony Naggs <tony.naggs@gmail.com>, Adam Laurie <adam@aperturelabs.com>
|
||||
|
||||
|
||||
#ifndef HITAGCRYPTO_H
|
||||
#define HITAGCRYPTO_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
Our model of Hitag 2 crypto uses 2 parallel shift registers:
|
||||
a. 48 bit Feedback Shift Register, required for inputs to the nonlinear function.
|
||||
b. 48 bit Linear Feedback Shift Register (LFSR).
|
||||
A transform of initial register (a) value, which is then run in parallel.
|
||||
Enables much faster calculation of the feedback values.
|
||||
|
||||
API:
|
||||
void hitag2_init(Hitag_State* pstate, uint64_t sharedkey, uint32_t serialnum,
|
||||
uint32_t initvector);
|
||||
Initialise state from 48 bit shared (secret) reader/tag key,
|
||||
32 bit tag serial number and 32 bit initialisation vector from reader.
|
||||
|
||||
uint32_t hitag2_nstep(Hitag_State* pstate, uint32_t steps);
|
||||
update shift register state and generate N cipher bits (N should be <= 32)
|
||||
*/
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint64_t shiftreg; // naive shift register, required for nonlinear fn input
|
||||
uint64_t lfsr; // fast lfsr, used to make software faster
|
||||
} Hitag_State;
|
||||
|
||||
void hitag2_init(Hitag_State* pstate, uint64_t sharedkey, uint32_t serialnum, uint32_t initvector);
|
||||
|
||||
uint32_t hitag2_nstep(Hitag_State* pstate, uint32_t steps);
|
||||
|
||||
unsigned int hitag2_benchtest_gen32();
|
||||
unsigned int hitag2_benchtest(uint32_t count);
|
||||
unsigned hitag2_verifytest();
|
||||
|
||||
#endif /* HITAGCRYPTO_H */
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* ht2crack2gentests.c
|
||||
* this uses the RFIDler hitag2 PRNG code to generate test cases to test the tables
|
||||
*/
|
||||
|
||||
#include "ht2crack2utils.h"
|
||||
|
||||
|
||||
int makerandom(char *hex, unsigned int len, int fd)
|
||||
{
|
||||
unsigned char raw[32];
|
||||
int i;
|
||||
|
||||
if (!hex) {
|
||||
printf("makerandom: hex is NULL\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!len || (len > 32)) {
|
||||
printf("makerandom: len must be between 1 and 32 inclusive\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (read(fd, raw, len) != len) {
|
||||
printf("makerandom: cannot read random bytes\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i=0; i<len; i++) {
|
||||
sprintf(hex + (2 * i), "%02X", raw[i]);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Hitag_State hstate;
|
||||
char key[32];
|
||||
char uid[32];
|
||||
char nR[32];
|
||||
char filename[256];
|
||||
int i, j;
|
||||
int numtests;
|
||||
int urandomfd;
|
||||
FILE *fp;
|
||||
|
||||
if (argc < 2) {
|
||||
printf("ht2crack2gentest number\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
numtests = atoi(argv[1]);
|
||||
if (numtests <= 0) {
|
||||
printf("need positive number of tests\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
urandomfd = open("/dev/urandom", O_RDONLY);
|
||||
if (urandomfd <= 0) {
|
||||
printf("cannot open /dev/urandom\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
for (i=0; i<numtests; i++) {
|
||||
|
||||
makerandom(key, 6, urandomfd);
|
||||
makerandom(uid, 4, urandomfd);
|
||||
makerandom(nR, 4, urandomfd);
|
||||
sprintf(filename, "keystream.key-%s.uid-%s.nR-%s", key, uid, nR);
|
||||
|
||||
fp = fopen(filename, "w");
|
||||
if (!fp) {
|
||||
printf("cannot open file '%s' for writing\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
hstate.shiftreg = 0;
|
||||
hstate.lfsr = 0;
|
||||
|
||||
hitag2_init(&hstate, rev64(hexreversetoulonglong(key)), rev32(hexreversetoulong(uid)), rev32(hexreversetoulong(nR)));
|
||||
|
||||
hitag2_nstep(&hstate, 64);
|
||||
|
||||
for (j=0; j<64; j++) {
|
||||
fprintf(fp, "%08X\n", hitag2_nstep(&hstate, 32));
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,433 @@
|
||||
/*
|
||||
* ht2crack2search.c
|
||||
* this searches the sorted tables for the given RNG data, retrieves the matching
|
||||
* PRNG state, checks it is correct, and then rolls back the PRNG to recover the key
|
||||
*/
|
||||
|
||||
#include "ht2crack2utils.h"
|
||||
|
||||
|
||||
#define INPUTFILE "sorted/%02x/%02x.bin"
|
||||
#define DATASIZE 10
|
||||
|
||||
struct rngdata {
|
||||
unsigned char *data;
|
||||
int len;
|
||||
};
|
||||
|
||||
|
||||
|
||||
static int datacmp(const void *p1, const void *p2)
|
||||
{
|
||||
unsigned char *d1 = (unsigned char *)p1;
|
||||
unsigned char *d2 = (unsigned char *)p2;
|
||||
|
||||
return memcmp(d1, d2, DATASIZE - 6);
|
||||
}
|
||||
|
||||
int loadrngdata(struct rngdata *r, char *file)
|
||||
{
|
||||
int fd;
|
||||
int i, j;
|
||||
int nibble;
|
||||
struct stat filestat;
|
||||
unsigned char *data;
|
||||
|
||||
if (!r || !file) {
|
||||
printf("loadrngdata: invalid params\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
fd = open(file, O_RDONLY);
|
||||
|
||||
if (fd <= 0) {
|
||||
printf("cannot open file %s\n", file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (fstat(fd, &filestat)) {
|
||||
printf("cannot stat file %s\n", file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (filestat.st_size < 6) {
|
||||
printf("file %s is too small\n", file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
data = mmap((caddr_t)0, filestat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (data == MAP_FAILED) {
|
||||
printf("cannot mmap file %s\n", file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
r->len = filestat.st_size / 2;
|
||||
// printf("r->len = %d\n", r->len);
|
||||
|
||||
r->data = (unsigned char *)malloc(r->len);
|
||||
if (!(r->data)) {
|
||||
printf("cannot malloc\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
j = 0;
|
||||
nibble = 0;
|
||||
for (i=0; (i<filestat.st_size) && (j < r->len); i++) {
|
||||
if ((data[i] != 0x0a) && (data[i] != 0x0d) && (data[i] != 0x20)) {
|
||||
if (!nibble) {
|
||||
r->data[j] = hex2bin(data[i]) << 4;
|
||||
nibble = 1;
|
||||
} else {
|
||||
r->data[j] |= hex2bin(data[i]);
|
||||
nibble = 0;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r->len = j;
|
||||
|
||||
munmap(data, filestat.st_size);
|
||||
close(fd);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int makecand(unsigned char *c, struct rngdata *r, int bitoffset)
|
||||
{
|
||||
int bytenum;
|
||||
int bitnum;
|
||||
int i;
|
||||
|
||||
if (!c || !r || (bitoffset > ((r->len * 8) - 48))) {
|
||||
printf("makecand: invalid params\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
bytenum = bitoffset / 8;
|
||||
bitnum = bitoffset % 8;
|
||||
|
||||
for (i=0; i<6; i++) {
|
||||
if (!bitnum) {
|
||||
c[i] = r->data[bytenum + i];
|
||||
} else {
|
||||
c[i] = (r->data[bytenum + i] << bitnum) | (r->data[bytenum + i + 1] >> (8 - bitnum));
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// test the candidate against the next or previous rng data
|
||||
int testcand(unsigned char *f, unsigned char *rt, int fwd)
|
||||
{
|
||||
Hitag_State hstate;
|
||||
int i;
|
||||
uint32_t ks1;
|
||||
uint32_t ks2;
|
||||
unsigned char buf[6];
|
||||
|
||||
// build the prng state at the candidate
|
||||
hstate.shiftreg = 0;
|
||||
for (i=0; i<6; i++) {
|
||||
hstate.shiftreg = (hstate.shiftreg << 8) | f[i+4];
|
||||
}
|
||||
buildlfsr(&hstate);
|
||||
|
||||
if (fwd) {
|
||||
// roll forwards 48 bits
|
||||
hitag2_nstep(&hstate, 48);
|
||||
} else {
|
||||
// roll backwards 48 bits
|
||||
rollback(&hstate, 48);
|
||||
buildlfsr(&hstate);
|
||||
}
|
||||
|
||||
// get 48 bits of RNG from the rolled to state
|
||||
ks1 = hitag2_nstep(&hstate, 24);
|
||||
ks2 = hitag2_nstep(&hstate, 24);
|
||||
|
||||
writebuf(buf, ks1, 3);
|
||||
writebuf(buf+3, ks2, 3);
|
||||
|
||||
// compare them
|
||||
if (!memcmp(buf, rt, 6)) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int searchcand(unsigned char *c, unsigned char *rt, int fwd, unsigned char *m, unsigned char *s)
|
||||
{
|
||||
int fd;
|
||||
struct stat filestat;
|
||||
char file[64];
|
||||
unsigned char *data;
|
||||
unsigned char item[10];
|
||||
unsigned char *found = NULL;
|
||||
|
||||
|
||||
if (!c || !rt || !m || !s) {
|
||||
printf("searchcand: invalid params\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
sprintf(file, INPUTFILE, c[0], c[1]);
|
||||
|
||||
fd = open(file, O_RDONLY);
|
||||
if (fd <= 0) {
|
||||
printf("cannot open table file %s\n", file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (fstat(fd, &filestat)) {
|
||||
printf("cannot stat file %s\n", file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
data = mmap((caddr_t)0, filestat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (data == MAP_FAILED) {
|
||||
printf("cannot mmap file %s\n", file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(item, c+2, 4);
|
||||
|
||||
found = (unsigned char *)bsearch(item, data, filestat.st_size / DATASIZE, DATASIZE, datacmp);
|
||||
|
||||
if (found) {
|
||||
|
||||
// our candidate is in the table
|
||||
// go backwards and see if there are other matches
|
||||
while (((found - data) >= DATASIZE) && (!memcmp(found - DATASIZE, item, 4))) {
|
||||
found = found - DATASIZE;
|
||||
}
|
||||
|
||||
// now test all matches
|
||||
while (((found - data) <= (filestat.st_size - DATASIZE)) && (!memcmp(found, item, 4))) {
|
||||
if (testcand(found, rt, fwd)) {
|
||||
memcpy(m, c, 2);
|
||||
memcpy(m+2, found, 4);
|
||||
memcpy(s, found+4, 6);
|
||||
|
||||
munmap(data, filestat.st_size);
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
found = found + DATASIZE;
|
||||
}
|
||||
}
|
||||
|
||||
munmap(data, filestat.st_size);
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int findmatch(struct rngdata *r, unsigned char *outmatch, unsigned char *outstate, int *bitoffset)
|
||||
{
|
||||
int i;
|
||||
int bitlen;
|
||||
unsigned char cand[6];
|
||||
unsigned char rngtest[6];
|
||||
int fwd;
|
||||
|
||||
if (!r || !outmatch || !outstate || !bitoffset) {
|
||||
printf("findmatch: invalid params\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
bitlen = r->len * 8;
|
||||
|
||||
for (i=0; i<=bitlen - 48; i++) {
|
||||
// print progress
|
||||
if ((i % 100) == 0) {
|
||||
printf("searching on bit %d\n", i);
|
||||
}
|
||||
|
||||
if (!makecand(cand, r, i)) {
|
||||
printf("cannot makecand, %d\n", i);
|
||||
return 0;
|
||||
}
|
||||
// printf("cand: %02x %02x %02x %02x %02x %02x : ", cand[0], cand[1], cand[2], cand[3], cand[4], cand[5]);
|
||||
// printbin(cand);
|
||||
|
||||
/* make following or preceding RNG test data to confirm match */
|
||||
if (i < (bitlen - 96)) {
|
||||
if (!makecand(rngtest, r, i + 48)) {
|
||||
printf("cannot makecand rngtest %d + 48\n", i);
|
||||
return 0;
|
||||
}
|
||||
fwd = 1;
|
||||
} else {
|
||||
if (!makecand(rngtest, r, i - 48)) {
|
||||
printf("cannot makecand rngtest %d - 48\n", i);
|
||||
return 0;
|
||||
}
|
||||
fwd = 0;
|
||||
}
|
||||
|
||||
if (searchcand(cand, rngtest, fwd, outmatch, outstate)) {
|
||||
*bitoffset = i;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void rollbackrng(Hitag_State *hstate, unsigned char *s, int offset)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!s) {
|
||||
printf("rollbackrng: invalid params\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// build prng at recovered offset
|
||||
hstate->shiftreg = 0;
|
||||
for (i=0; i<6; i++) {
|
||||
hstate->shiftreg = (hstate->shiftreg << 8) | s[i];
|
||||
}
|
||||
|
||||
printf("recovered prng state at offset %d:\n", offset);
|
||||
printstate(hstate);
|
||||
|
||||
// rollback to state after auth
|
||||
rollback(hstate, offset);
|
||||
|
||||
// rollback through auth (aR, p3)
|
||||
rollback(hstate, 64);
|
||||
|
||||
printf("prng state after initialisation:\n");
|
||||
printstate(hstate);
|
||||
|
||||
|
||||
}
|
||||
|
||||
uint64_t recoverkey(Hitag_State *hstate, char *uidstr, char *nRstr)
|
||||
{
|
||||
uint64_t key;
|
||||
uint64_t keyupper;
|
||||
uint32_t uid;
|
||||
uint32_t uidtmp;
|
||||
uint32_t nRenc;
|
||||
uint32_t nR;
|
||||
uint32_t nRxork;
|
||||
uint32_t b = 0;
|
||||
int i;
|
||||
|
||||
// key lower 16 bits are lower 16 bits of prng state
|
||||
key = hstate->shiftreg & 0xffff;
|
||||
nRxork = (hstate->shiftreg >> 16) & 0xffffffff;
|
||||
uid = rev32(hexreversetoulong(uidstr));
|
||||
nRenc = rev32(hexreversetoulong(nRstr));
|
||||
|
||||
uidtmp = uid;
|
||||
// rollback and extract bits b
|
||||
for (i=0; i<32; i++) {
|
||||
hstate->shiftreg = ((hstate->shiftreg) << 1) | ((uidtmp >> 31) & 0x1);
|
||||
uidtmp = uidtmp << 1;
|
||||
b = (b << 1) | fnf(hstate->shiftreg);
|
||||
}
|
||||
|
||||
printf("end state:\n");
|
||||
printstate(hstate);
|
||||
printf("b:\t\t");
|
||||
printbin2(b, 32);
|
||||
printf("\n");
|
||||
printf("nRenc:\t\t");
|
||||
printbin2(nRenc, 32);
|
||||
printf("\n");
|
||||
|
||||
nR = nRenc ^ b;
|
||||
|
||||
printf("nR:\t\t");
|
||||
printbin2(nR, 32);
|
||||
printf("\n");
|
||||
|
||||
keyupper = nRxork ^ nR;
|
||||
key = key | (keyupper << 16);
|
||||
printf("key:\t\t");
|
||||
printbin2(key, 48);
|
||||
printf("\n");
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Hitag_State hstate;
|
||||
struct rngdata rng;
|
||||
int bitoffset = 0;
|
||||
unsigned char rngmatch[6];
|
||||
unsigned char rngstate[6];
|
||||
char *uidstr;
|
||||
char *nRstr;
|
||||
uint64_t keyrev;
|
||||
uint64_t key;
|
||||
int i;
|
||||
|
||||
if (argc < 4) {
|
||||
printf("ht2crack2search rngdatafile UID nR\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!loadrngdata(&rng, argv[1])) {
|
||||
printf("loadrngdata failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!strncmp(argv[2], "0x", 2)) {
|
||||
uidstr = argv[2] + 2;
|
||||
} else {
|
||||
uidstr = argv[2];
|
||||
}
|
||||
|
||||
if (!strncmp(argv[3], "0x", 2)) {
|
||||
nRstr = argv[3] + 2;
|
||||
} else {
|
||||
nRstr = argv[3];
|
||||
}
|
||||
|
||||
|
||||
if (!findmatch(&rng, rngmatch, rngstate, &bitoffset)) {
|
||||
printf("couldn't find a match\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("found match:\n");
|
||||
printf("rngmatch = %02x %02x %02x %02x %02x %02x\n", rngmatch[0], rngmatch[1], rngmatch[2], rngmatch[3], rngmatch[4], rngmatch[5]);
|
||||
printf("rngstate = %02x %02x %02x %02x %02x %02x\n", rngstate[0], rngstate[1], rngstate[2], rngstate[3], rngstate[4], rngstate[5]);
|
||||
printf("bitoffset = %d\n", bitoffset);
|
||||
|
||||
rollbackrng(&hstate, rngstate, bitoffset);
|
||||
|
||||
keyrev = recoverkey(&hstate, uidstr, nRstr);
|
||||
key = rev64(keyrev);
|
||||
|
||||
printf("keyrev:\t\t");
|
||||
printbin2(key, 48);
|
||||
printf("\n");
|
||||
|
||||
printf("KEY:\t\t");
|
||||
for (i=0; i<6; i++) {
|
||||
printf("%02X", (int)(key & 0xff));
|
||||
key = key >> 8;
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
#include "ht2crack2utils.h"
|
||||
|
||||
// writes a value into a buffer as a series of bytes
|
||||
void writebuf(unsigned char *buf, uint64_t val, unsigned int len)
|
||||
{
|
||||
int i;
|
||||
char c;
|
||||
|
||||
for (i=len-1; i>=0; i--)
|
||||
{
|
||||
c = val & 0xff;
|
||||
buf[i] = c;
|
||||
val = val >> 8;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* simple hexdump for testing purposes */
|
||||
void shexdump(unsigned char *data, int data_len)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!data || (data_len <= 0)) {
|
||||
printf("shexdump: invalid parameters\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Hexdump from %p:\n", data);
|
||||
|
||||
for (i=0; i<data_len; i++) {
|
||||
if ((i % HEX_PER_ROW) == 0) {
|
||||
printf("\n0x%04x: ", i);
|
||||
}
|
||||
printf("%02x ", data[i]);
|
||||
}
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
void printbin(unsigned char *c)
|
||||
{
|
||||
int i, j;
|
||||
unsigned char x;
|
||||
|
||||
if (!c) {
|
||||
printf("printbin: invalid params\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (i=0; i<6; i++) {
|
||||
x = c[i];
|
||||
for (j=0; j<8; j++) {
|
||||
printf("%d", (x & 0x80) >> 7);
|
||||
x = x << 1;
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
void printbin2(uint64_t val, unsigned int size)
|
||||
{
|
||||
int i;
|
||||
uint64_t mask = 1;
|
||||
|
||||
mask = mask << (size - 1);
|
||||
|
||||
for (i=0; i<size; i++) {
|
||||
if (val & mask) {
|
||||
printf("1");
|
||||
} else {
|
||||
printf("0");
|
||||
}
|
||||
val = val << 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void printstate(Hitag_State *hstate)
|
||||
{
|
||||
printf("shiftreg =\t");
|
||||
printbin2(hstate->shiftreg, 48);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// convert hex char to binary
|
||||
unsigned char hex2bin(unsigned char c)
|
||||
{
|
||||
if ((c >= '0') && (c <= '9')) {
|
||||
return (c - '0');
|
||||
} else if ((c >= 'a') && (c <= 'f')) {
|
||||
return (c - 'a' + 10);
|
||||
} else if ((c >= 'A') && (c <= 'F')) {
|
||||
return (c - 'A' + 10);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// return a single bit from a value
|
||||
int bitn(uint64_t x, int bit)
|
||||
{
|
||||
uint64_t bitmask = 1;
|
||||
|
||||
bitmask = bitmask << bit;
|
||||
|
||||
if (x & bitmask) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// the sub-function R that rollback depends upon
|
||||
int fnR(uint64_t x)
|
||||
{
|
||||
// renumbered bits because my state is 0-47, not 1-48
|
||||
return (bitn(x, 1) ^ bitn(x, 2) ^ bitn(x, 5) ^ bitn(x, 6) ^ bitn(x, 7) ^
|
||||
bitn(x, 15) ^ bitn(x, 21) ^ bitn(x, 22) ^ bitn(x, 25) ^ bitn(x, 29) ^ bitn(x, 40) ^
|
||||
bitn(x, 41) ^ bitn(x, 42) ^ bitn(x, 45) ^ bitn(x, 46) ^ bitn(x, 47));
|
||||
}
|
||||
|
||||
// the rollback function that lets us go backwards in time
|
||||
void rollback(Hitag_State *hstate, unsigned int steps)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i=0; i<steps; i++) {
|
||||
hstate->shiftreg = ((hstate->shiftreg << 1) & 0xffffffffffff) | fnR(hstate->shiftreg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// the three filter sub-functions that feed fnf
|
||||
int fa(unsigned int i)
|
||||
{
|
||||
return bitn(0x2C79, i);
|
||||
}
|
||||
|
||||
int fb(unsigned int i)
|
||||
{
|
||||
return bitn(0x6671, i);
|
||||
}
|
||||
|
||||
int fc(unsigned int i)
|
||||
{
|
||||
return bitn(0x7907287B, i);
|
||||
}
|
||||
|
||||
// the filter function that generates a bit of output from the prng state
|
||||
int fnf(uint64_t s)
|
||||
{
|
||||
unsigned int x1, x2, x3, x4, x5, x6;
|
||||
|
||||
x1 = (bitn(s, 2) << 0) | (bitn(s, 3) << 1) | (bitn(s, 5) << 2) | (bitn(s, 6) << 3);
|
||||
x2 = (bitn(s, 8) << 0) | (bitn(s, 12) << 1) | (bitn(s, 14) << 2) | (bitn(s, 15) << 3);
|
||||
x3 = (bitn(s, 17) << 0) | (bitn(s, 21) << 1) | (bitn(s, 23) << 2) | (bitn(s, 26) << 3);
|
||||
x4 = (bitn(s, 28) << 0) | (bitn(s, 29) << 1) | (bitn(s, 31) << 2) | (bitn(s, 33) << 3);
|
||||
x5 = (bitn(s, 34) << 0) | (bitn(s, 43) << 1) | (bitn(s, 44) << 2) | (bitn(s, 46) << 3);
|
||||
|
||||
x6 = (fa(x1) << 0) | (fb(x2) << 1) | (fb(x3) << 2) | (fb(x4) << 3) | (fa(x5) << 4);
|
||||
|
||||
return fc(x6);
|
||||
}
|
||||
|
||||
// builds the lfsr for the prng (quick calcs for hitag2_nstep())
|
||||
void buildlfsr(Hitag_State *hstate)
|
||||
{
|
||||
uint64_t state = hstate->shiftreg;
|
||||
uint64_t temp;
|
||||
|
||||
temp = state ^ (state >> 1);
|
||||
hstate->lfsr = state ^ (state >> 6) ^ (state >> 16)
|
||||
^ (state >> 26) ^ (state >> 30) ^ (state >> 41)
|
||||
^ (temp >> 2) ^ (temp >> 7) ^ (temp >> 22)
|
||||
^ (temp >> 42) ^ (temp >> 46);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "HardwareProfile.h"
|
||||
#include "rfidler.h"
|
||||
#include "util.h"
|
||||
|
||||
#include "hitagcrypto.h"
|
||||
|
||||
#define HEX_PER_ROW 16
|
||||
|
||||
|
||||
|
||||
void writebuf(unsigned char *buf, uint64_t val, unsigned int len);
|
||||
void shexdump(unsigned char *data, int data_len);
|
||||
void printbin(unsigned char *c);
|
||||
void printbin2(uint64_t val, unsigned int size);
|
||||
void printstate(Hitag_State *hstate);
|
||||
unsigned char hex2bin(unsigned char c);
|
||||
int bitn(uint64_t x, int bit);
|
||||
int fnR(uint64_t x);
|
||||
void rollback(Hitag_State *hstate, unsigned int steps);
|
||||
int fa(unsigned int i);
|
||||
int fb(unsigned int i);
|
||||
int fc(unsigned int i);
|
||||
int fnf(uint64_t s);
|
||||
void buildlfsr(Hitag_State *hstate);
|
||||
@@ -0,0 +1,68 @@
|
||||
ht2crack2 suite
|
||||
|
||||
|
||||
|
||||
Build
|
||||
-----
|
||||
|
||||
Edit ht2crack2buildtable.c and set the DATAMAX, NUM_BUILD_THREADS and NUM_SORT_THREADS values.
|
||||
These are important if you want it to run quickly. Ideally set DATAMAX to the largest value
|
||||
that you can get away with and set NUM_BUILD_THREADS and NUM_SORT_THREADS to the number of
|
||||
virtual cores you have available, which MUST be a power of 2. NUM_BUILD_THREADS MUST be >=
|
||||
NUM_SORT_THREADS.
|
||||
|
||||
Calculate DATAMAX = free RAM available / 65536, and then round down to a power of 10.
|
||||
|
||||
The Makefile is configured for linux. To compile on Mac, edit it and swap the LIBS= lines.
|
||||
|
||||
make clean
|
||||
make
|
||||
|
||||
|
||||
Run ht2crack2buildtable
|
||||
-----------------------
|
||||
|
||||
Make sure you are in a directory on a disk with at least 1.5TB of space.
|
||||
./ht2crack2buildtable
|
||||
|
||||
Wait a very long time. Maybe a few days.
|
||||
|
||||
This will create a directory tree called table/ while it is working that will contain
|
||||
files that will slowly build up in size to approx 20MB each. Once it has finished making
|
||||
these unsorted files, it will sort them into the directory tree sorted/ and remove the
|
||||
original files. It will then exit and you'll have your shiny table.
|
||||
|
||||
|
||||
Test with ht2crack2gentests
|
||||
---------------------------
|
||||
|
||||
./ht2crack2gentests NUMBER_OF_TESTS
|
||||
|
||||
to generate NUMBER_OF_TESTS test files. These will all be named
|
||||
keystream.key-KEYVALUE.uid-UIDVALUE.nR-NRVALUE
|
||||
|
||||
Test a single test with
|
||||
./runtest.sh KEYSTREAMFILE
|
||||
|
||||
or manually with
|
||||
./ht2crack2search KEYSTREAMFILE UIDVALUE NRVALUE
|
||||
|
||||
or run all tests with
|
||||
./runalltests.sh
|
||||
|
||||
Feel free to edit the shell scripts to find your tools. You might want to create a
|
||||
symbolic link to your sorted/ directory called 'sorted' to help ht2crack2seach find the
|
||||
table.
|
||||
|
||||
If the tests work, then the table is sound.
|
||||
|
||||
|
||||
Search for key in real keystream
|
||||
--------------------------------
|
||||
|
||||
Recover 2048 bits of keystream from the target RFID tag with the RFIDler. You will have had
|
||||
to supply an NR value and you should know the tag's UID (you can get this using the RFIDler).
|
||||
|
||||
./ht2crack2search KEYSTREAMFILE UIDVALUE NRVALUE
|
||||
|
||||
|
||||
@@ -0,0 +1,412 @@
|
||||
/***************************************************************************
|
||||
* A copy of the GNU GPL is appended to this file. *
|
||||
* *
|
||||
* This licence is based on the nmap licence, and we express our gratitude *
|
||||
* for the work that went into producing it. There is no other connection *
|
||||
* between RFIDler and nmap either expressed or implied. *
|
||||
* *
|
||||
********************** IMPORTANT RFIDler LICENSE TERMS ********************
|
||||
* *
|
||||
* *
|
||||
* All references to RFIDler herein imply all it's derivatives, namely: *
|
||||
* *
|
||||
* o RFIDler-LF Standard *
|
||||
* o RFIDler-LF Lite *
|
||||
* o RFIDler-LF Nekkid *
|
||||
* *
|
||||
* *
|
||||
* RFIDler is (C) 2013-2015 Aperture Labs Ltd. *
|
||||
* *
|
||||
* This program is free software; you may redistribute and/or modify it *
|
||||
* under the terms of the GNU General Public License as published by the *
|
||||
* Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE *
|
||||
* CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your *
|
||||
* right to use, modify, and redistribute this software under certain *
|
||||
* conditions. If you wish to embed RFIDler technology into proprietary *
|
||||
* software or hardware, we sell alternative licenses *
|
||||
* (contact sales@aperturelabs.com). *
|
||||
* *
|
||||
* Note that the GPL places important restrictions on "derivative works", *
|
||||
* yet it does not provide a detailed definition of that term. To avoid *
|
||||
* misunderstandings, we interpret that term as broadly as copyright law *
|
||||
* allows. For example, we consider an application to constitute a *
|
||||
* derivative work for the purpose of this license if it does any of the *
|
||||
* following with any software or content covered by this license *
|
||||
* ("Covered Software"): *
|
||||
* *
|
||||
* o Integrates source code from Covered Software. *
|
||||
* *
|
||||
* o Is designed specifically to execute Covered Software and parse the *
|
||||
* results (as opposed to typical shell or execution-menu apps, which will *
|
||||
* execute anything you tell them to). *
|
||||
* *
|
||||
* o Includes Covered Software in a proprietary executable installer. The *
|
||||
* installers produced by InstallShield are an example of this. Including *
|
||||
* RFIDler with other software in compressed or archival form does not *
|
||||
* trigger this provision, provided appropriate open source decompression *
|
||||
* or de-archiving software is widely available for no charge. For the *
|
||||
* purposes of this license, an installer is considered to include Covered *
|
||||
* Software even if it actually retrieves a copy of Covered Software from *
|
||||
* another source during runtime (such as by downloading it from the *
|
||||
* Internet). *
|
||||
* *
|
||||
* o Links (statically or dynamically) to a library which does any of the *
|
||||
* above. *
|
||||
* *
|
||||
* o Executes a helper program, module, or script to do any of the above. *
|
||||
* *
|
||||
* This list is not exclusive, but is meant to clarify our interpretation *
|
||||
* of derived works with some common examples. Other people may interpret *
|
||||
* the plain GPL differently, so we consider this a special exception to *
|
||||
* the GPL that we apply to Covered Software. Works which meet any of *
|
||||
* these conditions must conform to all of the terms of this license, *
|
||||
* particularly including the GPL Section 3 requirements of providing *
|
||||
* source code and allowing free redistribution of the work as a whole. *
|
||||
* *
|
||||
* As another special exception to the GPL terms, Aperture Labs Ltd. grants*
|
||||
* permission to link the code of this program with any version of the *
|
||||
* OpenSSL library which is distributed under a license identical to that *
|
||||
* listed in the included docs/licenses/OpenSSL.txt file, and distribute *
|
||||
* linked combinations including the two. *
|
||||
* *
|
||||
* Any redistribution of Covered Software, including any derived works, *
|
||||
* must obey and carry forward all of the terms of this license, including *
|
||||
* obeying all GPL rules and restrictions. For example, source code of *
|
||||
* the whole work must be provided and free redistribution must be *
|
||||
* allowed. All GPL references to "this License", are to be treated as *
|
||||
* including the terms and conditions of this license text as well. *
|
||||
* *
|
||||
* Because this license imposes special exceptions to the GPL, Covered *
|
||||
* Work may not be combined (even as part of a larger work) with plain GPL *
|
||||
* software. The terms, conditions, and exceptions of this license must *
|
||||
* be included as well. This license is incompatible with some other open *
|
||||
* source licenses as well. In some cases we can relicense portions of *
|
||||
* RFIDler or grant special permissions to use it in other open source *
|
||||
* software. Please contact sales@aperturelabs.com with any such requests.*
|
||||
* Similarly, we don't incorporate incompatible open source software into *
|
||||
* Covered Software without special permission from the copyright holders. *
|
||||
* *
|
||||
* If you have any questions about the licensing restrictions on using *
|
||||
* RFIDler in other works, are happy to help. As mentioned above, we also *
|
||||
* offer alternative license to integrate RFIDler into proprietary *
|
||||
* applications and appliances. These contracts have been sold to dozens *
|
||||
* of software vendors, and generally include a perpetual license as well *
|
||||
* as providing for priority support and updates. They also fund the *
|
||||
* continued development of RFIDler. Please email sales@aperturelabs.com *
|
||||
* for further information. *
|
||||
* If you have received a written license agreement or contract for *
|
||||
* Covered Software stating terms other than these, you may choose to use *
|
||||
* and redistribute Covered Software under those terms instead of these. *
|
||||
* *
|
||||
* Source is provided to this software because we believe users have a *
|
||||
* right to know exactly what a program is going to do before they run it. *
|
||||
* This also allows you to audit the software for security holes (none *
|
||||
* have been found so far). *
|
||||
* *
|
||||
* Source code also allows you to port RFIDler to new platforms, fix bugs, *
|
||||
* and add new features. You are highly encouraged to send your changes *
|
||||
* to the RFIDler mailing list for possible incorporation into the *
|
||||
* main distribution. By sending these changes to Aperture Labs Ltd. or *
|
||||
* one of the Aperture Labs Ltd. development mailing lists, or checking *
|
||||
* them into the RFIDler source code repository, it is understood (unless *
|
||||
* you specify otherwise) that you are offering the RFIDler Project *
|
||||
* (Aperture Labs Ltd.) the unlimited, non-exclusive right to reuse, *
|
||||
* modify, and relicense the code. RFIDler will always be available Open *
|
||||
* Source, but this is important because the inability to relicense code *
|
||||
* has caused devastating problems for other Free Software projects (such *
|
||||
* as KDE and NASM). We also occasionally relicense the code to third *
|
||||
* parties as discussed above. If you wish to specify special license *
|
||||
* conditions of your contributions, just say so when you send them. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but *
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the RFIDler *
|
||||
* license file for more details (it's in a COPYING file included with *
|
||||
* RFIDler, and also available from *
|
||||
* https://github.com/ApertureLabsLtd/RFIDler/COPYING *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
// Author: Adam Laurie <adam@aperturelabs.com>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// BCD hardware revision for usb descriptor (usb_descriptors.c)
|
||||
#define RFIDLER_HW_VERSION 0x020
|
||||
|
||||
// max sizes in BITS
|
||||
#define MAXBLOCKSIZE 512
|
||||
#define MAXTAGSIZE 4096
|
||||
#define MAXUID 512
|
||||
|
||||
#define TMP_LARGE_BUFF_LEN 2048
|
||||
#define TMP_SMALL_BUFF_LEN 256
|
||||
#define ANALOGUE_BUFF_LEN 8192
|
||||
|
||||
#define COMMS_BUFFER_SIZE 128
|
||||
|
||||
#define DETECT_BUFFER_SIZE 512
|
||||
|
||||
#define SAMPLEMASK ~(BIT_1 | BIT_0) // mask to remove two bottom bits from analogue sample - we will then use those for reader & bit period
|
||||
|
||||
// globals
|
||||
|
||||
extern BOOL WiegandOutput; // Output wiegand data whenenver UID is read
|
||||
extern BYTE *EMU_Reset_Data; // Pointer to full array of bits as bytes, stored as 0x00/0x01, '*' terminated
|
||||
extern BYTE *EMU_Data; // Pointer to current location in EMU_Reset_Data
|
||||
extern BYTE EMU_ThisBit; // The next data bit to transmit
|
||||
extern BYTE EMU_SubCarrier_T0; // Number of Frame Clocks for sub-carrier '0'
|
||||
extern BYTE EMU_SubCarrier_T1; // Number of Frame Clocks for sub-carrier '1'
|
||||
extern unsigned int EMU_Repeat; // Number of times to transmit full data set
|
||||
extern BOOL EMU_Background; // Emulate in the background until told to stop
|
||||
extern unsigned int EMU_DataBitRate; // Number of Frame Clocks per bit
|
||||
extern BYTE TmpBits[TMP_LARGE_BUFF_LEN]; // Shared scratchpad
|
||||
extern BYTE ReaderPeriod; // Flag for sample display
|
||||
extern unsigned char Comms_In_Buffer[COMMS_BUFFER_SIZE]; // USB/Serial buffer
|
||||
extern BYTE Interface; // user interface - CLI or API
|
||||
extern BYTE CommsChannel; // user comms channel - USB or UART
|
||||
extern BOOL FakeRead; // flag for analogue sampler to signal it wants access to buffers during read
|
||||
extern BOOL PWD_Mode; // is this tag password protected?
|
||||
extern BYTE Password[9]; // 32 bits as HEX string set with LOGIN
|
||||
extern unsigned int Led_Count; // LED status counter, also used for entropy
|
||||
extern unsigned long Reader_Bit_Count; // Reader ISR bit counter
|
||||
extern char Previous; // Reader ISR previous bit type
|
||||
|
||||
// RWD (read/write device) coil state
|
||||
extern BYTE RWD_State; // current state of RWD coil
|
||||
extern unsigned int RWD_Fc; // field clock in uS
|
||||
extern unsigned int RWD_Gap_Period; // length of command gaps in OC5 ticks
|
||||
extern unsigned int RWD_Zero_Period; // length of '0' in OC5 ticks
|
||||
extern unsigned int RWD_One_Period; // length of '1' in OC5 ticks
|
||||
extern unsigned int RWD_Sleep_Period; // length of initial sleep to reset tag in OC5 ticks
|
||||
extern unsigned int RWD_Wake_Period; // length required for tag to restart in OC5 ticks
|
||||
extern unsigned int RWD_Wait_Switch_TX_RX; // length to wait when switching from TX to RX in OC5 ticks
|
||||
extern unsigned int RWD_Wait_Switch_RX_TX; // length to wait when switching from RX to TX in OC5 ticks
|
||||
extern unsigned int RWD_Post_Wait; // low level ISR wait period in OC5 ticks
|
||||
extern unsigned int RWD_OC5_config; // Output Compare Module settings
|
||||
extern unsigned int RWD_OC5_r; // Output Compare Module primary compare value
|
||||
extern unsigned int RWD_OC5_rs; // Output Compare Module secondary compare value
|
||||
extern BYTE RWD_Command_Buff[TMP_SMALL_BUFF_LEN]; // Command buffer, array of bits as bytes, stored as 0x00/0x01, '*' terminated
|
||||
extern BYTE *RWD_Command_ThisBit; // Current command bit
|
||||
extern BOOL Reader_ISR_State; // current state of reader ISR
|
||||
|
||||
// NVM variables
|
||||
// timings etc. that want to survive a reboot should go here
|
||||
typedef struct {
|
||||
BYTE Name[7]; // will be set to "RFIDler" so we can test for new device
|
||||
BYTE AutoRun[128]; // optional command to run at startup
|
||||
unsigned char TagType;
|
||||
unsigned int PSK_Quality;
|
||||
unsigned int Timeout;
|
||||
unsigned int Wiegand_Pulse;
|
||||
unsigned int Wiegand_Gap;
|
||||
BOOL Wiegand_IdleState;
|
||||
unsigned int FrameClock;
|
||||
unsigned char Modulation;
|
||||
unsigned int DataRate;
|
||||
unsigned int DataRateSub0;
|
||||
unsigned int DataRateSub1;
|
||||
unsigned int DataBits;
|
||||
unsigned int DataBlocks;
|
||||
unsigned int BlockSize;
|
||||
unsigned char SyncBits;
|
||||
BYTE Sync[4];
|
||||
BOOL BiPhase;
|
||||
BOOL Invert;
|
||||
BOOL Manchester;
|
||||
BOOL HalfDuplex;
|
||||
unsigned int Repeat;
|
||||
unsigned int PotLow;
|
||||
unsigned int PotHigh;
|
||||
unsigned int RWD_Gap_Period;
|
||||
unsigned int RWD_Zero_Period;
|
||||
unsigned int RWD_One_Period;
|
||||
unsigned int RWD_Sleep_Period;
|
||||
unsigned int RWD_Wake_Period;
|
||||
unsigned int RWD_Wait_Switch_TX_RX;
|
||||
unsigned int RWD_Wait_Switch_RX_TX;
|
||||
} StoredConfig;
|
||||
|
||||
// somewhere to store TAG data. this will be interpreted according to the TAG
|
||||
// type.
|
||||
typedef struct {
|
||||
BYTE TagType; // raw tag type
|
||||
BYTE EmulatedTagType; // tag type this tag is configured to emulate
|
||||
BYTE UID[MAXUID + 1]; // Null-terminated HEX string
|
||||
BYTE Data[MAXTAGSIZE]; // raw data
|
||||
unsigned char DataBlocks; // number of blocks in Data field
|
||||
unsigned int BlockSize; // blocksize in bits
|
||||
} VirtualTag;
|
||||
|
||||
extern StoredConfig RFIDlerConfig;
|
||||
extern VirtualTag RFIDlerVTag;
|
||||
extern BYTE TmpBuff[NVM_PAGE_SIZE];
|
||||
extern BYTE DataBuff[ANALOGUE_BUFF_LEN];
|
||||
extern unsigned int DataBuffCount;
|
||||
extern const BYTE *ModulationSchemes[];
|
||||
extern const BYTE *OnOff[];
|
||||
extern const BYTE *HighLow[];
|
||||
extern const BYTE *TagTypes[];
|
||||
|
||||
// globals for ISRs
|
||||
extern BYTE EmulationMode;
|
||||
extern unsigned long HW_Bits;
|
||||
extern BYTE HW_Skip_Bits;
|
||||
extern unsigned int PSK_Min_Pulse;
|
||||
extern BOOL PSK_Read_Error;
|
||||
extern BOOL Manchester_Error;
|
||||
extern BOOL SnifferMode;
|
||||
extern unsigned int Clock_Tick_Counter;
|
||||
extern BOOL Clock_Tick_Counter_Reset;
|
||||
|
||||
// smart card lib
|
||||
#define MAX_ATR_LEN (BYTE)33
|
||||
extern BYTE scCardATR[MAX_ATR_LEN];
|
||||
extern BYTE scATRLength;
|
||||
|
||||
// RTC
|
||||
extern rtccTime RTC_time; // time structure
|
||||
extern rtccDate RTC_date; // date structure
|
||||
|
||||
// digital pots
|
||||
#define POTLOW_DEFAULT 100
|
||||
#define POTHIGH_DEFAULT 150
|
||||
#define DC_OFFSET 60 // analogue circuit DC offset (as close as we can get without using 2 LSB)
|
||||
#define VOLTS_TO_POT 0.019607843F
|
||||
|
||||
// RWD/clock states
|
||||
#define RWD_STATE_INACTIVE 0 // RWD not in use
|
||||
#define RWD_STATE_GO_TO_SLEEP 1 // RWD coil shutdown request
|
||||
#define RWD_STATE_SLEEPING 2 // RWD coil shutdown for sleep period
|
||||
#define RWD_STATE_WAKING 3 // RWD active for pre-determined period after reset
|
||||
#define RWD_STATE_START_SEND 4 // RWD starting send of data
|
||||
#define RWD_STATE_SENDING_GAP 5 // RWD sending a gap
|
||||
#define RWD_STATE_SENDING_BIT 6 // RWD sending a data bit
|
||||
#define RWD_STATE_POST_WAIT 7 // RWD finished sending data, now in forced wait period
|
||||
#define RWD_STATE_ACTIVE 8 // RWD finished, now just clocking a carrier
|
||||
|
||||
// reader ISR states
|
||||
#define READER_STOPPED 0 // reader not in use
|
||||
#define READER_IDLING 1 // reader ISR running to preserve timing, but not reading
|
||||
#define READER_RUNNING 2 // reader reading bits
|
||||
|
||||
|
||||
// user interface types
|
||||
#define INTERFACE_API 0
|
||||
#define INTERFACE_CLI 1
|
||||
|
||||
// comms channel
|
||||
#define COMMS_NONE 0
|
||||
#define COMMS_USB 1
|
||||
#define COMMS_UART 2
|
||||
|
||||
#define MAX_HISTORY 2 // disable most of history for now - memory issue
|
||||
|
||||
// tag write retries
|
||||
#define TAG_WRITE_RETRY 5
|
||||
|
||||
// modulation modes - uppdate ModulationSchemes[] in tags.c if you change this
|
||||
#define MOD_MODE_NONE 0
|
||||
#define MOD_MODE_ASK_OOK 1
|
||||
#define MOD_MODE_FSK1 2
|
||||
#define MOD_MODE_FSK2 3
|
||||
#define MOD_MODE_PSK1 4
|
||||
#define MOD_MODE_PSK2 5
|
||||
#define MOD_MODE_PSK3 6
|
||||
|
||||
// TAG types - update TagTypes[] in tags.c if you add to this list
|
||||
#define TAG_TYPE_NONE 0
|
||||
#define TAG_TYPE_ASK_RAW 1
|
||||
#define TAG_TYPE_FSK1_RAW 2
|
||||
#define TAG_TYPE_FSK2_RAW 3
|
||||
#define TAG_TYPE_PSK1_RAW 4
|
||||
#define TAG_TYPE_PSK2_RAW 5
|
||||
#define TAG_TYPE_PSK3_RAW 6
|
||||
#define TAG_TYPE_HITAG1 7
|
||||
#define TAG_TYPE_HITAG2 8
|
||||
#define TAG_TYPE_EM4X02 9
|
||||
#define TAG_TYPE_Q5 10
|
||||
#define TAG_TYPE_HID_26 11
|
||||
#define TAG_TYPE_INDALA_64 12
|
||||
#define TAG_TYPE_INDALA_224 13
|
||||
#define TAG_TYPE_UNIQUE 14
|
||||
#define TAG_TYPE_FDXB 15
|
||||
#define TAG_TYPE_T55X7 16 // same as Q5 but different timings and no modulation-defeat
|
||||
#define TAG_TYPE_AWID_26 17
|
||||
#define TAG_TYPE_EM4X05 18
|
||||
#define TAG_TYPE_TAMAGOTCHI 19
|
||||
#define TAG_TYPE_HDX 20 // same underlying data as FDX-B, but different modulation & telegram
|
||||
|
||||
// various
|
||||
|
||||
#define BINARY 0
|
||||
#define HEX 1
|
||||
|
||||
#define NO_ADDRESS -1
|
||||
|
||||
#define ACK TRUE
|
||||
#define NO_ACK FALSE
|
||||
|
||||
#define BLOCK TRUE
|
||||
#define NO_BLOCK FALSE
|
||||
|
||||
#define DATA TRUE
|
||||
#define NO_DATA FALSE
|
||||
|
||||
#define DEBUG_PIN_ON HIGH
|
||||
#define DEBUG_PIN_OFF LOW
|
||||
|
||||
#define FAST FALSE
|
||||
#define SLOW TRUE
|
||||
|
||||
#define NO_TRIGGER 0
|
||||
|
||||
#define LOCK TRUE
|
||||
#define NO_LOCK FALSE
|
||||
|
||||
#define NFC_MODE TRUE
|
||||
#define NO_NFC_MODE FALSE
|
||||
|
||||
#define ONESHOT_READ TRUE
|
||||
#define NO_ONESHOT_READ FALSE
|
||||
|
||||
#define RESET TRUE
|
||||
#define NO_RESET FALSE
|
||||
|
||||
#define SHUTDOWN_CLOCK TRUE
|
||||
#define NO_SHUTDOWN_CLOCK FALSE
|
||||
|
||||
#define SYNC TRUE
|
||||
#define NO_SYNC FALSE
|
||||
|
||||
#define VERIFY TRUE
|
||||
#define NO_VERIFY FALSE
|
||||
|
||||
#define VOLATILE FALSE
|
||||
#define NON_VOLATILE TRUE
|
||||
|
||||
#define NEWLINE TRUE
|
||||
#define NO_NEWLINE FALSE
|
||||
|
||||
#define WAIT TRUE
|
||||
#define NO_WAIT FALSE
|
||||
|
||||
#define WIPER_HIGH 0
|
||||
#define WIPER_LOW 1
|
||||
|
||||
// conversion for time to ticks
|
||||
#define US_TO_TICKS 1000000L
|
||||
#define US_OVER_10_TO_TICKS 10000000L
|
||||
#define US_OVER_100_TO_TICKS 100000000L
|
||||
// we can't get down to this level on pic, but we want to standardise on timings, so for now we fudge it
|
||||
#define CONVERT_TO_TICKS(x) ((x / 10) * (GetSystemClock() / US_OVER_10_TO_TICKS))
|
||||
#define CONVERT_TICKS_TO_US(x) (x / (GetSystemClock() / US_TO_TICKS))
|
||||
#define TIMER5_PRESCALER 16
|
||||
#define MAX_TIMER5_TICKS (65535 * TIMER5_PRESCALER)
|
||||
|
||||
// other conversions
|
||||
|
||||
// bits to hex digits
|
||||
#define HEXDIGITS(x) (x / 4)
|
||||
#define HEXTOBITS(x) (x * 4)
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
for i in keystream*; do
|
||||
./runtest.sh $i
|
||||
done
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ "$1" == "" ]; then
|
||||
echo "runtest.sh testfile"
|
||||
echo "testfile name should be of the form:"
|
||||
echo "keystream.key-KEY.uid-UID.nR-NR"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
filename=$1
|
||||
|
||||
UIDV=`echo $1 | cut -d'-' -f3 | cut -d'.' -f1`
|
||||
NR=`echo $1 | cut -d'-' -f4`
|
||||
KEYV=`echo $1 | cut -d'-' -f2 | cut -d'.' -f1`
|
||||
|
||||
echo "********************"
|
||||
echo "FILENAME = $filename"
|
||||
echo "UID = $UIDV"
|
||||
echo "NR = $NR"
|
||||
echo "Expected KEY = $KEYV"
|
||||
|
||||
./ht2crack2search $filename $UIDV $NR
|
||||
echo "Expected KEY = $KEYV"
|
||||
echo "********************"
|
||||
echo ""
|
||||
@@ -0,0 +1,206 @@
|
||||
/***************************************************************************
|
||||
* A copy of the GNU GPL is appended to this file. *
|
||||
* *
|
||||
* This licence is based on the nmap licence, and we express our gratitude *
|
||||
* for the work that went into producing it. There is no other connection *
|
||||
* between RFIDler and nmap either expressed or implied. *
|
||||
* *
|
||||
********************** IMPORTANT RFIDler LICENSE TERMS ********************
|
||||
* *
|
||||
* *
|
||||
* All references to RFIDler herein imply all it's derivatives, namely: *
|
||||
* *
|
||||
* o RFIDler-LF Standard *
|
||||
* o RFIDler-LF Lite *
|
||||
* o RFIDler-LF Nekkid *
|
||||
* *
|
||||
* *
|
||||
* RFIDler is (C) 2013-2015 Aperture Labs Ltd. *
|
||||
* *
|
||||
* This program is free software; you may redistribute and/or modify it *
|
||||
* under the terms of the GNU General Public License as published by the *
|
||||
* Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE *
|
||||
* CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your *
|
||||
* right to use, modify, and redistribute this software under certain *
|
||||
* conditions. If you wish to embed RFIDler technology into proprietary *
|
||||
* software or hardware, we sell alternative licenses *
|
||||
* (contact sales@aperturelabs.com). *
|
||||
* *
|
||||
* Note that the GPL places important restrictions on "derivative works", *
|
||||
* yet it does not provide a detailed definition of that term. To avoid *
|
||||
* misunderstandings, we interpret that term as broadly as copyright law *
|
||||
* allows. For example, we consider an application to constitute a *
|
||||
* derivative work for the purpose of this license if it does any of the *
|
||||
* following with any software or content covered by this license *
|
||||
* ("Covered Software"): *
|
||||
* *
|
||||
* o Integrates source code from Covered Software. *
|
||||
* *
|
||||
* o Is designed specifically to execute Covered Software and parse the *
|
||||
* results (as opposed to typical shell or execution-menu apps, which will *
|
||||
* execute anything you tell them to). *
|
||||
* *
|
||||
* o Includes Covered Software in a proprietary executable installer. The *
|
||||
* installers produced by InstallShield are an example of this. Including *
|
||||
* RFIDler with other software in compressed or archival form does not *
|
||||
* trigger this provision, provided appropriate open source decompression *
|
||||
* or de-archiving software is widely available for no charge. For the *
|
||||
* purposes of this license, an installer is considered to include Covered *
|
||||
* Software even if it actually retrieves a copy of Covered Software from *
|
||||
* another source during runtime (such as by downloading it from the *
|
||||
* Internet). *
|
||||
* *
|
||||
* o Links (statically or dynamically) to a library which does any of the *
|
||||
* above. *
|
||||
* *
|
||||
* o Executes a helper program, module, or script to do any of the above. *
|
||||
* *
|
||||
* This list is not exclusive, but is meant to clarify our interpretation *
|
||||
* of derived works with some common examples. Other people may interpret *
|
||||
* the plain GPL differently, so we consider this a special exception to *
|
||||
* the GPL that we apply to Covered Software. Works which meet any of *
|
||||
* these conditions must conform to all of the terms of this license, *
|
||||
* particularly including the GPL Section 3 requirements of providing *
|
||||
* source code and allowing free redistribution of the work as a whole. *
|
||||
* *
|
||||
* As another special exception to the GPL terms, Aperture Labs Ltd. grants*
|
||||
* permission to link the code of this program with any version of the *
|
||||
* OpenSSL library which is distributed under a license identical to that *
|
||||
* listed in the included docs/licenses/OpenSSL.txt file, and distribute *
|
||||
* linked combinations including the two. *
|
||||
* *
|
||||
* Any redistribution of Covered Software, including any derived works, *
|
||||
* must obey and carry forward all of the terms of this license, including *
|
||||
* obeying all GPL rules and restrictions. For example, source code of *
|
||||
* the whole work must be provided and free redistribution must be *
|
||||
* allowed. All GPL references to "this License", are to be treated as *
|
||||
* including the terms and conditions of this license text as well. *
|
||||
* *
|
||||
* Because this license imposes special exceptions to the GPL, Covered *
|
||||
* Work may not be combined (even as part of a larger work) with plain GPL *
|
||||
* software. The terms, conditions, and exceptions of this license must *
|
||||
* be included as well. This license is incompatible with some other open *
|
||||
* source licenses as well. In some cases we can relicense portions of *
|
||||
* RFIDler or grant special permissions to use it in other open source *
|
||||
* software. Please contact sales@aperturelabs.com with any such requests.*
|
||||
* Similarly, we don't incorporate incompatible open source software into *
|
||||
* Covered Software without special permission from the copyright holders. *
|
||||
* *
|
||||
* If you have any questions about the licensing restrictions on using *
|
||||
* RFIDler in other works, are happy to help. As mentioned above, we also *
|
||||
* offer alternative license to integrate RFIDler into proprietary *
|
||||
* applications and appliances. These contracts have been sold to dozens *
|
||||
* of software vendors, and generally include a perpetual license as well *
|
||||
* as providing for priority support and updates. They also fund the *
|
||||
* continued development of RFIDler. Please email sales@aperturelabs.com *
|
||||
* for further information. *
|
||||
* If you have received a written license agreement or contract for *
|
||||
* Covered Software stating terms other than these, you may choose to use *
|
||||
* and redistribute Covered Software under those terms instead of these. *
|
||||
* *
|
||||
* Source is provided to this software because we believe users have a *
|
||||
* right to know exactly what a program is going to do before they run it. *
|
||||
* This also allows you to audit the software for security holes (none *
|
||||
* have been found so far). *
|
||||
* *
|
||||
* Source code also allows you to port RFIDler to new platforms, fix bugs, *
|
||||
* and add new features. You are highly encouraged to send your changes *
|
||||
* to the RFIDler mailing list for possible incorporation into the *
|
||||
* main distribution. By sending these changes to Aperture Labs Ltd. or *
|
||||
* one of the Aperture Labs Ltd. development mailing lists, or checking *
|
||||
* them into the RFIDler source code repository, it is understood (unless *
|
||||
* you specify otherwise) that you are offering the RFIDler Project *
|
||||
* (Aperture Labs Ltd.) the unlimited, non-exclusive right to reuse, *
|
||||
* modify, and relicense the code. RFIDler will always be available Open *
|
||||
* Source, but this is important because the inability to relicense code *
|
||||
* has caused devastating problems for other Free Software projects (such *
|
||||
* as KDE and NASM). We also occasionally relicense the code to third *
|
||||
* parties as discussed above. If you wish to specify special license *
|
||||
* conditions of your contributions, just say so when you send them. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but *
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the RFIDler *
|
||||
* license file for more details (it's in a COPYING file included with *
|
||||
* RFIDler, and also available from *
|
||||
* https://github.com/ApertureLabsLtd/RFIDler/COPYING *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
// Author: Adam Laurie <adam@aperturelabs.com>
|
||||
|
||||
#define CRC16_MASK_CCITT 0x1021 // CRC-CCITT mask (ISO 3309, used in X25, HDLC)
|
||||
#define CRC16_MASK_ISO_11785 0x8408 // ISO 11785 animal tags
|
||||
#define CRC16_MASK_CRC16 0xA001 // standard CRC16 mask (used in ARC files)
|
||||
|
||||
/*
|
||||
* Hitag Crypto support macros
|
||||
* These macros reverse the bit order in a byte, or *within* each byte of a
|
||||
* 16 , 32 or 64 bit unsigned integer. (Not across the whole 16 etc bits.)
|
||||
*/
|
||||
#define rev8(X) ((((X) >> 7) &1) + (((X) >> 5) &2) + (((X) >> 3) &4) \
|
||||
+ (((X) >> 1) &8) + (((X) << 1) &16) + (((X) << 3) &32) \
|
||||
+ (((X) << 5) &64) + (((X) << 7) &128) )
|
||||
#define rev16(X) (rev8 (X) + (rev8 (X >> 8) << 8))
|
||||
#define rev32(X) (rev16(X) + (rev16(X >> 16) << 16))
|
||||
#define rev64(X) (rev32(X) + (rev32(X >> 32) << 32))
|
||||
|
||||
|
||||
BYTE approx(unsigned long number, unsigned long target, unsigned char percentage);
|
||||
unsigned int bcdtouint(BYTE *bcd, BYTE length);
|
||||
unsigned long long bcdtoulonglong(BYTE *bcd, BYTE length);
|
||||
void inttobinarray(BYTE *target, unsigned int source, unsigned int bits);
|
||||
void ulongtobinarray(BYTE *target, unsigned long source, unsigned int bits);
|
||||
void ulonglongtobinarray(BYTE *target, unsigned long long source, unsigned int bits);
|
||||
void inttobinstring(BYTE *target, unsigned int source, unsigned int bits);
|
||||
void ulongtobinstring(BYTE *target, unsigned long source, unsigned int bits);
|
||||
BOOL ulongtohex(BYTE *target, unsigned long source);
|
||||
unsigned int binarraytoint(BYTE *bin, BYTE length);
|
||||
unsigned long long binarraytolonglong(BYTE *bin, BYTE length);
|
||||
unsigned long binarraytoulong(BYTE *bin, BYTE length);
|
||||
BYTE hextobyte(BYTE *hex);
|
||||
void printhexreadable(BYTE *hex, BYTE maxlength);
|
||||
unsigned long hextoulong(BYTE *hex);
|
||||
unsigned long hexreversetoulong(BYTE *hex);
|
||||
unsigned long long hextoulonglong(BYTE *hex);
|
||||
unsigned long long hexreversetoulonglong(BYTE *hex);
|
||||
char hextolonglong(unsigned long long *out, unsigned char *hex);
|
||||
unsigned int hextobinarray(unsigned char *target, unsigned char *source);
|
||||
unsigned int hextobinstring(unsigned char *target, unsigned char *source);
|
||||
unsigned int binarraytohex(unsigned char *target, unsigned char *source, unsigned int length);
|
||||
void hexprintbinarray(BYTE *bin, unsigned int length);
|
||||
unsigned int binstringtohex(unsigned char *target, unsigned char *source);
|
||||
unsigned int binstringtobinarray(BYTE *target, BYTE *source);
|
||||
void binstringtobyte(BYTE *target, unsigned char *source, BYTE length);
|
||||
void binarraytobinstring(BYTE *target, BYTE *source, unsigned int length);
|
||||
void printhexasbin(unsigned char *hex);
|
||||
void printbinashex(unsigned char *bin);
|
||||
void invertbinarray(BYTE *target, BYTE *source, unsigned int length);
|
||||
void invertbinstring(BYTE *target, BYTE *source);
|
||||
void printbinarray(unsigned char *bin, unsigned int length);
|
||||
unsigned char getbit(unsigned char byte, unsigned char bit);
|
||||
void bytestohex(unsigned char *target, unsigned char *source, unsigned int length);
|
||||
unsigned int manchester_encode(unsigned char *target, unsigned char *source, unsigned int length);
|
||||
unsigned int manchester_decode(unsigned char *target, unsigned char *source, unsigned int length);
|
||||
char * strip_newline(char *buff);
|
||||
BOOL command_ack(BOOL data);
|
||||
BOOL command_nack(BYTE *reason);
|
||||
BOOL command_unknown(void);
|
||||
void ToUpper(char *string);
|
||||
void string_reverse(unsigned char *string, unsigned int length);
|
||||
BOOL string_byteswap(unsigned char *string, unsigned int length);
|
||||
BYTE parity(unsigned char *string, BYTE type, unsigned int length);
|
||||
unsigned long get_reader_pulse(unsigned int timeout_us);
|
||||
unsigned long get_reader_gap(unsigned int timeout_us);
|
||||
unsigned int crc_ccitt(BYTE *data, unsigned int length);
|
||||
unsigned int crc16(unsigned int crc, BYTE *data, unsigned int length, unsigned int mask);
|
||||
void space_indent(BYTE count);
|
||||
void xml_version(void);
|
||||
void xml_header(BYTE *item, BYTE *indent);
|
||||
void xml_footer(BYTE *item, BYTE *indent, BOOL newline);
|
||||
void xml_indented_text(BYTE *data, BYTE indent);
|
||||
void xml_item_text(BYTE *item, BYTE *data, BYTE *indent);
|
||||
void xml_item_decimal(BYTE *item, BYTE num, BYTE *indent);
|
||||
void xml_indented_array(BYTE *data, BYTE mask, unsigned int length, BYTE indent);
|
||||
void xml_item_array(BYTE *item, BYTE *data, BYTE mask, unsigned int length, BYTE *indent);
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
/***************************************************************************
|
||||
* A copy of the GNU GPL is appended to this file. *
|
||||
* *
|
||||
* This licence is based on the nmap licence, and we express our gratitude *
|
||||
* for the work that went into producing it. There is no other connection *
|
||||
* between RFIDler and nmap either expressed or implied. *
|
||||
* *
|
||||
********************** IMPORTANT RFIDler LICENSE TERMS ********************
|
||||
* *
|
||||
* *
|
||||
* All references to RFIDler herein imply all it's derivatives, namely: *
|
||||
* *
|
||||
* o RFIDler-LF Standard *
|
||||
* o RFIDler-LF Lite *
|
||||
* o RFIDler-LF Nekkid *
|
||||
* *
|
||||
* *
|
||||
* RFIDler is (C) 2013-2014 Aperture Labs Ltd. *
|
||||
* *
|
||||
* This program is free software; you may redistribute and/or modify it *
|
||||
* under the terms of the GNU General Public License as published by the *
|
||||
* Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE *
|
||||
* CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your *
|
||||
* right to use, modify, and redistribute this software under certain *
|
||||
* conditions. If you wish to embed RFIDler technology into proprietary *
|
||||
* software or hardware, we sell alternative licenses *
|
||||
* (contact sales@aperturelabs.com). *
|
||||
* *
|
||||
* Note that the GPL places important restrictions on "derivative works", *
|
||||
* yet it does not provide a detailed definition of that term. To avoid *
|
||||
* misunderstandings, we interpret that term as broadly as copyright law *
|
||||
* allows. For example, we consider an application to constitute a *
|
||||
* derivative work for the purpose of this license if it does any of the *
|
||||
* following with any software or content covered by this license *
|
||||
* ("Covered Software"): *
|
||||
* *
|
||||
* o Integrates source code from Covered Software. *
|
||||
* *
|
||||
* o Is designed specifically to execute Covered Software and parse the *
|
||||
* results (as opposed to typical shell or execution-menu apps, which will *
|
||||
* execute anything you tell them to). *
|
||||
* *
|
||||
* o Includes Covered Software in a proprietary executable installer. The *
|
||||
* installers produced by InstallShield are an example of this. Including *
|
||||
* RFIDler with other software in compressed or archival form does not *
|
||||
* trigger this provision, provided appropriate open source decompression *
|
||||
* or de-archiving software is widely available for no charge. For the *
|
||||
* purposes of this license, an installer is considered to include Covered *
|
||||
* Software even if it actually retrieves a copy of Covered Software from *
|
||||
* another source during runtime (such as by downloading it from the *
|
||||
* Internet). *
|
||||
* *
|
||||
* o Links (statically or dynamically) to a library which does any of the *
|
||||
* above. *
|
||||
* *
|
||||
* o Executes a helper program, module, or script to do any of the above. *
|
||||
* *
|
||||
* This list is not exclusive, but is meant to clarify our interpretation *
|
||||
* of derived works with some common examples. Other people may interpret *
|
||||
* the plain GPL differently, so we consider this a special exception to *
|
||||
* the GPL that we apply to Covered Software. Works which meet any of *
|
||||
* these conditions must conform to all of the terms of this license, *
|
||||
* particularly including the GPL Section 3 requirements of providing *
|
||||
* source code and allowing free redistribution of the work as a whole. *
|
||||
* *
|
||||
* As another special exception to the GPL terms, Aperture Labs Ltd. grants*
|
||||
* permission to link the code of this program with any version of the *
|
||||
* OpenSSL library which is distributed under a license identical to that *
|
||||
* listed in the included docs/licenses/OpenSSL.txt file, and distribute *
|
||||
* linked combinations including the two. *
|
||||
* *
|
||||
* Any redistribution of Covered Software, including any derived works, *
|
||||
* must obey and carry forward all of the terms of this license, including *
|
||||
* obeying all GPL rules and restrictions. For example, source code of *
|
||||
* the whole work must be provided and free redistribution must be *
|
||||
* allowed. All GPL references to "this License", are to be treated as *
|
||||
* including the terms and conditions of this license text as well. *
|
||||
* *
|
||||
* Because this license imposes special exceptions to the GPL, Covered *
|
||||
* Work may not be combined (even as part of a larger work) with plain GPL *
|
||||
* software. The terms, conditions, and exceptions of this license must *
|
||||
* be included as well. This license is incompatible with some other open *
|
||||
* source licenses as well. In some cases we can relicense portions of *
|
||||
* RFIDler or grant special permissions to use it in other open source *
|
||||
* software. Please contact sales@aperturelabs.com with any such requests.*
|
||||
* Similarly, we don't incorporate incompatible open source software into *
|
||||
* Covered Software without special permission from the copyright holders. *
|
||||
* *
|
||||
* If you have any questions about the licensing restrictions on using *
|
||||
* RFIDler in other works, are happy to help. As mentioned above, we also *
|
||||
* offer alternative license to integrate RFIDler into proprietary *
|
||||
* applications and appliances. These contracts have been sold to dozens *
|
||||
* of software vendors, and generally include a perpetual license as well *
|
||||
* as providing for priority support and updates. They also fund the *
|
||||
* continued development of RFIDler. Please email sales@aperturelabs.com *
|
||||
* for further information. *
|
||||
* If you have received a written license agreement or contract for *
|
||||
* Covered Software stating terms other than these, you may choose to use *
|
||||
* and redistribute Covered Software under those terms instead of these. *
|
||||
* *
|
||||
* Source is provided to this software because we believe users have a *
|
||||
* right to know exactly what a program is going to do before they run it. *
|
||||
* This also allows you to audit the software for security holes (none *
|
||||
* have been found so far). *
|
||||
* *
|
||||
* Source code also allows you to port RFIDler to new platforms, fix bugs, *
|
||||
* and add new features. You are highly encouraged to send your changes *
|
||||
* to the RFIDler mailing list for possible incorporation into the *
|
||||
* main distribution. By sending these changes to Aperture Labs Ltd. or *
|
||||
* one of the Aperture Labs Ltd. development mailing lists, or checking *
|
||||
* them into the RFIDler source code repository, it is understood (unless *
|
||||
* you specify otherwise) that you are offering the RFIDler Project *
|
||||
* (Aperture Labs Ltd.) the unlimited, non-exclusive right to reuse, *
|
||||
* modify, and relicense the code. RFIDler will always be available Open *
|
||||
* Source, but this is important because the inability to relicense code *
|
||||
* has caused devastating problems for other Free Software projects (such *
|
||||
* as KDE and NASM). We also occasionally relicense the code to third *
|
||||
* parties as discussed above. If you wish to specify special license *
|
||||
* conditions of your contributions, just say so when you send them. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but *
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the RFIDler *
|
||||
* license file for more details (it's in a COPYING file included with *
|
||||
* RFIDler, and also available from *
|
||||
* https://github.com/ApertureLabsLtd/RFIDler/COPYING *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
// Author: Adam Laurie <adam@aperturelabs.com>
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "HardwareProfile.h"
|
||||
#include "util.h"
|
||||
#include "rfidler.h"
|
||||
//#include "comms.h"
|
||||
|
||||
// rtc
|
||||
rtccTime RTC_time; // time structure
|
||||
rtccDate RTC_date; // date structure
|
||||
|
||||
// convert byte-reversed 8 digit hex to unsigned long
|
||||
unsigned long hexreversetoulong(BYTE *hex)
|
||||
{
|
||||
unsigned long ret= 0L;
|
||||
unsigned int x;
|
||||
BYTE i;
|
||||
|
||||
if(strlen(hex) != 8)
|
||||
return 0L;
|
||||
|
||||
for(i= 0 ; i < 4 ; ++i)
|
||||
{
|
||||
if(sscanf(hex, "%2X", &x) != 1)
|
||||
return 0L;
|
||||
ret += ((unsigned long) x) << i * 8;
|
||||
hex += 2;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// convert byte-reversed 12 digit hex to unsigned long
|
||||
unsigned long long hexreversetoulonglong(BYTE *hex)
|
||||
{
|
||||
unsigned long long ret= 0LL;
|
||||
BYTE tmp[9];
|
||||
|
||||
// this may seem an odd way to do it, but weird compiler issues were
|
||||
// breaking direct conversion!
|
||||
|
||||
tmp[8]= '\0';
|
||||
memset(tmp + 4, '0', 4);
|
||||
memcpy(tmp, hex + 8, 4);
|
||||
ret= hexreversetoulong(tmp);
|
||||
ret <<= 32;
|
||||
memcpy(tmp, hex, 8);
|
||||
ret += hexreversetoulong(tmp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
ht2crack3
|
||||
ht2test
|
||||
|
||||
ht2crack3.exe
|
||||
ht2test.exe
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
WARN=-Wall
|
||||
INCLUDE=-I../include
|
||||
CFLAGS=-c $(WARN) $(INCLUDE)
|
||||
LIBS=
|
||||
|
||||
all: ht2crack3.c ht2test.c hitagcrypto.o utilpart.o
|
||||
cc $(WARN) -o ht2crack3 ht2crack3.c hitagcrypto.o utilpart.o -lpthread $(LIBS)
|
||||
cc $(WARN) -o ht2test ht2test.c hitagcrypto.o utilpart.o $(LIBS)
|
||||
|
||||
hitagcrypto.o: hitagcrypto.c hitagcrypto.h
|
||||
cc $(CFLAGS) hitagcrypto.c
|
||||
|
||||
utilpart.o: utilpart.c util.h
|
||||
cc $(CFLAGS) utilpart.c
|
||||
|
||||
clean:
|
||||
rm -rf *.o ht2crack3 ht2test
|
||||
|
||||
fresh: clean all
|
||||
@@ -0,0 +1,485 @@
|
||||
/***************************************************************************
|
||||
* A copy of the GNU GPL is appended to this file. *
|
||||
* *
|
||||
* This licence is based on the nmap licence, and we express our gratitude *
|
||||
* for the work that went into producing it. There is no other connection *
|
||||
* between RFIDler and nmap either expressed or implied. *
|
||||
* *
|
||||
********************** IMPORTANT RFIDler LICENSE TERMS ********************
|
||||
* *
|
||||
* *
|
||||
* All references to RFIDler herein imply all it's derivatives, namely: *
|
||||
* *
|
||||
* o RFIDler-LF Standard *
|
||||
* o RFIDler-LF Lite *
|
||||
* o RFIDler-LF Nekkid *
|
||||
* *
|
||||
* *
|
||||
* RFIDler is (C) 2013-2015 Aperture Labs Ltd. *
|
||||
* *
|
||||
* This program is free software; you may redistribute and/or modify it *
|
||||
* under the terms of the GNU General Public License as published by the *
|
||||
* Free Software Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE *
|
||||
* CLARIFICATIONS AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your *
|
||||
* right to use, modify, and redistribute this software under certain *
|
||||
* conditions. If you wish to embed RFIDler technology into proprietary *
|
||||
* software or hardware, we sell alternative licenses *
|
||||
* (contact sales@aperturelabs.com). *
|
||||
* *
|
||||
* Note that the GPL places important restrictions on "derivative works", *
|
||||
* yet it does not provide a detailed definition of that term. To avoid *
|
||||
* misunderstandings, we interpret that term as broadly as copyright law *
|
||||
* allows. For example, we consider an application to constitute a *
|
||||
* derivative work for the purpose of this license if it does any of the *
|
||||
* following with any software or content covered by this license *
|
||||
* ("Covered Software"): *
|
||||
* *
|
||||
* o Integrates source code from Covered Software. *
|
||||
* *
|
||||
* o Is designed specifically to execute Covered Software and parse the *
|
||||
* results (as opposed to typical shell or execution-menu apps, which will *
|
||||
* execute anything you tell them to). *
|
||||
* *
|
||||
* o Includes Covered Software in a proprietary executable installer. The *
|
||||
* installers produced by InstallShield are an example of this. Including *
|
||||
* RFIDler with other software in compressed or archival form does not *
|
||||
* trigger this provision, provided appropriate open source decompression *
|
||||
* or de-archiving software is widely available for no charge. For the *
|
||||
* purposes of this license, an installer is considered to include Covered *
|
||||
* Software even if it actually retrieves a copy of Covered Software from *
|
||||
* another source during runtime (such as by downloading it from the *
|
||||
* Internet). *
|
||||
* *
|
||||
* o Links (statically or dynamically) to a library which does any of the *
|
||||
* above. *
|
||||
* *
|
||||
* o Executes a helper program, module, or script to do any of the above. *
|
||||
* *
|
||||
* This list is not exclusive, but is meant to clarify our interpretation *
|
||||
* of derived works with some common examples. Other people may interpret *
|
||||
* the plain GPL differently, so we consider this a special exception to *
|
||||
* the GPL that we apply to Covered Software. Works which meet any of *
|
||||
* these conditions must conform to all of the terms of this license, *
|
||||
* particularly including the GPL Section 3 requirements of providing *
|
||||
* source code and allowing free redistribution of the work as a whole. *
|
||||
* *
|
||||
* As another special exception to the GPL terms, Aperture Labs Ltd. grants*
|
||||
* permission to link the code of this program with any version of the *
|
||||
* OpenSSL library which is distributed under a license identical to that *
|
||||
* listed in the included docs/licenses/OpenSSL.txt file, and distribute *
|
||||
* linked combinations including the two. *
|
||||
* *
|
||||
* Any redistribution of Covered Software, including any derived works, *
|
||||
* must obey and carry forward all of the terms of this license, including *
|
||||
* obeying all GPL rules and restrictions. For example, source code of *
|
||||
* the whole work must be provided and free redistribution must be *
|
||||
* allowed. All GPL references to "this License", are to be treated as *
|
||||
* including the terms and conditions of this license text as well. *
|
||||
* *
|
||||
* Because this license imposes special exceptions to the GPL, Covered *
|
||||
* Work may not be combined (even as part of a larger work) with plain GPL *
|
||||
* software. The terms, conditions, and exceptions of this license must *
|
||||
* be included as well. This license is incompatible with some other open *
|
||||
* source licenses as well. In some cases we can relicense portions of *
|
||||
* RFIDler or grant special permissions to use it in other open source *
|
||||
* software. Please contact sales@aperturelabs.com with any such requests.*
|
||||
* Similarly, we don't incorporate incompatible open source software into *
|
||||
* Covered Software without special permission from the copyright holders. *
|
||||
* *
|
||||
* If you have any questions about the licensing restrictions on using *
|
||||
* RFIDler in other works, are happy to help. As mentioned above, we also *
|
||||
* offer alternative license to integrate RFIDler into proprietary *
|
||||
* applications and appliances. These contracts have been sold to dozens *
|
||||
* of software vendors, and generally include a perpetual license as well *
|
||||
* as providing for priority support and updates. They also fund the *
|
||||
* continued development of RFIDler. Please email sales@aperturelabs.com *
|
||||
* for further information. *
|
||||
* If you have received a written license agreement or contract for *
|
||||
* Covered Software stating terms other than these, you may choose to use *
|
||||
* and redistribute Covered Software under those terms instead of these. *
|
||||
* *
|
||||
* Source is provided to this software because we believe users have a *
|
||||
* right to know exactly what a program is going to do before they run it. *
|
||||
* This also allows you to audit the software for security holes (none *
|
||||
* have been found so far). *
|
||||
* *
|
||||
* Source code also allows you to port RFIDler to new platforms, fix bugs, *
|
||||
* and add new features. You are highly encouraged to send your changes *
|
||||
* to the RFIDler mailing list for possible incorporation into the *
|
||||
* main distribution. By sending these changes to Aperture Labs Ltd. or *
|
||||
* one of the Aperture Labs Ltd. development mailing lists, or checking *
|
||||
* them into the RFIDler source code repository, it is understood (unless *
|
||||
* you specify otherwise) that you are offering the RFIDler Project *
|
||||
* (Aperture Labs Ltd.) the unlimited, non-exclusive right to reuse, *
|
||||
* modify, and relicense the code. RFIDler will always be available Open *
|
||||
* Source, but this is important because the inability to relicense code *
|
||||
* has caused devastating problems for other Free Software projects (such *
|
||||
* as KDE and NASM). We also occasionally relicense the code to third *
|
||||
* parties as discussed above. If you wish to specify special license *
|
||||
* conditions of your contributions, just say so when you send them. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but *
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the RFIDler *
|
||||
* license file for more details (it's in a COPYING file included with *
|
||||
* RFIDler, and also available from *
|
||||
* https://github.com/ApertureLabsLtd/RFIDler/COPYING *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
// Author: unknown.
|
||||
// Modifications for RFIDler: Tony Naggs <tony.naggs@gmail.com>, Adam Laurie <adam@aperturelabs.com>
|
||||
|
||||
// uncomment this to build file as a standalone crypto test program
|
||||
// #define UNIT_TEST
|
||||
// also uncomment to include verbose debug prints
|
||||
// #define TEST_DEBUG
|
||||
|
||||
//#include <GenericTypeDefs.h>
|
||||
#include "HardwareProfile.h"
|
||||
#include "rfidler.h"
|
||||
#include "hitagcrypto.h"
|
||||
#include "util.h"
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if defined(UNIT_TEST) && defined(TEST_DEBUG)
|
||||
// Note that printf format %I64x prints 64 bit ints in MS Visual C/C++.
|
||||
// This may need changing for other compilers/platforms.
|
||||
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define DEBUG_PRINTF(...)
|
||||
#endif
|
||||
|
||||
|
||||
/* Brief info about NXP Hitag 1, Hitag 2, Hitag S and Hitag u (mu)
|
||||
|
||||
Hitag 125kHz RFID was created by a company called Mikron (Mikron Gesellschaft
|
||||
fur Integrierte Mikroelektronik Mbh), of Austria, for micropayment applications.
|
||||
At about the same time, late 1980s to early 1990s, Mikron developed the
|
||||
similarly featured Mifare micropayment card for 13.56MHz RFID.
|
||||
(Mikron's European Patent EP 0473569 A2 was filed 23 August 1991, with a
|
||||
priority date of 23 Aug 1990.)
|
||||
Mikron was subsequently acquired by Philips Semiconductors in 1995.
|
||||
Philips Semiconductors divsion subsequently became NXP.
|
||||
|
||||
+ Modulation read/write device -> transponder: 100 % ASK and binary pulse
|
||||
length coding
|
||||
+ Modulation transponder -> read/write device: Strong ASK modulation,
|
||||
selectable Manchester or Biphase coding
|
||||
+ Hitag S, Hitag u; anti-collision procedure
|
||||
+ Fast anti-collision protocol
|
||||
+ Hitag u; optional Cyclic Redundancy Check (CRC)
|
||||
+ Reader Talks First mode
|
||||
+ Hitag 2 & later; Transponder Talks First (TTF) mode
|
||||
+ Temporary switch from Transponder Talks First into Reader Talks First
|
||||
(RTF) Mode
|
||||
+ Data rate read/write device to transponder: 5.2 kbit/s
|
||||
+ Data rates transponder to read/write device: 2 kbit/s, 4 kbit/s, 8 kbit/s
|
||||
+ 32-bit password feature
|
||||
+ Hitag 2, S = 32-bit Unique Identifier
|
||||
+ Hitag u = 48-bit Unique Identifier
|
||||
+ Selectable password modes for reader / tag mutual authentication
|
||||
(Hitag 1 has 2 pairs of keys, later versions have 1 pair)
|
||||
+ Hitag 2 & Hitag S; Selectable encrypted mode, 48 bit key
|
||||
|
||||
Known tag types:
|
||||
|
||||
HITAG 1 2048 bits total memory
|
||||
|
||||
HITAG 2 256 Bit total memory Read/Write
|
||||
8 pages of 32 bits, inc UID (32),
|
||||
secret key (64), password (24), config (8)
|
||||
|
||||
HITAG S 32 32 bits Unique Identifier Read Only
|
||||
HITAG S 256 256 bits total memory Read/Write
|
||||
HITAG S 2048 2048 bits total memory Read/Write
|
||||
|
||||
HITAG u RO64 64 bits total memory Read Only
|
||||
HITAG u 128 bits total memory Read/Write
|
||||
HITAG u Advanced 512 bits total memory Read/Write
|
||||
HITAG u Advanced+ 1760 bits total memory Read/Write
|
||||
|
||||
Default 48-bit key for Hitag 2, S encryption:
|
||||
"MIKRON" = O N M I K R
|
||||
Key = 4F 4E 4D 49 4B 52
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// We want the crypto functions to be as fast as possible, so optimize!
|
||||
// The best compiler optimization in Microchip's free XC32 edition is -O1
|
||||
#pragma GCC optimize("O1")
|
||||
|
||||
// private, nonlinear function to generate 1 crypto bit
|
||||
static uint32_t hitag2_crypt(uint64_t x);
|
||||
|
||||
|
||||
// macros to pick out 4 bits in various patterns of 1s & 2s & make a new number
|
||||
#define pickbits2_2(S, A, B) ( ((S >> A) & 3) | ((S >> (B - 2)) & 0xC) )
|
||||
#define pickbits1x4(S, A, B, C, D) ( ((S >> A) & 1) | ((S >> (B - 1)) & 2) | \
|
||||
((S >> (C - 2)) & 4) | ((S >> (D - 3)) & 8) )
|
||||
#define pickbits1_1_2(S, A, B, C) ( ((S >> A) & 1) | ((S >> (B - 1)) & 2) | \
|
||||
((S >> (C - 2)) & 0xC) )
|
||||
#define pickbits2_1_1(S, A, B, C) ( ((S >> A) & 3) | ((S >> (B - 2)) & 4) | \
|
||||
((S >> (C - 3)) & 8) )
|
||||
#define pickbits1_2_1(S, A, B, C) ( ((S >> A) & 1) | ((S >> (B - 1)) & 6) | \
|
||||
((S >> (C - 3)) & 8) )
|
||||
|
||||
|
||||
static uint32_t hitag2_crypt(uint64_t s)
|
||||
{
|
||||
const uint32_t ht2_function4a = 0x2C79; // 0010 1100 0111 1001
|
||||
const uint32_t ht2_function4b = 0x6671; // 0110 0110 0111 0001
|
||||
const uint32_t ht2_function5c = 0x7907287B; // 0111 1001 0000 0111 0010 1000 0111 1011
|
||||
uint32_t bitindex;
|
||||
|
||||
bitindex = (ht2_function4a >> pickbits2_2 (s, 1, 4)) & 1;
|
||||
bitindex |= ((ht2_function4b << 1) >> pickbits1_1_2 (s, 7, 11, 13)) & 0x02;
|
||||
bitindex |= ((ht2_function4b << 2) >> pickbits1x4 (s, 16, 20, 22, 25)) & 0x04;
|
||||
bitindex |= ((ht2_function4b << 3) >> pickbits2_1_1 (s, 27, 30, 32)) & 0x08;
|
||||
bitindex |= ((ht2_function4a << 4) >> pickbits1_2_1(s, 33, 42, 45)) & 0x10;
|
||||
|
||||
DEBUG_PRINTF("hitag2_crypt bitindex = %02x\n", bitindex);
|
||||
return (ht2_function5c >> bitindex) & 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parameters:
|
||||
* Hitag_State* pstate - output, internal state after initialisation
|
||||
* uint64_t sharedkey - 48 bit key shared between reader & tag
|
||||
* uint32_t serialnum - 32 bit tag serial number
|
||||
* uint32_t initvector - 32 bit random IV from reader, part of tag authentication
|
||||
*/
|
||||
void hitag2_init(Hitag_State* pstate, uint64_t sharedkey, uint32_t serialnum, uint32_t initvector)
|
||||
{
|
||||
// init state, from serial number and lowest 16 bits of shared key
|
||||
uint64_t state = ((sharedkey & 0xFFFF) << 32) | serialnum;
|
||||
|
||||
// mix the initialisation vector and highest 32 bits of the shared key
|
||||
initvector ^= (uint32_t) (sharedkey >> 16);
|
||||
|
||||
// move 16 bits from (IV xor Shared Key) to top of uint64_t state
|
||||
// these will be XORed in turn with output of the crypto function
|
||||
state |= (uint64_t) initvector << 48;
|
||||
initvector >>= 16;
|
||||
|
||||
// unrolled loop is faster on PIC32 (MIPS), do 32 times
|
||||
// shift register, then calc new bit
|
||||
state >>= 1;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
|
||||
// highest 16 bits of IV XOR Shared Key
|
||||
state |= (uint64_t) initvector << 47;
|
||||
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state = (state >> 1) ^ (uint64_t) hitag2_crypt(state) << 46;
|
||||
state ^= (uint64_t) hitag2_crypt(state) << 47;
|
||||
|
||||
DEBUG_PRINTF("hitag2_init result = %012I64x\n", state);
|
||||
pstate->shiftreg = state;
|
||||
/* naive version for reference, LFSR has 16 taps
|
||||
pstate->lfsr = state ^ (state >> 2) ^ (state >> 3) ^ (state >> 6)
|
||||
^ (state >> 7) ^ (state >> 8) ^ (state >> 16) ^ (state >> 22)
|
||||
^ (state >> 23) ^ (state >> 26) ^ (state >> 30) ^ (state >> 41)
|
||||
^ (state >> 42) ^ (state >> 43) ^ (state >> 46) ^ (state >> 47);
|
||||
*/
|
||||
{
|
||||
// optimise with one 64-bit intermediate
|
||||
uint64_t temp = state ^ (state >> 1);
|
||||
pstate->lfsr = state ^ (state >> 6) ^ (state >> 16)
|
||||
^ (state >> 26) ^ (state >> 30) ^ (state >> 41)
|
||||
^ (temp >> 2) ^ (temp >> 7) ^ (temp >> 22)
|
||||
^ (temp >> 42) ^ (temp >> 46);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Return up to 32 crypto bits.
|
||||
* Last bit is in least significant bit, earlier bits are shifted left.
|
||||
* Note that the Hitag transmission protocol is least significant bit,
|
||||
* so we may want to change this, or add a function, that returns the
|
||||
* crypto output bits in the other order.
|
||||
*
|
||||
* Parameters:
|
||||
* Hitag_State* pstate - in/out, internal cipher state after initialisation
|
||||
* uint32_t steps - number of bits requested, (capped at 32)
|
||||
*/
|
||||
uint32_t hitag2_nstep(Hitag_State* pstate, uint32_t steps)
|
||||
{
|
||||
uint64_t state = pstate->shiftreg;
|
||||
uint32_t result = 0;
|
||||
uint64_t lfsr = pstate->lfsr;
|
||||
|
||||
if (steps == 0)
|
||||
return 0;
|
||||
|
||||
// if (steps > 32)
|
||||
// steps = 32;
|
||||
|
||||
do {
|
||||
// update shift registers
|
||||
if (lfsr & 1) {
|
||||
state = (state >> 1) | 0x800000000000;
|
||||
lfsr = (lfsr >> 1) ^ 0xB38083220073;
|
||||
|
||||
// accumulate next bit of crypto
|
||||
result = (result << 1) | hitag2_crypt(state);
|
||||
} else {
|
||||
state >>= 1;
|
||||
lfsr >>= 1;
|
||||
|
||||
result = (result << 1) | hitag2_crypt(state);
|
||||
}
|
||||
} while (--steps);
|
||||
|
||||
DEBUG_PRINTF("hitag2_nstep state = %012I64x, result %02x\n", state, result);
|
||||
pstate->shiftreg = state;
|
||||
pstate->lfsr = lfsr;
|
||||
return result;
|
||||
}
|
||||
|
||||
// end of crypto core, revert to default optimization level
|
||||
#pragma GCC reset_options
|
||||
|
||||
|
||||
/* Test code
|
||||
|
||||
Test data and below information about it comes from
|
||||
http://www.mikrocontroller.net/attachment/102194/hitag2.c
|
||||
Written by "I.C. Wiener 2006-2007"
|
||||
|
||||
"MIKRON" = O N M I K R
|
||||
Key = 4F 4E 4D 49 4B 52 - Secret 48-bit key
|
||||
Serial = 49 43 57 69 - Serial number of the tag, transmitted in clear
|
||||
Random = 65 6E 45 72 - Random IV, transmitted in clear
|
||||
~28~DC~80~31 = D7 23 7F CE - Authenticator value = inverted first 4 bytes of the keystream
|
||||
|
||||
The code below must print out "D7 23 7F CE 8C D0 37 A9 57 49 C1 E6 48 00 8A B6".
|
||||
The inverse of the first 4 bytes is sent to the tag to authenticate.
|
||||
The rest is encrypted by XORing it with the subsequent keystream.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
unsigned int hitag2_benchtest_gen32()
|
||||
{
|
||||
const uint64_t key = 0x4ad292b272f2;
|
||||
const uint32_t serial = 0x96eac292;
|
||||
const uint32_t initvec = 0x4ea276a6;
|
||||
Hitag_State state;
|
||||
|
||||
// init crypto
|
||||
hitag2_init(&state, key, serial, initvec);
|
||||
|
||||
// benchmark: generation of 32 bit stream (excludes initialisation)
|
||||
GetTimer_us(RESET);
|
||||
|
||||
(void) hitag2_nstep(&state, 32);
|
||||
|
||||
return GetTimer_us(NO_RESET);
|
||||
}
|
||||
|
||||
|
||||
unsigned int hitag2_benchtest(uint32_t count)
|
||||
{
|
||||
const uint64_t key = 0x4ad292b272f2;
|
||||
const uint32_t serial = 0x96eac292;
|
||||
const uint32_t initvec = 0x4ea276a6;
|
||||
Hitag_State state;
|
||||
uint32_t i;
|
||||
|
||||
// start timer
|
||||
GetTimer_us(RESET);
|
||||
|
||||
// benchmark: initialise crypto & generate 32 bit authentication
|
||||
// adding i stops gcc optimizer moving init function call out of loop
|
||||
for (i = 0; i < count; i++) {
|
||||
hitag2_init(&state, key, serial, initvec + i);
|
||||
(void) hitag2_nstep(&state, 32);
|
||||
}
|
||||
|
||||
return GetTimer_us(NO_RESET);
|
||||
}
|
||||
|
||||
|
||||
unsigned hitag2_verifytest()
|
||||
{
|
||||
uint8_t expected[16] = { 0xD7, 0x23, 0x7F, 0xCE, 0x8C, 0xD0, 0x37, 0xA9, 0x57, 0x49, 0xC1, 0xE6, 0x48, 0x00, 0x8A, 0xB6 };
|
||||
// key = 0x4ad292b272f2 after each byte has its bit order reversed
|
||||
// serial = 0x96eac292 ditto
|
||||
// initvec = 0x4ea276a6 ditto
|
||||
const uint64_t key = rev64 (0x524B494D4E4FUL);
|
||||
const uint32_t serial = rev32 (0x69574349);
|
||||
const uint32_t initvec = rev32 (0x72456E65);
|
||||
|
||||
uint32_t i;
|
||||
Hitag_State state;
|
||||
|
||||
// initialise
|
||||
hitag2_init(&state, key, serial, initvec);
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
// get 8 bits of keystream
|
||||
uint8_t x = (uint8_t) hitag2_nstep(&state, 8);
|
||||
uint8_t y = expected[i];
|
||||
|
||||
DEBUG_PRINTF ("%02X (%02X) \n", x, y);
|
||||
if (x != y)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
unsigned pass = hitag2_verifytest();
|
||||
|
||||
printf ("Crypto Verify test = %s\n\n", pass ? "PASS" : "FAIL");
|
||||
|
||||
if (pass) {
|
||||
hitag2_benchtest(10000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // UNIT_TEST
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user