Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* ISO7816-3 Fi/Di tables + computation |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 2 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 3 | * (C) 2010-2015 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 4 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 5 | * 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 Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 9 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 10 | * 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 Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 14 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 15 | * 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 Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 18 | */ |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <errno.h> |
| 21 | |
Harald Welte | f64f688 | 2015-11-08 21:31:48 +0100 | [diff] [blame] | 22 | #include "utils.h" |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 23 | #include "iso7816_fidi.h" |
| 24 | |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 25 | /* Table 7 of ISO 7816-3:2006 */ |
Kévin Redon | 7406337 | 2018-07-03 15:57:03 +0200 | [diff] [blame] | 26 | const uint16_t fi_table[] = { |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 27 | 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 Redon | 7406337 | 2018-07-03 15:57:03 +0200 | [diff] [blame] | 32 | const uint8_t di_table[] = { |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 33 | 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 */ |
| 38 | int 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 | } |