]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/cipherutils.c
ADD: added a lot of ic ids to cmdhf15.c Thanks to Asper for the list.
[proxmark3-svn] / client / loclass / cipherutils.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 IClassCipher. If not, see <http://www.gnu.org/licenses/>.
34 ****************************************************************************/
35
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include "fileutils.h"
40 #include "cipherutils.h"
41 /**
42 *
43 * @brief Return and remove the first bit (x0) in the stream : <x0 x1 x2 x3 ... xn >
44 * @param stream
45 * @return
46 */
47 bool headBit( BitstreamIn *stream)
48 {
49 int bytepos = stream->position >> 3; // divide by 8
50 int bitpos = (stream->position++) & 7; // mask out 00000111
51 return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
52 }
53 /**
54 * @brief Return and remove the last bit (xn) in the stream: <x0 x1 x2 ... xn>
55 * @param stream
56 * @return
57 */
58 bool tailBit( BitstreamIn *stream)
59 {
60 int bitpos = stream->numbits -1 - (stream->position++);
61
62 int bytepos= bitpos >> 3;
63 bitpos &= 7;
64 return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
65 }
66 /**
67 * @brief Pushes bit onto the stream
68 * @param stream
69 * @param bit
70 */
71 void pushBit( BitstreamOut* stream, bool bit)
72 {
73 int bytepos = stream->position >> 3; // divide by 8
74 int bitpos = stream->position & 7;
75 *(stream->buffer+bytepos) |= (bit & 1) << (7 - bitpos);
76 stream->position++;
77 stream->numbits++;
78 }
79
80 /**
81 * @brief Pushes the lower six bits onto the stream
82 * as b0 b1 b2 b3 b4 b5 b6
83 * @param stream
84 * @param bits
85 */
86 void push6bits( BitstreamOut* stream, uint8_t bits)
87 {
88 pushBit(stream, bits & 0x20);
89 pushBit(stream, bits & 0x10);
90 pushBit(stream, bits & 0x08);
91 pushBit(stream, bits & 0x04);
92 pushBit(stream, bits & 0x02);
93 pushBit(stream, bits & 0x01);
94 }
95
96 /**
97 * @brief bitsLeft
98 * @param stream
99 * @return number of bits left in stream
100 */
101 int bitsLeft( BitstreamIn *stream)
102 {
103 return stream->numbits - stream->position;
104 }
105 /**
106 * @brief numBits
107 * @param stream
108 * @return Number of bits stored in stream
109 */
110 int numBits(BitstreamOut *stream)
111 {
112 return stream->numbits;
113 }
114
115 void x_num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
116 {
117 while (len--) {
118 dest[len] = (uint8_t) n;
119 n >>= 8;
120 }
121 }
122
123 uint64_t x_bytes_to_num(uint8_t* src, size_t len)
124 {
125 uint64_t num = 0;
126 while (len--)
127 {
128 num = (num << 8) | (*src);
129 src++;
130 }
131 return num;
132 }
133 uint8_t reversebytes(uint8_t b) {
134 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
135 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
136 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
137 return b;
138 }
139 void reverse_arraybytes(uint8_t* arr, size_t len)
140 {
141 uint8_t i;
142 for( i =0; i< len ; i++)
143 {
144 arr[i] = reversebytes(arr[i]);
145 }
146 }
147 void reverse_arraycopy(uint8_t* arr, uint8_t* dest, size_t len)
148 {
149 uint8_t i;
150 for( i =0; i< len ; i++)
151 {
152 dest[i] = reversebytes(arr[i]);
153 }
154 }
155
156 void printarr(char * name, uint8_t* arr, int len)
157 {
158 int cx;
159 size_t outsize = 40+strlen(name)+len*5;
160 char* output = malloc(outsize);
161 memset(output, 0,outsize);
162
163 int i ;
164 cx = snprintf(output,outsize, "uint8_t %s[] = {", name);
165 for(i =0 ; i< len ; i++)
166 {
167 cx += snprintf(output+cx,outsize-cx,"0x%02x,",*(arr+i));//5 bytes per byte
168 }
169 cx += snprintf(output+cx,outsize-cx,"};");
170 prnlog(output);
171 }
172
173 void printvar(char * name, uint8_t* arr, int len)
174 {
175 int cx;
176 size_t outsize = 40+strlen(name)+len*2;
177 char* output = malloc(outsize);
178 memset(output, 0,outsize);
179
180 int i ;
181 cx = snprintf(output,outsize,"%s = ", name);
182 for(i =0 ; i< len ; i++)
183 {
184 cx += snprintf(output+cx,outsize-cx,"%02x",*(arr+i));//2 bytes per byte
185 }
186
187 prnlog(output);
188 }
189
190 void printarr_human_readable(char * title, uint8_t* arr, int len)
191 {
192 int cx;
193 size_t outsize = 100+strlen(title)+len*4;
194 char* output = malloc(outsize);
195 memset(output, 0,outsize);
196
197
198 int i;
199 cx = snprintf(output,outsize, "\n\t%s\n", title);
200 for(i =0 ; i< len ; i++)
201 {
202 if(i % 16 == 0)
203 cx += snprintf(output+cx,outsize-cx,"\n%02x| ", i );
204 cx += snprintf(output+cx,outsize-cx, "%02x ",*(arr+i));
205 }
206 prnlog(output);
207 free(output);
208 }
209
210 //-----------------------------
211 // Code for testing below
212 //-----------------------------
213
214
215 int testBitStream()
216 {
217 uint8_t input [] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
218 uint8_t output [] = {0,0,0,0,0,0,0,0};
219 BitstreamIn in = { input, sizeof(input) * 8,0};
220 BitstreamOut out ={ output, 0,0}
221 ;
222 while(bitsLeft(&in) > 0)
223 {
224 pushBit(&out, headBit(&in));
225 //printf("Bits left: %d\n", bitsLeft(&in));
226 //printf("Bits out: %d\n", numBits(&out));
227 }
228 if(memcmp(input, output, sizeof(input)) == 0)
229 {
230 prnlog(" Bitstream test 1 ok");
231 }else
232 {
233 prnlog(" Bitstream test 1 failed");
234 uint8_t i;
235 for(i = 0 ; i < sizeof(input) ; i++)
236 {
237 prnlog(" IN %02x, OUT %02x", input[i], output[i]);
238 }
239 return 1;
240 }
241 return 0;
242 }
243
244 int testReversedBitstream()
245 {
246 uint8_t input [] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
247 uint8_t reverse [] = {0,0,0,0,0,0,0,0};
248 uint8_t output [] = {0,0,0,0,0,0,0,0};
249 BitstreamIn in = { input, sizeof(input) * 8,0};
250 BitstreamOut out ={ output, 0,0};
251 BitstreamIn reversed_in ={ reverse, sizeof(input)*8,0};
252 BitstreamOut reversed_out ={ reverse,0 ,0};
253
254 while(bitsLeft(&in) > 0)
255 {
256 pushBit(&reversed_out, tailBit(&in));
257 }
258 while(bitsLeft(&reversed_in) > 0)
259 {
260 pushBit(&out, tailBit(&reversed_in));
261 }
262 if(memcmp(input, output, sizeof(input)) == 0)
263 {
264 prnlog(" Bitstream test 2 ok");
265 }else
266 {
267 prnlog(" Bitstream test 2 failed");
268 uint8_t i;
269 for(i = 0 ; i < sizeof(input) ; i++)
270 {
271 prnlog(" IN %02x, MIDDLE: %02x, OUT %02x", input[i],reverse[i], output[i]);
272 }
273 return 1;
274 }
275 return 0;
276 }
277
278
279 int testCipherUtils(void)
280 {
281 prnlog("[+] Testing some internals...");
282 int retval = 0;
283 retval |= testBitStream();
284 retval |= testReversedBitstream();
285 return retval;
286 }
Impressum, Datenschutz