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
));
198 //-----------------------------
199 // Code for testing below
200 //-----------------------------
205 uint8_t input
[] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
206 uint8_t output
[] = {0,0,0,0,0,0,0,0};
207 BitstreamIn in
= { input
, sizeof(input
) * 8,0};
208 BitstreamOut out
={ output
, 0,0}
210 while(bitsLeft(&in
) > 0)
212 pushBit(&out
, headBit(&in
));
213 //printf("Bits left: %d\n", bitsLeft(&in));
214 //printf("Bits out: %d\n", numBits(&out));
216 if(memcmp(input
, output
, sizeof(input
)) == 0)
218 prnlog(" Bitstream test 1 ok");
221 prnlog(" Bitstream test 1 failed");
223 for(i
= 0 ; i
< sizeof(input
) ; i
++)
225 prnlog(" IN %02x, OUT %02x", input
[i
], output
[i
]);
232 int testReversedBitstream()
234 uint8_t input
[] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
235 uint8_t reverse
[] = {0,0,0,0,0,0,0,0};
236 uint8_t output
[] = {0,0,0,0,0,0,0,0};
237 BitstreamIn in
= { input
, sizeof(input
) * 8,0};
238 BitstreamOut out
={ output
, 0,0};
239 BitstreamIn reversed_in
={ reverse
, sizeof(input
)*8,0};
240 BitstreamOut reversed_out
={ reverse
,0 ,0};
242 while(bitsLeft(&in
) > 0)
244 pushBit(&reversed_out
, tailBit(&in
));
246 while(bitsLeft(&reversed_in
) > 0)
248 pushBit(&out
, tailBit(&reversed_in
));
250 if(memcmp(input
, output
, sizeof(input
)) == 0)
252 prnlog(" Bitstream test 2 ok");
255 prnlog(" Bitstream test 2 failed");
257 for(i
= 0 ; i
< sizeof(input
) ; i
++)
259 prnlog(" IN %02x, MIDDLE: %02x, OUT %02x", input
[i
],reverse
[i
], output
[i
]);
267 int testCipherUtils(void)
269 prnlog("[+] Testing some internals...");
271 retval
|= testBitStream();
272 retval
|= testReversedBitstream();