hf mf fchk - output style
[RRG-proxmark3.git] / tools / hitag2crack / crack5opencl / hitag2.h
blob964482c582b63ebd888a8cab051f377f157b1be4
1 #ifndef HITAG2_H
2 #define HITAG2_H
4 #include <stdint.h>
5 #include <stdbool.h>
7 // as the HITAG2 original implementation, with some minor changes
9 #define i4(x,a,b,c,d) ((uint32_t)((((x)>>(a))&1)<<3)|(((x)>>(b))&1)<<2|(((x)>>(c))&1)<<1|(((x)>>(d))&1))
10 #define f(state) ((0xdd3929b >> ( (((0x3c65 >> i4(state, 2, 3, 5, 6) ) & 1) <<4) \
11 | ((( 0xee5 >> i4(state, 8,12,14,15) ) & 1) <<3) \
12 | ((( 0xee5 >> i4(state,17,21,23,26) ) & 1) <<2) \
13 | ((( 0xee5 >> i4(state,28,29,31,33) ) & 1) <<1) \
14 | (((0x3c65 >> i4(state,34,43,44,46) ) & 1) ))) & 1)
16 #define get_bit(n, word) ((word >> (n)) & 1)
19 * Hitag Crypto support macros
20 * These macros reverse the bit order in a byte, or *within* each byte of a
21 * 16 , 32 or 64 bit unsigned integer. (Not across the whole 16 etc bits.)
23 #define rev8(X) ((((X) >> 7) &1) + (((X) >> 5) &2) + (((X) >> 3) &4) \
24 + (((X) >> 1) &8) + (((X) << 1) &16) + (((X) << 3) &32) \
25 + (((X) << 5) &64) + (((X) << 7) &128) )
26 #define rev16(X) (rev8 (X) + (rev8 (X >> 8) << 8))
27 #define rev32(X) (rev16(X) + (rev16(X >> 16) << 16))
28 #define rev64(X) (rev32(X) + (rev32(X >> 32) << 32))
30 typedef struct {
31 uint64_t shiftreg; // naive shift register, required for nonlinear fn input
32 uint64_t lfsr; // fast lfsr, used to make software faster
33 } Hitag_State;
35 // return a single bit from a value
36 int bitn(uint64_t x, int bit);
38 // the sub-function R that rollback depends upon
39 int fnR(uint64_t x);
41 // the three filter sub-functions that feed fnf
42 int fa(unsigned int i);
44 int fb(unsigned int i);
46 // the filter function that generates a bit of output from the prng state
47 int fnf(uint64_t s);
49 // macros to pick out 4 bits in various patterns of 1s & 2s & make a new number
50 #define pickbits2_2(S, A, B) ( ((S >> A) & 3) | ((S >> (B - 2)) & 0xC) )
51 #define pickbits1x4(S, A, B, C, D) ( ((S >> A) & 1) | ((S >> (B - 1)) & 2) | ((S >> (C - 2)) & 4) | ((S >> (D - 3)) & 8) )
52 #define pickbits1_1_2(S, A, B, C) ( ((S >> A) & 1) | ((S >> (B - 1)) & 2) | ((S >> (C - 2)) & 0xC) )
53 #define pickbits2_1_1(S, A, B, C) ( ((S >> A) & 3) | ((S >> (B - 2)) & 4) | ((S >> (C - 3)) & 8) )
54 #define pickbits1_2_1(S, A, B, C) ( ((S >> A) & 1) | ((S >> (B - 1)) & 6) | ((S >> (C - 3)) & 8) )
56 uint32_t hitag2_crypt(uint64_t x);
59 * Return up to 32 crypto bits.
60 * Last bit is in least significant bit, earlier bits are shifted left.
61 * Note that the Hitag transmission protocol is least significant bit,
62 * so we may want to change this, or add a function, that returns the
63 * crypto output bits in the other order.
65 * Parameters:
66 * Hitag_State* pstate - in/out, internal cipher state after initialisation
67 * uint32_t steps - number of bits requested, (capped at 32)
69 uint32_t hitag2_nstep(Hitag_State *pstate, uint32_t steps);
72 * Parameters:
73 * Hitag_State* pstate - output, internal state after initialisation
74 * uint64_t sharedkey - 48 bit key shared between reader & tag
75 * uint32_t serialnum - 32 bit tag serial number
76 * uint32_t initvector - 32 bit random IV from reader, part of tag authentication
78 void hitag2_init(Hitag_State *pstate, uint64_t sharedkey, uint32_t serialnum, uint32_t initvector);
80 // try_state
81 bool try_state(uint64_t s, uint32_t uid, uint32_t aR2, uint32_t nR1, uint32_t nR2, uint64_t *key);
83 #endif // HITAG2_H