1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2015, 2016 by piwi
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Implements a card only attack based on crypto text (encrypted nonces
9 // received during a nested authentication) only. Unlike other card only
10 // attacks this doesn't rely on implementation errors but only on the
11 // inherent weaknesses of the crypto1 cypher. Described in
12 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened
13 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on
14 // Computer and Communications Security, 2015
15 //-----------------------------------------------------------------------------
17 // This program calculates tables with possible states for a given
20 //-----------------------------------------------------------------------------
28 #include "crapto1/crapto1.h"
32 #define NUM_PART_SUMS 9
33 #define BITFLIP_2ND_BYTE 0x0200
41 static uint16_t PartialSumProperty(uint32_t state
, odd_even_t odd_even
)
44 for (uint16_t j
= 0; j
< 16; j
++) {
46 uint16_t part_sum
= 0;
47 if (odd_even
== ODD_STATE
) {
48 for (uint16_t i
= 0; i
< 5; i
++) {
49 part_sum
^= filter(st
);
50 st
= (st
<< 1) | ((j
>> (3-i
)) & 0x01) ;
52 part_sum
^= 1; // XOR 1 cancelled out for the other 8 bits
54 for (uint16_t i
= 0; i
< 4; i
++) {
55 st
= (st
<< 1) | ((j
>> (3-i
)) & 0x01) ;
56 part_sum
^= filter(st
);
65 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68 #define malloc_bitarray(x) __builtin_assume_aligned(_aligned_malloc(x, __BIGGEST_ALIGNMENT__), __BIGGEST_ALIGNMENT__)
69 #define free_bitarray(x) _aligned_free(x)
71 static inline void clear_bitarray24(uint32_t *bitarray
)
73 memset(bitarray
, 0x00, sizeof(uint32_t) * (1<<19));
77 static inline uint32_t test_bit24(uint32_t *bitarray
, uint32_t index
)
79 return bitarray
[index
>>5] & (0x80000000>>(index
&0x0000001f));
83 static inline void set_bit24(uint32_t *bitarray
, uint32_t index
)
85 bitarray
[index
>>5] |= 0x80000000>>(index
&0x0000001f);
89 static inline uint32_t next_state(uint32_t *bitset
, uint32_t state
)
91 if (++state
== 1<<24) return 1<<24;
92 uint32_t index
= state
>> 5;
93 uint_fast8_t bit
= state
& 0x1f;
94 uint32_t line
= bitset
[index
] << bit
;
96 if (line
& 0x80000000) return state
;
102 while (bitset
[index
] == 0x00000000 && state
< 1<<24) {
106 if (state
>= 1<<24) return 1<<24;
108 return state
+ __builtin_clz(bitset
[index
]);
111 line
= bitset
[index
];
112 while (bit
<= 0x1f) {
113 if (line
& 0x80000000) return state
;
123 static inline uint32_t next_not_state(uint32_t *bitset
, uint32_t state
)
125 if (++state
== 1<<24) return 1<<24;
126 uint32_t index
= state
>> 5;
127 uint_fast8_t bit
= state
& 0x1f;
128 uint32_t line
= bitset
[index
] << bit
;
129 while (bit
<= 0x1f) {
130 if ((line
& 0x80000000) == 0) return state
;
136 while (bitset
[index
] == 0xffffffff && state
< 1<<24) {
140 if (state
>= 1<<24) return 1<<24;
142 return state
+ __builtin_clz(~bitset
[index
]);
145 line
= bitset
[index
];
146 while (bit
<= 0x1f) {
147 if ((line
& 0x80000000) == 0) return state
;
157 static inline uint32_t bitcount(uint32_t a
)
160 return __builtin_popcountl(a
);
162 a
= a
- ((a
>> 1) & 0x55555555);
163 a
= (a
& 0x33333333) + ((a
>> 2) & 0x33333333);
164 return (((a
+ (a
>> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24;
169 static inline uint32_t count_states(uint32_t *bitset
)
172 for (uint32_t i
= 0; i
< (1<<19); i
++) {
173 count
+= bitcount(bitset
[i
]);
179 static void write_bitflips_file(odd_even_t odd_even
, uint16_t bitflip
, int sum_a0
, uint32_t *bitset
)
182 sprintf(filename
, "bitflip_%d_%03" PRIx16
"_sum%d_states.bin", odd_even
, bitflip
, sum_a0
);
183 FILE *outfile
= fopen(filename
, "wb");
184 fwrite(bitset
, 1, sizeof(uint32_t)*(1<<19), outfile
);
189 uint32_t *restrict part_sum_a0_bitarrays
[2][NUM_PART_SUMS
];
191 static void init_part_sum_bitarrays(void)
193 printf("init_part_sum_bitarrays()...");
194 for (odd_even_t odd_even
= EVEN_STATE
; odd_even
<= ODD_STATE
; odd_even
++) {
195 for (uint16_t part_sum_a0
= 0; part_sum_a0
< NUM_PART_SUMS
; part_sum_a0
++) {
196 part_sum_a0_bitarrays
[odd_even
][part_sum_a0
] = (uint32_t *)malloc_bitarray(sizeof(uint32_t) * (1<<19));
197 if (part_sum_a0_bitarrays
[odd_even
][part_sum_a0
] == NULL
) {
198 printf("Out of memory error in init_part_suma0_statelists(). Aborting...\n");
201 clear_bitarray24(part_sum_a0_bitarrays
[odd_even
][part_sum_a0
]);
204 for (odd_even_t odd_even
= EVEN_STATE
; odd_even
<= ODD_STATE
; odd_even
++) {
205 //printf("(%d, %" PRIu16 ")...", odd_even, part_sum_a0);
206 for (uint32_t state
= 0; state
< (1<<20); state
++) {
207 uint16_t part_sum_a0
= PartialSumProperty(state
, odd_even
) / 2;
208 for (uint16_t low_bits
= 0; low_bits
< 1<<4; low_bits
++) {
209 set_bit24(part_sum_a0_bitarrays
[odd_even
][part_sum_a0
], state
<<4 | low_bits
);
217 static void free_part_sum_bitarrays(void)
219 printf("free_part_sum_bitarrays()...");
220 for (int16_t part_sum_a0
= (NUM_PART_SUMS
-1); part_sum_a0
>= 0; part_sum_a0
--) {
221 free_bitarray(part_sum_a0_bitarrays
[ODD_STATE
][part_sum_a0
]);
223 for (int16_t part_sum_a0
= (NUM_PART_SUMS
-1); part_sum_a0
>= 0; part_sum_a0
--) {
224 free_bitarray(part_sum_a0_bitarrays
[EVEN_STATE
][part_sum_a0
]);
229 uint32_t *restrict sum_a0_bitarray
[2];
231 void init_sum_bitarray(uint16_t sum_a0
)
233 printf("init_sum_bitarray()...\n");
234 for (odd_even_t odd_even
= EVEN_STATE
; odd_even
<= ODD_STATE
; odd_even
++) {
235 sum_a0_bitarray
[odd_even
] = (uint32_t *)malloc_bitarray(sizeof(uint32_t) * (1<<19));
236 if (sum_a0_bitarray
[odd_even
] == NULL
) {
237 printf("Out of memory error in init_sum_bitarrays(). Aborting...\n");
240 clear_bitarray24(sum_a0_bitarray
[odd_even
]);
242 for (uint8_t p
= 0; p
< NUM_PART_SUMS
; p
++) {
243 for (uint8_t q
= 0; q
< NUM_PART_SUMS
; q
++) {
244 if (sum_a0
== 2*p
*(16-2*q
) + (16-2*p
)*2*q
) {
245 for (uint32_t i
= 0; i
< (1<<19); i
++) {
246 sum_a0_bitarray
[EVEN_STATE
][i
] |= part_sum_a0_bitarrays
[EVEN_STATE
][q
][i
];
247 sum_a0_bitarray
[ODD_STATE
][i
] |= part_sum_a0_bitarrays
[ODD_STATE
][p
][i
];
252 for (odd_even_t odd_even
= EVEN_STATE
; odd_even
<= ODD_STATE
; odd_even
++) {
253 uint32_t count
= count_states(sum_a0_bitarray
[odd_even
]);
254 printf("sum_a0_bitarray[%s] has %d states (%5.2f%%)\n", odd_even
==EVEN_STATE
?"even":"odd ", count
, (float)count
/(1<<24)*100.0);
260 static void free_sum_bitarray(void)
262 printf("free_sum_bitarray()...");
263 free_bitarray(sum_a0_bitarray
[ODD_STATE
]);
264 free_bitarray(sum_a0_bitarray
[EVEN_STATE
]);
269 static void precalculate_bit0_bitflip_bitarrays(uint8_t const bitflip
, uint16_t const sum_a0
)
273 #define NUM_TEST_STATES (1<<10)
275 #define NUM_TEST_STATES (1<<23)
278 time_t start_time
= time(NULL
);
279 time_t last_check_time
= start_time
;
281 uint32_t *restrict test_bitarray
[2];
282 uint32_t *restrict test_not_bitarray
[2];
284 test_bitarray
[EVEN_STATE
] = malloc_bitarray(sizeof(uint32_t) * (1<<19));
285 clear_bitarray24(test_bitarray
[EVEN_STATE
]);
286 test_bitarray
[ODD_STATE
] = malloc_bitarray(sizeof(uint32_t) * (1<<19));
287 clear_bitarray24(test_bitarray
[ODD_STATE
]);
289 test_not_bitarray
[EVEN_STATE
] = malloc_bitarray(sizeof(uint32_t) * (1<<19));
290 clear_bitarray24(test_not_bitarray
[EVEN_STATE
]);
291 test_not_bitarray
[ODD_STATE
] = malloc_bitarray(sizeof(uint32_t) * (1<<19));
292 clear_bitarray24(test_not_bitarray
[ODD_STATE
]);
295 bool all_odd_states_are_possible_for_notbitflip
= false;
297 printf("\n\nStarting search for crypto1 states resulting in bitflip property 0x%03x...\n", bitflip
);
298 for (uint32_t even_state
= next_state(sum_a0_bitarray
[EVEN_STATE
], -1); even_state
< NUM_TEST_STATES
; even_state
= next_state(sum_a0_bitarray
[EVEN_STATE
], even_state
)) {
299 bool even_state_is_possible
= false;
300 time_t time_now
= time(NULL
);
301 if (difftime(time_now
, last_check_time
) > 5*60) { // print status every 5 minutes
302 float runtime
= difftime(time_now
, start_time
);
303 float remaining_time
= runtime
* ((1<<23) - even_state
) / even_state
;
304 printf("\n%1.1f hours elapsed, expected completion in %1.1f hours (%1.1f days)", runtime
/3600, remaining_time
/3600, remaining_time
/3600/24);
305 last_check_time
= time_now
;
307 for (uint32_t odd_state
= next_state(sum_a0_bitarray
[ODD_STATE
], -1); odd_state
< (1<<24); odd_state
= next_state(test_bitarray
[ODD_STATE
], odd_state
)) {
308 if (even_state_is_possible
&& test_bit24(test_bitarray
[ODD_STATE
], odd_state
)) continue;
309 // load crypto1 state
310 struct Crypto1State cs
;
311 cs
.odd
= odd_state
>> 4;
312 cs
.even
= even_state
>> 4;
314 // track flipping bits in state
315 struct Crypto1DeltaState
{
322 uint_fast16_t keystream
= 0;
325 for (int i
= 0; i
< 9; i
++) {
326 uint_fast8_t keystream_bit
= filter(cs
.odd
& 0x000fffff) ^ filter((cs
.odd
& 0x000fffff) ^ cs_delta
.odd
);
327 keystream
= keystream
<< 1 | keystream_bit
;
328 uint_fast8_t nt_bit
= BIT(bitflip
, i
) ^ keystream_bit
;
329 uint_fast8_t LSFR_feedback
= BIT(cs_delta
.odd
, 2) ^ BIT(cs_delta
.even
, 2) ^ BIT(cs_delta
.odd
, 3);
331 cs_delta
.even
= cs_delta
.even
<< 1 | (LSFR_feedback
^ nt_bit
);
332 uint_fast8_t tmp
= cs_delta
.odd
;
333 cs_delta
.odd
= cs_delta
.even
;
338 cs
.odd
= odd_state
>> (7 - i
) / 2;
340 cs
.odd
= even_state
>> (7 - i
) / 2;
344 if (evenparity32(keystream
) == evenparity32(bitflip
)) {
345 // found valid bitflip state
346 even_state_is_possible
= true;
347 set_bit24(test_bitarray
[EVEN_STATE
], even_state
);
348 set_bit24(test_bitarray
[EVEN_STATE
], 1 << 23 | even_state
);
349 set_bit24(test_bitarray
[ODD_STATE
], odd_state
);
351 // found valid !bitflip state
352 set_bit24(test_not_bitarray
[EVEN_STATE
], even_state
);
353 set_bit24(test_not_bitarray
[EVEN_STATE
], 1 << 23 | even_state
);
354 set_bit24(test_not_bitarray
[ODD_STATE
], odd_state
);
357 if (!even_state_is_possible
) {
358 all_odd_states_are_possible_for_notbitflip
= true;
362 printf("\nAnalysis completed. Checking for effective bitflip properties...\n");
363 for (odd_even_t odd_even
= EVEN_STATE
; odd_even
<= ODD_STATE
; odd_even
++) {
364 count
[odd_even
] = count_states(test_bitarray
[odd_even
]);
365 if (count
[odd_even
] != 1<<24) {
366 printf("Writing %d possible %s states for bitflip property %03x (%d (%1.2f%%) states eliminated)\n",
368 odd_even
==EVEN_STATE
?"even":"odd",
369 bitflip
, (1<<24) - count
[odd_even
],
370 (float)((1<<24) - count
[odd_even
]) / (1<<24) * 100.0);
372 write_bitflips_file(odd_even
, bitflip
, sum_a0
, test_bitarray
[odd_even
]);
375 printf("All %s states for bitflip property %03x are possible. No file written.\n", odd_even
==EVEN_STATE
?"even":"odd", bitflip
);
378 uint32_t *restrict test_bitarray_2nd
= malloc_bitarray(sizeof(uint32_t) * (1<<19));
379 clear_bitarray24(test_bitarray_2nd
);
380 for (odd_even_t odd_even
= EVEN_STATE
; odd_even
<= ODD_STATE
; odd_even
++) {
381 if (count
[odd_even
] != 1<<24) {
382 for (uint32_t state
= 0; state
< (1<<24); state
+= 1<<4) {
383 uint32_t line
= test_bitarray
[odd_even
][state
>>5];
384 uint16_t half_line
= state
&0x000000010 ? line
&0x0000ffff : line
>>16;
385 if (half_line
!= 0) {
386 for (uint32_t low_bits
= 0; low_bits
< (1<<4); low_bits
++) {
387 set_bit24(test_bitarray_2nd
, low_bits
<< 20 | state
>> 4);
391 count
[odd_even
] = count_states(test_bitarray_2nd
);
392 if (count
[odd_even
] != 1<<24) {
393 printf("Writing %d possible %s states for bitflip property %03x (%d (%1.2f%%) states eliminated)\n",
395 odd_even
==EVEN_STATE
?"even":"odd",
396 bitflip
| BITFLIP_2ND_BYTE
, (1<<24) - count
[odd_even
],
397 (float)((1<<24) - count
[odd_even
]) / (1<<24) * 100.0);
399 write_bitflips_file(odd_even
, bitflip
| BITFLIP_2ND_BYTE
, sum_a0
, test_bitarray_2nd
);
402 printf("All %s states for bitflip property %03x are possible. No file written.\n", odd_even
==EVEN_STATE
?"even":"odd", bitflip
| BITFLIP_2ND_BYTE
);
405 printf("All %s states for bitflip property %03x are possible. No file written.\n", odd_even
==EVEN_STATE
?"even":"odd", bitflip
| BITFLIP_2ND_BYTE
);
409 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
410 // second run for the remaining "not bitflip" states
411 printf("\n\nStarting search for crypto1 states resulting in bitflip property 0x%03x...", bitflip
| 0x100);
412 start_time
= time(NULL
);
413 last_check_time
= start_time
;
414 for (uint32_t even_state
= next_state(sum_a0_bitarray
[EVEN_STATE
], -1); even_state
< NUM_TEST_STATES
; even_state
= next_state(sum_a0_bitarray
[EVEN_STATE
], even_state
)) {
415 bool even_state_is_possible
= test_bit24(test_not_bitarray
[EVEN_STATE
], even_state
);
416 time_t time_now
= time(NULL
);
417 if (difftime(time_now
, last_check_time
) > 5*60) { // print status every 5 minutes
418 float runtime
= difftime(time_now
, start_time
);
419 float remaining_time
= runtime
* ((1<<23) - even_state
) / even_state
;
420 printf("\n%1.1f hours elapsed, expected completion in %1.1f hours (%1.1f days)", runtime
/3600, remaining_time
/3600, remaining_time
/3600/24);
421 last_check_time
= time_now
;
423 for (uint32_t odd_state
= next_state(sum_a0_bitarray
[ODD_STATE
], -1); odd_state
< (1<<24); odd_state
= next_state(sum_a0_bitarray
[ODD_STATE
], odd_state
)) {
424 if (even_state_is_possible
) {
425 if (all_odd_states_are_possible_for_notbitflip
) break;
426 if (test_bit24(test_not_bitarray
[ODD_STATE
], odd_state
)) continue;
428 // load crypto1 state
429 struct Crypto1State cs
;
430 cs
.odd
= odd_state
>> 4;
431 cs
.even
= even_state
>> 4;
433 // track flipping bits in state
434 struct Crypto1DeltaState
{
441 uint_fast16_t keystream
= 0;
442 // uint_fast16_t nt = 0;
445 for (int i
= 0; i
< 9; i
++) {
446 uint_fast8_t keystream_bit
= filter(cs
.odd
& 0x000fffff) ^ filter((cs
.odd
& 0x000fffff) ^ cs_delta
.odd
);
447 keystream
= keystream
<< 1 | keystream_bit
;
448 uint_fast8_t nt_bit
= BIT(bitflip
|0x100, i
) ^ keystream_bit
;
449 uint_fast8_t LSFR_feedback
= BIT(cs_delta
.odd
, 2) ^ BIT(cs_delta
.even
, 2) ^ BIT(cs_delta
.odd
, 3);
451 cs_delta
.even
= cs_delta
.even
<< 1 | (LSFR_feedback
^ nt_bit
);
452 uint_fast8_t tmp
= cs_delta
.odd
;
453 cs_delta
.odd
= cs_delta
.even
;
458 cs
.odd
= odd_state
>> (7 - i
) / 2;
460 cs
.odd
= even_state
>> (7 - i
) / 2;
464 if (evenparity32(keystream
) != evenparity32(bitflip
)) {
465 // found valid !bitflip state
466 even_state_is_possible
= true;
467 set_bit24(test_not_bitarray
[EVEN_STATE
], even_state
);
468 set_bit24(test_not_bitarray
[EVEN_STATE
], 1 << 23 | even_state
);
469 set_bit24(test_not_bitarray
[ODD_STATE
], odd_state
);
474 printf("\nAnalysis completed. Checking for effective !bitflip properties...\n");
475 for (odd_even_t odd_even
= EVEN_STATE
; odd_even
<= ODD_STATE
; odd_even
++) {
476 count
[odd_even
] = count_states(test_not_bitarray
[odd_even
]);
477 if (count
[odd_even
] != 1<<24) {
478 printf("Writing %d possible %s states for bitflip property %03x (%d (%1.2f%%) states eliminated)\n",
480 odd_even
==EVEN_STATE
?"even":"odd",
481 bitflip
|0x100, (1<<24) - count
[odd_even
],
482 (float)((1<<24) - count
[odd_even
]) / (1<<24) * 100.0);
484 write_bitflips_file(odd_even
, bitflip
|0x100, sum_a0
, test_not_bitarray
[odd_even
]);
487 printf("All %s states for bitflip property %03x are possible. No file written.\n", odd_even
==EVEN_STATE
?"even":"odd", bitflip
|0x100);
491 clear_bitarray24(test_bitarray_2nd
);
492 for (odd_even_t odd_even
= EVEN_STATE
; odd_even
<= ODD_STATE
; odd_even
++) {
493 if (count
[odd_even
] != 1<<24) {
494 for (uint32_t state
= 0; state
< (1<<24); state
+= 1<<4) {
495 uint32_t line
= test_not_bitarray
[odd_even
][state
>>5];
496 uint16_t half_line
= state
&0x000000010 ? line
&0x0000ffff : line
>>16;
497 if (half_line
!= 0) {
498 for (uint32_t low_bits
= 0; low_bits
< (1<<4); low_bits
++) {
499 set_bit24(test_bitarray_2nd
, low_bits
<< 20 | state
>> 4);
503 count
[odd_even
] = count_states(test_bitarray_2nd
);
504 if (count
[odd_even
] != 1<<24) {
505 printf("Writing %d possible %s states for bitflip property %03x (%d (%1.2f%%) states eliminated)\n",
507 odd_even
==EVEN_STATE
?"even":"odd",
508 bitflip
| 0x100| BITFLIP_2ND_BYTE
, (1<<24) - count
[odd_even
],
509 (float)((1<<24) - count
[odd_even
]) / (1<<24) * 100.0);
511 write_bitflips_file(odd_even
, bitflip
| 0x100 | BITFLIP_2ND_BYTE
, sum_a0
, test_bitarray_2nd
);
514 printf("All %s states for bitflip property %03x are possible. No file written.\n", odd_even
==EVEN_STATE
?"even":"odd", bitflip
| 0x100 | BITFLIP_2ND_BYTE
);
517 printf("All %s states for bitflip property %03x are possible. No file written.\n", odd_even
==EVEN_STATE
?"even":"odd", bitflip
| 0x100 | BITFLIP_2ND_BYTE
);
521 free_bitarray(test_bitarray_2nd
);
522 free_bitarray(test_not_bitarray
[ODD_STATE
]);
523 free_bitarray(test_not_bitarray
[EVEN_STATE
]);
524 free_bitarray(test_bitarray
[ODD_STATE
]);
525 free_bitarray(test_bitarray
[EVEN_STATE
]);
531 int main (int argc
, char *argv
[]) {
533 unsigned int bitflip_in
;
536 printf("Create tables required by hardnested attack.\n");
537 printf("Expect a runtime in the range of days or weeks.\n");
538 printf("Single thread only. If you want to use several threads, start it multiple times :-)\n\n");
540 if (argc
!= 2 && argc
!= 3) {
541 printf(" syntax: %s <bitflip property> [<Sum_a0>]\n\n", argv
[0]);
542 printf(" example: %s 1f\n", argv
[0]);
546 sscanf(argv
[1],"%x", &bitflip_in
);
548 if (bitflip_in
> 255) {
549 printf("Bitflip property must be less than or equal to 0xff\n\n");
554 sscanf(argv
[2], "%d", &sum_a0
);
577 default: sum_a0
= -1;
580 printf("Calculating for bitflip = %02x, sum_a0 = %d\n", bitflip_in
, sum_a0
);
582 init_part_sum_bitarrays();
583 init_sum_bitarray(sum_a0
);
585 precalculate_bit0_bitflip_bitarrays(bitflip_in
, sum_a0
);
588 free_part_sum_bitarrays();