1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
17 //-----------------------------------------------------------------------------
18 #include "hitag2_crypto.h"
22 #include "commonutil.h"
29 /* Following is a modified version of cryptolib.com/ciphers/hitag2/ */
30 // Software optimized 48-bit Philips/NXP Mifare Hitag2 PCF7936/46/47/52 stream cipher algorithm by I.C. Wiener 2006-2007.
31 // For educational purposes only.
32 // No warranties or guarantees of any kind.
33 // This code is released into the public domain by its author.
36 // Single bit Hitag2 functions:
38 #define i4(x,a,b,c,d) ((uint32_t)((((x)>>(a))&1)+(((x)>>(b))&1)*2+(((x)>>(c))&1)*4+(((x)>>(d))&1)*8))
41 static const uint32_t ht2_f4a
= 0x2C79; // 0010 1100 0111 1001
42 static const uint32_t ht2_f4b
= 0x6671; // 0110 0110 0111 0001
43 static const uint32_t ht2_f5c
= 0x7907287B; // 0111 1001 0000 0111 0010 1000 0111 1011
45 static uint32_t ht2_f20(const uint64_t state
) {
47 uint32_t i5
= ((ht2_f4a
>> i4(state
, 1, 2, 4, 5)) & 1) * 1
48 + ((ht2_f4b
>> i4(state
, 7, 11, 13, 14)) & 1) * 2
49 + ((ht2_f4b
>> i4(state
, 16, 20, 22, 25)) & 1) * 4
50 + ((ht2_f4b
>> i4(state
, 27, 28, 30, 32)) & 1) * 8
51 + ((ht2_f4a
>> i4(state
, 33, 42, 43, 45)) & 1) * 16;
53 return (ht2_f5c
>> i5
) & 1;
56 // return a single bit from a value
57 static int ht2_bitn(uint64_t x
, int bit
) {
58 const uint64_t bitmask
= (uint64_t)(1) << bit
;
59 return (x
& bitmask
) ? 1 : 0;
62 // the sub-function R that rollback depends upon
63 int ht2_fnR(uint64_t state
) {
64 // renumbered bits because my state is 0-47, not 1-48
66 ht2_bitn(state
, 1) ^ ht2_bitn(state
, 2) ^ ht2_bitn(state
, 5) ^
67 ht2_bitn(state
, 6) ^ ht2_bitn(state
, 7) ^ ht2_bitn(state
, 15) ^
68 ht2_bitn(state
, 21) ^ ht2_bitn(state
, 22) ^ ht2_bitn(state
, 25) ^
69 ht2_bitn(state
, 29) ^ ht2_bitn(state
, 40) ^ ht2_bitn(state
, 41) ^
70 ht2_bitn(state
, 42) ^ ht2_bitn(state
, 45) ^ ht2_bitn(state
, 46) ^
76 static void ht2_rollback(hitag_state_t *hstate, unsigned int steps) {
77 for (int i = 0; i < steps; i++) {
78 hstate->shiftreg = ((hstate->shiftreg << 1) & 0xffffffffffff) | ht2_fnR(hstate->shiftreg);
82 // the rollback function that lets us go backwards in time
83 void ht2_rollback(hitag_state_t
*hstate
, uint32_t steps
) {
84 for (uint32_t i
= 0; i
< steps
; i
++) {
85 hstate
->shiftreg
= ((hstate
->shiftreg
<< 1) & 0xffffffffffff) | ht2_fnR(hstate
->shiftreg
);
86 hstate
->lfsr
= LFSR_INV(hstate
->lfsr
);
90 // the three filter sub-functions that feed fnf
91 #define ht2_fa(x) ht2_bitn(0x2C79, (x))
92 #define ht2_fb(x) ht2_bitn(0x6671, (x))
93 #define ht2_fc(x) ht2_bitn(0x7907287B, (x))
95 // the filter function that generates a bit of output from the prng state
96 int ht2_fnf(uint64_t state
) {
98 uint32_t x1
= (ht2_bitn(state
, 2) << 0) | (ht2_bitn(state
, 3) << 1) | (ht2_bitn(state
, 5) << 2) | (ht2_bitn(state
, 6) << 3);
99 uint32_t x2
= (ht2_bitn(state
, 8) << 0) | (ht2_bitn(state
, 12) << 1) | (ht2_bitn(state
, 14) << 2) | (ht2_bitn(state
, 15) << 3);
100 uint32_t x3
= (ht2_bitn(state
, 17) << 0) | (ht2_bitn(state
, 21) << 1) | (ht2_bitn(state
, 23) << 2) | (ht2_bitn(state
, 26) << 3);
101 uint32_t x4
= (ht2_bitn(state
, 28) << 0) | (ht2_bitn(state
, 29) << 1) | (ht2_bitn(state
, 31) << 2) | (ht2_bitn(state
, 33) << 3);
102 uint32_t x5
= (ht2_bitn(state
, 34) << 0) | (ht2_bitn(state
, 43) << 1) | (ht2_bitn(state
, 44) << 2) | (ht2_bitn(state
, 46) << 3);
104 uint32_t x6
= (ht2_fa(x1
) << 0) | (ht2_fb(x2
) << 1) | (ht2_fb(x3
) << 2) | (ht2_fb(x4
) << 3) | (ht2_fa(x5
) << 4);
108 // builds the lfsr for the prng (quick calcs for hitag2_nstep())
110 static void ht2_buildlfsr(hitag_state_t *hstate) {
111 if (hstate == NULL) {
115 uint64_t state = hstate->shiftreg;
116 uint64_t temp = state ^ (state >> 1);
117 hstate->lfsr = state ^ (state >> 6) ^ (state >> 16)
118 ^ (state >> 26) ^ (state >> 30) ^ (state >> 41)
119 ^ (temp >> 2) ^ (temp >> 7) ^ (temp >> 22)
120 ^ (temp >> 42) ^ (temp >> 46);
127 uint64_t ht2_recoverkey(hitag_state_t
*hstate
, uint32_t uid
, uint32_t nRenc
) {
129 // hstate->shiftreg = (uint64_t)(((hstate->shiftreg << 1) & 0xffffffffffff) | (uint64_t)ht2_fnR(hstate->shiftreg));
130 // hstate->shiftreg = (uint64_t)(((hstate->shiftreg << 1) & 0xffffffffffff) | (uint64_t)ht2_fnR(hstate->shiftreg));
133 PrintAndLogEx(INFO
, "shiftreg.... %" PRIx64
, hstate
->shiftreg
);
136 // key lower 16 bits are lower 16 bits of prng state
137 uint64_t key
= hstate
->shiftreg
& 0xffff;
138 uint32_t nRxork
= (hstate
->shiftreg
>> 16) & 0xffffffff;
140 // rollback and extract bits b
142 for (uint8_t i
= 0; i
< 32; i
++) {
143 hstate
->shiftreg
= ((hstate
->shiftreg
) << 1) | ((uid
>> (31 - i
)) & 0x1);
144 b
= (b
<< 1) | (unsigned int) ht2_fnf(hstate
->shiftreg
);
147 uint32_t nR
= nRenc
^ b
;
148 uint64_t keyupper
= nRxork
^ nR
;
149 key
= key
| (keyupper
<< 16);
155 PrintAndLogEx(INFO
, "b..... %08" PRIx32
" %08" PRIx32
" %012" PRIx64
, b
, nRenc
, hstate
->shiftreg
);
156 PrintAndLogEx(INFO
, "key... %012" PRIx64
" %012" PRIx64
"\n", key
, REV64(key
));
163 * Hitag_State* pstate - output, internal state after initialisation
164 * uint64_t sharedkey - 48 bit key shared between reader & tag
165 * uint32_t serialnum - 32 bit tag serial number
166 * uint32_t iv - 32 bit random IV from reader, part of tag authentication
168 void ht2_hitag2_init_ex(hitag_state_t
*hstate
, uint64_t sharedkey
, uint32_t serialnum
, uint32_t iv
) {
169 // init state, from serial number and lowest 16 bits of shared key
170 uint64_t state
= ((sharedkey
& 0xFFFF) << 32) | serialnum
;
172 // mix the initialisation vector and highest 32 bits of the shared key
173 iv
^= (uint32_t)(sharedkey
>> 16);
175 // move 16 bits from (IV xor Shared Key) to top of uint64_t state
176 // these will be XORed in turn with output of the crypto function
177 state
|= (uint64_t) iv
<< 48;
180 // unrolled loop is faster on PIC32 (MIPS), do 32 times
181 // shift register, then calc new bit
183 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
184 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
185 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
186 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
187 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
188 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
189 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
190 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
192 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
193 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
194 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
195 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
196 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
197 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
198 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
199 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
201 // highest 16 bits of IV XOR Shared Key
202 state
|= (uint64_t) iv
<< 47;
204 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
205 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
206 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
207 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
208 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
209 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
210 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
211 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
213 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
214 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
215 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
216 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
217 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
218 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
219 state
= (state
>> 1) ^ (uint64_t) ht2_f20(state
) << 46;
220 state
^= (uint64_t) ht2_f20(state
) << 47;
224 hstate
->shiftreg
= state
;
225 /* naive version for reference, LFSR has 16 taps
226 pstate->lfsr = state ^ (state >> 2) ^ (state >> 3) ^ (state >> 6)
227 ^ (state >> 7) ^ (state >> 8) ^ (state >> 16) ^ (state >> 22)
228 ^ (state >> 23) ^ (state >> 26) ^ (state >> 30) ^ (state >> 41)
229 ^ (state >> 42) ^ (state >> 43) ^ (state >> 46) ^ (state >> 47);
232 // optimise with one 64-bit intermediate
233 uint64_t temp
= state
^ (state
>> 1);
234 hstate
->lfsr
= state
^ (state
>> 6) ^ (state
>> 16)
235 ^ (state
>> 26) ^ (state
>> 30) ^ (state
>> 41)
236 ^ (temp
>> 2) ^ (temp
>> 7) ^ (temp
>> 22)
237 ^ (temp
>> 42) ^ (temp
>> 46);
242 * Return up to 32 crypto bits.
243 * Last bit is in least significant bit, earlier bits are shifted left.
244 * Note that the Hitag transmission protocol is least significant bit,
245 * so we may want to change this, or add a function, that returns the
246 * crypto output bits in the other order.
249 * Hitag_State* pstate - in/out, internal cipher state after initialisation
250 * uint32_t steps - number of bits requested, (capped at 32)
252 uint32_t ht2_hitag2_nstep(hitag_state_t
*hstate
, uint32_t steps
) {
253 uint64_t state
= hstate
->shiftreg
;
255 uint64_t lfsr
= hstate
->lfsr
;
262 // update shift registers
264 state
= (state
>> 1) | 0x800000000000;
265 lfsr
= (lfsr
>> 1) ^ 0xB38083220073;
266 // accumulate next bit of crypto
267 result
= (result
<< 1) | ht2_f20(state
);
271 result
= (result
<< 1) | ht2_f20(state
);
275 hstate
->shiftreg
= state
;
280 uint64_t ht2_hitag2_init(const uint64_t key
, const uint32_t serial
, const uint32_t iv
) {
282 uint64_t x
= ((key
& 0xFFFF) << 32) + serial
;
284 for (uint32_t i
= 0; i
< 32; i
++) {
286 x
+= (uint64_t)(ht2_f20(x
) ^ (((iv
>> i
) ^ (key
>> (i
+ 16))) & 1)) << 47;
291 int ht2_try_state(uint64_t s
, uint32_t uid
, uint32_t aR2
, uint32_t nR1
, uint32_t nR2
, uint64_t *key
) {
293 hitag_state_t hstate
;
297 hstate
.shiftreg
= (uint64_t)(((hstate
.shiftreg
<< 1) & 0xffffffffffff) | (uint64_t)ht2_fnR(hstate
.shiftreg
));
298 hstate
.shiftreg
= (uint64_t)(((hstate
.shiftreg
<< 1) & 0xffffffffffff) | (uint64_t)ht2_fnR(hstate
.shiftreg
));
304 ht2_rollback(&hs2
, 2);
306 PrintAndLogEx(INFO
, "hstate shiftreg.... %" PRIx64
" lfsr... %" PRIx64
, hstate
.shiftreg
, hstate
.lfsr
);
307 PrintAndLogEx(INFO
, "hstate shiftreg.... %" PRIx64
" lfsr... %" PRIx64
, hs2
.shiftreg
, hs2
.lfsr
);
311 uint64_t keyrev
= hstate
.shiftreg
& 0xffff;
312 uint64_t nR1xk
= (hstate
.shiftreg
>> 16) & 0xffffffff;
315 PrintAndLogEx(INFO
, "keyrev...... %012" PRIx64
" nR1xk... %08" PRIx64
, keyrev
, nR1xk
);
319 for (uint8_t i
= 0; i
< 32; i
++) {
320 hstate
.shiftreg
= ((hstate
.shiftreg
) << 1) | ((uid
>> (31 - i
)) & 0x1);
321 b
= (b
<< 1) | (unsigned int) ht2_fnf(hstate
.shiftreg
);
325 PrintAndLogEx(INFO
, "b..... %08" PRIx32
" %08" PRIx32
" %012" PRIx64
, b
, nR1
, hstate
.shiftreg
);
328 keyrev
|= (nR1xk
^ nR1
^ b
) << 16;
331 PrintAndLogEx(INFO
, "key... %012" PRIx64
" %012" PRIx64
, keyrev
, REV64(keyrev
));
335 ht2_hitag2_init_ex(&hstate
, keyrev
, uid
, nR2
);
337 if ((aR2
^ ht2_hitag2_nstep(&hstate
, 32)) == 0xFFFFFFFF) {
338 *key
= REV64(keyrev
);
345 // "MIKRON" = O N M I K R
346 // Key = 4F 4E 4D 49 4B 52 - Secret 48-bit key
347 // Serial = 49 43 57 69 - Serial number of the tag, transmitted in clear
348 // Random = 65 6E 45 72 - Random IV, transmitted in clear
349 //~28~DC~80~31 = D7 23 7F CE - Authenticator value = inverted first 4 bytes of the keystream
351 // The code below must print out "D7 23 7F CE 8C D0 37 A9 57 49 C1 E6 48 00 8A B6".
352 // The inverse of the first 4 bytes is sent to the tag to authenticate.
353 // The rest is encrypted by XORing it with the subsequent keystream.
356 * Return 8 crypto bits.
357 * Last bit is in least significant bit, earlier bits are shifted left.
358 * Note that the Hitag transmission protocol is least significant bit,
359 * so we may want to change this, or add a function, that returns the
360 * crypto output bits in the other order.
363 * uint64_t *state - in/out, internal cipher state after initialisation
365 uint64_t ht2_hitag2_bit(uint64_t *state
) {
369 ((((x
>> 0) ^ (x
>> 2) ^ (x
>> 3) ^ (x
>> 6)
370 ^ (x
>> 7) ^ (x
>> 8) ^ (x
>> 16) ^ (x
>> 22)
371 ^ (x
>> 23) ^ (x
>> 26) ^ (x
>> 30) ^ (x
>> 41)
372 ^ (x
>> 42) ^ (x
>> 43) ^ (x
>> 46) ^ (x
>> 47)) & 1) << 47);
378 // Take a state and create one byte (8bits) of crypto
379 uint32_t ht2_hitag2_byte(uint64_t *state
) {
381 c
+= (uint32_t) ht2_hitag2_bit(state
) << 7; // 7
382 c
+= (uint32_t) ht2_hitag2_bit(state
) << 6; // 6
383 c
+= (uint32_t) ht2_hitag2_bit(state
) << 5; // 5
384 c
+= (uint32_t) ht2_hitag2_bit(state
) << 4;
385 c
+= (uint32_t) ht2_hitag2_bit(state
) << 3;
386 c
+= (uint32_t) ht2_hitag2_bit(state
) << 2;
387 c
+= (uint32_t) ht2_hitag2_bit(state
) << 1;
388 c
+= (uint32_t) ht2_hitag2_bit(state
) << 0;
392 uint32_t ht2_hitag2_word(uint64_t *state
, uint32_t steps
) {
395 c
+= (uint32_t) ht2_hitag2_bit(state
) << (steps
- 1);
400 void ht2_hitag2_cipher_reset(hitag2_t
*tag
, const uint8_t *iv
) {
401 uint64_t key
= ((uint64_t)tag
->sectors
[2][2]) |
402 ((uint64_t)tag
->sectors
[2][3] << 8) |
403 ((uint64_t)tag
->sectors
[1][0] << 16) |
404 ((uint64_t)tag
->sectors
[1][1] << 24) |
405 ((uint64_t)tag
->sectors
[1][2] << 32) |
406 ((uint64_t)tag
->sectors
[1][3] << 40);
407 uint32_t uid
= ((uint32_t)tag
->sectors
[0][0]) |
408 ((uint32_t)tag
->sectors
[0][1] << 8) |
409 ((uint32_t)tag
->sectors
[0][2] << 16) |
410 ((uint32_t)tag
->sectors
[0][3] << 24);
411 uint32_t iv_
= (((uint32_t)(iv
[0]))) |
412 (((uint32_t)(iv
[1])) << 8) |
413 (((uint32_t)(iv
[2])) << 16) |
414 (((uint32_t)(iv
[3])) << 24);
415 tag
->cs
= ht2_hitag2_init(REV64(key
), REV32(uid
), REV32(iv_
));
418 int ht2_hitag2_cipher_authenticate(uint64_t *state
, const uint8_t *authenticator_is
) {
419 uint8_t authenticator_should
[4];
420 authenticator_should
[0] = ~ht2_hitag2_byte(state
);
421 authenticator_should
[1] = ~ht2_hitag2_byte(state
);
422 authenticator_should
[2] = ~ht2_hitag2_byte(state
);
423 authenticator_should
[3] = ~ht2_hitag2_byte(state
);
424 return (memcmp(authenticator_should
, authenticator_is
, 4) == 0);
427 void ht2_hitag2_cipher_transcrypt(uint64_t *state
, uint8_t *data
, uint16_t bytes
, uint16_t bits
) {
429 for (i
= 0; i
< bytes
; i
++) {
430 data
[i
] ^= ht2_hitag2_byte(state
);
433 for (i
= 0; i
< bits
; i
++) {
434 data
[bytes
] ^= ht2_hitag2_bit(state
) << (7 - i
);