23. Cryptographic processor (CRYP)

This section applies to all STM32F77xxx devices, unless otherwise specified.

23.1 Introduction

The cryptographic processor (CRYP) can be used both to encrypt and decrypt data using the DES, Triple-DES or AES algorithms. It is a fully compliant implementation of the following standards:

Multiple key sizes and chaining modes are supported:

The CRYP processor is a 32-bit AHB peripheral. It supports DMA transfers for incoming and outgoing data (two DMA channels are required). The peripheral also includes input and output FIFOs (each 8 words deep) for better performance.

23.2 CRYP main features

23.3 CRYP implementation

This device has one CRYP instance.

23.4 CRYP functional description

23.4.1 CRYP block diagram

The figure below shows the block diagram of the cryptographic processor.

Figure 164. CRYP block diagram

CRYP block diagram showing internal architecture including AHB interface, registers, FIFOs, AES/DES cores, DMA, and IRQ interfaces.

The block diagram illustrates the internal architecture of the CRYP processor. On the left, a vertical bar represents the 32-bit AHB2 bus. The AHB interface connects this bus to the internal components. The 'Banked Registers (main)' section includes CRYP_DIN (data in), CRYP_DOUT (data out), CRYP_SR (status), CRYP_CR (control), CRYP_Kx (key), and CRYP_IVx (iv). CRYP_DIN feeds into an 8x32-bit IN FIFO, which then connects to the AES Core (AEA). CRYP_DOUT receives data from the AEA through an 8x32-bit OUT FIFO. CRYP_Kx and CRYP_IVx provide Key and IV inputs to the DES/TDES Core (DEA). The 'Banked Registers (IRQ & DMA)' section includes CRYP_IMSCR, CRYP_RIS, CRYP_MIS, and CRYP_DMACR. These registers connect to a DMA interface (with cryp_in_dma and cryp_out_dma signals) and an IRQ interface (with cryp_it signal). Both interfaces connect to a central Control Logic block. The cryp_hclk signal provides the AHB bus clock to the AHB interface and Control Logic. The diagram is labeled MSv41968V2 in the bottom right corner.

CRYP block diagram showing internal architecture including AHB interface, registers, FIFOs, AES/DES cores, DMA, and IRQ interfaces.

23.4.2 CRYP internal signals

Table 154 provides a list of useful-to-know internal signals available at cryptographic processor level and not at STM32 product level (on pads).

Table 154. CRYP internal input/output signals

Signal nameSignal typeDescription
cryp_hclkDigital inputAHB bus clock
cryp_itDigital outputCryptographic processor global interrupt request
cryp_in_dmaDigital input/outputIN FIFO DMA burst request/ acknowledge
cryp_out_dmaDigital input/outputOUT FIFO DMA burst request/ acknowledge (with single request for DES/TDES)

23.4.3 CRYP DES/TDES cryptographic core

Overview

The DES/Triple-DES cryptographic core consists of three components:

The DES/Triple-DES cryptographic core provides two operating modes:

The operating mode is selected by programming the ALGODIR bit in the CRYP_CR register.

Typical data processing

Typical usage of the cryptographic processor in DES modes can be found in Section 23.4.10: CRYP DES/TDES basic chaining modes (ECB, CBC) .

Note: The outputs of the intermediate DEA stages are never revealed outside the cryptographic boundary, with the exclusion of the IV registers in CBC mode.

DES/TDES keying and chaining modes

The DES/TDES allows three different keying options:

The first option specifies that all the keys are independent, that is, K1, K2 and K3 are independent. FIPS PUB 46-3 – 1999 (and ANSI X9.52 – 1998) refers to this option as the Keying Option 1 and, to the TDES as 3-key TDES.

The second option specifies that K1 and K2 are independent and K3 is equal to K1, that is, K1 and K2 are independent, K3 = K1. FIPS PUB 46-3 – 1999 (and ANSI X9.52 – 1998) refers to this second option as the Keying Option 2 and, to the TDES as 2-key TDES.

The third option specifies that K1, K2 and K3 are equal, that is:

\[ K1 = K2 = K3 \]

FIPS PUB 46-3 – 1999 (and ANSI X9.52 – 1998) refers to the third option as the Keying Option 3. This “1-key” TDES is equivalent to single DES.

The following chaining algorithms are supported by the DES hardware and can be selected through the ALGOMODE bits in the CRYP_CR register:

These modes are described in details in Section 23.4.10: CRYP DES/TDES basic chaining modes (ECB, CBC) .

23.4.4 CRYP AES cryptographic core

Overview

The AES cryptographic core consists of the following components:

The AES core works on 128-bit data blocks of (four words) with 128-, 192- or 256-bit key lengths. Depending on the chaining mode, the peripheral requires zero or one 128-bit initialization vector (IV).

The cryptographic peripheral features two operating modes:

The operating mode is selected by programming the ALGODIR bit in the CRYP_CR register.

Typical data processing

A description of cryptographic processor typical usage in AES mode can be found in Section 23.4.11: CRYP AES basic chaining modes (ECB, CBC) .

Note: The outputs of the intermediate AEA stages is never revealed outside the cryptographic boundary, with the exclusion of the IV registers.

AES chaining modes

The following chaining algorithms are supported by the cryptographic processor and can be selected through the ALGOMODE bits in the CRYP_CR register:

A quick introduction on these chaining modes can be found in the following subsections.

For detailed instructions, refer to Section 23.4.11: CRYP AES basic chaining modes (ECB, CBC) and onward.

AES Electronic CodeBook (ECB) Figure 165. AES-ECB mode overview Diagram of AES-ECB mode showing encryption and decryption of three blocks.

The diagram illustrates the AES-ECB mode of operation, divided into two sections: Encryption and Decryption .

Encryption: Three separate paths are shown. Each path starts with a Plaintext block (1, 2, or 3) entering an Encrypt block. A key is also input to each Encrypt block. The output of each Encrypt block is a Ciphertext block (1, 2, or 3).

Decryption: Three separate paths are shown. Each path starts with a Ciphertext block (1, 2, or 3) entering a Decrypt block. A key is also input to each Decrypt block. The output of each Decrypt block is a Plaintext block (1, 2, or 3). A circular arrow icon is present on the Decrypt blocks, indicating key scheduling.

Legend:

MSv42140V1

Diagram of AES-ECB mode showing encryption and decryption of three blocks.

ECB is the simplest operating mode. There are no chaining operations, and no special initialization stage. The message is divided into blocks and each block is encrypted or decrypted separately.

Note: For decryption, a special key scheduling is required before processing the first block.

AES Cipher block chaining (CBC)

Figure 166. AES-CBC mode overview

Diagram of AES-CBC mode showing encryption and decryption processes for three blocks. It includes labels for plaintext, ciphertext, initialization vector, and key. A legend defines input, output, and key scheduling symbols.

The diagram illustrates the AES-CBC mode of operation for encryption and decryption across three blocks.

Encryption:

Decryption:

Legend:

MSv42141V1

Diagram of AES-CBC mode showing encryption and decryption processes for three blocks. It includes labels for plaintext, ciphertext, initialization vector, and key. A legend defines input, output, and key scheduling symbols.

CBC operating mode chains the output of each block with the input of the following block. To make each message unique, an initialization vector is used during the first block processing.

Note: For decryption, a special key scheduling is required before processing the first block.

AES Counter mode (CTR)

Figure 167. AES-CTR mode overview

Diagram of AES-CTR mode overview showing encryption and decryption processes.

The diagram illustrates the AES Counter mode (CTR) for encryption and decryption. It is divided into two main sections: Encryption and Decryption .

Encryption: A sequence of three counters is shown. The first counter outputs its value to an Encrypt block, which also takes a key as input. The output of the Encrypt block is XORed (indicated by a circle with a cross) with Plaintext block 1 to produce Ciphertext block 1 . The first counter also outputs a +1 increment to the second counter. The second counter outputs value + 1 to its Encrypt block, which XORs it with Plaintext block 2 to produce Ciphertext block 2 . The second counter outputs a +1 increment to the third counter. The third counter outputs value + 2 to its Encrypt block, which XORs it with Plaintext block 3 to produce Ciphertext block 3 .

Decryption: The process is similar but uses Decrypt blocks. The counters and their outputs ( value , value + 1 , value + 2 ) are the same. The output of each Decrypt block is XORed with the corresponding Ciphertext block (1, 2, or 3) to produce the Plaintext block (1, 2, or 3). The counters are still incremented by +1 between each block.

Legend:

MSv42142V1

Diagram of AES-CTR mode overview showing encryption and decryption processes.

The CTR mode uses the AES core to generate a key stream; these keys are then XORed with the plaintext to obtain the ciphertext as specified in NIST Special Publication 800-38A, Recommendation for Block Cipher Modes of Operation .

Note: Unlike ECB and CBC modes, no key scheduling is required for the CTR decryption, since in this chaining scheme the AES core is always used in encryption mode for producing the counter blocks.

AES Galois/Counter mode (GCM)

Figure 168. AES-GCM mode overview

Figure 168. AES-GCM mode overview diagram showing the encryption and authentication process.

The diagram illustrates the AES-GCM mode of operation. It starts with an Initialization vector (IV) and a key being input to an Init (Encrypt) block. This block produces a value H . The IV also feeds into a Counter block. The key is also input to a series of Encrypt blocks. The first Encrypt block takes the Counter value and Plaintext block 1 as input, producing Ciphertext block 1 . The Counter value is incremented by 1 for each subsequent block. The key is also input to a series of GF2mul blocks. The first GF2mul block takes H and Ciphertext block 1 as input, producing an intermediate value. This value is XORed with Plaintext block 2 and then input to the second GF2mul block, which produces another intermediate value. This value is XORed with Plaintext block 3 and then input to the third GF2mul block, which produces the final intermediate value. This value is input to a Final block, which produces the TAG .

Legend

MSv42143V1

Figure 168. AES-GCM mode overview diagram showing the encryption and authentication process.

In Galois/Counter mode (GCM), the plaintext message is encrypted, while a message authentication code (MAC) is computed in parallel, thus generating the corresponding ciphertext and its MAC (also known as authentication tag). It is defined in NIST Special Publication 800-38D, Recommendation for Block Cipher Modes of Operation - Galois/Counter Mode (GCM) and GMAC .

GCM mode is based on AES in counter mode for confidentiality. It uses a multiplier over a fixed finite field for computing the message authentication code. It requires an initial value and a particular 128-bit block at the end of the message.

AES Galois message authentication code (GMAC)

Figure 169. AES-GMAC mode overview

Figure 169. AES-GMAC mode overview diagram showing the authentication process.

The diagram illustrates the AES-GMAC mode of operation. It starts with an Initialization vector (IV) and a key being input to an Init (Encrypt) block. This block produces a value H . The key is also input to a series of GF2mul blocks. The first GF2mul block takes H and Plaintext block 1 as input, producing an intermediate value. This value is XORed with Plaintext block 2 and then input to the second GF2mul block, which produces another intermediate value. This value is XORed with Plaintext block 3 and then input to the third GF2mul block, which produces the final intermediate value. This value is input to a Final block, which produces the TAG .

Legend

MSv42144V1

Figure 169. AES-GMAC mode overview diagram showing the authentication process.

Galois Message Authentication Code (GMAC) allows authenticating a message and generating the corresponding message authentication code (MAC). It is defined in NIST Special Publication 800-38D, Recommendation for Block Cipher Modes of Operation - Galois/Counter Mode (GCM) and GMAC .

GMAC is similar to Galois/Counter mode (GCM), except that it is applied on a message composed only by clear-text authenticated data (i.e. only header, no payload).

AES Counter with CBC-MAC (CCM)

Figure 170. AES-CCM mode overview

Figure 170. AES-CCM mode overview diagram showing the encryption and authentication process. The diagram illustrates the flow from the initialization vector (B0) through counter generation and parallel encryption/encryption steps to produce ciphertext blocks and a final authentication tag.

The diagram illustrates the AES-CCM mode of operation. It starts with \( B_0 \) (Initialization vector) being input to an Init (Encrypt) block along with a key . The output of this block is the Initialization vector . Simultaneously, \( B_0 \) is used to generate a sequence of counters: Count 1 , Count 2 , and Count 3 , where each count is derived from the previous one by adding 1 ( \( +1 \) ). Each counter is input to an Encrypt block along with the key . The outputs of these Encrypt blocks are Ciphertext block 1 , Ciphertext block 2 , and Ciphertext block 3 . The Initialization vector is XORed ( \( \oplus \) ) with Plaintext block 1 and then input to an Encrypt block. The output of this Encrypt block is XORed with Plaintext block 2 and input to another Encrypt block. The output of this Encrypt block is XORed with Plaintext block 3 and input to a final Encrypt block. The output of this final Encrypt block is input to a Final block, which produces the TAG . A legend indicates that light gray boxes represent input , dark gray boxes represent output , and the \( \oplus \) symbol represents XOR . The diagram is labeled MSv42145V1.

Figure 170. AES-CCM mode overview diagram showing the encryption and authentication process. The diagram illustrates the flow from the initialization vector (B0) through counter generation and parallel encryption/encryption steps to produce ciphertext blocks and a final authentication tag.

In Counter with Cipher Block Chaining-Message Authentication Code (CCM), the plaintext message is encrypted while a message authentication code (MAC) is computed in parallel, thus generating the corresponding ciphertext and the corresponding MAC (also known as tag). It is described by NIST in Special Publication 800-38C, Recommendation for Block Cipher Modes of Operation - The CCM Mode for Authentication and Confidentiality .

CCM mode is based on AES in counter mode for confidentiality and it uses CBC for computing the message authentication code. It requires an initial value.

Like GCM CCM chaining mode, AES-CCM mode can be applied on a message composed only by cleartext authenticated data (i.e. only header, no payload). Note that this way of using CCM is not called CMAC (it is not similar to GCM/GMAC), and its usage is not recommended by NIST.

23.4.5 CRYP procedure to perform a cipher operation

Introduction

To understand how the cryptographic peripheral operates, a typical cipher operation is described below. For the detailed peripheral usage according to the cipher mode, refer to the specific section, for example Section 23.4.11: CRYP AES basic chaining modes (ECB, CBC) .

CRYP initialization

To initialize the cryptographic processor, first disable it by clearing the CRYPTEN bit in the CRYP_CR register. Then execute the following steps:

  1. 1. Configure the algorithm and the chaining mode through the ALGOMODE and ALGODIR bits in the CRYP_CR register. Configure also the key size with the KEYSIZE bits.
  2. 2. When the previous step is complete, and when applicable, write the symmetric key into the CRYP_KxL/R registers. The way to write key registers is defined in Section 23.4.17: CRYP key registers
  3. 3. Configure the data type (1, 8, 16 or 32 bits) through the DATATYPE bits of the CRYP_CR register.
  4. 4. When it is required (for example for CBC or CTR chaining modes), write the initialization vectors into the CRYP_IVx(L/R)R register.
  5. 5. Flush the IN and OUT FIFOs by writing the FFLUSH bit to 1 in the CRYP_CR register.

Preliminary warning for all cases

If the ECB or CBC mode is selected and data are not a multiple of 64 bits (for DES) or 128 bits (for AES), the second and the last block management is more complex than the sequences below. Refer to Section 23.4.8: CRYP stealing and data padding for more details.

Appending data using the CPU in Polling mode

  1. 1. Enable the cryptographic processor by setting to 1 the CRYPTEN bit in the CRYP_CR register.
  2. 2. Write data in the IN FIFO (one block or until the FIFO is full).
  3. 3. Repeat the following sequence until the second last block of data has been processed:
    1. a) Wait until the not-empty-flag OFNE is set to 1, then read the OUT FIFO (one block or until the FIFO is empty).
    2. b) Wait until the not-full-flag IFNF is set to 1, then write the IN FIFO (one block or until the FIFO is full) except if it is the last block.
  4. 4. The BUSY bit is set automatically by the cryptographic processor. At the end of the processing, the BUSY bit returns to 0 and both FIFOs are empty (IN FIFO empty flag IFEM = 1 and OUT FIFO not empty flag OFNE = 0).
  5. 5. If the next processing block is the last block, the CPU must pad (when applicable) the data with zeroes to obtain a complete block
  6. 6. When the operation is complete, the cryptographic processor can be disabled by clearing the CRYPTEN bit in CRYP_CR register.

Appending data using the CPU in Interrupt mode

  1. 1. Enable the interrupts by setting the INIM and OUTIM bits in the CRYP_IMSCR register.
  2. 2. Enable the cryptographic processor by setting to 1 the CRYPTEN bit in the CRYP_CR register.
  3. 3. In the interrupt service routine that manages the input data:
    1. a) If the last block is being loaded, the CPU must pad (when applicable) the data with zeroes to have a complete block. Then load the block into the IN FIFO.
    2. b) If it is not the last block, load the data into the IN FIFO. You can load only one block (2 words for DES, 4 words for AES), or load data until the FIFO is full.
    3. c) In all cases, after the last word of data has been written, disable the interrupt by clearing the INIM interrupt mask.
  4. 4. In the interrupt service routine that manages the input data:
    1. a) Read the output data from the OUT FIFO. You can read only one block (2 words for DES, 4 words for AES), or read data until the FIFO is empty.
    2. b) When the last word has been read, INIM and BUSY bits are set to 0 and both FIFOs are empty (IFEM = 1 and OFNE = 0). You can disable the interrupt by clearing the OUTIM bit, and disable the peripheral by clearing the CRYPTEN bit.
    3. c) If you read the last block of cleartext data (i.e. decryption), optionally discard the data that is not part of message/payload.

Appending data using the DMA

  1. 1. Prepare the last block of data by optionally padding it with zeroes to have a complete block.
  2. 2. Configure the DMA controller to transfer the input data from the memory and transfer the output data from the peripheral to the memory, as described in Section 23.4.19 .

CRYP DMA interface. The DMA should be configured to set an interrupt on transfer completion to indicate that the processing is complete.

  1. 3. Enable the cryptographic processor by setting to 1 the CRYPTEN bit in CRYP_CR register, then enable the DMA IN and OUT requests by setting to 1 the DIEN and DOEN bits in the CRYP_DMACR register.
  2. 4. All the transfers and processing are managed by the DMA and the cryptographic processor. The DMA interrupt indicates that the processing is complete. Both FIFOs are normally empty and BUSY flag is set 0.

Caution: It is important that DMA controller empties the cryptographic processor output FIFO before filling up the cryptographic processor input FIFO. To achieve this, the DMA controller should be configured so that the transfer from the cryptographic peripheral to the memory has a higher priority than the transfer from the memory to the cryptographic peripheral.

23.4.6 CRYP busy state

The cryptographic processor is busy and processing data (BUSY set to 1 in CRYP_SR register) when all the conditions below are met:

Write operations to the CRYP_Kx(L/R)R key registers, to the CRYP_IVx(L/R)R initialization registers, or to bits [9:2] of the CRYP_CR register, are ignored when cryptographic processor is busy (i.e. the registers are not modified). It is thus not possible to modify the configuration of the cryptographic processor while it is processing a data block.

It is possible to clear the CRYPTEN bit while BUSY bit is set to 1. In this case the ongoing DES/TDES or AES processing first completes (i.e. the word results are written to the output FIFO) before the BUSY bit is cleared by hardware.

Note: If the application needs to suspend a message to process another one with a higher priority, refer to Section 23.4.9: CRYP suspend/resume operations

When a block is being processed in DES or TDES mode, if the output FIFO becomes full and the input FIFO contains at least one new block, then the new block is popped off the input FIFO and the BUSY bit remains high until there is enough space to store this new block into the output FIFO.

23.4.7 Preparing the CRYP AES key for decryption

When performing an AES ECB or CBC decryption , the AES key has to be prepared. Indeed, in AES encryption the round 0 key is the one stored in the key registers, and AES decryption must start using the last round key. Hence, as the encryption key is stored in memory, a special key scheduling must be performed to obtain the decryption key. This preparation is not required in any other AES modes than ECB or CBC decryption.

When the cryptographic processor is disabled (CRYPEN = 0), the CRYP key preparation process is performed as follows:

  1. 1. Program ALGOMODE bits to 0x7 and ALGODIR to 0x0 in CRYP_CR. In addition, configure the key length with the KEYSIZE bits.
  2. 2. Write the symmetric key to the CRYP_KxL/R registers, as described in Section 23.4.17: CRYP key registers .
  3. 3. Enable the cryptographic processor by setting the CRYPEN bit in the CRYP_CR register. It immediately starts an AES round for key preparation (BUSY = 1).
  4. 4. Wait until the BUSY bit is cleared in the CRYP_SR register. Then update ALGOMODE bits in the CRYP_CR register to select the correct chaining mode, that is 0x4 for ECB or 0x5 for CBC.
  5. 5. The AES key is available in the CRYP key registers, ready to use for decryption.

Note: As the CRYPEN bitfield is reset by hardware at the end of the key preparation, the application software must set it again for the next operation.

The latency of the key preparation operation is 14, 16 or 18 clock cycles depending on the key size (128, 192 or 256 bits).

23.4.8 CRYP stealing and data padding

When using DES or AES algorithm in ECB or CBC modes to manage messages that are not multiple of the block size (64 bits for DES, 128 bits for AES), use ciphertext stealing techniques such as those described in NIST Special Publication 800-38A, Recommendation for Block Cipher Modes of Operation: Three Variants of Ciphertext Stealing for CBC Mode . Since the cryptographic processor does not implement such techniques, the last two blocks must be handled in a special way by the application.

Note: Ciphertext stealing techniques are not documented in this reference manual.

Similarly, when the AES algorithm is used in other modes than ECB or CBC, incomplete input data blocks (i.e. block shorter than 128 bits) have to be padded with zeroes by the application prior to encryption (i.e. extra bits should be appended to the trailing end of the data string). After decryption, the extra bits have to be discarded. The cryptographic processor does not implement automatic data padding operation to the last block , so the application should follow the recommendation given in Section 23.4.5: CRYP procedure to perform a cipher operation to manage messages that are not multiple of 128 bits.

Note: Padding data are swapped in a similar way as normal data, according to the DATATYPE field in CRYP_CR register (see Section 23.4.16: CRYP data registers and data swapping for details).

With this version of cryptographic processor, a special workaround is required in order to properly compute authentication tags while doing a GCM encryption or a CCM decryption with the last block of payload size inferior to 128 bits . This workaround is described below:

23.4.9 CRYP suspend/resume operations

A message can be suspended if another message with a higher priority has to be processed. When this highest priority message has been sent, the suspended message can be resumed in both encryption or decryption mode.

Suspend/resume operations do not break the chaining operation and the message processing can be resumed as soon as cryptographic processor is enabled again to receive the next data block.

Figure 171 gives an example of suspend/resume operation: message 1 is suspended in order to send a higher priority message (message 2), which is shorter than message 1 (AES algorithm).

Figure 171. Example of suspend mode management

Flowchart showing Message 1 processing being interrupted by a CRYP suspend sequence after block 3. Message 2 (blocks 1 and 2) is then processed. After Message 2, a CRYP resume sequence occurs, and Message 1 continues from block 4.
graph TD
    subgraph Message1 [Message 1]
    M1B1[128-bit block 1] --> M1B2[128-bit block 2]
    M1B2 --> M1B3[128-bit block 3]
    M1B3 -.-> Suspend[CRYP suspend sequence]
    Resume[CRYP resume sequence] --> M1B4[128-bit block 4]
    M1B4 --> M1B5[128-bit block 5]
    M1B5 --> M1B6[128-bit block 6]
    M1B6 --> Ellipsis[...]
    end

    subgraph Message2 [Message 2]
    M2B1[128-bit block 1] --> M2B2[128-bit block 2]
    end

    NewMsg((New higher priority message 2 to be processed)) --> M1B3
    Suspend --> M2B1
    M2B2 --> Resume
Flowchart showing Message 1 processing being interrupted by a CRYP suspend sequence after block 3. Message 2 (blocks 1 and 2) is then processed. After Message 2, a CRYP resume sequence occurs, and Message 1 continues from block 4.

A detailed description of suspend/resume operations can be found in each AES mode section.

23.4.10 CRYP DES/TDES basic chaining modes (ECB, CBC)

Overview

FIPS PUB 46-3 – 1999 (and ANSI X9.52-1998) provides a thorough explanation of the processing involved in the four operation modes supplied by the DES computing core: TDES-ECB encryption, TDES-ECB decryption, TDES-CBC encryption and TDES-CBC decryption. This section only gives a brief explanation of each mode.

DES/TDES-ECB encryption

Figure 172 illustrates the encryption in DES and TDES Electronic CodeBook (DES/TDES-ECB) mode. This mode is selected by programming ALGOMODE 0x0 and ALGODIR to 0 in CRYP_CR.

Figure 172. DES/TDES-ECB mode encryption

Figure 172. DES/TDES-ECB mode encryption diagram

The diagram shows the data flow for DES/TDES-ECB encryption. Plaintext P from the IN FIFO is passed through a swapping block controlled by DATATYPE. It then undergoes a three-stage DEA process: DEA encrypt with key K1, DEA decrypt with key K2, and DEA encrypt with key K3. The resulting output block O passes through another swapping block (also controlled by DATATYPE) to become ciphertext C, which is stored in the OUT FIFO.

graph TD
    IN[IN FIFO
plaintext P] -- "P, 64 bits" --> SW1[swapping] DT1[DATATYPE] --> SW1 SW1 --> DEA1[DEA, encrypt] K1[K1] -- "64" --> DEA1 DEA1 --> DEA2[DEA, decrypt] K2[K2] -- "64" --> DEA2 DEA2 --> DEA3[DEA, encrypt] K3[K3] -- "64" --> DEA3 DEA3 -- "O, 64 bits" --> SW2[swapping] DT2[DATATYPE] --> SW2 SW2 -- "C, 64 bits" --> OUT[OUT FIFO
ciphertext C]

ai16069b

Figure 172. DES/TDES-ECB mode encryption diagram

1. K: key; C: cipher text; I: input block; O: output block; P: plain text.

A 64-bit plaintext data block (P) is used after bit/byte/half-word as the input block (I). The input block is processed through the DEA in the encrypt state using K1. The output of this process is fed back directly to the input of the DEA where the DES is performed in the decrypt state using K2. The output of this process is fed back directly to the input of the DEA where the DES is performed in the encrypt state using K3. The resultant 64-bit output block (O) is used, after bit/byte/half-word swapping, as ciphertext (C) and it is pushed into the OUT FIFO.

Note: For more information on data swapping, refer to Section 23.4.16: CRYP data registers and data swapping .

Detailed DES/TDES encryption sequence can be found in Section 23.4.5: CRYP procedure to perform a cipher operation .

DES/TDES-ECB mode decryption

Figure 173 illustrates the decryption in DES and TDES Electronic CodeBook (DES/TDES-ECB) mode. This mode is selected by programming ALGOMODE to 0x0 and ALGODIR to 1 in CRYP_CR.

Figure 173. DES/TDES-ECB mode decryption

Diagram of DES/TDES-ECB mode decryption process. The flow starts with 'IN FIFO ciphertext C' (64 bits) entering a 'swapping' block controlled by 'DATATYPE'. The output is 'I, 64 bits' (input block). This block is processed through three 'DEA' (Data Encryption Algorithm) stages: 'DEA, decrypt' with key 'K3', 'DEA, encrypt' with key 'K2', and 'DEA, decrypt' with key 'K1'. The output is 'O, 64 bits' (output block), which enters another 'swapping' block controlled by 'DATATYPE'. The final output is 'P, 64 bits' (plaintext) in the 'OUT FIFO'.
graph TD
    C[IN FIFO ciphertext C] -- "C, 64 bits" --> S1[swapping]
    S1 -- "I, 64 bits" --> D1[DEA, decrypt]
    K3[K3 64] --> D1
    D1 --> D2[DEA, encrypt]
    K2[K2 64] --> D2
    D2 --> D3[DEA, decrypt]
    K1[K1 64] --> D3
    D3 -- "O, 64 bits" --> S2[swapping]
    S2 -- "P, 64 bits" --> P[OUT FIFO plaintext P]
    DATATYPE1[DATATYPE] --> S1
    DATATYPE2[DATATYPE] --> S2
  
Diagram of DES/TDES-ECB mode decryption process. The flow starts with 'IN FIFO ciphertext C' (64 bits) entering a 'swapping' block controlled by 'DATATYPE'. The output is 'I, 64 bits' (input block). This block is processed through three 'DEA' (Data Encryption Algorithm) stages: 'DEA, decrypt' with key 'K3', 'DEA, encrypt' with key 'K2', and 'DEA, decrypt' with key 'K1'. The output is 'O, 64 bits' (output block), which enters another 'swapping' block controlled by 'DATATYPE'. The final output is 'P, 64 bits' (plaintext) in the 'OUT FIFO'.
  1. 1. K: key; C: cipher text; I: input block; O: output block; P: plain text.

A 64-bit ciphertext block (C) is used, after bit/byte/half-word swapping, as the input block (I). The keying sequence is reversed compared to that used in the encryption process. The input block is processed through the DEA in the decrypt state using K3. The output of this process is fed back directly to the input of the DEA where the DES is performed in the encrypt state using K2. The new result is directly fed to the input of the DEA where the DES is performed in the decrypt state using K1. The resultant 64-bit output block (O), after bit/byte/half-word swapping, produces the plaintext (P).

Note: For more information on data swapping refer to Section 23.4.16: CRYP data registers and data swapping . Detailed DES/TDES encryption sequence can be found in Section 23.4.5: CRYP procedure to perform a cipher operation .

DES/TDES-CBC encryption

Figure 174 illustrates the encryption in DES and TDES Cipher Block Chaining (DES/TDES-CBC) mode. This mode is selected by programming ALGOMODE to 0x1 and ALGODIR to 0 in CRYP_CR.

Figure 174. DES/TDES-CBC mode encryption

Figure 174. DES/TDES-CBC mode encryption diagram. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block (producing Ps, 64 bits) to an XOR gate. The XOR gate also takes an initialization vector (IV) from an IV0(L/R) register. The output of the XOR gate (I, 64 bits) is processed by a DEA (encrypt) block using key K1. The output of the DEA (O, 64 bits) is then passed through another swapping block (producing C, 64 bits) to an OUT FIFO (ciphertext C). A feedback loop shows that the output O is written back into the IV register at the same time it is pushed into the OUT FIFO. The diagram also shows keys K1, K2, and K3 being used by the DEA blocks. A note indicates that AHB2 data write (before CRYP is enabled) is used to write data into the IN FIFO.
Figure 174. DES/TDES-CBC mode encryption diagram. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block (producing Ps, 64 bits) to an XOR gate. The XOR gate also takes an initialization vector (IV) from an IV0(L/R) register. The output of the XOR gate (I, 64 bits) is processed by a DEA (encrypt) block using key K1. The output of the DEA (O, 64 bits) is then passed through another swapping block (producing C, 64 bits) to an OUT FIFO (ciphertext C). A feedback loop shows that the output O is written back into the IV register at the same time it is pushed into the OUT FIFO. The diagram also shows keys K1, K2, and K3 being used by the DEA blocks. A note indicates that AHB2 data write (before CRYP is enabled) is used to write data into the IN FIFO.

K: key; C: cipher text; I: input block; O: output block; Ps: plain text before swapping (when decoding) or after swapping (when encoding); P: plain text; IV: initialization vectors.

This mode begins by dividing a plaintext message into 64-bit data blocks. In TCBC encryption, the first input block ( \( I_1 \) ), obtained after bit/byte/half-word swapping, is formed by exclusive-ORing the first plaintext data block ( \( P_1 \) ) with a 64-bit initialization vector IV ( \( I_1 = IV \oplus P_1 \) ). The input block is processed through the DEA in the encrypt state using K1. The output of this process is fed back directly to the input of the DEA, which performs the DES in the decrypt state using K2. The output of this process is fed directly to the input of the DEA, which performs the DES in the encrypt state using K3. The resultant 64-bit output block ( \( O_1 \) ) is used directly as the ciphertext ( \( C_1 \) ), that is, \( C_1 = O_1 \) .

This first ciphertext block is then exclusive-ORed with the second plaintext data block to produce the second input block, ( \( I_2 = (C_1 \oplus P_2) \) ). Note that \( I_2 \) and \( P_2 \) now refer to the second block. The second input block is processed through the TDEA to produce the second ciphertext block.

This encryption process continues to “chain” successive cipher and plaintext blocks together until the last plaintext block in the message is encrypted.

If the message does not consist of an integral number of data blocks, then the final partial data block should be encrypted in a manner specified for the application.

Note: For more information on data swapping refer to Section 23.4.16: CRYP data registers and data swapping .

Detailed DES/TDES encryption sequence can be found in Section 23.4.5: CRYP procedure to perform a cipher operation .

DES/TDES-CBC decryption

Figure 174 illustrates the decryption in DES and TDES Cipher Block Chaining (DES/TDES-ECB) mode. This mode is selected by writing ALGOMODE to 0x1 and ALGODIR to 1 in CRYP_CR.

Figure 175. DES/TDES-CBC mode decryption

Diagram of DES/TDES-CBC mode decryption process. The process starts with ciphertext C (64 bits) from the IN FIFO. This is processed through a 'swapping' block (controlled by DATATYPE) to produce input block I (64 bits). I is then processed through three DEA stages: DEA, decrypt (K3), DEA, encrypt (K2), and DEA, decrypt (K1). The output O (64 bits) is XORed with the IV (64 bits) from the IV0(L/R) register. The result is Ps (64 bits), which is then processed through another 'swapping' block (controlled by DATATYPE) to produce the final plaintext P (64 bits) in the OUT FIFO. A note indicates that I is written back into IV at the same time as P is pushed into the OUT FIFO. An AHB2 data write (before CRYP is enabled) is shown to the IV0(L/R) register. The diagram is labeled MS19022V1.
Diagram of DES/TDES-CBC mode decryption process. The process starts with ciphertext C (64 bits) from the IN FIFO. This is processed through a 'swapping' block (controlled by DATATYPE) to produce input block I (64 bits). I is then processed through three DEA stages: DEA, decrypt (K3), DEA, encrypt (K2), and DEA, decrypt (K1). The output O (64 bits) is XORed with the IV (64 bits) from the IV0(L/R) register. The result is Ps (64 bits), which is then processed through another 'swapping' block (controlled by DATATYPE) to produce the final plaintext P (64 bits) in the OUT FIFO. A note indicates that I is written back into IV at the same time as P is pushed into the OUT FIFO. An AHB2 data write (before CRYP is enabled) is shown to the IV0(L/R) register. The diagram is labeled MS19022V1.
  1. 1. K: key; C: cipher text; I: input block; O: output block; Ps: plain text before swapping (when decoding) or after swapping (when encoding); P: plain text; IV: initialization vectors.

In this mode the first ciphertext block ( \( C_1 \) ) is used directly as the input block ( \( I_1 \) ). The keying sequence is reversed compared to that used for the encrypt process. The input block is processed through the DEA in the decrypt state using K3. The output of this process is fed directly to the input of the DEA where the DES is processed in the encrypt state using K2. This resulting value is directly fed to the input of the DEA where the DES is processed in the decrypt state using K1. The resulting output block is exclusive-ORed with the IV (which must be the same as that used during encryption) to produce the first plaintext block ( \( P_1 = O_1 \oplus IV \) ).

The second ciphertext block is then used as the next input block and is processed through the TDEA. The resulting output block is exclusive-ORed with the first ciphertext block to produce the second plaintext data block ( \( P_2 = O_2 \oplus C_1 \) ). Note that \( P_2 \) and \( O_2 \) refer to the second block of data.

The DES/TDES-CBC decryption process continues in this manner until the last complete ciphertext block has been decrypted.

Ciphertext representing a partial data block must be decrypted in a manner specified for the application.

Note: For more information on data swapping refer to Section 23.4.16: CRYP data registers and data swapping .

Detailed DES/TDES encryption sequence can be found in Section 23.4.5: CRYP procedure to perform a cipher operation .

DES/TDES suspend/resume operations in ECB/CBC modes

Before interrupting the current message, the user application must respect the following steps:

  1. 1. If DMA is used, stop the DMA transfers to the IN FIFO by clearing to 0 the DIEN bit in the CRYP_DMACR register.
  2. 2. Wait until both the IN and the OUT FIFOs are empty (IFEM = 1 and OFNE = 0 in the CRYP_SR) and the BUSY bit is cleared. Alternatively, as the input FIFO can contain up to four unprocessed DES blocks, the application could decide for real-time reason to interrupt the cryptographic processing without waiting for the IN FIFO to be empty. In this case, the alternative is:
    1. a) Wait until OUT FIFO is empty (OFNE = 0).
    2. b) Read back the data loaded in the IN FIFO that have not been processed and save them in the memory until the IN FIFO is empty.
  3. 3. If DMA is used stop the DMA transfers from the OUT FIFO by clearing to 0 the DOEN bit in the CRYP_DMACR register.
  4. 4. Disable the cryptographic processor by setting the CRYPTEN bit to 0 in CRYP_CR, then save the current configuration (bits [9:2] in the CRYP_CR register). If CBC mode is selected, save the initialization vector registers, since CRYP_IVx(L/R)R registers have changed from initial values during the data processing.

Note: Key registers do not need to be saved as the original key value is known by the application.

  1. 5. If DMA is used, save the DMA controller status (such as the pointers to IN and OUT data transfers, number of remaining bytes).

To resume message processing, the user application must respect the following sequence:

  1. 1. If DMA is used, reconfigure the DMA controller to complete the rest of the FIFO IN and FIFO OUT transfers.
  2. 2. Make sure the cryptographic processor is disabled by reading the CRYPTEN bit in CRYP_CR (it must be 0).
  3. 3. Configure again the cryptographic processor with the initial setting in CRYP_CR, as well as the key registers using the saved configuration.
  4. 4. If the CBC mode is selected, restore CRYP_IVx(L/R)R registers using the saved configuration.
  5. 5. Optionally, write the data that were saved during context saving into the IN FIFO.
  6. 6. Enable the cryptographic processor by setting the CRYPTEN bit to 1.
  7. 7. If DMA is used, enable again DMA requests for the cryptographic processor, by setting to 1 the DIEN and DOEN bits in the CRYP_DMACR register.

23.4.11 CRYP AES basic chaining modes (ECB, CBC)

Overview

FIPS PUB 197 (November 26, 2001) provides a thorough explanation of the processing involved in the four basic operation modes supplied by the AES computing core: AES-ECB encryption, AES-ECB decryption, AES-CBC encryption and AES-CBC decryption. This section only gives a brief explanation of each mode.

AES ECB encryption

Figure 176 illustrates the AES Electronic codebook (AES-ECB) mode encryption. This mode is selected by writing ALGOMODE to 0x4 and ALGODIR to 0 in CRYP_CR.

Figure 176. AES-ECB mode encryption

Figure 176. AES-ECB mode encryption diagram. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block, then through an AEA (encrypt) block using a key (K0...3(1)), then through another swapping block, and finally to an OUT FIFO (ciphertext C). The data is 128 bits wide throughout the process. The swapping blocks are controlled by DATATYPE. The key size can be 128, 192, or 256 bits.
graph TD
    IN_FIFO["IN FIFO
plaintext P"] -- "P, 128 bits" --> SWAP1["swapping"] SWAP1 -- "I, 128 bits" --> AEA["AEA, encrypt"] KEY["K0...3(1)
128/192 or 256"] --> AEA AEA --> SWAP2["swapping"] SWAP2 -- "C, 128 bits" --> OUT_FIFO["OUT FIFO
ciphertext C"] DATATYPE1["DATATYPE"] --> SWAP1 DATATYPE2["DATATYPE"] --> SWAP2
Figure 176. AES-ECB mode encryption diagram. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block, then through an AEA (encrypt) block using a key (K0...3(1)), then through another swapping block, and finally to an OUT FIFO (ciphertext C). The data is 128 bits wide throughout the process. The swapping blocks are controlled by DATATYPE. The key size can be 128, 192, or 256 bits.
  1. 1. K: key; C: cipher text; I: input block; O: output block; P: plain text.
  2. 2. If Key size = 128: Key = [K3 K2].
    If Key size = 192: Key = [K3 K2 K1].
    If Key size = 256: Key = [K3 K2 K1 K0].

In this mode a 128- bit plaintext data block (P) is used after bit/byte/half-word swapping as the input block (I). The input block is processed through the AEA in the encrypt state using the 128, 192 or 256-bit key. The resultant 128-bit output block (O) is used after bit/byte/half-word swapping as ciphertext (C). It is then pushed into the OUT FIFO.

For more information on data swapping refer to Section 23.4.16: CRYP data registers and data swapping .

AES ECB decryption

Figure 177 illustrates the AES Electronic codebook (AES-ECB) mode decryption. This mode is selected by programming ALGOMODE to 0x4 and ALGODIR to 1 in CRYP_CR.

Figure 177. AES-ECB mode decryption

Block diagram of AES-ECB mode decryption. The process starts with 'IN FIFO ciphertext C' (128 bits) entering a 'swapping' block. A 'DATATYPE' control is applied. The output is 'I, 128 bits' (input block). This block 'I' and a key 'K 0...3(1)' (128/192 or 256 bits) are inputs to the 'AEA, decrypt' block. The output is 'O, 128 bits' (output block). This block 'O' enters another 'swapping' block with 'DATATYPE' control, resulting in 'P, 128 bits' (plaintext) which is then sent to 'OUT FIFO plaintext P'. A reference code 'MS19023V1' is in the bottom right.
Block diagram of AES-ECB mode decryption. The process starts with 'IN FIFO ciphertext C' (128 bits) entering a 'swapping' block. A 'DATATYPE' control is applied. The output is 'I, 128 bits' (input block). This block 'I' and a key 'K 0...3(1)' (128/192 or 256 bits) are inputs to the 'AEA, decrypt' block. The output is 'O, 128 bits' (output block). This block 'O' enters another 'swapping' block with 'DATATYPE' control, resulting in 'P, 128 bits' (plaintext) which is then sent to 'OUT FIFO plaintext P'. A reference code 'MS19023V1' is in the bottom right.
  1. 1. K: key; C: cipher text; I: input block; O: output block; P: plain text.
  2. 2. If Key size = 128 => Key = [K3 K2]
    If Key size = 192 => Key = [K3 K2 K1]
    If Key size = 256 => Key = [K3 K2 K1 K0].

To perform an AES decryption in ECB mode, the secret key has to be prepared (it is necessary to execute the complete key schedule for encryption) by collecting the last round key, and using it as the first round key for the decryption of the ciphertext. This preparation phase is computed by the AES core. Refer to Section 23.4.7: Preparing the CRYP AES key for decryption for more details on how to prepare the key.

When the key preparation is complete, the decryption proceed as follows: a 128-bit ciphertext block (C) is used after bit/byte/half-word swapping as the input block (I). The keying sequence is reversed compared to that of the encryption process. The resultant 128-bit output block (O), after bit/byte or half-word swapping, produces the plaintext (P). The AES-CBC decryption process continues in this manner until the last complete ciphertext block has been decrypted.

For more information on data swapping refer to Section 23.4.16: CRYP data registers and data swapping .

AES CBC encryption

Figure 178 illustrates the AES Cipher block chaining (AES-CBC) mode encryption. This mode is selected by writing ALGOMODE to 0x5 and ALGODIR to 0 in CRYP_CR.

Figure 178. AES-CBC mode encryption

Block diagram of AES-CBC mode encryption. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block (producing Ps, 128 bits), an XOR block (producing I, 128 bits), an AEA (encrypt) block (producing O, 128 bits), and another swapping block (producing C, 128 bits) into an OUT FIFO (ciphertext C). The XOR block also takes an Initialization Vector (IV) as input. The IV is updated with the output O. The AEA block takes a key (K0...3) as input. The diagram includes labels for data types (DATATYPE), AHB2 data write, and a note about writing O back into IV.

The diagram illustrates the data flow in AES-CBC encryption. Plaintext P (128 bits) is read from the IN FIFO and passes through a 'swapping' block. The output of this block is Ps (128 bits). Ps is then XORed with the Initialization Vector (IV) to produce the input block I (128 bits). I is processed by the AEA (encrypt) block using a key K0...3 (128, 192, or 256 bits) to produce the output block O (128 bits). O is written back into the IV and also passes through another 'swapping' block to produce the ciphertext C (128 bits), which is then written to the OUT FIFO. The IV is initialized to [IV1 IV0] (2). The AHB2 data write (before CRYP is enabled) is used to write data into the IN FIFO. The DATATYPE parameter controls the data width (128, 192, or 256 bits).

Block diagram of AES-CBC mode encryption. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block (producing Ps, 128 bits), an XOR block (producing I, 128 bits), an AEA (encrypt) block (producing O, 128 bits), and another swapping block (producing C, 128 bits) into an OUT FIFO (ciphertext C). The XOR block also takes an Initialization Vector (IV) as input. The IV is updated with the output O. The AEA block takes a key (K0...3) as input. The diagram includes labels for data types (DATATYPE), AHB2 data write, and a note about writing O back into IV.
  1. 1. K: key; C: cipher text; I: input block; O: output block; Ps: plain text before swapping (when decoding) or after swapping (when encoding); P: plain text; IV: Initialization vectors.
  2. 2. \( IVx = [IVxR\ IVxL] \) , R = right, L = left.
  3. 3. If Key size = 128 => Key = [K3 K2].
    If Key size = 192 => Key = [K3 K2 K1]
    If Key size = 256 => Key = [K3 K2 K1 K0].

In this mode the first input block ( \( I_1 \) ) obtained after bit/byte/half-word swapping is formed by exclusive-ORing the first plaintext data block ( \( P_1 \) ) with a 128-bit initialization vector IV ( \( I_1 = IV \oplus P_1 \) ). The input block is processed through the AEA in the encrypt state using the 128-, 192- or 256-bit key ( \( K_0 \dots K_3 \) ). The resultant 128-bit output block ( \( O_1 \) ) is used directly as ciphertext ( \( C_1 \) ), that is, \( C_1 = O_1 \) . This first ciphertext block is then exclusive-ORed with the second plaintext data block to produce the second input block, \( (I_2) = (C_1 \oplus P_2) \) . Note that \( I_2 \) and \( P_2 \) now refer to the second block. The second input block is processed through the AEA to produce the second ciphertext block. This encryption process continues to “chain” successive cipher and plaintext blocks together until the last plaintext block in the message is encrypted.

If the message does not consist of an integral number of data blocks, then the final partial data block should be encrypted in a manner specified for the application, as explained in Section 23.4.8: CRYP stealing and data padding .

For more information on data swapping, refer to Section 23.4.16: CRYP data registers and data swapping .

AES CBC decryption

Figure 179 illustrates the AES Cipher block chaining (AES-CBC) mode decryption. This mode is selected by writing ALGOMODE to 0x5 and ALGODIR to 1 in CRYP_CR.

Figure 179. AES-CBC mode decryption

Block diagram of AES-CBC mode decryption. The process starts with ciphertext C (128 bits) from an IN FIFO. This is processed through a 'swapping' block (controlled by DATATYPE) to produce an input block I (128 bits). I is then processed by the 'AEA, decrypt' block using a key K (128, 192, or 256 bits) to produce an output block O (128 bits). O is XORed with the initialization vector IV (128 bits) to produce the plaintext before swapping, Ps (128 bits). Ps is then processed through another 'swapping' block (controlled by DATATYPE) to produce the final plaintext P (128 bits), which is pushed into an OUT FIFO. A dashed box indicates that I is written back into IV at the same time P is pushed into the OUT FIFO. An AHB2 data write (before CRYP is enabled) is used to initialize the IV = [IV1 IV0] (128 bits).
Block diagram of AES-CBC mode decryption. The process starts with ciphertext C (128 bits) from an IN FIFO. This is processed through a 'swapping' block (controlled by DATATYPE) to produce an input block I (128 bits). I is then processed by the 'AEA, decrypt' block using a key K (128, 192, or 256 bits) to produce an output block O (128 bits). O is XORed with the initialization vector IV (128 bits) to produce the plaintext before swapping, Ps (128 bits). Ps is then processed through another 'swapping' block (controlled by DATATYPE) to produce the final plaintext P (128 bits), which is pushed into an OUT FIFO. A dashed box indicates that I is written back into IV at the same time P is pushed into the OUT FIFO. An AHB2 data write (before CRYP is enabled) is used to initialize the IV = [IV1 IV0] (128 bits).

MS19024V1

  1. 1. K: key; C: cipher text; I: input block; O: output block; Ps: plain text before swapping (when decoding) or after swapping (when encoding); P: plain text; IV: Initialization vectors.
  2. 2. \( IV_x = [IV_{xR} \ IV_{xL}] \) , R = right, L = left.
  3. 3. If Key size = 128 => Key = [K3 K2].
    If Key size = 192 => Key = [K3 K2 K1].
    If Key size = 256 => Key = [K3 K2 K1 K0].

In CBC mode, like in ECB mode, the secret key must be prepared to perform an AES decryption. Refer to Section 23.4.7: Preparing the CRYP AES key for decryption for more details on how to prepare the key.

When the key preparation process is complete, the decryption proceeds as follows: the first 128-bit ciphertext block ( \( C_1 \) ) is used directly as the input block ( \( I_1 \) ). The input block is processed through the AEA in the decrypt state using the 128-, 192- or 256-bit key. The resulting output block is exclusive-ORed with the 128-bit initialization vector IV (which must be the same as that used during encryption) to produce the first plaintext block ( \( P_1 = O_1 \oplus IV \) ).

The second ciphertext block is then used as the next input block and is processed through the AEA. The resulting output block is exclusive-ORed with the first ciphertext block to produce the second plaintext data block ( \( P_2 = O_2 \oplus C_1 \) ). Note that \( P_2 \) and \( O_2 \) refer to the second block of data. The AES-CBC decryption process continues in this manner until the last complete ciphertext block has been decrypted.

Ciphertext representing a partial data block must be decrypted in a manner specified for the application, as explained in Section 23.4.8: CRYP stealing and data padding .

For more information on data swapping, refer to Section 23.4.16: CRYP data registers and data swapping .

AES suspend/resume operations in ECB/CBC modes

Before interrupting the current message, the user application must respect the following sequence:

  1. 1. If DMA is used, stop the DMA transfers to the IN FIFO by clearing to 0 the DIEN bit in the CRYP_DMACR register.
  2. 2. Wait until both the IN and the OUT FIFOs are empty (IFEM = 1 and OFNE = 0 in the CRYP_SR) and the BUSY bit is cleared.
  3. 3. If DMA is used, stop the DMA transfers from the OUT FIFO by clearing to 0 the DOEN bit in the CRYP_DMACR register.
  4. 4. Disable the CRYP by setting the CRYPTEN bit to 0 in CRYP_CR, then save the current configuration (bits [9:2] in the CRYP_CR register). If ECB mode is not selected, save the initialization vector registers, because CRYP_IVx(L/R)R registers have changed from initial values during the data processing.

Note: Key registers do not need to be saved as the original key value is known by the application.

  1. 5. If DMA is used, save the DMA controller status (such as pointers to IN and OUT data transfers, number of remaining bytes).

To resume message processing, the user application must respect the following sequence:

  1. 1. If DMA is used, reconfigure the DMA controller to complete the rest of the FIFO IN and FIFO OUT transfers.
  2. 2. Make sure the cryptographic processor is disabled by reading the CRYPTEN bit in CRYP_CR (it must be set to 0).
  3. 3. Configure the cryptographic processor again with the initial setting in CRYP_CR, as well as the key registers using the saved configuration.
  4. 4. For AES-ECB or AES-CBC decryption, the key must be prepared again, as described in Section 23.4.7: Preparing the CRYP AES key for decryption .
  5. 5. If ECB mode is not selected, restore CRYP_IVx(L/R)R registers using the saved configuration.
  6. 6. Enable the cryptographic processor by setting the CRYPTEN bit to 1.
  7. 7. If DMA is used, enable again the DMA requests from the cryptographic processor, by setting DIEN and DOEN bits to 1 in the CRYP_DMACR register.

23.4.12 CRYP AES counter mode (AES-CTR)

Overview

The AES counter mode (CTR) uses the AES block as a key stream generator. The generated keys are then XORed with the plaintext to obtain the ciphertext.

CTR chaining is defined in NIST Special Publication 800-38A, Recommendation for Block Cipher Modes of Operation . A typical message construction in CTR mode is given in Figure 180 .

Figure 180. Message construction for the Counter mode

Diagram illustrating the message construction for the Counter mode (CTR).

The diagram shows the structure of a message in CTR mode. At the top, a horizontal bar represents the message components: an Initial Counter Block (ICB), Encrypted Ciphertext (C), and Padding. Above this bar, vertical lines indicate '16-Bytes boundaries'. Below the ICB, a callout box details its internal structure with '4-Bytes boundaries': Nonce, Initialization vector (IV), and Counter (0x1). A 'decrypt' arrow points from the Encrypted Ciphertext (C) to a Plaintext (P) block below. The diagram is labeled 'CTR mode' in the top left and 'MSV43716V1' in the bottom right.

Diagram illustrating the message construction for the Counter mode (CTR).

The structure of this message is as below:

AES CTR processing

Figure 181 (respectively Figure 182) describes the AES-CTR encryption (respectively decryption) process implemented within this peripheral. This mode is selected by programming ALGOMODE bitfield to 0x6 in CRYP_CR.

Figure 181. AES-CTR mode encryption

Block diagram of AES-CTR mode encryption process. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block, then to an AEA (encrypt) block which also takes a key (K0...3) and an initialization vector (IV0...1(L/R)). The output of the AEA block is XORed with the output of the swapping block to produce ciphertext C, which is then pushed to an OUT FIFO. The IV is incremented by 1 and written back into the IV0...1(L/R) block. The DATATYPE control signal is used to configure the swapping blocks. A dashed box indicates that (I + 1) is written back into IV at the same time that C is pushed into the OUT FIFO.

The diagram illustrates the AES-CTR mode encryption process. Data flows from the IN FIFO plaintext P (128 bits) through a swapping block (controlled by DATATYPE ) to produce Ps, 128 bits . Simultaneously, an IV0...1(L/R) block receives an AHB2 data write (before CRYP is enabled) and an incremented value from a +1 block. This block outputs I, 128 bits to the AEA, encrypt block. The AEA, encrypt block also takes a key K0...3 (128, 192, or 256 bits) and produces an output O, 128 bits . The output O is XORed with Ps to produce Cs, 128 bit . This value is then processed by another swapping block (controlled by DATATYPE ) to produce the final C, 128 bits ciphertext, which is pushed to the OUT FIFO ciphertext C . A dashed box indicates that (I + 1) is written back into IV at the same time that C is pushed into the OUT FIFO .

Block diagram of AES-CTR mode encryption process. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block, then to an AEA (encrypt) block which also takes a key (K0...3) and an initialization vector (IV0...1(L/R)). The output of the AEA block is XORed with the output of the swapping block to produce ciphertext C, which is then pushed to an OUT FIFO. The IV is incremented by 1 and written back into the IV0...1(L/R) block. The DATATYPE control signal is used to configure the swapping blocks. A dashed box indicates that (I + 1) is written back into IV at the same time that C is pushed into the OUT FIFO.
  1. 1. K: key; C: cipher text; I: input Block; o: output block; Ps: plain text before swapping (when decoding) or after swapping (when encoding); Cs: cipher text after swapping (when decoding) or before swapping (when encoding); P: plain text; IV: Initialization vectors.

Figure 182. AES-CTR mode decryption

Block diagram of AES-CTR mode decryption. The diagram shows the flow of data from an IN FIFO (ciphertext P) through a swapping block (producing Cs, 128 bits) to an XOR gate. The XOR gate also receives an output O (128 bits) from an AEA, encrypt block. The AEA block takes an input block I (128 bits) from an IV0...1(L/R) register and a key K0...3 (128, 192, or 256 bits). The IV0...1(L/R) register is initialized by an AHB2 data write and is incremented by +1. The output of the XOR gate is Ps (128 bits), which is then swapped to produce the final plaintext C, which is pushed into an OUT FIFO. A dashed box indicates that (I + 1) is written back into IV at the same time that P is pushed into the OUT FIFO. The diagram is labeled MS19025V1.
Block diagram of AES-CTR mode decryption. The diagram shows the flow of data from an IN FIFO (ciphertext P) through a swapping block (producing Cs, 128 bits) to an XOR gate. The XOR gate also receives an output O (128 bits) from an AEA, encrypt block. The AEA block takes an input block I (128 bits) from an IV0...1(L/R) register and a key K0...3 (128, 192, or 256 bits). The IV0...1(L/R) register is initialized by an AHB2 data write and is incremented by +1. The output of the XOR gate is Ps (128 bits), which is then swapped to produce the final plaintext C, which is pushed into an OUT FIFO. A dashed box indicates that (I + 1) is written back into IV at the same time that P is pushed into the OUT FIFO. The diagram is labeled MS19025V1.
  1. 1. K: key; C: cipher text; I: input Block; o: output block; Ps: plain text before swapping (when decoding) or after swapping (when encoding); Cs: cipher text after swapping (when decoding) or before swapping (when encoding); P: plain text; IV: Initialization vectors.

In CTR mode, the output block is XORed with the subsequent input block before it is input to the algorithm. Initialization vectors in the peripheral must be initialized as shown on Table 155 .

Table 155. Counter mode initialization vector

CRYP_IV1RR[31:0]CRYP_IV1LR[31:0]CRYP_IV0RR[31:0]CRYP_IV0LR[31:0]
IVI[127:96]IVI[95:64]IVI[63:32]IVI[31:0]
32-bit counter = 0x1

Unlike in CBC mode, which uses the CRYP_IVx(L/R)R registers only once when processing the first data block, in CTR mode IV registers are used for processing each data block, and the peripheral increments the least significant 32 bits (leaving the other most significant 96 bits unchanged).

CTR decryption does not differ from CTR encryption, since the core always encrypts the current counter block to produce the key stream that will be XORed with the plaintext or cipher as input. Thus when ALGOMODE is set to 0x6, ALGODIR is don't care.

Note: In this mode the key must NOT be prepared for decryption.

The following sequence must be used to perform an encryption or a decryption in CTR chaining mode:

  1. 1. Make sure the cryptographic processor is disabled by clearing the CRYPTEN bit in the CRYP_CR register.
  2. 2. Configure CRYP_CR as follows:
    1. a) Program ALGOMODE bits to 0x6 to select CTR mode. ALGODIR can be set to any value.
    2. b) Configure the data type (1, 8, 16 or 32 bits) through the DATATYPE bits.
    3. c) Define the key length using KEYSIZE bits.
  3. 3. Initialize the key registers (128, 192 or 256 bits) in CRYP_KEYRx as well as the initialization vector (IV) as described in Table 155 .
  4. 4. Flush the IN and OUT FIFOs by writing the FFLUSH bit to 1 in the CRYP_CR register.
  5. 5. If it is the last block, optionally pad the data with zeros to have a complete block.
  6. 6. Append data in the cryptographic processor and read the result. The three possible scenarios are described in Section 23.4.5: CRYP procedure to perform a cipher operation .
  7. 7. Repeat the previous step until the second last block is processed. For the last block, execute the two previous steps. For this last block, the driver must discard the data that is not part of the data when the last block size is less than 16 bytes.

Suspend/resume operations in CTR mode

Like for the CBC mode, it is possible to interrupt a message to send a higher priority message, and resume the message which was interrupted. Detailed CBC sequence can be found in Section 23.4.11: CRYP AES basic chaining modes (ECB, CBC) .

Note: Like for CBC mode, IV registers must be reloaded during the resume operation.

23.4.13 CRYP AES Galois/counter mode (GCM)

Overview

The AES Galois/counter mode (GCM) allows encrypting and authenticating the plaintext, and generating the correspondent ciphertext and tag (also known as message authentication code). To ensure confidentiality, GCM algorithm is based on AES counter mode. It uses a multiplier over a fixed finite field to generate the tag.

GCM chaining is defined in NIST Special Publication 800-38D, Recommendation for Block Cipher Modes of Operation - Galois/Counter Mode (GCM) and GMAC . A typical message construction in GCM mode is given in Figure 183 .

Figure 183. Message construction for the Galois/counter mode

Diagram illustrating the message construction for the Galois/counter mode (GCM).

The diagram shows the structure of a GCM message. It starts with a 16-byte Initial Counter Block (ICB), which is further divided into a 96-bit Initialization vector (IV) and a 32-bit Counter. This is followed by Additional authenticated data (AAD) of length \( Len(A) \) . The plaintext (P) of length \( Len(P) = Len(C) \) is then shown. The last block of the plaintext is followed by zero padding. The ciphertext (C) is shown as the encrypted version of the plaintext. The authentication tag (T) is generated from the ICB, AAD, and ciphertext. The diagram also indicates 16-byte and 4-byte boundaries, and the use of zero padding for non-multiple lengths.

Diagram illustrating the message construction for the Galois/counter mode (GCM).

The structure of this message is defined as below:

Note: GCM standard specifies that ciphertext C has same bit length as the plaintext P.

Table 156. GCM last block definition

EndiannessBit[0]Bit[32]Bit[64]Bit[96]
Input data0x0Header length[31:0]0x0Payload length[31:0]

AES GCM processing

This mode is selected by writing ALGOMODE bitfield to 0x8 in CRYP_CR.

The mechanism for the confidentiality of the plaintext in GCM mode is a variation of the Counter mode, with a particular 32-bit incrementing function that generates the necessary sequence of counter blocks.

CRYP_IVx(L/R)R registers are used for processing each data block. The cryptographic processor automatically increments the 32 least significant bits of the counter block. The first counter block (CB1) written by the application is equal to the Initial Counter Block incremented by one (see Table 157 ).

Table 157. GCM mode IV registers initialization

RegisterCRYP_IV0LR[31:0]CRYP_IV0RR[31:0]CRYP_IV1LR[31:0]CRYP_IV1RR[31:0]
Input dataICB[127:96]ICB[95:64]ICB[63:32]ICB[31:0]
32-bit counter = 0x2

Note: In this mode the key must NOT be prepared for decryption.

The authentication mechanism in GCM mode is based on a hash function, called GF2mul , that performs multiplication by a fixed parameter, called the hash subkey (H), within a binary Galois field.

To process a GCM message, the driver must go through four phases, which are described in the following subsections.

1. GCM initialization phase

During this first step, the GCM hash subkey (H) is calculated and saved internally to be used for processing all the blocks. It is recommended to follow the sequence below:

  1. a) Make sure the cryptographic processor is disabled by clearing the CRYPTEN bit in the CRYP_CR register.
  2. b) Select the GCM chaining mode by programming ALGOMODE bits to 0x8 in CRYP_CR. In the same register define the key length using KEYSIZE bits and the data type using DATATYPE bits.
  3. c) Configure GCM_CCMPH bits to 0b00 in CRYP_CR to indicate that the initialization phase is ongoing.
  4. d) Initialize the key registers (128, 192 or 256 bits) in CRYP_KEYRx as well as the initialization vector (IV) as defined in Table 157 .
  5. e) Set CRYPTEN bit to 1 to start the calculation of the hash key.
  6. f) Wait for the CRYPTEN bit to be cleared to 0 by the cryptographic processor, before moving on to the next phase.

2. GCM header phase

The below sequence shall be performed after the GCM initialization phase. It must be complete before jumping to the payload phase. The sequence is identical for encryption and decryption.

  1. g) Set the GCM_CCMPH bits to 0b01 in CRYP_CR to indicate that the header phase is ongoing.
  2. h) Set the CRYPTEN bit to 1 to start accepting data.
  3. i) If it is the last block of additional authenticated data, optionally pad the data with zeros to have a complete block.
  4. j) Append additional authenticated data in the cryptographic processor. The three possible scenarios are described in Section 23.4.5: CRYP procedure to perform a cipher operation .
  5. k) Repeat the previous step until the second last additional authenticated data block is processed. For the last block, execute the two previous steps. Once all the additional authenticated data have been supplied, wait until the BUSY flag is cleared before moving on to the next phase.

Note: This phase can be skipped if there is no additional authenticated data, i.e. Len(A) = 0. In header and payload phases, CRYPTEN bit is not automatically cleared by the cryptographic processor.

3. GCM payload phase (encryption or decryption)

When the payload size is not null, this sequence must be executed after the GCM header phase. During this phase, the encrypted/decrypted payload is stored in the CRYP_DOUT register.

Note: This phase can be skipped if there is no payload data, i.e. Len(C) = 0 (see GMAC mode).

4. GCM final phase

In this last step, the cryptographic processor generates the GCM authentication tag and stores it in CRYP_DOUT register.

Note: In this final phase data have to be swapped according to the DATATYPE programmed in CRYP_CR register.

Suspend/resume operations in GCM mode

Before interrupting the current message in header or payload phase, the user application must respect the following sequence:

  1. 1. If DMA is used, stop DMA transfers to the IN FIFO by clearing to 0 the DIEN bit in the CRYP_DMACR register.
  2. 2. Wait until both the IN and the OUT FIFOs are empty (IFEM = 1 and OFNE = 0 in the CRYP_SR register) and the BUSY bit is cleared.
  3. 3. If DMA is used, stop DMA transfers from the OUT FIFO by clearing to 0 the DOEN bit in the CRYP_DMACR register.
  4. 4. Disable the cryptographic processor by setting the CRYPTEN bit to 0 in CRYP_CR, then save the current configuration (bits [9:2], bits [17:16] and bits 19 of the CRYP_CR register). In addition, save the initialization vector registers, since CRYP_IVx(L/R)R registers have changed from their initial values during data processing.

Note: Key registers do not need to be saved as original their key value is known by the application.

  1. 5. Save context swap registers: CRYP_CSGCMCCM0..7R and CRYP_CSGCM0..7R
  2. 6. If DMA is used, save the DMA controller status (pointers to IN and OUT data transfers, number of remaining bytes, etc.).

To resume message processing, the user must respect the following sequence:

  1. 1. If DMA is used, reconfigure the DMA controller to complete the rest of the FIFO IN and FIFO OUT transfers.
  2. 2. Make sure the cryptographic processor is disabled by reading the CRYPTEN bit in CRYP_CR (it must be 0).
  3. 3. Configure again the cryptographic processor with the initial setting in CRYP_CR, as well as the key registers using the saved configuration.
  4. 4. Restore context swap registers: CRYP_CSGCMCCM0..7R and CRYP_CSGCM0..7R
  5. 5. Restore CRYP_IVx(L/R)R registers using the saved configuration.
  6. 6. Enable the cryptographic processor by setting the CRYPTEN bit to 1.
  7. 7. If DMA is used, enable again cryptographic processor DMA requests by setting to 1 the DIEN and DOEN bits in the CRYP_DMACR register.

Note: In Header phase, DMA OUT FIFO transfer is not used.

23.4.14 CRYP AES Galois message authentication code (GMAC)

Overview

The Galois message authentication code (GMAC) allows authenticating a plaintext and generating the corresponding tag information (also known as message authentication code). It is based on GCM algorithm, as defined in NIST Special Publication 800-38D, Recommendation for Block Cipher Modes of Operation - Galois/Counter Mode (GCM) and GMAC .

A typical message construction in GMAC mode is given in Figure 184 .

Figure 184. Message construction for the Galois Message Authentication Code mode

Diagram illustrating the message construction for the Galois Message Authentication Code (GMAC) mode. The diagram shows a sequence of blocks: ICB (Initialization Counter Block), Authenticated data, and Last block. The ICB is split into an Initialization vector (IV) and a Counter. The Authenticated data is split into blocks of 16-byte boundaries. The Last block is split into a 0 (zero padding) and a Last block. Above the Last block, there is a callout showing two 64-bit values: [Len(A)]64 and [0]64. An arrow labeled 'auth.' points from the Authenticated data to an Authentication tag (T). A legend indicates that grey shading represents zero padding. The diagram is labeled MSv42158V2.
Diagram illustrating the message construction for the Galois Message Authentication Code (GMAC) mode. The diagram shows a sequence of blocks: ICB (Initialization Counter Block), Authenticated data, and Last block. The ICB is split into an Initialization vector (IV) and a Counter. The Authenticated data is split into blocks of 16-byte boundaries. The Last block is split into a 0 (zero padding) and a Last block. Above the Last block, there is a callout showing two 64-bit values: [Len(A)]64 and [0]64. An arrow labeled 'auth.' points from the Authenticated data to an Authentication tag (T). A legend indicates that grey shading represents zero padding. The diagram is labeled MSv42158V2.

AES GMAC processing

This mode is selected by writing ALGOMODE bitfield to 0x8 in CRYP_CR.

GMAC algorithm corresponds to the GCM algorithm applied on a message composed only of an header. As a consequence, all steps and settings are the same as in GCM mode, except that the payload phase (3) is not used.

Suspend/resume operations in GMAC

GMAC is exactly the same as GCM algorithm except that only header phase (2) can be interrupted.

23.4.15 CRYP AES Counter with CBC-MAC (CCM)

Overview

The AES Counter with Cipher Block Chaining-Message Authentication Code (CCM) algorithm allows encrypting and authenticating the plaintext, and generating the correspondent ciphertext and tag (also known as message authentication code). To ensure confidentiality, CCM algorithm is based on AES counter mode. It uses Cipher Block Chaining technique to generate the message authentication code. This is commonly called CBC-MAC

Note: NIST does not approve this CBC-MAC as an authentication mode outside of the context of the CCM specification.

CCM chaining is specified in NIST Special Publication 800-38C, Recommendation for Block Cipher Modes of Operation - The CCM Mode for Authentication and Confidentiality . A typical message construction in CCM mode is given in Figure 185

Figure 185. Message construction for the Counter with CBC-MAC mode

Diagram illustrating the message construction for the Counter with CBC-MAC mode. The diagram shows the structure of the message blocks and the flow of data through the cryptographic processor. The message is composed of several fields: B0 (16-byte first authentication block), Associated data (A), Plaintext (P), and Enc (T) ( ciphertext and tag). The B0 block is further detailed as containing flags, a Nonce (N), and a Q field. The diagram also shows the flow of data through the MAC (T) and Decrypt and compare blocks, and the resulting Authenticated & encrypted ciphertext (C).

The diagram illustrates the message construction for the Counter with CBC-MAC mode. It shows the structure of the message blocks and the flow of data through the cryptographic processor.

The message is composed of several fields:

The B0 block is further detailed as containing:

The diagram also shows the flow of data through the MAC (T) and Decrypt and compare blocks, and the resulting Authenticated & encrypted ciphertext (C).

Legend: Zero padding (indicated by a grey square).

MSV42159V2

Diagram illustrating the message construction for the Counter with CBC-MAC mode. The diagram shows the structure of the message blocks and the flow of data through the cryptographic processor. The message is composed of several fields: B0 (16-byte first authentication block), Associated data (A), Plaintext (P), and Enc (T) ( ciphertext and tag). The B0 block is further detailed as containing flags, a Nonce (N), and a Q field. The diagram also shows the flow of data through the MAC (T) and Decrypt and compare blocks, and the resulting Authenticated & encrypted ciphertext (C).

The structure of this message is as below:

Note: The cryptographic peripheral can only manage padded plaintext/ciphertext messages of length \( Plen < 2^{36} + 1 \) bytes.

This part of the message is only authenticated, not encrypted. This section has a known length, ALen , that can be a non-multiple of 16 bytes (see Figure 185 ). The standard also states that, on the MSB bits of the first message block (B1), the associated data length expressed in bytes ( a ) must be encoded as defined below:

Note: CCM chaining mode can also be used with associated data only (i.e. no payload).

As an example, the C.1 section in NIST Special Publication 800-38C gives the following:

N: 10111213 141516 (Nlen = 56 bits or 0x7 bytes)
A: 00010203 04050607 (ALen = 64 bits or 0x8 bytes)
P: 20212223 (PLen = 32 bits i.e. Q = 0x4 bytes)
T: 6084341b (TLen = 32 bits or t = 4)
B0: 4f101112 13141516 00000000 00000004
B1: 00080001 02030405 06070000 00000000
B2: 20212223 00000000 00000000 00000000
CTR0: 0710111213 141516 00000000 00000000
CTR1: 0710111213 141516 00000000 00000001

The usage of control blocks CTRx is explained in the following section. The generation of CTR0 from the first block (B0) must be managed by software.

AES CCM processing

This mode is selected by writing ALGOMODE bitfield to 0x9 in CRYP_CR.

The data input to the generation-encryption process are a valid nonce, a valid payload string, and a valid associated data string, all properly formatted. The CBC chaining mechanism is applied to the formatted data to generate a MAC, whose length is known. Counter mode encryption, which requires a sufficiently long sequence of counter blocks as input, is applied to the payload string and separately to the MAC. The resulting data, called the ciphertext C, is the output of the generation-encryption process on plaintext P.

CRYP_IVx(L/R)R registers are used for processing each data block. The cryptographic processor automatically increments the CTR counter with a bit length defined by the first block (B0). The first counter written by application, CTR1, is equal to B0 with the first 5 bits zeroed and the most significant bits containing P byte length also zeroed, then incremented by one (see Table 158 ).

Table 158. CCM mode IV registers initialization

RegisterCRYP_IV0LR[31:0]CRYP_IV0RR[31:0]CRYP_IV1LR[31:0]CRYP_IV1RR[31:0]
Input dataB0[127:96], where the 5 most significant bits are set to 0 (flag bits)B0[95:64]B0[63:32]B0[31:0], where Q length bits are set to 0, except for bit 0 that is set to 1

Note: In this mode, the key must NOT be prepared for decryption.

To process a CCM message, the driver must go through four phases, which are described below.

1. CCM initialization phase

In this first step, the first block (B0) of the CCM message is programmed into the CRYP_DIN register. During this phase, the CRYP_DOUT register does not contain any output data. It is recommended to follow the sequence below:

  1. Make sure that the cryptographic processor is disabled by clearing the CRYPTEN bit in the CRYP_CR register.
  2. Select the CCM chaining mode by programming the ALGOMODE bits to 0x9 in the CRYP_CR register. In the same register define the key length using KEYSIZE bits and the data type using DATATYPE bits.
  3. Configure the GCM_CCMPH bits to 0b00 in CRYP_CR to indicate that we are in the initialization phase.
  4. Initialize the key registers (128, 192 or 256 bits) in CRYP_KEYRx as well as the initialization vector (IV) with CTR1 information, as defined in Table 158 .
  5. Set the CRYPTEN bit to 1 in CRYP_CR to start accepting data.
  6. Write the B0 packet into CRYP_DIN register, then wait for the CRYPTEN bit to be cleared to 0 by the cryptographic processor before moving on to the next phase.

Note: In this initialization phase data have to be swapped according to the DATATYPE programmed in CRYP_CR register.

2. CCM header phase

The below sequence shall be performed after the CCM initialization phase. It must be complete before jumping to the payload phase. The sequence is identical for encryption and decryption. During this phase, the CRYP_DOUT register does not contain any output data.

  1. Set the GCM_CCMPH bit to 0b01 in CRYP_CR to indicate that the header phase is ongoing.
  2. Set the CRYPTEN bit to 1 to start accepting data.
  3. If it is the last block of associated data, optionally pad the data with zeros to have a complete block.
  4. Append the associated data in the cryptographic processor. The three possible scenarios are described in Section 23.4.5: CRYP procedure to perform a cipher operation .
  5. Repeat the previous step until the second last associated data block is processed. For the last block, execute the two previous steps. Once all the additional authenticated data have been supplied, wait until the BUSY flag is cleared.

Note: This phase can be skipped if there is no associated data (Alen = 0).
The first block of the associated data B1 must be formatted with the associated data length.
This task must be managed by the driver.

3. CCM payload phase (encryption or decryption)

When the payload size is not null, this sequence must be performed after the CCM header phase. During this phase, the encrypted/decrypted payload is stored in the CRYP_DOUT register.

Note: This phase can be skipped if there is no payload data, i.e. Plen = 0 or Clen = Tlen

Note: Do not forget to remove \( LSB_{Tlen}(C) \) encrypted tag information when decrypting ciphertext C.

4. CCM final phase

In this last step, the cryptographic processor generates the CCM authentication tag and stores it in the CRYP_DOUT register.

Note: In this final phase, data have to be swapped according to the DATATYPE programmed in CRYP_CR register.

Suspend/resume operations in CCM mode

Before interrupting the current message in payload phase, the user application must respect the following sequence:

  1. 1. If DMA is used, stop the DMA transfers to the IN FIFO by clearing to 0 the DIEN bit in the CRYP_DMACR register.
  2. 2. Wait until both the IN and the OUT FIFOs are empty (IFEM = 1 and OFNE = 0 in the CRYP_SR register) and the BUSY bit is cleared.
  3. 3. If DMA is used, stop the DMA transfers from the OUT FIFO by clearing to 0 the DOEN bit in the CRYP_DMACR register.
  4. 4. Disable the cryptographic processor by setting the CRYPTEN bit to 0 in CRYP_CR, then save the current configuration (bits [9:2], bits [17:16] and bits 19 in the CRYP_CR register). In addition, save the initialization vector registers, since CRYP_IVx(L/R)R registers have changed from their initial values during the data processing.

Note: Key registers do not need to be saved as their original key value is known by the application.

  1. 5. Save context swap registers: CRYP_CSGCMCCM0..7R
  2. 6. If DMA is used, save the DMA controller status (pointers for IN and OUT data transfers, number of remaining bytes, etc.).

To resume message processing, the user application must respect the following sequence:

  1. 1. If DMA is used, reconfigure the DMA controller to complete the rest of the FIFO IN and FIFO OUT transfers.
  2. 2. Make sure the cryptographic processor is disabled by reading the CRYPTEN bit in CRYP_CR (must be 0).
  3. 3. Configure the cryptographic processor again with the initial setting in CRYP_CR and key registers using the saved configuration.
  4. 4. Restore context swap registers: CRYP_CSGCMCCM0..7R
  5. 5. Restore CRYP_IVx(L/R)R registers using the saved configuration.
  6. 6. Enable the cryptographic processor by setting the CRYPTEN bit to 1.
  7. 7. If DMA is used, enable again cryptographic processor DMA requests by setting to 1 the DIEN and DOEN bits in the CRYP_DMACR register.

Note: In Header phase DMA OUT FIFO transfer is not used.

23.4.16 CRYP data registers and data swapping

The CRYP_DIN register is the 32-bit wide data input register of the peripheral. It is used to enter into the input FIFO up to four 64-bit blocks (TDES) or two 128-bit blocks (AES) of plaintext (when encrypting) or ciphertext (when decrypting), one 32-bit word at a time.

The four adjacent (respectively two) words of the AES (respectively DES/TDES) data block are organized in big-endian order, with the most significant byte of a word on the lowest address.

The cryptographic accelerator can be configured to perform a bit, byte, half-word, or no swapping on the input data word in the CRYP_DINR register, before loading it to the CRYP processing core, and on the data output from the CRYP processing core, before sending it

to the CRYP_DOUTR register. The choice depends on the type of data. For example, a byte swapping is used for an ASCII text stream.

The data swap type is selected through the DATATYPE[1:0] bitfield of the AES_CR register. The selection applies both to the input and the output of the CRYP processing core.

Note: The CRYP Key registers (CRYP_Kx(L/R)) and initialization registers (CRYP_IVx(L/R)) are not sensitive to the swap mode selected.

More information on data input and data swapping can be found in the next subsections.

DES/TDES data input and output

A 64-bit data block is entered into the cryptographic processor with two successive 32-bit word write operations to the CRYP_DINR register (DIN[31:0] bitfield), the most significant word (bits [64:33]) first, the least significant word (bits [32:1]) last.

A 64-bit data block is retrieved from the cryptographic processor with two successive 32-bit word read operations from the CRYP_DOUTR register (DOUT[31:0] bitfield), the most significant word (bits [64:33]) first, the least significant word (bits [32:1]) last.

DES/TDES data swapping feature

The cryptographic processor data swapping feature for DES/TDES is summarized in Table 159 and Figure 186 .

Table 159. DES/TDES data swapping example

DATATYPE
in CRYP_CR
Swapping
performed
Data block representation (64-bit)
System memory data (big-endian)
00No swappingBlock[64..1]: 0xABCD7720 6973FE01
Address @, word[63..32]: 0xABCD 7720
Address @+4, word[31..0]: 0x6973 FE01
01Half-word (16-bit)
swapping
Block[64..1]: 0xABCD 7720 6973 FE01
Address @, word[63..32]: 0x7720 ABCD
Address @+4, word[31..0]: 0xFE01 6973
10Byte (8-bit)
swapping
Block[64..1]: 0x AB CD 77 20 69 73 FE 01
Address @, word[63..32]: 0x2077 CDAB
Address @+4, word[31..0]: 0x01FE 7369
11Bit swapping in
32-bit word
Block[64..33]: 0xABCD7720
1010 1011 1100 1101 0111 0111 0010 0000
Block[32..1]: 0x6973FE01
0110 1001 0111 0011 1111 1110 0000 0001
Address @, word[63..32]: 0x04EEB3D5
0000 0100 1110 1110 1011 0011 1101 0101
Address @+4, word[31..0]: 0x807FCE96
1000 0000 0111 1111 1100 1110 1001 0110

Figure 186. 64-bit block construction according to the data type (IN FIFO)

Figure 186: 64-bit block construction according to the data type (IN FIFO). The diagram shows four cases for DATATYPE[1:0]: 00 (no swapping), 01 (16-bit (half-word) swapping), 10 (8-bit (byte) swapping), and 11 (bit swapping). Each case shows two 32-bit words (Word1 and Word0) being written into a 64-bit block (M64 to M1). Arrows indicate the mapping of input bits (Dx) to output bits (Mx) for each swapping mode. A legend at the bottom defines Dx as input/output data bit 'x' and the double-headed arrow as 'Data swap'. A note indicates that 1..4 is the order of write to CRYP_DINR / read from CRYP_DOUTR. The diagram is labeled MSv43720V2.

DATATYPE[1:0] = 00: no swapping

Word1 (D63..D32) and Word0 (D31..D0) are written directly to M64..M33 and M32..M1 respectively.

DATATYPE[1:0] = 01: 16-bit (half-word) swapping

Word1 (D63..D32) is split into two 16-bit halves (D63..D48 and D47..D32) which are swapped and written to M48..M33 and M64..M49. Word0 (D31..D0) is split into two 16-bit halves (D31..D16 and D15..D0) which are swapped and written to M16..M1 and M32..M17.

DATATYPE[1:0] = 10: 8-bit (byte) swapping

Word1 (D63..D32) is split into four 8-bit bytes (D63..D56, D55..D48, D47..D40, D39..D32) which are swapped and written to M40..M33, M48..M41, M56..M49, and M64..M57. Word0 (D31..D0) is split into four 8-bit bytes (D31..D24, D23..D16, D15..D8, D7..D0) which are swapped and written to M8..M1, M16..M9, M24..M17, and M32..M25.

DATATYPE[1:0] = 11: bit swapping

Word1 (D63..D32) is split into four 16-bit halves (D63..D48, D47..D32, D31..D16, D15..D0) which are swapped and written to M63..M34, M33..M2, M31..M0, and M30..M1. Word0 (D31..D0) is split into four 16-bit halves (D31..D16, D15..D0, D63..D48, D47..D32) which are swapped and written to M62..M63, M64..M1, M2..M3, and M30..M32.

Legend: Dx input/output data bit 'x'

↔ Data swap

①..④ Order of write to CRYP_DINR / read from CRYP_DOUTR

MSv43720V2

Figure 186: 64-bit block construction according to the data type (IN FIFO). The diagram shows four cases for DATATYPE[1:0]: 00 (no swapping), 01 (16-bit (half-word) swapping), 10 (8-bit (byte) swapping), and 11 (bit swapping). Each case shows two 32-bit words (Word1 and Word0) being written into a 64-bit block (M64 to M1). Arrows indicate the mapping of input bits (Dx) to output bits (Mx) for each swapping mode. A legend at the bottom defines Dx as input/output data bit 'x' and the double-headed arrow as 'Data swap'. A note indicates that 1..4 is the order of write to CRYP_DINR / read from CRYP_DOUTR. The diagram is labeled MSv43720V2.

AES data input and output

A 128-bit data block is entered into the cryptographic processor with four successive 32-bit word writes into the CRYP_DINR register (bitfield DIN[31:0]), the most significant word (bits [127:96]) first, the least significant word (bits [31:0]) last.

A 128-bit data block is retrieved from the cryptographic processor with four successive 32-bit word reads from the CRYP_DOUTR register (bitfield DOUT[31:0]), the most significant word (bits [127:96]) first, the least significant word (bits [31:0]) last.

AES data swapping feature

The cryptographic processor data swapping feature for AES is summarized in Table 160 and Figure 187 .

Table 160. AES data swapping example

DATATYPE in CRYP_CRSwapping performedFirst half data block (64-bit)
System memory data (big-endian)
00No swappingBlock[63..0]: 0x4E6F7720 69732074
Address @, word[63..32]: 0x4E6F7720
Address @+4, word[31..0]: 0x69732074
01Half-word (16-bit) swappingBlock[63..0]: 0x4E6F 7720 6973 2074
Address @, word[63..32]: 0x7720 4E6F
Address @+4, word[31..0]: 0x2074 6973
10Byte (8-bit) swappingBlock[63..0]: 0x4E 6F 77 20 69 73 20 74
Address @, word[63..32]: 0x2077 6F4E
Address @+4, word[31..0]: 0x7420 7369

Table 160. AES data swapping example (continued)

DATATYPE in CRYP_CRSwapping performedFirst half data block (64-bit)
System memory data (big-endian)
11Bit swappingBlock[63..32]: 0x4E6F7720
0100 1110 0110 1111 0111 0111 0010 0000
Block[31..0]: 0x69732074
0110 1001 0111 0011 0010 0000 0111 0100
Address @, word[63..32]: 0x04EE F672
0000 0100 1110 1110 1111 0110 0111 0010
Address @+4, word[31..0]: 0x2E04 CE96
0010 1110 0000 0100 1100 1110 1001 0110

Figure 187. 128-bit block construction according to the data type

Diagram showing 128-bit block construction for different DATATYPE settings: 00 (no swapping), 01 (16-bit swapping), 10 (8-bit byte swapping), and 11 (bit swapping). Each section shows Word 3, Word 2, Word 1, and Word 0 with bit-level details and arrows indicating the order of write/read and data swaps. A legend at the bottom explains the symbols used.

DATATYPE[1:0] = 00: no swapping

Word 3: D127..D96, Word 2: D95..D64, Word 1: D63..D32, Word 0: D31..D0. Arrows 1-4 show direct mapping from input to output words.

DATATYPE[1:0] = 01: 16-bit (half-word) swapping

Word 3: D127..D112, D111..D96; Word 2: D95..D80, D79..D64; Word 1: D63..D48, D47..D32; Word 0: D31..D16, D15..D0. Arrows show 16-bit half-word swaps within each word.

DATATYPE[1:0] = 10: 8-bit (byte) swapping

Word 3: D127..D120, D119..D112, D111..D104, D103..D96; Word 2: D95..D88, D87..D80, D79..D72, D71..D64; Word 1: D63..D56, D55..D48, D47..D40, D39..D32; Word 0: D31..D24, D23..D16, D15..D8, D7..D0. Arrows show 8-bit byte swaps within each word.

DATATYPE[1:0] = 11: bit swapping

Word 3: D127..D125, D124..D120, D119..D112, D111..D104, D103..D96; Word 2: D95..D93, D92..D88, D87..D80, D79..D72, D71..D64; Word 1: D63..D61, D60..D56, D55..D48, D47..D40, D39..D32; Word 0: D31..D29, D28..D24, D23..D16, D15..D8, D7..D0. Arrows show individual bit swaps within each word.

Legend:

MSV43721V2

Diagram showing 128-bit block construction for different DATATYPE settings: 00 (no swapping), 01 (16-bit swapping), 10 (8-bit byte swapping), and 11 (bit swapping). Each section shows Word 3, Word 2, Word 1, and Word 0 with bit-level details and arrows indicating the order of write/read and data swaps. A legend at the bottom explains the symbols used.

23.4.17 CRYP key registers

The CRYP_Kx registers are write-only registers used to store the encryption or decryption keys. They are organized as four 64-bit registers, as shown in Table 161 and Table 162 .

Note: In memory and in CRYP key registers, AES and DES/TDES keys are stored in big-endian format, with most significant byte on the lowest address.

Table 161. Key endianness in CRYP_KxR/LR registers (AES 128/192/256-bit keys)

K0LR[31:0]K0RR[31:0]K1LR[31:0]K1RR[31:0]K2LR[31:0]K2RR[31:0]K3LR[31:0]K3RR[31:0]
----k[127:96]k[95:64]k[63:32]k[31:0]
K0LR[31:0]K0RR[31:0]K1LR[31:0]K1RR[31:0]K2LR[31:0]K2RR[31:0]K3LR[31:0]K3RR[31:0]
--k[191:160]k[159:128]k[127:96]k[95:64]k[63:32]k[31:0]
K0LR[31:0]K0RR[31:0]K1LR[31:0]K1RR[31:0]K2LR[31:0]K2RR[31:0]K3LR[31:0]K3RR[31:0]
k[255:224]k[223:192]k[191:160]k[159:128]k[127:96]k[95:64]k[63:32]k[31:0]

Table 162. Key endianness in CRYP_KxR/LR registers (DES K1 and TDES K1/2/3)

K0LR[31:0]K0RR[31:0]K1LR[31:0]K1RR[31:0]K2LR[31:0]K2RR[31:0]K3LR[31:0]K3RR[31:0]
--K1[64:33]K1[32:1]----
--K1[64:33]K1[32:1]K2[64:33]K2[32:1]K3[64:33]K3[32:1]

As shown on Table 162 , when TDES is selected (ALGOMODE[2:0] = 000 or 001) a 3-key vector (K1, K2, and K3) is used. When DES is selected (ALGOMODE[2:0] = 010 or 011) only 1-key vector (K1) is used.

Note: DES/TDES keys include 8-bit parity information that are not used by the cryptographic processor. In other words, bits 8, 16, 24, 32, 40, 48, 56 and 64 of each 64-bit key value Kx[1:64] are not used.

Write operations to the CRYP_Kx(L/R) registers when the BUSY bit is set to 1 in the CRYP_SR register are ignored (which means that the register content is not modified). The application must thus check that the BUSY bit is cleared to 0 before modifying key registers.

Key registers are not affected by the data swapping controlled by DATATYPE value in CRYP_CR register.

Refer to Section 23.7: CRYP registers for a detailed description of CRYP_Kx(L/R) registers.

23.4.18 CRYP initialization vector registers

The CRYP_IVxL/IVxR registers are used to store the initialization vector or the nonce, depending on the chaining mode selected. When used, these registers are updated by the core after each computation round of the TDES or AES core.

They are organized as four 64-bit registers, as shown in Table 163 and Table 164 . In DES/TDES mode only CRYP_IV0x are used.

Note: In memory and in CRYP IV registers, AES and DES/TDES initialization vectors are stored in big-endian format, with most significant byte on the lowest address.

Table 163. Initialization vector endianness in CRYP_IVx(L/R)R registers (AES)
CRYP_IV0LR[31:0]CRYP_IV0RR[31:0]CRYP_IV1LR[31:0]CRYP_IV1RR[31:0]
IVI[127:96]IVI[95:64]IVI[63:32]IVI[31:0]
Table 164. Initialization vector endianness in CRYP_IVx(L/R)R registers (DES/TDES)
CRYP_IV0LR[31:0]CRYP_IV0RR[31:0]CRYP_IV1LR[31:0]CRYP_IV1RR[31:0]
IVI[64:32]IVI[31:0]--

Any write operation to the CRYP_IV0...1(L/R)R registers when the BUSY bit is set to 1 in the CRYP_SR register is disregarded (which means that register content not modified). The software must thus check that the BUSY bit is cleared to 0 in the CRYP_SR register before modifying initialization vectors.

Reading the CRYP_IV0...1(L/R)R register returns the latest counter value (useful for managing suspend mode) except for CCM/GCM.

The initialization vector registers are not affected by the data swapping feature controlled by DATATYPE value in CRYP_CR register.

Refer to Section 23.7: CRYP registers for a detailed description of CRYP_IVxL/IVxR registers.

23.4.19 CRYP DMA interface

The cryptographic processor provides an interface to connect to the DMA (Direct Memory Access) controller. The DMA operation is controlled through the CRYP DMA control register (CRYP_DMACR).

Data input using DMA

DMA can be enabled for writing data into the cryptographic peripheral by setting the DIEN bit in the CRYP_DMACR register. When this bit is set, the cryptographic processor initiates a DMA request during the INPUT phase each time it requires a word to be written to the CRYP_DIN register.

Table 165 shows the recommended configuration to transfer data from memory to cryptographic processor through the DMA controller.

Table 165. Cryptographic processor configuration for memory-to-peripheral DMA transfers
DMA channel control register fieldProgramming recommendation
Transfer sizeMessage length, multiple of four 32-bit words. This 128-bit granularity corresponds to two blocks for DES/TDES, one block for AES.
According to the algorithm and the mode selected, special padding/ciphertext stealing might be required. Refer to Section 23.4.8: CRYP stealing and data padding for details.
Source burst size (memory)CRYP_FIFO_size /2 /transfer_width = 4
Table 165. Cryptographic processor configuration for memory-to-peripheral DMA transfers (continued)
DMA channel control register fieldProgramming recommendation
Destination burst size (peripheral)CRYP FIFO_size /2 /transfer_width = 4
(FIFO_size = 8x32-bit, transfer_width = 32-bit)
DMA FIFO sizeCRYP FIFO_size /2 = 16 bytes
Source transfer width (memory)32-bit words
Destination transfer width (peripheral)32-bit words
Source address increment (memory)Yes, after each 32-bit transfer.
Destination address increment (peripheral)Fixed address of CRYP_DIN shall be used (no increment).

Data output using DMA

To enable the DMA for reading data from CRYP processor, set the DOEN bit in the CRYP_DMACR register. When this bit is set, the cryptographic processor initiates a DMA request during the OUTPUT phase each time it requires a word to be read from the CRYP_DOUT register.

Table 166 shows the recommended configuration to transfer data from cryptographic processor to memory through the DMA controller.

Table 166. Cryptographic processor configuration for peripheral-to-memory DMA transfers
DMA channel control register fieldProgramming recommendation
Transfer sizeMessage length, multiple of four 32-bit words. This 128-bit granularity corresponds to two blocks for DES/TDES, one block for AES.
Depending on the chaining mode selected, extra bits have to be discarded.
Source burst size (peripheral)When DES is used:
Single transfer (burst size = 1)
When AES is used:
CRYP FIFO_size /2 /transfer_width = 4
(FIFO_size = 8x32-bit, transfer_width = 32-bit)
Destination burst size (memory)CRYP FIFO_size /2 /transfer_width = 4
DMA FIFO sizeCRYP FIFO_size /2 = 16 bytes
Source transfer width (peripheral)32-bit words
memory transfer width (memory)32-bit words

Table 166. Cryptographic processor configuration for peripheral-to-memory DMA transfers (continued)

DMA channel control register fieldProgramming recommendation
Source address increment (peripheral)Fixed address of CRYP_DOUT shall be used (no increment).
Destination address increment (memory)Yes, after each 32-bit transfer.

DMA mode

When AES is used, the cryptographic processor manages two DMA transfer requests through cryp_in_dma and cryp_out_dma internal input/output signals, which are asserted:

When DES/TDES is used, the cryptographic processor manages two DMA transfer requests through cryp_in_dma and cryp_out_dma internal input/output signals, which are asserted:

for OUT FIFO: every time a word has been written into the FIFO by the cryptographic processor (single transfer). Note that a burst transfer is also triggered when two blocks have been written into the FIFO.

All request signals are deasserted if the cryptographic peripheral is disabled or the DMA enable bit is cleared (DIEN bit for the IN FIFO and DOEN bit for the OUT FIFO in the CRYP_DMACR register).

Caution: It is important that DMA controller empties the cryptographic peripheral output FIFO before filling up the CRYP input FIFO. To achieve it, the DMA controller should be configured so that the transfer from the peripheral to the memory has a higher priority than the transfer from the memory to the peripheral.

For more detailed information on DMA operations, refer to Section 23.4.5: CRYP procedure to perform a cipher operation .

23.4.20 CRYP error management

No error flags are generated by the cryptographic processor.

23.5 CRYP interrupts

Overview

There are two individual maskable interrupt sources generated by the cryptographic processor to signal the following events:

These two sources are combined into a single interrupt signal which is the only interrupt signal from the CRYP processor that drives the Cortex® CPU interrupt controller. You can enable or disable CRYP interrupt sources individually by changing the mask bits in the CRYP_IMSCR register. Setting the appropriate mask bit to 1 enables the interrupt.

The status of the individual maskable interrupt sources can be read either from the CRYP_RISR register, for raw interrupt status, or from the CRYP_MISR register for masked interrupt status. The status of the individual source of event flags can be read from the CRYP_SR register.

Table 167 gives a summary of the available features.

Table 167. CRYP interrupt requests

Interrupt acronymInterrupt eventEvent flagEnable bitInterrupt clear method
in CRYP_xISR (1)in CRYP_SR
CRYPOutput FIFO fullOUTRIS (not masked)
OUTMIS (masked)
OFFUOUTIM (2) and
CRYPEN
Read one data from output FIFO
Output FIFO not emptyOFNERead all data from output FIFO
Input FIFO not fullINRIS (not masked)
INMIS (masked)
IFNFINIM (2) and
CRYPEN
Write data until input FIFO is full
Input FIFO emptyIFEMWrite at least one word in input FIFO

1. The flags belong to CRYP_RISR registers (unmasked or raw) or CRYP_MISR registers (masked).

2. The flags belong to CRYP_IMSCR register.

Output FIFO service interrupt - OUTMIS

The output FIFO service interrupt is asserted when there is one or more (32-bit word) data items in the output FIFO. This interrupt is cleared by reading data from the output FIFO until there is no valid (32-bit) word left (that is when the interrupt follows the state of the output FIFO not empty flag OFNE).

The output FIFO service interrupt OUTMIS is NOT enabled with the CRYP processor enable bit. Consequently, disabling the CRYP processor does not force the OUTMIS signal low if the output FIFO is not empty.

Input FIFO service interrupt - INMIS

The input FIFO service interrupt is asserted when there are less than four words in the input FIFO. It is cleared by performing write operations to the input FIFO until it holds four or more words.

The input FIFO service interrupt INMIS is enabled with the CRYP enable bit. Consequently, when CRYP is disabled, the INMIS signal is low even if the input FIFO is empty.

23.6 CRYP processing time

The time required to process a block for each mode of operation is summarized below. The block size is 128 bits for AES and 64 bits for DES/TDES.

Table 168. Processing latency for ECB, CBC and CTR

Key sizeOperating modesChaining algorithmClock cycles
64 bitsDES encryption or decryptionECD, CBC16
3x64 bitsTDES encryption or decryption48
128 bitsAES encryption or decryption (1)ECD, CBC, CTR14
AES key preparation-12
192 bitsAES encryption or decryption (1)ECD, CBC, CTR16
AES key preparation-14
256 bitsAES encryption or decryption (1)ECD, CBC, CTR18
AES key preparation-16

1. Excluding key preparation time (ECB and CBC only).

Table 169. Processing time (in clock cycle) for GCM and CCM per 128-bit block

Key sizeOperating modesChaining algorithmInitialization phaseHeader phasePayload phaseTag phase
128 bitsAES encryption or decryptionGCM24101414
CCM12142514
192 bitsGCM28101616
CCM14162916
256 bitsGCM32101818
CCM16183318

23.7 CRYP registers

The cryptographic core is associated with several control and status registers, eight key registers and four initialization vectors registers.

23.7.1 CRYP control register (CRYP_CR)

Address offset: 0x00

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ALGOMODE3Res.GCM_CCMPH [1:0]
rwrwrw
1514131211109876543210
CRYPENFFLUSHRes.Res.Res.Res.KEYSIZE[1:0]DATATYPE[1:0]ALGOMODE[2:0]ALGODIRRes.Res.
rwrwrwrwrwrwrwrwrwrw

Bits 31:24 Reserved, must be kept at reset value.

Bits 23:20 Reserved, must be kept at reset value.

Bit 18 Reserved, must be kept at reset value.

Bits 17:16 GCM_CCMPH[1:0] : GCM or CCM Phase selection

This bitfield has no effect if GCM, GMAC or CCM algorithm is not selected in ALGOMODE field.

00: Initialization phase

01: Header phase

10: Payload phase

11: Final phase

Writing to GCM_CCMPH bits while BUSY = 1 has no effect.

Bit 15 CRYPEN : CRYP processor Enable

0: Cryptographic processor peripheral is disabled

1: Cryptographic processor peripheral is enabled

This bit is automatically cleared by hardware when the key preparation process ends (ALGOMODE = 0111) or after GCM/GMAC or CCM Initialization phase.

Bit 14 FFLUSH : CRYP FIFO Flush

0: No FIFO flush

1: FIFO flush enabled

When CRYPEN = 0, writing this bit to 1 flushes the IN and OUT FIFOs (that is read and write pointers of the FIFOs are reset). Writing this bit to 0 has no effect. When CRYPEN = 1, writing this bit to 0 or 1 has no effect.

Reading this bit always returns 0.

FFLUSH bit has to be set only when BUSY = 0. If not, the FIFO is flushed, but the block being processed may be pushed into the output FIFO just after the flush operation, resulting in a non-empty FIFO condition.

Bits 13:10 Reserved, must be kept at reset value.

Bits 9:8 KEYSIZE[1:0] : Key size selection (AES mode only)

This bitfield defines the bit-length of the key used for the AES cryptographic core. This bitfield is 'don't care' in the DES or TDES modes.

00: 128-bit key length

01: 192-bit key length

10: 256-bit key length

11: Reserved, do not use this value

Writing KEYSIZE bits while BUSY = 1 has no effect.

Bits 7:6 DATATYPE[1:0] : Data Type selection

This bitfield defines the format of data written in CRYP_DIN or read from CRYP_DOUT registers. For more details refer to Section 23.4.16: CRYP data registers and data swapping .

00: 32-bit data. No swapping for each word. First word pushed into the IN FIFO (or popped off the OUT FIFO) forms bits 1...32 of the data block, the second word forms bits 33...64 etc.

01: 16-bit data, or half-word. Each word pushed into the IN FIFO (or popped off the OUT FIFO) is considered as 2 half-words, which are swapped with each other.

10: 8-bit data, or bytes. Each word pushed into the IN FIFO (or popped off the OUT FIFO) is considered as 4 bytes, which are swapped with each other.

11: bit data, or bit-string. Each word pushed into the IN FIFO (or popped off the OUT FIFO) is considered as 32 bits (1st bit of the string at position 0), which are swapped with each other.

Writing DATATYPE bits while BUSY = 1 has no effect.

Bits 19, 5:3 ALGOMODE[3:0] : Algorithm mode

Below definition includes the bit 19:

0000: TDES-ECB (triple-DES Electronic Codebook).

0001: TDES-CBC (triple-DES Cipher Block Chaining).

0010: DES-ECB (simple DES Electronic Codebook).

0011: DES-CBC (simple DES Cipher Block Chaining).

0100: AES-ECB (AES Electronic Codebook).

0101: AES-CBC (AES Cipher Block Chaining).

0110: AES-CTR (AES Counter mode).

0111: AES key preparation for ECB or CBC decryption.

1000: AES-GCM (Galois Counter mode) and AES-GMAC (Galois Message Authentication Code mode).

1001: AES-CCM (Counter with CBC-MAC).

Writing ALGOMODE bits while BUSY = 1 has no effect.

Others: Reserved, must no be used

Bit 2 ALGODIR : Algorithm Direction

0: Encrypt

1: Decrypt

Writing ALGODIR bit while BUSY = 1 has no effect.

Bits 1:0 Reserved, must be kept at reset value.

23.7.2 CRYP status register (CRYP_SR)

Address offset: 0x04

Reset value: 0x0000 0003

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.BUSYOFFUOFNEIFNFIFEM
rrrrr

Bits 31:5 Reserved, must be kept at reset value.

Bit 4 BUSY : Busy bit

0: The CRYP core is not processing any data. The reason is:

1: The CRYP core is currently processing a block of data or a key preparation is ongoing (AES ECB or CBC decryption only).

Bit 3 OFFU : Output FIFO full flag

0: Output FIFO is not full

1: Output FIFO is full

Bit 2 OFNE : Output FIFO not empty flag

0: Output FIFO is empty

1: Output FIFO is not empty

Bit 1 IFNF : Input FIFO not full flag

0: Input FIFO is full

1: Input FIFO is not full

Bit 0 IFEM : Input FIFO empty flag

0: Input FIFO is not empty

1: Input FIFO is empty

23.7.3 CRYP data input register (CRYP_DIN)

Address offset: 0x08

Reset value: 0x0000 0000

The CRYP_DIN register is the data input register. It is 32-bit wide. It is used to enter into the input FIFO up to four 64-bit blocks (TDES) or two 128-bit blocks (AES) of plaintext (when encrypting) or ciphertext (when decrypting), one 32-bit word at a time.

To fit different data sizes, the data can be swapped after processing by configuring the DATATYPE bits in the CRYP_CR register. Refer to Section 23.4.16: CRYP data registers and data swapping for more details.

When CRYP_DIN register is written to the data are pushed into the input FIFO.

When CRYP_DIN register is read:

Note: After the CRYP_DIN register has been read once or several times, the FIFO must be flushed by setting the FFLUSH bit prior to processing new data.

31302928272625242322212019181716
DATAIN[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
DATAIN[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 DATAIN[31:0] : Data input

On read FIFO is popped (last written value is returned), and its value is returned if CRYPTEN = 0. If CRYPTEN = 1 DATAIN register returns an undefined value.

On write current register content is pushed inside the FIFO.

23.7.4 CRYP data output register (CRYP_DOUT)

Address offset: 0x0C

Reset value: 0x0000 0000

The CRYP_DOUT register is the data output register. It is read-only and 32-bit wide. It is used to retrieve from the output FIFO up to four 64-bit blocks (TDES) or two 128-bit blocks

(AES) of plaintext (when encrypting) or ciphertext (when decrypting), one 32-bit word at a time.

To fit different data sizes, the data can be swapped after processing by configuring the DATATYPE bits in the CRYP_CR register. Refer to Section 23.4.16: CRYP data registers and data swapping for more details.

When CRYP_DOUT register is read, the last data entered into the output FIFO (pointed to by the read pointer) is returned.

31302928272625242322212019181716
DATAOUT[31:16]
rrrrrrrrrrrrrrrr
1514131211109876543210
DATAOUT[15:0]
rrrrrrrrrrrrrrrr

Bits 31:0 DATAOUT[31:0] : Data output

On read returns output FIFO content (pointed to by read pointer), else returns an undefined value.
On write, no effect.

23.7.5 CRYP DMA control register (CRYP_DMACR)

Address offset: 0x10
Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.DOENDIEN
rwrw

Bits 31:2 Reserved, must be kept at reset value.

Bit 1 DOEN : DMA output enable

When this bit is set, DMA requests are automatically generated by the peripheral during the output data phase.
0: Outgoing data transfer from CRYP via DMA is disabled
1: Outgoing data transfer from CRYP via DMA is enabled

Bit 0 DIEN : DMA input enable

When this bit is set, DMA requests are automatically generated by the peripheral during the input data phase.
0: Incoming data transfer to CRYP via DMA is disabled
1: Incoming data transfer to CRYP via DMA is enabled

23.7.6 CRYP interrupt mask set/clear register (CRYP_IMSCR)

Address offset: 0x14

Reset value: 0x0000 0000

The CRYP_IMSCR register is the interrupt mask set or clear register. It is a read/write register. When a read operation is performed, this register gives the current value of the mask applied to the relevant interrupt. Writing 1 to the particular bit sets the mask, thus enabling the interrupt to be read. Writing 0 to this bit clears the corresponding mask. All the bits are cleared to 0 when the peripheral is reset.

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.OUTIMINIM
rwrw

Bits 31:2 Reserved, must be kept at reset value.

Bit 1 OUTIM : Output FIFO service interrupt mask

0: Output FIFO service interrupt is masked

1: Output FIFO service interrupt is not masked

Bit 0 INIM : Input FIFO service interrupt mask

0: Input FIFO service interrupt is masked

1: Input FIFO service interrupt is not masked

23.7.7 CRYP raw interrupt status register (CRYP_RISR)

Address offset: 0x18

Reset value: 0x0000 0001

The CRYP_RISR register is the raw interrupt status register. It is a read-only register. When a read operation is performed, this register gives the current raw status of the corresponding interrupt, i.e. the interrupt information without taking CRYP_IMSCR mask into account. Write operations have no effect.

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.OUTRISINRIS
rr

Bits 31:2 Reserved, must be kept at reset value.

Bit 1 OUTRIS : Output FIFO service raw interrupt status

This bit gives the output FIFO interrupt information without taking CRYP_IMSCR corresponding mask into account.

0: Raw interrupt not pending

1: Raw interrupt pending

Bit 0 INRIS : Input FIFO service raw interrupt status

This bit gives the input FIFO interrupt information without taking CRYP_IMSCR corresponding mask into account.

0: Raw interrupt not pending

1: Raw interrupt pending

23.7.8 CRYP masked interrupt status register (CRYP_MISR)

Address offset: 0x1C

Reset value: 0x0000 0000

The CRYP_MISR register is the masked interrupt status register. It is a read-only register. When a read operation is performed, this register gives the current masked status of the corresponding interrupt, i.e. the interrupt information taking CRYP_IMSCR mask into account. Write operations have no effect.

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.OUTMISINMIS
rr

Bits 31:2 Reserved, must be kept at reset value.

Bit 1 OUTMIS : Output FIFO service masked interrupt status

This bit gives the output FIFO interrupt information taking into account the corresponding CRYP_IMSCR mask.

0: Output FIFO interrupt not pending

1: Output FIFO interrupt pending

Bit 0 INMIS : Input FIFO service masked interrupt status

This bit gives the input FIFO interrupt information taking into account the corresponding CRYP_IMSCR mask.

0: Input FIFO interrupt not pending

1: Input FIFO interrupt pending when CRYPTEN = 1

23.7.9 CRYP key register 0L (CRYP_K0LR)

Address offset: 0x20

Reset value: 0x0000 0000

CRYP key registers contain the cryptographic keys.

For more information refer to Section 23.4.17: CRYP key registers .

Note: Write accesses to these registers are disregarded when the cryptographic processor is busy (bit BUSY = 1 in the CRYP_SR register) .

31302928272625242322212019181716
K[255:240]
wwwwwwwwwwwwwwww
1514131211109876543210
K[239:224]
wwwwwwwwwwwwwwww

Bits 31:0 K[255:224] : Key bit x (x = 255 to 224)

This write-only bitfield contains the bits [255:224] of the AES encryption or decryption key, depending on the operating mode. This register is not used in DES/TDES mode.

23.7.10 CRYP key register 0R (CRYP_K0RR)

Address offset: 0x24

Reset value: 0x0000 0000

Refer to Section 23.7.9: CRYP key register 0L (CRYP_K0LR) for details.

31302928272625242322212019181716
K[223:208]
wwwwwwwwwwwwwwww
1514131211109876543210
K[207:192]
wwwwwwwwwwwwwwww

Bits 31:0 K[223:192] : Key bit x (x = 223 to 192)

This write-only bitfield contains the bits [223:192] of the AES encryption or decryption key, depending on the operating mode. This register is not used in DES/TDES mode.

23.7.11 CRYP key register 1L (CRYP_K1LR)

Address offset: 0x28

Reset value: 0x0000 0000

Refer to Section 23.7.9: CRYP key register 0L (CRYP_K0LR) for details.

31302928272625242322212019181716
K[191:176]
wwwwwwwwwwwwwwww
1514131211109876543210
K[175:160]
wwwwwwwwwwwwwwww

Bits 31:0 K[191:160] : Key bit x (x = 191 to 160)

This write-only bitfield contains the bits [191:160] of the AES encryption or decryption key, depending on the operating mode. In DES/TDES mode this bitfield contains the bits [64:33] of the key K1, with parity bits unused.

23.7.12 CRYP key register 1R (CRYP_K1RR)

Address offset: 0x2C

Reset value: 0x0000 0000

Refer to Section 23.7.9: CRYP key register 0L (CRYP_K0LR) for details.

31302928272625242322212019181716
K[159:144]
wwwwwwwwwwwwwwww
1514131211109876543210
K[143:128]
wwwwwwwwwwwwwwww

Bits 31:0 K[159:128] : Key bit x (x = 159 to 128)

This write-only bitfield contains the bits [159:128] of the AES encryption or decryption key, depending on the operating mode. In DES/TDES mode this bitfield contains the bits [32:1] of the key K1, with parity bits unused.

23.7.13 CRYP key register 2L (CRYP_K2LR)

Address offset: 0x30

Reset value: 0x0000 0000

Refer to Section 23.7.9: CRYP key register 0L (CRYP_K0LR) for details.

31302928272625242322212019181716
K[127:112]
wwwwwwwwwwwwwwww
1514131211109876543210
K[111:96]
wwwwwwwwwwwwwwww

Bits 31:0 K[127:96] : Key bit x (x = 127 to 96)

This write-only bitfield contains the bits [127:96] of the AES encryption or decryption key, depending on the operating mode. In DES/TDES mode this bitfield contains the bits [64:33] of the key K2, with parity bits unused.

23.7.14 CRYP key register 2R (CRYP_K2RR)

Address offset: 0x34

Reset value: 0x0000 0000

Refer to Section 23.7.9: CRYP key register 0L (CRYP_K0LR) for details.

31302928272625242322212019181716
K[95:80]
wwwwwwwwwwwwwwww
1514131211109876543210
K[79:64]
wwwwwwwwwwwwwwww

Bits 31:0 K[95:64] : Key bit x (x = 95 to 64)

This write-only bitfield contains the bits [95:64] of the AES encryption or decryption key, depending on the operating mode. In DES/TDES mode this bitfield contains the bits [32:1] of the key K2, with parity bits unused.

23.7.15 CRYP key register 3L (CRYP_K3LR)

Address offset: 0x38

Reset value: 0x0000 0000

Refer to Section 23.7.9: CRYP key register 0L (CRYP_K0LR) for details.

31302928272625242322212019181716
K[63:48]
wwwwwwwwwwwwwwww
1514131211109876543210
K[47:32]
wwwwwwwwwwwwwwww

Bits 31:0 K[63:32] : Key bit x (x = 63 to 32)

This write-only bitfield contains the bits [63:32] of the AES encryption or decryption key, depending on the operating mode. In DES/TDES mode this bitfield contains the bits [64:33] of the key K3, with parity bits unused.

23.7.16 CRYP key register 3R (CRYP_K3RR)

Address offset: 0x3C

Reset value: 0x0000 0000

Refer to Section 23.7.9: CRYP key register 0L (CRYP_K0LR) for details.

31302928272625242322212019181716
K[31:16]
wwwwwwwwwwwwwwww
1514131211109876543210
K[15:0]
wwwwwwwwwwwwwwww

Bits 31:0 K[31:0] : Key bit x (x = 31 to 0)

This write-only bitfield contains the bits [31:0] of the AES encryption or decryption key, depending on the operating mode. In DES/TDES mode this bitfield contains the bits [32:1] of the key K3, with parity bits unused.

23.7.17 CRYP initialization vector register 0L (CRYP_IV0LR)

Address offset: 0x40

Reset value: 0x0000 0000

The CRYP_IVx(L/R)R registers store the initialization vector or the nonce, depending on the chaining mode selected. The size of the IV data is 64 bits for DES/TDES and 128 bits for AES. For more information refer to Section 23.4.18: CRYP initialization vector registers .

Note: Write accesses to these registers are disregarded when the cryptographic processor is busy (BUSY = 1 in the CRYP_SR register).

31302928272625242322212019181716
IVI[127:112]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
IVI[111:96]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 IVI[127:96] : Initialization vector bit x (x = 127 to 96)

This bitfield stores the initialization vector bits [127:96] for AES chaining modes other than ECB. In DES/TDES mode it corresponds to IVI bits [63:32].

IV registers are updated by the core after each computation round of the DES/TDES or AES core.

23.7.18 CRYP initialization vector register 0R (CRYP_IV0RR)

Address offset: 0x44

Reset value: 0x0000 0000

Refer to Section 23.7.17: CRYP initialization vector register 0L (CRYP_IV0LR) for details.

31302928272625242322212019181716
IVI[95:80]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
IVI[79:64]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 IVI[95:64] : Initialization vector bit x (x = 95 to 64)

This bitfield stores the initialization vector bits [95:64] for AES chaining modes other than ECB. In DES/TDES mode it corresponds to IVI bits [31:0].

IV registers are updated by the core after each computation round of the DES/TDES or AES core.

23.7.19 CRYP initialization vector register 1L (CRYP_IV1LR)

Address offset: 0x48

Reset value: 0x0000 0000

Refer to Section 23.7.17: CRYP initialization vector register 0L (CRYP_IV0LR) for details.

31302928272625242322212019181716
IVI[63:48]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
IVI[47:32]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 IVI[63:32] : Initialization vector bit x (x = 63 to 32)

This bitfield stores the initialization vector bits [63:32] for AES chaining modes other than ECB. This register is not used in DES mode.

IV registers are updated by the core after each computation round of the AES core.

23.7.20 CRYP initialization vector register 1R (CRYP_IV1RR)

Address offset: 0x4C

Reset value: 0x0000 0000

Refer to Section 23.7.17: CRYP initialization vector register 0L (CRYP_IV0LR) for details.

31302928272625242322212019181716
IVI[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
IVI[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 IVI[31:0] : Initialization vector bit x (x = 31 to 0)

This bitfield stores the initialization vector bits [31:0] for AES chaining modes other than ECB. This register is not used in DES mode.

IV registers are updated by the core after each computation round of the AES core.

23.7.21 CRYP register map

Table 170. CRYP register map and reset values

OffsetRegister name
reset value
313029282726252423222120191817161514131211109876543210
0x00CRYP_CRRes.Res.Res.Res.Res.Res.Res.Res.ALGOMODE[3]Res.Res.GCM_CCMPHCRYPENFFLUSHRes.Res.Res.Res.KEYSIZEDATATYPEALGOMODE[2:0]ALGODIRRes.Res.
Reset value00000000000000000

Table 170. CRYP register map and reset values (continued)

OffsetRegister name
reset value
313029282726252423222120191817161514131211109876543210
0x04CRYP_SRResResResResResResResResResResResResResResResResResResResResResResResResResResResBUSYOFFUOFNEIFNFIFEM
Reset value00011
0x08CRYP_DINDATAIN
Reset value00000000000000000000000000000000
0x0CCRYP_DOUTDATAOUT
Reset value00000000000000000000000000000000
0x10CRYP_DMACRResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResDOENDIEN
Reset value00
0x14CRYP_IMSCRResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResOUTIMINIM
Reset value00
0x18CRYP_RISRResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResOUTRISINRIS
Reset value01
0x1CCRYP_MISRResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResOUTMISINMIS
Reset value00
0x20CRYP_K0LRK[255:224]
Reset value00000000000000000000000000000000
0x24CRYP_K0RRK[223:192]
Reset value00000000000000000000000000000000
...
0x38CRYP_K3LRK[63:32]
Reset value00000000000000000000000000000000
0x3CCRYP_K3RRK[31:0]
Reset value00000000000000000000000000000000
0x40CRYP_IV0LRIV[127:96]
Reset value00000000000000000000000000000000
0x44CRYP_IV0RRIV[95:64]
Reset value00000000000000000000000000000000
0x48CRYP_IV1LRIV[63:32]
Reset value00000000000000000000000000000000
0x4CCRYP_IV1RRIV[31:0]
Reset value00000000000000000000000000000000

Refer to Section 2.2 on page 76 for the register boundary addresses.