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