]> git.zerfleddert.de Git - proxmark3-svn/blob - common/iso15693tools.c
Increased Mifare Esave & Eload filename array to 255 from 20
[proxmark3-svn] / common / iso15693tools.c
1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
4 // the license.
5 //-----------------------------------------------------------------------------
6 // ISO15693 CRC & other commons
7 //-----------------------------------------------------------------------------
8
9
10 #include "proxmark3.h"
11 #include <stdint.h>
12 #include <stdlib.h>
13 //#include "iso15693tools.h"
14
15 #define POLY 0x8408
16
17 // The CRC as described in ISO 15693-Part 3-Annex C
18 // v buffer with data
19 // n length
20 // returns crc as 16bit value
21 uint16_t Iso15693Crc(uint8_t *v, int n)
22 {
23 uint32_t reg;
24 int i, j;
25
26 reg = 0xffff;
27 for(i = 0; i < n; i++) {
28 reg = reg ^ ((uint32_t)v[i]);
29 for (j = 0; j < 8; j++) {
30 if (reg & 0x0001) {
31 reg = (reg >> 1) ^ 0x8408;
32 } else {
33 reg = (reg >> 1);
34 }
35 }
36 }
37
38 return ~(uint16_t)(reg & 0xffff);
39 }
40
41 // adds a CRC to a dataframe
42 // req[] iso15963 frame without crc
43 // n length without crc
44 // returns the new length of the dataframe.
45 int Iso15693AddCrc(uint8_t *req, int n) {
46 uint16_t crc=Iso15693Crc(req,n);
47 req[n] = crc & 0xff;
48 req[n+1] = crc >> 8;
49 return n+2;
50 }
51
52
53 int sprintf(char *str, const char *format, ...);
54
55 // returns a string representation of the UID
56 // UID is transmitted and stored LSB first, displayed MSB first
57 // target char* buffer, where to put the UID, if NULL a static buffer is returned
58 // uid[] the UID in transmission order
59 // return: ptr to string
60 char* Iso15693sprintUID(char *target,uint8_t *uid) {
61 static char tempbuf[2*8+1]="";
62 if (target==NULL) target=tempbuf;
63 sprintf(target,"%02X%02X%02X%02X%02X%02X%02X%02X",
64 uid[7],uid[6],uid[5],uid[4],uid[3],uid[2],uid[1],uid[0]);
65 return target;
66 }
67
68 //CRC-16 Routine used within iclass
69 unsigned short iclass_crc16(char *data_p, unsigned short length)
70 {
71 unsigned char i;
72 unsigned int data;
73 unsigned int crc = 0xffff;
74
75 if (length == 0)
76 return (~crc);
77
78 do
79 {
80 for (i=0, data=(unsigned int)0xff & *data_p++;
81 i < 8;
82 i++, data >>= 1)
83 {
84 if ((crc & 0x0001) ^ (data & 0x0001))
85 crc = (crc >> 1) ^ POLY;
86 else crc >>= 1;
87 }
88 } while (--length);
89
90 crc = ~crc;
91 data = crc;
92 crc = (crc << 8) | (data >> 8 & 0xff);
93 crc = crc ^ 0xBC3;
94 return (crc);
95 }
Impressum, Datenschutz