1 /*****************************************************************************
2 * This file is part of iClassCipher. It is a reconstructon of the cipher engine
3 * used in iClass, and RFID techology.
5 * The implementation is based on the work performed by
6 * Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and
7 * Milosch Meriac in the paper "Dismantling IClass".
9 * Copyright (C) 2014 Martin Holst Swende
11 * This is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as published
13 * by the Free Software Foundation.
15 * This file is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with IClassCipher. If not, see <http://www.gnu.org/licenses/>.
22 ****************************************************************************/
29 #include "loclass/cipher.h"
30 #include "loclass/cipherutils.h"
31 #include "loclass/ikeys.h"
33 uint8_t keytable
[] = { 0,0,0,0,0,0,0,0};
36 * Definition 2. The feedback function for the top register T : F 16/2 → F 2
38 * T (x 0 x 1 . . . . . . x 15 ) = x 0 ⊕ x 1 ⊕ x 5 ⊕ x 7 ⊕ x 10 ⊕ x 11 ⊕ x 14 ⊕ x 15 .
42 bool x0
= state
.t
& 0x8000;
43 bool x1
= state
.t
& 0x4000;
44 bool x5
= state
.t
& 0x0400;
45 bool x7
= state
.t
& 0x0100;
46 bool x10
= state
.t
& 0x0020;
47 bool x11
= state
.t
& 0x0010;
48 bool x14
= state
.t
& 0x0002;
49 bool x15
= state
.t
& 0x0001;
50 return x0
^ x1
^ x5
^ x7
^ x10
^ x11
^ x14
^ x15
;
53 * Similarly, the feedback function for the bottom register B : F 8/2 → F 2 is defined as
54 * B(x 0 x 1 . . . x 7 ) = x 1 ⊕ x 2 ⊕ x 3 ⊕ x 7 .
58 bool x1
= state
.b
& 0x40;
59 bool x2
= state
.b
& 0x20;
60 bool x3
= state
.b
& 0x10;
61 bool x7
= state
.b
& 0x01;
63 return x1
^ x2
^ x3
^ x7
;
69 * Definition 3 (Selection function). The selection function select : F 2 × F 2 ×
70 * F 8/2 → F 3/2 is defined as select(x, y, r) = z 0 z 1 z 2 where
71 * z 0 = (r 0 ∧ r 2 ) ⊕ (r 1 ∧ r 3 ) ⊕ (r 2 ∨ r 4 )
72 * z 1 = (r 0 ∨ r 2 ) ⊕ (r 5 ∨ r 7 ) ⊕ r 1 ⊕ r 6 ⊕ x ⊕ y
73 * z 2 = (r 3 ∧ r 5 ) ⊕ (r 4 ∧ r 6 ) ⊕ r 7 ⊕ x
75 uint8_t _select(bool x
, bool y
, uint8_t r
)
77 bool r0
= r
>> 7 & 0x1;
78 bool r1
= r
>> 6 & 0x1;
79 bool r2
= r
>> 5 & 0x1;
80 bool r3
= r
>> 4 & 0x1;
81 bool r4
= r
>> 3 & 0x1;
82 bool r5
= r
>> 2 & 0x1;
83 bool r6
= r
>> 1 & 0x1;
86 bool z0
= (r0
& r2
) ^ (r1
& ~r3
) ^ (r2
| r4
);
87 bool z1
= (r0
| r2
) ^ ( r5
| r7
) ^ r1
^ r6
^ x
^ y
;
88 bool z2
= (r3
& ~r5
) ^ (r4
& r6
) ^ r7
^ x
;
90 // The three bitz z0.. z1 are packed into a uint8_t:
92 //Return value is a uint8_t
94 retval
|= (z0
<< 2) & 4;
95 retval
|= (z1
<< 1) & 2;
98 // Return value 0 <= retval <= 7
103 * Definition 4 (Successor state). Let s = l, r, t, b be a cipher state, k ∈ (F 82 ) 8
104 * be a key and y ∈ F 2 be the input bit. Then, the successor cipher state s ′ =
105 * l ′ , r ′ , t ′ , b ′ is defined as
106 * t ′ := (T (t) ⊕ r 0 ⊕ r 4 )t 0 . . . t 14 l ′ := (k [select(T (t),y,r)] ⊕ b ′ ) ⊞ l ⊞ r
107 * b ′ := (B(b) ⊕ r 7 )b 0 . . . b 6 r ′ := (k [select(T (t),y,r)] ⊕ b ′ ) ⊞ l
110 * @param k - array containing 8 bytes
112 State
successor(uint8_t* k
, State s
, bool y
)
114 bool r0
= s
.r
>> 7 & 0x1;
115 bool r4
= s
.r
>> 3 & 0x1;
118 State successor
= {0,0,0,0};
120 successor
.t
= s
.t
>> 1;
121 successor
.t
|= (T(s
) ^ r0
^ r4
) << 15;
123 successor
.b
= s
.b
>> 1;
124 successor
.b
|= (B(s
) ^ r7
) << 7;
128 successor
.l
= ((k
[_select(Tt
,y
,s
.r
)] ^ successor
.b
) + s
.l
+s
.r
) & 0xFF;
129 successor
.r
= ((k
[_select(Tt
,y
,s
.r
)] ^ successor
.b
) + s
.l
) & 0xFF;
134 * We define the successor function suc which takes a key k ∈ (F 82 ) 8 , a state s and
135 * an input y ∈ F 2 and outputs the successor state s ′ . We overload the function suc
136 * to multiple bit input x ∈ F n 2 which we define as
137 * @param k - array containing 8 bytes
139 State
suc(uint8_t* k
,State s
, BitstreamIn
*bitstream
)
141 if(bitsLeft(bitstream
) == 0)
145 bool lastbit
= tailBit(bitstream
);
146 return successor(k
,suc(k
,s
,bitstream
), lastbit
);
150 * Definition 5 (Output). Define the function output which takes an internal
151 * state s =< l, r, t, b > and returns the bit r 5 . We also define the function output
152 * on multiple bits input which takes a key k, a state s and an input x ∈ F n 2 as
153 * output(k, s, ǫ) = ǫ
154 * output(k, s, x 0 . . . x n ) = output(s) · output(k, s ′ , x 1 . . . x n )
155 * where s ′ = suc(k, s, x 0 ).
157 void output(uint8_t* k
,State s
, BitstreamIn
* in
, BitstreamOut
* out
)
159 if(bitsLeft(in
) == 0)
163 //printf("bitsleft %d" , bitsLeft(in));
164 //printf(" %0d", s.r >> 2 & 1);
165 pushBit(out
,(s
.r
>> 2) & 1);
167 uint8_t x0
= headBit(in
);
168 State ss
= successor(k
,s
,x0
);
169 output(k
,ss
,in
, out
);
173 * Definition 6 (Initial state). Define the function init which takes as input a
174 * key k ∈ (F 82 ) 8 and outputs the initial cipher state s =< l, r, t, b >
177 State
init(uint8_t* k
)
180 ((k
[0] ^ 0x4c) + 0xEC) & 0xFF,// l
181 ((k
[0] ^ 0x4c) + 0x21) & 0xFF,// r
187 void MAC(uint8_t* k
, BitstreamIn input
, BitstreamOut out
)
189 uint8_t zeroes_32
[] = {0,0,0,0};
190 BitstreamIn input_32_zeroes
= {zeroes_32
,sizeof(zeroes_32
)*8,0};
191 State initState
= suc(k
,init(k
),&input
);
192 output(k
,initState
,&input_32_zeroes
,&out
);
197 void printarr(char * name
, uint8_t* arr
, int len
)
200 printf("uint8_t %s[] = {", name
);
201 for(i
=0 ; i
< len
; i
++)
203 printf("0x%02x,",*(arr
+i
));
211 //From the "dismantling.IClass" paper:
212 uint8_t cc_nr
[] = {0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0,0,0,0};
213 // But actually, that must be reversed, it's "on-the-wire" data
214 reverse_arraybytes(cc_nr
,sizeof(cc_nr
));
217 uint8_t div_key
[] = {0xE0,0x33,0xCA,0x41,0x9A,0xEE,0x43,0xF9};
218 uint8_t correct_MAC
[] = {0x1d,0x49,0xC9,0xDA};
220 BitstreamIn bitstream
= {cc_nr
,sizeof(cc_nr
) * 8,0};
221 uint8_t dest
[]= {0,0,0,0,0,0,0,0};
222 BitstreamOut out
= { dest
, sizeof(dest
)*8, 0 };
223 MAC(div_key
,bitstream
, out
);
224 //The output MAC must also be reversed
225 reverse_arraybytes(dest
, sizeof(dest
));
227 if(false && memcmp(dest
, correct_MAC
,4) == 0)
229 printf("MAC calculation OK!\n");
233 printf("MAC calculation failed\n");
234 printarr("Calculated_MAC", dest
, 4);
235 printarr("Correct_MAC ", correct_MAC
, 4);
241 int calc_iclass_mac(uint8_t *cc_nr_p
, int length
, uint8_t *div_key_p
, uint8_t *mac
)
245 cc_nr
=(uint8_t*)malloc(length
+1);
246 memcpy(cc_nr
,cc_nr_p
,length
);
247 memcpy(div_key
,div_key_p
,8);
249 reverse_arraybytes(cc_nr
,length
);
250 BitstreamIn bitstream
= {cc_nr
,length
* 8,0};
251 uint8_t dest
[]= {0,0,0,0,0,0,0,0};
252 BitstreamOut out
= { dest
, sizeof(dest
)*8, 0 };
253 MAC(div_key
,bitstream
, out
);
254 //The output MAC must also be reversed
255 reverse_arraybytes(dest
, sizeof(dest
));
257 printf("Calculated_MAC\t%02x%02x%02x%02x\n", dest
[0],dest
[1],dest
[2],dest
[3]);