19. Cryptographic processor (CRYP)

This section applies to the whole STM32F20x and STM32F21x family, unless otherwise specified.

19.1 CRYP introduction

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

The CRYP processor performs data encryption and decryption using DES and TDES algorithms in Electronic codebook (ECB) or Cipher block chaining (CBC) mode.

The CRYP peripheral is a 32-bit AHB2 peripheral. It supports DMA transfer for incoming and processed data, and has input and output FIFOs (each 8 words deep).

19.2 CRYP main features

Table 73. Number of cycles required to process each 128-bit block

Algorithm / Key sizeECBCBCCTR
128b141414
192b161616
256b181818

19.3 CRYP functional description

The cryptographic processor implements a Triple-DES (TDES, that also supports DES) core and an AES cryptographic core. Section 19.3.1 and Section 19.3.2 provide details on these cores.

Since the TDES and the AES algorithms use block ciphers, incomplete input data blocks have to be padded prior to encryption (extra bits should be appended to the trailing end of the data string). After decryption, the padding has to be discarded. The hardware does not manage the padding operation, the software has to handle it.

Figure 195 shows the block diagram of the cryptographic processor.

Figure 195. Block diagram

Block diagram of the cryptographic processor (CRYP) showing its internal components and their connections to a 32-bit AHB2 bus.

The block diagram illustrates the internal architecture of the CRYP processor. At the top, a horizontal double-headed arrow represents the 32-bit AHB2 bus . Below this bus, several components are connected via vertical arrows:

The DES/TDES/AES Processor core is the central processing unit, receiving input from the IN FIFO and outputting to the OUT FIFO. The diagram is labeled with the identifier ai16068b in the bottom right corner.

Block diagram of the cryptographic processor (CRYP) showing its internal components and their connections to a 32-bit AHB2 bus.

19.3.1 DES/TDES cryptographic core

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

The basic processing involved in the TDES is as follows: an input block is read in the DEA and encrypted using the first key, K1 (K0 is not used in TDES mode). The output is then decrypted using the second key, K2, and encrypted using the third key, K3. The key depends on the algorithm which is used:

where Kx=[KxR KxL], R = right, L = left

According to the mode implemented, the resultant output block is used to calculate the ciphertext.

Note that the outputs of the intermediate DEA stages is never revealed outside the cryptographic boundary.

The 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.

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 TDEA (TDES algorithm): TDES-ECB encryption, TDES-ECB decryption, TDES-CBC encryption and TDES-CBC decryption.

This reference manual only gives a brief explanation of each mode.

DES and TDES Electronic codebook (DES/TDES-ECB) mode

Figure 196 illustrates the encryption in DES and TDES Electronic codebook (DES/TDES-ECB) mode. A 64-bit plaintext data block (P) is used after bit/byte/half-word swapping (refer to Section 19.3.3: Data type on page 513 ) 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.

Figure 197 illustrates the DES/TDES-ECB decryption. 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).

Figure 196. DES/TDES-ECB mode encryption

Flowchart of DES/TDES-ECB mode encryption process. It shows data flow from IN FIFO (plaintext P) through swapping, three DEA stages (encrypt, decrypt, encrypt) with keys K1, K2, K3, another swapping stage, to OUT FIFO (ciphertext C).

The diagram illustrates the encryption process. It begins with an 'IN FIFO' block containing 'plaintext P'. An arrow labeled 'P, 64 bits' points down to a 'swapping' block. A 'DATATYPE' input points to this block. The output of the 'swapping' block, also labeled 'P, 64 bits', points down to a 'DEA, encrypt' block. This block is associated with key 'K1' (64 bits). The output, labeled 'O, 64 bits', points down to another 'swapping' block, which also receives a 'DATATYPE' input. The output of this second 'swapping' block, labeled 'C, 64 bits', points down to an 'OUT FIFO' block containing 'ciphertext C'. Between the two 'swapping' blocks, there are three 'DEA' blocks: 'DEA, encrypt' (key K1), 'DEA, decrypt' (key K2), and 'DEA, encrypt' (key K3). Each 'DEA' block has a key input (K1, K2, or K3) with a '64' bit width indicator.

Flowchart of DES/TDES-ECB mode encryption process. It shows data flow from IN FIFO (plaintext P) through swapping, three DEA stages (encrypt, decrypt, encrypt) with keys K1, K2, K3, another swapping stage, to OUT FIFO (ciphertext C).

ai16069b

Figure 197. DES/TDES-ECB mode decryption

Flowchart of DES/TDES-ECB mode decryption process. It shows data flow from IN FIFO (ciphertext C) through swapping, three DEA stages (decrypt, encrypt, decrypt) with keys K3, K2, K1, another swapping stage, to OUT FIFO (plaintext P).

The diagram illustrates the decryption process. It begins with an 'IN FIFO' block containing 'ciphertext C'. An arrow labeled 'C, 64 bits' points down to a 'swapping' block. A 'DATATYPE' input points to this block. The output of the 'swapping' block, labeled 'I, 64 bits', points down to a 'DEA, decrypt' block. This block is associated with key 'K3' (64 bits). The output, labeled 'O, 64 bits', points down to another 'swapping' block, which also receives a 'DATATYPE' input. The output of this second 'swapping' block, labeled 'P, 64 bits', points down to an 'OUT FIFO' block containing 'plaintext P'. Between the two 'swapping' blocks, there are three 'DEA' blocks: 'DEA, decrypt' (key K3), 'DEA, encrypt' (key K2), and 'DEA, decrypt' (key K1). Each 'DEA' block has a key input (K3, K2, or K1) with a '64' bit width indicator.

Flowchart of DES/TDES-ECB mode decryption process. It shows data flow from IN FIFO (ciphertext C) through swapping, three DEA stages (decrypt, encrypt, decrypt) with keys K3, K2, K1, another swapping stage, to OUT FIFO (plaintext P).

MS19021V1

DES and TDES Cipher block chaining (DES/TDES-CBC) mode

Figure 198 illustrates the DES and Triple-DES Cipher block chaining (DES/TDES-CBC) mode encryption. 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 (refer to Section 19.3.3: Data type on page 513 ), 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.

In DES/TDES-CBC decryption (see Figure 199 ), 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 TCBC 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.

Figure 198. DES/TDES-CBC mode encryption

Block diagram of DES/TDES-CBC mode encryption showing the flow from IN FIFO to OUT FIFO through DEA blocks with keys K1, K2, and K3, including swapping and XOR operations with IV0(L/R).

The diagram illustrates the data flow for DES/TDES-CBC mode encryption. It starts with the IN FIFO providing plaintext P (64 bits). This data passes through a swapping block (controlled by DATATYPE ) to produce Ps (64 bits). Ps is then XORed ( + ) with the output of IV0(L/R) to produce the input block I (64 bits). I is processed by a DEA, encrypt block using key K1 to produce the output block O (64 bits). O is written back into IV and also pushed into the OUT FIFO via another swapping block (controlled by DATATYPE ) to produce the final ciphertext C (64 bits). The DEA blocks are configured as DEA, encrypt (K1), DEA, decrypt (K2), and DEA, encrypt (K3). A note indicates that an AHB2 data write (before CRYP is enabled) can be used to write into IV0(L/R) . Another note states that O is written back into IV at the same time as it is pushed into the OUT FIFO . The identifier ai16070b is shown in the bottom right corner.

Block diagram of DES/TDES-CBC mode encryption showing the flow from IN FIFO to OUT FIFO through DEA blocks with keys K1, K2, and K3, including swapping and XOR operations with IV0(L/R).
  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.

Figure 199. DES/TDES-CBC mode decryption

Diagram of DES/TDES-CBC mode decryption process. The flow starts with 'IN FIFO ciphertext C' (64 bits) entering a 'swapping' block. The output is 'I, 64 bits'. This 'I' is written back into the 'IV0(L/R)' register and also enters a series of three 'DEA' (Data Encryption Algorithm) blocks labeled 'DEA, decrypt', 'DEA, encrypt', and 'DEA, decrypt'. These blocks are controlled by keys 'K3', 'K2', and 'K1' (64 bits each). The output of the third DEA block is 'O, 64 bits'. 'O' is XORed with the value from 'IV0(L/R)' to produce 'Ps, 64 bits'. 'Ps' then enters another 'swapping' block to produce the final output 'P, 64 bits' in the 'OUT FIFO plaintext P'. An 'AHB2 data write (before CRYP is enabled)' is shown entering the 'IV0(L/R)' register. A note indicates 'I is written back into IV at the same time as P is pushed into the OUT FIFO'. The diagram is labeled 'MS19022V1'.
Diagram of DES/TDES-CBC mode decryption process. The flow starts with 'IN FIFO ciphertext C' (64 bits) entering a 'swapping' block. The output is 'I, 64 bits'. This 'I' is written back into the 'IV0(L/R)' register and also enters a series of three 'DEA' (Data Encryption Algorithm) blocks labeled 'DEA, decrypt', 'DEA, encrypt', and 'DEA, decrypt'. These blocks are controlled by keys 'K3', 'K2', and 'K1' (64 bits each). The output of the third DEA block is 'O, 64 bits'. 'O' is XORed with the value from 'IV0(L/R)' to produce 'Ps, 64 bits'. 'Ps' then enters another 'swapping' block to produce the final output 'P, 64 bits' in the 'OUT FIFO plaintext P'. An 'AHB2 data write (before CRYP is enabled)' is shown entering the 'IV0(L/R)' register. A note indicates 'I is written back into IV at the same time as P is pushed into the OUT FIFO'. 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.

19.3.2 AES cryptographic core

The AES cryptographic core consists of three components:

The AES utilizes keys of 3 possible lengths: 128, 192 or 256 bits and, depending on the operation mode used, zero or one 128-bit initialization vector (IV).

The basic processing involved in the AES is as follows: an input block of 128 bits is read from the input FIFO and sent to the AEA to be encrypted using the key (K0...3). The key format depends on the key size:

where \( Kx = [KxR KxL] \) , R=right, L=left

According to the mode implemented, the resultant output block is used to calculate the ciphertext.

FIPS PUB 197 (November 26, 2001) provides a thorough explanation of the processing involved in the four operation modes supplied by the AES core: AES-ECB encryption, AES-

ECB decryption, AES-CBC encryption and AES-CBC decryption. This reference manual only gives a brief explanation of each mode.

AES Electronic codebook (AES-ECB) mode

Figure 200 illustrates the AES Electronic codebook (AES-ECB) mode encryption.

In AES-ECB encryption, a 128-bit plaintext data block (P) is used after bit/byte/half-word swapping (refer to Section 19.3.3: Data type on page 513 ) 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.

Figure 201 illustrates the AES Electronic codebook (AES-ECB) mode encryption.

To perform an AES decryption in the 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 function is computed by the AES core. Refer to Section 19.3.6: Procedure to perform an encryption or a decryption for more details on how to prepare the key.

In AES-ECB decryption, 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).

Figure 200. AES-ECB mode encryption

Figure 200. AES-ECB mode encryption flow diagram. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block (DATATYPE) to produce an input block (I, 128 bits). This block is then processed by the AEA (encrypt) block using a key (K0...3(1), 128/192 or 256 bits). The output of the AEA is then processed by another swapping block (DATATYPE) to produce the ciphertext (C, 128 bits), which is then pushed into the OUT FIFO. The diagram is labeled ai16071b.
graph TD
    IN_FIFO["IN FIFO
plaintext P"] -- "P, 128 bits" --> SWAP1["swapping
DATATYPE"] SWAP1 -- "I, 128 bits" --> AEA["AEA, encrypt
K0...3(1)
128/192 or 256"] AEA --> SWAP2["swapping
DATATYPE"] SWAP2 -- "C, 128 bits" --> OUT_FIFO["OUT FIFO
ciphertext C"]
Figure 200. AES-ECB mode encryption flow diagram. The diagram shows the flow of data from an IN FIFO (plaintext P) through a swapping block (DATATYPE) to produce an input block (I, 128 bits). This block is then processed by the AEA (encrypt) block using a key (K0...3(1), 128/192 or 256 bits). The output of the AEA is then processed by another swapping block (DATATYPE) to produce the ciphertext (C, 128 bits), which is then pushed into the OUT FIFO. The diagram is labeled ai16071b.
  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].

Figure 201. AES-ECB mode decryption

Figure 201. AES-ECB mode decryption diagram showing data flow from IN FIFO ciphertext C through swapping, AEA decrypt with key K0...3(1), another swapping stage, to OUT FIFO plaintext P.
graph TD
    subgraph " "
    A[IN FIFO
ciphertext C] -->|C, 128 bits| B[swapping] D[DATATYPE] --> B B -->|I, 128 bits| E[AEA, decrypt] F["K0...3(1)"] -->|128/192
or 256| E E -->|O, 128 bits| G[swapping] H[DATATYPE] --> G G -->|P, 128 bits| I[OUT FIFO
plaintext P] end
MS19023V1
Figure 201. AES-ECB mode decryption diagram showing data flow from IN FIFO ciphertext C through swapping, AEA decrypt with key K0...3(1), another swapping stage, to OUT FIFO plaintext P.
  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].

AES Cipher block chaining (AES-CBC) mode

The AES Cipher block chaining (AES-CBC) mode decryption is shown on Figure 202 .

In AES-CBC encryption, the first input block ( \( I_1 \) ) obtained after bit/byte/half-word swapping (refer to Section 19.3.3: Data type on page 513 ) 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.

In the CBC mode, like in the ECB mode, the secret key must be prepared to perform an AES decryption. Refer to Section 19.3.6: Procedure to perform an encryption or a decryption on page 518 for more details on how to prepare the key.

In AES-CBC decryption (see Figure 203 ), 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.

Figure 202. AES-CBC mode encryption

Diagram of AES-CBC mode encryption process. The flow starts with IN FIFO (plaintext P) providing 128-bit blocks P. These pass through a 'swapping' block (controlled by DATATYPE) to become Ps (128 bits). Ps is XORed with the Initialization Vector (IV) to produce input block I (128 bits). I is then processed by the AEA (encrypt) block using key K (128, 192, or 256 bits) to produce output block O (128 bits). O is written back into the IV and also pushed into the OUT FIFO. O passes through another 'swapping' block (controlled by DATATYPE) to become ciphertext C (128 bits), which is then stored in the OUT FIFO. A note indicates that O is written back into IV at the same time it is pushed into the OUT FIFO. The diagram is labeled ai160721.
Diagram of AES-CBC mode encryption process. The flow starts with IN FIFO (plaintext P) providing 128-bit blocks P. These pass through a 'swapping' block (controlled by DATATYPE) to become Ps (128 bits). Ps is XORed with the Initialization Vector (IV) to produce input block I (128 bits). I is then processed by the AEA (encrypt) block using key K (128, 192, or 256 bits) to produce output block O (128 bits). O is written back into the IV and also pushed into the OUT FIFO. O passes through another 'swapping' block (controlled by DATATYPE) to become ciphertext C (128 bits), which is then stored in the OUT FIFO. A note indicates that O is written back into IV at the same time it is pushed into the OUT FIFO. The diagram is labeled ai160721.
  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].

Figure 203. AES-CBC mode decryption

Block diagram of AES-CBC mode decryption. The process starts with 'IN FIFO ciphertext C' (128 bits) entering a 'swapping' block. The output of 'swapping' is 'I, 128 bits', which is input to the 'AEA, decrypt' block. The 'AEA, decrypt' block also receives a key 'K 0...3(3)' (128, 192, or 256 bits). The output of 'AEA, decrypt' is 'O, 128 bits'. This 'O' is XORed with the output of an 'IV=[IV1 IV0](2)' block (128 bits) at a summation node. The output of the summation node is 'Ps, 128 bits', which is input to another 'swapping' block. The output of this second 'swapping' block is 'P, 128 bits', which is output to the 'OUT FIFO plaintext P'. A dashed box indicates that 'I is written back into IV at the same time I as P is pushed into the OUT FIFO'. An 'AHB2 data write (before CRYP is enabled)' is shown to the 'IV' block. A 'DATATYPE' control signal is shown for both 'swapping' blocks. The diagram is labeled 'MS19024V1' in the bottom right corner.
Block diagram of AES-CBC mode decryption. The process starts with 'IN FIFO ciphertext C' (128 bits) entering a 'swapping' block. The output of 'swapping' is 'I, 128 bits', which is input to the 'AEA, decrypt' block. The 'AEA, decrypt' block also receives a key 'K 0...3(3)' (128, 192, or 256 bits). The output of 'AEA, decrypt' is 'O, 128 bits'. This 'O' is XORed with the output of an 'IV=[IV1 IV0](2)' block (128 bits) at a summation node. The output of the summation node is 'Ps, 128 bits', which is input to another 'swapping' block. The output of this second 'swapping' block is 'P, 128 bits', which is output to the 'OUT FIFO plaintext P'. A dashed box indicates that 'I is written back into IV at the same time I as P is pushed into the OUT FIFO'. An 'AHB2 data write (before CRYP is enabled)' is shown to the 'IV' block. A 'DATATYPE' control signal is shown for both 'swapping' blocks. The diagram is labeled 'MS19024V1' in the bottom right corner.
  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].

AES counter mode (AES-CTR) mode

The AES counter mode uses the AES block as a key stream generator. The generated keys are then XORed with the plaintext to obtain the cipher. For this reason, it makes no sense to speak of different CTR encryption/decryption, since the two operations are exactly the same.

In fact, given:

The cipher is computed as follows:

\( C[i] = \text{enc}(\text{iv}[i]) \oplus P[i] \) , where:

\( \text{iv}[0] = \text{ICB} \) and \( \text{iv}[i+1] = \text{func}(\text{iv}[i]) \) , where func is an update function applied to the previous iv block; func is basically an increment of one of the fields composing the iv block.

Given that the ICB for decryption is the same as the one for encryption, the key stream generated during decryption is the same as the one generated during encryption. Then, the ciphertext is XORed with the key stream in order to retrieve the original plaintext. The decryption operation therefore acts exactly in the same way as the encryption operation.

Figure 204 and Figure 205 illustrate AES-CTR encryption and decryption, respectively.

Figure 204. AES-CTR mode encryption

Block diagram of AES-CTR mode encryption showing the flow from IN FIFO to OUT FIFO through various processing blocks including swapping, IV increment, AEA, and XOR.

The diagram illustrates the AES-CTR mode encryption process. The flow is as follows:

ai16073b

Block diagram of AES-CTR mode encryption showing the flow from IN FIFO to OUT FIFO through various processing blocks including swapping, IV increment, AEA, and XOR.
  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 205. 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, then to an XOR gate. The XOR gate also receives input from an AEA (encrypt) block. The AEA block takes a key (K0..3) and an input block (I) as input. The input block I is derived from an IV0...1(L/R) block, which is incremented by +1. A note indicates that (I + 1) is written back into IV at the same time P is pushed in OUT FIFO. The output of the XOR gate (Ps, 128 bits) is then passed through another swapping block to produce the final plaintext C, which is stored in an OUT FIFO. AHB2 data write (before CRYP is enabled) is shown as an input to the IV0...1(L/R) block. DATATYPE signals are shown for the swapping blocks. MS19025V1 is noted in the bottom right.
Block diagram of AES-CTR mode decryption. The diagram shows the flow of data from an IN FIFO (ciphertext P) through a swapping block, then to an XOR gate. The XOR gate also receives input from an AEA (encrypt) block. The AEA block takes a key (K0..3) and an input block (I) as input. The input block I is derived from an IV0...1(L/R) block, which is incremented by +1. A note indicates that (I + 1) is written back into IV at the same time P is pushed in OUT FIFO. The output of the XOR gate (Ps, 128 bits) is then passed through another swapping block to produce the final plaintext C, which is stored in an OUT FIFO. AHB2 data write (before CRYP is enabled) is shown as an input to the IV0...1(L/R) block. DATATYPE signals are shown for the swapping blocks. MS19025V1 is noted in the bottom right.
  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 206 shows the structure of the IV block as defined by the standard [2]. It is composed of three distinct fields.

Figure 206. Initial counter block structure for the Counter mode

Diagram showing the structure of the Initial counter block. It is a 128-bit block divided into three fields: Nonce (32 bits), Initialization vector (IV) (64 bits), and Counter (32 bits). ai16074 is noted in the bottom right.
Diagram showing the structure of the Initial counter block. It is a 128-bit block divided into three fields: Nonce (32 bits), Initialization vector (IV) (64 bits), and Counter (32 bits). ai16074 is noted in the bottom right.

The block increments the least significant 32 bits, while it leaves the other (most significant) 96 bits unchanged.

19.3.3 Data type

Data enter the CRYP processor 32 bits (word) at a time as they are written into the CRYP_DIN register. The principle of the DES is that streams of data are processed 64 bits by 64 bits and, for each 64-bit block, the bits are numbered from M1 to M64, with M1 the left-most bit and M64 the right-most bit of the block. The same principle is used for the AES, but with a 128-bit block size.

The system memory organization is little-endian: whatever the data type (bit, byte, 16-bit half-word, 32-bit word) used, the least-significant data occupy the lowest address locations. A bit, byte, or half-word swapping operation (depending on the kind of data to be encrypted) therefore has to be performed on the data read from the IN FIFO before they enter the CRYP processor. The same swapping operation should be performed on the CRYP data before they are written into the OUT FIFO. For example, the operation would be byte swapping for an ASCII text stream.

The kind of data to be processed is configured with the DATATYPE bitfield in the CRYP control register (CRYP_CR).

Table 74. Data types

DATATYPE in CRYP_CRSwapping performedSystem memory data (plaintext or cypher)
00bNo swapping

Example: TDES block value 0xABCD77206973FE01 is represented in system memory as:

TDES block size = 64bit = 2x 32 bit

Image: Diagram for 00b: A 64-bit TDES block value 0xABCD77206973FE01 is shown as two 32-bit words. An arrow points to system memory where the first 32-bit word 0xABCD7720 is at address @ and the second 32-bit word 0x6973FE01 is at address @+4.

system memory

0xABCD7720 @

0x6973FE01 @+4

01bHalf-word (16-bit) swapping

Example: TDES block value 0xABCD77206973FE01 is represented in system memory as:

TDES block size = 64bit = 2x 32 bit

Image: Diagram for 01b: A 64-bit TDES block value 0xABCD77206973FE01 is shown as four 16-bit half-words. An arrow points to system memory where the half-words are swapped: 0x7720ABCD is at address @ and 0xFE016973 is at address @+4.

system memory

0x7720ABCD @

0xFE016973 @+4

Table 74. Data types

DATATYPE in CRYP_CRSwapping performedSystem memory data (plaintext or cypher)
10bByte (8-bit) swapping

Example: TDES block value 0xABCD77206973FE01 is represented in system memory as:

TDES block size = 64bit = 2x 32 bit

Image: Diagram for 10b datatype showing byte swapping. Input: 0xAB CD 77 20 69 73 FE 01. Output in system memory: 0x 20 77 CD AB @ and 0x 01 FE 73 69 @+4.

11bBit swapping

TDES block value 0x4E6F772069732074 is represented in system memory as:

TDES Bloc size = 64bit = 2x 32 bit

Image: Diagram for 11b datatype showing bit swapping. Input: 0x4E 6F 77 20 69 73 20 74. Output in system memory: 0x04 EE F6 72 @, 0x2E 04 CE 96 @+4, 0000 0100 1110 1110 1111 0110 0111 0010 @, and 0010 1110 0000 0100 1100 1110 1001 0110 @+4.

Figure 207 shows how the 64-bit data block M1...64 is constructed from two consecutive 32-bit words popped off the IN FIFO by the CRYP processor, according to the DATATYPE value. The same schematic can easily be extended to form the 128-bit block for the AES cryptographic algorithm (for the AES, the block length is four 32-bit words, but swapping only takes place at word level, so it is identical to the one described here for the TDES).

Note: The same swapping is performed between the IN FIFO and the CRYP data block, and between the CRYP data block and the OUT FIFO.

Figure 207. 64-bit block construction according to DATATYPE

bit swapping operation

DATATYPE = 11b

IN FIFO

bit 31bit 30...bit 2bit 1bit 0
bit 31bit 30...bit 2bit 1bit 0

second word written into the CRYP_DIN register

first word written into the CRYP_DIN register

bit string

M1M2...M30M31M32M33M34...M62M63M64

byte swapping operation

DATATYPE = 10b

IN FIFO

Byte 1,3
bits 7..0
Byte 1,2
bits 7..0
Byte 1,1
bits 7..0
Byte 1,0
bits 7..0
Byte 0,3
bits 7..0
Byte 0,2
bits 7..0
Byte 0,1
bits 7..0
Byte 0,0
bits 7..0

second word written into the CRYP_DIN register

first word written into the CRYP_DIN register

(bit ordering within byte is unchanged)

bit string

Byte 0,0
M1...8
Byte 0,1
M9...16
Byte 0,2
M17...24
Byte 0,3
M25...32
Byte 1,0
M33...40
Byte 1,1
M41...48
Byte 1,2
M49...56
Byte 1,3
M57...64

half-word swapping operation

DATATYPE = 01b

IN FIFO

Half-word 1,1
bits 15..0
Half-word 1,0
bits 15..0
Half-word 0,1
bits 15..0
Half-word 0,0
bits 15..0

second word written into the CRYP_DIN register

first word written into the CRYP_DIN register

bit string

Half-word 0,0
M1...16
Half-word 0,1
M17...32
Half-word 1,0
M33...48
Half-word 1,1
M49...64

No swapping operation

DATATYPE = 00b

IN FIFO

Word 1
bits 31..0
Word 0
bits 31..0

second word written into the CRYP_DIN register

first word written into the CRYP_DIN register

bit string

Word 0
M1...32
Word 1
M33...64

ai16075

19.3.4 Initialization vectors - CRYP_IV0...1(L/R)

Initialization vectors are considered as two 64-bit data items. They therefore do not have the same data format and representation in system memory as plaintext or cypher data, and they are not affected by the DATATYPE value.

Initialization vectors are defined by two consecutive 32-bit words, CRYP_IVL (left part, noted as bits IV1...32) and CRYP_IVR (right part, noted as bits IV33...64).

During the DES or TDES CBC encryption, the CRYP_IV0(L/R) bits are XORed with the 64-bit data block popped off the IN FIFO after swapping (according to the DATATYPE value), that is, with the M1...64 bits of the data block. When the output of the DEA3 block is available, it is copied back into the CRYP_IV0(L/R) vector, and this new content is XORed with the next 64-bit data block popped off the IN FIFO, and so on.

During the DES or TDES CBC decryption, the CRYP_IV0(L/R) bits are XORed with the 64-bit data block (that is, with the M1...64 bits) delivered by the TDEA1 block before swapping (according to the DATATYPE value), and pushed into the OUT FIFO. When the XORed result is swapped and pushed into the OUT FIFO, the CRYP_IV0(L/R) value is replaced by the output of the IN FIFO, then the IN FIFO is popped, and a new 64-bit data block can be processed.

During the AES CBC encryption, the CRYP_IV0...1(L/R) bits are XORed with the 128-bit data block popped off the IN FIFO after swapping (according to the DATATYPE value). When the output of the AES core is available, it is copied back into the CRYP_IV0...1(L/R) vector, and this new content is XORed with the next 128-bit data block popped off the IN FIFO, and so on.

During the AES CBC decryption, the CRYP_IV0...1(L/R) bits are XORed with the 128-bit data block delivered by the AES core before swapping (according to the DATATYPE value) and pushed into the OUT FIFO. When the XORed result is swapped and pushed into the OUT FIFO, the CRYP_IV0...1(L/R) value is replaced by the output of the IN FIFO, then the IN FIFO is popped, and a new 128-bit data block can be processed.

During the AES CTR encryption or decryption, the CRYP_IV0...1(L/R) bits are encrypted by the AES core. Then the result of the encryption is XORed with the 128-bit data block popped off the IN FIFO after swapping (according to the DATATYPE value). When the XORed result is swapped and pushed into the OUT FIFO, the counter part of the CRYP_IV0...1(L/R) value (32 LSB) is incremented.

Any write operation to the CRYP_IV0...1(L/R) registers when bit BUSY = 1b in the CRYP_SR register is disregarded (CRYP_IV0...1(L/R) register content not modified). Thus, you must check that bit BUSY = 0b before modifying initialization vectors.

Figure 208. Initialization vectors use in the TDES-CBC encryption

Diagram illustrating the initialization vectors (IVs) use in TDES-CBC encryption. The diagram shows the flow of data from input words through the CRYP_DIN register, into the bit string (M1-M64), then through the CRYP_IVL and CRYP_IVR registers, and finally through the DEA (Triple Data Encryption Algorithm) blocks (Encrypt K1, Decrypt K2, Encrypt K3) to produce the ciphertext (I1-I64). The ciphertext is then copied back to the CRYP_IVL/R registers and also written to the OUT FIFO. The first word from the OUT FIFO contains the left part of the ciphertext block (O1...32), and the second word contains the right part (O33...64).

TDES-CBC encryption example, DATATYPE = 11b

bit string

second word written into the CRYP_DIN register

first word written into the CRYP_DIN register

CRYP_IVL

CRYP_IVR

DEA Encrypt, K1

DEA Decrypt, K2

DEA Encrypt, K3

CRYP result is copied back to the CRYP_IVL/R registers after cyphering

OUT FIFO

First word from the OUT FIFO contains the left part of cyphertext block (O1...32)

Second word from OUT FIFO contains the right part of cyphertext block (O33...64)

ai16076

Diagram illustrating the initialization vectors (IVs) use in TDES-CBC encryption. The diagram shows the flow of data from input words through the CRYP_DIN register, into the bit string (M1-M64), then through the CRYP_IVL and CRYP_IVR registers, and finally through the DEA (Triple Data Encryption Algorithm) blocks (Encrypt K1, Decrypt K2, Encrypt K3) to produce the ciphertext (I1-I64). The ciphertext is then copied back to the CRYP_IVL/R registers and also written to the OUT FIFO. The first word from the OUT FIFO contains the left part of the ciphertext block (O1...32), and the second word contains the right part (O33...64).

19.3.5 CRYP busy state

When there is enough data in the input FIFO (at least 2 words for the DES or TDES algorithm mode, 4 words for the AES algorithm mode) and enough free-space in the output FIFO (at least 2 (DES/TDES) or 4 (AES) word locations), and when the bit CRYPTEN = 1 in the CRYP_CR register, then the cryptographic processor automatically starts an encryption or decryption process (according to the value of the ALGODIR bit in the CRYP_CR register).

This process takes 48 AHB2 clock cycles for the Triple-DES algorithm, 16 AHB2 clock cycles for the simple DES algorithm, and 14, 16 or 18 AHB2 clock cycles for the AES with key lengths of 128, 192 or 256 bits, respectively. During the whole process, the BUSY bit in the CRYP_SR register is set to '1'. At the end of the process, two (DES/TDES) or four (AES) words are written by the CRYP Core into the output FIFO, and the BUSY bit is cleared. In

the CBC, CTR mode, the initialization vectors CRYP_IVx(L/R)R (x = 0..3) are updated as well.

A write operation to the key registers (CRYP_Kx(L/R)R, x = 0..3), the initialization registers (CRYP_IVx(L/R)R, x = 0..3), or to bits [9:2] in the CRYP_CR register are ignored when the cryptographic processor is busy (bit BUSY = 1b in the CRYP_SR register), and the registers are not modified. It is thus not possible to modify the configuration of the cryptographic processor while it is processing a block of data. It is however possible to clear the CRYPTEN bit while BUSY = 1, in which case the ongoing DES, TDES or AES processing is completed and the two/four word results are written into the output FIFO, and then, only then, the BUSY bit is cleared.

Note: When a block is being processed in the DES or TDES mode, if the output FIFO becomes full and if 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.

19.3.6 Procedure to perform an encryption or a decryption

Initialization

  1. 1. Initialize the peripheral (the order of operations is not important except for the key preparation for AES-ECB or AES-CBC decryption. The key size and the key value must be entered before preparing the key and the algorithm must be configured once the key has been prepared):
    1. a) Configure the key size (128-, 192- or 256-bit, in the AES only) with the KEYSIZE bits in the CRYP_CR register
    2. b) Write the symmetric key into the CRYP_KxL/R registers (2 to 8 registers to be written depending on the algorithm)
    3. c) Configure the data type (1-, 8-, 16- or 32-bit), with the DATATYPE bits in the CRYP_CR register
    4. d) In case of decryption in AES-ECB or AES-CBC, you must prepare the key: configure the key preparation mode by setting the ALGOMODE bits to '111' in the CRYP_CR register. Then write the CRYPTEN bit to '1': the BUSY bit is set. Wait until BUSY returns to 0 (CRYPTEN is automatically cleared as well): the key is prepared for decryption
    5. e) Configure the algorithm and chaining (the DES/TDES in ECB/CBC, the AES in ECB/CBC/CTR) with the ALGOMODE bits in the CRYP_CR register
    6. f) Configure the direction (encryption/decryption), with the ALGODIR bit in the CRYP_CR register
    7. g) Write the initialization vectors into the CRYP_IVxL/R register (in CBC or CTR modes only)
  2. 2. Flush the IN and OUT FIFOs by writing the FFLUSH bit to 1 in the CRYP_CR register

Processing when the DMA is used to transfer the data from/to the memory

  1. 1. Configure the DMA controller to transfer the input data from the memory. The transfer length is the length of the message. As message padding is not managed by the peripheral, the message length must be an entire number of blocks. The data are transferred in burst mode. The burst length is 4 words in the AES and 2 or 4 words in

the DES/TDES. The DMA should be configured to set an interrupt on transfer completion of the output data to indicate that the processing is finished.

  1. 2. Enable the cryptographic processor by writing the CRYPTEN bit to 1. Enable the DMA requests by setting the DIEN and DOEN bits in the CRYP_DMACR register.
  2. 3. 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 = 0.

Processing when the data are transferred by the CPU during interrupts

  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 the CRYPTEN bit in the CRYP_CR register.
  3. 3. In the interrupt managing the input data: load the input message into the IN FIFO. You can load 2 or 4 words at a time, or load data until the FIFO is full. When the last word of the message has been entered into the FIFO, disable the interrupt by clearing the INIM bit.
  4. 4. In the interrupt managing the output data: read the output message from the OUT FIFO. You can read 1 block (2 or 4 words) at a time or read data until the FIFO is empty. When the last word has been read, INIM=0, BUSY=0 and both FIFOs are empty (IFEM=1 and OFNE=0). You can disable the interrupt by clearing the OUTIM bit and, the peripheral by clearing the CRYPTEN bit.

Processing without using the DMA nor interrupts

  1. 1. Enable the cryptographic processor by setting the CRYPTEN bit in the CRYP_CR register.
  2. 2. Write the first blocks in the input FIFO (2 to 8 words).
  3. 3. Repeat the following sequence until the complete message has been processed:
    1. a) Wait for OFNE=1, then read the OUT-FIFO (1 block or until the FIFO is empty)
    2. b) Wait for IFNF=1, then write the IN FIFO (1 block or until the FIFO is full)
  4. 4. At the end of the processing, BUSY=0 and both FIFOs are empty (IFEM=1 and OFNE=0). You can disable the peripheral by clearing the CRYPTEN bit.

19.3.7 Context swapping

If a context switching is needed because a new task launched by the OS requires this resource, the following tasks have to be performed for full context restoration (example when the DMA is used):

Case of the AES and DES

  1. 1. Context saving
    1. a) Stop DMA transfers on the IN FIFO by clearing the DIEN bit in the CRYP_DMACR register.
    2. b) Wait until both the IN and OUT FIFOs are empty (IFEM=1 and OFNE=0 in the CRYP_SR register) and the BUSY bit is cleared.
    3. c) Stop DMA transfers on the OUT FIFO by writing the DOEN bit to 0 in the CRYP_DMACR register and clear the CRYPTEN bit.
    4. d) Save the current configuration (bits [9:2] and bits 19 in the CRYP_CR register) and, if not in ECB mode, the initialization vectors. The key value must already be available in the memory. When needed, save the DMA status (pointers for IN and OUT messages, number of remaining bytes, etc.).
  2. 2. Configure and execute the other processing.
  3. 3. Context restoration
    1. a) Configure the processor as in Section 19.3.6: Procedure to perform an encryption or a decryption on page 518, Initialization with the saved configuration. For the AES-ECB or AES-CBC decryption, the key must be prepared again.
    2. b) If needed, reconfigure the DMA controller to transfer the rest of the message.
    3. c) Enable the processor by setting the CRYPTEN bit and, the DMA requests by setting the DIEN and DOEN bits.

Case of the TDES

Context swapping can be done in the TDES in the same way as in the AES. But as the input FIFO can contain up to 4 unprocessed blocks and as the processing duration per block is higher, it can be faster in certain cases to interrupt the processing without waiting for the IN FIFO to be empty.

  1. 1. Context saving
    1. a) Stop DMA transfers on the IN FIFO by clearing the DIEN bit in the CRYP_DMACR register.
    2. b) Disable the processor by clearing the CRYPTEN bit (the processing stops at the end of the current block).
    3. c) Wait until the OUT FIFO is empty (OFNE=0 in the CRYP_SR register) and the BUSY bit is cleared.
    4. d) Stop DMA transfers on the OUT FIFO by writing the DOEN bit to 0 in the CRYP_DMACR register.
    5. e) Save the current configuration (bits [9:2] and bits 19 in the CRYP_CR register) and, if not in ECB mode, the initialization vectors. The key value must already be available in the memory. When needed, save the DMA status (pointers for IN and OUT messages, number of remaining bytes, etc.). Read back the data loaded in

the IN FIFO that have not been processed and save them in the memory until the FIFO is empty.

  1. 2. Configure and execute the other processing.
  2. 3. Context restoration
    1. a) Configure the processor as in Section 19.3.6: Procedure to perform an encryption or a decryption on page 518, Initialization with the saved configuration. For the AES-ECB or AES-CBC decryption, the key must be prepared again.
    2. b) Write the data that were saved during context saving into the IN FIFO.
    3. c) If needed, reconfigure the DMA controller to transfer the rest of the message.
    4. d) Enable the processor by setting the CRYPTEN bit and, the DMA requests by setting the DIEN and DOEN bits.

19.4 CRYP interrupts

There are two individual maskable interrupt sources generated by the CRYP. These two sources are combined into a single interrupt signal, which is the only interrupt signal from the CRYP that drives the NVIC (nested vectored interrupt controller). This combined interrupt, which is an OR function of the individual masked sources, is asserted if any of the individual interrupts listed below is asserted and enabled.

You can enable or disable the 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 interrupt sources can be read either from the CRYP_RISR register, for raw interrupt status, or from the CRYP_MISR register, for the masked interrupt status.

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, the interrupt follows the state of the OFNE (output FIFO not empty) flag).

The output FIFO service interrupt OUTMIS is NOT enabled with the CRYP enable bit. Consequently, disabling the CRYP 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.

Figure 209. CRYP interrupt mapping diagram

Figure 209. CRYP interrupt mapping diagram. Logic diagram showing how INRIS/INIM and OUTRIS/OUTIM signals are gated with CRYPEN to produce IN FIFO, OUT FIFO, and Global interrupts.
graph LR
    INRIS --> OR1[OR]
    INIM --> OR1
    OR1 -- INMIS --> AND1[AND]
    CRYPEN --> AND1
    AND1 --> OUT1[IN FIFO Interrupt - INMIS]

    OUTRIS --> OR2[OR]
    OUTIM --> OR2
    OR2 -- OUTMIS --> AND2[AND]
    CRYPEN --> AND2
    AND2 --> OUT2[OUT FIFO Interrupt - OUTMIS]

    AND1 --> OR3[OR]
    AND2 --> OR3
    OR3 --> OUT3[Global Interrupt]
  
Figure 209. CRYP interrupt mapping diagram. Logic diagram showing how INRIS/INIM and OUTRIS/OUTIM signals are gated with CRYPEN to produce IN FIFO, OUT FIFO, and Global interrupts.

19.5 CRYP DMA interface

The cryptographic processor provides an interface to connect to the DMA controller. The DMA operation is controlled through the CRYP DMA control register, CRYP_DMACR.

The burst and single transfer request signals are not mutually exclusive. They can both be asserted at the same time. For example, when there are 6 words available in the OUT FIFO, the burst transfer request and the single transfer request are asserted. After a burst transfer of 4 words, the single transfer request only is asserted to transfer the last 2 available words. This is useful for situations where the number of words left to be received in the stream is less than a burst.

Each request signal remains asserted until the relevant DMA clear signal is asserted. After the request clear signal is deasserted, a request signal can become active again, depending on the above described conditions. All request signals are deasserted if the CRYP 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).

Note: The DMA controller must be configured to perform burst of 4 words or less. Otherwise some data could be lost.

In order to let the DMA controller empty the OUT FIFO before filling up the IN FIFO, the OUTDMA channel should have a higher priority than the INDMA channel.

19.6 CRYP registers

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

19.6.1 CRYP control register (CRYP_CR)

Address offset: 0x00

Reset value: 0x0000 0000

31302928272625242322212019181716
Reserved
1514131211109876543210
CRYPENFFLUSHReservedKEYSIZEDATATYPEALGOMODE[2:0]ALGODIRRes.Res.
rwwrwrwrwrwrwrwrwrw

Bits 31:18 Reserved, must be kept at reset value

Bits 17:16 Reserved, must be kept at reset value

Bit 15 CRYPEN : Cryptographic processor enable

0: CRYP processor is disabled

1: CRYP processor is enabled

Note: The CRYPEN bit is automatically cleared by hardware when the key preparation process ends (ALGOMODE=111b) or GCM_CCM init Phase

Bit 14 FFLUSH : FIFO flush

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.

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

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

This bitfield defines the format of data entered in the CRYP_DIN register (refer to Section 19.3.3: Data type ).

00: 32-bit data. No swapping of 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.

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.

Bits 5:3 ALGOMODE[2:0] : Algorithm mode

000: TDES-ECB (triple-DES Electronic codebook): no feedback between blocks of data. Initialization vectors (CRYP_IV0(L/R)) are not used, three key vectors (K1, K2, and K3) are used (K0 is not used).

001: TDES-CBC (triple-DES Cipher block chaining): output block is XORed with the subsequent input block before its entry into the algorithm. Initialization vectors (CRYP_IV0(L/R)) must be initialized, three key vectors (K1, K2, and K3) are used (K0 is not used).

010: DES-ECB (simple DES Electronic codebook): no feedback between blocks of data. Initialization vectors (CRYP_IV0(L/R)) are not used, only one key vector (K1) is used (K0, K2, K3 are not used).

011: DES-CBC (simple DES Cipher block chaining): output block is XORed with the subsequent input block before its entry into the algorithm. Initialization vectors (CRYP_IV0(L/R)) must be initialized. Only one key vector (K1) is used (K0, K2, K3 are not used).

100: AES-ECB (AES Electronic codebook): no feedback between blocks of data. Initialization vectors (CRYP_IV0(L/R)...1L/R)) are not used. All four key vectors (K0...K3) are used.

101: AES-CBC (AES Cipher block chaining): output block is XORed with the subsequent input block before its entry into the algorithm. Initialization vectors (CRYP_IV0(L/R)...1L/R)) must be initialized. All four key vectors (K0...K3) are used.

110: AES-CTR (AES counter mode): output block is XORed with the subsequent input block before its entry into the algorithm. Initialization vectors (CRYP_IV0(L/R)...1L/R)) must be initialized. All four key vectors (K0...K3) are used. CTR decryption does not differ from CTR encryption, since the core always encrypts the current counter block to produce the key stream that is XORed with the plaintext or cipher in input. Thus, ALGODIR is don't care when ALGOMODE = 110b, and the key must NOT be unrolled (prepared) for decryption.

111: AES key preparation for decryption mode. Writing this value when CRYPTEN = 1 immediately starts an AES round for key preparation. The secret key must have previously been loaded into the K0...K3 registers. The BUSY bit in the CRYP_SR register is set during the key preparation. After key processing, the resulting key is copied back into the K0...K3 registers, and the BUSY bit is cleared.

Bit 2 ALGODIR : Algorithm direction

0: Encrypt

1: Decrypt

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

Note: Writing to the KEYSIZE, DATATYPE, ALGOMODE and ALGODIR bits while BUSY=1 has no effect. These bits can only be configured when BUSY=0. The 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 nonempty FIFO condition.

19.6.2 CRYP status register (CRYP_SR)

Address offset: 0x04

Reset value: 0x0000 0003

31302928272625242322212019181716
Reserved
1514131211109876543210
ReservedBUSYOFFUOFNEIFNFIFEM
rrrrrr

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 either that:

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

Bit 3 OFFU : Output FIFO full

0: Output FIFO is not full

1: Output FIFO is full

Bit 2 OFNE : Output FIFO not empty

0: Output FIFO is empty

1: Output FIFO is not empty

Bit 1 IFNF : Input FIFO not full

0: Input FIFO is full

1: Input FIFO is not full

Bit 0 IFEM : Input FIFO empty

0: Input FIFO is not empty

1: Input FIFO is empty

19.6.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 up to four 64-bit (TDES) or two 128-bit (AES) plaintext (when encrypting) or ciphertext (when decrypting) blocks into the input FIFO, one 32-bit word at a time.

The first word written into the FIFO is the MSB of the input block. The LSB of the input block is written at the end. Disregarding the data swapping, this gives:

To fit different data sizes, the data written in the CRYP_DIN register can be swapped before being processed by configuring the DATATYPE bits in the CRYP_CR register. Refer to Section 19.3.3: Data type on page 513 for more details.

When CRYP_DIN register is written to, the data are pushed into the input FIFO. When at least two 32-bit words in the DES/TDES mode (or four 32-bit words in the AES mode) have been pushed into the input FIFO, and when at least 2 words are free in the output FIFO, the CRYP engine starts an encrypting or decrypting process. This process takes two 32-bit words in the DES/TDES mode (or four 32-bit words in the AES mode) from the input FIFO and delivers two 32-bit words (or 4, respectively) to the output FIFO per process round.

When CRYP_DIN register is read:

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
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
DATAIN
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 DATAIN : Data input

Read = returns Input FIFO content if CRYPTEN = 0, else returns an undefined value.

Write = Input FIFO is written.

19.6.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 up to four 64-bit (TDES mode) or two 128-bit (AES mode) ciphertext (when encrypting) or plaintext (when decrypting) blocks from the output FIFO, one 32-bit word at a time.

Like for the input data, the MSB of the output block is the first word read from the output FIFO. The LSB of the output block is read at the end. Disregarding data swapping, this gives:

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 19.3.3: Data type on page 513 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
rrrrrrrrrrrrrrrr
1514131211109876543210
DATAOUT
rrrrrrrrrrrrrrrr

Bits 31:0 DATAOUT : Data output

Read = returns output FIFO content.

Write = No effect.

19.6.5 CRYP DMA control register (CRYP_DMACR)

Address offset: 0x10

Reset value: 0x0000 0000

31302928272625242322212019181716
Reserved
1514131211109876543210
ReservedDOENDIEN
rwrw

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

Bit 1 DOEN : DMA output enable

Bit 0 DIEN : DMA input enable

19.6.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. On a read operation, this register gives the current value of the mask on the relevant interrupt. Writing 1 to the particular bit sets the mask, 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
Reserved
1514131211109876543210
ReservedOUTIMINIM
rwrw

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

Bit 1 OUTIM : Output FIFO service interrupt mask

Bit 0 INIM : Input FIFO service interrupt mask

19.6.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. On a read, this register gives the current raw status of the corresponding interrupt prior to masking. A write has no effect.

31302928272625242322212019181716
Reserved
1514131211109876543210
ReservedOUTRISINRIS
rr

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

Bit 1 OUTRIS : Output FIFO service raw interrupt status

Gives the raw interrupt state prior to masking of the output FIFO service interrupt.

0: Raw interrupt not pending

1: Raw interrupt pending

Bit 0 INRIS : Input FIFO service raw interrupt status

Gives the raw interrupt state prior to masking of the Input FIFO service interrupt.

0: Raw interrupt not pending

1: Raw interrupt pending

19.6.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. On a read, this register gives the current masked status of the corresponding interrupt prior to masking. A write has no effect.

31302928272625242322212019181716
Reserved
1514131211109876543210
ReservedOUTMISINMIS
rr

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

Bit 1 OUTMIS : Output FIFO service masked interrupt status

Gives the interrupt state after masking of the output FIFO service interrupt.
0: Interrupt not pending
1: Interrupt pending

Bit 0 INMIS : Input FIFO service masked interrupt status

Gives the interrupt state after masking of the input FIFO service interrupt.
0: Interrupt not pending
1: Interrupt pending when CRYPEN = 1

19.6.9 CRYP key registers (CRYP_K0...3(L/R)R)

Address offset: 0x20 to 0x3C
Reset value: 0x0000 0000

These registers contain the cryptographic keys.

In the TDES mode, keys are 64-bit binary values (number from left to right, that is the leftmost bit is bit 1), named K1, K2 and K3 (K0 is not used), each key consists of 56 information bits and 8 parity bits. The parity bits are reserved for error detection purposes and are not used by the current block. Thus, bits 8, 16, 24, 32, 40, 48, 56 and 64 of each 64-bit key value Kx[1:64] are not used.

In the AES mode, the key is considered as a single 128-, 192- or 256-bit long bit sequence, \( k_0k_1k_2...k_{127/191/255} \) ( \( k_0 \) being the leftmost bit). The AES key is entered into the registers as follows:

In any case \( b_0 \) is the rightmost bit.

CRYP_K0LR (address offset: 0x20)

31302928272625242322212019181716
b255b254b253b252b251b250b249b248b247b246b245b244b243b242b241b240
wwwwwwwwwwwwwwww
1514131211109876543210
b239b238b237b236b235b234b233b232b231b230b229b228b227b226b225b224
wwwwwwwwwwwwwwww

CRYP_K0RR (address offset: 0x24)

31302928272625242322212019181716
b223b222b221b220b219b218b217b216b215b214b213b212b211b210b209b208
wwwwwwwwwwwwwwww
1514131211109876543210
b207b206b205b204b203b202b201b200b199b198b197b196b195b194b193b192
wwwwwwwwwwwwwwww
CRYP_K1LR (address offset: 0x28)
31302928272625242322212019181716
k1.1
b191
k1.2
b190
k1.3
b189
k1.4
b188
k1.5
b187
k1.6
b186
k1.7
b185
k1.8
b184
k1.9
b183
k1.10
b182
k1.11
b181
k1.12
b180
k1.13
b179
k1.14
b178
k1.15
b177
k1.16
b176
wwwwwwwwwwwwwwww
1514131211109876543210
k1.17
b175
k1.18
b174
k1.19
b173
k1.20
b172
k1.21
b171
k1.22
b170
k1.23
b169
k1.24
b168
k1.25
b167
k1.26
b166
k1.27
b165
k1.28
b164
k1.29
b163
k1.30
b162
k1.31
b161
k1.32
b160
wwwwwwwwwwwwwwww
CRYP_K1RR (address offset: 0x2C)
31302928272625242322212019181716
k1.33
b159
k1.34
b158
k1.35
b157
k1.36
b156
k1.37
b155
k1.38
b154
k1.39
b153
k1.40
b152
k1.41
b151
k1.42
b150
k1.43
b149
k1.44
b148
k1.45
b147
k1.46
b146
k1.47
b145
k1.48
b144
wwwwwwwwwwwwwwww
1514131211109876543210
k1.49
b143
k1.50
b142
k1.51
b141
k1.52
b140
k1.53
b139
k1.54
b138
k1.55
b137
k1.56
b136
k1.57
b135
k1.58
b134
k1.59
b133
k1.60
b132
k1.61
b131
k1.62
b130
k1.63
b129
k1.64
b128
wwwwwwwwwwwwwwww
CRYP_K2LR (address offset: 0x30)
31302928272625242322212019181716
k2.1
b127
k2.2
b126
k2.3
b125
k2.4
b124
k2.5
b123
k2.6
b122
k2.7
b121
k2.8
b120
k2.9
b119
k2.10
b118
k2.11
b117
k2.12
b116
k2.13
b115
k2.14
b114
k2.15
b113
k2.16
b112
wwwwwwwwwwwwwwww
1514131211109876543210
k2.17
b111
k2.18
b110
k2.19
b109
k2.20
b108
k2.21
b107
k2.22
b106
k2.23
b105
k2.24
b104
k2.25
b103
k2.26
b102
k2.27
b101
k2.28
b100
k2.29
b99
k2.30
b98
k2.31
b97
k2.32
b96
wwwwwwwwwwwwwwww
CRYP_K2RR (address offset: 0x34)
31302928272625242322212019181716
k2.33
b95
k2.34
b94
k2.35
b93
k2.36
b92
k2.37
b91
k2.38
b90
k2.39
b89
k2.40
b88
k2.41
b87
k2.42
b86
k2.43
b85
k2.44
b84
k2.45
b83
k2.46
b82
k2.47
b81
k2.48
b80
wwwwwwwwwwwwwwww
1514131211109876543210
k2.49
b79
k2.50
b78
k2.51
b77
k2.52
b76
k2.53
b75
k2.54
b74
k2.55
b73
k2.56
b72
k2.57
b71
k2.58
b70
k2.59
b69
k2.60
b68
k2.61
b67
k2.62
b66
k2.63
b65
k2.64
b64
wwwwwwwwwwwwwwww
CRYP_K3LR (address offset: 0x38)
31302928272625242322212019181716
k3.1
b63
k3.2
b62
k3.3
b61
k3.4
b60
k3.5
b59
k3.6
b58
k3.7
b57
k3.8
b56
k3.9
b55
k3.10
b54
k3.11
b53
k3.12
b52
k3.13
b51
k3.14
b50
k3.15
b49
k3.16
b48
wwwwwwwwwwwwwwww
1514131211109876543210
k3.17
b47
k3.18
b46
k3.19
b45
k3.20
b44
k3.21
b43
k3.22
b42
k3.23
b41
k3.24
b40
k3.25
b39
k3.26
b38
k3.27
b37
k3.28
b36
k3.29
b35
k3.30
b34
k3.31
b33
k3.32
b32
wwwwwwwwwwwwwwww
CRYP_K3RR (address offset: 0x3C)
31302928272625242322212019181716
k3.33
b31
k3.34
b30
k3.35
b29
k3.36
b28
k3.37
b27
k3.38
b26
k3.39
b25
k3.40
b24
k3.41
b23
k3.42
b22
k3.43
b21
k3.44
b20
k3.45
b19
k3.46
b18
k3.47
b17
k3.48
b16
wwwwwwwwwwwwwwww
1514131211109876543210
k3.49
b15
k3.50
b14
k3.51
b13
k3.52
b12
k3.53
b11
k3.54
b10
k3.55
b9
k3.56
b8
k3.57
b7
k3.58
b6
k3.59
b5
k3.60
b4
k3.61
b3
k3.62
b2
k3.63
b1
k3.64
b0
wwwwwwwwwwwwwwww

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

19.6.10 CRYP initialization vector registers (CRYP_IV0...1(L/R)R)

Address offset: 0x40 to 0x4C

Reset value: 0x0000 0000

The CRYP_IV0...1(L/R)R are the left-word and right-word registers for the initialization vector (64 bits for DES/TDES and 128 bits for AES) and are used in the CBC (Cipher block chaining) and Counter (CTR) modes. After each computation round of the TDES or AES Core, the CRYP_IV0...1(L/R)R registers are updated as described in Section : DES and TDES Cipher block chaining (DES/TDES-CBC) mode on page 504 , Section : AES Cipher block chaining (AES-CBC) mode on page 508 and Section : AES counter mode (AES-CTR) mode on page 510 .

IV0 is the leftmost bit whereas IV63 (DES, TDES) or IV127 (AES) are the rightmost bits of the initialization vector. IV1(L/R)R is used only in the AES.

CRYP_IV0LR (address offset: 0x40)
31302928272625242322212019181716
IV0IV1IV2IV3IV4IV5IV6IV7IV8IV9IV10IV11IV12IV13IV14IV15
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
IV16IV17IV18IV19IV20IV21IV22IV23IV24IV25IV26IV27IV28IV29IV30IV31
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
CRYP_IV0RR (address offset: 0x44)
31302928272625242322212019181716
IV32IV33IV34IV35IV36IV37IV38IV39IV40IV41IV42IV43IV44IV45IV46IV47
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
IV48IV49IV50IV51IV52IV53IV54IV55IV56IV57IV58IV59IV60IV61IV62IV63
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
CRYP_IV1LR (address offset: 0x48)
31302928272625242322212019181716
IV64IV65IV66IV67IV68IV69IV70IV71IV72IV73IV74IV75IV76IV77IV78IV79
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
IV80IV81IV82IV83IV84IV85IV86IV87IV88IV89IV90IV91IV92IV93IV94IV95
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
CRYP_IV1RR (address offset: 0x4C)
31302928272625242322212019181716
IV96IV97IV98IV99IV100IV101IV102IV103IV104IV105IV106IV107IV108IV109IV110IV111
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
IV112IV113IV114IV115IV116IV117IV118IV119IV120IV121IV122IV123IV124IV125IV126IV127
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Note: In DES/3DES modes, only CRYP_IV0(L/R) is used.

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

19.6.11 CRYP register map Table 75. CRYP register map and reset values
OffsetRegister name and reset value313029282726252423222120191817161514131211109876543210
0x00
0x00
CRYP_CRReservedCRYPENFFLUSHReservedKEYSIZEDATATYPEALOMODE[2:0]ALGODIRRes.
Reset value000000000000
0x04CRYP_SRReservedBUSYOFFUOFNEIFNFIFEM
Reset value00011
0x08CRYP_DINDATAIN
Reset value00000000000000000000000000000000
0x0CCRYP_DOUTDATAOUT
Reset value00000000000000000000000000000000
0x10CRYP_DMACRReservedDOENDIEN
Reset value00

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

OffsetRegister name and reset value313029282726252423222120191817161514131211109876543210
0x14CRYP_IMSC
R
ReservedOUTIM
Reset valueINIM
0x18CRYP_RISRReservedOUTRIS
Reset valueINRIS
0x1CCRYP_MISRReservedOUTMIS
Reset valueIN%IS
0x20CRYP_K0LRCRYP_K0LR
Reset value000000000000000000000000000000
0x24CRYP_K0RRCRYP_K0RR
Reset value000000000000000000000000000000
...
0x38CRYP_K3LRCRYP_K3LR
Reset value000000000000000000000000000000
0x3CCRYP_K3RRCRYP_K3RR
Reset value000000000000000000000000000000
0x40CRYP_IV0LRCRYP_IV0LR
Reset value000000000000000000000000000000
0x44CRYP_IV0RRCRYP_IV0RR
Reset value000000000000000000000000000000
0x48CRYP_IV1LRCRYP_IV1LR
Reset value000000000000000000000000000000
0x4CCRYP_IV1RRCRYP_IV1RR
Reset value000000000000000000000000000000

Refer to Section 2.3: Memory map for the register boundary addresses.