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