AN220253 Using the CRYPTO module in TRAVEO™ T2G family
About this document
Scope and purpose
AN220253 describes how to configure and use the cryptography block (CRYPTO) module in the TRAVEO™ T2G family. This application note covers the usage of various cryptography-related functions such as asymmetric/symmetric encryption/decryption, hash value calculation, and true and pseudo random number generation.
Intended audience
This document is intended for anyone who uses Infineon TRAVEO™ T2G MCUs for CRYPTO module.
Associated part family
TRAVEO™ T2G
Introduction
Embedded applications today have ever-increasing requirements regarding security features. In automotive applications, security features are typically used to do the following:
Protect intellectual property
Authenticate messages between electronic control units in the car or from the outside
Provide tamper protection (e.g., prevent tuning of engine parameters or modification of trip recorder)
Allow after-market feature activation (e.g., enable speed limiter)
Ensure that only original equipment manufacturer (OEM) spare parts can be used
The TRAVEO ™ T2G family of microcontrollers offers a specially developed set of security features to enable such use cases. Apart from life cycle and protection schemes such as those that control the access permissions for the debug interface or for the software running on the microcontroller, one important component is the cryptography block (or CRYPTO) which will be explained in this application note. These descriptions will focus on the Infineon “Crypto” driver, part of the Sample Driver Library (SDL).
Cryptographic operations and features
Random number generator (RNG)
There are two types: pseudo random number generator (PRNG) and true random number generator (TRNG).
A PRNG outputs a random looking sequence of numbers based on a mathematical formula and a given start value (also known as “seed”). Attackers with knowledge about the PRNG may be able to predict future random numbers.
TRNGs use physical properties (thermal noise, for example) as a source of true randomness. Typically, post-processing steps are executed and monitoring features are implemented to detect a malfunctioning of the TRNG, which might be caused by an attacker trying to override the output of a TRNG.
The advantages of PRNGs are that they are usually much faster in providing random numbers than the TRNG. In addition – depending on the physical implementation – active TRNGs may cause power dissipation that is unacceptable by certain applications (for example, a ring oscillator causes dynamic switching currents). These are the reasons why many applications use a TRNG to generate a true random seed for the PRNG.
Symmetric cryptography
Symmetric cryptography relies on a shared secret between the sender and receiver of a message. The same secret key is used for encryption as well as decryption.
Asymmetric cryptography
In the case of asymmetric cryptography, each party has a private and public key. The principles of asymmetric cryptography allow a sender to use the receiver’s public key to encrypt a message that can be decrypted by the receiver with its private key. Alternatively, the sender can generate a digital signature of a message using its private key and any receiver can verify this message by using the sender’s public key.
Hash functions
Hash functions produce a fixed-length output (“hash value” or “digest”) for a variable length input. Cryptographic hash functions are a special kind of hash functions with certain stronger requirements related to their characteristics.
The hash value together with the input data to the hash functions can be used to check the integrity of the input data or hash value.
Message authentication codes (MAC)
MACs are based on cryptographic hash functions or symmetric cryptography. In addition to the integrity of a message, the authenticity of a message can be ensured because of a shared secret between sender and receiver.
Digital signatures
Digital signatures are generated by using asymmetric cryptography in combination with cryptographic hash functions. They provide the following security features: integrity, authenticity, and non-repudiation, i.e., the sender cannot deny that a message originated from them because the sender’s private key was used to generate the signature and the receiver can prove this using the sender’s public key.
Crypto driver
Driver architecture
The design of the Crypto driver is based on a client-server architecture. The Crypto server runs only on the CM0+ core and works with the Crypto hardware by executing appropriate Crypto Core functions. The Crypto client can be run on either core, but this application note only covers the case that the client runs on the CM4 core. All descriptions and figures that refer to CM4 are also applicable for TRAVEO™ T2G devices with (dual) CM7. The client and server communicate through the Inter-Processor Communication (IPC). Using IPC for communication provides a simple synchronization mechanism to handle concurrent requests from different cores.
The Crypto driver utilizes the following hardware resources besides the CRYPTO block:
1 IPC channel structure for data exchange between client and server
2 IPC interrupt structures for notifications
1 interrupt for handling of hardware errors
The general communication concept between Crypto server and client is depicted in Figure 1 .
Figure 1.
Server/client communication example via IPC
Driver initialization
Before the driver can be used for cryptographic operations, it needs to be initialized in the correct sequence.
The Crypto server must be initialized by CM0+.
After the Crypto server initialization is completed, the client can be successfully initialized by CM4.
Note:
Because the Crypto server runs on CM0+ and the client can run on CM4 and both cores run independently from each other with different frequencies, server initialization may not be finished when client is initialized. Ideally, the CM4 core is released from reset after the Crypto server has been initialized by CM0+ to avoid this situation. Alternatively, the server state can be checked before initializing the client.
Figure 2 shows the initialization of the Crypto server and client.
Figure 2.
Server/client initialization flowchart example
Driver usage
Once the initialization of the server as well as the client is complete, the client can request cryptographic operations from the server. While the server running on CM0+ is processing the requested operation, CM4 can be used to process other tasks. The end of the operation can be determined by three different methods:
Calling the Crypto driver function
Cy_Crypto_Syncin blocking mode. Once the function returns, the client can be sure that the previous operation is complete.Periodically calling the Crypto driver function
Cy_Crypto_Syncin non-blocking mode and checking the return value. If the return value indicates that the server is ready, the previous operation is completeThe client can configure the IPC release interrupt to get notified immediately upon completion of the server operation.
In this application note, the first method (calling
Cy_Crypto_Sync
in blocking mode) is used and shown in the flowcharts, also
indicated by the function parameter and value “block=true”.
Figure 3
shows the general usage of the
Crypto driver once it has been initialized.
Figure 3.
General driver usage flowchart example
Cyclic redundancy check (CRC)
Use case
The CRC result of a memory area (for example, parameters in flash) can be stored in addition to this area. Software can then ensure that the contents of the area have not been corrupted by causes such as environmental influences or hardware defects.
Note:
CRC is not a cryptographic operation and must not be used to protect such areas against malicious modifications, because an attacker can easily compute the CRC result for modified areas.
Driver functions
The Crypto driver provides following CRC-related functions:
Cy_Crypto_Crc_Init
This function initializes basic CRC-related settings that can later be reused across
multiple CRC calculations. Many CRC standards such as the widely used CRC32 or CRC16-CCITT
differ in the used polynomial (and length) and input/output settings. Those settings are
defined by calling
Cy_Crypto_Crc_Init
.
Cy_Crypto_Crc_Run
The actual CRC calculation is requested by calling this function. The initial value for calculation, start address, and size are passed to the function and the CRC result is returned.
The initial value for the CRC calculation is defined by the used CRC standard and applies
for the start of a new CRC calculation. If CRC is calculated over multiple not-contiguous
memory areas,
Cy_Crypto_Crc_Run
must be called multiple times (after the
current operation has finished) and the result of the previous block must be used as the
initial value for the next block.
Flowchart
Figure 4 shows the general flow of how to use the Crypto driver to calculate a CRC value.
Figure 4.
Flowchart of CRC calculation
Note:
For repeated usage of CRC operations, it
is not necessary to call
Cy_Crypto_Crc_Init
again unless one of the
related parameters (e.g. polynomial) changes.
Pseudo random number generator (PRNG)
Use case
The pseudo random number generator can be used to derive the following:
symmetric keys for a communication session
"cryptographic salt", an additional random input for hash functions to prevent dictionary attacks that use huge databases of pre-computed hash values for all words in a dictionary
“challenge” values in challenge-response-authentication protocols.
Note:
The PRNG seed values should be initialized with true random values with high entropy to provide strong security characteristics. TRNG can be used for that purpose. Non-random or constant seed values should only be used if a deterministic behavior such as reproducible PRNG output is required in temporary situations such as during software development and testing.
Driver functions
The Crypto driver provides the following PRNG related functions:
Cy_Crypto_Prng_Init
This function takes the seed values to initialize the three linear feedback shift registers that constitute the PRNG.
Cy_Crypto_Prng_Generate
A pseudo random number is generated by calling this function. The upper limit for the pseudo random number can be specified so that the function returns a number between 0 and the specified limit. The maximum upper limit is 2 32 - 1.
Note:
Cy_Crypto_Prng_Init
needs to be called only when the seed values is
changed afterwards, it is sufficient to call
Cy_Crypto_Prng_Generate
when
multiple random numbers are needed.
Flowchart
Figure 5 shows the general flow of how to use the Crypto driver to generate a pseudo random number.
Figure 5.
Flowchart of pseudo random number generation
Note:
Cy_Crypto_Prng_Init
needs to be called only if the seed values is changed; this function need not be called for repeated generation of PRNG values.
True random number generator (TRNG)
Use case
TRNG is typically used only to generate true random seed values with high entropy for PRNG because it has a comparably high current consumption and produces the random bits rather slowly.
Driver functions
The Crypto driver provides the following TRNG related functions:
Cy_Crypto_Trng_Generate
This function generates a true random number with the specified bit length. Some parameters related to its physical construction (ring oscillator) need to be stated when calling this function.
Flowchart
Figure 6 shows the general flow of how to use the Crypto driver to generate a true random number.
Figure 6.
Flowchart of true random number generation
Symmetric key cryptography using advanced encryption standard (AES)
The Crypto block supports several symmetric key cryptography standards, but AES is the most popular and widely used one.
AES operations per se are based on a single block of data of 128 bits. If multiple blocks of related data are encrypted, it is recommended to use one of the block chaining modes that have been described in scientific publications such as National Institute of Standards and Technology (NIST) Special Publication 800- 38A . Encrypting each block independently usually provides an insufficient level of security-- an example is the case where blocks with the same plaintext would also have the same cipher text.
The Crypto driver supports the following AES operation modes:
ECB (Electronic Code Book Mode)
This mode encrypts/decrypts blocks independently without chaining and therefore provides a low level of security for multiple blocks of related data.
CBC (Cipher Block Chaining Mode)
The cipher text of one block is combined with the plaintext of the following block. Because the first block does not have a preceding block, a so-called initialization vector (IV) is needed, which is then combined with the plaintext of the first block.
CFB (Cipher Feedback Mode)
This mode can be used as stream cipher for a plaintext with arbitrary length. The plaintext is XORed with the output of the AES encryption operation. The cipher text is used as the encryption input for the succeeding block. The first block requires an initialization vector similar to CBC mode.
CTR (Counter Mode)
The counter mode of operation is also used as a stream cipher. Again, the plaintext is XORed with the output of the AES encryption operation. The input to the AES encryption is a combination or concatenation of a so-called nonce and a counter. The counter changes for every block; in the simplest case, it is just being incremented.
A nonce in the cryptographic context is an arbitrary number that is usually only used once for a cryptographic operation.
Use case
Because the performance requirements of AES are so much lower than for asymmetric key cryptography, it is typically used for secure communication between devices that possesses the cryptographic key. In contrast, asymmetric key cryptography is often only used by communication protocols to establish the communication session by exchanging the key between all involved parties. This key will then be used for symmetric encryption and decryption.
Driver functions
The Crypto driver provides following AES related functions. An initialization of AES is not needed.
Cy_Crypto_Aes_Ecb_Run
This function encrypts or decrypts a single 128-bit block of data. The user is requested to specify the direction (encryption/decryption), key location and key size, and source and destination block location.
Cy_Crypto_Aes_Cbc_Run
In addition to
Cy_Crypto_Aes_Ecb_Run
, this function expects the
initialization vector and the total size of the data to be encrypted/decrypted.
Cy_Crypto_Aes_Cfb_Run
This function expects the same arguments as
Cy_Crypto_Aes_Cbc_Run
.
Cy_Crypto_Aes_Ctr_Run
This function expects the same arguments as
Cy_Crypto_Aes_Cbc_Run
. The initialization vector is the
nonce combined with the starting counter value. The driver internally increments the
counter part for every new block encryption.
Flowchart
Figure 7 shows the general flow of how to use the Crypto driver to execute AES operations.
Figure 7.
Flowchart of AES operation
Other symmetric key algorithms
In addition to the popular AES algorithm, the Crypto driver and hardware support the following algorithms as well:
Data Encryption Standard (DES)
The
Cy_Crypto_Des_Runfunction is offered by the Crypto driver.TDES (Triple DES, sometimes also abbreviated as 3DES)
The corresponding driver function is
Cy_Crypto_Tdes_Run.ChaCha: It is supported by the
Cy_Crypto_Chacha_Rundriver function.