]> git.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/cipher.c
d7c9abdacc484c3ecc4911acf1b7a5ce5f10eddf
[proxmark3-svn] / client / loclass / cipher.c
1 /*****************************************************************************
2 * This file is part of iClassCipher. It is a reconstructon of the cipher engine
3 * used in iClass, and RFID techology.
4 *
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".
8 *
9 * Copyright (C) 2014 Martin Holst Swende
10 *
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.
14 *
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.
19 *
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 ****************************************************************************/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include "loclass/cipher.h"
30 #include "loclass/cipherutils.h"
31 #include "loclass/ikeys.h"
32
33 uint8_t keytable[] = { 0,0,0,0,0,0,0,0};
34
35 /**
36 * Definition 2. The feedback function for the top register T : F 16/2 → F 2
37 * is defined as
38 * T (x 0 x 1 . . . . . . x 15 ) = x 0 ⊕ x 1 ⊕ x 5 ⊕ x 7 ⊕ x 10 ⊕ x 11 ⊕ x 14 ⊕ x 15 .
39 **/
40 bool T(State state)
41 {
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;
51 }
52 /**
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 .
55 **/
56 bool B(State state)
57 {
58 bool x1 = state.b & 0x40;
59 bool x2 = state.b & 0x20;
60 bool x3 = state.b & 0x10;
61 bool x7 = state.b & 0x01;
62
63 return x1 ^ x2 ^ x3 ^ x7;
64
65 }
66
67
68 /**
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
74 **/
75 uint8_t _select(bool x, bool y, uint8_t r)
76 {
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;
84 bool r7 = r & 0x1;
85
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;
89
90 // The three bitz z0.. z1 are packed into a uint8_t:
91 // 00000ZZZ
92 //Return value is a uint8_t
93 uint8_t retval = 0;
94 retval |= (z0 << 2) & 4;
95 retval |= (z1 << 1) & 2;
96 retval |= z2 & 1;
97
98 // Return value 0 <= retval <= 7
99 return retval;
100 }
101
102 /**
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
108 *
109 * @param s - state
110 * @param k - array containing 8 bytes
111 **/
112 State successor(uint8_t* k, State s, bool y)
113 {
114 bool r0 = s.r >> 7 & 0x1;
115 bool r4 = s.r >> 3 & 0x1;
116 bool r7 = s.r & 0x1;
117
118 State successor = {0,0,0,0};
119
120 successor.t = s.t >> 1;
121 successor.t |= (T(s) ^ r0 ^ r4) << 15;
122
123 successor.b = s.b >> 1;
124 successor.b |= (B(s) ^ r7) << 7;
125
126 bool Tt = T(s);
127
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;
130
131 return successor;
132 }
133 /**
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
138 **/
139 State suc(uint8_t* k,State s, BitstreamIn *bitstream)
140 {
141 if(bitsLeft(bitstream) == 0)
142 {
143 return s;
144 }
145 bool lastbit = tailBit(bitstream);
146 return successor(k,suc(k,s,bitstream), lastbit);
147 }
148
149 /**
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 ).
156 **/
157 void output(uint8_t* k,State s, BitstreamIn* in, BitstreamOut* out)
158 {
159 if(bitsLeft(in) == 0)
160 {
161 return;
162 }
163 //printf("bitsleft %d" , bitsLeft(in));
164 //printf(" %0d", s.r >> 2 & 1);
165 pushBit(out,(s.r >> 2) & 1);
166 //Remove first bit
167 uint8_t x0 = headBit(in);
168 State ss = successor(k,s,x0);
169 output(k,ss,in, out);
170 }
171
172 /**
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 >
175 **/
176
177 State init(uint8_t* k)
178 {
179 State s = {
180 ((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
181 ((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
182 0x4c, // b
183 0xE012 // t
184 };
185 return s;
186 }
187 void MAC(uint8_t* k, BitstreamIn input, BitstreamOut out)
188 {
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);
193
194 }
195
196
197 void printarr(char * name, uint8_t* arr, int len)
198 {
199 int i ;
200 printf("uint8_t %s[] = {", name);
201 for(i =0 ; i< len ; i++)
202 {
203 printf("0x%02x,",*(arr+i));
204 }
205 printf("};\n");
206 }
207
208 int testMAC()
209 {
210
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));
215
216 //From the paper
217 uint8_t div_key[] = {0xE0,0x33,0xCA,0x41,0x9A,0xEE,0x43,0xF9};
218 uint8_t correct_MAC[] = {0x1d,0x49,0xC9,0xDA};
219
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));
226
227 if(false && memcmp(dest, correct_MAC,4) == 0)
228 {
229 printf("MAC calculation OK!\n");
230
231 }else
232 {
233 printf("MAC calculation failed\n");
234 printarr("Calculated_MAC", dest, 4);
235 printarr("Correct_MAC ", correct_MAC, 4);
236 return 1;
237 }
238 return 0;
239 }
240
241 int calc_iclass_mac(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t *mac)
242 {
243 uint8_t cc_nr[12];
244 uint8_t div_key[8];
245 memcpy(cc_nr,cc_nr_p,12);
246 memcpy(div_key,div_key_p,8);
247
248 reverse_arraybytes(cc_nr,sizeof(cc_nr));
249 BitstreamIn bitstream = {cc_nr,sizeof(cc_nr) * 8,0};
250 uint8_t dest []= {0,0,0,0,0,0,0,0};
251 BitstreamOut out = { dest, sizeof(dest)*8, 0 };
252 MAC(div_key,bitstream, out);
253 //The output MAC must also be reversed
254 reverse_arraybytes(dest, sizeof(dest));
255
256 printf("Calculated_MAC\t%02x%02x%02x%02x\n", dest[0],dest[1],dest[2],dest[3]);
257 memcpy(mac,dest,4);
258
259 return 1;
260 }
Impressum, Datenschutz