From 30a53f823a8c0a1764fc168a8ceeae256c93f626 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 8 Nov 2015 14:29:55 +0100 Subject: [PATCH] Move Fi/Di calculation functions to separate C file --- firmware/Makefile | 2 +- firmware/src_simtrace/iso7816_fidi.c | 65 ++++++++++++++++++++++++ firmware/src_simtrace/iso7816_fidi.h | 6 +++ firmware/src_simtrace/simtrace_iso7816.c | 41 +-------------- 4 files changed, 73 insertions(+), 41 deletions(-) create mode 100644 firmware/src_simtrace/iso7816_fidi.c create mode 100644 firmware/src_simtrace/iso7816_fidi.h diff --git a/firmware/Makefile b/firmware/Makefile index 267084bd..785837f1 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -138,7 +138,7 @@ C_CMSIS = core_cm3.o C_LOWLEVEL = board_cstartup_gnu.o board_lowlevel.o syscalls.o exceptions.o C_LIBLEVEL = spi.o pio.o pmc.o usart.o pio_it.o pio_capture.o uart_console.o iso7816_4.o wdt.o led.o tc.o C_CCID = cciddriver.o USBD.o USBDDriver.o USBD_HAL.o USBRequests.o USBDCallbacks.o USBDescriptors.o USBDDriverCallbacks.o -C_SIMTRACE = simtrace_iso7816.o usb.o ccid.o sniffer.o phone.o mitm.o ringbuffer.o host_communication.o #iso7816_uart.o +C_SIMTRACE = simtrace_iso7816.o usb.o ccid.o sniffer.o phone.o mitm.o ringbuffer.o host_communication.o iso7816_fidi.o #iso7816_uart.o C_APPLEVEL = main.o C_OBJECTS = $(C_CMSIS) $(C_LOWLEVEL) $(C_LIBLEVEL) $(C_APPLEVEL) $(C_CCID) $(C_SIMTRACE) diff --git a/firmware/src_simtrace/iso7816_fidi.c b/firmware/src_simtrace/iso7816_fidi.c new file mode 100644 index 00000000..052654c1 --- /dev/null +++ b/firmware/src_simtrace/iso7816_fidi.c @@ -0,0 +1,65 @@ +/* ISO7816-3 Fi/Di tables + computation */ +/* (C) 2010-2015 by Harald Welte + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include + +#include "iso7816_fidi.h" + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +/* Table 7 of ISO 7816-3:2006 */ +static const uint16_t fi_table[] = { + 372, 372, 558, 744, 1116, 1488, 1860, 0, + 0, 512, 768, 1024, 1536, 2048, 0, 0 +}; + +/* Table 8 from ISO 7816-3:2006 */ +static const uint8_t di_table[] = { + 0, 1, 2, 4, 8, 16, 32, 64, + 12, 20, 2, 4, 8, 16, 32, 64, +}; + +/* compute the F/D ratio based on Fi and Di values */ +int compute_fidi_ratio(uint8_t fi, uint8_t di) +{ + uint16_t f, d; + int ret; + + if (fi >= ARRAY_SIZE(fi_table) || + di >= ARRAY_SIZE(di_table)) + return -EINVAL; + + f = fi_table[fi]; + if (f == 0) + return -EINVAL; + + d = di_table[di]; + if (d == 0) + return -EINVAL; + + /* See table 7 of ISO 7816-3: From 1000 on we divide by 1/d, + * which equals a multiplication by d */ + if (di < 8) + ret = f / d; + else + ret = f * d; + + return ret; +} diff --git a/firmware/src_simtrace/iso7816_fidi.h b/firmware/src_simtrace/iso7816_fidi.h new file mode 100644 index 00000000..e4690a5d --- /dev/null +++ b/firmware/src_simtrace/iso7816_fidi.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +/* compute the F/D ratio based on Fi and Di values */ +int compute_fidi_ratio(uint8_t fi, uint8_t di); diff --git a/firmware/src_simtrace/simtrace_iso7816.c b/firmware/src_simtrace/simtrace_iso7816.c index 086cd323..5325bba4 100644 --- a/firmware/src_simtrace/simtrace_iso7816.c +++ b/firmware/src_simtrace/simtrace_iso7816.c @@ -32,6 +32,7 @@ *------------------------------------------------------------------------------*/ #include "board.h" +#include "iso7816_fidi.h" #include #include @@ -109,46 +110,6 @@ void USART1_IrqHandler( void ) } /* FIDI update functions */ -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) - -/* Table 6 from ISO 7816-3 */ -static const uint16_t fi_table[] = { - 0, 372, 558, 744, 1116, 1488, 1860, 0, - 0, 512, 768, 1024, 1536, 2048, 0, 0 -}; - -/* Table 7 from ISO 7816-3 */ -static const uint8_t di_table[] = { - 0, 1, 2, 4, 8, 16, 0, 0, - 0, 0, 2, 4, 8, 16, 32, 64, -}; - -/* compute the F/D ratio based on Fi and Di values */ -static int compute_fidi_ratio(uint8_t fi, uint8_t di) -{ - uint16_t f, d; - int ret; - - if (fi >= ARRAY_SIZE(fi_table) || - di >= ARRAY_SIZE(di_table)) - return -EINVAL; - - f = fi_table[fi]; - if (f == 0) - return -EINVAL; - - d = di_table[di]; - if (d == 0) - return -EINVAL; - - if (di < 8) - ret = f / d; - else - ret = f * d; - - return ret; -} - void update_fidi(uint8_t fidi) { int rc;