CTR mode encryption of N blocks of plaintext into N blocks of ciphertext without CPU interaction is achieved through the use of 2 DMA channels. To implement CTR encryption follow these steps:
- Configure Output DMA channel for saving ciphertext:
- Set DMA channel trigger selection to AES Trig1
- Set DMA channel source address to DATA_OUT
- Set DMA channel destination address to location where ciphertext is to be stored (for example, SRAM)
- Set DMA channel transfer size to N∗4
- Set DMA channel mode to single transfer mode
- In the AES event registers, unmask Trig1 in the IMASK register of DMA_TRIG_DATAOUT
- Configure Input DMA channel for loading plaintext:
- Set DMA channel trigger selection to AES Trig0
- Set DMA channel source address to location where plaintext is stored (for example, SRAM)
- Set DMA channel destination address to DATA_IN
- Set DMA channel transfer size to N∗4
- Set DMA channel mode to single transfer mode
- In the AES event registers, unmask Trig0 in the IMASK register of DMA_TRIG0
- Configure and enable the DMA interrupt for the Output DMA channel in the DMA controller
- Configure DMA_HS for DMA based handshake: set DMA_HS[DMA_DATA_ACK] = 1
- Load key as described in Section 11.2.1
- Load Initialization vector (IV) by writing to IV0, IV1, IV2 and IV3 registers
- Configure the CTRL register for block cipher encryption mode for CTR
- Select key size via CTRL[KEY_SIZ]
- Select Direction for Encryption by CTRL[DIR] = 1
- Select CTR mode by setting CTRL[CTR]=1
- Select CTR width by setting CTRL[CTR_WIDTH]
- 00b -CTR32
- 01b -CTR64
- 10b -CTR96
- 11b -CTR128
- Start encryption by writing number of bytes N×4 to AES C_LENGTH_0 and C_LENGTH_1 registers
- Wait for the DMA channel interrupt which indicates completion of the entire operation. The ciphertext output will be stored in the location configured in step 1c.