]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/optimized_cipher.c
CHG: `hf em` commands. Client side now has same commands as Peter Fillmores 14atagfuz...
[proxmark3-svn] / armsrc / optimized_cipher.c
1 /*****************************************************************************
2 * WARNING
3 *
4 * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
5 *
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.
9 *
10 * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
11 *
12 *****************************************************************************
13 *
14 * This file is part of loclass. It is a reconstructon of the cipher engine
15 * used in iClass, and RFID techology.
16 *
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".
20 *
21 * Copyright (C) 2014 Martin Holst Swende
22 *
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.
26 *
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.
31 *
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/>.
34 *
35 *
36 *
37 ****************************************************************************/
38
39 /**
40
41 This file contains an optimized version of the MAC-calculation algorithm. Some measurements on
42 a std laptop showed it runs in about 1/3 of the time:
43
44 Std: 0.428962
45 Opt: 0.151609
46
47 Additionally, it is self-reliant, not requiring e.g. bitstreams from the cipherutils, thus can
48 be easily dropped into a code base.
49
50 The optimizations have been performed in the following steps:
51 * Parameters passed by reference instead of by value.
52 * Iteration instead of recursion, un-nesting recursive loops into for-loops.
53 * Handling of bytes instead of individual bits, for less shuffling and masking
54 * Less creation of "objects", structs, and instead reuse of alloc:ed memory
55 * Inlining some functions via #define:s
56
57 As a consequence, this implementation is less generic. Also, I haven't bothered documenting this.
58 For a thorough documentation, check out the MAC-calculation within cipher.c instead.
59
60 -- MHS 2015
61 **/
62
63 #include "optimized_cipher.h"
64
65 #define opt_T(s) (0x1 & ((s->t >> 15) ^ (s->t >> 14)^ (s->t >> 10)^ (s->t >> 8)^ (s->t >> 5)^ (s->t >> 4)^ (s->t >> 1)^ s->t))
66
67 #define opt_B(s) (((s->b >> 6) ^ (s->b >> 5) ^ (s->b >> 4) ^ (s->b)) & 0x1)
68
69 #define opt__select(x,y,r) (4 & (((r & (r << 2)) >> 5) ^ ((r & ~(r << 2)) >> 4) ^ ( (r | r << 2) >> 3)))\
70 |(2 & (((r | r << 2) >> 6) ^ ( (r | r << 2) >> 1) ^ (r >> 5) ^ r ^ ((x^y) << 1)))\
71 |(1 & (((r & ~(r << 2)) >> 4) ^ ((r & (r << 2)) >> 3) ^ r ^ x))
72
73 /*
74 * Some background on the expression above can be found here...
75 uint8_t xopt__select(bool x, bool y, uint8_t r)
76 {
77 uint8_t r_ls2 = r << 2;
78 uint8_t r_and_ls2 = r & r_ls2;
79 uint8_t r_or_ls2 = r | r_ls2;
80
81 //r: r0 r1 r2 r3 r4 r5 r6 r7
82 //r_ls2: r2 r3 r4 r5 r6 r7 0 0
83 // z0
84 // z1
85
86 // uint8_t z0 = (r0 & r2) ^ (r1 & ~r3) ^ (r2 | r4); // <-- original
87 uint8_t z0 = (r_and_ls2 >> 5) ^ ((r & ~r_ls2) >> 4) ^ ( r_or_ls2 >> 3);
88
89 // uint8_t z1 = (r0 | r2) ^ ( r5 | r7) ^ r1 ^ r6 ^ x ^ y; // <-- original
90 uint8_t z1 = (r_or_ls2 >> 6) ^ ( r_or_ls2 >> 1) ^ (r >> 5) ^ r ^ ((x^y) << 1);
91
92 // uint8_t z2 = (r3 & ~r5) ^ (r4 & r6 ) ^ r7 ^ x; // <-- original
93 uint8_t z2 = ((r & ~r_ls2) >> 4) ^ (r_and_ls2 >> 3) ^ r ^ x;
94
95 return (z0 & 4) | (z1 & 2) | (z2 & 1);
96 }
97 */
98
99 void opt_successor(const uint8_t* k, State *s, bool y, State* successor)
100 {
101 uint8_t Tt = 1 & opt_T(s);
102
103 successor->t = (s->t >> 1);
104 successor->t |= (Tt ^ (s->r >> 7 & 0x1) ^ (s->r >> 3 & 0x1)) << 15;
105
106 successor->b = s->b >> 1;
107 successor->b |= (opt_B(s) ^ (s->r & 0x1)) << 7;
108
109 successor->r = (k[opt__select(Tt,y,s->r)] ^ successor->b) + s->l ;
110 successor->l = successor->r+s->r;
111
112 }
113
114 void opt_suc(const uint8_t* k,State* s, uint8_t *in, uint8_t length, bool add32Zeroes)
115 {
116 State x2;
117 int i;
118 uint8_t head = 0;
119 for(i =0 ; i < length ; i++)
120 {
121 head = 1 & (in[i] >> 7);
122 opt_successor(k,s,head,&x2);
123
124 head = 1 & (in[i] >> 6);
125 opt_successor(k,&x2,head,s);
126
127 head = 1 & (in[i] >> 5);
128 opt_successor(k,s,head,&x2);
129
130 head = 1 & (in[i] >> 4);
131 opt_successor(k,&x2,head,s);
132
133 head = 1 & (in[i] >> 3);
134 opt_successor(k,s,head,&x2);
135
136 head = 1 & (in[i] >> 2);
137 opt_successor(k,&x2,head,s);
138
139 head = 1 & (in[i] >> 1);
140 opt_successor(k,s,head,&x2);
141
142 head = 1 & in[i];
143 opt_successor(k,&x2,head,s);
144
145 }
146 //For tag MAC, an additional 32 zeroes
147 if(add32Zeroes)
148 for(i =0 ; i < 16 ; i++)
149 {
150 opt_successor(k,s,0,&x2);
151 opt_successor(k,&x2,0,s);
152 }
153 }
154
155 void opt_output(const uint8_t* k,State* s, uint8_t *buffer)
156 {
157 uint8_t times = 0;
158 uint8_t bout = 0;
159 State temp = {0,0,0,0};
160 for( ; times < 4 ; times++)
161 {
162 bout =0;
163 bout |= (s->r & 0x4) << 5;
164 opt_successor(k,s,0,&temp);
165 bout |= (temp.r & 0x4) << 4;
166 opt_successor(k,&temp,0,s);
167 bout |= (s->r & 0x4) << 3;
168 opt_successor(k,s,0,&temp);
169 bout |= (temp.r & 0x4) << 2;
170 opt_successor(k,&temp,0,s);
171 bout |= (s->r & 0x4) << 1;
172 opt_successor(k,s,0,&temp);
173 bout |= (temp.r & 0x4) ;
174 opt_successor(k,&temp,0,s);
175 bout |= (s->r & 0x4) >> 1;
176 opt_successor(k,s,0,&temp);
177 bout |= (temp.r & 0x4) >> 2;
178 opt_successor(k,&temp,0,s);
179 buffer[times] = bout;
180 }
181
182 }
183
184 void opt_MAC(uint8_t* k, uint8_t* input, uint8_t* out)
185 {
186 State _init = {
187 ((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
188 ((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
189 0x4c, // b
190 0xE012 // t
191 };
192
193 opt_suc(k,&_init,input,12, false);
194 //printf("\noutp ");
195 opt_output(k,&_init, out);
196 }
197 uint8_t rev_byte(uint8_t b) {
198 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
199 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
200 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
201 return b;
202 }
203 void opt_reverse_arraybytecpy(uint8_t* dest, uint8_t *src, size_t len)
204 {
205 uint8_t i;
206 for( i =0; i< len ; i++)
207 dest[i] = rev_byte(src[i]);
208 }
209
210 void opt_doReaderMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4])
211 {
212 static uint8_t cc_nr[12];
213
214 opt_reverse_arraybytecpy(cc_nr, cc_nr_p, 12);
215 uint8_t dest []= {0,0,0,0,0,0,0,0};
216 opt_MAC(div_key_p, cc_nr, dest);
217 //The output MAC must also be reversed
218 opt_reverse_arraybytecpy(mac, dest, 4);
219 return;
220 }
221 void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4])
222 {
223 static uint8_t cc_nr[8+4+4];
224 opt_reverse_arraybytecpy(cc_nr, cc_p, 12);
225 State _init = {
226 ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
227 ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
228 0x4c, // b
229 0xE012 // t
230 };
231 opt_suc(div_key_p, &_init, cc_nr, 12, true);
232 uint8_t dest []= {0,0,0,0};
233 opt_output(div_key_p, &_init, dest);
234 //The output MAC must also be reversed
235 opt_reverse_arraybytecpy(mac, dest,4);
236 return;
237
238 }
239 /**
240 * The tag MAC can be divided (both can, but no point in dividing the reader mac) into
241 * two functions, since the first 8 bytes are known, we can pre-calculate the state
242 * reached after feeding CC to the cipher.
243 * @param cc_p
244 * @param div_key_p
245 * @return the cipher state
246 */
247 State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p)
248 {
249 static uint8_t cc_nr[8];
250 opt_reverse_arraybytecpy(cc_nr, cc_p, 8);
251 State _init = {
252 ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
253 ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
254 0x4c, // b
255 0xE012 // t
256 };
257 opt_suc(div_key_p, &_init, cc_nr, 8, false);
258 return _init;
259 }
260 /**
261 * The second part of the tag MAC calculation, since the CC is already calculated into the state,
262 * this function is fed only the NR, and internally feeds the remaining 32 0-bits to generate the tag
263 * MAC response.
264 * @param _init - precalculated cipher state
265 * @param nr - the reader challenge
266 * @param mac - where to store the MAC
267 * @param div_key_p - the key to use
268 */
269 void opt_doTagMAC_2(State _init, uint8_t* nr, uint8_t mac[4], const uint8_t* div_key_p)
270 {
271 static uint8_t _nr [4];
272 opt_reverse_arraybytecpy(_nr, nr, 4);
273 opt_suc(div_key_p, &_init,_nr, 4, true);
274 //opt_suc(div_key_p, &_init,nr, 4, false);
275 uint8_t dest []= {0,0,0,0};
276 opt_output(div_key_p, &_init, dest);
277 //The output MAC must also be reversed
278 opt_reverse_arraybytecpy(mac, dest,4);
279 return;
280 }
Impressum, Datenschutz