Architecture
Electricals
The SIMshield board contains two independent Microcontrollers (MCUs). Each MCU has different responsibilities, and therefore runs on a different firmware.
The Mobile Equipment (ME) side communicates with a mobile device, therefore it needs to behave as a SIM card, while the Smart Card (SC) half faces the SIM card and it needs to act as the terminal.
An I2C bus connects both MCUs and allows them to communicate together. In our case, the ME side becomes the controller of the bus, while the SC side operates as the peripheral. We also created a custom messenger protocol on top of I2C to allow both sides to send and receive messages on the bus.
Mobile Equipment (main_me)
The firmware initializes all of the hardware and software components, and then enters an infinite while loop where it keeps executing the loop function of the iso7816 module. This is where the internal state machine is processed.
There is also a small asynchronous component: The UART0 interrupt handler. Whenever a UART transaction ends, an interrupt will be triggered and processed by our handler which, in essence, disconnects the TX pin from the data line of the ISO 7816 interface using its output latch, and then re-enables reception by re-connecting the RX pin to the data line.
The data line is an open-drain pin, therefore it is pulled up to a high state (VCC) using a pull-up resistor, and the driver of the pin only needs to pull the line down to the low state, i.e., it must open the drain to let the line fall back down to a low state.
The data line is actually shared between the RX and TX pins. The RX pin is configured to use an internal pull-up resistor, one built-into the MCU itself. This saves us from needing an external resistor, and gives us software control over the pull-up action as well. The TX pin, on the other hand, is configured to drive the data line, therefore whenever its output latch is enabled, i.e., the pin is connected to the output line, the data line can no longer be driven by the party on the other end, and it also means we should stop receiving to avoid obtaining a copy of what we send out.
The main goal of the ME half of the SIMshield is to process commands, i.e., Command TPDUs, coming in from the terminal, and parsing them and either rejecting the command if it's unsupported, or passing it onto the other SC half of the SIMshield so it can be forwarded to the SIM card. The SIM card produces Response TPDUs and passes them back through both MCUs, to the terminal.
Once the terminal resets the SIMshield, it expects a sequence of bytes called the Answer-to-Reset (ATR). We send a specially crafted ATR that describes to the terminal a safe electrical and logical configuration such that we can maximize the support and compatibility with terminals and SIM cards, while staying within the limits imposed by the standards that describe the terminal-SIM interface.
The MSPM0C1104 MCUs we are using are also only rated for voltages up to 3.6 V, therefore the ATR must also specify that we explicitly do not support the 5 V voltage class to avoid damage to the SIMshield electronics, in particular the MCUs. We have successfully tested SIMshield in the 5 V class to ensure that its internal ESD protection is sufficient to safeguard the rest of the integrated circuit from damage, but this is outside of the absolute maximum rating of 4.1 V, and therefore we avoid it as best as we can.
Below is a simplified diagram of the internal state machine as implemented by the iso7816 module of the ME firmware:
Smart Card (main_sc)
The firmware design is similar to the ME side, but with a different state machine in its iso7816 module.
Below is a diagram representing the high-level state machine for the interface. The ATR and TPDU states are actually compound states consisting of their own state machines. This is done because parsing an ATR statefully can be a complex process, especially since identical information can be represented in multiple ways and with ATRs of different lengths. Similarly for the TPDU state, once we initialize the card and process its ATR, we never leave the TPDU-processing phase of the transmission so keeping it isolated is a natural choice.
Parsing an ATR begins with the TS (initial character) that indicates what bytes A, B, C, and/or D will be present in the next portion of the sequence. Once this structural information is parsed, we know to expect, e.g., three bytes A, C, and D. The first parts indicate the supported protocols and supported clock and data rates. The ATR may also include historical bytes, i.e., a summary of how the card operates. This includes, e.g., card identification, capabilities, and detailed command handling behaviors.
The following is a summary of the stateful ATR parsing state machine:
The SC half of the SIMshield coordinates with the ME half to forward components of the commands and responses. The command first enters the ME half, and it gets forwarded to the SC side over the I2C bus. The header is then issued to the smart card and in exchange, it returns a procedure byte. This byte is forwarded back to the ME side and is sent to the terminal. A similar sequence of exchanges happens for every other portion of the command and response, including the command and response data payloads, and the status words.
Both halves of the SIMshield need to be aware, in advance, how long each portion of the command and response should be so that each half knows how many bytes to receive and transmit. The state machine below summarizes how we process the incoming and outgoing data, and how we transition between each phase of the exchange:
Summary
There is more to the firmware than just keeping track of states, as we also need to parse the commands, responses, and procedure bytes. Further explanations of the inner-workings of the firmware may be posted on the SIMcurity project log. At present, we encourage those who are interested to have a look at the repository itself:
git clone https://open.liskra.org/git/repo/simcurity/simshield__sw.git ./simshield__sw.git \
|| git clone https://codeberg.org/liskra_open/simcurity__simshield__sw.git ./simshield__sw.git;
We hope that the diagrams included on this page, combined with readability features embedded into our custom MCU Hardware Abstraction Layer (HAL) and the firmware itself, will aid in understanding and building on top of the SIMshield platform.