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