| 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 | // The CRC as described in ISO 15693-Part 3-Annex C |
| 16 | // v buffer with data |
| 17 | // n length |
| 18 | // returns crc as 16bit value |
| 19 | uint16_t Iso15693Crc(uint8_t *v, int n) |
| 20 | { |
| 21 | uint32_t reg; |
| 22 | int i, j; |
| 23 | |
| 24 | reg = 0xffff; |
| 25 | for(i = 0; i < n; i++) { |
| 26 | reg = reg ^ ((uint32_t)v[i]); |
| 27 | for (j = 0; j < 8; j++) { |
| 28 | if (reg & 0x0001) { |
| 29 | reg = (reg >> 1) ^ 0x8408; |
| 30 | } else { |
| 31 | reg = (reg >> 1); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return ~(uint16_t)(reg & 0xffff); |
| 37 | } |
| 38 | |
| 39 | // adds a CRC to a dataframe |
| 40 | // req[] iso15963 frame without crc |
| 41 | // n length without crc |
| 42 | // returns the new length of the dataframe. |
| 43 | int Iso15693AddCrc(uint8_t *req, int n) { |
| 44 | uint16_t crc=Iso15693Crc(req,n); |
| 45 | req[n] = crc & 0xff; |
| 46 | req[n+1] = crc >> 8; |
| 47 | return n+2; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | int sprintf(char *str, const char *format, ...); |
| 52 | |
| 53 | // returns a string representation of the UID |
| 54 | // UID is transmitted and stored LSB first, displayed MSB first |
| 55 | // target char* buffer, where to put the UID, if NULL a static buffer is returned |
| 56 | // uid[] the UID in transmission order |
| 57 | // return: ptr to string |
| 58 | char* Iso15693sprintUID(char *target,uint8_t *uid) { |
| 59 | static char tempbuf[2*8+1]=""; |
| 60 | if (target==NULL) target=tempbuf; |
| 61 | sprintf(target,"%02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX", |
| 62 | uid[7],uid[6],uid[5],uid[4],uid[3],uid[2],uid[1],uid[0]); |
| 63 | return target; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |