Change in simtrace2[master]: add library providing ISO 7816-3 utilities

2021-04-04 Thread laforge
laforge has submitted this change. ( 
https://gerrit.osmocom.org/c/simtrace2/+/23612 )

Change subject: add library providing ISO 7816-3 utilities
..

add library providing ISO 7816-3 utilities

this will become part of libosmocore since it it common to smart
card related projects (such as osmo-ccid-firmware)

Change-Id: I3d4c65d137fc4555fcb256443feadd1c695de73d
---
A firmware/libcommon/include/iso7816_3.h
A firmware/libcommon/source/iso7816_3.c
2 files changed, 229 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/firmware/libcommon/include/iso7816_3.h 
b/firmware/libcommon/include/iso7816_3.h
new file mode 100644
index 000..e6c1b4f
--- /dev/null
+++ b/firmware/libcommon/include/iso7816_3.h
@@ -0,0 +1,70 @@
+/*
+ * 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.
+*/
+
+/* this library provides utilities to handle the ISO-7816 part 3 communication 
aspects (e.g. related
+ * to F and D) */
+
+#pragma once
+
+#include 
+#include 
+
+/*! default clock rate conversion integer Fd.
+ *  ISO/IEC 7816-3:2006(E) section 8.1 */
+#define ISO7816_3_DEFAULT_FD 372
+
+/*! default baud rate adjustment factor Dd.
+ *  ISO/IEC 7816-3:2006(E) section 8.1 */
+#define ISO7816_3_DEFAULT_DD 1
+
+/*! default clock rate conversion integer Fi.
+ *  ISO/IEC 7816-3:2006(E) section 8.3
+ *  \note non-default value is optionally specified in TA1 */
+#define ISO7816_3_DEFAULT_FI 372
+
+/*! default baud rate adjustment factor Di.
+ *  ISO/IEC 7816-3:2006(E) section 8.3
+ *  \note non-default value is optionally specified in TA1 */
+#define ISO7816_3_DEFAULT_DI 1
+
+/*! default maximum clock frequency, in Hz.
+ *  ISO/IEC 7816-3:2006(E) section 8.3
+ *  \note non-default value is optionally specified in TA1 */
+#define ISO7816_3_DEFAULT_FMAX 500UL
+
+/*! default Waiting Integer (WI) value for T=0.
+ *  ISO/IEC 7816-3:2006(E) section 10.2
+ *  \note non-default value is optionally specified in TC2 */
+#define ISO7816_3_DEFAULT_WI 10
+
+/*! default Waiting Time (WT) value, in ETU.
+ *  ISO/IEC 7816-3:2006(E) section 8.1
+ *  \note depends on Fi, Di, and WI if protocol T=0 is selected */
+#define ISO7816_3_DEFAULT_WT 9600
+
+extern const uint16_t iso7816_3_fi_table[];
+
+extern const uint32_t iso7816_3_fmax_table[];
+
+extern const uint8_t iso7816_3_di_table[];
+
+bool iso7816_3_valid_f(uint16_t f);
+
+bool iso7816_3_valid_d(uint8_t d);
+
+int32_t iso7816_3_calculate_wt(uint8_t wi, uint16_t fi, uint8_t di, uint16_t 
f, uint8_t d);
diff --git a/firmware/libcommon/source/iso7816_3.c 
b/firmware/libcommon/source/iso7816_3.c
new file mode 100644
index 000..ccdff68
--- /dev/null
+++ b/firmware/libcommon/source/iso7816_3.c
@@ -0,0 +1,159 @@
+/*
+ * 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"
+
+/*! Table of clock rate conversion integer F indexed by Fi.
+ *  \note Fi is indicated in TA1, but the same table is used for F and Fn 
during PPS
+ *  ISO/IEC 7816-3:2006(E) table 7 */
+const uint16_t iso7816_3_fi_table[16] = {
+   372, 372, 558, 744, 1116, 1488, 1860, 0,
+   0, 512, 768, 1024, 1536, 2048, 0, 0
+};
+
+/*! Table of maximum clock frequency f_max in Hz indexed by Fi.
+ *  ISO/IEC 7816-3:2006(E) table 7
+ *  \note f_max is indicated in TA1, but the same table is used for F and Fn 
during PPS */
+const uint32_t iso7816_3_fmax_tab

Change in simtrace2[master]: add library providing ISO 7816-3 utilities

2021-04-04 Thread laforge
laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/simtrace2/+/23612 )

Change subject: add library providing ISO 7816-3 utilities
..


Patch Set 2: Code-Review+2


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

Gerrit-Project: simtrace2
Gerrit-Branch: master
Gerrit-Change-Id: I3d4c65d137fc4555fcb256443feadd1c695de73d
Gerrit-Change-Number: 23612
Gerrit-PatchSet: 2
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: tsaitgaist 
Gerrit-Comment-Date: Sun, 04 Apr 2021 21:25:17 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in simtrace2[master]: add library providing ISO 7816-3 utilities

2021-04-04 Thread laforge
laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/simtrace2/+/23612 )

Change subject: add library providing ISO 7816-3 utilities
..


Patch Set 2: Code-Review+1


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

Gerrit-Project: simtrace2
Gerrit-Branch: master
Gerrit-Change-Id: I3d4c65d137fc4555fcb256443feadd1c695de73d
Gerrit-Change-Number: 23612
Gerrit-PatchSet: 2
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: tsaitgaist 
Gerrit-Comment-Date: Sun, 04 Apr 2021 21:20:23 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in simtrace2[master]: add library providing ISO 7816-3 utilities

2021-04-04 Thread laforge
Hello Jenkins Builder, tsaitgaist,

I'd like you to reexamine a change. Please visit

https://gerrit.osmocom.org/c/simtrace2/+/23612

to look at the new patch set (#2).

Change subject: add library providing ISO 7816-3 utilities
..

add library providing ISO 7816-3 utilities

this will become part of libosmocore since it it common to smart
card related projects (such as osmo-ccid-firmware)

Change-Id: I3d4c65d137fc4555fcb256443feadd1c695de73d
---
A firmware/libcommon/include/iso7816_3.h
A firmware/libcommon/source/iso7816_3.c
2 files changed, 229 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/12/23612/2
--
To view, visit https://gerrit.osmocom.org/c/simtrace2/+/23612
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: simtrace2
Gerrit-Branch: master
Gerrit-Change-Id: I3d4c65d137fc4555fcb256443feadd1c695de73d
Gerrit-Change-Number: 23612
Gerrit-PatchSet: 2
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: tsaitgaist 
Gerrit-MessageType: newpatchset


Change in simtrace2[master]: add library providing ISO 7816-3 utilities

2021-04-04 Thread laforge
Hello tsaitgaist,

I'd like you to do a code review. Please visit

https://gerrit.osmocom.org/c/simtrace2/+/23612

to review the following change.


Change subject: add library providing ISO 7816-3 utilities
..

add library providing ISO 7816-3 utilities

this will become part of libosmocore since it it common to smart
card related projects (such as osmo-ccid-firmware)

Change-Id: I3d4c65d137fc4555fcb256443feadd1c695de73d
---
A firmware/libcommon/include/iso7816_3.h
A firmware/libcommon/source/iso7816_3.c
2 files changed, 221 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/12/23612/1

diff --git a/firmware/libcommon/include/iso7816_3.h 
b/firmware/libcommon/include/iso7816_3.h
new file mode 100644
index 000..738d2a2
--- /dev/null
+++ b/firmware/libcommon/include/iso7816_3.h
@@ -0,0 +1,98 @@
+/*
+ * 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.
+*/
+/* this library provides utilities to handle the ISO-7816 part 3 communication 
aspects (e.g. related to F and D) */
+#pragma once
+
+#include 
+#include 
+
+/** default clock rate conversion integer Fd
+ *  @implements ISO/IEC 7816-3:2006(E) section 8.1
+ */
+#define ISO7816_3_DEFAULT_FD 372
+/** default baud rate adjustment factor Dd
+ *  @implements ISO/IEC 7816-3:2006(E) section 8.1
+ */
+#define ISO7816_3_DEFAULT_DD 1
+/** default clock rate conversion integer Fi
+ *  @implements ISO/IEC 7816-3:2006(E) section 8.3
+ *  @note non-default value is optionally specified in TA1
+ */
+#define ISO7816_3_DEFAULT_FI 372
+/** default baud rate adjustment factor Di
+ *  @implements ISO/IEC 7816-3:2006(E) section 8.3
+ *  @note non-default value is optionally specified in TA1
+ */
+#define ISO7816_3_DEFAULT_DI 1
+/** default maximum clock frequency, in Hz
+ *  @implements ISO/IEC 7816-3:2006(E) section 8.3
+ *  @note non-default value is optionally specified in TA1
+ */
+#define ISO7816_3_DEFAULT_FMAX 500UL
+/** default Waiting Integer (WI) value for T=0
+ *  @implements ISO/IEC 7816-3:2006(E) section 10.2
+ *  @note non-default value is optionally specified in TC2
+ */
+#define ISO7816_3_DEFAULT_WI 10
+/** default Waiting Time (WT) value, in ETU
+ *  @implements ISO/IEC 7816-3:2006(E) section 8.1
+ *  @note depends on Fi, Di, and WI if protocol T=0 is selected
+ */
+#define ISO7816_3_DEFAULT_WT 9600
+
+/** Table encoding the clock rate conversion integer Fi
+ *  @note Fi is indicated in TA1, but the same table is used for F and Fn 
during PPS
+ *  @implements ISO/IEC 7816-3:2006(E) table 7
+ */
+extern const uint16_t iso7816_3_fi_table[];
+
+/** Table encoding the maximum clock frequency f_max in Hz
+ *  @implements ISO/IEC 7816-3:2006(E) table 7
+ *  @note f_max is indicated in TA1, but the same table is used for F and Fn 
during PPS
+ */
+extern const uint32_t iso7816_3_fmax_table[];
+
+/** Table encoding the baud rate adjust integer Di
+ *  @implements ISO/IEC 7816-3:2006(E) table 8
+ *  @note Di is indicated in TA1, but the same table is used for D and Dn 
during PPS
+ */
+extern const uint8_t iso7816_3_di_table[];
+
+/* verify if the clock rate conversion integer F value is valid
+ * @param[in] f F value to be validated
+ * @return if F value is valid
+ * @note only values in ISO/IEC 7816-3:2006(E) table 7 are valid
+ */
+bool iso7816_3_valid_f(uint16_t f);
+/* verify if the baud rate adjustment factor D value is valid
+ * @param[in] d D value to be validated
+ * @return if D value is valid
+ * @note only values in ISO/IEC 7816-3:2006(E) table 8 are valid
+ */
+bool iso7816_3_valid_d(uint8_t d);
+/** calculate Waiting Time (WT)
+ *  @param[in] wi Waiting Integer
+ *  @param[in] fi clock rate conversion integer Fi value
+ *  @param[in] di baud rate adjustment factor Di value
+ *  @param[in] f clock rate conversion integer F value
+ *  @param[in] d baud rate adjustment factor D value
+ *  @return Waiting Time WT, in ETU, or < 0 on error (see code for return 
codes)
+ *  @note this should happen after reset and T=0 protocol select (through PPS 
or implicit)
+ *  @implements ISO/IEC 7816-3:2006(E) section 8.1 and 10.2
+ */
+int32_t iso7816_3_calculate_wt(uint8_t wi, uint16_t fi, uint8_t di, uint16