blob: 052654c14cae6961e7fecaf6a0423d726e1beec9 [file] [log] [blame]
Harald Welte30a53f82015-11-08 14:29:55 +01001/* ISO7816-3 Fi/Di tables + computation */
2/* (C) 2010-2015 by Harald Welte <hwelte@hmw-consulting.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20#include <stdint.h>
21#include <errno.h>
22
23#include "iso7816_fidi.h"
24
25#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
26
27/* Table 7 of ISO 7816-3:2006 */
28static const uint16_t fi_table[] = {
29 372, 372, 558, 744, 1116, 1488, 1860, 0,
30 0, 512, 768, 1024, 1536, 2048, 0, 0
31};
32
33/* Table 8 from ISO 7816-3:2006 */
34static const uint8_t di_table[] = {
35 0, 1, 2, 4, 8, 16, 32, 64,
36 12, 20, 2, 4, 8, 16, 32, 64,
37};
38
39/* compute the F/D ratio based on Fi and Di values */
40int compute_fidi_ratio(uint8_t fi, uint8_t di)
41{
42 uint16_t f, d;
43 int ret;
44
45 if (fi >= ARRAY_SIZE(fi_table) ||
46 di >= ARRAY_SIZE(di_table))
47 return -EINVAL;
48
49 f = fi_table[fi];
50 if (f == 0)
51 return -EINVAL;
52
53 d = di_table[di];
54 if (d == 0)
55 return -EINVAL;
56
57 /* See table 7 of ISO 7816-3: From 1000 on we divide by 1/d,
58 * which equals a multiplication by d */
59 if (di < 8)
60 ret = f / d;
61 else
62 ret = f * d;
63
64 return ret;
65}