Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 1 | /* 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 Welte | f64f688 | 2015-11-08 21:31:48 +0100 | [diff] [blame^] | 23 | #include "utils.h" |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 24 | #include "iso7816_fidi.h" |
| 25 | |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 26 | /* Table 7 of ISO 7816-3:2006 */ |
| 27 | static 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 */ |
| 33 | static 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 */ |
| 39 | int 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 | } |