More work on iclass full simulation, and some work on iclass tag dump parsing
[legacy-proxmark3.git] / armsrc / cipherutils.c
blobc00e2be2db5b6cf3324bc1fcf01d250ec7024b05
1 /*****************************************************************************
2 * WARNING
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.
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 ****************************************************************************/
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include "cipherutils.h"
43 #ifndef ON_DEVICE
44 #include "fileutils.h"
45 #endif
46 /**
48 * @brief Return and remove the first bit (x0) in the stream : <x0 x1 x2 x3 ... xn >
49 * @param stream
50 * @return
52 bool headBit( BitstreamIn *stream)
54 int bytepos = stream->position >> 3; // divide by 8
55 int bitpos = (stream->position++) & 7; // mask out 00000111
56 return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
58 /**
59 * @brief Return and remove the last bit (xn) in the stream: <x0 x1 x2 ... xn>
60 * @param stream
61 * @return
63 bool tailBit( BitstreamIn *stream)
65 int bitpos = stream->numbits -1 - (stream->position++);
67 int bytepos= bitpos >> 3;
68 bitpos &= 7;
69 return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
71 /**
72 * @brief Pushes bit onto the stream
73 * @param stream
74 * @param bit
76 void pushBit( BitstreamOut* stream, bool bit)
78 int bytepos = stream->position >> 3; // divide by 8
79 int bitpos = stream->position & 7;
80 *(stream->buffer+bytepos) |= (bit & 1) << (7 - bitpos);
81 stream->position++;
82 stream->numbits++;
85 /**
86 * @brief Pushes the lower six bits onto the stream
87 * as b0 b1 b2 b3 b4 b5 b6
88 * @param stream
89 * @param bits
91 void push6bits( BitstreamOut* stream, uint8_t bits)
93 pushBit(stream, bits & 0x20);
94 pushBit(stream, bits & 0x10);
95 pushBit(stream, bits & 0x08);
96 pushBit(stream, bits & 0x04);
97 pushBit(stream, bits & 0x02);
98 pushBit(stream, bits & 0x01);
102 * @brief bitsLeft
103 * @param stream
104 * @return number of bits left in stream
106 int bitsLeft( BitstreamIn *stream)
108 return stream->numbits - stream->position;
111 * @brief numBits
112 * @param stream
113 * @return Number of bits stored in stream
115 int numBits(BitstreamOut *stream)
117 return stream->numbits;
120 void x_num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
122 while (len--) {
123 dest[len] = (uint8_t) n;
124 n >>= 8;
128 uint64_t x_bytes_to_num(uint8_t* src, size_t len)
130 uint64_t num = 0;
131 while (len--)
133 num = (num << 8) | (*src);
134 src++;
136 return num;
138 uint8_t reversebytes(uint8_t b) {
139 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
140 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
141 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
142 return b;
144 void reverse_arraybytes(uint8_t* arr, size_t len)
146 uint8_t i;
147 for( i =0; i< len ; i++)
149 arr[i] = reversebytes(arr[i]);
152 void reverse_arraycopy(uint8_t* arr, uint8_t* dest, size_t len)
154 uint8_t i;
155 for( i =0; i< len ; i++)
157 dest[i] = reversebytes(arr[i]);
160 #ifndef ON_DEVICE
161 void printarr(char * name, uint8_t* arr, int len)
163 int cx;
164 size_t outsize = 40+strlen(name)+len*5;
165 char* output = malloc(outsize);
166 memset(output, 0,outsize);
168 int i ;
169 cx = snprintf(output,outsize, "uint8_t %s[] = {", name);
170 for(i =0 ; i< len ; i++)
172 cx += snprintf(output+cx,outsize-cx,"0x%02x,",*(arr+i));//5 bytes per byte
174 cx += snprintf(output+cx,outsize-cx,"};");
175 prnlog(output);
178 void printvar(char * name, uint8_t* arr, int len)
180 int cx;
181 size_t outsize = 40+strlen(name)+len*2;
182 char* output = malloc(outsize);
183 memset(output, 0,outsize);
185 int i ;
186 cx = snprintf(output,outsize,"%s = ", name);
187 for(i =0 ; i< len ; i++)
189 cx += snprintf(output+cx,outsize-cx,"%02x",*(arr+i));//2 bytes per byte
192 prnlog(output);
195 void printarr_human_readable(char * title, uint8_t* arr, int len)
197 int cx;
198 size_t outsize = 100+strlen(title)+len*4;
199 char* output = malloc(outsize);
200 memset(output, 0,outsize);
203 int i;
204 cx = snprintf(output,outsize, "\n\t%s\n", title);
205 for(i =0 ; i< len ; i++)
207 if(i % 16 == 0)
208 cx += snprintf(output+cx,outsize-cx,"\n%02x| ", i );
209 cx += snprintf(output+cx,outsize-cx, "%02x ",*(arr+i));
211 prnlog(output);
212 free(output);
214 #endif
215 //-----------------------------
216 // Code for testing below
217 //-----------------------------
219 #ifndef ON_DEVICE
220 int testBitStream()
222 uint8_t input [] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
223 uint8_t output [] = {0,0,0,0,0,0,0,0};
224 BitstreamIn in = { input, sizeof(input) * 8,0};
225 BitstreamOut out ={ output, 0,0}
227 while(bitsLeft(&in) > 0)
229 pushBit(&out, headBit(&in));
230 //printf("Bits left: %d\n", bitsLeft(&in));
231 //printf("Bits out: %d\n", numBits(&out));
233 if(memcmp(input, output, sizeof(input)) == 0)
235 prnlog(" Bitstream test 1 ok");
236 }else
238 prnlog(" Bitstream test 1 failed");
239 uint8_t i;
240 for(i = 0 ; i < sizeof(input) ; i++)
242 prnlog(" IN %02x, OUT %02x", input[i], output[i]);
244 return 1;
246 return 0;
249 int testReversedBitstream()
251 uint8_t input [] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
252 uint8_t reverse [] = {0,0,0,0,0,0,0,0};
253 uint8_t output [] = {0,0,0,0,0,0,0,0};
254 BitstreamIn in = { input, sizeof(input) * 8,0};
255 BitstreamOut out ={ output, 0,0};
256 BitstreamIn reversed_in ={ reverse, sizeof(input)*8,0};
257 BitstreamOut reversed_out ={ reverse,0 ,0};
259 while(bitsLeft(&in) > 0)
261 pushBit(&reversed_out, tailBit(&in));
263 while(bitsLeft(&reversed_in) > 0)
265 pushBit(&out, tailBit(&reversed_in));
267 if(memcmp(input, output, sizeof(input)) == 0)
269 prnlog(" Bitstream test 2 ok");
270 }else
272 prnlog(" Bitstream test 2 failed");
273 uint8_t i;
274 for(i = 0 ; i < sizeof(input) ; i++)
276 prnlog(" IN %02x, MIDDLE: %02x, OUT %02x", input[i],reverse[i], output[i]);
278 return 1;
280 return 0;
284 int testCipherUtils(void)
286 prnlog("[+] Testing some internals...");
287 int retval = 0;
288 retval |= testBitStream();
289 retval |= testReversedBitstream();
290 return retval;
292 #endif