]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/optimized_cipher.c
Merge pull request #114 from pwpiwi/iso14443b_fix
[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 #include <stdlib.h>
65 #include <string.h>
66 #include <stdbool.h>
67 #include <stdint.h>
68 #include <time.h>
69
70
71 #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))
72
73 #define opt_B(s) (((s->b >> 6) ^ (s->b >> 5) ^ (s->b >> 4) ^ (s->b)) & 0x1)
74
75 #define opt__select(x,y,r) (4 & (((r & (r << 2)) >> 5) ^ ((r & ~(r << 2)) >> 4) ^ ( (r | r << 2) >> 3)))\
76 |(2 & (((r | r << 2) >> 6) ^ ( (r | r << 2) >> 1) ^ (r >> 5) ^ r ^ ((x^y) << 1)))\
77 |(1 & (((r & ~(r << 2)) >> 4) ^ ((r & (r << 2)) >> 3) ^ r ^ x))
78
79 /*
80 * Some background on the expression above can be found here...
81 uint8_t xopt__select(bool x, bool y, uint8_t r)
82 {
83 uint8_t r_ls2 = r << 2;
84 uint8_t r_and_ls2 = r & r_ls2;
85 uint8_t r_or_ls2 = r | r_ls2;
86
87 //r: r0 r1 r2 r3 r4 r5 r6 r7
88 //r_ls2: r2 r3 r4 r5 r6 r7 0 0
89 // z0
90 // z1
91
92 // uint8_t z0 = (r0 & r2) ^ (r1 & ~r3) ^ (r2 | r4); // <-- original
93 uint8_t z0 = (r_and_ls2 >> 5) ^ ((r & ~r_ls2) >> 4) ^ ( r_or_ls2 >> 3);
94
95 // uint8_t z1 = (r0 | r2) ^ ( r5 | r7) ^ r1 ^ r6 ^ x ^ y; // <-- original
96 uint8_t z1 = (r_or_ls2 >> 6) ^ ( r_or_ls2 >> 1) ^ (r >> 5) ^ r ^ ((x^y) << 1);
97
98 // uint8_t z2 = (r3 & ~r5) ^ (r4 & r6 ) ^ r7 ^ x; // <-- original
99 uint8_t z2 = ((r & ~r_ls2) >> 4) ^ (r_and_ls2 >> 3) ^ r ^ x;
100
101 return (z0 & 4) | (z1 & 2) | (z2 & 1);
102 }
103 */
104
105 void opt_successor(const uint8_t* k, State *s, bool y, State* successor)
106 {
107
108 uint8_t Tt = 1 & opt_T(s);
109
110 successor->t = (s->t >> 1);
111 successor->t |= (Tt ^ (s->r >> 7 & 0x1) ^ (s->r >> 3 & 0x1)) << 15;
112
113 successor->b = s->b >> 1;
114 successor->b |= (opt_B(s) ^ (s->r & 0x1)) << 7;
115
116 successor->r = (k[opt__select(Tt,y,s->r)] ^ successor->b) + s->l ;
117 successor->l = successor->r+s->r;
118
119 }
120
121 void opt_suc(const uint8_t* k,State* s, uint8_t *in, uint8_t length, bool add32Zeroes)
122 {
123 State x2;
124 int i;
125 uint8_t head = 0;
126 for(i =0 ; i < length ; i++)
127 {
128 head = 1 & (in[i] >> 7);
129 opt_successor(k,s,head,&x2);
130
131 head = 1 & (in[i] >> 6);
132 opt_successor(k,&x2,head,s);
133
134 head = 1 & (in[i] >> 5);
135 opt_successor(k,s,head,&x2);
136
137 head = 1 & (in[i] >> 4);
138 opt_successor(k,&x2,head,s);
139
140 head = 1 & (in[i] >> 3);
141 opt_successor(k,s,head,&x2);
142
143 head = 1 & (in[i] >> 2);
144 opt_successor(k,&x2,head,s);
145
146 head = 1 & (in[i] >> 1);
147 opt_successor(k,s,head,&x2);
148
149 head = 1 & in[i];
150 opt_successor(k,&x2,head,s);
151
152 }
153 //For tag MAC, an additional 32 zeroes
154 if(add32Zeroes)
155 for(i =0 ; i < 16 ; i++)
156 {
157 opt_successor(k,s,0,&x2);
158 opt_successor(k,&x2,0,s);
159 }
160 }
161
162 void opt_output(const uint8_t* k,State* s, uint8_t *buffer)
163 {
164 uint8_t times = 0;
165 uint8_t bout = 0;
166 State temp = {0,0,0,0};
167 for( ; times < 4 ; times++)
168 {
169 bout =0;
170 bout |= (s->r & 0x4) << 5;
171 opt_successor(k,s,0,&temp);
172 bout |= (temp.r & 0x4) << 4;
173 opt_successor(k,&temp,0,s);
174 bout |= (s->r & 0x4) << 3;
175 opt_successor(k,s,0,&temp);
176 bout |= (temp.r & 0x4) << 2;
177 opt_successor(k,&temp,0,s);
178 bout |= (s->r & 0x4) << 1;
179 opt_successor(k,s,0,&temp);
180 bout |= (temp.r & 0x4) ;
181 opt_successor(k,&temp,0,s);
182 bout |= (s->r & 0x4) >> 1;
183 opt_successor(k,s,0,&temp);
184 bout |= (temp.r & 0x4) >> 2;
185 opt_successor(k,&temp,0,s);
186 buffer[times] = bout;
187 }
188
189 }
190
191 void opt_MAC(uint8_t* k, uint8_t* input, uint8_t* out)
192 {
193 State _init = {
194 ((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
195 ((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
196 0x4c, // b
197 0xE012 // t
198 };
199
200 opt_suc(k,&_init,input,12, false);
201 //printf("\noutp ");
202 opt_output(k,&_init, out);
203 }
204 uint8_t rev_byte(uint8_t b) {
205 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
206 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
207 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
208 return b;
209 }
210 void opt_reverse_arraybytecpy(uint8_t* dest, uint8_t *src, size_t len)
211 {
212 uint8_t i;
213 for( i =0; i< len ; i++)
214 dest[i] = rev_byte(src[i]);
215 }
216
217 void opt_doReaderMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4])
218 {
219 static uint8_t cc_nr[12];
220
221 opt_reverse_arraybytecpy(cc_nr, cc_nr_p,12);
222 uint8_t dest []= {0,0,0,0,0,0,0,0};
223 opt_MAC(div_key_p,cc_nr, dest);
224 //The output MAC must also be reversed
225 opt_reverse_arraybytecpy(mac, dest,4);
226 return;
227 }
228 void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4])
229 {
230 static uint8_t cc_nr[8+4+4];
231 opt_reverse_arraybytecpy(cc_nr, cc_p,12);
232 State _init = {
233 ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
234 ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
235 0x4c, // b
236 0xE012 // t
237 };
238 opt_suc(div_key_p,&_init,cc_nr, 12,true);
239 uint8_t dest []= {0,0,0,0};
240 opt_output(div_key_p,&_init, dest);
241 //The output MAC must also be reversed
242 opt_reverse_arraybytecpy(mac, dest,4);
243 return;
244
245 }
246 /**
247 * The tag MAC can be divided (both can, but no point in dividing the reader mac) into
248 * two functions, since the first 8 bytes are known, we can pre-calculate the state
249 * reached after feeding CC to the cipher.
250 * @param cc_p
251 * @param div_key_p
252 * @return the cipher state
253 */
254 State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p)
255 {
256 static uint8_t cc_nr[8];
257 opt_reverse_arraybytecpy(cc_nr, cc_p,8);
258 State _init = {
259 ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
260 ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
261 0x4c, // b
262 0xE012 // t
263 };
264 opt_suc(div_key_p,&_init,cc_nr, 8,false);
265 return _init;
266 }
267 /**
268 * The second part of the tag MAC calculation, since the CC is already calculated into the state,
269 * this function is fed only the NR, and internally feeds the remaining 32 0-bits to generate the tag
270 * MAC response.
271 * @param _init - precalculated cipher state
272 * @param nr - the reader challenge
273 * @param mac - where to store the MAC
274 * @param div_key_p - the key to use
275 */
276 void opt_doTagMAC_2(State _init, uint8_t* nr, uint8_t mac[4], const uint8_t* div_key_p)
277 {
278 static uint8_t _nr [4];
279 opt_reverse_arraybytecpy(_nr, nr, 4);
280 opt_suc(div_key_p,&_init,_nr, 4, true);
281 //opt_suc(div_key_p,&_init,nr, 4, false);
282 uint8_t dest []= {0,0,0,0};
283 opt_output(div_key_p,&_init, dest);
284 //The output MAC must also be reversed
285 opt_reverse_arraybytecpy(mac, dest,4);
286 return;
287 }
Impressum, Datenschutz