ST AN3992 APPLICATION NOTE

AN3992
MS19972V1
ISO15693 RF
STM8L162M8
Plain data
Encrypted data
CR95HF
LRxk
contactless tag
ISO/IEC 15693 RF
transaction
SPI or UART bus
AES hardware
RF communication
Application note
Using an STM8L162M8 AES hardware accelerator with a CR95HF
to encrypt contactless tag data memory
Introduction
This application note describes STM8L162M8 demonstration firmware which reads and writes encrypted data into an LRxk contactless tag. The MCU encrypts data using its embedded AES hardware and sends it to a contactless tag through the CR95HF transceiver.
The data stored into the contactless tag can be read by anyone but decrypted only by the encryption or decryption key owner.

Figure 1. Data encryption diagram

April 2012 Doc ID 022369 Rev 3 1/22
www.st.com
Contents AN3992
Contents
1 Acronyms and notational conventions . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1 List of terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Notational conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.1 Binary number representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.2 Hexadecimal number representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.3 Decimal number representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 AES cryptography overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 CR95HF overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 STM8L162M8 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.1 AES hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 AES encryption mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Key derivation mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 Key derivation and decryption mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4 Application setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.1 Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.1.1 STM8L162M8 microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.1.2 STM8L1528_EVAL evaluation board . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.2 CR95HF plug board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.3 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.3.1 ST Visual Develop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.3.2 Cosmic compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.3.3 HyperTerminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.4 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.5 Pinout description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.5.1 Communication with CR95HF I/Os . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6 Contactless tag layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.7 Using the software implementation of AES chaining modes . . . . . . . . . . 16
4.7.1 HyperTerminal welcome screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2/22 Doc ID 022369 Rev 3
AN3992 Contents
4.7.2 Contactless tag memory initialization screen . . . . . . . . . . . . . . . . . . . . . 17
4.7.3 Reading contactless tag memory screen . . . . . . . . . . . . . . . . . . . . . . . . 18
4.7.4 Encrypting contactless tag memory screen . . . . . . . . . . . . . . . . . . . . . . 18
4.7.5 Decrypting contactless tag memory screen . . . . . . . . . . . . . . . . . . . . . . 19
5 Additional recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5.1 Firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5.2 Direct memory access (DMA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5.3 Encryption and decryption keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5.4 Block padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Doc ID 022369 Rev 3 3/22
List of figures AN3992
List of figures
Figure 1. Data encryption diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Figure 2. AES hardware accelerator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 3. AES hardware accelerator: encryption mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 4. AES hardware accelerator: key derivation mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 5. AES hardware accelerator: key derivation and decryption mode . . . . . . . . . . . . . . . . . . . . 11
Figure 6. STM8L1528_EVAL board (Rev. A) connectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Figure 7. PLUG-CR95HF-B Board I/Os . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Figure 8. Workspace organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Figure 9. Application flow chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Figure 10. HyperTerminal welcome screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Figure 11. Contactless tag memory initialization screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Figure 12. Reading contactless tag memory screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Figure 13. Encrypting contactless tag memory screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Figure 14. Decrypting contactless tag memory screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4/22 Doc ID 022369 Rev 3
AN3992 Acronyms and notational conventions

1 Acronyms and notational conventions

1.1 List of terms

Table 1. List of terms

Acronyms Definitions
ADC Analog to Digital Converter
CISC Complex Instruction Set Computer
DAC Digital to Analog Converter
EEPROM Electrically Erasable Programmable Read-Only Memory
IC Integrated Circuit
IEC International Electrotechnical Commission
ISO International Organization for Standardization
LED Light Emitting Diode
LCD Liquid Crystal Display
FIPS Federal Information Processing Standard
MIPS Million Instructions Per Second
NFC Near Field Communication
RF Radio Frequency
RFID Radio Frequency Identification
SPI Serial Peripheral Interface
USART Universal Synchronous/Asynchronous Receiver/Transmitter

1.2 Notational conventions

The following conventions and notations apply in this document unless otherwise stated.

1.2.1 Binary number representation

Binary numbers are represented by strings of digits 0 and 1, with the Most Significant Bit (MSB) on the left, the Least Significant Bit (LSB) on the right, and “0b” added at the beginning.
For example: 0b11110101

1.2.2 Hexadecimal number representation

Hexadecimal numbers are represented by numbers 0 to 9, characters A - F, and “0x” added at the beginning. The Most Significant Byte (MSB) is shown on the left and the Least Significant Byte (LSB) on the right.
For example: 0xF5
Doc ID 022369 Rev 3 5/22
Acronyms and notational conventions AN3992

1.2.3 Decimal number representation

Decimal numbers are represented as is, without any trailing character. For example: 245
6/22 Doc ID 022369 Rev 3
AN3992 Overview

2 Overview

2.1 AES cryptography overview

The purpose of cryptography is to protect sensitive data to avoid it from being read by unauthorized persons. There are many algorithms that implement cryptography. These techniques can be split into:
Asymmetric cryptography algorithms: These algorithms use a key to encrypt and
another key to decrypt messages. RSA and DSA are examples of this type of algorithm.
Symmetric cryptography algorithms: These algorithms use the same key to encrypt
and decrypt messages. Advanced Encryption Standard (AES), Data Encryption Standard (DES) are examples of this type of algorithm.
The Advanced Encryption Standard (AES) specifies a FIPS-approved cryptography algorithm that can be used to protect electronic data. AES exists in three versions: 128-bit, 192-bit and 256-bit.

2.2 CR95HF overview

The CR95HF device is an RF transceiver IC for contactless application (ISO/IEC 15693, ISO/IEC 14443-3 and ISO/IEC 18092). It manages the RF communication with RFID or NFC contactless tags. It includes frame coding, RF modulation and contactless tag response decoding.
The CR95HF is a slave device. A host (such as an MCU) is required to control it.

2.3 STM8L162M8 overview

High-density STM8L162M8 microcontrollers have an embedded AES 128-bit hardware accelerator to off-load the CPU from encryption or decryption tasks. This AES peripheral is a fully compliant implementation of the AES standard as defined by the FIPS publication (FIPS PUB 197, 2001 November 26).
This application note applies to STM8L162M8 high-density devices with built-in AES peripheral. The software supplied with this application note provides an implementation of some commonly used AES chaining modes (ECB, CBC, CFB, OFB and CTR).
For more detailed information, you should refer to the AES section of the STM8L15x and STM8L16x microcontroller family reference manual (RM0031).
Doc ID 022369 Rev 3 7/22
+ 15 hidden pages