]>
Commit | Line | Data |
---|---|---|
c99dc845 MHS |
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 <stdio.h> | |
65 | #include <stdlib.h> | |
66 | #include <string.h> | |
67 | #include <stdbool.h> | |
68 | #include <stdint.h> | |
69 | #include <time.h> | |
70 | ||
71 | /** | |
72 | * Definition 1 (Cipher state). A cipher state of iClass s is an element of F 40/2 | |
73 | * consisting of the following four components: | |
74 | * 1. the left register l = (l 0 . . . l 7 ) ∈ F 8/2 ; | |
75 | * 2. the right register r = (r 0 . . . r 7 ) ∈ F 8/2 ; | |
76 | * 3. the top register t = (t 0 . . . t 15 ) ∈ F 16/2 . | |
77 | * 4. the bottom register b = (b 0 . . . b 7 ) ∈ F 8/2 . | |
78 | **/ | |
79 | typedef struct { | |
80 | uint8_t l; | |
81 | uint8_t r; | |
82 | uint8_t b; | |
83 | uint16_t t; | |
84 | } State; | |
85 | ||
86 | ||
87 | #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)) | |
88 | ||
89 | #define opt_B(s) (((s->b >> 6) ^ (s->b >> 5) ^ (s->b >> 4) ^ (s->b)) & 0x1) | |
90 | ||
91 | #define opt__select(x,y,r) (4 & (((r & (r << 2)) >> 5) ^ ((r & ~(r << 2)) >> 4) ^ ( (r | r << 2) >> 3)))\ | |
92 | |(2 & (((r | r << 2) >> 6) ^ ( (r | r << 2) >> 1) ^ (r >> 5) ^ r ^ ((x^y) << 1)))\ | |
93 | |(1 & (((r & ~(r << 2)) >> 4) ^ ((r & (r << 2)) >> 3) ^ r ^ x)) | |
94 | ||
95 | /* | |
96 | * Some background on the expression above can be found here... | |
97 | uint8_t xopt__select(bool x, bool y, uint8_t r) | |
98 | { | |
99 | uint8_t r_ls2 = r << 2; | |
100 | uint8_t r_and_ls2 = r & r_ls2; | |
101 | uint8_t r_or_ls2 = r | r_ls2; | |
102 | ||
103 | //r: r0 r1 r2 r3 r4 r5 r6 r7 | |
104 | //r_ls2: r2 r3 r4 r5 r6 r7 0 0 | |
105 | // z0 | |
106 | // z1 | |
107 | ||
108 | // uint8_t z0 = (r0 & r2) ^ (r1 & ~r3) ^ (r2 | r4); // <-- original | |
109 | uint8_t z0 = (r_and_ls2 >> 5) ^ ((r & ~r_ls2) >> 4) ^ ( r_or_ls2 >> 3); | |
110 | ||
111 | // uint8_t z1 = (r0 | r2) ^ ( r5 | r7) ^ r1 ^ r6 ^ x ^ y; // <-- original | |
112 | uint8_t z1 = (r_or_ls2 >> 6) ^ ( r_or_ls2 >> 1) ^ (r >> 5) ^ r ^ ((x^y) << 1); | |
113 | ||
114 | // uint8_t z2 = (r3 & ~r5) ^ (r4 & r6 ) ^ r7 ^ x; // <-- original | |
115 | uint8_t z2 = ((r & ~r_ls2) >> 4) ^ (r_and_ls2 >> 3) ^ r ^ x; | |
116 | ||
117 | return (z0 & 4) | (z1 & 2) | (z2 & 1); | |
118 | } | |
119 | */ | |
120 | ||
121 | void opt_successor(uint8_t* k, State *s, bool y, State* successor) | |
122 | { | |
123 | ||
124 | uint8_t Tt = 1 & opt_T(s); | |
125 | ||
126 | successor->t = (s->t >> 1); | |
127 | successor->t |= (Tt ^ (s->r >> 7 & 0x1) ^ (s->r >> 3 & 0x1)) << 15; | |
128 | ||
129 | successor->b = s->b >> 1; | |
130 | successor->b |= (opt_B(s) ^ (s->r & 0x1)) << 7; | |
131 | ||
132 | successor->r = (k[opt__select(Tt,y,s->r)] ^ successor->b) + s->l ; | |
133 | successor->l = successor->r+s->r; | |
134 | ||
135 | } | |
136 | ||
137 | void opt_suc(uint8_t* k,State* s, uint8_t *in) | |
138 | { | |
139 | State x2; | |
140 | int i; | |
141 | uint8_t head = 0; | |
142 | for(i =0 ; i < 12 ; i++) | |
143 | { | |
144 | head = 1 & (in[i] >> 7); | |
145 | opt_successor(k,s,head,&x2); | |
146 | ||
147 | head = 1 & (in[i] >> 6); | |
148 | opt_successor(k,&x2,head,s); | |
149 | ||
150 | head = 1 & (in[i] >> 5); | |
151 | opt_successor(k,s,head,&x2); | |
152 | ||
153 | head = 1 & (in[i] >> 4); | |
154 | opt_successor(k,&x2,head,s); | |
155 | ||
156 | head = 1 & (in[i] >> 3); | |
157 | opt_successor(k,s,head,&x2); | |
158 | ||
159 | head = 1 & (in[i] >> 2); | |
160 | opt_successor(k,&x2,head,s); | |
161 | ||
162 | head = 1 & (in[i] >> 1); | |
163 | opt_successor(k,s,head,&x2); | |
164 | ||
165 | head = 1 & in[i]; | |
166 | opt_successor(k,&x2,head,s); | |
167 | ||
168 | } | |
169 | ||
170 | } | |
171 | ||
172 | void opt_output(uint8_t* k,State* s, uint8_t *buffer) | |
173 | { | |
174 | uint8_t times = 0; | |
175 | uint8_t bout = 0; | |
176 | State temp = {0,0,0,0}; | |
177 | for( ; times < 4 ; times++) | |
178 | { | |
179 | bout =0; | |
180 | bout |= (s->r & 0x4) << 5; | |
181 | opt_successor(k,s,0,&temp); | |
182 | bout |= (temp.r & 0x4) << 4; | |
183 | opt_successor(k,&temp,0,s); | |
184 | bout |= (s->r & 0x4) << 3; | |
185 | opt_successor(k,s,0,&temp); | |
186 | bout |= (temp.r & 0x4) << 2; | |
187 | opt_successor(k,&temp,0,s); | |
188 | bout |= (s->r & 0x4) << 1; | |
189 | opt_successor(k,s,0,&temp); | |
190 | bout |= (temp.r & 0x4) ; | |
191 | opt_successor(k,&temp,0,s); | |
192 | bout |= (s->r & 0x4) >> 1; | |
193 | opt_successor(k,s,0,&temp); | |
194 | bout |= (temp.r & 0x4) >> 2; | |
195 | opt_successor(k,&temp,0,s); | |
196 | buffer[times] = bout; | |
197 | } | |
198 | ||
199 | } | |
200 | ||
201 | void opt_MAC(uint8_t* k, uint8_t* input, uint8_t* out) | |
202 | { | |
203 | State _init = { | |
204 | ((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l | |
205 | ((k[0] ^ 0x4c) + 0x21) & 0xFF,// r | |
206 | 0x4c, // b | |
207 | 0xE012 // t | |
208 | }; | |
209 | ||
210 | opt_suc(k,&_init,input); | |
211 | //printf("\noutp "); | |
212 | opt_output(k,&_init, out); | |
213 | } | |
214 | uint8_t rev_byte(uint8_t b) { | |
215 | b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; | |
216 | b = (b & 0xCC) >> 2 | (b & 0x33) << 2; | |
217 | b = (b & 0xAA) >> 1 | (b & 0x55) << 1; | |
218 | return b; | |
219 | } | |
220 | void opt_reverse_arraybytecpy(uint8_t* dest, uint8_t *src, size_t len) | |
221 | { | |
222 | uint8_t i; | |
223 | for( i =0; i< len ; i++) | |
224 | dest[i] = rev_byte(src[i]); | |
225 | } | |
226 | ||
227 | void opt_doMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4]) | |
228 | { | |
229 | static uint8_t cc_nr[13]; | |
230 | static uint8_t div_key[8]; | |
231 | ||
232 | opt_reverse_arraybytecpy(cc_nr, cc_nr_p,12); | |
233 | memcpy(div_key,div_key_p,8); | |
234 | uint8_t dest []= {0,0,0,0,0,0,0,0}; | |
235 | opt_MAC(div_key,cc_nr, dest); | |
236 | //The output MAC must also be reversed | |
237 | opt_reverse_arraybytecpy(mac, dest,12); | |
238 | return; | |
239 | } |