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