]> git.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/cipherutils.c
Merge remote-tracking branch 'origin/master' into PenturaLabs-iclass-research
[proxmark3-svn] / client / loclass / cipherutils.c
1 /*****************************************************************************
2 * This file is part of iClassCipher. It is a reconstructon of the cipher engine
3 * used in iClass, and RFID techology.
4 *
5 * The implementation is based on the work performed by
6 * Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and
7 * Milosch Meriac in the paper "Dismantling IClass".
8 *
9 * Copyright (C) 2014 Martin Holst Swende
10 *
11 * This is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as published
13 * by the Free Software Foundation.
14 *
15 * This file is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with IClassCipher. If not, see <http://www.gnu.org/licenses/>.
22 ****************************************************************************/
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include "fileutils.h"
28 #include "cipherutils.h"
29 /**
30 *
31 * @brief Return and remove the first bit (x0) in the stream : <x0 x1 x2 x3 ... xn >
32 * @param stream
33 * @return
34 */
35 bool headBit( BitstreamIn *stream)
36 {
37 int bytepos = stream->position >> 3; // divide by 8
38 int bitpos = (stream->position++) & 7; // mask out 00000111
39 return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
40 }
41 /**
42 * @brief Return and remove the last bit (xn) in the stream: <x0 x1 x2 ... xn>
43 * @param stream
44 * @return
45 */
46 bool tailBit( BitstreamIn *stream)
47 {
48 int bitpos = stream->numbits -1 - (stream->position++);
49
50 int bytepos= bitpos >> 3;
51 bitpos &= 7;
52 return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
53 }
54 /**
55 * @brief Pushes bit onto the stream
56 * @param stream
57 * @param bit
58 */
59 void pushBit( BitstreamOut* stream, bool bit)
60 {
61 int bytepos = stream->position >> 3; // divide by 8
62 int bitpos = stream->position & 7;
63 *(stream->buffer+bytepos) |= (bit & 1) << (7 - bitpos);
64 stream->position++;
65 stream->numbits++;
66 }
67
68 /**
69 * @brief Pushes the lower six bits onto the stream
70 * as b0 b1 b2 b3 b4 b5 b6
71 * @param stream
72 * @param bits
73 */
74 void push6bits( BitstreamOut* stream, uint8_t bits)
75 {
76 pushBit(stream, bits & 0x20);
77 pushBit(stream, bits & 0x10);
78 pushBit(stream, bits & 0x08);
79 pushBit(stream, bits & 0x04);
80 pushBit(stream, bits & 0x02);
81 pushBit(stream, bits & 0x01);
82 }
83
84 /**
85 * @brief bitsLeft
86 * @param stream
87 * @return number of bits left in stream
88 */
89 int bitsLeft( BitstreamIn *stream)
90 {
91 return stream->numbits - stream->position;
92 }
93 /**
94 * @brief numBits
95 * @param stream
96 * @return Number of bits stored in stream
97 */
98 int numBits(BitstreamOut *stream)
99 {
100 return stream->numbits;
101 }
102
103 void x_num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
104 {
105 while (len--) {
106 dest[len] = (uint8_t) n;
107 n >>= 8;
108 }
109 }
110
111 uint64_t x_bytes_to_num(uint8_t* src, size_t len)
112 {
113 uint64_t num = 0;
114 while (len--)
115 {
116 num = (num << 8) | (*src);
117 src++;
118 }
119 return num;
120 }
121 uint8_t reversebytes(uint8_t b) {
122 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
123 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
124 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
125 return b;
126 }
127 void reverse_arraybytes(uint8_t* arr, size_t len)
128 {
129 uint8_t i;
130 for( i =0; i< len ; i++)
131 {
132 arr[i] = reversebytes(arr[i]);
133 }
134 }
135 void reverse_arraycopy(uint8_t* arr, uint8_t* dest, size_t len)
136 {
137 uint8_t i;
138 for( i =0; i< len ; i++)
139 {
140 dest[i] = reversebytes(arr[i]);
141 }
142 }
143
144 void printarr(char * name, uint8_t* arr, int len)
145 {
146 int cx;
147 size_t outsize = 40+strlen(name)+len*5;
148 char* output = malloc(outsize);
149 memset(output, 0,outsize);
150
151 int i ;
152 cx = snprintf(output,outsize, "uint8_t %s[] = {", name);
153 for(i =0 ; i< len ; i++)
154 {
155 cx += snprintf(output+cx,outsize-cx,"0x%02x,",*(arr+i));//5 bytes per byte
156 }
157 cx += snprintf(output+cx,outsize-cx,"};");
158 prnlog(output);
159 }
160
161 void printvar(char * name, uint8_t* arr, int len)
162 {
163 int cx;
164 size_t outsize = 40+strlen(name)+len*2;
165 char* output = malloc(outsize);
166 memset(output, 0,outsize);
167
168 int i ;
169 cx = snprintf(output,outsize,"%s = ", name);
170 for(i =0 ; i< len ; i++)
171 {
172 cx += snprintf(output+cx,outsize-cx,"%02x",*(arr+i));//2 bytes per byte
173 }
174
175 prnlog(output);
176 }
177
178 void printarr_human_readable(char * title, uint8_t* arr, int len)
179 {
180 int cx;
181 size_t outsize = 100+strlen(title)+len*4;
182 char* output = malloc(outsize);
183 memset(output, 0,outsize);
184
185
186 int i;
187 cx = snprintf(output,outsize, "\n\t%s\n", title);
188 for(i =0 ; i< len ; i++)
189 {
190 if(i % 16 == 0)
191 cx += snprintf(output+cx,outsize-cx,"\n%02x| ", i );
192 cx += snprintf(output+cx,outsize-cx, "%02x ",*(arr+i));
193 }
194 prnlog(output);
195 }
196
197 //-----------------------------
198 // Code for testing below
199 //-----------------------------
200
201
202 int testBitStream()
203 {
204 uint8_t input [] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
205 uint8_t output [] = {0,0,0,0,0,0,0,0};
206 BitstreamIn in = { input, sizeof(input) * 8,0};
207 BitstreamOut out ={ output, 0,0}
208 ;
209 while(bitsLeft(&in) > 0)
210 {
211 pushBit(&out, headBit(&in));
212 //printf("Bits left: %d\n", bitsLeft(&in));
213 //printf("Bits out: %d\n", numBits(&out));
214 }
215 if(memcmp(input, output, sizeof(input)) == 0)
216 {
217 prnlog(" Bitstream test 1 ok");
218 }else
219 {
220 prnlog(" Bitstream test 1 failed");
221 uint8_t i;
222 for(i = 0 ; i < sizeof(input) ; i++)
223 {
224 prnlog(" IN %02x, OUT %02x", input[i], output[i]);
225 }
226 return 1;
227 }
228 return 0;
229 }
230
231 int testReversedBitstream()
232 {
233 uint8_t input [] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
234 uint8_t reverse [] = {0,0,0,0,0,0,0,0};
235 uint8_t output [] = {0,0,0,0,0,0,0,0};
236 BitstreamIn in = { input, sizeof(input) * 8,0};
237 BitstreamOut out ={ output, 0,0};
238 BitstreamIn reversed_in ={ reverse, sizeof(input)*8,0};
239 BitstreamOut reversed_out ={ reverse,0 ,0};
240
241 while(bitsLeft(&in) > 0)
242 {
243 pushBit(&reversed_out, tailBit(&in));
244 }
245 while(bitsLeft(&reversed_in) > 0)
246 {
247 pushBit(&out, tailBit(&reversed_in));
248 }
249 if(memcmp(input, output, sizeof(input)) == 0)
250 {
251 prnlog(" Bitstream test 2 ok");
252 }else
253 {
254 prnlog(" Bitstream test 2 failed");
255 uint8_t i;
256 for(i = 0 ; i < sizeof(input) ; i++)
257 {
258 prnlog(" IN %02x, MIDDLE: %02x, OUT %02x", input[i],reverse[i], output[i]);
259 }
260 return 1;
261 }
262 return 0;
263 }
264
265
266 int testCipherUtils(void)
267 {
268 prnlog("[+] Testing some internals...");
269 int retval = 0;
270 retval |= testBitStream();
271 retval |= testReversedBitstream();
272 return retval;
273 }
Impressum, Datenschutz