iso14443-subr.c 2.77 KB
Newer Older
1 2
/*-
 * Public platform independent Near Field Communication (NFC) library
3
 *
4
 * Copyright (C) 2009 Roel Verdult
5
 *
6 7 8 9
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
10
 *
11 12 13 14 15 16 17 18 19
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

20 21 22 23
/**
* @file iso14443-subr.c
* @brief Defines some function extracted for ISO/IEC 14443
*/
24 25

#ifdef HAVE_CONFIG_H
26
#  include "config.h"
27 28 29
#endif // HAVE_CONFIG_H

#include <stdio.h>
30
#include <string.h>
31 32

#include <nfc/nfc.h>
33
#include "nfc-internal.h"
34

35 36 37 38 39

/**
 * @brief CRC
 *
 */
40
void
41
iso14443a_crc(uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc)
42
{
43
  uint8_t  bt;
44 45 46 47
  uint32_t wCrc = 0x6363;

  do {
    bt = *pbtData++;
48 49 50
    bt = (bt ^(uint8_t)(wCrc & 0x00FF));
    bt = (bt ^(bt << 4));
    wCrc = (wCrc >> 8) ^((uint32_t) bt << 8) ^((uint32_t) bt << 3) ^((uint32_t) bt >> 4);
51 52
  } while (--szLen);

53 54
  *pbtCrc++ = (uint8_t)(wCrc & 0xFF);
  *pbtCrc = (uint8_t)((wCrc >> 8) & 0xFF);
55 56
}

57 58 59 60
/**
 * @brief Append CRC
 *
 */
61
void
62
iso14443a_crc_append(uint8_t *pbtData, size_t szLen)
63
{
64
  iso14443a_crc(pbtData, szLen, pbtData + szLen);
65
}
66

67 68 69 70
/**
 * @brief Locate historical bytes
 * @see ISO/IEC 14443-4 (5.2.7 Historical bytes)
 */
71
uint8_t *
72
iso14443a_locate_historical_bytes(uint8_t *pbtAts, size_t szAts, size_t *pszTk)
73 74 75 76 77 78 79 80 81 82 83 84 85
{
  if (szAts) {
    size_t offset = 1;
    if (pbtAts[0] & 0x10) { // TA
      offset++;
    }
    if (pbtAts[0] & 0x20) { // TB
      offset++;
    }
    if (pbtAts[0] & 0x40) { // TC
      offset++;
    }
    if (szAts > offset) {
86 87
      *pszTk = (szAts - offset);
      return (pbtAts + offset);
88 89
    }
  }
90
  *pszTk = 0;
91 92
  return NULL;
}
93 94 95 96 97

/**
 * @brief Add cascade tags (0x88) in UID
 * @see ISO/IEC 14443-3 (6.4.4 UID contents and cascade levels)
 */
98
void
99
iso14443_cascade_uid(const uint8_t abtUID[], const size_t szUID, uint8_t *pbtCascadedUID, size_t *pszCascadedUID)
100
{
101 102 103
  switch (szUID) {
    case 7:
      pbtCascadedUID[0] = 0x88;
104
      memcpy(pbtCascadedUID + 1, abtUID, 7);
105 106 107 108 109
      *pszCascadedUID = 8;
      break;

    case 10:
      pbtCascadedUID[0] = 0x88;
110
      memcpy(pbtCascadedUID + 1, abtUID, 3);
111
      pbtCascadedUID[4] = 0x88;
112
      memcpy(pbtCascadedUID + 5, abtUID + 3, 7);
113 114 115 116 117
      *pszCascadedUID = 12;
      break;

    case 4:
    default:
118
      memcpy(pbtCascadedUID, abtUID, szUID);
119 120 121
      *pszCascadedUID = szUID;
      break;
  }
122
}