blob: 1c70467f701ca54e23ee0f3a3e0f184216efd657 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* ISO7816-3 Fi/Di tables + computation
Harald Welte30a53f82015-11-08 14:29:55 +01002 *
Kévin Redon9a12d682018-07-08 13:21:16 +02003 * (C) 2010-2015 by Harald Welte <laforge@gnumonks.org>
Harald Welte30a53f82015-11-08 14:29:55 +01004 *
Kévin Redon9a12d682018-07-08 13:21:16 +02005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
Harald Welte30a53f82015-11-08 14:29:55 +01009 *
Kévin Redon9a12d682018-07-08 13:21:16 +020010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Harald Welte30a53f82015-11-08 14:29:55 +010014 *
Kévin Redon9a12d682018-07-08 13:21:16 +020015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
Harald Welte30a53f82015-11-08 14:29:55 +010018 */
Harald Welte30a53f82015-11-08 14:29:55 +010019#include <stdint.h>
20#include <errno.h>
21
Harald Weltef64f6882015-11-08 21:31:48 +010022#include "utils.h"
Harald Welte30a53f82015-11-08 14:29:55 +010023#include "iso7816_fidi.h"
24
Harald Welte30a53f82015-11-08 14:29:55 +010025/* Table 7 of ISO 7816-3:2006 */
Kévin Redon74063372018-07-03 15:57:03 +020026const uint16_t fi_table[] = {
Harald Welte30a53f82015-11-08 14:29:55 +010027 372, 372, 558, 744, 1116, 1488, 1860, 0,
28 0, 512, 768, 1024, 1536, 2048, 0, 0
29};
30
31/* Table 8 from ISO 7816-3:2006 */
Kévin Redon74063372018-07-03 15:57:03 +020032const uint8_t di_table[] = {
Harald Welte30a53f82015-11-08 14:29:55 +010033 0, 1, 2, 4, 8, 16, 32, 64,
34 12, 20, 2, 4, 8, 16, 32, 64,
35};
36
37/* compute the F/D ratio based on Fi and Di values */
38int compute_fidi_ratio(uint8_t fi, uint8_t di)
39{
40 uint16_t f, d;
41 int ret;
42
43 if (fi >= ARRAY_SIZE(fi_table) ||
44 di >= ARRAY_SIZE(di_table))
45 return -EINVAL;
46
47 f = fi_table[fi];
48 if (f == 0)
49 return -EINVAL;
50
51 d = di_table[di];
52 if (d == 0)
53 return -EINVAL;
54
55 /* See table 7 of ISO 7816-3: From 1000 on we divide by 1/d,
56 * which equals a multiplication by d */
57 if (di < 8)
58 ret = f / d;
59 else
60 ret = f * d;
61
62 return ret;
63}