blob: b35f0688d656d589762f4d9fe6bf8b790b46af2a [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
Harald Weltef64f6882015-11-08 21:31:48 +010023#include "utils.h"
Harald Welte30a53f82015-11-08 14:29:55 +010024#include "iso7816_fidi.h"
25
Harald Welte30a53f82015-11-08 14:29:55 +010026/* Table 7 of ISO 7816-3:2006 */
27static const uint16_t fi_table[] = {
28 372, 372, 558, 744, 1116, 1488, 1860, 0,
29 0, 512, 768, 1024, 1536, 2048, 0, 0
30};
31
32/* Table 8 from ISO 7816-3:2006 */
33static const uint8_t di_table[] = {
34 0, 1, 2, 4, 8, 16, 32, 64,
35 12, 20, 2, 4, 8, 16, 32, 64,
36};
37
38/* compute the F/D ratio based on Fi and Di values */
39int compute_fidi_ratio(uint8_t fi, uint8_t di)
40{
41 uint16_t f, d;
42 int ret;
43
44 if (fi >= ARRAY_SIZE(fi_table) ||
45 di >= ARRAY_SIZE(di_table))
46 return -EINVAL;
47
48 f = fi_table[fi];
49 if (f == 0)
50 return -EINVAL;
51
52 d = di_table[di];
53 if (d == 0)
54 return -EINVAL;
55
56 /* See table 7 of ISO 7816-3: From 1000 on we divide by 1/d,
57 * which equals a multiplication by d */
58 if (di < 8)
59 ret = f / d;
60 else
61 ret = f * d;
62
63 return ret;
64}