1 /*****************************************************************************
2 * This file is part of iClassCipher. It is a reconstructon of the cipher engine
3 * used in iClass, and RFID techology.
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".
9 * Copyright (C) 2014 Martin Holst Swende
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.
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.
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 ****************************************************************************/
27 #include "fileutils.h"
28 #include "cipherutils.h"
31 * @brief Return and remove the first bit (x0) in the stream : <x0 x1 x2 x3 ... xn >
35 bool headBit( BitstreamIn
*stream
)
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;
42 * @brief Return and remove the last bit (xn) in the stream: <x0 x1 x2 ... xn>
46 bool tailBit( BitstreamIn
*stream
)
48 int bitpos
= stream
->numbits
-1 - (stream
->position
++);
50 int bytepos
= bitpos
>> 3;
52 return (*(stream
->buffer
+ bytepos
) >> (7-bitpos
)) & 1;
55 * @brief Pushes bit onto the stream
59 void pushBit( BitstreamOut
* stream
, bool bit
)
61 int bytepos
= stream
->position
>> 3; // divide by 8
62 int bitpos
= stream
->position
& 7;
63 *(stream
->buffer
+bytepos
) |= (bit
& 1) << (7 - bitpos
);
69 * @brief Pushes the lower six bits onto the stream
70 * as b0 b1 b2 b3 b4 b5 b6
74 void push6bits( BitstreamOut
* stream
, uint8_t bits
)
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);
87 * @return number of bits left in stream
89 int bitsLeft( BitstreamIn
*stream
)
91 return stream
->numbits
- stream
->position
;
96 * @return Number of bits stored in stream
98 int numBits(BitstreamOut
*stream
)
100 return stream
->numbits
;
103 void x_num_to_bytes(uint64_t n
, size_t len
, uint8_t* dest
)
106 dest
[len
] = (uint8_t) n
;
111 uint64_t x_bytes_to_num(uint8_t* src
, size_t len
)
116 num
= (num
<< 8) | (*src
);
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;
127 void reverse_arraybytes(uint8_t* arr
, size_t len
)
130 for( i
=0; i
< len
; i
++)
132 arr
[i
] = reversebytes(arr
[i
]);
135 void reverse_arraycopy(uint8_t* arr
, uint8_t* dest
, size_t len
)
138 for( i
=0; i
< len
; i
++)
140 dest
[i
] = reversebytes(arr
[i
]);
144 void printarr(char * name
, uint8_t* arr
, int len
)
147 size_t outsize
= 40+strlen(name
)+len
*5;
148 char* output
= malloc(outsize
);
149 memset(output
, 0,outsize
);
152 cx
= snprintf(output
,outsize
, "uint8_t %s[] = {", name
);
153 for(i
=0 ; i
< len
; i
++)
155 cx
+= snprintf(output
+cx
,outsize
-cx
,"0x%02x,",*(arr
+i
));//5 bytes per byte
157 cx
+= snprintf(output
+cx
,outsize
-cx
,"};");
161 void printvar(char * name
, uint8_t* arr
, int len
)
164 size_t outsize
= 40+strlen(name
)+len
*2;
165 char* output
= malloc(outsize
);
166 memset(output
, 0,outsize
);
169 cx
= snprintf(output
,outsize
,"%s = ", name
);
170 for(i
=0 ; i
< len
; i
++)
172 cx
+= snprintf(output
+cx
,outsize
-cx
,"%02x",*(arr
+i
));//2 bytes per byte
178 void printarr_human_readable(char * title
, uint8_t* arr
, int len
)
181 size_t outsize
= 100+strlen(title
)+len
*4;
182 char* output
= malloc(outsize
);
183 memset(output
, 0,outsize
);
187 cx
= snprintf(output
,outsize
, "\n\t%s\n", title
);
188 for(i
=0 ; i
< len
; i
++)
191 cx
+= snprintf(output
+cx
,outsize
-cx
,"\n%02x| ", i
);
192 cx
+= snprintf(output
+cx
,outsize
-cx
, "%02x ",*(arr
+i
));
197 //-----------------------------
198 // Code for testing below
199 //-----------------------------
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}
209 while(bitsLeft(&in
) > 0)
211 pushBit(&out
, headBit(&in
));
212 //printf("Bits left: %d\n", bitsLeft(&in));
213 //printf("Bits out: %d\n", numBits(&out));
215 if(memcmp(input
, output
, sizeof(input
)) == 0)
217 prnlog(" Bitstream test 1 ok");
220 prnlog(" Bitstream test 1 failed");
222 for(i
= 0 ; i
< sizeof(input
) ; i
++)
224 prnlog(" IN %02x, OUT %02x", input
[i
], output
[i
]);
231 int testReversedBitstream()
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};
241 while(bitsLeft(&in
) > 0)
243 pushBit(&reversed_out
, tailBit(&in
));
245 while(bitsLeft(&reversed_in
) > 0)
247 pushBit(&out
, tailBit(&reversed_in
));
249 if(memcmp(input
, output
, sizeof(input
)) == 0)
251 prnlog(" Bitstream test 2 ok");
254 prnlog(" Bitstream test 2 failed");
256 for(i
= 0 ; i
< sizeof(input
) ; i
++)
258 prnlog(" IN %02x, MIDDLE: %02x, OUT %02x", input
[i
],reverse
[i
], output
[i
]);
266 int testCipherUtils(void)
268 prnlog("[+] Testing some internals...");
270 retval
|= testBitStream();
271 retval
|= testReversedBitstream();