Change in osmo-ccid-firmware[master]: add ISO 7816-3 definitions and utilities

2019-05-09 Thread Harald Welte
Harald Welte has submitted this change and it was merged. ( 
https://gerrit.osmocom.org/13846 )

Change subject: add ISO 7816-3 definitions and utilities
..

add ISO 7816-3 definitions and utilities

Change-Id: Ice4eba380126ff92089b71d0ea328b0110f7ffec
---
M sysmoOCTSIM/gcc/Makefile
A sysmoOCTSIM/iso7816_3.c
A sysmoOCTSIM/iso7816_3.h
3 files changed, 225 insertions(+), 1 deletion(-)

Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved



diff --git a/sysmoOCTSIM/gcc/Makefile b/sysmoOCTSIM/gcc/Makefile
index 5d21cb0..cf0484c 100644
--- a/sysmoOCTSIM/gcc/Makefile
+++ b/sysmoOCTSIM/gcc/Makefile
@@ -83,6 +83,7 @@
 octsim_i2c.o \
 ncn8025.o \
 command.o \
+iso7816_3.o \
 hpl/osc32kctrl/hpl_osc32kctrl.o \
 driver_init.o \
 hal/src/hal_usart_async.o \
@@ -132,6 +133,7 @@
 "octsim_i2c.o" \
 "ncn8025.o" \
 "command.o" \
+"iso7816_3.o" \
 "hpl/osc32kctrl/hpl_osc32kctrl.o" \
 "driver_init.o" \
 "hal/src/hal_usart_async.o" \
@@ -188,7 +190,8 @@
 "octsim_i2c.d" \
 "ncn8025.d" \
 "command.d" \
-"hal/src/hal_cache.d" \
+"iso7816_3.d" \
+"hal/src/halcache.d" \
 "hal/src/hal_sleep.d" \
 "hal/utils/src/utils_ringbuffer.d" \
 "hpl/sercom/hpl_sercom.d" \
diff --git a/sysmoOCTSIM/iso7816_3.c b/sysmoOCTSIM/iso7816_3.c
new file mode 100644
index 000..f7262fb
--- /dev/null
+++ b/sysmoOCTSIM/iso7816_3.c
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon 

+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
USA.
+*/
+#include 
+#include 
+
+#include "utils.h"
+#include "iso7816_3.h"
+
+const uint16_t iso7816_3_fi_table[16] = {
+   372, 372, 558, 744, 1116, 1488, 1860, 0,
+   0, 512, 768, 1024, 1536, 2048, 0, 0
+};
+
+const uint32_t iso7816_3_fmax_table[16] = {
+   400, 500, 600, 800, 1200, 1600, 2000, 0,
+   0, 500, 750, 1000, 1500, 2000, 0, 0
+};
+
+const uint8_t iso7816_3_di_table[16] = {
+   0, 1, 2, 4, 8, 16, 32, 64,
+   12, 20, 0, 0, 0, 0, 0, 0,
+};
+
+/* all values are based on the Elementary Time Unit (ETU), defined in ISO/IEC 
7816-3 section 7.1
+ * this is the time required to transmit a bit, and is calculated as follows: 
1 ETU = (F / D) x (1 / f) where:
+ * - F is the clock rate conversion integer
+ * - D is the baud rate adjustment factor
+ * - f is the clock frequency
+ * the possible F, f(max), and D values are defined in ISO/IEC 7816-3 table 7 
and 8
+ * - the initial value for F (after reset) is Fd = 372
+ * - the initial value for D (after reset) is Dd = 1
+ * - the initial maximum frequency f(max) is 5 MHz
+ * the card must measure the ETU based on the clock signal provided by the 
reader
+ * one ETU (e.g. 1 bit) takes F/D clock cycles, which the card must count
+ *
+ * the card can indicate an alternative set of supported values Fi (with 
corresponding f(max)) and Di for higher baud rate in TA1 in the ATR (see 
ISO/IEC 7816-3 section 8.3)
+ * these values are selected according to ISO/IEC 7816-3 section 6.3.1:
+ * - card in specific mode: they are enforced if TA2 is present (the reader 
can deactivate the card if it does not support these values)
+ * - card in negotiable mode:
+ * -- they can be selected by the reader using the Protocol and Parameters 
Selection (PPS) procedure
+ * -- the first offered protocol and default values are used when no PPS is 
started
+ *
+ * PPS is done with Fd and Dd (see ISO/IEC 7816-3 section 9)
+ * the reader can propose any F and D values between from Fd to Fi, and from 
Dd to Di (Fi and Di are indicated in TA1)
+ * the in PPS agreed values F and D are called Fn and Dn and are applied after 
a successful exchange, corresponding to PPS1_Response bit 5
+ *
+ * the F and D values must be provided to the SAM3S USART peripheral (after 
reset and PPS)
+ */
+
+bool iso7816_3_valid_f(uint16_t f)
+{
+   if (0 == f) {
+   return false;
+   }
+   uint8_t i = 0;
+   for (i = 0; i < ARRAY_SIZE(iso7816_3_fi_table) && iso7816_3_fi_table[i] 
!= f; i++);
+   return (i < ARRAY_SIZE(iso7816_3_fi_table) && iso7816_3_fi_table[i] == 
f);
+}
+
+bool iso7816_3_valid_d(uint8_t d)
+{
+   if (0 == d) {
+   return false;
+   }
+   uint8_t i = 0;
+   for (i = 0; i < 

Change in osmo-ccid-firmware[master]: add ISO 7816-3 definitions and utilities

2019-05-02 Thread Harald Welte
Harald Welte has posted comments on this change. ( 
https://gerrit.osmocom.org/13846 )

Change subject: add ISO 7816-3 definitions and utilities
..


Patch Set 1: Code-Review+2


--
To view, visit https://gerrit.osmocom.org/13846
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-ccid-firmware
Gerrit-Branch: master
Gerrit-MessageType: comment
Gerrit-Change-Id: Ice4eba380126ff92089b71d0ea328b0110f7ffec
Gerrit-Change-Number: 13846
Gerrit-PatchSet: 1
Gerrit-Owner: Kévin Redon 
Gerrit-Reviewer: Harald Welte 
Gerrit-Reviewer: Jenkins Builder (102)
Gerrit-Comment-Date: Thu, 02 May 2019 15:00:38 +
Gerrit-HasComments: No
Gerrit-HasLabels: Yes


Change in osmo-ccid-firmware[master]: add ISO 7816-3 definitions and utilities

2019-05-02 Thread Kévin Redon
Kévin Redon has uploaded this change for review. ( 
https://gerrit.osmocom.org/13846


Change subject: add ISO 7816-3 definitions and utilities
..

add ISO 7816-3 definitions and utilities

Change-Id: Ice4eba380126ff92089b71d0ea328b0110f7ffec
---
M sysmoOCTSIM/gcc/Makefile
A sysmoOCTSIM/iso7816_3.c
A sysmoOCTSIM/iso7816_3.h
3 files changed, 225 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-ccid-firmware 
refs/changes/46/13846/1

diff --git a/sysmoOCTSIM/gcc/Makefile b/sysmoOCTSIM/gcc/Makefile
index 5d21cb0..cf0484c 100644
--- a/sysmoOCTSIM/gcc/Makefile
+++ b/sysmoOCTSIM/gcc/Makefile
@@ -83,6 +83,7 @@
 octsim_i2c.o \
 ncn8025.o \
 command.o \
+iso7816_3.o \
 hpl/osc32kctrl/hpl_osc32kctrl.o \
 driver_init.o \
 hal/src/hal_usart_async.o \
@@ -132,6 +133,7 @@
 "octsim_i2c.o" \
 "ncn8025.o" \
 "command.o" \
+"iso7816_3.o" \
 "hpl/osc32kctrl/hpl_osc32kctrl.o" \
 "driver_init.o" \
 "hal/src/hal_usart_async.o" \
@@ -188,7 +190,8 @@
 "octsim_i2c.d" \
 "ncn8025.d" \
 "command.d" \
-"hal/src/hal_cache.d" \
+"iso7816_3.d" \
+"hal/src/halcache.d" \
 "hal/src/hal_sleep.d" \
 "hal/utils/src/utils_ringbuffer.d" \
 "hpl/sercom/hpl_sercom.d" \
diff --git a/sysmoOCTSIM/iso7816_3.c b/sysmoOCTSIM/iso7816_3.c
new file mode 100644
index 000..f7262fb
--- /dev/null
+++ b/sysmoOCTSIM/iso7816_3.c
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon 

+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
USA.
+*/
+#include 
+#include 
+
+#include "utils.h"
+#include "iso7816_3.h"
+
+const uint16_t iso7816_3_fi_table[16] = {
+   372, 372, 558, 744, 1116, 1488, 1860, 0,
+   0, 512, 768, 1024, 1536, 2048, 0, 0
+};
+
+const uint32_t iso7816_3_fmax_table[16] = {
+   400, 500, 600, 800, 1200, 1600, 2000, 0,
+   0, 500, 750, 1000, 1500, 2000, 0, 0
+};
+
+const uint8_t iso7816_3_di_table[16] = {
+   0, 1, 2, 4, 8, 16, 32, 64,
+   12, 20, 0, 0, 0, 0, 0, 0,
+};
+
+/* all values are based on the Elementary Time Unit (ETU), defined in ISO/IEC 
7816-3 section 7.1
+ * this is the time required to transmit a bit, and is calculated as follows: 
1 ETU = (F / D) x (1 / f) where:
+ * - F is the clock rate conversion integer
+ * - D is the baud rate adjustment factor
+ * - f is the clock frequency
+ * the possible F, f(max), and D values are defined in ISO/IEC 7816-3 table 7 
and 8
+ * - the initial value for F (after reset) is Fd = 372
+ * - the initial value for D (after reset) is Dd = 1
+ * - the initial maximum frequency f(max) is 5 MHz
+ * the card must measure the ETU based on the clock signal provided by the 
reader
+ * one ETU (e.g. 1 bit) takes F/D clock cycles, which the card must count
+ *
+ * the card can indicate an alternative set of supported values Fi (with 
corresponding f(max)) and Di for higher baud rate in TA1 in the ATR (see 
ISO/IEC 7816-3 section 8.3)
+ * these values are selected according to ISO/IEC 7816-3 section 6.3.1:
+ * - card in specific mode: they are enforced if TA2 is present (the reader 
can deactivate the card if it does not support these values)
+ * - card in negotiable mode:
+ * -- they can be selected by the reader using the Protocol and Parameters 
Selection (PPS) procedure
+ * -- the first offered protocol and default values are used when no PPS is 
started
+ *
+ * PPS is done with Fd and Dd (see ISO/IEC 7816-3 section 9)
+ * the reader can propose any F and D values between from Fd to Fi, and from 
Dd to Di (Fi and Di are indicated in TA1)
+ * the in PPS agreed values F and D are called Fn and Dn and are applied after 
a successful exchange, corresponding to PPS1_Response bit 5
+ *
+ * the F and D values must be provided to the SAM3S USART peripheral (after 
reset and PPS)
+ */
+
+bool iso7816_3_valid_f(uint16_t f)
+{
+   if (0 == f) {
+   return false;
+   }
+   uint8_t i = 0;
+   for (i = 0; i < ARRAY_SIZE(iso7816_3_fi_table) && iso7816_3_fi_table[i] 
!= f; i++);
+   return (i < ARRAY_SIZE(iso7816_3_fi_table) && iso7816_3_fi_table[i] == 
f);
+}
+
+bool iso7816_3_valid_d(uint8_t d)
+{
+   if (0 == d) {
+   return false;
+   }
+   uint8_t i = 0;
+   for (i = 0; i <