fix little endian vs big endian in the macros... again... but this time correct
[RRG-proxmark3.git] / client / src / loclass / cipher.c
blob1753ebc9d78e563b9db18291fe387c728d43c18a
1 /*****************************************************************************
2 * WARNING
4 * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
6 * USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL
7 * PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL,
8 * AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES.
10 * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
12 *****************************************************************************
14 * This file is part of loclass. It is a reconstructon of the cipher engine
15 * used in iClass, and RFID techology.
17 * The implementation is based on the work performed by
18 * Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and
19 * Milosch Meriac in the paper "Dismantling IClass".
21 * Copyright (C) 2014 Martin Holst Swende
23 * This is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License version 2 as published
25 * by the Free Software Foundation, or, at your option, any later version.
27 * This file is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with loclass. If not, see <http://www.gnu.org/licenses/>.
36 ****************************************************************************/
39 #include "cipher.h"
40 #include "cipherutils.h"
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdbool.h>
44 #include <stdint.h>
45 #ifndef ON_DEVICE
46 #include "fileutils.h"
47 #endif
50 /**
51 * Definition 1 (Cipher state). A cipher state of iClass s is an element of F 40/2
52 * consisting of the following four components:
53 * 1. the left register l = (l 0 . . . l 7 ) ∈ F 8/2 ;
54 * 2. the right register r = (r 0 . . . r 7 ) ∈ F 8/2 ;
55 * 3. the top register t = (t 0 . . . t 15 ) ∈ F 16/2 .
56 * 4. the bottom register b = (b 0 . . . b 7 ) ∈ F 8/2 .
57 **/
58 typedef struct {
59 uint8_t l;
60 uint8_t r;
61 uint8_t b;
62 uint16_t t;
63 } State;
65 /**
66 * Definition 2. The feedback function for the top register T : F 16/2 → F 2
67 * is defined as
68 * T (x 0 x 1 . . . . . . x 15 ) = x 0 ⊕ x 1 ⊕ x 5 ⊕ x 7 ⊕ x 10 ⊕ x 11 ⊕ x 14 ⊕ x 15 .
69 **/
70 static bool T(State state) {
72 bool x0 = state.t & 0x8000;
73 bool x1 = state.t & 0x4000;
74 bool x5 = state.t & 0x0400;
75 bool x7 = state.t & 0x0100;
76 bool x10 = state.t & 0x0020;
77 bool x11 = state.t & 0x0010;
78 bool x14 = state.t & 0x0002;
79 bool x15 = state.t & 0x0001;
80 return x0 ^ x1 ^ x5 ^ x7 ^ x10 ^ x11 ^ x14 ^ x15;
82 #define _x0 ((state.t & 0x8000) >> 15 )
83 #define _x1 ((state.t & 0x4000) >> 14 )
84 #define _x5 ((state.t & 0x0400) >> 10 )
85 #define _x7 ((state.t & 0x0100) >> 8 )
86 #define _x10 ((state.t & 0x0020) >> 5 )
87 #define _x11 ((state.t & 0x0010) >> 4 )
88 #define _x14 ((state.t & 0x0002) >> 1 )
89 #define _x15 (state.t & 0x0001)
90 return (_x0) ^ (_x1) ^ (_x5) ^ (_x7) ^ (_x10) ^ (_x11) ^ (_x14) ^ (_x15);
92 /**
93 * Similarly, the feedback function for the bottom register B : F 8/2 → F 2 is defined as
94 * B(x 0 x 1 . . . x 7 ) = x 1 ⊕ x 2 ⊕ x 3 ⊕ x 7 .
95 **/
96 /*static bool B(State state) {
97 bool x1 = state.b & 0x40;
98 bool x2 = state.b & 0x20;
99 bool x3 = state.b & 0x10;
100 bool x7 = state.b & 0x01;
101 return x1 ^ x2 ^ x3 ^ x7;
104 #define B(x) (((x.b & 0x40) >> 6) ^ ((x.b & 0x20) >> 5) ^ ((x.b & 0x10) >> 4) ^ (x.b & 0x01))
106 // 12 3456
107 // 0100 0000
110 * Definition 3 (Selection function). The selection function select : F 2 × F 2 ×
111 * F 8/2 → F 3/2 is defined as select(x, y, r) = z 0 z 1 z 2 where
112 * z 0 = (r 0 ∧ r 2 ) ⊕ (r 1 ∧ r 3 ) ⊕ (r 2 ∨ r 4 )
113 * z 1 = (r 0 ∨ r 2 ) ⊕ (r 5 ∨ r 7 ) ⊕ r 1 ⊕ r 6 ⊕ x ⊕ y
114 * z 2 = (r 3 ∧ r 5 ) ⊕ (r 4 ∧ r 6 ) ⊕ r 7 ⊕ x
116 static uint8_t _select(bool x, bool y, uint8_t r) {
117 #define _r0 ((r >> 7) & 0x01)
118 #define _r1 ((r >> 6) & 0x01)
119 #define _r2 ((r >> 5) & 0x01)
120 #define _r3 ((r >> 4) & 0x01)
121 #define _r4 ((r >> 3) & 0x01)
122 #define _r5 ((r >> 2) & 0x01)
123 #define _r6 ((r >> 1) & 0x01)
124 #define _r7 (r & 0x01)
126 #define _z0 ( (_r0 & _r2) ^ ( _r1 & (!_r3)) ^ (_r2 | _r4) )
127 #define _z1 ( (_r0 | _r2) ^ ( _r5 | _r7) ^_r1 ^ _r6 ^ (x) ^ (y) )
128 #define _z2 ( (_r3 & (!_r5)) ^ (_r4 & _r6) ^ _r7 ^ (x) )
131 uint8_t r0 = r >> 7 & 0x1;
132 uint8_t r1 = r >> 6 & 0x1;
133 uint8_t r2 = r >> 5 & 0x1;
134 uint8_t r3 = r >> 4 & 0x1;
135 uint8_t r4 = r >> 3 & 0x1;
136 uint8_t r5 = r >> 2 & 0x1;
137 uint8_t r6 = r >> 1 & 0x1;
138 uint8_t r7 = r & 0x1;
140 bool z0 = (r0 & r2) ^ (r1 & (!r3)) ^ (r2 | r4);
141 bool z1 = (r0 | r2) ^ (r5 | r7) ^ r1 ^ r6 ^ x ^ y;
142 bool z2 = (r3 & (!r5)) ^ (r4 & r6) ^ r7 ^ x;
144 // The three bitz z0.. z1 are packed into a uint8_t:
145 // 00000ZZZ
146 //Return value is a uint8_t
147 return ((z0 << 2) & 4) | ((z1 << 1) & 2) | (z2 & 1);
149 return ((_z0 << 2) & 4) | ((_z1 << 1) & 2) | (_z2 & 1);
152 uint8_t retval = 0;
153 retval |= (z0 << 2) & 4;
154 retval |= (z1 << 1) & 2;
155 retval |= (z2) & 1;
157 // Return value 0 <= retval <= 7
158 return retval;
163 * Definition 4 (Successor state). Let s = l, r, t, b be a cipher state, k ∈ (F 82 ) 8
164 * be a key and y ∈ F 2 be the input bit. Then, the successor cipher state s ′ =
165 * l ′ , r ′ , t ′ , b ′ is defined as
166 * t ′ := (T (t) ⊕ r 0 ⊕ r 4 )t 0 . . . t 14 l ′ := (k [select(T (t),y,r)] ⊕ b ′ ) ⊞ l ⊞ r
167 * b ′ := (B(b) ⊕ r 7 )b 0 . . . b 6 r ′ := (k [select(T (t),y,r)] ⊕ b ′ ) ⊞ l
169 * @param s - state
170 * @param k - array containing 8 bytes
172 static State successor(uint8_t *k, State s, bool y) {
173 bool r0 = s.r >> 7 & 0x1;
174 bool r4 = s.r >> 3 & 0x1;
175 bool r7 = s.r & 0x1;
177 State successor = {0, 0, 0, 0};
179 successor.t = s.t >> 1;
180 successor.t |= ((T(s)) ^ (r0) ^ (r4)) << 15;
182 successor.b = s.b >> 1;
183 successor.b |= ((B(s)) ^ (r7)) << 7;
185 bool Tt = T(s);
187 successor.l = ((k[_select(Tt, y, s.r)] ^ successor.b) + s.l + s.r) & 0xFF;
188 successor.r = ((k[_select(Tt, y, s.r)] ^ successor.b) + s.l) & 0xFF;
190 return successor;
193 * We define the successor function suc which takes a key k ∈ (F 82 ) 8 , a state s and
194 * an input y ∈ F 2 and outputs the successor state s ′ . We overload the function suc
195 * to multiple bit input x ∈ F n 2 which we define as
196 * @param k - array containing 8 bytes
198 static State suc(uint8_t *k, State s, BitstreamIn *bitstream) {
199 if (bitsLeft(bitstream) == 0) {
200 return s;
202 bool lastbit = tailBit(bitstream);
203 return successor(k, suc(k, s, bitstream), lastbit);
207 * Definition 5 (Output). Define the function output which takes an internal
208 * state s =< l, r, t, b > and returns the bit r 5 . We also define the function output
209 * on multiple bits input which takes a key k, a state s and an input x ∈ F n 2 as
210 * output(k, s, ǫ) = ǫ
211 * output(k, s, x 0 . . . x n ) = output(s) · output(k, s ′ , x 1 . . . x n )
212 * where s ′ = suc(k, s, x 0 ).
214 static void output(uint8_t *k, State s, BitstreamIn *in, BitstreamOut *out) {
215 if (bitsLeft(in) == 0) {
216 return;
218 pushBit(out, (s.r >> 2) & 1);
219 //Remove first bit
220 uint8_t x0 = headBit(in);
221 State ss = successor(k, s, x0);
222 output(k, ss, in, out);
226 * Definition 6 (Initial state). Define the function init which takes as input a
227 * key k ∈ (F 82 ) 8 and outputs the initial cipher state s =< l, r, t, b >
230 static State init(uint8_t *k) {
231 State s = {
232 ((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
233 ((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
234 0x4c, // b
235 0xE012 // t
237 return s;
240 static void MAC(uint8_t *k, BitstreamIn input, BitstreamOut out) {
241 uint8_t zeroes_32[] = {0, 0, 0, 0};
242 BitstreamIn input_32_zeroes = {zeroes_32, sizeof(zeroes_32) * 8, 0};
243 State initState = suc(k, init(k), &input);
244 output(k, initState, &input_32_zeroes, &out);
247 void doMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4]) {
248 uint8_t cc_nr[13] = { 0 };
249 uint8_t div_key[8];
251 memcpy(cc_nr, cc_nr_p, 12);
252 memcpy(div_key, div_key_p, 8);
254 reverse_arraybytes(cc_nr, 12);
255 BitstreamIn bitstream = {cc_nr, 12 * 8, 0};
256 uint8_t dest [] = {0, 0, 0, 0, 0, 0, 0, 0};
257 BitstreamOut out = { dest, sizeof(dest) * 8, 0 };
258 MAC(div_key, bitstream, out);
259 //The output MAC must also be reversed
260 reverse_arraybytes(dest, sizeof(dest));
261 memcpy(mac, dest, 4);
264 void doMAC_N(uint8_t *address_data_p, uint8_t address_data_size, uint8_t *div_key_p, uint8_t mac[4]) {
265 uint8_t *address_data;
266 uint8_t div_key[8];
267 address_data = (uint8_t *) calloc(address_data_size, sizeof(uint8_t));
269 memcpy(address_data, address_data_p, address_data_size);
270 memcpy(div_key, div_key_p, 8);
272 reverse_arraybytes(address_data, address_data_size);
273 BitstreamIn bitstream = {address_data, address_data_size * 8, 0};
274 uint8_t dest [] = {0, 0, 0, 0, 0, 0, 0, 0};
275 BitstreamOut out = { dest, sizeof(dest) * 8, 0 };
276 MAC(div_key, bitstream, out);
277 //The output MAC must also be reversed
278 reverse_arraybytes(dest, sizeof(dest));
279 memcpy(mac, dest, 4);
280 free(address_data);
283 #ifndef ON_DEVICE
284 int testMAC(void) {
285 PrintAndLogEx(SUCCESS, "Testing MAC calculation...");
287 //From the "dismantling.IClass" paper:
288 uint8_t cc_nr[] = {0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0};
289 //From the paper
290 uint8_t div_key[8] = {0xE0, 0x33, 0xCA, 0x41, 0x9A, 0xEE, 0x43, 0xF9};
291 uint8_t correct_MAC[4] = {0x1d, 0x49, 0xC9, 0xDA};
293 uint8_t calculated_mac[4] = {0};
294 doMAC(cc_nr, div_key, calculated_mac);
296 if (memcmp(calculated_mac, correct_MAC, 4) == 0) {
297 PrintAndLogEx(SUCCESS, " MAC calculation (%s)", _GREEN_("ok"));
298 } else {
299 PrintAndLogEx(FAILED, " MAC calculation (%s)", _RED_("failed"));
300 printarr(" Calculated_MAC", calculated_mac, 4);
301 printarr(" Correct_MAC ", correct_MAC, 4);
302 return PM3_ESOFT;
304 return PM3_SUCCESS;
306 #endif