Kévin Redon | 632502d | 2019-05-02 15:55:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | */ |
| 18 | #include <stdint.h> |
| 19 | #include <stddef.h> |
| 20 | |
| 21 | #include "utils.h" |
| 22 | #include "iso7816_3.h" |
| 23 | |
| 24 | const uint16_t iso7816_3_fi_table[16] = { |
| 25 | 372, 372, 558, 744, 1116, 1488, 1860, 0, |
| 26 | 0, 512, 768, 1024, 1536, 2048, 0, 0 |
| 27 | }; |
| 28 | |
| 29 | const uint32_t iso7816_3_fmax_table[16] = { |
| 30 | 4000000, 5000000, 6000000, 8000000, 12000000, 16000000, 20000000, 0, |
| 31 | 0, 5000000, 7500000, 10000000, 15000000, 20000000, 0, 0 |
| 32 | }; |
| 33 | |
| 34 | const uint8_t iso7816_3_di_table[16] = { |
| 35 | 0, 1, 2, 4, 8, 16, 32, 64, |
| 36 | 12, 20, 0, 0, 0, 0, 0, 0, |
| 37 | }; |
| 38 | |
| 39 | /* all values are based on the Elementary Time Unit (ETU), defined in ISO/IEC 7816-3 section 7.1 |
| 40 | * this is the time required to transmit a bit, and is calculated as follows: 1 ETU = (F / D) x (1 / f) where: |
| 41 | * - F is the clock rate conversion integer |
| 42 | * - D is the baud rate adjustment factor |
| 43 | * - f is the clock frequency |
| 44 | * the possible F, f(max), and D values are defined in ISO/IEC 7816-3 table 7 and 8 |
| 45 | * - the initial value for F (after reset) is Fd = 372 |
| 46 | * - the initial value for D (after reset) is Dd = 1 |
| 47 | * - the initial maximum frequency f(max) is 5 MHz |
| 48 | * the card must measure the ETU based on the clock signal provided by the reader |
| 49 | * one ETU (e.g. 1 bit) takes F/D clock cycles, which the card must count |
| 50 | * |
| 51 | * the card can indicate an alternative set of supported values Fi (with corresponding f(max)) and Di for higher baud rate in TA1 in the ATR (see ISO/IEC 7816-3 section 8.3) |
| 52 | * these values are selected according to ISO/IEC 7816-3 section 6.3.1: |
| 53 | * - card in specific mode: they are enforced if TA2 is present (the reader can deactivate the card if it does not support these values) |
| 54 | * - card in negotiable mode: |
| 55 | * -- they can be selected by the reader using the Protocol and Parameters Selection (PPS) procedure |
| 56 | * -- the first offered protocol and default values are used when no PPS is started |
| 57 | * |
| 58 | * PPS is done with Fd and Dd (see ISO/IEC 7816-3 section 9) |
| 59 | * the reader can propose any F and D values between from Fd to Fi, and from Dd to Di (Fi and Di are indicated in TA1) |
| 60 | * the in PPS agreed values F and D are called Fn and Dn and are applied after a successful exchange, corresponding to PPS1_Response bit 5 |
| 61 | * |
| 62 | * the F and D values must be provided to the SAM3S USART peripheral (after reset and PPS) |
| 63 | */ |
| 64 | |
| 65 | bool iso7816_3_valid_f(uint16_t f) |
| 66 | { |
| 67 | if (0 == f) { |
| 68 | return false; |
| 69 | } |
| 70 | uint8_t i = 0; |
| 71 | for (i = 0; i < ARRAY_SIZE(iso7816_3_fi_table) && iso7816_3_fi_table[i] != f; i++); |
| 72 | return (i < ARRAY_SIZE(iso7816_3_fi_table) && iso7816_3_fi_table[i] == f); |
| 73 | } |
| 74 | |
| 75 | bool iso7816_3_valid_d(uint8_t d) |
| 76 | { |
| 77 | if (0 == d) { |
| 78 | return false; |
| 79 | } |
| 80 | uint8_t i = 0; |
| 81 | for (i = 0; i < ARRAY_SIZE(iso7816_3_di_table) && iso7816_3_di_table[i] != d; i++); |
| 82 | return (i < ARRAY_SIZE(iso7816_3_di_table) && iso7816_3_di_table[i] == d); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * the ETU is not only used to define the baud rate, but also the Waiting Time (WT) (see ISO/IEC 7816-3 section 8.1) |
| 87 | * when exceeding WT without card response, the reader flags the card as unresponsive, and resets it |
| 88 | * this can be used by the card to indicate errors or unsupported operations |
| 89 | * if the card requires more time to respond, it shall send a procedure byte to restart WT |
| 90 | * WT is calculated as follows (for T=0, see ISO/IEC 7816-3 section 10.2): WT = WI x 960 x (Fi / f(max)) where |
| 91 | * - WI is encoded in TC2 in the ATR (10 if absent) |
| 92 | * - WI does not depend on D/Di (used for the ETU) |
| 93 | * - after reset WT is 9600 ETU |
| 94 | * - WI (e.g. the new WT) is applied when T=0 is used (after 6.3.1), even if Fi is not Fn (this WT extension is important to know for the reader so to have the right timeout) |
| 95 | */ |
| 96 | |
| 97 | int32_t iso7816_3_calculate_wt(uint8_t wi, uint16_t fi, uint8_t di, uint16_t f, uint8_t d) |
| 98 | { |
| 99 | // sanity checks |
| 100 | if (0 == wi) { |
| 101 | return -1; |
| 102 | } |
| 103 | if (!iso7816_3_valid_f(fi)) { |
| 104 | return -2; |
| 105 | } |
| 106 | if (!iso7816_3_valid_d(di)) { |
| 107 | return -3; |
| 108 | } |
| 109 | if (!iso7816_3_valid_f(f)) { |
| 110 | return -4; |
| 111 | } |
| 112 | if (!iso7816_3_valid_d(d)) { |
| 113 | return -5; |
| 114 | } |
| 115 | if (f > fi) { |
| 116 | return -6; |
| 117 | } |
| 118 | if (d > di) { |
| 119 | return -7; |
| 120 | } |
| 121 | |
| 122 | return wi * 960UL * (fi/f) * (di/d); // calculate timeout value in ETU |
| 123 | } |