]> cvs.zerfleddert.de Git - proxmark3-svn/blob - common/iso15693tools.c
CHG: moved into header files.
[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 #include "iso15693tools.h"
10
11 // The CRC as described in ISO 15693-Part 3-Annex C
12 // v buffer with data
13 // n length
14 // returns crc as 16bit value
15 uint16_t Iso15693Crc(uint8_t *v, int n)
16 {
17 uint32_t reg;
18 int i, j;
19
20 reg = 0xffff;
21 for(i = 0; i < n; i++) {
22 reg = reg ^ ((uint32_t)v[i]);
23 for (j = 0; j < 8; j++) {
24 if (reg & 0x0001) {
25 reg = (reg >> 1) ^ 0x8408;
26 } else {
27 reg = (reg >> 1);
28 }
29 }
30 }
31
32 return ~(uint16_t)(reg & 0xffff);
33 }
34
35 // adds a CRC to a dataframe
36 // req[] iso15963 frame without crc
37 // n length without crc
38 // returns the new length of the dataframe.
39 int Iso15693AddCrc(uint8_t *req, int n) {
40 uint16_t crc=Iso15693Crc(req,n);
41 req[n] = crc & 0xff;
42 req[n+1] = crc >> 8;
43 return n+2;
44 }
45
46
47 int sprintf(char *str, const char *format, ...);
48
49 // returns a string representation of the UID
50 // UID is transmitted and stored LSB first, displayed MSB first
51 // target char* buffer, where to put the UID, if NULL a static buffer is returned
52 // uid[] the UID in transmission order
53 // return: ptr to string
54 char* Iso15693sprintUID(char *target, uint8_t *uid) {
55 static char tempbuf[2*8+1] = {0};
56 if (target==NULL) target=tempbuf;
57 sprintf(target,"%02X%02X%02X%02X%02X%02X%02X%02X",
58 uid[7],uid[6],uid[5],uid[4],uid[3],uid[2],uid[1],uid[0]);
59 return target;
60 }
61
62 uint16_t iclass_crc16(char *data_p, unsigned short length)
63 {
64 unsigned char i;
65 unsigned int data;
66 uint16_t crc = 0xffff;
67
68 if (length == 0)
69 return (~crc);
70
71 do
72 {
73 for (i=0, data=(unsigned int)0xff & *data_p++;
74 i < 8;
75 i++, data >>= 1)
76 {
77 if ((crc & 0x0001) ^ (data & 0x0001))
78 crc = (crc >> 1) ^ POLY;
79 else crc >>= 1;
80 }
81 } while (--length);
82
83 crc = ~crc;
84 data = crc;
85 crc = (crc << 8) | (data >> 8 & 0xff);
86 crc = crc ^ 0xBC3;
87 return (crc);
88 }
89
Impressum, Datenschutz