1 /*****************************************************************************
4 * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
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.
10 * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
12 *****************************************************************************
14 * This file is part of loclass. It is a reconstructon of the cipher engine
15 * used in iClass, and RFID techology.
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".
21 * Copyright (C) 2014 Martin Holst Swende
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.
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.
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/>.
37 ****************************************************************************/
42 #include "fileutils.h"
43 #include "cipherutils.h"
46 * @brief Return and remove the first bit (x0) in the stream : <x0 x1 x2 x3 ... xn >
50 bool headBit( BitstreamIn
*stream
)
52 int bytepos
= stream
->position
>> 3; // divide by 8
53 int bitpos
= (stream
->position
++) & 7; // mask out 00000111
54 return (*(stream
->buffer
+ bytepos
) >> (7-bitpos
)) & 1;
57 * @brief Return and remove the last bit (xn) in the stream: <x0 x1 x2 ... xn>
61 bool tailBit( BitstreamIn
*stream
)
63 int bitpos
= stream
->numbits
-1 - (stream
->position
++);
65 int bytepos
= bitpos
>> 3;
67 return (*(stream
->buffer
+ bytepos
) >> (7-bitpos
)) & 1;
70 * @brief Pushes bit onto the stream
74 void pushBit( BitstreamOut
* stream
, bool bit
)
76 int bytepos
= stream
->position
>> 3; // divide by 8
77 int bitpos
= stream
->position
& 7;
78 *(stream
->buffer
+bytepos
) |= (bit
& 1) << (7 - bitpos
);
84 * @brief Pushes the lower six bits onto the stream
85 * as b0 b1 b2 b3 b4 b5 b6
89 void push6bits( BitstreamOut
* stream
, uint8_t bits
)
91 pushBit(stream
, bits
& 0x20);
92 pushBit(stream
, bits
& 0x10);
93 pushBit(stream
, bits
& 0x08);
94 pushBit(stream
, bits
& 0x04);
95 pushBit(stream
, bits
& 0x02);
96 pushBit(stream
, bits
& 0x01);
102 * @return number of bits left in stream
104 int bitsLeft( BitstreamIn
*stream
)
106 return stream
->numbits
- stream
->position
;
111 * @return Number of bits stored in stream
113 int numBits(BitstreamOut
*stream
)
115 return stream
->numbits
;
118 void x_num_to_bytes(uint64_t n
, size_t len
, uint8_t* dest
)
121 dest
[len
] = (uint8_t) n
;
126 uint64_t x_bytes_to_num(uint8_t* src
, size_t len
)
131 num
= (num
<< 8) | (*src
);
136 uint8_t reversebytes(uint8_t b
) {
137 b
= (b
& 0xF0) >> 4 | (b
& 0x0F) << 4;
138 b
= (b
& 0xCC) >> 2 | (b
& 0x33) << 2;
139 b
= (b
& 0xAA) >> 1 | (b
& 0x55) << 1;
142 void reverse_arraybytes(uint8_t* arr
, size_t len
)
145 for( i
=0; i
< len
; i
++)
147 arr
[i
] = reversebytes(arr
[i
]);
150 void reverse_arraycopy(uint8_t* arr
, uint8_t* dest
, size_t len
)
153 for( i
=0; i
< len
; i
++)
155 dest
[i
] = reversebytes(arr
[i
]);
159 void printarr(char * name
, uint8_t* arr
, int len
)
162 size_t outsize
= 40+strlen(name
)+len
*5;
163 char* output
= malloc(outsize
);
164 memset(output
, 0,outsize
);
167 cx
= snprintf(output
,outsize
, "uint8_t %s[] = {", name
);
168 for(i
=0 ; i
< len
; i
++)
170 cx
+= snprintf(output
+cx
,outsize
-cx
,"0x%02x,",*(arr
+i
));//5 bytes per byte
172 cx
+= snprintf(output
+cx
,outsize
-cx
,"};");
176 void printvar(char * name
, uint8_t* arr
, int len
)
179 size_t outsize
= 40+strlen(name
)+len
*2;
180 char* output
= malloc(outsize
);
181 memset(output
, 0,outsize
);
184 cx
= snprintf(output
,outsize
,"%s = ", name
);
185 for(i
=0 ; i
< len
; i
++)
187 cx
+= snprintf(output
+cx
,outsize
-cx
,"%02x",*(arr
+i
));//2 bytes per byte
193 void printarr_human_readable(char * title
, uint8_t* arr
, int len
)
196 size_t outsize
= 100+strlen(title
)+len
*4;
197 char* output
= malloc(outsize
);
198 memset(output
, 0,outsize
);
202 cx
= snprintf(output
,outsize
, "\n\t%s\n", title
);
203 for(i
=0 ; i
< len
; i
++)
206 cx
+= snprintf(output
+cx
,outsize
-cx
,"\n%02x| ", i
);
207 cx
+= snprintf(output
+cx
,outsize
-cx
, "%02x ",*(arr
+i
));
212 //-----------------------------
213 // Code for testing below
214 //-----------------------------
219 uint8_t input
[] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
220 uint8_t output
[] = {0,0,0,0,0,0,0,0};
221 BitstreamIn in
= { input
, sizeof(input
) * 8,0};
222 BitstreamOut out
={ output
, 0,0}
224 while(bitsLeft(&in
) > 0)
226 pushBit(&out
, headBit(&in
));
227 //printf("Bits left: %d\n", bitsLeft(&in));
228 //printf("Bits out: %d\n", numBits(&out));
230 if(memcmp(input
, output
, sizeof(input
)) == 0)
232 prnlog(" Bitstream test 1 ok");
235 prnlog(" Bitstream test 1 failed");
237 for(i
= 0 ; i
< sizeof(input
) ; i
++)
239 prnlog(" IN %02x, OUT %02x", input
[i
], output
[i
]);
246 int testReversedBitstream()
248 uint8_t input
[] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
249 uint8_t reverse
[] = {0,0,0,0,0,0,0,0};
250 uint8_t output
[] = {0,0,0,0,0,0,0,0};
251 BitstreamIn in
= { input
, sizeof(input
) * 8,0};
252 BitstreamOut out
={ output
, 0,0};
253 BitstreamIn reversed_in
={ reverse
, sizeof(input
)*8,0};
254 BitstreamOut reversed_out
={ reverse
,0 ,0};
256 while(bitsLeft(&in
) > 0)
258 pushBit(&reversed_out
, tailBit(&in
));
260 while(bitsLeft(&reversed_in
) > 0)
262 pushBit(&out
, tailBit(&reversed_in
));
264 if(memcmp(input
, output
, sizeof(input
)) == 0)
266 prnlog(" Bitstream test 2 ok");
269 prnlog(" Bitstream test 2 failed");
271 for(i
= 0 ; i
< sizeof(input
) ; i
++)
273 prnlog(" IN %02x, MIDDLE: %02x, OUT %02x", input
[i
],reverse
[i
], output
[i
]);
281 int testCipherUtils(void)
283 prnlog("[+] Testing some internals...");
285 retval
|= testBitStream();
286 retval
|= testReversedBitstream();